X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=comstack%2Ftcpip.c;h=cb24279c37d73684f44b8568cbf0dd6afc5e904c;hb=ac5f8ad9508f5b3ada38117b0da7ae23c650ab1e;hp=22a8ae5227194ddefd6749b8ec232661770f2d6e;hpb=8af7fd342d3c905ddd1e6be521242d881510ba14;p=yaz-moved-to-github.git diff --git a/comstack/tcpip.c b/comstack/tcpip.c index 22a8ae5..cb24279 100644 --- a/comstack/tcpip.c +++ b/comstack/tcpip.c @@ -1,8 +1,8 @@ /* - * Copyright (c) 1995-2001, Index Data + * Copyright (c) 1995-2002, Index Data * See the file LICENSE for details. * - * $Id: tcpip.c,v 1.44 2001-11-06 17:01:25 adam Exp $ + * $Id: tcpip.c,v 1.47 2002-06-12 19:42:38 adam Exp $ */ #include @@ -13,6 +13,7 @@ #endif #include #include +#include #if HAVE_OPENSSL_SSL_H #include #include @@ -125,10 +126,17 @@ COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp) #ifdef WIN32 if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0) + return 0; #else - if (!(p->blocking = blocking) && fcntl(s, F_SETFL, O_NONBLOCK) < 0) + if (!(p->blocking = blocking)) + { + if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) + return 0; +#ifndef MSG_NOSIGNAL + signal (SIGPIPE, SIG_IGN); +#endif + } #endif - return 0; p->io_pending = 0; p->iofile = s; @@ -275,40 +283,56 @@ int tcpip_connect(COMSTACK h, void *address) { struct sockaddr_in *add = (struct sockaddr_in *)address; #if HAVE_OPENSSL_SSL_H - tcpip_state *sp = (tcpip_state *)h->cprivate; + tcpip_state *sp = (tcpip_state *)h->cprivate; #endif int r; TRC(fprintf(stderr, "tcpip_connect\n")); h->io_pending = 0; - if (h->state == CS_ST_UNBND) + if (h->state != CS_ST_UNBND) + { + h->cerrno = CSOUTSTATE; + return -1; + } + r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add)); + if (r < 0) { - r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add)); - if (r < 0) - { #ifdef WIN32 - if (WSAGetLastError() == WSAEWOULDBLOCK) - { - h->event = CS_CONNECT; - h->state = CS_ST_CONNECTING; - h->io_pending = CS_WANT_WRITE; - return 1; - } + if (WSAGetLastError() == WSAEWOULDBLOCK) + { + h->event = CS_CONNECT; + h->state = CS_ST_CONNECTING; + h->io_pending = CS_WANT_WRITE; + return 1; + } #else - if (errno == EINPROGRESS) - { - h->event = CS_CONNECT; - h->state = CS_ST_CONNECTING; - h->io_pending = CS_WANT_WRITE|CS_WANT_READ; - return 1; - } + if (errno == EINPROGRESS) + { + h->event = CS_CONNECT; + h->state = CS_ST_CONNECTING; + h->io_pending = CS_WANT_WRITE|CS_WANT_READ; + return 1; + } #endif - h->cerrno = CSYSERR; - return -1; - } - h->event = CS_CONNECT; - h->state = CS_ST_CONNECTING; + h->cerrno = CSYSERR; + return -1; } + h->event = CS_CONNECT; + h->state = CS_ST_CONNECTING; + + return tcpip_rcvconnect (h); +} + +/* + * nop + */ +int tcpip_rcvconnect(COMSTACK h) +{ + tcpip_state *sp = (tcpip_state *)h->cprivate; + TRC(fprintf(stderr, "tcpip_rcvconnect\n")); + + if (h->state == CS_ST_DATAXFER) + return 0; if (h->state != CS_ST_CONNECTING) { h->cerrno = CSOUTSTATE; @@ -330,13 +354,11 @@ int tcpip_connect(COMSTACK h, void *address) int err = SSL_get_error(sp->ssl, res); if (err == SSL_ERROR_WANT_READ) { - yaz_log (LOG_LOG, "SSL_connect. want_read"); h->io_pending = CS_WANT_READ; return 1; } if (err == SSL_ERROR_WANT_WRITE) { - yaz_log (LOG_LOG, "SSL_connect. want_write"); h->io_pending = CS_WANT_WRITE; return 1; } @@ -350,45 +372,6 @@ int tcpip_connect(COMSTACK h, void *address) return 0; } -/* - * nop - */ -int tcpip_rcvconnect(COMSTACK cs) -{ - TRC(fprintf(stderr, "tcpip_rcvconnect\n")); - - if (cs->event == CS_CONNECT) - { - int fd = cs->iofile; - fd_set input, output; - struct timeval tv; - int r; - - tv.tv_sec = 0; - tv.tv_usec = 1; - - FD_ZERO(&input); - FD_ZERO(&output); - FD_SET (fd, &input); - FD_SET (fd, &output); - - r = select (fd+1, &input, &output, 0, &tv); - if (r > 0) - { - if (FD_ISSET(cs->iofile, &output)) - { - cs->event = CS_DATA; - return 0; /* write OK, we're OK */ - } - else - return -1; /* an error, for sure */ - } - else if (r == 0) - return 0; /* timeout - incomplete */ - } - return -1; /* wrong state or bad select */ -} - #define CERTF "ztest.pem" #define KEYF "ztest.pem" @@ -614,13 +597,11 @@ COMSTACK tcpip_accept(COMSTACK h) if (err == SSL_ERROR_WANT_READ) { h->io_pending = CS_WANT_READ; - yaz_log (LOG_LOG, "SSL_accept. want_read"); return h; } if (err == SSL_ERROR_WANT_WRITE) { h->io_pending = CS_WANT_WRITE; - yaz_log (LOG_LOG, "SSL_accept. want_write"); return h; } cs_close (h); @@ -698,6 +679,9 @@ int tcpip_get(COMSTACK h, char **buf, int *bufsize) #endif #endif || errno == EINPROGRESS +#ifdef __sun__ + || errno == ENOENT /* Sun's sometimes set errno to this */ +#endif ) { h->io_pending = CS_WANT_READ; @@ -784,13 +768,11 @@ int ssl_get(COMSTACK h, char **buf, int *bufsize) if (ssl_err == SSL_ERROR_WANT_READ) { h->io_pending = CS_WANT_READ; - yaz_log (LOG_LOG, "SSL_read. want_read"); break; } if (ssl_err == SSL_ERROR_WANT_WRITE) { h->io_pending = CS_WANT_WRITE; - yaz_log (LOG_LOG, "SSL_read. want_write"); break; } if (res == 0) @@ -925,13 +907,11 @@ int ssl_put(COMSTACK h, char *buf, int size) if (ssl_err == SSL_ERROR_WANT_READ) { h->io_pending = CS_WANT_READ; - yaz_log (LOG_LOG, "SSL_write. want_read"); return 1; } if (ssl_err == SSL_ERROR_WANT_WRITE) { h->io_pending = CS_WANT_WRITE; - yaz_log (LOG_LOG, "SSL_write. want_write"); return 1; } h->cerrno = CSERRORSSL;