X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Ftcpip.c;h=3fa26defb4e4971ce5e8f4c5ad313b90f5a6725b;hb=fc66745b60adb55d6b9f998c0bb9d8f607953e51;hp=715d0cf93116ce8339621f2a964b2dfd5ffc2666;hpb=4fc0e0c70a077abd8009e139315a54a0e0ddd293;p=yaz-moved-to-github.git diff --git a/src/tcpip.c b/src/tcpip.c index 715d0cf..3fa26de 100644 --- a/src/tcpip.c +++ b/src/tcpip.c @@ -423,6 +423,7 @@ void *tcpip_straddr(COMSTACK h, const char *str) { tcpip_state *sp = (tcpip_state *)h->cprivate; const char *port = "210"; + struct addrinfo *ai; if (h->protocol == PROTO_HTTP) port = "80"; if (!tcpip_init()) @@ -434,24 +435,35 @@ void *tcpip_straddr(COMSTACK h, const char *str) if (sp->ai && h->state == CS_ST_UNBND) { int s = -1; - struct addrinfo *ai = sp->ai; - for (; ai; ai = ai->ai_next) + /* try to make IPV6 socket first */ + for (ai = sp->ai; ai; ai = ai->ai_next) { - s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); - if (s != -1) + if (ai->ai_family == AF_INET6) { - sp->ai = ai; - break; + s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); + if (s != -1) + break; + } + } + if (s == -1) + { + /* no IPV6 could be made.. Try them all */ + for (ai = sp->ai; ai; ai = ai->ai_next) + { + s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); + if (s != -1) + break; } } if (s == -1) return 0; + assert(ai); h->iofile = s; if (!tcpip_set_blocking(h, h->flags)) return 0; } - return sp->ai; + return ai; } #else void *tcpip_straddr(COMSTACK h, const char *str) @@ -495,6 +507,7 @@ int tcpip_more(COMSTACK h) int tcpip_connect(COMSTACK h, void *address) { #if HAVE_GETADDRINFO + struct addrinfo *ai = (struct addrinfo *) address; tcpip_state *sp = (tcpip_state *)h->cprivate; #else struct sockaddr_in *add = (struct sockaddr_in *) address; @@ -511,13 +524,6 @@ int tcpip_connect(COMSTACK h, void *address) h->cerrno = CSOUTSTATE; return -1; } -#if HAVE_GETADDRINFO - if (sp->ai != (struct addrinfo *) address) - { - h->cerrno = CSOUTSTATE; - return -1; - } -#endif #ifdef __sun__ /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect * This gives the connect a chance to negotiate with the other side @@ -546,7 +552,7 @@ int tcpip_connect(COMSTACK h, void *address) #endif #if HAVE_GETADDRINFO - r = connect(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen); + r = connect(h->iofile, ai->ai_addr, ai->ai_addrlen); freeaddrinfo(sp->ai); sp->ai = 0; #else @@ -686,7 +692,8 @@ static int tcpip_bind(COMSTACK h, void *address, int mode) { int r; tcpip_state *sp = (tcpip_state *)h->cprivate; -#if HAVE_GETADDRINFO +#if HAVE_GETADDRINFO + struct addrinfo *ai = (struct addrinfo *) address; #else struct sockaddr *addr = (struct sockaddr *)address; #endif @@ -696,14 +703,6 @@ static int tcpip_bind(COMSTACK h, void *address, int mode) int one = 1; #endif -#if HAVE_GETADDRINFO - if (sp->ai != (struct addrinfo *) address) - { - h->cerrno = CSOUTSTATE; - return -1; - } -#endif - #if HAVE_GNUTLS_H if (h->type == ssl_type && !sp->session) { @@ -780,7 +779,7 @@ static int tcpip_bind(COMSTACK h, void *address, int mode) #endif tcpip_setsockopt(h->iofile); #if HAVE_GETADDRINFO - r = bind(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen); + r = bind(h->iofile, ai->ai_addr, ai->ai_addrlen); freeaddrinfo(sp->ai); sp->ai = 0; #else