/*
- * 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.45 2002-01-21 21:50:32 adam Exp $
*/
#include <stdio.h>
{
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);
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;
/*
- * $Id: zoom-c.c,v 1.20 2002-01-09 12:44:31 adam Exp $
+ * $Id: zoom-c.c,v 1.21 2002-01-21 21:50:32 adam Exp $
*
* ZOOM layer for C, connections, result sets, queries.
*/
static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
{
- // put in back of queue
if (c->m_queue_back)
{
c->m_queue_back->prev = event;
static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
{
- // get from front of queue
ZOOM_Event event = c->m_queue_front;
if (!event)
return 0;
if (ret >= 0)
{
c->state = STATE_CONNECTING;
- c->mask = ZOOM_SELECT_READ | ZOOM_SELECT_WRITE |
- ZOOM_SELECT_EXCEPT;
+ c->mask = ZOOM_SELECT_EXCEPT;
+ if (c->cs->io_pending & CS_WANT_WRITE)
+ c->mask += ZOOM_SELECT_WRITE;
+ if (c->cs->io_pending & CS_WANT_READ)
+ c->mask += ZOOM_SELECT_READ;
return 1;
}
}
return 1;
}
else if (r == 1)
- {
- c->state = STATE_ESTABLISHED;
- c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_WRITE|ZOOM_SELECT_EXCEPT;
+ {
+ c->mask = ZOOM_SELECT_EXCEPT;
+ if (c->cs->io_pending & CS_WANT_WRITE)
+ c->mask += ZOOM_SELECT_WRITE;
+ if (c->cs->io_pending & CS_WANT_READ)
+ c->mask += ZOOM_SELECT_READ;
+ yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
}
else
{
- c->state = STATE_ESTABLISHED;
- c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
+ c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
+ yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
}
return 0;
}
int ZOOM_connection_do_io(ZOOM_connection c, int mask)
{
ZOOM_Event event = 0;
-#if 0
int r = cs_look(c->cs);
- yaz_log (LOG_LOG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
+ yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
c, mask, r);
if (r == CS_NONE)
{
- event = ZOOM_Event_create (ZOOM_EVENT_IO_CONNECT);
+ event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
c->error = ZOOM_ERROR_CONNECT;
do_close (c);
ZOOM_connection_put_event (c, event);
}
else if (r == CS_CONNECT)
{
- event = ZOOM_Event_create (ZOOM_EVENT_IO_CONNECT);
- yaz_log (LOG_LOG, "calling rcvconnect");
- if (cs_rcvconnect (c->cs) < 0)
- {
- c->error = ZOOM_ERROR_CONNECT;
- do_close (c);
- ZOOM_connection_put_event (c, event);
- }
- else
+ int ret;
+ event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
+
+ ret = cs_rcvconnect (c->cs);
+ yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
+ if (ret == 1)
{
+ c->mask = ZOOM_SELECT_EXCEPT;
+ if (c->cs->io_pending & CS_WANT_WRITE)
+ c->mask += ZOOM_SELECT_WRITE;
+ if (c->cs->io_pending & CS_WANT_READ)
+ c->mask += ZOOM_SELECT_READ;
ZOOM_connection_put_event (c, event);
- ZOOM_connection_send_init (c);
}
- }
- else
- {
- if (mask & ZOOM_SELECT_READ)
- do_read (c);
- if (c->cs && (mask & ZOOM_SELECT_WRITE))
- do_write (c);
- }
-#else
- yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d", c, mask);
- if (c->state == STATE_CONNECTING)
- {
- event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
- if (mask & ZOOM_SELECT_WRITE)
+ else if (ret == 0)
{
ZOOM_connection_put_event (c, event);
- ZOOM_connection_send_init (c);
+ ZOOM_connection_send_init (c);
+ c->state = STATE_ESTABLISHED;
}
- else
- {
- c->error = ZOOM_ERROR_CONNECT;
- do_close (c);
+ else
+ {
+ c->error = ZOOM_ERROR_CONNECT;
+ do_close (c);
ZOOM_connection_put_event (c, event);
- }
- }
- else if (c->state == STATE_ESTABLISHED)
- {
- if (mask & ZOOM_SELECT_READ)
- do_read (c);
- if (c->cs && (mask & ZOOM_SELECT_WRITE))
- do_write (c);
+ }
}
else
{
- event = ZOOM_Event_create (ZOOM_EVENT_UNKNOWN);
- ZOOM_connection_put_event (c, event);
- c->error = ZOOM_ERROR_INTERNAL;
- do_close (c);
+ if (mask & ZOOM_SELECT_READ)
+ do_read (c);
+ if (c->cs && (mask & ZOOM_SELECT_WRITE))
+ do_write (c);
}
-#endif
return 1;
}
if (!nfds)
return 0;
#if HAVE_SYS_POLL_H
- yaz_log (LOG_DEBUG, "poll start");
r = poll (pollfds, nfds, 15000);
- yaz_log (LOG_DEBUG, "poll stop, returned r=%d", r);
for (i = 0; i<nfds; i++)
{
ZOOM_connection c = poll_cs[i];