X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Ftcpip.c;h=ba3853abe54b1ed9c216d59cbff979fab939c46b;hb=30be9a32fd26a2d08aa4413942408500c93ad4a3;hp=b948603d2620c3e3d7f136f2ff4d86d307074768;hpb=f8d2abd0349cdff6c9bcf41937cd1dfd5de54ed2;p=yaz-moved-to-github.git diff --git a/src/tcpip.c b/src/tcpip.c index b948603..ba3853a 100644 --- a/src/tcpip.c +++ b/src/tcpip.c @@ -1,5 +1,5 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2008 Index Data + * Copyright (C) 1995-2009 Index Data * See the file LICENSE for details. */ /** @@ -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 @@ -260,7 +265,7 @@ COMSTACK yaz_tcpip_create(int s, int flags, int protocol, static void tcpip_create_cred(COMSTACK cs) { tcpip_state *sp = (tcpip_state *) cs->cprivate; - sp->cred_ptr = xmalloc(sizeof(*sp->cred_ptr)); + sp->cred_ptr = (struct tcpip_cred_ptr *) xmalloc(sizeof(*sp->cred_ptr)); sp->cred_ptr->ref = 1; gnutls_certificate_allocate_credentials(&sp->cred_ptr->xcred); } @@ -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; @@ -599,7 +622,7 @@ int tcpip_rcvconnect(COMSTACK h) tcpip_create_cred(h); gnutls_init(&sp->session, GNUTLS_CLIENT); - gnutls_priority_set_direct(sp->session, "PERFORMANCE", NULL); + gnutls_set_default_priority(sp->session); gnutls_credentials_set (sp->session, GNUTLS_CRD_CERTIFICATE, sp->cred_ptr->xcred); @@ -806,7 +829,11 @@ int tcpip_listen(COMSTACK h, char *raddr, int *addrlen, h->cerrno = CSOUTSTATE; return -1; } +#ifdef WIN32 + h->newfd = accept(h->iofile, 0, 0); +#else h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len); +#endif if (h->newfd < 0) { if ( @@ -938,8 +965,7 @@ COMSTACK tcpip_accept(COMSTACK h) xfree(state); return 0; } - res = gnutls_priority_set_direct(state->session, - "PERFORMANCE", NULL); + res = gnutls_set_default_priority(state->session); if (res != GNUTLS_E_SUCCESS) { xfree(cnew); @@ -1497,7 +1523,7 @@ char *tcpip_addrstr(COMSTACK h) return buf; } -int static tcpip_set_blocking(COMSTACK p, int flags) +static int tcpip_set_blocking(COMSTACK p, int flags) { unsigned long flag; @@ -1572,9 +1598,9 @@ void *cs_get_ssl(COMSTACK cs) #endif } -#if ENABLE_SSL int cs_set_ssl_ctx(COMSTACK cs, void *ctx) { +#if ENABLE_SSL struct tcpip_state *sp; if (!cs || cs->type != ssl_type) return 0; @@ -1585,10 +1611,14 @@ int cs_set_ssl_ctx(COMSTACK cs, void *ctx) sp->ctx = (SSL_CTX *) ctx; #endif return 1; +#else + return 0; +#endif } int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname) { +#if ENABLE_SSL struct tcpip_state *sp; if (!cs || cs->type != ssl_type) return 0; @@ -1596,6 +1626,9 @@ int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname) strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1); sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0'; return 1; +#else + return 0; +#endif } int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len) @@ -1621,23 +1654,6 @@ int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len) #endif return 0; } -#else -int cs_set_ssl_ctx(COMSTACK cs, void *ctx) -{ - return 0; -} - -int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len) -{ - return 0; -} - -int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname) -{ - return 0; -} -#endif - static int tcpip_put_connect(COMSTACK h, char *buf, int size) { @@ -1673,6 +1689,7 @@ static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize) /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil * End: * vim: shiftwidth=4 tabstop=8 expandtab