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 * From:
30*86d7f5d3SJohn Marino * Id: find_interface.c,v 1.1 1995/08/14 16:08:39 wollman Exp
31*86d7f5d3SJohn Marino *
32*86d7f5d3SJohn Marino * $FreeBSD: src/usr.bin/talk/get_iface.c,v 1.7 1999/08/28 01:06:12 peter Exp $
33*86d7f5d3SJohn Marino */
34*86d7f5d3SJohn Marino
35*86d7f5d3SJohn Marino #include <errno.h>
36*86d7f5d3SJohn Marino #include <string.h>
37*86d7f5d3SJohn Marino #include "talk.h"
38*86d7f5d3SJohn Marino
39*86d7f5d3SJohn Marino /*
40*86d7f5d3SJohn Marino * Try to find the interface address that is used to route an IP
41*86d7f5d3SJohn Marino * packet to a remote peer.
42*86d7f5d3SJohn Marino */
43*86d7f5d3SJohn Marino
44*86d7f5d3SJohn Marino int
get_iface(struct in_addr * dst,struct in_addr * iface)45*86d7f5d3SJohn Marino get_iface(struct in_addr *dst, struct in_addr *iface)
46*86d7f5d3SJohn Marino {
47*86d7f5d3SJohn Marino static struct sockaddr_in local;
48*86d7f5d3SJohn Marino struct sockaddr_in remote;
49*86d7f5d3SJohn Marino int s, rv, namelen;
50*86d7f5d3SJohn Marino
51*86d7f5d3SJohn Marino memcpy(&remote.sin_addr, dst, sizeof remote.sin_addr);
52*86d7f5d3SJohn Marino remote.sin_port = htons(60000);
53*86d7f5d3SJohn Marino remote.sin_family = AF_INET;
54*86d7f5d3SJohn Marino remote.sin_len = sizeof remote;
55*86d7f5d3SJohn Marino
56*86d7f5d3SJohn Marino local.sin_addr.s_addr = htonl(INADDR_ANY);
57*86d7f5d3SJohn Marino local.sin_port = htons(60000);
58*86d7f5d3SJohn Marino local.sin_family = AF_INET;
59*86d7f5d3SJohn Marino local.sin_len = sizeof local;
60*86d7f5d3SJohn Marino
61*86d7f5d3SJohn Marino s = socket(PF_INET, SOCK_DGRAM, 0);
62*86d7f5d3SJohn Marino if (s < 0)
63*86d7f5d3SJohn Marino return -1;
64*86d7f5d3SJohn Marino
65*86d7f5d3SJohn Marino do {
66*86d7f5d3SJohn Marino rv = bind(s, (struct sockaddr *)&local, sizeof local);
67*86d7f5d3SJohn Marino local.sin_port = htons(ntohs(local.sin_port) + 1);
68*86d7f5d3SJohn Marino } while(rv < 0 && errno == EADDRINUSE);
69*86d7f5d3SJohn Marino
70*86d7f5d3SJohn Marino if (rv < 0) {
71*86d7f5d3SJohn Marino close(s);
72*86d7f5d3SJohn Marino return -1;
73*86d7f5d3SJohn Marino }
74*86d7f5d3SJohn Marino
75*86d7f5d3SJohn Marino do {
76*86d7f5d3SJohn Marino rv = connect(s, (struct sockaddr *)&remote, sizeof remote);
77*86d7f5d3SJohn Marino remote.sin_port = htons(ntohs(remote.sin_port) + 1);
78*86d7f5d3SJohn Marino } while(rv < 0 && errno == EADDRINUSE);
79*86d7f5d3SJohn Marino
80*86d7f5d3SJohn Marino if (rv < 0) {
81*86d7f5d3SJohn Marino close(s);
82*86d7f5d3SJohn Marino return -1;
83*86d7f5d3SJohn Marino }
84*86d7f5d3SJohn Marino
85*86d7f5d3SJohn Marino namelen = sizeof local;
86*86d7f5d3SJohn Marino rv = getsockname(s, (struct sockaddr *)&local, &namelen);
87*86d7f5d3SJohn Marino close(s);
88*86d7f5d3SJohn Marino if (rv < 0)
89*86d7f5d3SJohn Marino return -1;
90*86d7f5d3SJohn Marino
91*86d7f5d3SJohn Marino memcpy(iface, &local.sin_addr, sizeof local.sin_addr);
92*86d7f5d3SJohn Marino return 0;
93*86d7f5d3SJohn Marino }
94