2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.55 2003-02-21 12:08:57 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>
32 #include <netinet/tcp.h>
35 static int tcpip_close(COMSTACK h);
36 static int tcpip_put(COMSTACK h, char *buf, int size);
37 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
38 static int tcpip_connect(COMSTACK h, void *address);
39 static int tcpip_more(COMSTACK h);
40 static int tcpip_rcvconnect(COMSTACK h);
41 static int tcpip_bind(COMSTACK h, void *address, int mode);
42 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
43 int (*check_ip)(void *cd, const char *a, int len, int type),
45 static int tcpip_set_blocking(COMSTACK p, int blocking);
47 #if HAVE_OPENSSL_SSL_H
48 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
49 static int ssl_put(COMSTACK h, char *buf, int size);
52 static COMSTACK tcpip_accept(COMSTACK h);
53 static char *tcpip_addrstr(COMSTACK h);
54 static void *tcpip_straddr(COMSTACK h, const char *str);
63 #define YAZ_SOCKLEN_T int
66 /* this state is used for both SSL and straight TCP/IP */
67 typedef struct tcpip_state
69 char *altbuf; /* alternate buffer for surplus data */
70 int altsize; /* size as xmalloced */
71 int altlen; /* length of data or 0 if none */
73 int written; /* -1 if we aren't writing */
74 int towrite; /* to verify against user input */
75 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
76 struct sockaddr_in addr; /* returned by cs_straddr */
77 char buf[128]; /* returned by cs_addrstr */
78 #if HAVE_OPENSSL_SSL_H
86 static int tcpip_init (void)
88 static int initialized = 0;
94 requested = MAKEWORD(1, 1);
95 if (WSAStartup(requested, &wd))
102 static int tcpip_init (void)
109 * This function is always called through the cs_create() macro.
110 * s >= 0: socket has already been established for us.
112 COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
118 unsigned long tru = 1;
125 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
131 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
133 if (!(state = (struct tcpip_state *)(p->cprivate =
134 xmalloc(sizeof(tcpip_state)))))
138 if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0)
141 if (!(p->blocking = blocking))
143 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
146 signal (SIGPIPE, SIG_IGN);
153 p->type = tcpip_type;
154 p->protocol = (enum oid_proto) protocol;
156 p->f_connect = tcpip_connect;
157 p->f_rcvconnect = tcpip_rcvconnect;
158 p->f_get = tcpip_get;
159 p->f_put = tcpip_put;
160 p->f_close = tcpip_close;
161 p->f_more = tcpip_more;
162 p->f_bind = tcpip_bind;
163 p->f_listen = tcpip_listen;
164 p->f_accept = tcpip_accept;
165 p->f_addrstr = tcpip_addrstr;
166 p->f_straddr = tcpip_straddr;
167 p->f_set_blocking = tcpip_set_blocking;
169 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
174 #if HAVE_OPENSSL_SSL_H
175 state->ctx = state->ctx_alloc = 0;
180 state->altsize = state->altlen = 0;
181 state->towrite = state->written = -1;
182 if (protocol == PROTO_WAIS)
183 state->complete = completeWAIS;
185 state->complete = cs_complete_auto;
187 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
188 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
193 #if HAVE_OPENSSL_SSL_H
195 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
199 yaz_log(LOG_LOG, "ssl_type begin");
201 p = tcpip_type (s, blocking, protocol, 0);
207 state = (tcpip_state *) p->cprivate;
212 SSL_load_error_strings();
213 SSLeay_add_all_algorithms();
215 state->ctx = state->ctx_alloc = SSL_CTX_new (SSLv23_method());
222 /* note: we don't handle already opened socket in SSL mode - yet */
223 yaz_log(LOG_LOG, "ssl_type end");
228 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add)
232 short int port = 210;
237 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
238 add->sin_family = AF_INET;
239 strncpy(buf, str, 511);
241 if ((p = strchr(buf, '/')))
243 if ((p = strchr(buf, ':')))
248 add->sin_port = htons(port);
249 if (!strcmp("@", buf))
250 add->sin_addr.s_addr = INADDR_ANY;
251 else if ((hp = gethostbyname(buf)))
252 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
253 sizeof(struct in_addr));
254 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
255 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
261 void *tcpip_straddr(COMSTACK h, const char *str)
263 tcpip_state *sp = (tcpip_state *)h->cprivate;
265 if (!tcpip_strtoaddr_ex (str, &sp->addr))
270 struct sockaddr_in *tcpip_strtoaddr(const char *str)
272 static struct sockaddr_in add;
274 if (!tcpip_strtoaddr_ex (str, &add))
279 int tcpip_more(COMSTACK h)
281 tcpip_state *sp = (tcpip_state *)h->cprivate;
283 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
288 * connect(2) will block (sometimes) - nothing we can do short of doing
289 * weird things like spawning subprocesses or threading or some weird junk
292 int tcpip_connect(COMSTACK h, void *address)
294 struct sockaddr_in *add = (struct sockaddr_in *)address;
295 #if HAVE_OPENSSL_SSL_H
296 tcpip_state *sp = (tcpip_state *)h->cprivate;
301 socklen_t rbufsize = sizeof(recbuflen);
303 TRC(fprintf(stderr, "tcpip_connect\n"));
305 if (h->state != CS_ST_UNBND)
307 h->cerrno = CSOUTSTATE;
311 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
312 * This gives the connect a chance to negotiate with the other side
315 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
320 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
322 recbuflen *= 10; /* lets be optimistic */
323 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
328 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
333 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
336 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
340 if (WSAGetLastError() == WSAEWOULDBLOCK)
342 h->event = CS_CONNECT;
343 h->state = CS_ST_CONNECTING;
344 h->io_pending = CS_WANT_WRITE;
348 if (yaz_errno() == EINPROGRESS)
350 h->event = CS_CONNECT;
351 h->state = CS_ST_CONNECTING;
352 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
359 h->event = CS_CONNECT;
360 h->state = CS_ST_CONNECTING;
362 return tcpip_rcvconnect (h);
368 int tcpip_rcvconnect(COMSTACK h)
370 #if HAVE_OPENSSL_SSL_H
371 tcpip_state *sp = (tcpip_state *)h->cprivate;
373 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
375 if (h->state == CS_ST_DATAXFER)
377 if (h->state != CS_ST_CONNECTING)
379 h->cerrno = CSOUTSTATE;
382 #if HAVE_OPENSSL_SSL_H
389 sp->ssl = SSL_new (sp->ctx);
390 SSL_set_fd (sp->ssl, h->iofile);
392 res = SSL_connect (sp->ssl);
395 int err = SSL_get_error(sp->ssl, res);
396 if (err == SSL_ERROR_WANT_READ)
398 h->io_pending = CS_WANT_READ;
401 if (err == SSL_ERROR_WANT_WRITE)
403 h->io_pending = CS_WANT_WRITE;
406 h->cerrno = CSERRORSSL;
412 h->state = CS_ST_DATAXFER;
416 #define CERTF "ztest.pem"
417 #define KEYF "ztest.pem"
419 static void tcpip_setsockopt (int fd)
425 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
427 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
429 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
431 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
433 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
435 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
440 static int tcpip_bind(COMSTACK h, void *address, int mode)
442 struct sockaddr *addr = (struct sockaddr *)address;
446 unsigned long one = 1;
449 #if HAVE_OPENSSL_SSL_H
450 tcpip_state *sp = (tcpip_state *)h->cprivate;
456 res = SSL_CTX_use_certificate_file (sp->ctx, CERTF,
460 ERR_print_errors_fp(stderr);
463 res = SSL_CTX_use_PrivateKey_file (sp->ctx, KEYF,
467 ERR_print_errors_fp(stderr);
470 res = SSL_CTX_check_private_key (sp->ctx);
473 ERR_print_errors_fp(stderr);
477 TRC (fprintf (stderr, "ssl_bind\n"));
481 TRC (fprintf (stderr, "tcpip_bind\n"));
484 TRC (fprintf (stderr, "tcpip_bind\n"));
487 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
488 &one, sizeof(one)) < 0)
494 tcpip_setsockopt(h->iofile);
495 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)))
500 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
505 h->state = CS_ST_IDLE;
506 h->event = CS_LISTEN;
510 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
511 int (*check_ip)(void *cd, const char *a, int len, int t),
514 struct sockaddr_in addr;
515 YAZ_SOCKLEN_T len = sizeof(addr);
517 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
518 if (h->state != CS_ST_IDLE)
520 h->cerrno = CSOUTSTATE;
523 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
528 WSAGetLastError() == WSAEWOULDBLOCK
530 yaz_errno() == EWOULDBLOCK
532 #if EAGAIN != EWOULDBLOCK
533 || yaz_errno() == EAGAIN
538 h->cerrno = CSNODATA;
543 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
544 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
547 if (check_ip && (*check_ip)(cd, (const char *) &addr,
548 sizeof(addr), AF_INET))
552 closesocket(h->newfd);
559 h->state = CS_ST_INCON;
560 tcpip_setsockopt (h->newfd);
564 COMSTACK tcpip_accept(COMSTACK h)
567 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
569 unsigned long tru = 1;
572 TRC(fprintf(stderr, "tcpip_accept\n"));
573 if (h->state == CS_ST_INCON)
575 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
579 closesocket(h->newfd);
586 memcpy(cnew, h, sizeof(*h));
587 cnew->iofile = h->newfd;
588 cnew->io_pending = 0;
589 if (!(state = (tcpip_state *)
590 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
596 closesocket(h->newfd);
604 if (!cnew->blocking &&
606 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
608 (!cnew->blocking && fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
616 closesocket(h->newfd);
628 state->altsize = state->altlen = 0;
629 state->towrite = state->written = -1;
630 state->complete = st->complete;
631 cnew->state = CS_ST_ACCEPT;
632 h->state = CS_ST_IDLE;
634 #if HAVE_OPENSSL_SSL_H
635 state->ctx = st->ctx;
636 state->ctx_alloc = 0;
637 state->ssl = st->ssl;
640 state->ssl = SSL_new (state->ctx);
641 SSL_set_fd (state->ssl, cnew->iofile);
646 if (h->state == CS_ST_ACCEPT)
648 #if HAVE_OPENSSL_SSL_H
649 tcpip_state *state = (tcpip_state *)h->cprivate;
652 int res = SSL_accept (state->ssl);
653 TRC(fprintf(stderr, "SSL_accept\n"));
656 int err = SSL_get_error(state->ssl, res);
657 if (err == SSL_ERROR_WANT_READ)
659 h->io_pending = CS_WANT_READ;
662 if (err == SSL_ERROR_WANT_WRITE)
664 h->io_pending = CS_WANT_WRITE;
675 h->cerrno = CSOUTSTATE;
679 h->state = CS_ST_DATAXFER;
684 #define CS_TCPIP_BUFCHUNK 4096
687 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
688 * 0=connection closed.
690 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
692 tcpip_state *sp = (tcpip_state *)h->cprivate;
694 int tmpi, berlen, rest, req, tomove;
695 int hasread = 0, res;
697 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
698 if (sp->altlen) /* switch buffers */
700 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
701 (unsigned) sp->altbuf));
705 *bufsize = sp->altsize;
706 hasread = sp->altlen;
712 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
716 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
719 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
720 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
724 // unfortunatly, sun sometimes forgets to set errno in recv
725 // when EWOULDBLOCK etc. would be required (res = -1)
727 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
728 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
731 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
732 strerror(yaz_errno())));
734 if (WSAGetLastError() == WSAEWOULDBLOCK)
736 h->io_pending = CS_WANT_READ;
742 if (yaz_errno() == EWOULDBLOCK
744 #if EAGAIN != EWOULDBLOCK
745 || yaz_errno() == EAGAIN
748 || yaz_errno() == EINPROGRESS
750 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
754 h->io_pending = CS_WANT_READ;
757 else if (yaz_errno() == 0)
767 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
769 /* move surplus buffer (or everything if we didn't get a BER rec.) */
770 if (hasread > berlen)
772 tomove = req = hasread - berlen;
773 rest = tomove % CS_TCPIP_BUFCHUNK;
775 req += CS_TCPIP_BUFCHUNK - rest;
778 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
780 } else if (sp->altsize < req)
781 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
783 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
784 (unsigned) sp->altbuf));
785 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
787 if (berlen < CS_TCPIP_BUFCHUNK - 1)
788 *(*buf + berlen) = '\0';
789 return berlen ? berlen : 1;
793 #if HAVE_OPENSSL_SSL_H
795 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
796 * 0=connection closed.
798 int ssl_get(COMSTACK h, char **buf, int *bufsize)
800 tcpip_state *sp = (tcpip_state *)h->cprivate;
802 int tmpi, berlen, rest, req, tomove;
803 int hasread = 0, res;
805 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
806 if (sp->altlen) /* switch buffers */
808 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
809 (unsigned) sp->altbuf));
813 *bufsize = sp->altsize;
814 hasread = sp->altlen;
820 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
824 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
827 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
828 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
830 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
831 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
834 int ssl_err = SSL_get_error(sp->ssl, res);
835 if (ssl_err == SSL_ERROR_WANT_READ)
837 h->io_pending = CS_WANT_READ;
840 if (ssl_err == SSL_ERROR_WANT_WRITE)
842 h->io_pending = CS_WANT_WRITE;
847 h->cerrno = CSERRORSSL;
852 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
854 /* move surplus buffer (or everything if we didn't get a BER rec.) */
855 if (hasread > berlen)
857 tomove = req = hasread - berlen;
858 rest = tomove % CS_TCPIP_BUFCHUNK;
860 req += CS_TCPIP_BUFCHUNK - rest;
863 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
865 } else if (sp->altsize < req)
866 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
868 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
869 (unsigned) sp->altbuf));
870 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
872 if (berlen < CS_TCPIP_BUFCHUNK - 1)
873 *(*buf + berlen) = '\0';
874 return berlen ? berlen : 1;
880 * In nonblocking mode, you must call again with same buffer while
883 int tcpip_put(COMSTACK h, char *buf, int size)
886 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
888 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
891 if (state->towrite < 0)
893 state->towrite = size;
896 else if (state->towrite != size)
898 h->cerrno = CSWRONGBUF;
901 while (state->towrite > state->written)
904 send(h->iofile, buf + state->written, size -
915 WSAGetLastError() == WSAEWOULDBLOCK
917 yaz_errno() == EWOULDBLOCK
919 #if EAGAIN != EWOULDBLOCK
920 || yaz_errno() == EAGAIN
924 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
926 || yaz_errno() == EINPROGRESS
930 TRC(fprintf(stderr, " Flow control stop\n"));
931 h->io_pending = CS_WANT_WRITE;
937 state->written += res;
938 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
939 res, state->written, size));
941 state->towrite = state->written = -1;
942 TRC(fprintf(stderr, " Ok\n"));
947 #if HAVE_OPENSSL_SSL_H
950 * In nonblocking mode, you must call again with same buffer while
953 int ssl_put(COMSTACK h, char *buf, int size)
956 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
958 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
961 if (state->towrite < 0)
963 state->towrite = size;
966 else if (state->towrite != size)
968 h->cerrno = CSWRONGBUF;
971 while (state->towrite > state->written)
973 res = SSL_write (state->ssl, buf + state->written,
974 size - state->written);
977 int ssl_err = SSL_get_error(state->ssl, res);
978 if (ssl_err == SSL_ERROR_WANT_READ)
980 h->io_pending = CS_WANT_READ;
983 if (ssl_err == SSL_ERROR_WANT_WRITE)
985 h->io_pending = CS_WANT_WRITE;
988 h->cerrno = CSERRORSSL;
991 state->written += res;
992 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
993 res, state->written, size));
995 state->towrite = state->written = -1;
996 TRC(fprintf(stderr, " Ok\n"));
1001 int tcpip_close(COMSTACK h)
1003 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1005 TRC(fprintf(stderr, "tcpip_close\n"));
1006 if (h->iofile != -1)
1008 #if HAVE_OPENSSL_SSL_H
1011 SSL_shutdown (sp->ssl);
1015 closesocket(h->iofile);
1022 #if HAVE_OPENSSL_SSL_H
1025 TRC (fprintf(stderr, "SSL_free\n"));
1030 SSL_CTX_free (sp->ctx_alloc);
1037 char *tcpip_addrstr(COMSTACK h)
1039 struct sockaddr_in addr;
1040 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1041 char *r, *buf = sp->buf;
1043 struct hostent *host;
1046 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1048 h->cerrno = CSYSERR;
1051 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1053 r = (char*) host->h_name;
1055 r = inet_ntoa(addr.sin_addr);
1056 sprintf(buf, "tcp:%s", r);
1057 #if HAVE_OPENSSL_SSL_H
1059 sprintf(buf, "ssl:%s", r);
1064 int static tcpip_set_blocking(COMSTACK p, int blocking)
1068 if (p->blocking == blocking)
1072 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1075 flag = fcntl(p->iofile, F_GETFL, 0);
1077 flag = flag & ~O_NONBLOCK;
1079 flag = flag | O_NONBLOCK;
1080 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1083 p->blocking = blocking;