2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
6 * Revision 1.40 2001-08-23 09:02:46 adam
7 * WIN32 fixes: Socket not re-used for bind. yaz_log logs WIN32 error
10 * Revision 1.39 2001/07/19 19:49:40 adam
11 * Fixed bug in tcpip_set_blocking.
13 * Revision 1.38 2001/03/21 12:43:36 adam
14 * Implemented cs_create_host. Better error reporting for SSL comstack.
16 * Revision 1.37 2001/03/08 20:18:55 adam
17 * Added cs_set_blocking. Patch from Matthew Carey.
19 * Revision 1.36 2001/02/21 13:46:53 adam
22 * Revision 1.35 2000/11/27 15:17:40 adam
23 * Using SSLeay_add_all_algorithms instead of OpenSSL_add_all_algorithms.
25 * Revision 1.34 2000/11/23 10:58:32 adam
26 * SSL comstack support. Separate POSIX thread support library.
28 * Revision 1.33 2000/09/04 08:27:11 adam
29 * Work on error handling for tcpip_accept.
31 * Revision 1.32 1999/11/30 13:47:11 adam
32 * Improved installation. Moved header files to include/yaz.
34 * Revision 1.31 1999/04/29 07:31:23 adam
35 * Changed tcpip_strtoaddr_ex so that only part 'till '/' is considered
38 * Revision 1.30 1999/04/20 09:56:48 adam
39 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
40 * Modified all encoders/decoders to reflect this change.
42 * Revision 1.29 1999/04/16 14:45:55 adam
43 * Added interface for tcpd wrapper for access control.
45 * Revision 1.28 1999/03/31 11:11:14 adam
46 * Function getprotobyname only called once. Minor change in tcpip_get
47 * to handle multi-threaded conditions.
49 * Revision 1.27 1999/02/02 13:57:31 adam
50 * Uses preprocessor define WIN32 instead of WINDOWS to build code
51 * for Microsoft WIN32.
53 * Revision 1.26 1999/01/08 11:23:14 adam
54 * Added const modifier to some of the BER/ODR encoding routines.
56 * Revision 1.25 1998/07/07 15:49:23 adam
57 * Added braces to avoid warning.
59 * Revision 1.24 1998/06/29 07:59:17 adam
62 * Revision 1.23 1998/06/23 15:37:50 adam
63 * Added type cast to prevent warning.
65 * Revision 1.22 1998/06/22 11:32:36 adam
66 * Added 'conditional cs_listen' feature.
68 * Revision 1.21 1998/05/20 09:55:32 adam
69 * Function tcpip_get treats EINPROGRESS error in the same way as
70 * EWOULDBLOCK. EINPROGRESS shouldn't be returned - but it is on
71 * Solaris in some cases.
73 * Revision 1.20 1998/05/18 10:10:40 adam
74 * Minor change to avoid C++ warning.
76 * Revision 1.19 1998/02/11 11:53:33 adam
77 * Changed code so that it compiles as C++.
79 * Revision 1.18 1997/09/29 07:15:25 adam
80 * Changed use of setsockopt to avoid warnings on MSVC.
82 * Revision 1.17 1997/09/17 12:10:30 adam
85 * Revision 1.16 1997/09/01 08:49:14 adam
86 * New windows NT/95 port using MSV5.0. Minor changes only.
88 * Revision 1.15 1997/05/14 06:53:33 adam
91 * Revision 1.14 1997/05/01 15:06:32 adam
92 * Moved WINSOCK init. code to tcpip_init routine.
94 * Revision 1.13 1996/11/01 08:45:18 adam
95 * Bug fix: used close on MS-Windows. Fixed to closesocket.
97 * Revision 1.12 1996/07/06 19:58:30 quinn
98 * System headerfiles gathered in yconfig
100 * Revision 1.11 1996/02/23 10:00:39 quinn
103 * Revision 1.10 1996/02/20 12:52:11 quinn
104 * WAIS protocol support.
106 * Revision 1.9 1996/02/10 12:23:11 quinn
107 * Enablie inetd operations fro TCP/IP stack
109 * Revision 1.8 1995/11/01 13:54:27 quinn
112 * Revision 1.7 1995/10/30 12:41:16 quinn
113 * Added hostname lookup for server.
115 * Revision 1.6 1995/09/29 17:12:00 quinn
118 * Revision 1.5 1995/09/29 17:01:48 quinn
121 * Revision 1.4 1995/09/28 10:12:26 quinn
122 * Windows-support changes
124 * Revision 1.3 1995/09/27 15:02:45 quinn
125 * Modified function heads & prototypes.
127 * Revision 1.2 1995/06/15 12:30:06 quinn
128 * Added @ as hostname alias for INADDR ANY.
130 * Revision 1.1 1995/06/14 09:58:20 quinn
131 * Renamed yazlib to comstack.
133 * Revision 1.20 1995/05/16 08:51:16 quinn
134 * License, documentation, and memory fixes
136 * Revision 1.19 1995/04/10 10:24:08 quinn
139 * Revision 1.18 1995/03/30 13:29:27 quinn
140 * Added REUSEADDR in tcpip_bind
142 * Revision 1.17 1995/03/27 08:36:10 quinn
143 * Some work on nonblocking operation in xmosi.c and rfct.c.
144 * Added protocol parameter to cs_create()
146 * Revision 1.16 1995/03/21 15:53:41 quinn
149 * Revision 1.15 1995/03/21 12:31:27 quinn
150 * Added check for EINPROGRESS on connect.
152 * Revision 1.14 1995/03/20 09:47:21 quinn
153 * Added server-side support to xmosi.c
154 * Fixed possible problems in rfct
157 * Revision 1.13 1995/03/15 16:15:13 adam
160 * Revision 1.12 1995/03/15 15:36:27 quinn
161 * Mods to support nonblocking I/O
163 * Revision 1.11 1995/03/15 08:37:57 quinn
164 * Now we're pretty much set for nonblocking I/O.
166 * Revision 1.10 1995/03/14 17:00:07 quinn
167 * Bug-fixes - added tracing info to tcpip.c
169 * Revision 1.9 1995/03/14 10:28:42 quinn
170 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
172 * Revision 1.8 1995/03/10 14:22:50 quinn
173 * Removed debug output.
175 * Revision 1.7 1995/03/10 11:44:59 quinn
176 * Fixes and debugging
178 * Revision 1.6 1995/03/07 10:26:55 quinn
179 * Initialized type field in the comstacks.
181 * Revision 1.5 1995/02/14 20:40:07 quinn
184 * Revision 1.4 1995/02/14 11:54:49 quinn
185 * Beginning to add full CCL.
187 * Revision 1.3 1995/02/10 18:58:10 quinn
188 * Fixed tcpip_get (formerly tcpip_read).
189 * Turned tst (cli) into a proper, event-driven thingy.
191 * Revision 1.2 1995/02/10 15:55:47 quinn
194 * Revision 1.1 1995/02/09 15:51:52 quinn
207 #if HAVE_OPENSSL_SSL_H
208 #include <openssl/ssl.h>
209 #include <openssl/err.h>
212 #include <yaz/comstack.h>
213 #include <yaz/tcpip.h>
216 /* Chas added the following, so we get the definition of completeBER */
219 int tcpip_close(COMSTACK h);
220 int tcpip_put(COMSTACK h, char *buf, int size);
221 int tcpip_get(COMSTACK h, char **buf, int *bufsize);
222 int tcpip_connect(COMSTACK h, void *address);
223 int tcpip_more(COMSTACK h);
224 int tcpip_rcvconnect(COMSTACK h);
225 int tcpip_bind(COMSTACK h, void *address, int mode);
226 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
227 int (*check_ip)(void *cd, const char *a, int len, int type),
229 int static tcpip_set_blocking(COMSTACK p, int blocking);
231 #if HAVE_OPENSSL_SSL_H
232 int ssl_get(COMSTACK h, char **buf, int *bufsize);
233 int ssl_put(COMSTACK h, char *buf, int size);
236 COMSTACK tcpip_accept(COMSTACK h);
237 char *tcpip_addrstr(COMSTACK h);
238 void *tcpip_straddr(COMSTACK h, const char *str);
246 /* this state is used for both SSL and straight TCP/IP */
247 typedef struct tcpip_state
249 char *altbuf; /* alternate buffer for surplus data */
250 int altsize; /* size as xmalloced */
251 int altlen; /* length of data or 0 if none */
253 int written; /* -1 if we aren't writing */
254 int towrite; /* to verify against user input */
255 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
256 struct sockaddr_in addr; /* returned by cs_straddr */
257 char buf[128]; /* returned by cs_addrstr */
258 #if HAVE_OPENSSL_SSL_H
266 static int tcpip_init (void)
268 static int initialized = 0;
274 requested = MAKEWORD(1, 1);
275 if (WSAStartup(requested, &wd))
282 static int proto_number = 0;
284 static int tcpip_init (void)
286 struct protoent *proto;
287 /* only call getprotobyname once, in case it allocates memory */
288 if (!(proto = getprotobyname("tcp")))
290 proto_number = proto->p_proto;
296 * This function is always called through the cs_create() macro.
297 * s >= 0: socket has already been established for us.
299 COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
305 unsigned long tru = 1;
313 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
316 if ((s = socket(AF_INET, SOCK_STREAM, proto_number)) < 0)
323 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
325 if (!(state = (struct tcpip_state *)(p->cprivate =
326 xmalloc(sizeof(tcpip_state)))))
330 if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0)
332 if (!(p->blocking = blocking) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
338 p->type = tcpip_type;
339 p->protocol = (enum oid_proto) protocol;
341 p->f_connect = tcpip_connect;
342 p->f_rcvconnect = tcpip_rcvconnect;
343 p->f_get = tcpip_get;
344 p->f_put = tcpip_put;
345 p->f_close = tcpip_close;
346 p->f_more = tcpip_more;
347 p->f_bind = tcpip_bind;
348 p->f_listen = tcpip_listen;
349 p->f_accept = tcpip_accept;
350 p->f_addrstr = tcpip_addrstr;
351 p->f_straddr = tcpip_straddr;
352 p->f_set_blocking = tcpip_set_blocking;
354 p->state = new_socket ? CS_UNBND : CS_IDLE; /* state of line */
359 #if HAVE_OPENSSL_SSL_H
360 state->ctx = state->ctx_alloc = 0;
365 state->altsize = state->altlen = 0;
366 state->towrite = state->written = -1;
367 if (protocol == PROTO_WAIS)
368 state->complete = completeWAIS;
370 state->complete = completeBER;
372 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
373 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
378 #if HAVE_OPENSSL_SSL_H
380 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
384 yaz_log(LOG_LOG, "ssl_type begin");
386 p = tcpip_type (s, blocking, protocol, 0);
392 state = (tcpip_state *) p->cprivate;
397 SSL_load_error_strings();
398 SSLeay_add_all_algorithms();
400 state->ctx = state->ctx_alloc = SSL_CTX_new (SSLv23_method());
407 /* note: we don't handle already opened socket in SSL mode - yet */
408 yaz_log(LOG_LOG, "ssl_type end");
413 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add)
417 short int port = 210;
422 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
423 add->sin_family = AF_INET;
424 strncpy(buf, str, 511);
426 if ((p = strchr(buf, '/')))
428 if ((p = strchr(buf, ':')))
433 add->sin_port = htons(port);
434 if (!strcmp("@", buf))
435 add->sin_addr.s_addr = INADDR_ANY;
436 else if ((hp = gethostbyname(buf)))
437 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
438 sizeof(struct in_addr));
439 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
440 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
446 void *tcpip_straddr(COMSTACK h, const char *str)
448 tcpip_state *sp = (tcpip_state *)h->cprivate;
450 if (!tcpip_strtoaddr_ex (str, &sp->addr))
455 struct sockaddr_in *tcpip_strtoaddr(const char *str)
457 static struct sockaddr_in add;
459 if (!tcpip_strtoaddr_ex (str, &add))
464 int tcpip_more(COMSTACK h)
466 tcpip_state *sp = (tcpip_state *)h->cprivate;
468 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
473 * connect(2) will block (sometimes) - nothing we can do short of doing
474 * weird things like spawning subprocesses or threading or some weird junk
477 int tcpip_connect(COMSTACK h, void *address)
479 struct sockaddr_in *add = (struct sockaddr_in *)address;
480 #if HAVE_OPENSSL_SSL_H
481 tcpip_state *sp = (tcpip_state *)h->cprivate;
485 TRC(fprintf(stderr, "tcpip_connect\n"));
487 if (h->state == CS_UNBND)
489 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
493 if (WSAGetLastError() == WSAEWOULDBLOCK)
495 h->state = CS_CONNECTING;
496 h->io_pending = CS_WANT_WRITE;
500 if (errno == EINPROGRESS)
502 h->state = CS_CONNECTING;
503 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
510 h->state = CS_CONNECTING;
512 if (h->state != CS_CONNECTING)
514 h->cerrno = CSOUTSTATE;
517 #if HAVE_OPENSSL_SSL_H
524 sp->ssl = SSL_new (sp->ctx);
525 SSL_set_fd (sp->ssl, h->iofile);
527 res = SSL_connect (sp->ssl);
530 int err = SSL_get_error(sp->ssl, res);
531 if (err == SSL_ERROR_WANT_READ)
533 yaz_log (LOG_LOG, "SSL_connect. want_read");
534 h->io_pending = CS_WANT_READ;
537 if (err == SSL_ERROR_WANT_WRITE)
539 yaz_log (LOG_LOG, "SSL_connect. want_write");
540 h->io_pending = CS_WANT_WRITE;
543 h->cerrno = CSERRORSSL;
548 h->state = CS_DATAXFER;
555 int tcpip_rcvconnect(COMSTACK h)
557 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
561 #define CERTF "ztest.pem"
562 #define KEYF "ztest.pem"
564 int tcpip_bind(COMSTACK h, void *address, int mode)
566 struct sockaddr *addr = (struct sockaddr *)address;
570 unsigned long one = 1;
573 #if HAVE_OPENSSL_SSL_H
574 tcpip_state *sp = (tcpip_state *)h->cprivate;
580 res = SSL_CTX_use_certificate_file (sp->ctx, CERTF,
584 ERR_print_errors_fp(stderr);
587 res = SSL_CTX_use_PrivateKey_file (sp->ctx, KEYF,
591 ERR_print_errors_fp(stderr);
594 res = SSL_CTX_check_private_key (sp->ctx);
597 ERR_print_errors_fp(stderr);
601 TRC (fprintf (stderr, "ssl_bind\n"));
605 TRC (fprintf (stderr, "tcpip_bind\n"));
608 TRC (fprintf (stderr, "tcpip_bind\n"));
611 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
612 &one, sizeof(one)) < 0)
618 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)))
623 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
632 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
633 int (*check_ip)(void *cd, const char *a, int len, int t),
636 struct sockaddr_in addr;
638 socklen_t len = sizeof(addr);
640 int len = sizeof(addr);
643 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
644 if (h->state != CS_IDLE)
646 h->cerrno = CSOUTSTATE;
649 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
654 WSAGetLastError() == WSAEWOULDBLOCK
659 h->cerrno = CSNODATA;
664 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
665 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
668 if (check_ip && (*check_ip)(cd, (const char *) &addr,
669 sizeof(addr), AF_INET))
673 closesocket(h->newfd);
684 COMSTACK tcpip_accept(COMSTACK h)
687 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
689 unsigned long tru = 1;
692 TRC(fprintf(stderr, "tcpip_accept\n"));
693 if (h->state == CS_INCON)
695 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
699 closesocket(h->newfd);
706 memcpy(cnew, h, sizeof(*h));
707 cnew->iofile = h->newfd;
708 cnew->io_pending = 0;
709 if (!(state = (tcpip_state *)
710 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
716 closesocket(h->newfd);
724 if (!cnew->blocking &&
726 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
728 (!cnew->blocking && fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
736 closesocket(h->newfd);
748 state->altsize = state->altlen = 0;
749 state->towrite = state->written = -1;
750 state->complete = st->complete;
751 cnew->state = CS_ACCEPT;
754 #if HAVE_OPENSSL_SSL_H
755 state->ctx = st->ctx;
756 state->ctx_alloc = 0;
757 state->ssl = st->ssl;
760 state->ssl = SSL_new (state->ctx);
761 SSL_set_fd (state->ssl, cnew->iofile);
766 if (h->state == CS_ACCEPT)
768 #if HAVE_OPENSSL_SSL_H
769 tcpip_state *state = (tcpip_state *)h->cprivate;
772 int res = SSL_accept (state->ssl);
773 TRC(fprintf(stderr, "SSL_accept\n"));
776 int err = SSL_get_error(state->ssl, res);
777 if (err == SSL_ERROR_WANT_READ)
779 h->io_pending = CS_WANT_READ;
780 yaz_log (LOG_LOG, "SSL_accept. want_read");
783 if (err == SSL_ERROR_WANT_WRITE)
785 h->io_pending = CS_WANT_WRITE;
786 yaz_log (LOG_LOG, "SSL_accept. want_write");
797 h->cerrno = CSOUTSTATE;
801 h->state = CS_DATAXFER;
805 #define CS_TCPIP_BUFCHUNK 4096
808 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
809 * 0=connection closed.
811 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
813 tcpip_state *sp = (tcpip_state *)h->cprivate;
815 int tmpi, berlen, rest, req, tomove;
816 int hasread = 0, res;
818 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
819 if (sp->altlen) /* switch buffers */
821 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
822 (unsigned) sp->altbuf));
826 *bufsize = sp->altsize;
827 hasread = sp->altlen;
833 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
837 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
840 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
841 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
843 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
844 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
848 if (WSAGetLastError() == WSAEWOULDBLOCK)
850 h->io_pending = CS_WANT_READ;
856 if (errno == EWOULDBLOCK
858 || errno == EINPROGRESS
862 h->io_pending = CS_WANT_READ;
875 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
877 /* move surplus buffer (or everything if we didn't get a BER rec.) */
878 if (hasread > berlen)
880 tomove = req = hasread - berlen;
881 rest = tomove % CS_TCPIP_BUFCHUNK;
883 req += CS_TCPIP_BUFCHUNK - rest;
886 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
888 } else if (sp->altsize < req)
889 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
891 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
892 (unsigned) sp->altbuf));
893 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
895 if (berlen < CS_TCPIP_BUFCHUNK - 1)
896 *(*buf + berlen) = '\0';
897 return berlen ? berlen : 1;
901 #if HAVE_OPENSSL_SSL_H
903 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
904 * 0=connection closed.
906 int ssl_get(COMSTACK h, char **buf, int *bufsize)
908 tcpip_state *sp = (tcpip_state *)h->cprivate;
910 int tmpi, berlen, rest, req, tomove;
911 int hasread = 0, res;
913 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
914 if (sp->altlen) /* switch buffers */
916 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
917 (unsigned) sp->altbuf));
921 *bufsize = sp->altsize;
922 hasread = sp->altlen;
928 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
932 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
935 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
936 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
938 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
939 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
942 int ssl_err = SSL_get_error(sp->ssl, res);
943 if (ssl_err == SSL_ERROR_WANT_READ)
945 h->io_pending = CS_WANT_READ;
946 yaz_log (LOG_LOG, "SSL_read. want_read");
949 if (ssl_err == SSL_ERROR_WANT_WRITE)
951 h->io_pending = CS_WANT_WRITE;
952 yaz_log (LOG_LOG, "SSL_read. want_write");
957 h->cerrno = CSERRORSSL;
962 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
964 /* move surplus buffer (or everything if we didn't get a BER rec.) */
965 if (hasread > berlen)
967 tomove = req = hasread - berlen;
968 rest = tomove % CS_TCPIP_BUFCHUNK;
970 req += CS_TCPIP_BUFCHUNK - rest;
973 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
975 } else if (sp->altsize < req)
976 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
978 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
979 (unsigned) sp->altbuf));
980 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
982 if (berlen < CS_TCPIP_BUFCHUNK - 1)
983 *(*buf + berlen) = '\0';
984 return berlen ? berlen : 1;
990 * In nonblocking mode, you must call again with same buffer while
993 int tcpip_put(COMSTACK h, char *buf, int size)
996 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
998 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
1000 if (state->towrite < 0)
1002 state->towrite = size;
1005 else if (state->towrite != size)
1007 h->cerrno = CSWRONGBUF;
1010 while (state->towrite > state->written)
1012 if ((res = send(h->iofile, buf + state->written, size -
1013 state->written, 0)) < 0)
1017 WSAGetLastError() == WSAEWOULDBLOCK
1023 TRC(fprintf(stderr, " Flow control stop\n"));
1024 h->io_pending = CS_WANT_WRITE;
1027 h->cerrno = CSYSERR;
1030 state->written += res;
1031 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1032 res, state->written, size));
1034 state->towrite = state->written = -1;
1035 TRC(fprintf(stderr, " Ok\n"));
1040 #if HAVE_OPENSSL_SSL_H
1042 * Returns 1, 0 or -1
1043 * In nonblocking mode, you must call again with same buffer while
1044 * return value is 1.
1046 int ssl_put(COMSTACK h, char *buf, int size)
1049 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1051 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1053 if (state->towrite < 0)
1055 state->towrite = size;
1058 else if (state->towrite != size)
1060 h->cerrno = CSWRONGBUF;
1063 while (state->towrite > state->written)
1065 res = SSL_write (state->ssl, buf + state->written,
1066 size - state->written);
1069 int ssl_err = SSL_get_error(state->ssl, res);
1070 if (ssl_err == SSL_ERROR_WANT_READ)
1072 h->io_pending = CS_WANT_READ;
1073 yaz_log (LOG_LOG, "SSL_write. want_read");
1076 if (ssl_err == SSL_ERROR_WANT_WRITE)
1078 h->io_pending = CS_WANT_WRITE;
1079 yaz_log (LOG_LOG, "SSL_write. want_write");
1082 h->cerrno = CSERRORSSL;
1085 state->written += res;
1086 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1087 res, state->written, size));
1089 state->towrite = state->written = -1;
1090 TRC(fprintf(stderr, " Ok\n"));
1095 int tcpip_close(COMSTACK h)
1097 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1099 TRC(fprintf(stderr, "tcpip_close\n"));
1100 if (h->iofile != -1)
1102 #if HAVE_OPENSSL_SSL_H
1105 SSL_shutdown (sp->ssl);
1109 closesocket(h->iofile);
1116 #if HAVE_OPENSSL_SSL_H
1119 TRC (fprintf(stderr, "SSL_free\n"));
1124 SSL_CTX_free (sp->ctx_alloc);
1131 char *tcpip_addrstr(COMSTACK h)
1133 struct sockaddr_in addr;
1134 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1135 char *r, *buf = sp->buf;
1137 struct hostent *host;
1140 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1142 h->cerrno = CSYSERR;
1145 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1147 r = (char*) host->h_name;
1149 r = inet_ntoa(addr.sin_addr);
1150 sprintf(buf, "tcp:%s", r);
1151 #if HAVE_OPENSSL_SSL_H
1153 sprintf(buf, "ssl:%s", r);
1158 int static tcpip_set_blocking(COMSTACK p, int blocking)
1162 if (p->blocking == blocking)
1166 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1169 flag = fcntl(p->iofile, F_GETFL, 0);
1171 flag = flag & ~O_NONBLOCK;
1173 flag = flag | O_NONBLOCK;
1174 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1177 p->blocking = blocking;