$NetBSD: patch-af,v 1.4 2012/02/16 14:05:07 hans Exp $ --- src/contoport.c.orig 2000-03-16 00:13:28 +0200 +++ src/contoport.c 2009-06-05 11:30:39 +0300 @@ -3,10 +3,11 @@ /* connect to a port */ #include +#include #include #include #include -#include +#include #include #include #include @@ -14,14 +15,15 @@ #include "icb.h" #include "externs.h" -int connecttoport (char *host_name, int port_number); +int connecttoport (char *host_name, int port_number, char *lhost_name); int -connecttoport (char *host_name, int port_number) +connecttoport (char *host_name, int port_number, char *lhost_name) { struct addrinfo hints, *res, *a; + struct addrinfo lhints, *lres, *la; char p[10]; - int err, s; + int err, s=-1; snprintf(p, 9, "%d", port_number); @@ -36,12 +38,43 @@ connecttoport (char *host_name, int port perror(gai_strerror(err)); return(-1); } + + if (lhost_name != NULL) { + memset(&lhints, 0, sizeof(lhints)); + lhints.ai_socktype = SOCK_STREAM; + lhints.ai_family = PF_UNSPEC; + lhints.ai_flags = AI_PASSIVE; + + err = getaddrinfo(lhost_name, p, &lhints, &lres); + if (err) { + perror(gai_strerror(err)); + return(-1); + } + } + a = res; while (a) { if ((s = socket(a->ai_family, a->ai_socktype, a->ai_protocol)) < 0) { a = a->ai_next; continue; } + + if (lhost_name != NULL) { + err = -1; + for(la = lres; la; la=la->ai_next) { + if (bind(s, la->ai_addr, la->ai_addrlen) == 0) { + /* bound locally! */ + err = 0; + break; + } + } + freeaddrinfo(lres); + if (err < 0) { + perror(gai_strerror(err)); + return(-1); + } + } + if (connect(s, a->ai_addr, a->ai_addrlen) < 0) { close(s); a = a->ai_next;