2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.38 2007-11-30 11:44:47 adam Exp $
9 * \brief Implements TCP/IP + SSL COMSTACK.
19 #include <sys/types.h>
30 /* VS 2003 or later has getaddrinfo; older versions do not */
34 #define HAVE_GETADDRINFO 1
36 #define HAVE_GETADDRINFO 0
40 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #include <netinet/tcp.h>
47 #include <sys/socket.h>
53 #if HAVE_OPENSSL_SSL_H
54 #include <openssl/ssl.h>
55 #include <openssl/err.h>
58 #include <yaz/comstack.h>
59 #include <yaz/tcpip.h>
62 static int tcpip_close(COMSTACK h);
63 static int tcpip_put(COMSTACK h, char *buf, int size);
64 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
65 static int tcpip_put_connect(COMSTACK h, char *buf, int size);
66 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize);
67 static int tcpip_connect(COMSTACK h, void *address);
68 static int tcpip_more(COMSTACK h);
69 static int tcpip_rcvconnect(COMSTACK h);
70 static int tcpip_bind(COMSTACK h, void *address, int mode);
71 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
72 int (*check_ip)(void *cd, const char *a, int len, int type),
74 static int tcpip_set_blocking(COMSTACK p, int blocking);
76 #if HAVE_OPENSSL_SSL_H
77 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
78 static int ssl_put(COMSTACK h, char *buf, int size);
81 static COMSTACK tcpip_accept(COMSTACK h);
82 static char *tcpip_addrstr(COMSTACK h);
83 static void *tcpip_straddr(COMSTACK h, const char *str);
92 #define YAZ_SOCKLEN_T int
95 /* this state is used for both SSL and straight TCP/IP */
96 typedef struct tcpip_state
98 char *altbuf; /* alternate buffer for surplus data */
99 int altsize; /* size as xmalloced */
100 int altlen; /* length of data or 0 if none */
102 int written; /* -1 if we aren't writing */
103 int towrite; /* to verify against user input */
104 int (*complete)(const char *buf, int len); /* length/complete. */
108 struct sockaddr_in addr; /* returned by cs_straddr */
110 char buf[128]; /* returned by cs_addrstr */
111 #if HAVE_OPENSSL_SSL_H
112 SSL_CTX *ctx; /* current CTX. */
113 SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
115 char cert_fname[256];
117 char *connect_request_buf;
118 int connect_request_len;
119 char *connect_response_buf;
120 int connect_response_len;
124 static int tcpip_init (void)
126 static int initialized = 0;
132 requested = MAKEWORD(1, 1);
133 if (WSAStartup(requested, &wd))
140 static int tcpip_init (void)
147 * This function is always called through the cs_create() macro.
148 * s >= 0: socket has already been established for us.
150 COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
157 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
159 if (!(sp = (struct tcpip_state *)(p->cprivate =
160 xmalloc(sizeof(tcpip_state)))))
167 p->type = tcpip_type;
168 p->protocol = (enum oid_proto) protocol;
170 p->f_connect = tcpip_connect;
171 p->f_rcvconnect = tcpip_rcvconnect;
172 p->f_get = tcpip_get;
173 p->f_put = tcpip_put;
174 p->f_close = tcpip_close;
175 p->f_more = tcpip_more;
176 p->f_bind = tcpip_bind;
177 p->f_listen = tcpip_listen;
178 p->f_accept = tcpip_accept;
179 p->f_addrstr = tcpip_addrstr;
180 p->f_straddr = tcpip_straddr;
181 p->f_set_blocking = tcpip_set_blocking;
182 p->max_recv_bytes = 5000000;
184 p->state = s < 0 ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
190 #if HAVE_OPENSSL_SSL_H
191 sp->ctx = sp->ctx_alloc = 0;
193 strcpy(sp->cert_fname, "yaz.pem");
200 sp->altsize = sp->altlen = 0;
201 sp->towrite = sp->written = -1;
202 if (protocol == PROTO_WAIS)
203 sp->complete = completeWAIS;
205 sp->complete = cs_complete_auto;
207 sp->connect_request_buf = 0;
208 sp->connect_request_len = 0;
209 sp->connect_response_buf = 0;
210 sp->connect_response_len = 0;
212 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
213 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
218 COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
219 const char *connect_host)
221 COMSTACK p = tcpip_type(s, flags, protocol, 0);
226 tcpip_state *sp = (tcpip_state *) p->cprivate;
227 sp->connect_request_buf = (char *) xmalloc(strlen(connect_host) + 30);
228 sprintf(sp->connect_request_buf, "CONNECT %s HTTP/1.0\r\n\r\n",
230 sp->connect_request_len = strlen(sp->connect_request_buf);
231 p->f_put = tcpip_put_connect;
232 p->f_get = tcpip_get_connect;
233 sp->complete = cs_complete_auto_head; /* only want HTTP header */
239 #if HAVE_OPENSSL_SSL_H
241 COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
246 p = tcpip_type (s, flags, protocol, 0);
252 sp = (tcpip_state *) p->cprivate;
254 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
256 /* note: we don't handle already opened socket in SSL mode - yet */
262 /* resolve using getaddrinfo */
263 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
265 struct addrinfo hints, *res;
270 hints.ai_family = AF_UNSPEC;
271 hints.ai_socktype = SOCK_STREAM;
272 hints.ai_protocol = 0;
273 hints.ai_addrlen = 0;
274 hints.ai_addr = NULL;
275 hints.ai_canonname = NULL;
276 hints.ai_next = NULL;
278 strncpy(host, str, sizeof(host)-1);
279 host[sizeof(host)-1] = 0;
280 if ((p = strchr(host, '/')))
282 if ((p = strrchr(host, ':')))
288 if (!strcmp("@", host))
290 hints.ai_flags = AI_PASSIVE;
291 error = getaddrinfo(0, port, &hints, &res);
295 error = getaddrinfo(host, port, &hints, &res);
303 /* gethostbyname .. old systems */
304 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
309 short int port = default_port;
311 unsigned long tmpadd;
315 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
316 add->sin_family = AF_INET;
317 strncpy(buf, str, sizeof(buf)-1);
318 buf[sizeof(buf)-1] = 0;
319 if ((p = strchr(buf, '/')))
321 if ((p = strrchr(buf, ':')))
326 add->sin_port = htons(port);
327 if (!strcmp("@", buf))
329 add->sin_addr.s_addr = INADDR_ANY;
331 else if ((tmpadd = inet_addr(buf)) != -1)
333 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
335 else if ((hp = gethostbyname(buf)))
337 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
338 sizeof(struct in_addr));
347 void *tcpip_straddr(COMSTACK h, const char *str)
349 tcpip_state *sp = (tcpip_state *)h->cprivate;
350 const char *port = "210";
351 if (h->protocol == PROTO_HTTP)
357 freeaddrinfo(sp->ai);
358 sp->ai = tcpip_getaddrinfo(str, port);
359 if (sp->ai && h->state == CS_ST_UNBND)
362 struct addrinfo *ai = sp->ai;
363 for (; ai; ai = ai->ai_next)
365 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
373 if (!tcpip_set_blocking(h, h->flags))
379 void *tcpip_straddr(COMSTACK h, const char *str)
381 tcpip_state *sp = (tcpip_state *)h->cprivate;
383 if (h->protocol == PROTO_HTTP)
388 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
390 if (h->state == CS_ST_UNBND)
393 s = socket(AF_INET, SOCK_STREAM, 0);
398 if (!tcpip_set_blocking(h, h->flags))
405 int tcpip_more(COMSTACK h)
407 tcpip_state *sp = (tcpip_state *)h->cprivate;
409 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
413 * connect(2) will block (sometimes) - nothing we can do short of doing
414 * weird things like spawning subprocesses or threading or some weird junk
417 int tcpip_connect(COMSTACK h, void *address)
420 tcpip_state *sp = (tcpip_state *)h->cprivate;
422 struct sockaddr_in *add = (struct sockaddr_in *) address;
427 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
429 TRC(fprintf(stderr, "tcpip_connect\n"));
431 if (h->state != CS_ST_UNBND)
433 h->cerrno = CSOUTSTATE;
437 if (sp->ai != (struct addrinfo *) address)
439 h->cerrno = CSOUTSTATE;
444 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
445 * This gives the connect a chance to negotiate with the other side
448 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
453 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
455 recbuflen *= 10; /* lets be optimistic */
456 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
461 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
466 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
471 r = connect(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
472 freeaddrinfo(sp->ai);
475 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
480 if (WSAGetLastError() == WSAEWOULDBLOCK)
482 h->event = CS_CONNECT;
483 h->state = CS_ST_CONNECTING;
484 h->io_pending = CS_WANT_WRITE;
488 if (yaz_errno() == EINPROGRESS)
490 h->event = CS_CONNECT;
491 h->state = CS_ST_CONNECTING;
492 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
499 h->event = CS_CONNECT;
500 h->state = CS_ST_CONNECTING;
502 return tcpip_rcvconnect (h);
508 int tcpip_rcvconnect(COMSTACK h)
510 #if HAVE_OPENSSL_SSL_H
511 tcpip_state *sp = (tcpip_state *)h->cprivate;
513 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
515 if (h->state == CS_ST_DATAXFER)
517 if (h->state != CS_ST_CONNECTING)
519 h->cerrno = CSOUTSTATE;
522 #if HAVE_OPENSSL_SSL_H
523 if (h->type == ssl_type && !sp->ctx)
526 SSL_load_error_strings();
528 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
531 h->cerrno = CSERRORSSL;
541 sp->ssl = SSL_new (sp->ctx);
542 SSL_set_fd (sp->ssl, h->iofile);
544 res = SSL_connect (sp->ssl);
547 int err = SSL_get_error(sp->ssl, res);
548 if (err == SSL_ERROR_WANT_READ)
550 h->io_pending = CS_WANT_READ;
553 if (err == SSL_ERROR_WANT_WRITE)
555 h->io_pending = CS_WANT_WRITE;
558 h->cerrno = CSERRORSSL;
564 h->state = CS_ST_DATAXFER;
568 #define CERTF "ztest.pem"
569 #define KEYF "ztest.pem"
571 static void tcpip_setsockopt (int fd)
577 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
579 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
581 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
583 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
585 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
587 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
592 static int tcpip_bind(COMSTACK h, void *address, int mode)
595 tcpip_state *sp = (tcpip_state *)h->cprivate;
598 struct sockaddr *addr = (struct sockaddr *)address;
607 if (sp->ai != (struct addrinfo *) address)
609 h->cerrno = CSOUTSTATE;
614 #if HAVE_OPENSSL_SSL_H
615 if (h->type == ssl_type && !sp->ctx)
618 SSL_load_error_strings();
620 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
623 h->cerrno = CSERRORSSL;
632 res = SSL_CTX_use_certificate_chain_file(sp->ctx, sp->cert_fname);
635 ERR_print_errors_fp(stderr);
638 res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
642 ERR_print_errors_fp(stderr);
645 res = SSL_CTX_check_private_key (sp->ctx);
648 ERR_print_errors_fp(stderr);
652 TRC (fprintf (stderr, "ssl_bind\n"));
656 TRC (fprintf (stderr, "tcpip_bind\n"));
659 TRC (fprintf (stderr, "tcpip_bind\n"));
662 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
663 &one, sizeof(one)) < 0)
669 tcpip_setsockopt(h->iofile);
671 r = bind(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
672 freeaddrinfo(sp->ai);
675 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
682 /* Allow a maximum-sized backlog of waiting-to-connect clients */
683 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
688 h->state = CS_ST_IDLE;
689 h->event = CS_LISTEN;
693 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
694 int (*check_ip)(void *cd, const char *a, int len, int t),
697 struct sockaddr_in addr;
698 YAZ_SOCKLEN_T len = sizeof(addr);
700 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
701 if (h->state != CS_ST_IDLE)
703 h->cerrno = CSOUTSTATE;
706 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
711 WSAGetLastError() == WSAEWOULDBLOCK
713 yaz_errno() == EWOULDBLOCK
715 #if EAGAIN != EWOULDBLOCK
716 || yaz_errno() == EAGAIN
721 h->cerrno = CSNODATA;
726 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
727 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
730 if (check_ip && (*check_ip)(cd, (const char *) &addr,
731 sizeof(addr), AF_INET))
735 closesocket(h->newfd);
742 h->state = CS_ST_INCON;
743 tcpip_setsockopt (h->newfd);
747 COMSTACK tcpip_accept(COMSTACK h)
750 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
752 unsigned long tru = 1;
755 TRC(fprintf(stderr, "tcpip_accept\n"));
756 if (h->state == CS_ST_INCON)
758 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
762 closesocket(h->newfd);
769 memcpy(cnew, h, sizeof(*h));
770 cnew->iofile = h->newfd;
771 cnew->io_pending = 0;
772 if (!(state = (tcpip_state *)
773 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
779 closesocket(h->newfd);
787 if (!tcpip_set_blocking(cnew, cnew->flags))
793 closesocket(h->newfd);
805 state->altsize = state->altlen = 0;
806 state->towrite = state->written = -1;
807 state->complete = st->complete;
811 cnew->state = CS_ST_ACCEPT;
812 h->state = CS_ST_IDLE;
814 #if HAVE_OPENSSL_SSL_H
815 state->ctx = st->ctx;
816 state->ctx_alloc = 0;
817 state->ssl = st->ssl;
820 state->ssl = SSL_new (state->ctx);
821 SSL_set_fd (state->ssl, cnew->iofile);
823 state->connect_request_buf = 0;
824 state->connect_response_buf = 0;
828 if (h->state == CS_ST_ACCEPT)
830 #if HAVE_OPENSSL_SSL_H
831 tcpip_state *state = (tcpip_state *)h->cprivate;
834 int res = SSL_accept (state->ssl);
835 TRC(fprintf(stderr, "SSL_accept\n"));
838 int err = SSL_get_error(state->ssl, res);
839 if (err == SSL_ERROR_WANT_READ)
841 h->io_pending = CS_WANT_READ;
844 if (err == SSL_ERROR_WANT_WRITE)
846 h->io_pending = CS_WANT_WRITE;
857 h->cerrno = CSOUTSTATE;
861 h->state = CS_ST_DATAXFER;
866 #define CS_TCPIP_BUFCHUNK 4096
869 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
870 * 0=connection closed.
872 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
874 tcpip_state *sp = (tcpip_state *)h->cprivate;
876 int tmpi, berlen, rest, req, tomove;
877 int hasread = 0, res;
879 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
880 if (sp->altlen) /* switch buffers */
882 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
883 (unsigned) sp->altbuf));
887 *bufsize = sp->altsize;
888 hasread = sp->altlen;
894 while (!(berlen = (*sp->complete)(*buf, hasread)))
898 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
904 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
905 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
912 /* unfortunatly, sun sometimes forgets to set errno in recv
913 when EWOULDBLOCK etc. would be required (res = -1) */
915 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
916 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
919 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
920 strerror(yaz_errno())));
922 if (WSAGetLastError() == WSAEWOULDBLOCK)
924 h->io_pending = CS_WANT_READ;
933 if (yaz_errno() == EWOULDBLOCK
935 #if EAGAIN != EWOULDBLOCK
936 || yaz_errno() == EAGAIN
939 || yaz_errno() == EINPROGRESS
941 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
945 h->io_pending = CS_WANT_READ;
948 else if (yaz_errno() == 0)
960 if (hasread > h->max_recv_bytes)
962 h->cerrno = CSBUFSIZE;
966 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
968 /* move surplus buffer (or everything if we didn't get a BER rec.) */
969 if (hasread > berlen)
971 tomove = req = hasread - berlen;
972 rest = tomove % CS_TCPIP_BUFCHUNK;
974 req += CS_TCPIP_BUFCHUNK - rest;
977 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
982 } else if (sp->altsize < req)
983 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
988 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
989 (unsigned) sp->altbuf));
990 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
992 if (berlen < CS_TCPIP_BUFCHUNK - 1)
993 *(*buf + berlen) = '\0';
994 return berlen ? berlen : 1;
998 #if HAVE_OPENSSL_SSL_H
1000 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
1001 * 0=connection closed.
1003 int ssl_get(COMSTACK h, char **buf, int *bufsize)
1005 tcpip_state *sp = (tcpip_state *)h->cprivate;
1007 int tmpi, berlen, rest, req, tomove;
1008 int hasread = 0, res;
1010 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
1011 if (sp->altlen) /* switch buffers */
1013 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
1014 (unsigned) sp->altbuf));
1018 *bufsize = sp->altsize;
1019 hasread = sp->altlen;
1025 while (!(berlen = (*sp->complete)(*buf, hasread)))
1029 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
1032 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
1033 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
1035 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
1036 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
1039 int ssl_err = SSL_get_error(sp->ssl, res);
1040 if (ssl_err == SSL_ERROR_WANT_READ)
1042 h->io_pending = CS_WANT_READ;
1045 if (ssl_err == SSL_ERROR_WANT_WRITE)
1047 h->io_pending = CS_WANT_WRITE;
1052 h->cerrno = CSERRORSSL;
1057 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1059 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1060 if (hasread > berlen)
1062 tomove = req = hasread - berlen;
1063 rest = tomove % CS_TCPIP_BUFCHUNK;
1065 req += CS_TCPIP_BUFCHUNK - rest;
1068 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1070 } else if (sp->altsize < req)
1071 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1073 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1074 (unsigned) sp->altbuf));
1075 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1077 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1078 *(*buf + berlen) = '\0';
1079 return berlen ? berlen : 1;
1084 * Returns 1, 0 or -1
1085 * In nonblocking mode, you must call again with same buffer while
1086 * return value is 1.
1088 int tcpip_put(COMSTACK h, char *buf, int size)
1091 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1093 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
1096 if (state->towrite < 0)
1098 state->towrite = size;
1101 else if (state->towrite != size)
1103 h->cerrno = CSWRONGBUF;
1106 while (state->towrite > state->written)
1109 send(h->iofile, buf + state->written, size -
1120 WSAGetLastError() == WSAEWOULDBLOCK
1122 yaz_errno() == EWOULDBLOCK
1124 #if EAGAIN != EWOULDBLOCK
1125 || yaz_errno() == EAGAIN
1129 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1131 || yaz_errno() == EINPROGRESS
1135 TRC(fprintf(stderr, " Flow control stop\n"));
1136 h->io_pending = CS_WANT_WRITE;
1139 h->cerrno = CSYSERR;
1142 state->written += res;
1143 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1144 res, state->written, size));
1146 state->towrite = state->written = -1;
1147 TRC(fprintf(stderr, " Ok\n"));
1152 #if HAVE_OPENSSL_SSL_H
1154 * Returns 1, 0 or -1
1155 * In nonblocking mode, you must call again with same buffer while
1156 * return value is 1.
1158 int ssl_put(COMSTACK h, char *buf, int size)
1161 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1163 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1166 if (state->towrite < 0)
1168 state->towrite = size;
1171 else if (state->towrite != size)
1173 h->cerrno = CSWRONGBUF;
1176 while (state->towrite > state->written)
1178 res = SSL_write (state->ssl, buf + state->written,
1179 size - state->written);
1182 int ssl_err = SSL_get_error(state->ssl, res);
1183 if (ssl_err == SSL_ERROR_WANT_READ)
1185 h->io_pending = CS_WANT_READ;
1188 if (ssl_err == SSL_ERROR_WANT_WRITE)
1190 h->io_pending = CS_WANT_WRITE;
1193 h->cerrno = CSERRORSSL;
1196 state->written += res;
1197 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1198 res, state->written, size));
1200 state->towrite = state->written = -1;
1201 TRC(fprintf(stderr, " Ok\n"));
1206 int tcpip_close(COMSTACK h)
1208 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1210 TRC(fprintf(stderr, "tcpip_close\n"));
1211 if (h->iofile != -1)
1213 #if HAVE_OPENSSL_SSL_H
1216 SSL_shutdown (sp->ssl);
1220 closesocket(h->iofile);
1227 #if HAVE_OPENSSL_SSL_H
1230 TRC (fprintf(stderr, "SSL_free\n"));
1235 SSL_CTX_free (sp->ctx_alloc);
1237 #if HAVE_GETADDRINFO
1239 freeaddrinfo(sp->ai);
1241 xfree(sp->connect_request_buf);
1242 xfree(sp->connect_response_buf);
1248 char *tcpip_addrstr(COMSTACK h)
1250 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1251 char *r = 0, *buf = sp->buf;
1253 #if HAVE_GETADDRINFO
1255 struct sockaddr_storage addr;
1256 YAZ_SOCKLEN_T len = sizeof(addr);
1258 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1260 h->cerrno = CSYSERR;
1263 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
1265 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1274 struct sockaddr_in addr;
1275 YAZ_SOCKLEN_T len = sizeof(addr);
1276 struct hostent *host;
1278 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1280 h->cerrno = CSYSERR;
1283 if (!(h->flags & CS_FLAGS_NUMERICHOST))
1285 if ((host = gethostbyaddr((char*)&addr.sin_addr,
1286 sizeof(addr.sin_addr),
1288 r = (char*) host->h_name;
1291 r = inet_ntoa(addr.sin_addr);
1294 if (h->protocol == PROTO_HTTP)
1295 sprintf(buf, "http:%s", r);
1297 sprintf(buf, "tcp:%s", r);
1298 #if HAVE_OPENSSL_SSL_H
1301 if (h->protocol == PROTO_HTTP)
1302 sprintf(buf, "https:%s", r);
1304 sprintf(buf, "ssl:%s", r);
1310 int static tcpip_set_blocking(COMSTACK p, int flags)
1315 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1316 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1319 flag = fcntl(p->iofile, F_GETFL, 0);
1320 if (flags & CS_FLAGS_BLOCKING)
1321 flag = flag & ~O_NONBLOCK; /* blocking */
1324 flag = flag | O_NONBLOCK; /* non-blocking */
1325 signal(SIGPIPE, SIG_IGN);
1327 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1334 #if HAVE_OPENSSL_SSL_H
1335 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1337 struct tcpip_state *sp;
1338 if (!cs || cs->type != ssl_type)
1340 sp = (struct tcpip_state *) cs->cprivate;
1343 sp->ctx = (SSL_CTX *) ctx;
1347 void *cs_get_ssl(COMSTACK cs)
1349 struct tcpip_state *sp;
1350 if (!cs || cs->type != ssl_type)
1352 sp = (struct tcpip_state *) cs->cprivate;
1356 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1358 struct tcpip_state *sp;
1359 if (!cs || cs->type != ssl_type)
1361 sp = (struct tcpip_state *) cs->cprivate;
1362 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1363 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1367 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1369 SSL *ssl = (SSL *) cs_get_ssl(cs);
1372 X509 *server_cert = SSL_get_peer_certificate (ssl);
1375 BIO *bio = BIO_new(BIO_s_mem());
1377 /* get PEM buffer in memory */
1378 PEM_write_bio_X509(bio, server_cert);
1379 *len = BIO_get_mem_data(bio, &pem_buf);
1380 *buf = (char *) xmalloc(*len);
1381 memcpy(*buf, pem_buf, *len);
1389 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1394 void *cs_get_ssl(COMSTACK cs)
1399 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1404 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1411 static int tcpip_put_connect(COMSTACK h, char *buf, int size)
1413 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1415 int r = tcpip_put(h, state->connect_request_buf,
1416 state->connect_request_len);
1420 h->f_put = tcpip_put; /* switch to normal tcpip put */
1421 r = tcpip_put(h, buf, size);
1426 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize)
1428 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1431 r = tcpip_get(h, &state->connect_response_buf,
1432 &state->connect_response_len);
1435 /* got the connect response completely */
1436 state->complete = cs_complete_auto; /* switch to normal tcpip get */
1437 h->f_get = tcpip_get;
1438 return tcpip_get(h, buf, bufsize);
1445 * indent-tabs-mode: nil
1447 * vim: shiftwidth=4 tabstop=8 expandtab