/*
- * 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 <stdio.h>
#endif
#include <errno.h>
#include <fcntl.h>
+#include <signal.h>
#if HAVE_OPENSSL_SSL_H
#include <openssl/ssl.h>
#include <openssl/err.h>
#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;
{
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;
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;
}
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"
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);
#endif
#endif
|| errno == EINPROGRESS
+#ifdef __sun__
+ || errno == ENOENT /* Sun's sometimes set errno to this */
+#endif
)
{
h->io_pending = CS_WANT_READ;
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)
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;