xref: /dflybsd-src/share/examples/find_interface/find_interface.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*
2*86d7f5d3SJohn Marino  * Copyright 1994, 1995 Massachusetts Institute of Technology
3*86d7f5d3SJohn Marino  *
4*86d7f5d3SJohn Marino  * Permission to use, copy, modify, and distribute this software and
5*86d7f5d3SJohn Marino  * its documentation for any purpose and without fee is hereby
6*86d7f5d3SJohn Marino  * granted, provided that both the above copyright notice and this
7*86d7f5d3SJohn Marino  * permission notice appear in all copies, that both the above
8*86d7f5d3SJohn Marino  * copyright notice and this permission notice appear in all
9*86d7f5d3SJohn Marino  * supporting documentation, and that the name of M.I.T. not be used
10*86d7f5d3SJohn Marino  * in advertising or publicity pertaining to distribution of the
11*86d7f5d3SJohn Marino  * software without specific, written prior permission.  M.I.T. makes
12*86d7f5d3SJohn Marino  * no representations about the suitability of this software for any
13*86d7f5d3SJohn Marino  * purpose.  It is provided "as is" without express or implied
14*86d7f5d3SJohn Marino  * warranty.
15*86d7f5d3SJohn Marino  *
16*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17*86d7f5d3SJohn Marino  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18*86d7f5d3SJohn Marino  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*86d7f5d3SJohn Marino  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20*86d7f5d3SJohn Marino  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21*86d7f5d3SJohn Marino  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22*86d7f5d3SJohn Marino  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23*86d7f5d3SJohn Marino  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24*86d7f5d3SJohn Marino  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25*86d7f5d3SJohn Marino  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26*86d7f5d3SJohn Marino  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*86d7f5d3SJohn Marino  * SUCH DAMAGE.
28*86d7f5d3SJohn Marino  *
29*86d7f5d3SJohn Marino  * $FreeBSD: src/share/examples/find_interface/find_interface.c,v 1.5 1999/08/28 00:19:18 peter Exp $
30*86d7f5d3SJohn Marino  * $DragonFly: src/share/examples/find_interface/find_interface.c,v 1.2 2003/06/17 04:36:57 dillon Exp $
31*86d7f5d3SJohn Marino  */
32*86d7f5d3SJohn Marino 
33*86d7f5d3SJohn Marino /*
34*86d7f5d3SJohn Marino  * This is a simple program which demonstrates how to query the kernel
35*86d7f5d3SJohn Marino  * routing mechanism using only a UDP socket.  Pass it a hostname on
36*86d7f5d3SJohn Marino  * the command line (sorry, it doesn't parse dotted decimal) and it will
37*86d7f5d3SJohn Marino  * print out an IP address which names the interface over which UDP
38*86d7f5d3SJohn Marino  * packets intended for that destination would be sent.
39*86d7f5d3SJohn Marino  * A more sophisticated program might use the list obtained from SIOCGIFCONF
40*86d7f5d3SJohn Marino  * to match the address with an interface name, but applications programmers
41*86d7f5d3SJohn Marino  * much more often need to know the address of the interface rather than
42*86d7f5d3SJohn Marino  * the name.
43*86d7f5d3SJohn Marino  */
44*86d7f5d3SJohn Marino #include <sys/types.h>
45*86d7f5d3SJohn Marino #include <sys/socket.h>
46*86d7f5d3SJohn Marino #include <unistd.h>
47*86d7f5d3SJohn Marino #include <netinet/in.h>
48*86d7f5d3SJohn Marino #include <arpa/inet.h>
49*86d7f5d3SJohn Marino #include <stdlib.h>
50*86d7f5d3SJohn Marino #include <stdio.h>
51*86d7f5d3SJohn Marino #include <netdb.h>
52*86d7f5d3SJohn Marino #include <err.h>
53*86d7f5d3SJohn Marino #include <errno.h>
54*86d7f5d3SJohn Marino #include <string.h>
55*86d7f5d3SJohn Marino #include <sysexits.h>
56*86d7f5d3SJohn Marino 
57*86d7f5d3SJohn Marino int
main(int argc,char ** argv)58*86d7f5d3SJohn Marino main(int argc, char **argv)
59*86d7f5d3SJohn Marino {
60*86d7f5d3SJohn Marino 	struct sockaddr_in local, remote;
61*86d7f5d3SJohn Marino 	struct hostent *hp;
62*86d7f5d3SJohn Marino 	int s, rv, namelen;
63*86d7f5d3SJohn Marino 
64*86d7f5d3SJohn Marino 	argc--, argv++;
65*86d7f5d3SJohn Marino 
66*86d7f5d3SJohn Marino 	if (!*argv) {
67*86d7f5d3SJohn Marino 		errx(EX_USAGE, "must supply a hostname");
68*86d7f5d3SJohn Marino 	}
69*86d7f5d3SJohn Marino 
70*86d7f5d3SJohn Marino 	hp = gethostbyname(*argv);
71*86d7f5d3SJohn Marino 	if (!hp) {
72*86d7f5d3SJohn Marino 		errx(EX_NOHOST, "cannot resolve hostname: %s", *argv);
73*86d7f5d3SJohn Marino 	}
74*86d7f5d3SJohn Marino 
75*86d7f5d3SJohn Marino 	memcpy(&remote.sin_addr, hp->h_addr_list[0], sizeof remote.sin_addr);
76*86d7f5d3SJohn Marino 	remote.sin_port = htons(60000);
77*86d7f5d3SJohn Marino 	remote.sin_family = AF_INET;
78*86d7f5d3SJohn Marino 	remote.sin_len = sizeof remote;
79*86d7f5d3SJohn Marino 
80*86d7f5d3SJohn Marino 	local.sin_addr.s_addr = htonl(INADDR_ANY);
81*86d7f5d3SJohn Marino 	local.sin_port = htons(60000);
82*86d7f5d3SJohn Marino 	local.sin_family = AF_INET;
83*86d7f5d3SJohn Marino 	local.sin_len = sizeof local;
84*86d7f5d3SJohn Marino 
85*86d7f5d3SJohn Marino 	s = socket(PF_INET, SOCK_DGRAM, 0);
86*86d7f5d3SJohn Marino 	if (s < 0)
87*86d7f5d3SJohn Marino 		err(EX_OSERR, "socket");
88*86d7f5d3SJohn Marino 
89*86d7f5d3SJohn Marino 	do {
90*86d7f5d3SJohn Marino 		rv = bind(s, (struct sockaddr *)&local, sizeof local);
91*86d7f5d3SJohn Marino 		local.sin_port = htons(ntohs(local.sin_port) + 1);
92*86d7f5d3SJohn Marino 	} while(rv < 0 && errno == EADDRINUSE);
93*86d7f5d3SJohn Marino 
94*86d7f5d3SJohn Marino 	if (rv < 0)
95*86d7f5d3SJohn Marino 		err(EX_OSERR, "bind");
96*86d7f5d3SJohn Marino 
97*86d7f5d3SJohn Marino 	do {
98*86d7f5d3SJohn Marino 		rv = connect(s, (struct sockaddr *)&remote, sizeof remote);
99*86d7f5d3SJohn Marino 		remote.sin_port = htons(ntohs(remote.sin_port) + 1);
100*86d7f5d3SJohn Marino 	} while(rv < 0 && errno == EADDRINUSE);
101*86d7f5d3SJohn Marino 
102*86d7f5d3SJohn Marino 	if (rv < 0)
103*86d7f5d3SJohn Marino 		err(EX_OSERR, "connect");
104*86d7f5d3SJohn Marino 
105*86d7f5d3SJohn Marino 	namelen = sizeof local;
106*86d7f5d3SJohn Marino 	rv = getsockname(s, (struct sockaddr *)&local, &namelen);
107*86d7f5d3SJohn Marino 	if (rv < 0)
108*86d7f5d3SJohn Marino 		err(EX_OSERR, "getsockname");
109*86d7f5d3SJohn Marino 
110*86d7f5d3SJohn Marino 	printf("Route to %s is out %s\n", *argv, inet_ntoa(local.sin_addr));
111*86d7f5d3SJohn Marino 	return 0;
112*86d7f5d3SJohn Marino }
113