114443Sjoerg/* 214443Sjoerg * Copyright 1994, 1995 Massachusetts Institute of Technology 314443Sjoerg * 414443Sjoerg * Permission to use, copy, modify, and distribute this software and 514443Sjoerg * its documentation for any purpose and without fee is hereby 614443Sjoerg * granted, provided that both the above copyright notice and this 714443Sjoerg * permission notice appear in all copies, that both the above 814443Sjoerg * copyright notice and this permission notice appear in all 914443Sjoerg * supporting documentation, and that the name of M.I.T. not be used 1014443Sjoerg * in advertising or publicity pertaining to distribution of the 1114443Sjoerg * software without specific, written prior permission. M.I.T. makes 1214443Sjoerg * no representations about the suitability of this software for any 1314443Sjoerg * purpose. It is provided "as is" without express or implied 1414443Sjoerg * warranty. 15117238Sluigi * 1614443Sjoerg * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 1714443Sjoerg * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 1814443Sjoerg * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 1914443Sjoerg * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 2014443Sjoerg * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2114443Sjoerg * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2214443Sjoerg * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 2314443Sjoerg * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 2414443Sjoerg * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2514443Sjoerg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 2614443Sjoerg * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2714443Sjoerg * SUCH DAMAGE. 2887710Smarkm */ 2987710Smarkm 3087710Smarkm#include <sys/cdefs.h> 3187710Smarkm 3287710Smarkm__FBSDID("$FreeBSD$"); 3387710Smarkm 3487710Smarkm/* 3514443Sjoerg * From: 3614443Sjoerg * Id: find_interface.c,v 1.1 1995/08/14 16:08:39 wollman Exp 3714443Sjoerg */ 3814443Sjoerg 3914443Sjoerg#include <errno.h> 4033359Sjb#include <string.h> 41200418Sdelphij#include <unistd.h> 4287710Smarkm 4314443Sjoerg#include "talk.h" 4414443Sjoerg 4514443Sjoerg/* 4614443Sjoerg * Try to find the interface address that is used to route an IP 4714443Sjoerg * packet to a remote peer. 4814443Sjoerg */ 4914443Sjoerg 5014443Sjoergint 51178642Sdelphijget_iface(struct in_addr *dst, struct in_addr *iface) 5214443Sjoerg{ 5314443Sjoerg static struct sockaddr_in local; 5414443Sjoerg struct sockaddr_in remote; 55143415Sstefanf socklen_t namelen; 56143415Sstefanf int s, rv; 5714443Sjoerg 5814443Sjoerg memcpy(&remote.sin_addr, dst, sizeof remote.sin_addr); 5914443Sjoerg remote.sin_port = htons(60000); 6014443Sjoerg remote.sin_family = AF_INET; 6114443Sjoerg remote.sin_len = sizeof remote; 6214443Sjoerg 6314443Sjoerg local.sin_addr.s_addr = htonl(INADDR_ANY); 6414443Sjoerg local.sin_port = htons(60000); 6514443Sjoerg local.sin_family = AF_INET; 6614443Sjoerg local.sin_len = sizeof local; 6714443Sjoerg 6814443Sjoerg s = socket(PF_INET, SOCK_DGRAM, 0); 6914443Sjoerg if (s < 0) 7014443Sjoerg return -1; 7114443Sjoerg 7214443Sjoerg do { 7314443Sjoerg rv = bind(s, (struct sockaddr *)&local, sizeof local); 7414585Sache local.sin_port = htons(ntohs(local.sin_port) + 1); 7514443Sjoerg } while(rv < 0 && errno == EADDRINUSE); 7614443Sjoerg 7714443Sjoerg if (rv < 0) { 7814443Sjoerg close(s); 7914443Sjoerg return -1; 8014443Sjoerg } 8114443Sjoerg 8214443Sjoerg do { 8314443Sjoerg rv = connect(s, (struct sockaddr *)&remote, sizeof remote); 8414585Sache remote.sin_port = htons(ntohs(remote.sin_port) + 1); 8514443Sjoerg } while(rv < 0 && errno == EADDRINUSE); 8614443Sjoerg 8714443Sjoerg if (rv < 0) { 8814443Sjoerg close(s); 8914443Sjoerg return -1; 9014443Sjoerg } 9114443Sjoerg 9214443Sjoerg namelen = sizeof local; 9314443Sjoerg rv = getsockname(s, (struct sockaddr *)&local, &namelen); 9414443Sjoerg close(s); 9514443Sjoerg if (rv < 0) 9614443Sjoerg return -1; 9714443Sjoerg 9814443Sjoerg memcpy(iface, &local.sin_addr, sizeof local.sin_addr); 9914443Sjoerg return 0; 10014443Sjoerg} 101