2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
6 * Revision 1.39 2001-07-19 19:49:40 adam
7 * Fixed bug in tcpip_set_blocking.
9 * Revision 1.38 2001/03/21 12:43:36 adam
10 * Implemented cs_create_host. Better error reporting for SSL comstack.
12 * Revision 1.37 2001/03/08 20:18:55 adam
13 * Added cs_set_blocking. Patch from Matthew Carey.
15 * Revision 1.36 2001/02/21 13:46:53 adam
18 * Revision 1.35 2000/11/27 15:17:40 adam
19 * Using SSLeay_add_all_algorithms instead of OpenSSL_add_all_algorithms.
21 * Revision 1.34 2000/11/23 10:58:32 adam
22 * SSL comstack support. Separate POSIX thread support library.
24 * Revision 1.33 2000/09/04 08:27:11 adam
25 * Work on error handling for tcpip_accept.
27 * Revision 1.32 1999/11/30 13:47:11 adam
28 * Improved installation. Moved header files to include/yaz.
30 * Revision 1.31 1999/04/29 07:31:23 adam
31 * Changed tcpip_strtoaddr_ex so that only part 'till '/' is considered
34 * Revision 1.30 1999/04/20 09:56:48 adam
35 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
36 * Modified all encoders/decoders to reflect this change.
38 * Revision 1.29 1999/04/16 14:45:55 adam
39 * Added interface for tcpd wrapper for access control.
41 * Revision 1.28 1999/03/31 11:11:14 adam
42 * Function getprotobyname only called once. Minor change in tcpip_get
43 * to handle multi-threaded conditions.
45 * Revision 1.27 1999/02/02 13:57:31 adam
46 * Uses preprocessor define WIN32 instead of WINDOWS to build code
47 * for Microsoft WIN32.
49 * Revision 1.26 1999/01/08 11:23:14 adam
50 * Added const modifier to some of the BER/ODR encoding routines.
52 * Revision 1.25 1998/07/07 15:49:23 adam
53 * Added braces to avoid warning.
55 * Revision 1.24 1998/06/29 07:59:17 adam
58 * Revision 1.23 1998/06/23 15:37:50 adam
59 * Added type cast to prevent warning.
61 * Revision 1.22 1998/06/22 11:32:36 adam
62 * Added 'conditional cs_listen' feature.
64 * Revision 1.21 1998/05/20 09:55:32 adam
65 * Function tcpip_get treats EINPROGRESS error in the same way as
66 * EWOULDBLOCK. EINPROGRESS shouldn't be returned - but it is on
67 * Solaris in some cases.
69 * Revision 1.20 1998/05/18 10:10:40 adam
70 * Minor change to avoid C++ warning.
72 * Revision 1.19 1998/02/11 11:53:33 adam
73 * Changed code so that it compiles as C++.
75 * Revision 1.18 1997/09/29 07:15:25 adam
76 * Changed use of setsockopt to avoid warnings on MSVC.
78 * Revision 1.17 1997/09/17 12:10:30 adam
81 * Revision 1.16 1997/09/01 08:49:14 adam
82 * New windows NT/95 port using MSV5.0. Minor changes only.
84 * Revision 1.15 1997/05/14 06:53:33 adam
87 * Revision 1.14 1997/05/01 15:06:32 adam
88 * Moved WINSOCK init. code to tcpip_init routine.
90 * Revision 1.13 1996/11/01 08:45:18 adam
91 * Bug fix: used close on MS-Windows. Fixed to closesocket.
93 * Revision 1.12 1996/07/06 19:58:30 quinn
94 * System headerfiles gathered in yconfig
96 * Revision 1.11 1996/02/23 10:00:39 quinn
99 * Revision 1.10 1996/02/20 12:52:11 quinn
100 * WAIS protocol support.
102 * Revision 1.9 1996/02/10 12:23:11 quinn
103 * Enablie inetd operations fro TCP/IP stack
105 * Revision 1.8 1995/11/01 13:54:27 quinn
108 * Revision 1.7 1995/10/30 12:41:16 quinn
109 * Added hostname lookup for server.
111 * Revision 1.6 1995/09/29 17:12:00 quinn
114 * Revision 1.5 1995/09/29 17:01:48 quinn
117 * Revision 1.4 1995/09/28 10:12:26 quinn
118 * Windows-support changes
120 * Revision 1.3 1995/09/27 15:02:45 quinn
121 * Modified function heads & prototypes.
123 * Revision 1.2 1995/06/15 12:30:06 quinn
124 * Added @ as hostname alias for INADDR ANY.
126 * Revision 1.1 1995/06/14 09:58:20 quinn
127 * Renamed yazlib to comstack.
129 * Revision 1.20 1995/05/16 08:51:16 quinn
130 * License, documentation, and memory fixes
132 * Revision 1.19 1995/04/10 10:24:08 quinn
135 * Revision 1.18 1995/03/30 13:29:27 quinn
136 * Added REUSEADDR in tcpip_bind
138 * Revision 1.17 1995/03/27 08:36:10 quinn
139 * Some work on nonblocking operation in xmosi.c and rfct.c.
140 * Added protocol parameter to cs_create()
142 * Revision 1.16 1995/03/21 15:53:41 quinn
145 * Revision 1.15 1995/03/21 12:31:27 quinn
146 * Added check for EINPROGRESS on connect.
148 * Revision 1.14 1995/03/20 09:47:21 quinn
149 * Added server-side support to xmosi.c
150 * Fixed possible problems in rfct
153 * Revision 1.13 1995/03/15 16:15:13 adam
156 * Revision 1.12 1995/03/15 15:36:27 quinn
157 * Mods to support nonblocking I/O
159 * Revision 1.11 1995/03/15 08:37:57 quinn
160 * Now we're pretty much set for nonblocking I/O.
162 * Revision 1.10 1995/03/14 17:00:07 quinn
163 * Bug-fixes - added tracing info to tcpip.c
165 * Revision 1.9 1995/03/14 10:28:42 quinn
166 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
168 * Revision 1.8 1995/03/10 14:22:50 quinn
169 * Removed debug output.
171 * Revision 1.7 1995/03/10 11:44:59 quinn
172 * Fixes and debugging
174 * Revision 1.6 1995/03/07 10:26:55 quinn
175 * Initialized type field in the comstacks.
177 * Revision 1.5 1995/02/14 20:40:07 quinn
180 * Revision 1.4 1995/02/14 11:54:49 quinn
181 * Beginning to add full CCL.
183 * Revision 1.3 1995/02/10 18:58:10 quinn
184 * Fixed tcpip_get (formerly tcpip_read).
185 * Turned tst (cli) into a proper, event-driven thingy.
187 * Revision 1.2 1995/02/10 15:55:47 quinn
190 * Revision 1.1 1995/02/09 15:51:52 quinn
203 #if HAVE_OPENSSL_SSL_H
204 #include <openssl/ssl.h>
205 #include <openssl/err.h>
208 #include <yaz/comstack.h>
209 #include <yaz/tcpip.h>
212 /* Chas added the following, so we get the definition of completeBER */
215 int tcpip_close(COMSTACK h);
216 int tcpip_put(COMSTACK h, char *buf, int size);
217 int tcpip_get(COMSTACK h, char **buf, int *bufsize);
218 int tcpip_connect(COMSTACK h, void *address);
219 int tcpip_more(COMSTACK h);
220 int tcpip_rcvconnect(COMSTACK h);
221 int tcpip_bind(COMSTACK h, void *address, int mode);
222 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
223 int (*check_ip)(void *cd, const char *a, int len, int type),
225 int static tcpip_set_blocking(COMSTACK p, int blocking);
227 #if HAVE_OPENSSL_SSL_H
228 int ssl_get(COMSTACK h, char **buf, int *bufsize);
229 int ssl_put(COMSTACK h, char *buf, int size);
232 COMSTACK tcpip_accept(COMSTACK h);
233 char *tcpip_addrstr(COMSTACK h);
234 void *tcpip_straddr(COMSTACK h, const char *str);
242 /* this state is used for both SSL and straight TCP/IP */
243 typedef struct tcpip_state
245 char *altbuf; /* alternate buffer for surplus data */
246 int altsize; /* size as xmalloced */
247 int altlen; /* length of data or 0 if none */
249 int written; /* -1 if we aren't writing */
250 int towrite; /* to verify against user input */
251 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
252 struct sockaddr_in addr; /* returned by cs_straddr */
253 char buf[128]; /* returned by cs_addrstr */
254 #if HAVE_OPENSSL_SSL_H
262 static int tcpip_init (void)
264 static int initialized = 0;
270 requested = MAKEWORD(1, 1);
271 if (WSAStartup(requested, &wd))
278 static int proto_number = 0;
280 static int tcpip_init (void)
282 struct protoent *proto;
283 /* only call getprotobyname once, in case it allocates memory */
284 if (!(proto = getprotobyname("tcp")))
286 proto_number = proto->p_proto;
292 * This function is always called through the cs_create() macro.
293 * s >= 0: socket has already been established for us.
295 COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
301 unsigned long tru = 1;
309 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
312 if ((s = socket(AF_INET, SOCK_STREAM, proto_number)) < 0)
319 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
321 if (!(state = (struct tcpip_state *)(p->cprivate =
322 xmalloc(sizeof(tcpip_state)))))
326 if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0)
328 if (!(p->blocking = blocking) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
334 p->type = tcpip_type;
335 p->protocol = (enum oid_proto) protocol;
337 p->f_connect = tcpip_connect;
338 p->f_rcvconnect = tcpip_rcvconnect;
339 p->f_get = tcpip_get;
340 p->f_put = tcpip_put;
341 p->f_close = tcpip_close;
342 p->f_more = tcpip_more;
343 p->f_bind = tcpip_bind;
344 p->f_listen = tcpip_listen;
345 p->f_accept = tcpip_accept;
346 p->f_addrstr = tcpip_addrstr;
347 p->f_straddr = tcpip_straddr;
348 p->f_set_blocking = tcpip_set_blocking;
350 p->state = new_socket ? CS_UNBND : CS_IDLE; /* state of line */
355 #if HAVE_OPENSSL_SSL_H
356 state->ctx = state->ctx_alloc = 0;
361 state->altsize = state->altlen = 0;
362 state->towrite = state->written = -1;
363 if (protocol == PROTO_WAIS)
364 state->complete = completeWAIS;
366 state->complete = completeBER;
368 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
369 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
374 #if HAVE_OPENSSL_SSL_H
376 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
380 yaz_log(LOG_LOG, "ssl_type begin");
382 p = tcpip_type (s, blocking, protocol, 0);
388 state = (tcpip_state *) p->cprivate;
393 SSL_load_error_strings();
394 SSLeay_add_all_algorithms();
396 state->ctx = state->ctx_alloc = SSL_CTX_new (SSLv23_method());
403 /* note: we don't handle already opened socket in SSL mode - yet */
404 yaz_log(LOG_LOG, "ssl_type end");
409 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add)
413 short int port = 210;
418 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
419 add->sin_family = AF_INET;
420 strncpy(buf, str, 511);
422 if ((p = strchr(buf, '/')))
424 if ((p = strchr(buf, ':')))
429 add->sin_port = htons(port);
430 if (!strcmp("@", buf))
431 add->sin_addr.s_addr = INADDR_ANY;
432 else if ((hp = gethostbyname(buf)))
433 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
434 sizeof(struct in_addr));
435 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
436 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
442 void *tcpip_straddr(COMSTACK h, const char *str)
444 tcpip_state *sp = (tcpip_state *)h->cprivate;
446 if (!tcpip_strtoaddr_ex (str, &sp->addr))
451 struct sockaddr_in *tcpip_strtoaddr(const char *str)
453 static struct sockaddr_in add;
455 if (!tcpip_strtoaddr_ex (str, &add))
460 int tcpip_more(COMSTACK h)
462 tcpip_state *sp = (tcpip_state *)h->cprivate;
464 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
469 * connect(2) will block (sometimes) - nothing we can do short of doing
470 * weird things like spawning subprocesses or threading or some weird junk
473 int tcpip_connect(COMSTACK h, void *address)
475 struct sockaddr_in *add = (struct sockaddr_in *)address;
476 #if HAVE_OPENSSL_SSL_H
477 tcpip_state *sp = (tcpip_state *)h->cprivate;
481 TRC(fprintf(stderr, "tcpip_connect\n"));
483 if (h->state == CS_UNBND)
485 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
489 if (WSAGetLastError() == WSAEWOULDBLOCK)
491 h->state = CS_CONNECTING;
492 h->io_pending = CS_WANT_WRITE;
496 if (errno == EINPROGRESS)
498 h->state = CS_CONNECTING;
499 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
506 h->state = CS_CONNECTING;
508 if (h->state != CS_CONNECTING)
510 h->cerrno = CSOUTSTATE;
513 #if HAVE_OPENSSL_SSL_H
520 sp->ssl = SSL_new (sp->ctx);
521 SSL_set_fd (sp->ssl, h->iofile);
523 res = SSL_connect (sp->ssl);
526 int err = SSL_get_error(sp->ssl, res);
527 if (err == SSL_ERROR_WANT_READ)
529 yaz_log (LOG_LOG, "SSL_connect. want_read");
530 h->io_pending = CS_WANT_READ;
533 if (err == SSL_ERROR_WANT_WRITE)
535 yaz_log (LOG_LOG, "SSL_connect. want_write");
536 h->io_pending = CS_WANT_WRITE;
539 h->cerrno = CSERRORSSL;
544 h->state = CS_DATAXFER;
551 int tcpip_rcvconnect(COMSTACK h)
553 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
557 #define CERTF "ztest.pem"
558 #define KEYF "ztest.pem"
560 int tcpip_bind(COMSTACK h, void *address, int mode)
562 struct sockaddr *addr = (struct sockaddr *)address;
566 unsigned long one = 1;
569 #if HAVE_OPENSSL_SSL_H
570 tcpip_state *sp = (tcpip_state *)h->cprivate;
576 res = SSL_CTX_use_certificate_file (sp->ctx, CERTF,
580 ERR_print_errors_fp(stderr);
583 res = SSL_CTX_use_PrivateKey_file (sp->ctx, KEYF,
587 ERR_print_errors_fp(stderr);
590 res = SSL_CTX_check_private_key (sp->ctx);
593 ERR_print_errors_fp(stderr);
597 TRC (fprintf (stderr, "ssl_bind\n"));
601 TRC (fprintf (stderr, "tcpip_bind\n"));
604 TRC (fprintf (stderr, "tcpip_bind\n"));
606 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
607 &one, sizeof(one)) < 0)
612 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)) < 0)
617 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
626 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
627 int (*check_ip)(void *cd, const char *a, int len, int t),
630 struct sockaddr_in addr;
632 socklen_t len = sizeof(addr);
634 int len = sizeof(addr);
637 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
638 if (h->state != CS_IDLE)
640 h->cerrno = CSOUTSTATE;
643 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
648 WSAGetLastError() == WSAEWOULDBLOCK
653 h->cerrno = CSNODATA;
658 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
659 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
662 if (check_ip && (*check_ip)(cd, (const char *) &addr,
663 sizeof(addr), AF_INET))
667 closesocket(h->newfd);
678 COMSTACK tcpip_accept(COMSTACK h)
681 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
683 unsigned long tru = 1;
686 TRC(fprintf(stderr, "tcpip_accept\n"));
687 if (h->state == CS_INCON)
689 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
693 closesocket(h->newfd);
700 memcpy(cnew, h, sizeof(*h));
701 cnew->iofile = h->newfd;
702 cnew->io_pending = 0;
703 if (!(state = (tcpip_state *)
704 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
710 closesocket(h->newfd);
718 if (!cnew->blocking &&
720 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
722 (!cnew->blocking && fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
730 closesocket(h->newfd);
742 state->altsize = state->altlen = 0;
743 state->towrite = state->written = -1;
744 state->complete = st->complete;
745 cnew->state = CS_ACCEPT;
748 #if HAVE_OPENSSL_SSL_H
749 state->ctx = st->ctx;
750 state->ctx_alloc = 0;
751 state->ssl = st->ssl;
754 state->ssl = SSL_new (state->ctx);
755 SSL_set_fd (state->ssl, cnew->iofile);
760 if (h->state == CS_ACCEPT)
762 #if HAVE_OPENSSL_SSL_H
763 tcpip_state *state = (tcpip_state *)h->cprivate;
766 int res = SSL_accept (state->ssl);
767 TRC(fprintf(stderr, "SSL_accept\n"));
770 int err = SSL_get_error(state->ssl, res);
771 if (err == SSL_ERROR_WANT_READ)
773 h->io_pending = CS_WANT_READ;
774 yaz_log (LOG_LOG, "SSL_accept. want_read");
777 if (err == SSL_ERROR_WANT_WRITE)
779 h->io_pending = CS_WANT_WRITE;
780 yaz_log (LOG_LOG, "SSL_accept. want_write");
791 h->cerrno = CSOUTSTATE;
795 h->state = CS_DATAXFER;
799 #define CS_TCPIP_BUFCHUNK 4096
802 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
803 * 0=connection closed.
805 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
807 tcpip_state *sp = (tcpip_state *)h->cprivate;
809 int tmpi, berlen, rest, req, tomove;
810 int hasread = 0, res;
812 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
813 if (sp->altlen) /* switch buffers */
815 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
816 (unsigned) sp->altbuf));
820 *bufsize = sp->altsize;
821 hasread = sp->altlen;
827 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
831 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
834 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
835 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
837 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
838 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
842 if (WSAGetLastError() == WSAEWOULDBLOCK)
844 h->io_pending = CS_WANT_READ;
850 if (errno == EWOULDBLOCK
852 || errno == EINPROGRESS
856 h->io_pending = CS_WANT_READ;
869 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
871 /* move surplus buffer (or everything if we didn't get a BER rec.) */
872 if (hasread > berlen)
874 tomove = req = hasread - berlen;
875 rest = tomove % CS_TCPIP_BUFCHUNK;
877 req += CS_TCPIP_BUFCHUNK - rest;
880 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
882 } else if (sp->altsize < req)
883 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
885 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
886 (unsigned) sp->altbuf));
887 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
889 if (berlen < CS_TCPIP_BUFCHUNK - 1)
890 *(*buf + berlen) = '\0';
891 return berlen ? berlen : 1;
895 #if HAVE_OPENSSL_SSL_H
897 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
898 * 0=connection closed.
900 int ssl_get(COMSTACK h, char **buf, int *bufsize)
902 tcpip_state *sp = (tcpip_state *)h->cprivate;
904 int tmpi, berlen, rest, req, tomove;
905 int hasread = 0, res;
907 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
908 if (sp->altlen) /* switch buffers */
910 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
911 (unsigned) sp->altbuf));
915 *bufsize = sp->altsize;
916 hasread = sp->altlen;
922 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
926 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
929 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
930 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
932 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
933 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
936 int ssl_err = SSL_get_error(sp->ssl, res);
937 if (ssl_err == SSL_ERROR_WANT_READ)
939 h->io_pending = CS_WANT_READ;
940 yaz_log (LOG_LOG, "SSL_read. want_read");
943 if (ssl_err == SSL_ERROR_WANT_WRITE)
945 h->io_pending = CS_WANT_WRITE;
946 yaz_log (LOG_LOG, "SSL_read. want_write");
951 h->cerrno = CSERRORSSL;
956 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
958 /* move surplus buffer (or everything if we didn't get a BER rec.) */
959 if (hasread > berlen)
961 tomove = req = hasread - berlen;
962 rest = tomove % CS_TCPIP_BUFCHUNK;
964 req += CS_TCPIP_BUFCHUNK - rest;
967 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
969 } else if (sp->altsize < req)
970 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
972 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
973 (unsigned) sp->altbuf));
974 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
976 if (berlen < CS_TCPIP_BUFCHUNK - 1)
977 *(*buf + berlen) = '\0';
978 return berlen ? berlen : 1;
984 * In nonblocking mode, you must call again with same buffer while
987 int tcpip_put(COMSTACK h, char *buf, int size)
990 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
992 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
994 if (state->towrite < 0)
996 state->towrite = size;
999 else if (state->towrite != size)
1001 h->cerrno = CSWRONGBUF;
1004 while (state->towrite > state->written)
1006 if ((res = send(h->iofile, buf + state->written, size -
1007 state->written, 0)) < 0)
1011 WSAGetLastError() == WSAEWOULDBLOCK
1017 TRC(fprintf(stderr, " Flow control stop\n"));
1018 h->io_pending = CS_WANT_WRITE;
1021 h->cerrno = CSYSERR;
1024 state->written += res;
1025 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1026 res, state->written, size));
1028 state->towrite = state->written = -1;
1029 TRC(fprintf(stderr, " Ok\n"));
1034 #if HAVE_OPENSSL_SSL_H
1036 * Returns 1, 0 or -1
1037 * In nonblocking mode, you must call again with same buffer while
1038 * return value is 1.
1040 int ssl_put(COMSTACK h, char *buf, int size)
1043 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1045 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1047 if (state->towrite < 0)
1049 state->towrite = size;
1052 else if (state->towrite != size)
1054 h->cerrno = CSWRONGBUF;
1057 while (state->towrite > state->written)
1059 res = SSL_write (state->ssl, buf + state->written,
1060 size - state->written);
1063 int ssl_err = SSL_get_error(state->ssl, res);
1064 if (ssl_err == SSL_ERROR_WANT_READ)
1066 h->io_pending = CS_WANT_READ;
1067 yaz_log (LOG_LOG, "SSL_write. want_read");
1070 if (ssl_err == SSL_ERROR_WANT_WRITE)
1072 h->io_pending = CS_WANT_WRITE;
1073 yaz_log (LOG_LOG, "SSL_write. want_write");
1076 h->cerrno = CSERRORSSL;
1079 state->written += res;
1080 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1081 res, state->written, size));
1083 state->towrite = state->written = -1;
1084 TRC(fprintf(stderr, " Ok\n"));
1089 int tcpip_close(COMSTACK h)
1091 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1093 TRC(fprintf(stderr, "tcpip_close\n"));
1094 if (h->iofile != -1)
1096 #if HAVE_OPENSSL_SSL_H
1099 SSL_shutdown (sp->ssl);
1103 closesocket(h->iofile);
1110 #if HAVE_OPENSSL_SSL_H
1113 TRC (fprintf(stderr, "SSL_free\n"));
1118 SSL_CTX_free (sp->ctx_alloc);
1125 char *tcpip_addrstr(COMSTACK h)
1127 struct sockaddr_in addr;
1128 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1129 char *r, *buf = sp->buf;
1131 struct hostent *host;
1134 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1136 h->cerrno = CSYSERR;
1139 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1141 r = (char*) host->h_name;
1143 r = inet_ntoa(addr.sin_addr);
1144 sprintf(buf, "tcp:%s", r);
1145 #if HAVE_OPENSSL_SSL_H
1147 sprintf(buf, "ssl:%s", r);
1152 int static tcpip_set_blocking(COMSTACK p, int blocking)
1156 if (p->blocking == blocking)
1160 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1163 flag = fcntl(p->iofile, F_GETFL, 0);
1165 flag = flag & ~O_NONBLOCK;
1167 flag = flag | O_NONBLOCK;
1168 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1171 p->blocking = blocking;