Path: blob/main/share/examples/find_interface/find_interface.c
39485 views
/*1* Copyright 1994, 1995 Massachusetts Institute of Technology2*3* Permission to use, copy, modify, and distribute this software and4* its documentation for any purpose and without fee is hereby5* granted, provided that both the above copyright notice and this6* permission notice appear in all copies, that both the above7* copyright notice and this permission notice appear in all8* supporting documentation, and that the name of M.I.T. not be used9* in advertising or publicity pertaining to distribution of the10* software without specific, written prior permission. M.I.T. makes11* no representations about the suitability of this software for any12* purpose. It is provided "as is" without express or implied13* warranty.14*15* THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS16* ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,17* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF18* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT19* SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,20* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT21* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF22* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND23* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,24* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT25* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* This is a simple program which demonstrates how to query the kernel31* routing mechanism using only a UDP socket. Pass it a hostname on32* the command line (sorry, it doesn't parse dotted decimal) and it will33* print out an IP address which names the interface over which UDP34* packets intended for that destination would be sent.35* A more sophisticated program might use the list obtained from SIOCGIFCONF36* to match the address with an interface name, but applications programmers37* much more often need to know the address of the interface rather than38* the name.39*/40#include <sys/types.h>41#include <sys/socket.h>42#include <unistd.h>43#include <netinet/in.h>44#include <arpa/inet.h>45#include <stdlib.h>46#include <stdio.h>47#include <netdb.h>48#include <err.h>49#include <errno.h>50#include <string.h>51#include <sysexits.h>5253int54main(int argc, char **argv)55{56struct sockaddr_in local, remote;57struct hostent *hp;58int s, rv, namelen;5960argc--, argv++;6162if (!*argv) {63errx(EX_USAGE, "must supply a hostname");64}6566hp = gethostbyname(*argv);67if (!hp) {68errx(EX_NOHOST, "cannot resolve hostname: %s", *argv);69}7071memcpy(&remote.sin_addr, hp->h_addr_list[0], sizeof remote.sin_addr);72remote.sin_port = htons(60000);73remote.sin_family = AF_INET;74remote.sin_len = sizeof remote;7576local.sin_addr.s_addr = htonl(INADDR_ANY);77local.sin_port = htons(60000);78local.sin_family = AF_INET;79local.sin_len = sizeof local;8081s = socket(PF_INET, SOCK_DGRAM, 0);82if (s < 0)83err(EX_OSERR, "socket");8485do {86rv = bind(s, (struct sockaddr *)&local, sizeof local);87local.sin_port = htons(ntohs(local.sin_port) + 1);88} while(rv < 0 && errno == EADDRINUSE);8990if (rv < 0)91err(EX_OSERR, "bind");9293do {94rv = connect(s, (struct sockaddr *)&remote, sizeof remote);95remote.sin_port = htons(ntohs(remote.sin_port) + 1);96} while(rv < 0 && errno == EADDRINUSE);9798if (rv < 0)99err(EX_OSERR, "connect");100101namelen = sizeof local;102rv = getsockname(s, (struct sockaddr *)&local, &namelen);103if (rv < 0)104err(EX_OSERR, "getsockname");105106printf("Route to %s is out %s\n", *argv, inet_ntoa(local.sin_addr));107return 0;108}109110111