2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.54 2003-02-20 15:10:24 adam Exp $
20 #if HAVE_OPENSSL_SSL_H
21 #include <openssl/ssl.h>
22 #include <openssl/err.h>
25 #include <yaz/comstack.h>
26 #include <yaz/tcpip.h>
31 #include <netinet/tcp.h>
34 /* Chas added the following, so we get the definition of completeBER */
37 static int tcpip_close(COMSTACK h);
38 static int tcpip_put(COMSTACK h, char *buf, int size);
39 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
40 static int tcpip_connect(COMSTACK h, void *address);
41 static int tcpip_more(COMSTACK h);
42 static int tcpip_rcvconnect(COMSTACK h);
43 static int tcpip_bind(COMSTACK h, void *address, int mode);
44 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
45 int (*check_ip)(void *cd, const char *a, int len, int type),
47 static int tcpip_set_blocking(COMSTACK p, int blocking);
49 #if HAVE_OPENSSL_SSL_H
50 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
51 static int ssl_put(COMSTACK h, char *buf, int size);
54 static COMSTACK tcpip_accept(COMSTACK h);
55 static char *tcpip_addrstr(COMSTACK h);
56 static void *tcpip_straddr(COMSTACK h, const char *str);
65 #define YAZ_SOCKLEN_T int
68 /* this state is used for both SSL and straight TCP/IP */
69 typedef struct tcpip_state
71 char *altbuf; /* alternate buffer for surplus data */
72 int altsize; /* size as xmalloced */
73 int altlen; /* length of data or 0 if none */
75 int written; /* -1 if we aren't writing */
76 int towrite; /* to verify against user input */
77 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
78 struct sockaddr_in addr; /* returned by cs_straddr */
79 char buf[128]; /* returned by cs_addrstr */
80 #if HAVE_OPENSSL_SSL_H
88 static int tcpip_init (void)
90 static int initialized = 0;
96 requested = MAKEWORD(1, 1);
97 if (WSAStartup(requested, &wd))
104 static int tcpip_init (void)
111 * This function is always called through the cs_create() macro.
112 * s >= 0: socket has already been established for us.
114 COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
120 unsigned long tru = 1;
127 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
133 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
135 if (!(state = (struct tcpip_state *)(p->cprivate =
136 xmalloc(sizeof(tcpip_state)))))
140 if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0)
143 if (!(p->blocking = blocking))
145 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
148 signal (SIGPIPE, SIG_IGN);
155 p->type = tcpip_type;
156 p->protocol = (enum oid_proto) protocol;
158 p->f_connect = tcpip_connect;
159 p->f_rcvconnect = tcpip_rcvconnect;
160 p->f_get = tcpip_get;
161 p->f_put = tcpip_put;
162 p->f_close = tcpip_close;
163 p->f_more = tcpip_more;
164 p->f_bind = tcpip_bind;
165 p->f_listen = tcpip_listen;
166 p->f_accept = tcpip_accept;
167 p->f_addrstr = tcpip_addrstr;
168 p->f_straddr = tcpip_straddr;
169 p->f_set_blocking = tcpip_set_blocking;
171 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
176 #if HAVE_OPENSSL_SSL_H
177 state->ctx = state->ctx_alloc = 0;
182 state->altsize = state->altlen = 0;
183 state->towrite = state->written = -1;
184 if (protocol == PROTO_WAIS)
185 state->complete = completeWAIS;
187 state->complete = completeBER;
189 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
190 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
195 #if HAVE_OPENSSL_SSL_H
197 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
201 yaz_log(LOG_LOG, "ssl_type begin");
203 p = tcpip_type (s, blocking, protocol, 0);
209 state = (tcpip_state *) p->cprivate;
214 SSL_load_error_strings();
215 SSLeay_add_all_algorithms();
217 state->ctx = state->ctx_alloc = SSL_CTX_new (SSLv23_method());
224 /* note: we don't handle already opened socket in SSL mode - yet */
225 yaz_log(LOG_LOG, "ssl_type end");
230 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add)
234 short int port = 210;
239 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
240 add->sin_family = AF_INET;
241 strncpy(buf, str, 511);
243 if ((p = strchr(buf, '/')))
245 if ((p = strchr(buf, ':')))
250 add->sin_port = htons(port);
251 if (!strcmp("@", buf))
252 add->sin_addr.s_addr = INADDR_ANY;
253 else if ((hp = gethostbyname(buf)))
254 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
255 sizeof(struct in_addr));
256 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
257 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
263 void *tcpip_straddr(COMSTACK h, const char *str)
265 tcpip_state *sp = (tcpip_state *)h->cprivate;
267 if (!tcpip_strtoaddr_ex (str, &sp->addr))
272 struct sockaddr_in *tcpip_strtoaddr(const char *str)
274 static struct sockaddr_in add;
276 if (!tcpip_strtoaddr_ex (str, &add))
281 int tcpip_more(COMSTACK h)
283 tcpip_state *sp = (tcpip_state *)h->cprivate;
285 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
290 * connect(2) will block (sometimes) - nothing we can do short of doing
291 * weird things like spawning subprocesses or threading or some weird junk
294 int tcpip_connect(COMSTACK h, void *address)
296 struct sockaddr_in *add = (struct sockaddr_in *)address;
297 #if HAVE_OPENSSL_SSL_H
298 tcpip_state *sp = (tcpip_state *)h->cprivate;
303 socklen_t rbufsize = sizeof(recbuflen);
305 TRC(fprintf(stderr, "tcpip_connect\n"));
307 if (h->state != CS_ST_UNBND)
309 h->cerrno = CSOUTSTATE;
313 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
314 * This gives the connect a chance to negotiate with the other side
317 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
322 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
324 recbuflen *= 10; /* lets be optimistic */
325 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
330 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
335 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
338 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
342 if (WSAGetLastError() == WSAEWOULDBLOCK)
344 h->event = CS_CONNECT;
345 h->state = CS_ST_CONNECTING;
346 h->io_pending = CS_WANT_WRITE;
350 if (yaz_errno() == EINPROGRESS)
352 h->event = CS_CONNECT;
353 h->state = CS_ST_CONNECTING;
354 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
361 h->event = CS_CONNECT;
362 h->state = CS_ST_CONNECTING;
364 return tcpip_rcvconnect (h);
370 int tcpip_rcvconnect(COMSTACK h)
372 #if HAVE_OPENSSL_SSL_H
373 tcpip_state *sp = (tcpip_state *)h->cprivate;
375 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
377 if (h->state == CS_ST_DATAXFER)
379 if (h->state != CS_ST_CONNECTING)
381 h->cerrno = CSOUTSTATE;
384 #if HAVE_OPENSSL_SSL_H
391 sp->ssl = SSL_new (sp->ctx);
392 SSL_set_fd (sp->ssl, h->iofile);
394 res = SSL_connect (sp->ssl);
397 int err = SSL_get_error(sp->ssl, res);
398 if (err == SSL_ERROR_WANT_READ)
400 h->io_pending = CS_WANT_READ;
403 if (err == SSL_ERROR_WANT_WRITE)
405 h->io_pending = CS_WANT_WRITE;
408 h->cerrno = CSERRORSSL;
414 h->state = CS_ST_DATAXFER;
418 #define CERTF "ztest.pem"
419 #define KEYF "ztest.pem"
421 static void tcpip_setsockopt (int fd)
427 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
429 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
431 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
433 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
435 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
437 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
442 static int tcpip_bind(COMSTACK h, void *address, int mode)
444 struct sockaddr *addr = (struct sockaddr *)address;
448 unsigned long one = 1;
451 #if HAVE_OPENSSL_SSL_H
452 tcpip_state *sp = (tcpip_state *)h->cprivate;
458 res = SSL_CTX_use_certificate_file (sp->ctx, CERTF,
462 ERR_print_errors_fp(stderr);
465 res = SSL_CTX_use_PrivateKey_file (sp->ctx, KEYF,
469 ERR_print_errors_fp(stderr);
472 res = SSL_CTX_check_private_key (sp->ctx);
475 ERR_print_errors_fp(stderr);
479 TRC (fprintf (stderr, "ssl_bind\n"));
483 TRC (fprintf (stderr, "tcpip_bind\n"));
486 TRC (fprintf (stderr, "tcpip_bind\n"));
489 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
490 &one, sizeof(one)) < 0)
496 tcpip_setsockopt(h->iofile);
497 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)))
502 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
507 h->state = CS_ST_IDLE;
508 h->event = CS_LISTEN;
512 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
513 int (*check_ip)(void *cd, const char *a, int len, int t),
516 struct sockaddr_in addr;
517 YAZ_SOCKLEN_T len = sizeof(addr);
519 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
520 if (h->state != CS_ST_IDLE)
522 h->cerrno = CSOUTSTATE;
525 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
530 WSAGetLastError() == WSAEWOULDBLOCK
532 yaz_errno() == EWOULDBLOCK
534 #if EAGAIN != EWOULDBLOCK
535 || yaz_errno() == EAGAIN
540 h->cerrno = CSNODATA;
545 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
546 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
549 if (check_ip && (*check_ip)(cd, (const char *) &addr,
550 sizeof(addr), AF_INET))
554 closesocket(h->newfd);
561 h->state = CS_ST_INCON;
562 tcpip_setsockopt (h->newfd);
566 COMSTACK tcpip_accept(COMSTACK h)
569 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
571 unsigned long tru = 1;
574 TRC(fprintf(stderr, "tcpip_accept\n"));
575 if (h->state == CS_ST_INCON)
577 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
581 closesocket(h->newfd);
588 memcpy(cnew, h, sizeof(*h));
589 cnew->iofile = h->newfd;
590 cnew->io_pending = 0;
591 if (!(state = (tcpip_state *)
592 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
598 closesocket(h->newfd);
606 if (!cnew->blocking &&
608 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
610 (!cnew->blocking && fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
618 closesocket(h->newfd);
630 state->altsize = state->altlen = 0;
631 state->towrite = state->written = -1;
632 state->complete = st->complete;
633 cnew->state = CS_ST_ACCEPT;
634 h->state = CS_ST_IDLE;
636 #if HAVE_OPENSSL_SSL_H
637 state->ctx = st->ctx;
638 state->ctx_alloc = 0;
639 state->ssl = st->ssl;
642 state->ssl = SSL_new (state->ctx);
643 SSL_set_fd (state->ssl, cnew->iofile);
648 if (h->state == CS_ST_ACCEPT)
650 #if HAVE_OPENSSL_SSL_H
651 tcpip_state *state = (tcpip_state *)h->cprivate;
654 int res = SSL_accept (state->ssl);
655 TRC(fprintf(stderr, "SSL_accept\n"));
658 int err = SSL_get_error(state->ssl, res);
659 if (err == SSL_ERROR_WANT_READ)
661 h->io_pending = CS_WANT_READ;
664 if (err == SSL_ERROR_WANT_WRITE)
666 h->io_pending = CS_WANT_WRITE;
677 h->cerrno = CSOUTSTATE;
681 h->state = CS_ST_DATAXFER;
686 #define CS_TCPIP_BUFCHUNK 4096
689 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
690 * 0=connection closed.
692 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
694 tcpip_state *sp = (tcpip_state *)h->cprivate;
696 int tmpi, berlen, rest, req, tomove;
697 int hasread = 0, res;
699 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
700 if (sp->altlen) /* switch buffers */
702 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
703 (unsigned) sp->altbuf));
707 *bufsize = sp->altsize;
708 hasread = sp->altlen;
714 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
718 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
721 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
722 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
726 // unfortunatly, sun sometimes forgets to set errno in recv
727 // when EWOULDBLOCK etc. would be required (res = -1)
729 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
730 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
733 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
734 strerror(yaz_errno())));
736 if (WSAGetLastError() == WSAEWOULDBLOCK)
738 h->io_pending = CS_WANT_READ;
744 if (yaz_errno() == EWOULDBLOCK
746 #if EAGAIN != EWOULDBLOCK
747 || yaz_errno() == EAGAIN
750 || yaz_errno() == EINPROGRESS
752 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
756 h->io_pending = CS_WANT_READ;
759 else if (yaz_errno() == 0)
769 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
771 /* move surplus buffer (or everything if we didn't get a BER rec.) */
772 if (hasread > berlen)
774 tomove = req = hasread - berlen;
775 rest = tomove % CS_TCPIP_BUFCHUNK;
777 req += CS_TCPIP_BUFCHUNK - rest;
780 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
782 } else if (sp->altsize < req)
783 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
785 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
786 (unsigned) sp->altbuf));
787 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
789 if (berlen < CS_TCPIP_BUFCHUNK - 1)
790 *(*buf + berlen) = '\0';
791 return berlen ? berlen : 1;
795 #if HAVE_OPENSSL_SSL_H
797 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
798 * 0=connection closed.
800 int ssl_get(COMSTACK h, char **buf, int *bufsize)
802 tcpip_state *sp = (tcpip_state *)h->cprivate;
804 int tmpi, berlen, rest, req, tomove;
805 int hasread = 0, res;
807 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
808 if (sp->altlen) /* switch buffers */
810 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
811 (unsigned) sp->altbuf));
815 *bufsize = sp->altsize;
816 hasread = sp->altlen;
822 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
826 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
829 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
830 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
832 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
833 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
836 int ssl_err = SSL_get_error(sp->ssl, res);
837 if (ssl_err == SSL_ERROR_WANT_READ)
839 h->io_pending = CS_WANT_READ;
842 if (ssl_err == SSL_ERROR_WANT_WRITE)
844 h->io_pending = CS_WANT_WRITE;
849 h->cerrno = CSERRORSSL;
854 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
856 /* move surplus buffer (or everything if we didn't get a BER rec.) */
857 if (hasread > berlen)
859 tomove = req = hasread - berlen;
860 rest = tomove % CS_TCPIP_BUFCHUNK;
862 req += CS_TCPIP_BUFCHUNK - rest;
865 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
867 } else if (sp->altsize < req)
868 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
870 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
871 (unsigned) sp->altbuf));
872 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
874 if (berlen < CS_TCPIP_BUFCHUNK - 1)
875 *(*buf + berlen) = '\0';
876 return berlen ? berlen : 1;
882 * In nonblocking mode, you must call again with same buffer while
885 int tcpip_put(COMSTACK h, char *buf, int size)
888 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
890 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
893 if (state->towrite < 0)
895 state->towrite = size;
898 else if (state->towrite != size)
900 h->cerrno = CSWRONGBUF;
903 while (state->towrite > state->written)
906 send(h->iofile, buf + state->written, size -
917 WSAGetLastError() == WSAEWOULDBLOCK
919 yaz_errno() == EWOULDBLOCK
921 #if EAGAIN != EWOULDBLOCK
922 || yaz_errno() == EAGAIN
926 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
928 || yaz_errno() == EINPROGRESS
932 TRC(fprintf(stderr, " Flow control stop\n"));
933 h->io_pending = CS_WANT_WRITE;
939 state->written += res;
940 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
941 res, state->written, size));
943 state->towrite = state->written = -1;
944 TRC(fprintf(stderr, " Ok\n"));
949 #if HAVE_OPENSSL_SSL_H
952 * In nonblocking mode, you must call again with same buffer while
955 int ssl_put(COMSTACK h, char *buf, int size)
958 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
960 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
963 if (state->towrite < 0)
965 state->towrite = size;
968 else if (state->towrite != size)
970 h->cerrno = CSWRONGBUF;
973 while (state->towrite > state->written)
975 res = SSL_write (state->ssl, buf + state->written,
976 size - state->written);
979 int ssl_err = SSL_get_error(state->ssl, res);
980 if (ssl_err == SSL_ERROR_WANT_READ)
982 h->io_pending = CS_WANT_READ;
985 if (ssl_err == SSL_ERROR_WANT_WRITE)
987 h->io_pending = CS_WANT_WRITE;
990 h->cerrno = CSERRORSSL;
993 state->written += res;
994 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
995 res, state->written, size));
997 state->towrite = state->written = -1;
998 TRC(fprintf(stderr, " Ok\n"));
1003 int tcpip_close(COMSTACK h)
1005 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1007 TRC(fprintf(stderr, "tcpip_close\n"));
1008 if (h->iofile != -1)
1010 #if HAVE_OPENSSL_SSL_H
1013 SSL_shutdown (sp->ssl);
1017 closesocket(h->iofile);
1024 #if HAVE_OPENSSL_SSL_H
1027 TRC (fprintf(stderr, "SSL_free\n"));
1032 SSL_CTX_free (sp->ctx_alloc);
1039 char *tcpip_addrstr(COMSTACK h)
1041 struct sockaddr_in addr;
1042 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1043 char *r, *buf = sp->buf;
1045 struct hostent *host;
1048 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1050 h->cerrno = CSYSERR;
1053 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1055 r = (char*) host->h_name;
1057 r = inet_ntoa(addr.sin_addr);
1058 sprintf(buf, "tcp:%s", r);
1059 #if HAVE_OPENSSL_SSL_H
1061 sprintf(buf, "ssl:%s", r);
1066 int static tcpip_set_blocking(COMSTACK p, int blocking)
1070 if (p->blocking == blocking)
1074 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1077 flag = fcntl(p->iofile, F_GETFL, 0);
1079 flag = flag & ~O_NONBLOCK;
1081 flag = flag | O_NONBLOCK;
1082 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1085 p->blocking = blocking;