2 * Copyright (c) 1995-2000, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.36 2001-02-21 13:46:53 adam
10 * Revision 1.35 2000/11/27 15:17:40 adam
11 * Using SSLeay_add_all_algorithms instead of OpenSSL_add_all_algorithms.
13 * Revision 1.34 2000/11/23 10:58:32 adam
14 * SSL comstack support. Separate POSIX thread support library.
16 * Revision 1.33 2000/09/04 08:27:11 adam
17 * Work on error handling for tcpip_accept.
19 * Revision 1.32 1999/11/30 13:47:11 adam
20 * Improved installation. Moved header files to include/yaz.
22 * Revision 1.31 1999/04/29 07:31:23 adam
23 * Changed tcpip_strtoaddr_ex so that only part 'till '/' is considered
26 * Revision 1.30 1999/04/20 09:56:48 adam
27 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
28 * Modified all encoders/decoders to reflect this change.
30 * Revision 1.29 1999/04/16 14:45:55 adam
31 * Added interface for tcpd wrapper for access control.
33 * Revision 1.28 1999/03/31 11:11:14 adam
34 * Function getprotobyname only called once. Minor change in tcpip_get
35 * to handle multi-threaded conditions.
37 * Revision 1.27 1999/02/02 13:57:31 adam
38 * Uses preprocessor define WIN32 instead of WINDOWS to build code
39 * for Microsoft WIN32.
41 * Revision 1.26 1999/01/08 11:23:14 adam
42 * Added const modifier to some of the BER/ODR encoding routines.
44 * Revision 1.25 1998/07/07 15:49:23 adam
45 * Added braces to avoid warning.
47 * Revision 1.24 1998/06/29 07:59:17 adam
50 * Revision 1.23 1998/06/23 15:37:50 adam
51 * Added type cast to prevent warning.
53 * Revision 1.22 1998/06/22 11:32:36 adam
54 * Added 'conditional cs_listen' feature.
56 * Revision 1.21 1998/05/20 09:55:32 adam
57 * Function tcpip_get treats EINPROGRESS error in the same way as
58 * EWOULDBLOCK. EINPROGRESS shouldn't be returned - but it is on
59 * Solaris in some cases.
61 * Revision 1.20 1998/05/18 10:10:40 adam
62 * Minor change to avoid C++ warning.
64 * Revision 1.19 1998/02/11 11:53:33 adam
65 * Changed code so that it compiles as C++.
67 * Revision 1.18 1997/09/29 07:15:25 adam
68 * Changed use of setsockopt to avoid warnings on MSVC.
70 * Revision 1.17 1997/09/17 12:10:30 adam
73 * Revision 1.16 1997/09/01 08:49:14 adam
74 * New windows NT/95 port using MSV5.0. Minor changes only.
76 * Revision 1.15 1997/05/14 06:53:33 adam
79 * Revision 1.14 1997/05/01 15:06:32 adam
80 * Moved WINSOCK init. code to tcpip_init routine.
82 * Revision 1.13 1996/11/01 08:45:18 adam
83 * Bug fix: used close on MS-Windows. Fixed to closesocket.
85 * Revision 1.12 1996/07/06 19:58:30 quinn
86 * System headerfiles gathered in yconfig
88 * Revision 1.11 1996/02/23 10:00:39 quinn
91 * Revision 1.10 1996/02/20 12:52:11 quinn
92 * WAIS protocol support.
94 * Revision 1.9 1996/02/10 12:23:11 quinn
95 * Enablie inetd operations fro TCP/IP stack
97 * Revision 1.8 1995/11/01 13:54:27 quinn
100 * Revision 1.7 1995/10/30 12:41:16 quinn
101 * Added hostname lookup for server.
103 * Revision 1.6 1995/09/29 17:12:00 quinn
106 * Revision 1.5 1995/09/29 17:01:48 quinn
109 * Revision 1.4 1995/09/28 10:12:26 quinn
110 * Windows-support changes
112 * Revision 1.3 1995/09/27 15:02:45 quinn
113 * Modified function heads & prototypes.
115 * Revision 1.2 1995/06/15 12:30:06 quinn
116 * Added @ as hostname alias for INADDR ANY.
118 * Revision 1.1 1995/06/14 09:58:20 quinn
119 * Renamed yazlib to comstack.
121 * Revision 1.20 1995/05/16 08:51:16 quinn
122 * License, documentation, and memory fixes
124 * Revision 1.19 1995/04/10 10:24:08 quinn
127 * Revision 1.18 1995/03/30 13:29:27 quinn
128 * Added REUSEADDR in tcpip_bind
130 * Revision 1.17 1995/03/27 08:36:10 quinn
131 * Some work on nonblocking operation in xmosi.c and rfct.c.
132 * Added protocol parameter to cs_create()
134 * Revision 1.16 1995/03/21 15:53:41 quinn
137 * Revision 1.15 1995/03/21 12:31:27 quinn
138 * Added check for EINPROGRESS on connect.
140 * Revision 1.14 1995/03/20 09:47:21 quinn
141 * Added server-side support to xmosi.c
142 * Fixed possible problems in rfct
145 * Revision 1.13 1995/03/15 16:15:13 adam
148 * Revision 1.12 1995/03/15 15:36:27 quinn
149 * Mods to support nonblocking I/O
151 * Revision 1.11 1995/03/15 08:37:57 quinn
152 * Now we're pretty much set for nonblocking I/O.
154 * Revision 1.10 1995/03/14 17:00:07 quinn
155 * Bug-fixes - added tracing info to tcpip.c
157 * Revision 1.9 1995/03/14 10:28:42 quinn
158 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
160 * Revision 1.8 1995/03/10 14:22:50 quinn
161 * Removed debug output.
163 * Revision 1.7 1995/03/10 11:44:59 quinn
164 * Fixes and debugging
166 * Revision 1.6 1995/03/07 10:26:55 quinn
167 * Initialized type field in the comstacks.
169 * Revision 1.5 1995/02/14 20:40:07 quinn
172 * Revision 1.4 1995/02/14 11:54:49 quinn
173 * Beginning to add full CCL.
175 * Revision 1.3 1995/02/10 18:58:10 quinn
176 * Fixed tcpip_get (formerly tcpip_read).
177 * Turned tst (cli) into a proper, event-driven thingy.
179 * Revision 1.2 1995/02/10 15:55:47 quinn
182 * Revision 1.1 1995/02/09 15:51:52 quinn
195 #if HAVE_OPENSSL_SSL_H
196 #include <openssl/ssl.h>
197 #include <openssl/err.h>
200 #include <yaz/comstack.h>
201 #include <yaz/tcpip.h>
204 /* Chas added the following, so we get the definition of completeBER */
207 int tcpip_close(COMSTACK h);
208 int tcpip_put(COMSTACK h, char *buf, int size);
209 int tcpip_get(COMSTACK h, char **buf, int *bufsize);
210 int tcpip_connect(COMSTACK h, void *address);
211 int tcpip_more(COMSTACK h);
212 int tcpip_rcvconnect(COMSTACK h);
213 int tcpip_bind(COMSTACK h, void *address, int mode);
214 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
215 int (*check_ip)(void *cd, const char *a, int len, int type),
218 #if HAVE_OPENSSL_SSL_H
219 int ssl_get(COMSTACK h, char **buf, int *bufsize);
220 int ssl_put(COMSTACK h, char *buf, int size);
223 COMSTACK tcpip_accept(COMSTACK h);
224 char *tcpip_addrstr(COMSTACK h);
225 void *tcpip_straddr(COMSTACK h, const char *str);
233 /* this state is used for both SSL and straight TCP/IP */
234 typedef struct tcpip_state
236 char *altbuf; /* alternate buffer for surplus data */
237 int altsize; /* size as xmalloced */
238 int altlen; /* length of data or 0 if none */
240 int written; /* -1 if we aren't writing */
241 int towrite; /* to verify against user input */
242 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
243 struct sockaddr_in addr; /* returned by cs_straddr */
244 char buf[128]; /* returned by cs_addrstr */
245 #if HAVE_OPENSSL_SSL_H
253 static int tcpip_init (void)
255 static int initialized = 0;
261 requested = MAKEWORD(1, 1);
262 if (WSAStartup(requested, &wd))
269 static int proto_number = 0;
271 static int tcpip_init (void)
273 struct protoent *proto;
274 /* only call getprotobyname once, in case it allocates memory */
275 if (!(proto = getprotobyname("tcp")))
277 proto_number = proto->p_proto;
283 * This function is always called through the cs_create() macro.
284 * s >= 0: socket has already been established for us.
286 COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
292 unsigned long tru = 1;
300 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
303 if ((s = socket(AF_INET, SOCK_STREAM, proto_number)) < 0)
310 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
312 if (!(state = (struct tcpip_state *)(p->cprivate =
313 xmalloc(sizeof(tcpip_state)))))
317 if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0)
319 if (!(p->blocking = blocking) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
325 p->type = tcpip_type;
326 p->protocol = (enum oid_proto) protocol;
328 p->f_connect = tcpip_connect;
329 p->f_rcvconnect = tcpip_rcvconnect;
330 p->f_get = tcpip_get;
331 p->f_put = tcpip_put;
332 p->f_close = tcpip_close;
333 p->f_more = tcpip_more;
334 p->f_bind = tcpip_bind;
335 p->f_listen = tcpip_listen;
336 p->f_accept = tcpip_accept;
337 p->f_addrstr = tcpip_addrstr;
338 p->f_straddr = tcpip_straddr;
340 p->state = new_socket ? CS_UNBND : CS_IDLE; /* state of line */
345 #if HAVE_OPENSSL_SSL_H
346 state->ctx = state->ctx_alloc = 0;
351 state->altsize = state->altlen = 0;
352 state->towrite = state->written = -1;
353 if (protocol == PROTO_WAIS)
354 state->complete = completeWAIS;
356 state->complete = completeBER;
358 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
359 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
364 #if HAVE_OPENSSL_SSL_H
366 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
370 yaz_log(LOG_LOG, "ssl_type begin");
372 p = tcpip_type (s, blocking, protocol, 0);
378 state = (tcpip_state *) p->cprivate;
383 SSL_load_error_strings();
384 SSLeay_add_all_algorithms();
386 state->ctx = state->ctx_alloc = SSL_CTX_new (SSLv23_method());
393 /* note: we don't handle already opened socket in SSL mode - yet */
394 yaz_log(LOG_LOG, "ssl_type end");
399 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add)
403 short int port = 210;
408 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
409 add->sin_family = AF_INET;
410 strncpy(buf, str, 511);
412 if ((p = strchr(buf, '/')))
414 if ((p = strchr(buf, ':')))
419 add->sin_port = htons(port);
420 if (!strcmp("@", buf))
421 add->sin_addr.s_addr = INADDR_ANY;
422 else if ((hp = gethostbyname(buf)))
423 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
424 sizeof(struct in_addr));
425 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
426 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
432 void *tcpip_straddr(COMSTACK h, const char *str)
434 tcpip_state *sp = (tcpip_state *)h->cprivate;
436 if (!tcpip_strtoaddr_ex (str, &sp->addr))
441 struct sockaddr_in *tcpip_strtoaddr(const char *str)
443 static struct sockaddr_in add;
445 if (!tcpip_strtoaddr_ex (str, &add))
450 int tcpip_more(COMSTACK h)
452 tcpip_state *sp = (tcpip_state *)h->cprivate;
454 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
459 * connect(2) will block (sometimes) - nothing we can do short of doing
460 * weird things like spawning subprocesses or threading or some weird junk
463 int tcpip_connect(COMSTACK h, void *address)
465 struct sockaddr_in *add = (struct sockaddr_in *)address;
466 #if HAVE_OPENSSL_SSL_H
467 tcpip_state *sp = (tcpip_state *)h->cprivate;
471 TRC(fprintf(stderr, "tcpip_connect\n"));
473 if (h->state == CS_UNBND)
475 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
479 if (WSAGetLastError() == WSAEWOULDBLOCK)
481 h->state = CS_CONNECTING;
482 h->io_pending = CS_WANT_WRITE;
486 if (errno == EINPROGRESS)
488 h->state = CS_CONNECTING;
489 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
495 h->state = CS_CONNECTING;
497 if (h->state != CS_CONNECTING)
499 h->cerrno = CSOUTSTATE;
502 #if HAVE_OPENSSL_SSL_H
509 sp->ssl = SSL_new (sp->ctx);
510 SSL_set_fd (sp->ssl, h->iofile);
512 res = SSL_connect (sp->ssl);
515 int err = SSL_get_error(sp->ssl, res);
516 if (err == SSL_ERROR_WANT_READ)
518 yaz_log (LOG_LOG, "SSL_connect. want_read");
519 h->io_pending = CS_WANT_READ;
522 if (err == SSL_ERROR_WANT_WRITE)
524 yaz_log (LOG_LOG, "SSL_connect. want_write");
525 h->io_pending = CS_WANT_WRITE;
532 h->state = CS_DATAXFER;
539 int tcpip_rcvconnect(COMSTACK h)
541 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
545 #define CERTF "ztest.pem"
546 #define KEYF "ztest.pem"
548 int tcpip_bind(COMSTACK h, void *address, int mode)
550 struct sockaddr *addr = (struct sockaddr *)address;
554 unsigned long one = 1;
557 #if HAVE_OPENSSL_SSL_H
558 tcpip_state *sp = (tcpip_state *)h->cprivate;
564 res = SSL_CTX_use_certificate_file (sp->ctx, CERTF,
568 ERR_print_errors_fp(stderr);
571 res = SSL_CTX_use_PrivateKey_file (sp->ctx, KEYF,
575 ERR_print_errors_fp(stderr);
578 res = SSL_CTX_check_private_key (sp->ctx);
581 ERR_print_errors_fp(stderr);
585 TRC (fprintf (stderr, "ssl_bind\n"));
589 TRC (fprintf (stderr, "tcpip_bind\n"));
592 TRC (fprintf (stderr, "tcpip_bind\n"));
594 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
595 &one, sizeof(one)) < 0)
600 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)) < 0)
605 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
614 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
615 int (*check_ip)(void *cd, const char *a, int len, int t),
618 struct sockaddr_in addr;
620 socklen_t len = sizeof(addr);
622 int len = sizeof(addr);
625 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
626 if (h->state != CS_IDLE)
628 h->cerrno = CSOUTSTATE;
631 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
636 WSAGetLastError() == WSAEWOULDBLOCK
641 h->cerrno = CSNODATA;
646 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
647 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
650 if (check_ip && (*check_ip)(cd, (const char *) &addr,
651 sizeof(addr), AF_INET))
655 closesocket(h->newfd);
666 COMSTACK tcpip_accept(COMSTACK h)
669 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
671 unsigned long tru = 1;
674 TRC(fprintf(stderr, "tcpip_accept\n"));
675 if (h->state == CS_INCON)
677 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
681 closesocket(h->newfd);
688 memcpy(cnew, h, sizeof(*h));
689 cnew->iofile = h->newfd;
690 cnew->io_pending = 0;
691 if (!(state = (tcpip_state *)
692 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
698 closesocket(h->newfd);
706 if (!cnew->blocking &&
708 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
710 (!cnew->blocking && fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
718 closesocket(h->newfd);
730 state->altsize = state->altlen = 0;
731 state->towrite = state->written = -1;
732 state->complete = st->complete;
733 cnew->state = CS_ACCEPT;
736 #if HAVE_OPENSSL_SSL_H
737 state->ctx = st->ctx;
738 state->ctx_alloc = 0;
739 state->ssl = st->ssl;
742 state->ssl = SSL_new (state->ctx);
743 SSL_set_fd (state->ssl, cnew->iofile);
748 if (h->state == CS_ACCEPT)
750 #if HAVE_OPENSSL_SSL_H
751 tcpip_state *state = (tcpip_state *)h->cprivate;
754 int res = SSL_accept (state->ssl);
755 TRC(fprintf(stderr, "SSL_accept\n"));
758 int err = SSL_get_error(state->ssl, res);
759 if (err == SSL_ERROR_WANT_READ)
761 h->io_pending = CS_WANT_READ;
762 yaz_log (LOG_LOG, "SSL_accept. want_read");
765 if (err == SSL_ERROR_WANT_WRITE)
767 h->io_pending = CS_WANT_WRITE;
768 yaz_log (LOG_LOG, "SSL_accept. want_write");
779 h->cerrno = CSOUTSTATE;
783 h->state = CS_DATAXFER;
787 #define CS_TCPIP_BUFCHUNK 4096
790 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
791 * 0=connection closed.
793 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
795 tcpip_state *sp = (tcpip_state *)h->cprivate;
797 int tmpi, berlen, rest, req, tomove;
798 int hasread = 0, res;
800 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
801 if (sp->altlen) /* switch buffers */
803 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
804 (unsigned) sp->altbuf));
808 *bufsize = sp->altsize;
809 hasread = sp->altlen;
815 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
819 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
822 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
823 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
825 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
826 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
830 if (WSAGetLastError() == WSAEWOULDBLOCK)
832 h->io_pending = CS_WANT_READ;
838 if (errno == EWOULDBLOCK
840 || errno == EINPROGRESS
844 h->io_pending = CS_WANT_READ;
857 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
859 /* move surplus buffer (or everything if we didn't get a BER rec.) */
860 if (hasread > berlen)
862 tomove = req = hasread - berlen;
863 rest = tomove % CS_TCPIP_BUFCHUNK;
865 req += CS_TCPIP_BUFCHUNK - rest;
868 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
870 } else if (sp->altsize < req)
871 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
873 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
874 (unsigned) sp->altbuf));
875 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
877 if (berlen < CS_TCPIP_BUFCHUNK - 1)
878 *(*buf + berlen) = '\0';
879 return berlen ? berlen : 1;
883 #if HAVE_OPENSSL_SSL_H
885 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
886 * 0=connection closed.
888 int ssl_get(COMSTACK h, char **buf, int *bufsize)
890 tcpip_state *sp = (tcpip_state *)h->cprivate;
892 int tmpi, berlen, rest, req, tomove;
893 int hasread = 0, res;
895 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
896 if (sp->altlen) /* switch buffers */
898 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
899 (unsigned) sp->altbuf));
903 *bufsize = sp->altsize;
904 hasread = sp->altlen;
910 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
914 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
917 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
918 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
920 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
921 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
924 int ssl_err = SSL_get_error(sp->ssl, res);
925 if (ssl_err == SSL_ERROR_WANT_READ)
927 h->io_pending = CS_WANT_READ;
928 yaz_log (LOG_LOG, "SSL_read. want_read");
931 if (ssl_err == SSL_ERROR_WANT_WRITE)
933 h->io_pending = CS_WANT_WRITE;
934 yaz_log (LOG_LOG, "SSL_read. want_write");
943 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
945 /* move surplus buffer (or everything if we didn't get a BER rec.) */
946 if (hasread > berlen)
948 tomove = req = hasread - berlen;
949 rest = tomove % CS_TCPIP_BUFCHUNK;
951 req += CS_TCPIP_BUFCHUNK - rest;
954 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
956 } else if (sp->altsize < req)
957 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
959 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
960 (unsigned) sp->altbuf));
961 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
963 if (berlen < CS_TCPIP_BUFCHUNK - 1)
964 *(*buf + berlen) = '\0';
965 return berlen ? berlen : 1;
971 * In nonblocking mode, you must call again with same buffer while
974 int tcpip_put(COMSTACK h, char *buf, int size)
977 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
979 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
981 if (state->towrite < 0)
983 state->towrite = size;
986 else if (state->towrite != size)
988 h->cerrno = CSWRONGBUF;
991 while (state->towrite > state->written)
993 if ((res = send(h->iofile, buf + state->written, size -
994 state->written, 0)) < 0)
998 WSAGetLastError() == WSAEWOULDBLOCK
1004 TRC(fprintf(stderr, " Flow control stop\n"));
1005 h->io_pending = CS_WANT_WRITE;
1008 h->cerrno = CSYSERR;
1011 state->written += res;
1012 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1013 res, state->written, size));
1015 state->towrite = state->written = -1;
1016 TRC(fprintf(stderr, " Ok\n"));
1021 #if HAVE_OPENSSL_SSL_H
1023 * Returns 1, 0 or -1
1024 * In nonblocking mode, you must call again with same buffer while
1025 * return value is 1.
1027 int ssl_put(COMSTACK h, char *buf, int size)
1030 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1032 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1034 if (state->towrite < 0)
1036 state->towrite = size;
1039 else if (state->towrite != size)
1041 h->cerrno = CSWRONGBUF;
1044 while (state->towrite > state->written)
1046 res = SSL_write (state->ssl, buf + state->written,
1047 size - state->written);
1050 int ssl_err = SSL_get_error(state->ssl, res);
1051 if (ssl_err == SSL_ERROR_WANT_READ)
1053 h->io_pending = CS_WANT_READ;
1054 yaz_log (LOG_LOG, "SSL_write. want_read");
1057 if (ssl_err == SSL_ERROR_WANT_WRITE)
1059 h->io_pending = CS_WANT_WRITE;
1060 yaz_log (LOG_LOG, "SSL_write. want_write");
1065 state->written += res;
1066 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1067 res, state->written, size));
1069 state->towrite = state->written = -1;
1070 TRC(fprintf(stderr, " Ok\n"));
1075 int tcpip_close(COMSTACK h)
1077 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1079 TRC(fprintf(stderr, "tcpip_close\n"));
1080 if (h->iofile != -1)
1082 #if HAVE_OPENSSL_SSL_H
1085 SSL_shutdown (sp->ssl);
1089 closesocket(h->iofile);
1096 #if HAVE_OPENSSL_SSL_H
1099 TRC (fprintf(stderr, "SSL_free\n"));
1104 SSL_CTX_free (sp->ctx_alloc);
1111 char *tcpip_addrstr(COMSTACK h)
1113 struct sockaddr_in addr;
1114 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1115 char *r, *buf = sp->buf;
1117 struct hostent *host;
1120 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1122 h->cerrno = CSYSERR;
1125 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1127 r = (char*) host->h_name;
1129 r = inet_ntoa(addr.sin_addr);
1130 sprintf(buf, "tcp:%s", r);
1131 #if HAVE_OPENSSL_SSL_H
1133 sprintf(buf, "ssl:%s", r);