X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Ftcpip.c;h=b9466d69379fb450eb45e2998924415aec6d3006;hp=ba3853abe54b1ed9c216d59cbff979fab939c46b;hb=896b145fa7a02199bfc3e8aa824c018b9d7cbf98;hpb=fee4224d6897ad849d74587d99a1a0f7e137128e diff --git a/src/tcpip.c b/src/tcpip.c index ba3853a..b9466d6 100644 --- a/src/tcpip.c +++ b/src/tcpip.c @@ -1,11 +1,14 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2009 Index Data + * Copyright (C) 1995-2011 Index Data * See the file LICENSE for details. */ /** * \file tcpip.c * \brief Implements TCP/IP + SSL COMSTACK. */ +#if HAVE_CONFIG_H +#include +#endif #include #include @@ -68,9 +71,9 @@ #include #include -#include +#include -static int tcpip_close(COMSTACK h); +static void tcpip_close(COMSTACK h); static int tcpip_put(COMSTACK h, char *buf, int size); static int tcpip_get(COMSTACK h, char **buf, int *bufsize); static int tcpip_put_connect(COMSTACK h, char *buf, int size); @@ -90,7 +93,7 @@ static int ssl_put(COMSTACK h, char *buf, int size); #endif static COMSTACK tcpip_accept(COMSTACK h); -static char *tcpip_addrstr(COMSTACK h); +static const char *tcpip_addrstr(COMSTACK h); static void *tcpip_straddr(COMSTACK h, const char *str); #if 0 @@ -206,7 +209,6 @@ COMSTACK tcpip_type(int s, int flags, int protocol, void *vp) p->state = s < 0 ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */ p->event = CS_NONE; p->cerrno = 0; - p->stackerr = 0; p->user = 0; #if HAVE_GNUTLS_H @@ -364,6 +366,13 @@ struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port) if (!strcmp("@", host)) { hints.ai_flags = AI_PASSIVE; + hints.ai_family = AF_INET; + error = getaddrinfo(0, port, &hints, &res); + } + else if (!strcmp("@6", host)) + { + hints.ai_flags = AI_PASSIVE; + hints.ai_family = AF_INET6; error = getaddrinfo(0, port, &hints, &res); } else @@ -419,30 +428,11 @@ int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add, } #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; const char *port = "210"; + struct addrinfo *ai = 0; if (h->protocol == PROTO_HTTP) port = "80"; if (!tcpip_init()) @@ -453,21 +443,22 @@ void *tcpip_straddr(COMSTACK h, const char *str) sp->ai = tcpip_getaddrinfo(str, port); if (sp->ai && h->state == CS_ST_UNBND) { - /* 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); + int s = -1; + 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) @@ -511,15 +502,12 @@ 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; #endif int r; -#ifdef __sun__ - int recbuflen; - YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen); -#endif TRC(fprintf(stderr, "tcpip_connect\n")); h->io_pending = 0; if (h->state != CS_ST_UNBND) @@ -528,41 +516,7 @@ int tcpip_connect(COMSTACK h, void *address) 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 - * (see 'man tcp') - */ - if (getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 ) - { - h->cerrno = CSYSERR; - return -1; - } - TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n", - recbuflen )); - recbuflen *= 10; /* lets be optimistic */ - if (setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 ) - { - h->cerrno = CSYSERR; - return -1; - } - if (getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) ) - { - h->cerrno = CSYSERR; - return -1; - } - TRC(fprintf(stderr, "New Size of TCP Receive Buffer = %d\n", - recbuflen )); -#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 @@ -626,8 +580,10 @@ int tcpip_rcvconnect(COMSTACK h) gnutls_credentials_set (sp->session, GNUTLS_CRD_CERTIFICATE, sp->cred_ptr->xcred); - gnutls_transport_set_ptr(sp->session, (gnutls_transport_ptr_t) h->iofile); - + /* cast to intermediate size_t to avoid GCC warning. */ + gnutls_transport_set_ptr(sp->session, + (gnutls_transport_ptr_t) + (size_t) h->iofile); res = gnutls_handshake(sp->session); if (res < 0) { @@ -675,32 +631,12 @@ int tcpip_rcvconnect(COMSTACK h) #define CERTF "ztest.pem" #define KEYF "ztest.pem" -static void tcpip_setsockopt(int fd) -{ -#if 0 - int len = 4096; - int set = 1; - - if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int))) - { - yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY"); - } - if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int))) - { - yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF"); - } - if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int))) - { - yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF"); - } -#endif -} - 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 @@ -710,14 +646,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) { @@ -792,9 +720,8 @@ static int tcpip_bind(COMSTACK h, void *address, int mode) return -1; } #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 @@ -820,8 +747,12 @@ int tcpip_listen(COMSTACK h, char *raddr, int *addrlen, int (*check_ip)(void *cd, const char *a, int len, int t), void *cd) { +#ifdef WIN32 + /* we don't get peer address on Windows (via accept) */ +#else struct sockaddr_in addr; YAZ_SOCKLEN_T len = sizeof(addr); +#endif TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid())); if (h->state != CS_ST_IDLE) @@ -861,6 +792,10 @@ int tcpip_listen(COMSTACK h, char *raddr, int *addrlen, } return -1; } +#ifdef WIN32 + if (addrlen) + *addrlen = 0; +#else if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in)) memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in)); else if (addrlen) @@ -877,8 +812,8 @@ int tcpip_listen(COMSTACK h, char *raddr, int *addrlen, h->newfd = -1; return -1; } +#endif h->state = CS_ST_INCON; - tcpip_setsockopt(h->newfd); return 0; } @@ -981,8 +916,10 @@ COMSTACK tcpip_accept(COMSTACK h) xfree(state); return 0; } + /* cast to intermediate size_t to avoid GCC warning. */ gnutls_transport_set_ptr(state->session, - (gnutls_transport_ptr_t) cnew->iofile); + (gnutls_transport_ptr_t) + (size_t) cnew->iofile); } #elif HAVE_OPENSSL_SSL_H state->ctx = st->ctx; @@ -1066,8 +1003,8 @@ int tcpip_get(COMSTACK h, char **buf, int *bufsize) TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize)); if (sp->altlen) /* switch buffers */ { - TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen, - (unsigned) sp->altbuf)); + TRC(fprintf(stderr, " %d bytes in altbuf (%p)\n", sp->altlen, + sp->altbuf)); tmpc = *buf; tmpi = *bufsize; *buf = sp->altbuf; @@ -1172,8 +1109,8 @@ int tcpip_get(COMSTACK h, char **buf, int *bufsize) h->cerrno = CSYSERR; return -1; } - TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove, - (unsigned) sp->altbuf)); + TRC(fprintf(stderr, " Moving %d bytes to altbuf(%p)\n", tomove, + sp->altbuf)); memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove); } if (berlen < CS_TCPIP_BUFCHUNK - 1) @@ -1197,8 +1134,8 @@ int ssl_get(COMSTACK h, char **buf, int *bufsize) TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize)); if (sp->altlen) /* switch buffers */ { - TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen, - (unsigned) sp->altbuf)); + TRC(fprintf(stderr, " %d bytes in altbuf (%p)\n", sp->altlen, + sp->altbuf)); tmpc = *buf; tmpi = *bufsize; *buf = sp->altbuf; @@ -1256,8 +1193,8 @@ int ssl_get(COMSTACK h, char **buf, int *bufsize) } else if (sp->altsize < req) if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req))) return -1; - TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove, - (unsigned) sp->altbuf)); + TRC(fprintf(stderr, " Moving %d bytes to altbuf(%p)\n", tomove, + sp->altbuf)); memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove); } if (berlen < CS_TCPIP_BUFCHUNK - 1) @@ -1390,7 +1327,7 @@ int ssl_put(COMSTACK h, char *buf, int size) } #endif -int tcpip_close(COMSTACK h) +void tcpip_close(COMSTACK h) { tcpip_state *sp = (struct tcpip_state *)h->cprivate; @@ -1450,10 +1387,9 @@ int tcpip_close(COMSTACK h) xfree(sp->connect_response_buf); xfree(sp); xfree(h); - return 0; } -char *tcpip_addrstr(COMSTACK h) +const char *tcpip_addrstr(COMSTACK h) { tcpip_state *sp = (struct tcpip_state *)h->cprivate; char *r = 0, *buf = sp->buf; @@ -1588,47 +1524,44 @@ void cs_print_session_info(COMSTACK cs) void *cs_get_ssl(COMSTACK cs) { #if HAVE_OPENSSL_SSL_H - struct tcpip_state *sp; - if (!cs || cs->type != ssl_type) - return 0; - sp = (struct tcpip_state *) cs->cprivate; - return sp->ssl; -#else - return 0; + if (cs && cs->type == ssl_type) + { + struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate; + return sp->ssl; + } #endif + return 0; } int cs_set_ssl_ctx(COMSTACK cs, void *ctx) { #if ENABLE_SSL - struct tcpip_state *sp; - if (!cs || cs->type != ssl_type) - return 0; - sp = (struct tcpip_state *) cs->cprivate; + if (cs && cs->type == ssl_type) + { #if HAVE_OPENSSL_SSL_H - if (sp->ctx_alloc) - return 0; - sp->ctx = (SSL_CTX *) ctx; + struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate; + if (sp->ctx_alloc) + return 0; + sp->ctx = (SSL_CTX *) ctx; #endif - return 1; -#else - return 0; + return 1; + } #endif + return 0; } 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; - sp = (struct tcpip_state *) cs->cprivate; - strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1); - sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0'; - return 1; -#else - return 0; + if (cs && cs->type == ssl_type) + { + struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate; + strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1); + sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0'; + return 1; + } #endif + return 0; } int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)