X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Ftcpip.c;h=11b676c11dbece0fb71a77cd1003e82242238c63;hp=fcdcce12a695d74c1635d65491f4fbe70700b9c7;hb=4ce7bffcae2b1faaa0fb5d74eca9aeeb7f6d0c6e;hpb=bf95c509245f5cb4025ef2db348d5f7ace3b5994 diff --git a/src/tcpip.c b/src/tcpip.c index fcdcce1..11b676c 100644 --- a/src/tcpip.c +++ b/src/tcpip.c @@ -25,7 +25,6 @@ #endif #ifdef WIN32 - /* VS 2003 or later has getaddrinfo; older versions do not */ #include #if _MSC_VER >= 1300 @@ -34,14 +33,20 @@ #else #define HAVE_GETADDRINFO 0 #endif +#endif -#else +#if HAVE_NETINET_IN_H #include +#endif +#if HAVE_NETDB_H #include +#endif +#if HAVE_ARPA_INET_H #include +#endif +#if HAVE_NETINET_TCP_H #include #endif - #if HAVE_SYS_SOCKET_H #include #endif @@ -413,8 +418,27 @@ int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add, return 1; } - #if HAVE_GETADDRINFO +/** \brief Creates socket using particular address family (AF_) + \param ai getaddrinfo result + \param mask family mask + \returns socket or -1 if none could be created + +*/ +static int create_socket_family(struct addrinfo *ai, unsigned mask) +{ + for (; ai; ai = ai->ai_next) + { + if ((ai->ai_family & mask) == mask) + { + int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); + if (s != -1) + return s; + } + } + return -1; +} + void *tcpip_straddr(COMSTACK h, const char *str) { tcpip_state *sp = (tcpip_state *)h->cprivate; @@ -429,14 +453,13 @@ void *tcpip_straddr(COMSTACK h, const char *str) sp->ai = tcpip_getaddrinfo(str, port); if (sp->ai && h->state == CS_ST_UNBND) { - int s = -1; - struct addrinfo *ai = sp->ai; - for (; ai; ai = ai->ai_next) - { - s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); - if (s != -1) - break; - } + /* The getaddrinfo call may return multiple addresses when passive + flags are used (AI_PASSIVE). This function picks the IPV6 if a + socket can be created for it. Otherwise IPV4 is used. + See also bug #2350 */ + int s = create_socket_family(sp->ai, AF_INET6); + if (s == -1) + s = create_socket_family(sp->ai, AF_INET); if (s == -1) return 0; h->iofile = s;