From owner-freebsd-current Mon Aug 7 08:09:09 1995 Return-Path: current-owner Received: (from majordom@localhost) by freefall.cdrom.com (8.6.11/8.6.6) id IAA08964 for current-outgoing; Mon, 7 Aug 1995 08:09:09 -0700 Received: from halloran-eldar.lcs.mit.edu (halloran-eldar.lcs.mit.edu [18.26.0.159]) by freefall.cdrom.com (8.6.11/8.6.6) with SMTP id IAA08929 for ; Mon, 7 Aug 1995 08:08:59 -0700 Received: by halloran-eldar.lcs.mit.edu; (5.65/1.1.3.6) id AA00541; Mon, 7 Aug 1995 11:05:10 -0400 Date: Mon, 7 Aug 1995 11:05:10 -0400 From: Garrett Wollman Message-Id: <9508071505.AA00541@halloran-eldar.lcs.mit.edu> To: freebsd-current@FreeBSD.org (FreeBSD-current users), joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch) Subject: workaround for talk's address problem In-Reply-To: <199508061603.SAA07779@uriah.heep.sax.de> References: <199508061603.SAA07779@uriah.heep.sax.de> Sender: current-owner@FreeBSD.org Precedence: bulk < said: > The correct solution would be asking the routing socket to see which > interface address must be used to get in contact with the remote > peer. Not necessarily. The following program figures out which address to use to contact a host passed on the command line: #include #include #include #include #include #include #include #include #include #include #include #include int main(int argc, char **argv) { struct sockaddr_in local, remote; struct hostent *hp; int s, rv, namelen; argc--, argv++; if (!*argv) { errx(EX_USAGE, "must supply a hostname"); } hp = gethostbyname(*argv); if (!hp) { errx(EX_NOHOST, "cannot resolve hostname: %s", *argv); } memcpy(&remote.sin_addr, hp->h_addr_list[0], sizeof remote.sin_addr); remote.sin_port = htons(60000); remote.sin_family = AF_INET; remote.sin_len = sizeof remote; local.sin_addr.s_addr = htonl(INADDR_ANY); local.sin_port = htons(60000); local.sin_family = AF_INET; local.sin_len = sizeof local; s = socket(PF_INET, SOCK_DGRAM, 0); if (s < 0) err(EX_OSERR, "socket"); do { rv = bind(s, (struct sockaddr *)&local, sizeof local); local.sin_port++; } while(rv < 0 && errno == EADDRINUSE); if (rv < 0) err(EX_OSERR, "bind"); do { rv = connect(s, (struct sockaddr *)&remote, sizeof remote); remote.sin_port++; } while(rv < 0 && errno == EADDRINUSE); if (rv < 0) err(EX_OSERR, "connect"); namelen = sizeof local; rv = getsockname(s, (struct sockaddr *)&local, &namelen); if (rv < 0) err(EX_OSERR, "getsockname"); printf("Route to %s is out %s\n", *argv, inet_ntoa(local.sin_addr)); return 0; } -GAWollman -- Garrett A. Wollman | Shashish is simple, it's discreet, it's brief. ... wollman@lcs.mit.edu | Shashish is the bonding of hearts in spite of distance. Opinions not those of| It is a bond more powerful than absence. We like people MIT, LCS, ANA, or NSA| who like Shashish. - Claude McKenzie + Florent Vollant