X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Ftcpip.c;h=bd4db34f2614a151d9b226a17bfb629d9a6e1df8;hp=8df1e814671a767b7fe3a84cce65398921977e73;hb=2db517d64ac08da1f812f33812cf58f6aeb905e3;hpb=2788a4851b551e1a3efb320a2878b809f2d8a9d7 diff --git a/src/tcpip.c b/src/tcpip.c index 8df1e81..bd4db34 100644 --- a/src/tcpip.c +++ b/src/tcpip.c @@ -1,5 +1,5 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2009 Index Data + * Copyright (C) 1995-2010 Index Data * See the file LICENSE for details. */ /** @@ -68,9 +68,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 +90,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 +206,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 @@ -419,30 +418,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 +433,36 @@ 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); + int s = -1; + /* try to make IPV6 socket first */ + for (ai = sp->ai; ai; ai = ai->ai_next) + { + if (ai->ai_family == AF_INET6) + { + s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); + if (s != -1) + break; + } + } if (s == -1) - s = create_socket_family(sp->ai, AF_INET); + { + /* 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) @@ -511,15 +506,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 +520,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 +584,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 +635,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 +650,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 +724,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 @@ -829,7 +760,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 ( @@ -874,7 +809,6 @@ int tcpip_listen(COMSTACK h, char *raddr, int *addrlen, return -1; } h->state = CS_ST_INCON; - tcpip_setsockopt(h->newfd); return 0; } @@ -977,8 +911,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; @@ -1062,8 +998,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; @@ -1168,8 +1104,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) @@ -1193,8 +1129,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; @@ -1252,8 +1188,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) @@ -1386,7 +1322,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; @@ -1446,10 +1382,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; @@ -1685,6 +1620,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