2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
6 * Revision 1.42 2001-10-22 13:57:24 adam
7 * Implemented cs_rcvconnect and cs_look as described in the documentation.
9 * Revision 1.41 2001/10/12 21:49:26 adam
10 * For accept/recv/send check for EAGAIN if it's differs from EWOULDBLOCK.
12 * Revision 1.40 2001/08/23 09:02:46 adam
13 * WIN32 fixes: Socket not re-used for bind. yaz_log logs WIN32 error
16 * Revision 1.39 2001/07/19 19:49:40 adam
17 * Fixed bug in tcpip_set_blocking.
19 * Revision 1.38 2001/03/21 12:43:36 adam
20 * Implemented cs_create_host. Better error reporting for SSL comstack.
22 * Revision 1.37 2001/03/08 20:18:55 adam
23 * Added cs_set_blocking. Patch from Matthew Carey.
25 * Revision 1.36 2001/02/21 13:46:53 adam
28 * Revision 1.35 2000/11/27 15:17:40 adam
29 * Using SSLeay_add_all_algorithms instead of OpenSSL_add_all_algorithms.
31 * Revision 1.34 2000/11/23 10:58:32 adam
32 * SSL comstack support. Separate POSIX thread support library.
34 * Revision 1.33 2000/09/04 08:27:11 adam
35 * Work on error handling for tcpip_accept.
37 * Revision 1.32 1999/11/30 13:47:11 adam
38 * Improved installation. Moved header files to include/yaz.
40 * Revision 1.31 1999/04/29 07:31:23 adam
41 * Changed tcpip_strtoaddr_ex so that only part 'till '/' is considered
44 * Revision 1.30 1999/04/20 09:56:48 adam
45 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
46 * Modified all encoders/decoders to reflect this change.
48 * Revision 1.29 1999/04/16 14:45:55 adam
49 * Added interface for tcpd wrapper for access control.
51 * Revision 1.28 1999/03/31 11:11:14 adam
52 * Function getprotobyname only called once. Minor change in tcpip_get
53 * to handle multi-threaded conditions.
55 * Revision 1.27 1999/02/02 13:57:31 adam
56 * Uses preprocessor define WIN32 instead of WINDOWS to build code
57 * for Microsoft WIN32.
59 * Revision 1.26 1999/01/08 11:23:14 adam
60 * Added const modifier to some of the BER/ODR encoding routines.
62 * Revision 1.25 1998/07/07 15:49:23 adam
63 * Added braces to avoid warning.
65 * Revision 1.24 1998/06/29 07:59:17 adam
68 * Revision 1.23 1998/06/23 15:37:50 adam
69 * Added type cast to prevent warning.
71 * Revision 1.22 1998/06/22 11:32:36 adam
72 * Added 'conditional cs_listen' feature.
74 * Revision 1.21 1998/05/20 09:55:32 adam
75 * Function tcpip_get treats EINPROGRESS error in the same way as
76 * EWOULDBLOCK. EINPROGRESS shouldn't be returned - but it is on
77 * Solaris in some cases.
79 * Revision 1.20 1998/05/18 10:10:40 adam
80 * Minor change to avoid C++ warning.
82 * Revision 1.19 1998/02/11 11:53:33 adam
83 * Changed code so that it compiles as C++.
85 * Revision 1.18 1997/09/29 07:15:25 adam
86 * Changed use of setsockopt to avoid warnings on MSVC.
88 * Revision 1.17 1997/09/17 12:10:30 adam
91 * Revision 1.16 1997/09/01 08:49:14 adam
92 * New windows NT/95 port using MSV5.0. Minor changes only.
94 * Revision 1.15 1997/05/14 06:53:33 adam
97 * Revision 1.14 1997/05/01 15:06:32 adam
98 * Moved WINSOCK init. code to tcpip_init routine.
100 * Revision 1.13 1996/11/01 08:45:18 adam
101 * Bug fix: used close on MS-Windows. Fixed to closesocket.
103 * Revision 1.12 1996/07/06 19:58:30 quinn
104 * System headerfiles gathered in yconfig
106 * Revision 1.11 1996/02/23 10:00:39 quinn
109 * Revision 1.10 1996/02/20 12:52:11 quinn
110 * WAIS protocol support.
112 * Revision 1.9 1996/02/10 12:23:11 quinn
113 * Enablie inetd operations fro TCP/IP stack
115 * Revision 1.8 1995/11/01 13:54:27 quinn
118 * Revision 1.7 1995/10/30 12:41:16 quinn
119 * Added hostname lookup for server.
121 * Revision 1.6 1995/09/29 17:12:00 quinn
124 * Revision 1.5 1995/09/29 17:01:48 quinn
127 * Revision 1.4 1995/09/28 10:12:26 quinn
128 * Windows-support changes
130 * Revision 1.3 1995/09/27 15:02:45 quinn
131 * Modified function heads & prototypes.
133 * Revision 1.2 1995/06/15 12:30:06 quinn
134 * Added @ as hostname alias for INADDR ANY.
136 * Revision 1.1 1995/06/14 09:58:20 quinn
137 * Renamed yazlib to comstack.
139 * Revision 1.20 1995/05/16 08:51:16 quinn
140 * License, documentation, and memory fixes
142 * Revision 1.19 1995/04/10 10:24:08 quinn
145 * Revision 1.18 1995/03/30 13:29:27 quinn
146 * Added REUSEADDR in tcpip_bind
148 * Revision 1.17 1995/03/27 08:36:10 quinn
149 * Some work on nonblocking operation in xmosi.c and rfct.c.
150 * Added protocol parameter to cs_create()
152 * Revision 1.16 1995/03/21 15:53:41 quinn
155 * Revision 1.15 1995/03/21 12:31:27 quinn
156 * Added check for EINPROGRESS on connect.
158 * Revision 1.14 1995/03/20 09:47:21 quinn
159 * Added server-side support to xmosi.c
160 * Fixed possible problems in rfct
163 * Revision 1.13 1995/03/15 16:15:13 adam
166 * Revision 1.12 1995/03/15 15:36:27 quinn
167 * Mods to support nonblocking I/O
169 * Revision 1.11 1995/03/15 08:37:57 quinn
170 * Now we're pretty much set for nonblocking I/O.
172 * Revision 1.10 1995/03/14 17:00:07 quinn
173 * Bug-fixes - added tracing info to tcpip.c
175 * Revision 1.9 1995/03/14 10:28:42 quinn
176 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
178 * Revision 1.8 1995/03/10 14:22:50 quinn
179 * Removed debug output.
181 * Revision 1.7 1995/03/10 11:44:59 quinn
182 * Fixes and debugging
184 * Revision 1.6 1995/03/07 10:26:55 quinn
185 * Initialized type field in the comstacks.
187 * Revision 1.5 1995/02/14 20:40:07 quinn
190 * Revision 1.4 1995/02/14 11:54:49 quinn
191 * Beginning to add full CCL.
193 * Revision 1.3 1995/02/10 18:58:10 quinn
194 * Fixed tcpip_get (formerly tcpip_read).
195 * Turned tst (cli) into a proper, event-driven thingy.
197 * Revision 1.2 1995/02/10 15:55:47 quinn
200 * Revision 1.1 1995/02/09 15:51:52 quinn
213 #if HAVE_OPENSSL_SSL_H
214 #include <openssl/ssl.h>
215 #include <openssl/err.h>
218 #include <yaz/comstack.h>
219 #include <yaz/tcpip.h>
222 /* Chas added the following, so we get the definition of completeBER */
225 int tcpip_close(COMSTACK h);
226 int tcpip_put(COMSTACK h, char *buf, int size);
227 int tcpip_get(COMSTACK h, char **buf, int *bufsize);
228 int tcpip_connect(COMSTACK h, void *address);
229 int tcpip_more(COMSTACK h);
230 int tcpip_rcvconnect(COMSTACK h);
231 int tcpip_bind(COMSTACK h, void *address, int mode);
232 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
233 int (*check_ip)(void *cd, const char *a, int len, int type),
235 int static tcpip_set_blocking(COMSTACK p, int blocking);
237 #if HAVE_OPENSSL_SSL_H
238 int ssl_get(COMSTACK h, char **buf, int *bufsize);
239 int ssl_put(COMSTACK h, char *buf, int size);
242 COMSTACK tcpip_accept(COMSTACK h);
243 char *tcpip_addrstr(COMSTACK h);
244 void *tcpip_straddr(COMSTACK h, const char *str);
252 /* this state is used for both SSL and straight TCP/IP */
253 typedef struct tcpip_state
255 char *altbuf; /* alternate buffer for surplus data */
256 int altsize; /* size as xmalloced */
257 int altlen; /* length of data or 0 if none */
259 int written; /* -1 if we aren't writing */
260 int towrite; /* to verify against user input */
261 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
262 struct sockaddr_in addr; /* returned by cs_straddr */
263 char buf[128]; /* returned by cs_addrstr */
264 #if HAVE_OPENSSL_SSL_H
272 static int tcpip_init (void)
274 static int initialized = 0;
280 requested = MAKEWORD(1, 1);
281 if (WSAStartup(requested, &wd))
288 static int proto_number = 0;
290 static int tcpip_init (void)
292 struct protoent *proto;
293 /* only call getprotobyname once, in case it allocates memory */
294 if (!(proto = getprotobyname("tcp")))
296 proto_number = proto->p_proto;
302 * This function is always called through the cs_create() macro.
303 * s >= 0: socket has already been established for us.
305 COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
311 unsigned long tru = 1;
319 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
322 if ((s = socket(AF_INET, SOCK_STREAM, proto_number)) < 0)
329 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
331 if (!(state = (struct tcpip_state *)(p->cprivate =
332 xmalloc(sizeof(tcpip_state)))))
336 if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0)
338 if (!(p->blocking = blocking) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
344 p->type = tcpip_type;
345 p->protocol = (enum oid_proto) protocol;
347 p->f_connect = tcpip_connect;
348 p->f_rcvconnect = tcpip_rcvconnect;
349 p->f_get = tcpip_get;
350 p->f_put = tcpip_put;
351 p->f_close = tcpip_close;
352 p->f_more = tcpip_more;
353 p->f_bind = tcpip_bind;
354 p->f_listen = tcpip_listen;
355 p->f_accept = tcpip_accept;
356 p->f_addrstr = tcpip_addrstr;
357 p->f_straddr = tcpip_straddr;
358 p->f_set_blocking = tcpip_set_blocking;
360 p->state = new_socket ? CS_UNBND : CS_IDLE; /* state of line */
365 #if HAVE_OPENSSL_SSL_H
366 state->ctx = state->ctx_alloc = 0;
371 state->altsize = state->altlen = 0;
372 state->towrite = state->written = -1;
373 if (protocol == PROTO_WAIS)
374 state->complete = completeWAIS;
376 state->complete = completeBER;
378 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
379 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
384 #if HAVE_OPENSSL_SSL_H
386 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
390 yaz_log(LOG_LOG, "ssl_type begin");
392 p = tcpip_type (s, blocking, protocol, 0);
398 state = (tcpip_state *) p->cprivate;
403 SSL_load_error_strings();
404 SSLeay_add_all_algorithms();
406 state->ctx = state->ctx_alloc = SSL_CTX_new (SSLv23_method());
413 /* note: we don't handle already opened socket in SSL mode - yet */
414 yaz_log(LOG_LOG, "ssl_type end");
419 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add)
423 short int port = 210;
428 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
429 add->sin_family = AF_INET;
430 strncpy(buf, str, 511);
432 if ((p = strchr(buf, '/')))
434 if ((p = strchr(buf, ':')))
439 add->sin_port = htons(port);
440 if (!strcmp("@", buf))
441 add->sin_addr.s_addr = INADDR_ANY;
442 else if ((hp = gethostbyname(buf)))
443 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
444 sizeof(struct in_addr));
445 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
446 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
452 void *tcpip_straddr(COMSTACK h, const char *str)
454 tcpip_state *sp = (tcpip_state *)h->cprivate;
456 if (!tcpip_strtoaddr_ex (str, &sp->addr))
461 struct sockaddr_in *tcpip_strtoaddr(const char *str)
463 static struct sockaddr_in add;
465 if (!tcpip_strtoaddr_ex (str, &add))
470 int tcpip_more(COMSTACK h)
472 tcpip_state *sp = (tcpip_state *)h->cprivate;
474 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
479 * connect(2) will block (sometimes) - nothing we can do short of doing
480 * weird things like spawning subprocesses or threading or some weird junk
483 int tcpip_connect(COMSTACK h, void *address)
485 struct sockaddr_in *add = (struct sockaddr_in *)address;
486 #if HAVE_OPENSSL_SSL_H
487 tcpip_state *sp = (tcpip_state *)h->cprivate;
491 TRC(fprintf(stderr, "tcpip_connect\n"));
493 if (h->state == CS_UNBND)
495 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
499 if (WSAGetLastError() == WSAEWOULDBLOCK)
501 h->event = CS_CONNECT;
502 h->state = CS_CONNECTING;
503 h->io_pending = CS_WANT_WRITE;
507 if (errno == EINPROGRESS)
509 h->event = CS_CONNECT;
510 h->state = CS_CONNECTING;
511 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
518 h->event = CS_CONNECT;
519 h->state = CS_CONNECTING;
521 if (h->state != CS_CONNECTING)
523 h->cerrno = CSOUTSTATE;
526 #if HAVE_OPENSSL_SSL_H
533 sp->ssl = SSL_new (sp->ctx);
534 SSL_set_fd (sp->ssl, h->iofile);
536 res = SSL_connect (sp->ssl);
539 int err = SSL_get_error(sp->ssl, res);
540 if (err == SSL_ERROR_WANT_READ)
542 yaz_log (LOG_LOG, "SSL_connect. want_read");
543 h->io_pending = CS_WANT_READ;
546 if (err == SSL_ERROR_WANT_WRITE)
548 yaz_log (LOG_LOG, "SSL_connect. want_write");
549 h->io_pending = CS_WANT_WRITE;
552 h->cerrno = CSERRORSSL;
558 h->state = CS_DATAXFER;
565 int tcpip_rcvconnect(COMSTACK cs)
567 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
569 if (cs->event == CS_CONNECT)
572 fd_set input, output;
582 FD_SET (fd, &output);
584 r = select (fd+1, &input, &output, 0, &tv);
587 if (FD_ISSET(cs->iofile, &output))
590 return 0; /* write OK, we're OK */
593 return -1; /* an error, for sure */
595 return 0; /* timeout - incomplete */
597 return -1; /* wrong state */
600 #define CERTF "ztest.pem"
601 #define KEYF "ztest.pem"
603 int tcpip_bind(COMSTACK h, void *address, int mode)
605 struct sockaddr *addr = (struct sockaddr *)address;
609 unsigned long one = 1;
612 #if HAVE_OPENSSL_SSL_H
613 tcpip_state *sp = (tcpip_state *)h->cprivate;
619 res = SSL_CTX_use_certificate_file (sp->ctx, CERTF,
623 ERR_print_errors_fp(stderr);
626 res = SSL_CTX_use_PrivateKey_file (sp->ctx, KEYF,
630 ERR_print_errors_fp(stderr);
633 res = SSL_CTX_check_private_key (sp->ctx);
636 ERR_print_errors_fp(stderr);
640 TRC (fprintf (stderr, "ssl_bind\n"));
644 TRC (fprintf (stderr, "tcpip_bind\n"));
647 TRC (fprintf (stderr, "tcpip_bind\n"));
650 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
651 &one, sizeof(one)) < 0)
657 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)))
662 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
668 h->event = CS_LISTEN;
672 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
673 int (*check_ip)(void *cd, const char *a, int len, int t),
676 struct sockaddr_in addr;
678 socklen_t len = sizeof(addr);
680 int len = sizeof(addr);
683 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
684 if (h->state != CS_IDLE)
686 h->cerrno = CSOUTSTATE;
689 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
694 WSAGetLastError() == WSAEWOULDBLOCK
698 #if EAGAIN != EWOULDBLOCK
704 h->cerrno = CSNODATA;
709 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
710 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
713 if (check_ip && (*check_ip)(cd, (const char *) &addr,
714 sizeof(addr), AF_INET))
718 closesocket(h->newfd);
729 COMSTACK tcpip_accept(COMSTACK h)
732 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
734 unsigned long tru = 1;
737 TRC(fprintf(stderr, "tcpip_accept\n"));
738 if (h->state == CS_INCON)
740 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
744 closesocket(h->newfd);
751 memcpy(cnew, h, sizeof(*h));
752 cnew->iofile = h->newfd;
753 cnew->io_pending = 0;
754 if (!(state = (tcpip_state *)
755 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
761 closesocket(h->newfd);
769 if (!cnew->blocking &&
771 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
773 (!cnew->blocking && fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
781 closesocket(h->newfd);
793 state->altsize = state->altlen = 0;
794 state->towrite = state->written = -1;
795 state->complete = st->complete;
796 cnew->state = CS_ACCEPT;
799 #if HAVE_OPENSSL_SSL_H
800 state->ctx = st->ctx;
801 state->ctx_alloc = 0;
802 state->ssl = st->ssl;
805 state->ssl = SSL_new (state->ctx);
806 SSL_set_fd (state->ssl, cnew->iofile);
811 if (h->state == CS_ACCEPT)
813 #if HAVE_OPENSSL_SSL_H
814 tcpip_state *state = (tcpip_state *)h->cprivate;
817 int res = SSL_accept (state->ssl);
818 TRC(fprintf(stderr, "SSL_accept\n"));
821 int err = SSL_get_error(state->ssl, res);
822 if (err == SSL_ERROR_WANT_READ)
824 h->io_pending = CS_WANT_READ;
825 yaz_log (LOG_LOG, "SSL_accept. want_read");
828 if (err == SSL_ERROR_WANT_WRITE)
830 h->io_pending = CS_WANT_WRITE;
831 yaz_log (LOG_LOG, "SSL_accept. want_write");
842 h->cerrno = CSOUTSTATE;
846 h->state = CS_DATAXFER;
851 #define CS_TCPIP_BUFCHUNK 4096
854 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
855 * 0=connection closed.
857 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
859 tcpip_state *sp = (tcpip_state *)h->cprivate;
861 int tmpi, berlen, rest, req, tomove;
862 int hasread = 0, res;
864 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
865 if (sp->altlen) /* switch buffers */
867 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
868 (unsigned) sp->altbuf));
872 *bufsize = sp->altsize;
873 hasread = sp->altlen;
879 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
883 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
886 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
887 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
889 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
890 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
894 if (WSAGetLastError() == WSAEWOULDBLOCK)
896 h->io_pending = CS_WANT_READ;
902 if (errno == EWOULDBLOCK
904 #if EAGAIN != EWOULDBLOCK
908 || errno == EINPROGRESS
911 h->io_pending = CS_WANT_READ;
924 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
926 /* move surplus buffer (or everything if we didn't get a BER rec.) */
927 if (hasread > berlen)
929 tomove = req = hasread - berlen;
930 rest = tomove % CS_TCPIP_BUFCHUNK;
932 req += CS_TCPIP_BUFCHUNK - rest;
935 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
937 } else if (sp->altsize < req)
938 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
940 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
941 (unsigned) sp->altbuf));
942 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
944 if (berlen < CS_TCPIP_BUFCHUNK - 1)
945 *(*buf + berlen) = '\0';
946 return berlen ? berlen : 1;
950 #if HAVE_OPENSSL_SSL_H
952 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
953 * 0=connection closed.
955 int ssl_get(COMSTACK h, char **buf, int *bufsize)
957 tcpip_state *sp = (tcpip_state *)h->cprivate;
959 int tmpi, berlen, rest, req, tomove;
960 int hasread = 0, res;
962 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
963 if (sp->altlen) /* switch buffers */
965 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
966 (unsigned) sp->altbuf));
970 *bufsize = sp->altsize;
971 hasread = sp->altlen;
977 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
981 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
984 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
985 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
987 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
988 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
991 int ssl_err = SSL_get_error(sp->ssl, res);
992 if (ssl_err == SSL_ERROR_WANT_READ)
994 h->io_pending = CS_WANT_READ;
995 yaz_log (LOG_LOG, "SSL_read. want_read");
998 if (ssl_err == SSL_ERROR_WANT_WRITE)
1000 h->io_pending = CS_WANT_WRITE;
1001 yaz_log (LOG_LOG, "SSL_read. want_write");
1006 h->cerrno = CSERRORSSL;
1011 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1013 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1014 if (hasread > berlen)
1016 tomove = req = hasread - berlen;
1017 rest = tomove % CS_TCPIP_BUFCHUNK;
1019 req += CS_TCPIP_BUFCHUNK - rest;
1022 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1024 } else if (sp->altsize < req)
1025 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1027 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1028 (unsigned) sp->altbuf));
1029 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1031 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1032 *(*buf + berlen) = '\0';
1033 return berlen ? berlen : 1;
1038 * Returns 1, 0 or -1
1039 * In nonblocking mode, you must call again with same buffer while
1040 * return value is 1.
1042 int tcpip_put(COMSTACK h, char *buf, int size)
1045 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1047 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
1050 if (state->towrite < 0)
1052 state->towrite = size;
1055 else if (state->towrite != size)
1057 h->cerrno = CSWRONGBUF;
1060 while (state->towrite > state->written)
1063 send(h->iofile, buf + state->written, size -
1074 WSAGetLastError() == WSAEWOULDBLOCK
1076 errno == EWOULDBLOCK
1078 #if EAGAIN != EWOULDBLOCK
1085 TRC(fprintf(stderr, " Flow control stop\n"));
1086 h->io_pending = CS_WANT_WRITE;
1089 h->cerrno = CSYSERR;
1092 state->written += res;
1093 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1094 res, state->written, size));
1096 state->towrite = state->written = -1;
1097 TRC(fprintf(stderr, " Ok\n"));
1102 #if HAVE_OPENSSL_SSL_H
1104 * Returns 1, 0 or -1
1105 * In nonblocking mode, you must call again with same buffer while
1106 * return value is 1.
1108 int ssl_put(COMSTACK h, char *buf, int size)
1111 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1113 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1116 if (state->towrite < 0)
1118 state->towrite = size;
1121 else if (state->towrite != size)
1123 h->cerrno = CSWRONGBUF;
1126 while (state->towrite > state->written)
1128 res = SSL_write (state->ssl, buf + state->written,
1129 size - state->written);
1132 int ssl_err = SSL_get_error(state->ssl, res);
1133 if (ssl_err == SSL_ERROR_WANT_READ)
1135 h->io_pending = CS_WANT_READ;
1136 yaz_log (LOG_LOG, "SSL_write. want_read");
1139 if (ssl_err == SSL_ERROR_WANT_WRITE)
1141 h->io_pending = CS_WANT_WRITE;
1142 yaz_log (LOG_LOG, "SSL_write. want_write");
1145 h->cerrno = CSERRORSSL;
1148 state->written += res;
1149 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1150 res, state->written, size));
1152 state->towrite = state->written = -1;
1153 TRC(fprintf(stderr, " Ok\n"));
1158 int tcpip_close(COMSTACK h)
1160 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1162 TRC(fprintf(stderr, "tcpip_close\n"));
1163 if (h->iofile != -1)
1165 #if HAVE_OPENSSL_SSL_H
1168 SSL_shutdown (sp->ssl);
1172 closesocket(h->iofile);
1179 #if HAVE_OPENSSL_SSL_H
1182 TRC (fprintf(stderr, "SSL_free\n"));
1187 SSL_CTX_free (sp->ctx_alloc);
1194 char *tcpip_addrstr(COMSTACK h)
1196 struct sockaddr_in addr;
1197 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1198 char *r, *buf = sp->buf;
1200 struct hostent *host;
1203 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1205 h->cerrno = CSYSERR;
1208 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1210 r = (char*) host->h_name;
1212 r = inet_ntoa(addr.sin_addr);
1213 sprintf(buf, "tcp:%s", r);
1214 #if HAVE_OPENSSL_SSL_H
1216 sprintf(buf, "ssl:%s", r);
1221 int static tcpip_set_blocking(COMSTACK p, int blocking)
1225 if (p->blocking == blocking)
1229 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1232 flag = fcntl(p->iofile, F_GETFL, 0);
1234 flag = flag & ~O_NONBLOCK;
1236 flag = flag | O_NONBLOCK;
1237 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1240 p->blocking = blocking;