2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
6 * Revision 1.37 2001-03-08 20:18:55 adam
7 * Added cs_set_blocking. Patch from Matthew Carey.
9 * Revision 1.36 2001/02/21 13:46:53 adam
12 * Revision 1.35 2000/11/27 15:17:40 adam
13 * Using SSLeay_add_all_algorithms instead of OpenSSL_add_all_algorithms.
15 * Revision 1.34 2000/11/23 10:58:32 adam
16 * SSL comstack support. Separate POSIX thread support library.
18 * Revision 1.33 2000/09/04 08:27:11 adam
19 * Work on error handling for tcpip_accept.
21 * Revision 1.32 1999/11/30 13:47:11 adam
22 * Improved installation. Moved header files to include/yaz.
24 * Revision 1.31 1999/04/29 07:31:23 adam
25 * Changed tcpip_strtoaddr_ex so that only part 'till '/' is considered
28 * Revision 1.30 1999/04/20 09:56:48 adam
29 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
30 * Modified all encoders/decoders to reflect this change.
32 * Revision 1.29 1999/04/16 14:45:55 adam
33 * Added interface for tcpd wrapper for access control.
35 * Revision 1.28 1999/03/31 11:11:14 adam
36 * Function getprotobyname only called once. Minor change in tcpip_get
37 * to handle multi-threaded conditions.
39 * Revision 1.27 1999/02/02 13:57:31 adam
40 * Uses preprocessor define WIN32 instead of WINDOWS to build code
41 * for Microsoft WIN32.
43 * Revision 1.26 1999/01/08 11:23:14 adam
44 * Added const modifier to some of the BER/ODR encoding routines.
46 * Revision 1.25 1998/07/07 15:49:23 adam
47 * Added braces to avoid warning.
49 * Revision 1.24 1998/06/29 07:59:17 adam
52 * Revision 1.23 1998/06/23 15:37:50 adam
53 * Added type cast to prevent warning.
55 * Revision 1.22 1998/06/22 11:32:36 adam
56 * Added 'conditional cs_listen' feature.
58 * Revision 1.21 1998/05/20 09:55:32 adam
59 * Function tcpip_get treats EINPROGRESS error in the same way as
60 * EWOULDBLOCK. EINPROGRESS shouldn't be returned - but it is on
61 * Solaris in some cases.
63 * Revision 1.20 1998/05/18 10:10:40 adam
64 * Minor change to avoid C++ warning.
66 * Revision 1.19 1998/02/11 11:53:33 adam
67 * Changed code so that it compiles as C++.
69 * Revision 1.18 1997/09/29 07:15:25 adam
70 * Changed use of setsockopt to avoid warnings on MSVC.
72 * Revision 1.17 1997/09/17 12:10:30 adam
75 * Revision 1.16 1997/09/01 08:49:14 adam
76 * New windows NT/95 port using MSV5.0. Minor changes only.
78 * Revision 1.15 1997/05/14 06:53:33 adam
81 * Revision 1.14 1997/05/01 15:06:32 adam
82 * Moved WINSOCK init. code to tcpip_init routine.
84 * Revision 1.13 1996/11/01 08:45:18 adam
85 * Bug fix: used close on MS-Windows. Fixed to closesocket.
87 * Revision 1.12 1996/07/06 19:58:30 quinn
88 * System headerfiles gathered in yconfig
90 * Revision 1.11 1996/02/23 10:00:39 quinn
93 * Revision 1.10 1996/02/20 12:52:11 quinn
94 * WAIS protocol support.
96 * Revision 1.9 1996/02/10 12:23:11 quinn
97 * Enablie inetd operations fro TCP/IP stack
99 * Revision 1.8 1995/11/01 13:54:27 quinn
102 * Revision 1.7 1995/10/30 12:41:16 quinn
103 * Added hostname lookup for server.
105 * Revision 1.6 1995/09/29 17:12:00 quinn
108 * Revision 1.5 1995/09/29 17:01:48 quinn
111 * Revision 1.4 1995/09/28 10:12:26 quinn
112 * Windows-support changes
114 * Revision 1.3 1995/09/27 15:02:45 quinn
115 * Modified function heads & prototypes.
117 * Revision 1.2 1995/06/15 12:30:06 quinn
118 * Added @ as hostname alias for INADDR ANY.
120 * Revision 1.1 1995/06/14 09:58:20 quinn
121 * Renamed yazlib to comstack.
123 * Revision 1.20 1995/05/16 08:51:16 quinn
124 * License, documentation, and memory fixes
126 * Revision 1.19 1995/04/10 10:24:08 quinn
129 * Revision 1.18 1995/03/30 13:29:27 quinn
130 * Added REUSEADDR in tcpip_bind
132 * Revision 1.17 1995/03/27 08:36:10 quinn
133 * Some work on nonblocking operation in xmosi.c and rfct.c.
134 * Added protocol parameter to cs_create()
136 * Revision 1.16 1995/03/21 15:53:41 quinn
139 * Revision 1.15 1995/03/21 12:31:27 quinn
140 * Added check for EINPROGRESS on connect.
142 * Revision 1.14 1995/03/20 09:47:21 quinn
143 * Added server-side support to xmosi.c
144 * Fixed possible problems in rfct
147 * Revision 1.13 1995/03/15 16:15:13 adam
150 * Revision 1.12 1995/03/15 15:36:27 quinn
151 * Mods to support nonblocking I/O
153 * Revision 1.11 1995/03/15 08:37:57 quinn
154 * Now we're pretty much set for nonblocking I/O.
156 * Revision 1.10 1995/03/14 17:00:07 quinn
157 * Bug-fixes - added tracing info to tcpip.c
159 * Revision 1.9 1995/03/14 10:28:42 quinn
160 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
162 * Revision 1.8 1995/03/10 14:22:50 quinn
163 * Removed debug output.
165 * Revision 1.7 1995/03/10 11:44:59 quinn
166 * Fixes and debugging
168 * Revision 1.6 1995/03/07 10:26:55 quinn
169 * Initialized type field in the comstacks.
171 * Revision 1.5 1995/02/14 20:40:07 quinn
174 * Revision 1.4 1995/02/14 11:54:49 quinn
175 * Beginning to add full CCL.
177 * Revision 1.3 1995/02/10 18:58:10 quinn
178 * Fixed tcpip_get (formerly tcpip_read).
179 * Turned tst (cli) into a proper, event-driven thingy.
181 * Revision 1.2 1995/02/10 15:55:47 quinn
184 * Revision 1.1 1995/02/09 15:51:52 quinn
197 #if HAVE_OPENSSL_SSL_H
198 #include <openssl/ssl.h>
199 #include <openssl/err.h>
202 #include <yaz/comstack.h>
203 #include <yaz/tcpip.h>
206 /* Chas added the following, so we get the definition of completeBER */
209 int tcpip_close(COMSTACK h);
210 int tcpip_put(COMSTACK h, char *buf, int size);
211 int tcpip_get(COMSTACK h, char **buf, int *bufsize);
212 int tcpip_connect(COMSTACK h, void *address);
213 int tcpip_more(COMSTACK h);
214 int tcpip_rcvconnect(COMSTACK h);
215 int tcpip_bind(COMSTACK h, void *address, int mode);
216 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
217 int (*check_ip)(void *cd, const char *a, int len, int type),
219 int static tcpip_set_blocking(COMSTACK p, int blocking);
221 #if HAVE_OPENSSL_SSL_H
222 int ssl_get(COMSTACK h, char **buf, int *bufsize);
223 int ssl_put(COMSTACK h, char *buf, int size);
226 COMSTACK tcpip_accept(COMSTACK h);
227 char *tcpip_addrstr(COMSTACK h);
228 void *tcpip_straddr(COMSTACK h, const char *str);
236 /* this state is used for both SSL and straight TCP/IP */
237 typedef struct tcpip_state
239 char *altbuf; /* alternate buffer for surplus data */
240 int altsize; /* size as xmalloced */
241 int altlen; /* length of data or 0 if none */
243 int written; /* -1 if we aren't writing */
244 int towrite; /* to verify against user input */
245 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
246 struct sockaddr_in addr; /* returned by cs_straddr */
247 char buf[128]; /* returned by cs_addrstr */
248 #if HAVE_OPENSSL_SSL_H
256 static int tcpip_init (void)
258 static int initialized = 0;
264 requested = MAKEWORD(1, 1);
265 if (WSAStartup(requested, &wd))
272 static int proto_number = 0;
274 static int tcpip_init (void)
276 struct protoent *proto;
277 /* only call getprotobyname once, in case it allocates memory */
278 if (!(proto = getprotobyname("tcp")))
280 proto_number = proto->p_proto;
286 * This function is always called through the cs_create() macro.
287 * s >= 0: socket has already been established for us.
289 COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
295 unsigned long tru = 1;
303 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
306 if ((s = socket(AF_INET, SOCK_STREAM, proto_number)) < 0)
313 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
315 if (!(state = (struct tcpip_state *)(p->cprivate =
316 xmalloc(sizeof(tcpip_state)))))
320 if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0)
322 if (!(p->blocking = blocking) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
328 p->type = tcpip_type;
329 p->protocol = (enum oid_proto) protocol;
331 p->f_connect = tcpip_connect;
332 p->f_rcvconnect = tcpip_rcvconnect;
333 p->f_get = tcpip_get;
334 p->f_put = tcpip_put;
335 p->f_close = tcpip_close;
336 p->f_more = tcpip_more;
337 p->f_bind = tcpip_bind;
338 p->f_listen = tcpip_listen;
339 p->f_accept = tcpip_accept;
340 p->f_addrstr = tcpip_addrstr;
341 p->f_straddr = tcpip_straddr;
342 p->f_set_blocking = tcpip_set_blocking;
344 p->state = new_socket ? CS_UNBND : CS_IDLE; /* state of line */
349 #if HAVE_OPENSSL_SSL_H
350 state->ctx = state->ctx_alloc = 0;
355 state->altsize = state->altlen = 0;
356 state->towrite = state->written = -1;
357 if (protocol == PROTO_WAIS)
358 state->complete = completeWAIS;
360 state->complete = completeBER;
362 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
363 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
368 #if HAVE_OPENSSL_SSL_H
370 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
374 yaz_log(LOG_LOG, "ssl_type begin");
376 p = tcpip_type (s, blocking, protocol, 0);
382 state = (tcpip_state *) p->cprivate;
387 SSL_load_error_strings();
388 SSLeay_add_all_algorithms();
390 state->ctx = state->ctx_alloc = SSL_CTX_new (SSLv23_method());
397 /* note: we don't handle already opened socket in SSL mode - yet */
398 yaz_log(LOG_LOG, "ssl_type end");
403 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add)
407 short int port = 210;
412 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
413 add->sin_family = AF_INET;
414 strncpy(buf, str, 511);
416 if ((p = strchr(buf, '/')))
418 if ((p = strchr(buf, ':')))
423 add->sin_port = htons(port);
424 if (!strcmp("@", buf))
425 add->sin_addr.s_addr = INADDR_ANY;
426 else if ((hp = gethostbyname(buf)))
427 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
428 sizeof(struct in_addr));
429 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
430 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
436 void *tcpip_straddr(COMSTACK h, const char *str)
438 tcpip_state *sp = (tcpip_state *)h->cprivate;
440 if (!tcpip_strtoaddr_ex (str, &sp->addr))
445 struct sockaddr_in *tcpip_strtoaddr(const char *str)
447 static struct sockaddr_in add;
449 if (!tcpip_strtoaddr_ex (str, &add))
454 int tcpip_more(COMSTACK h)
456 tcpip_state *sp = (tcpip_state *)h->cprivate;
458 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
463 * connect(2) will block (sometimes) - nothing we can do short of doing
464 * weird things like spawning subprocesses or threading or some weird junk
467 int tcpip_connect(COMSTACK h, void *address)
469 struct sockaddr_in *add = (struct sockaddr_in *)address;
470 #if HAVE_OPENSSL_SSL_H
471 tcpip_state *sp = (tcpip_state *)h->cprivate;
475 TRC(fprintf(stderr, "tcpip_connect\n"));
477 if (h->state == CS_UNBND)
479 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
483 if (WSAGetLastError() == WSAEWOULDBLOCK)
485 h->state = CS_CONNECTING;
486 h->io_pending = CS_WANT_WRITE;
490 if (errno == EINPROGRESS)
492 h->state = CS_CONNECTING;
493 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
499 h->state = CS_CONNECTING;
501 if (h->state != CS_CONNECTING)
503 h->cerrno = CSOUTSTATE;
506 #if HAVE_OPENSSL_SSL_H
513 sp->ssl = SSL_new (sp->ctx);
514 SSL_set_fd (sp->ssl, h->iofile);
516 res = SSL_connect (sp->ssl);
519 int err = SSL_get_error(sp->ssl, res);
520 if (err == SSL_ERROR_WANT_READ)
522 yaz_log (LOG_LOG, "SSL_connect. want_read");
523 h->io_pending = CS_WANT_READ;
526 if (err == SSL_ERROR_WANT_WRITE)
528 yaz_log (LOG_LOG, "SSL_connect. want_write");
529 h->io_pending = CS_WANT_WRITE;
536 h->state = CS_DATAXFER;
543 int tcpip_rcvconnect(COMSTACK h)
545 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
549 #define CERTF "ztest.pem"
550 #define KEYF "ztest.pem"
552 int tcpip_bind(COMSTACK h, void *address, int mode)
554 struct sockaddr *addr = (struct sockaddr *)address;
558 unsigned long one = 1;
561 #if HAVE_OPENSSL_SSL_H
562 tcpip_state *sp = (tcpip_state *)h->cprivate;
568 res = SSL_CTX_use_certificate_file (sp->ctx, CERTF,
572 ERR_print_errors_fp(stderr);
575 res = SSL_CTX_use_PrivateKey_file (sp->ctx, KEYF,
579 ERR_print_errors_fp(stderr);
582 res = SSL_CTX_check_private_key (sp->ctx);
585 ERR_print_errors_fp(stderr);
589 TRC (fprintf (stderr, "ssl_bind\n"));
593 TRC (fprintf (stderr, "tcpip_bind\n"));
596 TRC (fprintf (stderr, "tcpip_bind\n"));
598 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
599 &one, sizeof(one)) < 0)
604 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)) < 0)
609 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
618 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
619 int (*check_ip)(void *cd, const char *a, int len, int t),
622 struct sockaddr_in addr;
624 socklen_t len = sizeof(addr);
626 int len = sizeof(addr);
629 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
630 if (h->state != CS_IDLE)
632 h->cerrno = CSOUTSTATE;
635 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
640 WSAGetLastError() == WSAEWOULDBLOCK
645 h->cerrno = CSNODATA;
650 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
651 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
654 if (check_ip && (*check_ip)(cd, (const char *) &addr,
655 sizeof(addr), AF_INET))
659 closesocket(h->newfd);
670 COMSTACK tcpip_accept(COMSTACK h)
673 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
675 unsigned long tru = 1;
678 TRC(fprintf(stderr, "tcpip_accept\n"));
679 if (h->state == CS_INCON)
681 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
685 closesocket(h->newfd);
692 memcpy(cnew, h, sizeof(*h));
693 cnew->iofile = h->newfd;
694 cnew->io_pending = 0;
695 if (!(state = (tcpip_state *)
696 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
702 closesocket(h->newfd);
710 if (!cnew->blocking &&
712 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
714 (!cnew->blocking && fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
722 closesocket(h->newfd);
734 state->altsize = state->altlen = 0;
735 state->towrite = state->written = -1;
736 state->complete = st->complete;
737 cnew->state = CS_ACCEPT;
740 #if HAVE_OPENSSL_SSL_H
741 state->ctx = st->ctx;
742 state->ctx_alloc = 0;
743 state->ssl = st->ssl;
746 state->ssl = SSL_new (state->ctx);
747 SSL_set_fd (state->ssl, cnew->iofile);
752 if (h->state == CS_ACCEPT)
754 #if HAVE_OPENSSL_SSL_H
755 tcpip_state *state = (tcpip_state *)h->cprivate;
758 int res = SSL_accept (state->ssl);
759 TRC(fprintf(stderr, "SSL_accept\n"));
762 int err = SSL_get_error(state->ssl, res);
763 if (err == SSL_ERROR_WANT_READ)
765 h->io_pending = CS_WANT_READ;
766 yaz_log (LOG_LOG, "SSL_accept. want_read");
769 if (err == SSL_ERROR_WANT_WRITE)
771 h->io_pending = CS_WANT_WRITE;
772 yaz_log (LOG_LOG, "SSL_accept. want_write");
783 h->cerrno = CSOUTSTATE;
787 h->state = CS_DATAXFER;
791 #define CS_TCPIP_BUFCHUNK 4096
794 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
795 * 0=connection closed.
797 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
799 tcpip_state *sp = (tcpip_state *)h->cprivate;
801 int tmpi, berlen, rest, req, tomove;
802 int hasread = 0, res;
804 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
805 if (sp->altlen) /* switch buffers */
807 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
808 (unsigned) sp->altbuf));
812 *bufsize = sp->altsize;
813 hasread = sp->altlen;
819 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
823 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
826 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
827 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
829 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
830 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
834 if (WSAGetLastError() == WSAEWOULDBLOCK)
836 h->io_pending = CS_WANT_READ;
842 if (errno == EWOULDBLOCK
844 || errno == EINPROGRESS
848 h->io_pending = CS_WANT_READ;
861 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
863 /* move surplus buffer (or everything if we didn't get a BER rec.) */
864 if (hasread > berlen)
866 tomove = req = hasread - berlen;
867 rest = tomove % CS_TCPIP_BUFCHUNK;
869 req += CS_TCPIP_BUFCHUNK - rest;
872 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
874 } else if (sp->altsize < req)
875 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
877 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
878 (unsigned) sp->altbuf));
879 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
881 if (berlen < CS_TCPIP_BUFCHUNK - 1)
882 *(*buf + berlen) = '\0';
883 return berlen ? berlen : 1;
887 #if HAVE_OPENSSL_SSL_H
889 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
890 * 0=connection closed.
892 int ssl_get(COMSTACK h, char **buf, int *bufsize)
894 tcpip_state *sp = (tcpip_state *)h->cprivate;
896 int tmpi, berlen, rest, req, tomove;
897 int hasread = 0, res;
899 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
900 if (sp->altlen) /* switch buffers */
902 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
903 (unsigned) sp->altbuf));
907 *bufsize = sp->altsize;
908 hasread = sp->altlen;
914 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
918 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
921 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
922 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
924 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
925 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
928 int ssl_err = SSL_get_error(sp->ssl, res);
929 if (ssl_err == SSL_ERROR_WANT_READ)
931 h->io_pending = CS_WANT_READ;
932 yaz_log (LOG_LOG, "SSL_read. want_read");
935 if (ssl_err == SSL_ERROR_WANT_WRITE)
937 h->io_pending = CS_WANT_WRITE;
938 yaz_log (LOG_LOG, "SSL_read. want_write");
947 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
949 /* move surplus buffer (or everything if we didn't get a BER rec.) */
950 if (hasread > berlen)
952 tomove = req = hasread - berlen;
953 rest = tomove % CS_TCPIP_BUFCHUNK;
955 req += CS_TCPIP_BUFCHUNK - rest;
958 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
960 } else if (sp->altsize < req)
961 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
963 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
964 (unsigned) sp->altbuf));
965 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
967 if (berlen < CS_TCPIP_BUFCHUNK - 1)
968 *(*buf + berlen) = '\0';
969 return berlen ? berlen : 1;
975 * In nonblocking mode, you must call again with same buffer while
978 int tcpip_put(COMSTACK h, char *buf, int size)
981 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
983 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
985 if (state->towrite < 0)
987 state->towrite = size;
990 else if (state->towrite != size)
992 h->cerrno = CSWRONGBUF;
995 while (state->towrite > state->written)
997 if ((res = send(h->iofile, buf + state->written, size -
998 state->written, 0)) < 0)
1002 WSAGetLastError() == WSAEWOULDBLOCK
1008 TRC(fprintf(stderr, " Flow control stop\n"));
1009 h->io_pending = CS_WANT_WRITE;
1012 h->cerrno = CSYSERR;
1015 state->written += res;
1016 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1017 res, state->written, size));
1019 state->towrite = state->written = -1;
1020 TRC(fprintf(stderr, " Ok\n"));
1025 #if HAVE_OPENSSL_SSL_H
1027 * Returns 1, 0 or -1
1028 * In nonblocking mode, you must call again with same buffer while
1029 * return value is 1.
1031 int ssl_put(COMSTACK h, char *buf, int size)
1034 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1036 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1038 if (state->towrite < 0)
1040 state->towrite = size;
1043 else if (state->towrite != size)
1045 h->cerrno = CSWRONGBUF;
1048 while (state->towrite > state->written)
1050 res = SSL_write (state->ssl, buf + state->written,
1051 size - state->written);
1054 int ssl_err = SSL_get_error(state->ssl, res);
1055 if (ssl_err == SSL_ERROR_WANT_READ)
1057 h->io_pending = CS_WANT_READ;
1058 yaz_log (LOG_LOG, "SSL_write. want_read");
1061 if (ssl_err == SSL_ERROR_WANT_WRITE)
1063 h->io_pending = CS_WANT_WRITE;
1064 yaz_log (LOG_LOG, "SSL_write. want_write");
1069 state->written += res;
1070 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1071 res, state->written, size));
1073 state->towrite = state->written = -1;
1074 TRC(fprintf(stderr, " Ok\n"));
1079 int tcpip_close(COMSTACK h)
1081 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1083 TRC(fprintf(stderr, "tcpip_close\n"));
1084 if (h->iofile != -1)
1086 #if HAVE_OPENSSL_SSL_H
1089 SSL_shutdown (sp->ssl);
1093 closesocket(h->iofile);
1100 #if HAVE_OPENSSL_SSL_H
1103 TRC (fprintf(stderr, "SSL_free\n"));
1108 SSL_CTX_free (sp->ctx_alloc);
1115 char *tcpip_addrstr(COMSTACK h)
1117 struct sockaddr_in addr;
1118 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1119 char *r, *buf = sp->buf;
1121 struct hostent *host;
1124 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1126 h->cerrno = CSYSERR;
1129 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1131 r = (char*) host->h_name;
1133 r = inet_ntoa(addr.sin_addr);
1134 sprintf(buf, "tcp:%s", r);
1135 #if HAVE_OPENSSL_SSL_H
1137 sprintf(buf, "ssl:%s", r);
1142 int static tcpip_set_blocking(COMSTACK p, int blocking)
1146 if (p->blocking == blocking)
1151 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1154 flag = fcntl(p->iofile, F_GETFL, 0);
1156 flag = flag & ~O_NONBLOCK;
1158 flag = flag | O_NONBLOCK;
1159 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1162 p->blocking = blocking;