2 * Copyright (c) 1995-2000, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.35 2000-11-27 15:17:40 adam
8 * Using SSLeay_add_all_algorithms instead of OpenSSL_add_all_algorithms.
10 * Revision 1.34 2000/11/23 10:58:32 adam
11 * SSL comstack support. Separate POSIX thread support library.
13 * Revision 1.33 2000/09/04 08:27:11 adam
14 * Work on error handling for tcpip_accept.
16 * Revision 1.32 1999/11/30 13:47:11 adam
17 * Improved installation. Moved header files to include/yaz.
19 * Revision 1.31 1999/04/29 07:31:23 adam
20 * Changed tcpip_strtoaddr_ex so that only part 'till '/' is considered
23 * Revision 1.30 1999/04/20 09:56:48 adam
24 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
25 * Modified all encoders/decoders to reflect this change.
27 * Revision 1.29 1999/04/16 14:45:55 adam
28 * Added interface for tcpd wrapper for access control.
30 * Revision 1.28 1999/03/31 11:11:14 adam
31 * Function getprotobyname only called once. Minor change in tcpip_get
32 * to handle multi-threaded conditions.
34 * Revision 1.27 1999/02/02 13:57:31 adam
35 * Uses preprocessor define WIN32 instead of WINDOWS to build code
36 * for Microsoft WIN32.
38 * Revision 1.26 1999/01/08 11:23:14 adam
39 * Added const modifier to some of the BER/ODR encoding routines.
41 * Revision 1.25 1998/07/07 15:49:23 adam
42 * Added braces to avoid warning.
44 * Revision 1.24 1998/06/29 07:59:17 adam
47 * Revision 1.23 1998/06/23 15:37:50 adam
48 * Added type cast to prevent warning.
50 * Revision 1.22 1998/06/22 11:32:36 adam
51 * Added 'conditional cs_listen' feature.
53 * Revision 1.21 1998/05/20 09:55:32 adam
54 * Function tcpip_get treats EINPROGRESS error in the same way as
55 * EWOULDBLOCK. EINPROGRESS shouldn't be returned - but it is on
56 * Solaris in some cases.
58 * Revision 1.20 1998/05/18 10:10:40 adam
59 * Minor change to avoid C++ warning.
61 * Revision 1.19 1998/02/11 11:53:33 adam
62 * Changed code so that it compiles as C++.
64 * Revision 1.18 1997/09/29 07:15:25 adam
65 * Changed use of setsockopt to avoid warnings on MSVC.
67 * Revision 1.17 1997/09/17 12:10:30 adam
70 * Revision 1.16 1997/09/01 08:49:14 adam
71 * New windows NT/95 port using MSV5.0. Minor changes only.
73 * Revision 1.15 1997/05/14 06:53:33 adam
76 * Revision 1.14 1997/05/01 15:06:32 adam
77 * Moved WINSOCK init. code to tcpip_init routine.
79 * Revision 1.13 1996/11/01 08:45:18 adam
80 * Bug fix: used close on MS-Windows. Fixed to closesocket.
82 * Revision 1.12 1996/07/06 19:58:30 quinn
83 * System headerfiles gathered in yconfig
85 * Revision 1.11 1996/02/23 10:00:39 quinn
88 * Revision 1.10 1996/02/20 12:52:11 quinn
89 * WAIS protocol support.
91 * Revision 1.9 1996/02/10 12:23:11 quinn
92 * Enablie inetd operations fro TCP/IP stack
94 * Revision 1.8 1995/11/01 13:54:27 quinn
97 * Revision 1.7 1995/10/30 12:41:16 quinn
98 * Added hostname lookup for server.
100 * Revision 1.6 1995/09/29 17:12:00 quinn
103 * Revision 1.5 1995/09/29 17:01:48 quinn
106 * Revision 1.4 1995/09/28 10:12:26 quinn
107 * Windows-support changes
109 * Revision 1.3 1995/09/27 15:02:45 quinn
110 * Modified function heads & prototypes.
112 * Revision 1.2 1995/06/15 12:30:06 quinn
113 * Added @ as hostname alias for INADDR ANY.
115 * Revision 1.1 1995/06/14 09:58:20 quinn
116 * Renamed yazlib to comstack.
118 * Revision 1.20 1995/05/16 08:51:16 quinn
119 * License, documentation, and memory fixes
121 * Revision 1.19 1995/04/10 10:24:08 quinn
124 * Revision 1.18 1995/03/30 13:29:27 quinn
125 * Added REUSEADDR in tcpip_bind
127 * Revision 1.17 1995/03/27 08:36:10 quinn
128 * Some work on nonblocking operation in xmosi.c and rfct.c.
129 * Added protocol parameter to cs_create()
131 * Revision 1.16 1995/03/21 15:53:41 quinn
134 * Revision 1.15 1995/03/21 12:31:27 quinn
135 * Added check for EINPROGRESS on connect.
137 * Revision 1.14 1995/03/20 09:47:21 quinn
138 * Added server-side support to xmosi.c
139 * Fixed possible problems in rfct
142 * Revision 1.13 1995/03/15 16:15:13 adam
145 * Revision 1.12 1995/03/15 15:36:27 quinn
146 * Mods to support nonblocking I/O
148 * Revision 1.11 1995/03/15 08:37:57 quinn
149 * Now we're pretty much set for nonblocking I/O.
151 * Revision 1.10 1995/03/14 17:00:07 quinn
152 * Bug-fixes - added tracing info to tcpip.c
154 * Revision 1.9 1995/03/14 10:28:42 quinn
155 * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
157 * Revision 1.8 1995/03/10 14:22:50 quinn
158 * Removed debug output.
160 * Revision 1.7 1995/03/10 11:44:59 quinn
161 * Fixes and debugging
163 * Revision 1.6 1995/03/07 10:26:55 quinn
164 * Initialized type field in the comstacks.
166 * Revision 1.5 1995/02/14 20:40:07 quinn
169 * Revision 1.4 1995/02/14 11:54:49 quinn
170 * Beginning to add full CCL.
172 * Revision 1.3 1995/02/10 18:58:10 quinn
173 * Fixed tcpip_get (formerly tcpip_read).
174 * Turned tst (cli) into a proper, event-driven thingy.
176 * Revision 1.2 1995/02/10 15:55:47 quinn
179 * Revision 1.1 1995/02/09 15:51:52 quinn
192 #if HAVE_OPENSSL_SSL_H
193 #include <openssl/ssl.h>
194 #include <openssl/err.h>
197 #include <yaz/comstack.h>
198 #include <yaz/tcpip.h>
201 /* Chas added the following, so we get the definition of completeBER */
204 int tcpip_close(COMSTACK h);
205 int tcpip_put(COMSTACK h, char *buf, int size);
206 int tcpip_get(COMSTACK h, char **buf, int *bufsize);
207 int tcpip_connect(COMSTACK h, void *address);
208 int tcpip_more(COMSTACK h);
209 int tcpip_rcvconnect(COMSTACK h);
210 int tcpip_bind(COMSTACK h, void *address, int mode);
211 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
212 int (*check_ip)(void *cd, const char *a, int len, int type),
215 #if HAVE_OPENSSL_SSL_H
216 int ssl_get(COMSTACK h, char **buf, int *bufsize);
217 int ssl_put(COMSTACK h, char *buf, int size);
220 COMSTACK tcpip_accept(COMSTACK h);
221 char *tcpip_addrstr(COMSTACK h);
222 void *tcpip_straddr(COMSTACK h, const char *str);
230 /* this state is used for both SSL and straight TCP/IP */
231 typedef struct tcpip_state
233 char *altbuf; /* alternate buffer for surplus data */
234 int altsize; /* size as xmalloced */
235 int altlen; /* length of data or 0 if none */
237 int written; /* -1 if we aren't writing */
238 int towrite; /* to verify against user input */
239 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
240 struct sockaddr_in addr; /* returned by cs_straddr */
241 char buf[128]; /* returned by cs_addrstr */
242 #if HAVE_OPENSSL_SSL_H
250 static int tcpip_init (void)
252 static int initialized = 0;
258 requested = MAKEWORD(1, 1);
259 if (WSAStartup(requested, &wd))
266 static int proto_number = 0;
268 static int tcpip_init (void)
270 struct protoent *proto;
271 /* only call getprotobyname once, in case it allocates memory */
272 if (!(proto = getprotobyname("tcp")))
274 proto_number = proto->p_proto;
280 * This function is always called through the cs_create() macro.
281 * s >= 0: socket has already been established for us.
283 COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
289 unsigned long tru = 1;
297 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
300 if ((s = socket(AF_INET, SOCK_STREAM, proto_number)) < 0)
307 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
309 if (!(state = (struct tcpip_state *)(p->cprivate =
310 xmalloc(sizeof(tcpip_state)))))
314 if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0)
316 if (!(p->blocking = blocking) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
322 p->type = tcpip_type;
323 p->protocol = (enum oid_proto) protocol;
325 p->f_connect = tcpip_connect;
326 p->f_rcvconnect = tcpip_rcvconnect;
327 p->f_get = tcpip_get;
328 p->f_put = tcpip_put;
329 p->f_close = tcpip_close;
330 p->f_more = tcpip_more;
331 p->f_bind = tcpip_bind;
332 p->f_listen = tcpip_listen;
333 p->f_accept = tcpip_accept;
334 p->f_addrstr = tcpip_addrstr;
335 p->f_straddr = tcpip_straddr;
337 p->state = new_socket ? CS_UNBND : CS_IDLE; /* state of line */
342 #if HAVE_OPENSSL_SSL_H
343 state->ctx = state->ctx_alloc = 0;
348 state->altsize = state->altlen = 0;
349 state->towrite = state->written = -1;
350 if (protocol == PROTO_WAIS)
351 state->complete = completeWAIS;
353 state->complete = completeBER;
355 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
356 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
361 #if HAVE_OPENSSL_SSL_H
363 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
367 yaz_log(LOG_LOG, "ssl_type begin");
369 p = tcpip_type (s, blocking, protocol, 0);
375 state = (tcpip_state *) p->cprivate;
380 SSL_load_error_strings();
381 SSLeay_add_all_algorithms();
383 state->ctx = state->ctx_alloc = SSL_CTX_new (SSLv23_method());
390 /* note: we don't handle already opened socket in SSL mode - yet */
391 yaz_log(LOG_LOG, "ssl_type end");
396 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add)
400 short int port = 210;
405 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
406 add->sin_family = AF_INET;
407 strncpy(buf, str, 511);
409 if ((p = strchr(buf, '/')))
411 if ((p = strchr(buf, ':')))
416 add->sin_port = htons(port);
417 if (!strcmp("@", buf))
418 add->sin_addr.s_addr = INADDR_ANY;
419 else if ((hp = gethostbyname(buf)))
420 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
421 sizeof(struct in_addr));
422 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
423 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
429 void *tcpip_straddr(COMSTACK h, const char *str)
431 tcpip_state *sp = (tcpip_state *)h->cprivate;
433 if (!tcpip_strtoaddr_ex (str, &sp->addr))
438 struct sockaddr_in *tcpip_strtoaddr(const char *str)
440 static struct sockaddr_in add;
442 if (!tcpip_strtoaddr_ex (str, &add))
447 int tcpip_more(COMSTACK h)
449 tcpip_state *sp = (tcpip_state *)h->cprivate;
451 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
456 * connect(2) will block (sometimes) - nothing we can do short of doing
457 * weird things like spawning subprocesses or threading or some weird junk
460 int tcpip_connect(COMSTACK h, void *address)
462 struct sockaddr_in *add = (struct sockaddr_in *)address;
463 #if HAVE_OPENSSL_SSL_H
464 tcpip_state *sp = (tcpip_state *)h->cprivate;
468 TRC(fprintf(stderr, "tcpip_connect\n"));
470 if (h->state == CS_UNBND)
472 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
476 if (WSAGetLastError() == WSAEWOULDBLOCK)
478 h->state = CS_CONNECTING;
479 h->io_pending = CS_WANT_WRITE;
483 if (errno == EINPROGRESS)
485 h->state = CS_CONNECTING;
486 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
492 h->state = CS_CONNECTING;
494 if (h->state != CS_CONNECTING)
496 h->cerrno = CSOUTSTATE;
499 #if HAVE_OPENSSL_SSL_H
506 sp->ssl = SSL_new (sp->ctx);
507 SSL_set_fd (sp->ssl, h->iofile);
509 res = SSL_connect (sp->ssl);
512 int err = SSL_get_error(sp->ssl, res);
513 if (err == SSL_ERROR_WANT_READ)
515 yaz_log (LOG_LOG, "SSL_connect. want_read");
516 h->io_pending = CS_WANT_READ;
519 if (err == SSL_ERROR_WANT_WRITE)
521 yaz_log (LOG_LOG, "SSL_connect. want_write");
522 h->io_pending = CS_WANT_WRITE;
529 h->state = CS_DATAXFER;
536 int tcpip_rcvconnect(COMSTACK h)
538 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
542 #define CERTF "ztest.pem"
543 #define KEYF "ztest.pem"
545 int tcpip_bind(COMSTACK h, void *address, int mode)
547 struct sockaddr *addr = (struct sockaddr *)address;
551 unsigned long one = 1;
554 #if HAVE_OPENSSL_SSL_H
555 tcpip_state *sp = (tcpip_state *)h->cprivate;
561 res = SSL_CTX_use_certificate_file (sp->ctx, CERTF,
565 ERR_print_errors_fp(stderr);
568 res = SSL_CTX_use_PrivateKey_file (sp->ctx, KEYF,
572 ERR_print_errors_fp(stderr);
575 res = SSL_CTX_check_private_key (sp->ctx);
578 ERR_print_errors_fp(stderr);
582 TRC (fprintf (stderr, "ssl_bind\n"));
586 TRC (fprintf (stderr, "tcpip_bind\n"));
589 TRC (fprintf (stderr, "tcpip_bind\n"));
591 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
592 &one, sizeof(one)) < 0)
597 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)) < 0)
602 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
611 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
612 int (*check_ip)(void *cd, const char *a, int len, int t),
615 struct sockaddr_in addr;
616 int len = sizeof(addr);
618 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
619 if (h->state != CS_IDLE)
621 h->cerrno = CSOUTSTATE;
624 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
629 WSAGetLastError() == WSAEWOULDBLOCK
634 h->cerrno = CSNODATA;
639 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
640 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
643 if (check_ip && (*check_ip)(cd, (const char *) &addr,
644 sizeof(addr), AF_INET))
648 closesocket(h->newfd);
659 COMSTACK tcpip_accept(COMSTACK h)
662 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
664 unsigned long tru = 1;
667 TRC(fprintf(stderr, "tcpip_accept\n"));
668 if (h->state == CS_INCON)
670 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
674 closesocket(h->newfd);
681 memcpy(cnew, h, sizeof(*h));
682 cnew->iofile = h->newfd;
683 cnew->io_pending = 0;
684 if (!(state = (tcpip_state *)
685 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
691 closesocket(h->newfd);
699 if (!cnew->blocking &&
701 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
703 (!cnew->blocking && fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
711 closesocket(h->newfd);
723 state->altsize = state->altlen = 0;
724 state->towrite = state->written = -1;
725 state->complete = st->complete;
726 cnew->state = CS_ACCEPT;
729 #if HAVE_OPENSSL_SSL_H
730 state->ctx = st->ctx;
731 state->ctx_alloc = 0;
732 state->ssl = st->ssl;
735 state->ssl = SSL_new (state->ctx);
736 SSL_set_fd (state->ssl, cnew->iofile);
741 if (h->state == CS_ACCEPT)
743 #if HAVE_OPENSSL_SSL_H
744 tcpip_state *state = (tcpip_state *)h->cprivate;
747 int res = SSL_accept (state->ssl);
748 TRC(fprintf(stderr, "SSL_accept\n"));
751 int err = SSL_get_error(state->ssl, res);
752 if (err == SSL_ERROR_WANT_READ)
754 h->io_pending = CS_WANT_READ;
755 yaz_log (LOG_LOG, "SSL_accept. want_read");
758 if (err == SSL_ERROR_WANT_WRITE)
760 h->io_pending = CS_WANT_WRITE;
761 yaz_log (LOG_LOG, "SSL_accept. want_write");
772 h->cerrno = CSOUTSTATE;
776 h->state = CS_DATAXFER;
780 #define CS_TCPIP_BUFCHUNK 4096
783 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
784 * 0=connection closed.
786 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
788 tcpip_state *sp = (tcpip_state *)h->cprivate;
790 int tmpi, berlen, rest, req, tomove;
791 int hasread = 0, res;
793 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
794 if (sp->altlen) /* switch buffers */
796 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
797 (unsigned) sp->altbuf));
801 *bufsize = sp->altsize;
802 hasread = sp->altlen;
808 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
812 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
815 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
816 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
818 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
819 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
823 if (WSAGetLastError() == WSAEWOULDBLOCK)
825 h->io_pending = CS_WANT_READ;
831 if (errno == EWOULDBLOCK
833 || errno == EINPROGRESS
837 h->io_pending = CS_WANT_READ;
850 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
852 /* move surplus buffer (or everything if we didn't get a BER rec.) */
853 if (hasread > berlen)
855 tomove = req = hasread - berlen;
856 rest = tomove % CS_TCPIP_BUFCHUNK;
858 req += CS_TCPIP_BUFCHUNK - rest;
861 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
863 } else if (sp->altsize < req)
864 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
866 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
867 (unsigned) sp->altbuf));
868 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
870 if (berlen < CS_TCPIP_BUFCHUNK - 1)
871 *(*buf + berlen) = '\0';
872 return berlen ? berlen : 1;
876 #if HAVE_OPENSSL_SSL_H
878 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
879 * 0=connection closed.
881 int ssl_get(COMSTACK h, char **buf, int *bufsize)
883 tcpip_state *sp = (tcpip_state *)h->cprivate;
885 int tmpi, berlen, rest, req, tomove;
886 int hasread = 0, res;
888 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
889 if (sp->altlen) /* switch buffers */
891 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
892 (unsigned) sp->altbuf));
896 *bufsize = sp->altsize;
897 hasread = sp->altlen;
903 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
907 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
910 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
911 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
913 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
914 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
917 int ssl_err = SSL_get_error(sp->ssl, res);
918 if (ssl_err == SSL_ERROR_WANT_READ)
920 h->io_pending = CS_WANT_READ;
921 yaz_log (LOG_LOG, "SSL_read. want_read");
924 if (ssl_err == SSL_ERROR_WANT_WRITE)
926 h->io_pending = CS_WANT_WRITE;
927 yaz_log (LOG_LOG, "SSL_read. want_write");
936 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
938 /* move surplus buffer (or everything if we didn't get a BER rec.) */
939 if (hasread > berlen)
941 tomove = req = hasread - berlen;
942 rest = tomove % CS_TCPIP_BUFCHUNK;
944 req += CS_TCPIP_BUFCHUNK - rest;
947 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
949 } else if (sp->altsize < req)
950 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
952 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
953 (unsigned) sp->altbuf));
954 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
956 if (berlen < CS_TCPIP_BUFCHUNK - 1)
957 *(*buf + berlen) = '\0';
958 return berlen ? berlen : 1;
964 * In nonblocking mode, you must call again with same buffer while
967 int tcpip_put(COMSTACK h, char *buf, int size)
970 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
972 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
974 if (state->towrite < 0)
976 state->towrite = size;
979 else if (state->towrite != size)
981 h->cerrno = CSWRONGBUF;
984 while (state->towrite > state->written)
986 if ((res = send(h->iofile, buf + state->written, size -
987 state->written, 0)) < 0)
991 WSAGetLastError() == WSAEWOULDBLOCK
997 TRC(fprintf(stderr, " Flow control stop\n"));
998 h->io_pending = CS_WANT_WRITE;
1001 h->cerrno = CSYSERR;
1004 state->written += res;
1005 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1006 res, state->written, size));
1008 state->towrite = state->written = -1;
1009 TRC(fprintf(stderr, " Ok\n"));
1014 #if HAVE_OPENSSL_SSL_H
1016 * Returns 1, 0 or -1
1017 * In nonblocking mode, you must call again with same buffer while
1018 * return value is 1.
1020 int ssl_put(COMSTACK h, char *buf, int size)
1023 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1025 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1027 if (state->towrite < 0)
1029 state->towrite = size;
1032 else if (state->towrite != size)
1034 h->cerrno = CSWRONGBUF;
1037 while (state->towrite > state->written)
1039 res = SSL_write (state->ssl, buf + state->written,
1040 size - state->written);
1043 int ssl_err = SSL_get_error(state->ssl, res);
1044 if (ssl_err == SSL_ERROR_WANT_READ)
1046 h->io_pending = CS_WANT_READ;
1047 yaz_log (LOG_LOG, "SSL_write. want_read");
1050 if (ssl_err == SSL_ERROR_WANT_WRITE)
1052 h->io_pending = CS_WANT_WRITE;
1053 yaz_log (LOG_LOG, "SSL_write. want_write");
1058 state->written += res;
1059 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1060 res, state->written, size));
1062 state->towrite = state->written = -1;
1063 TRC(fprintf(stderr, " Ok\n"));
1068 int tcpip_close(COMSTACK h)
1070 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1072 TRC(fprintf(stderr, "tcpip_close\n"));
1073 if (h->iofile != -1)
1075 #if HAVE_OPENSSL_SSL_H
1078 SSL_shutdown (sp->ssl);
1082 closesocket(h->iofile);
1089 #if HAVE_OPENSSL_SSL_H
1092 TRC (fprintf(stderr, "SSL_free\n"));
1097 SSL_CTX_free (sp->ctx_alloc);
1104 char *tcpip_addrstr(COMSTACK h)
1106 struct sockaddr_in addr;
1107 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1108 char *r, *buf = sp->buf;
1110 struct hostent *host;
1113 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1115 h->cerrno = CSYSERR;
1118 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1120 r = (char*) host->h_name;
1122 r = inet_ntoa(addr.sin_addr);
1123 sprintf(buf, "tcp:%s", r);
1124 #if HAVE_OPENSSL_SSL_H
1126 sprintf(buf, "ssl:%s", r);