186d7f5d3SJohn Marino /*
286d7f5d3SJohn Marino * Copyright 1994, 1995 Massachusetts Institute of Technology
386d7f5d3SJohn Marino *
486d7f5d3SJohn Marino * Permission to use, copy, modify, and distribute this software and
586d7f5d3SJohn Marino * its documentation for any purpose and without fee is hereby
686d7f5d3SJohn Marino * granted, provided that both the above copyright notice and this
786d7f5d3SJohn Marino * permission notice appear in all copies, that both the above
886d7f5d3SJohn Marino * copyright notice and this permission notice appear in all
986d7f5d3SJohn Marino * supporting documentation, and that the name of M.I.T. not be used
1086d7f5d3SJohn Marino * in advertising or publicity pertaining to distribution of the
1186d7f5d3SJohn Marino * software without specific, written prior permission. M.I.T. makes
1286d7f5d3SJohn Marino * no representations about the suitability of this software for any
1386d7f5d3SJohn Marino * purpose. It is provided "as is" without express or implied
1486d7f5d3SJohn Marino * warranty.
1586d7f5d3SJohn Marino *
1686d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
1786d7f5d3SJohn Marino * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
1886d7f5d3SJohn Marino * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1986d7f5d3SJohn Marino * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
2086d7f5d3SJohn Marino * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2186d7f5d3SJohn Marino * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2286d7f5d3SJohn Marino * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2386d7f5d3SJohn Marino * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2486d7f5d3SJohn Marino * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2586d7f5d3SJohn Marino * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2686d7f5d3SJohn Marino * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2786d7f5d3SJohn Marino * SUCH DAMAGE.
2886d7f5d3SJohn Marino *
2986d7f5d3SJohn Marino * $FreeBSD: src/share/examples/find_interface/find_interface.c,v 1.5 1999/08/28 00:19:18 peter Exp $
3086d7f5d3SJohn Marino * $DragonFly: src/share/examples/find_interface/find_interface.c,v 1.2 2003/06/17 04:36:57 dillon Exp $
3186d7f5d3SJohn Marino */
3286d7f5d3SJohn Marino
3386d7f5d3SJohn Marino /*
3486d7f5d3SJohn Marino * This is a simple program which demonstrates how to query the kernel
3586d7f5d3SJohn Marino * routing mechanism using only a UDP socket. Pass it a hostname on
3686d7f5d3SJohn Marino * the command line (sorry, it doesn't parse dotted decimal) and it will
3786d7f5d3SJohn Marino * print out an IP address which names the interface over which UDP
3886d7f5d3SJohn Marino * packets intended for that destination would be sent.
3986d7f5d3SJohn Marino * A more sophisticated program might use the list obtained from SIOCGIFCONF
4086d7f5d3SJohn Marino * to match the address with an interface name, but applications programmers
4186d7f5d3SJohn Marino * much more often need to know the address of the interface rather than
4286d7f5d3SJohn Marino * the name.
4386d7f5d3SJohn Marino */
4486d7f5d3SJohn Marino #include <sys/types.h>
4586d7f5d3SJohn Marino #include <sys/socket.h>
4686d7f5d3SJohn Marino #include <unistd.h>
4786d7f5d3SJohn Marino #include <netinet/in.h>
4886d7f5d3SJohn Marino #include <arpa/inet.h>
4986d7f5d3SJohn Marino #include <stdlib.h>
5086d7f5d3SJohn Marino #include <stdio.h>
5186d7f5d3SJohn Marino #include <netdb.h>
5286d7f5d3SJohn Marino #include <err.h>
5386d7f5d3SJohn Marino #include <errno.h>
5486d7f5d3SJohn Marino #include <string.h>
5586d7f5d3SJohn Marino #include <sysexits.h>
5686d7f5d3SJohn Marino
5786d7f5d3SJohn Marino int
main(int argc,char ** argv)5886d7f5d3SJohn Marino main(int argc, char **argv)
5986d7f5d3SJohn Marino {
6086d7f5d3SJohn Marino struct sockaddr_in local, remote;
6186d7f5d3SJohn Marino struct hostent *hp;
6286d7f5d3SJohn Marino int s, rv, namelen;
6386d7f5d3SJohn Marino
6486d7f5d3SJohn Marino argc--, argv++;
6586d7f5d3SJohn Marino
6686d7f5d3SJohn Marino if (!*argv) {
6786d7f5d3SJohn Marino errx(EX_USAGE, "must supply a hostname");
6886d7f5d3SJohn Marino }
6986d7f5d3SJohn Marino
7086d7f5d3SJohn Marino hp = gethostbyname(*argv);
7186d7f5d3SJohn Marino if (!hp) {
7286d7f5d3SJohn Marino errx(EX_NOHOST, "cannot resolve hostname: %s", *argv);
7386d7f5d3SJohn Marino }
7486d7f5d3SJohn Marino
7586d7f5d3SJohn Marino memcpy(&remote.sin_addr, hp->h_addr_list[0], sizeof remote.sin_addr);
7686d7f5d3SJohn Marino remote.sin_port = htons(60000);
7786d7f5d3SJohn Marino remote.sin_family = AF_INET;
7886d7f5d3SJohn Marino remote.sin_len = sizeof remote;
7986d7f5d3SJohn Marino
8086d7f5d3SJohn Marino local.sin_addr.s_addr = htonl(INADDR_ANY);
8186d7f5d3SJohn Marino local.sin_port = htons(60000);
8286d7f5d3SJohn Marino local.sin_family = AF_INET;
8386d7f5d3SJohn Marino local.sin_len = sizeof local;
8486d7f5d3SJohn Marino
8586d7f5d3SJohn Marino s = socket(PF_INET, SOCK_DGRAM, 0);
8686d7f5d3SJohn Marino if (s < 0)
8786d7f5d3SJohn Marino err(EX_OSERR, "socket");
8886d7f5d3SJohn Marino
8986d7f5d3SJohn Marino do {
9086d7f5d3SJohn Marino rv = bind(s, (struct sockaddr *)&local, sizeof local);
9186d7f5d3SJohn Marino local.sin_port = htons(ntohs(local.sin_port) + 1);
9286d7f5d3SJohn Marino } while(rv < 0 && errno == EADDRINUSE);
9386d7f5d3SJohn Marino
9486d7f5d3SJohn Marino if (rv < 0)
9586d7f5d3SJohn Marino err(EX_OSERR, "bind");
9686d7f5d3SJohn Marino
9786d7f5d3SJohn Marino do {
9886d7f5d3SJohn Marino rv = connect(s, (struct sockaddr *)&remote, sizeof remote);
9986d7f5d3SJohn Marino remote.sin_port = htons(ntohs(remote.sin_port) + 1);
10086d7f5d3SJohn Marino } while(rv < 0 && errno == EADDRINUSE);
10186d7f5d3SJohn Marino
10286d7f5d3SJohn Marino if (rv < 0)
10386d7f5d3SJohn Marino err(EX_OSERR, "connect");
10486d7f5d3SJohn Marino
10586d7f5d3SJohn Marino namelen = sizeof local;
10686d7f5d3SJohn Marino rv = getsockname(s, (struct sockaddr *)&local, &namelen);
10786d7f5d3SJohn Marino if (rv < 0)
10886d7f5d3SJohn Marino err(EX_OSERR, "getsockname");
10986d7f5d3SJohn Marino
11086d7f5d3SJohn Marino printf("Route to %s is out %s\n", *argv, inet_ntoa(local.sin_addr));
11186d7f5d3SJohn Marino return 0;
11286d7f5d3SJohn Marino }
113