2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.36 2007-10-09 06:00:56 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>
50 #include <sys/select.h>
56 #if HAVE_OPENSSL_SSL_H
57 #include <openssl/ssl.h>
58 #include <openssl/err.h>
61 #include <yaz/comstack.h>
62 #include <yaz/tcpip.h>
65 static int tcpip_close(COMSTACK h);
66 static int tcpip_put(COMSTACK h, char *buf, int size);
67 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
68 static int tcpip_put_connect(COMSTACK h, char *buf, int size);
69 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize);
70 static int tcpip_connect(COMSTACK h, void *address);
71 static int tcpip_more(COMSTACK h);
72 static int tcpip_rcvconnect(COMSTACK h);
73 static int tcpip_bind(COMSTACK h, void *address, int mode);
74 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
75 int (*check_ip)(void *cd, const char *a, int len, int type),
77 static int tcpip_set_blocking(COMSTACK p, int blocking);
79 #if HAVE_OPENSSL_SSL_H
80 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
81 static int ssl_put(COMSTACK h, char *buf, int size);
84 static COMSTACK tcpip_accept(COMSTACK h);
85 static char *tcpip_addrstr(COMSTACK h);
86 static void *tcpip_straddr(COMSTACK h, const char *str);
95 #define YAZ_SOCKLEN_T int
98 /* this state is used for both SSL and straight TCP/IP */
99 typedef struct tcpip_state
101 char *altbuf; /* alternate buffer for surplus data */
102 int altsize; /* size as xmalloced */
103 int altlen; /* length of data or 0 if none */
105 int written; /* -1 if we aren't writing */
106 int towrite; /* to verify against user input */
107 int (*complete)(const char *buf, int len); /* length/complete. */
111 struct sockaddr_in addr; /* returned by cs_straddr */
113 char buf[128]; /* returned by cs_addrstr */
114 #if HAVE_OPENSSL_SSL_H
115 SSL_CTX *ctx; /* current CTX. */
116 SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
118 char cert_fname[256];
120 char *connect_request_buf;
121 int connect_request_len;
122 char *connect_response_buf;
123 int connect_response_len;
127 static int tcpip_init (void)
129 static int initialized = 0;
135 requested = MAKEWORD(1, 1);
136 if (WSAStartup(requested, &wd))
143 static int tcpip_init (void)
150 * This function is always called through the cs_create() macro.
151 * s >= 0: socket has already been established for us.
153 COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
160 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
162 if (!(sp = (struct tcpip_state *)(p->cprivate =
163 xmalloc(sizeof(tcpip_state)))))
170 p->type = tcpip_type;
171 p->protocol = (enum oid_proto) protocol;
173 p->f_connect = tcpip_connect;
174 p->f_rcvconnect = tcpip_rcvconnect;
175 p->f_get = tcpip_get;
176 p->f_put = tcpip_put;
177 p->f_close = tcpip_close;
178 p->f_more = tcpip_more;
179 p->f_bind = tcpip_bind;
180 p->f_listen = tcpip_listen;
181 p->f_accept = tcpip_accept;
182 p->f_addrstr = tcpip_addrstr;
183 p->f_straddr = tcpip_straddr;
184 p->f_set_blocking = tcpip_set_blocking;
185 p->max_recv_bytes = 5000000;
187 p->state = s < 0 ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
193 #if HAVE_OPENSSL_SSL_H
194 sp->ctx = sp->ctx_alloc = 0;
196 strcpy(sp->cert_fname, "yaz.pem");
203 sp->altsize = sp->altlen = 0;
204 sp->towrite = sp->written = -1;
205 if (protocol == PROTO_WAIS)
206 sp->complete = completeWAIS;
208 sp->complete = cs_complete_auto;
210 sp->connect_request_buf = 0;
211 sp->connect_request_len = 0;
212 sp->connect_response_buf = 0;
213 sp->connect_response_len = 0;
215 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
216 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
221 COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
222 const char *connect_host)
224 COMSTACK p = tcpip_type(s, flags, protocol, 0);
229 tcpip_state *sp = (tcpip_state *) p->cprivate;
230 sp->connect_request_buf = xmalloc(strlen(connect_host) + 30);
231 sprintf(sp->connect_request_buf, "CONNECT %s HTTP/1.0\r\n\r\n",
233 sp->connect_request_len = strlen(sp->connect_request_buf);
234 p->f_put = tcpip_put_connect;
235 p->f_get = tcpip_get_connect;
236 sp->complete = cs_complete_auto_head; /* only want HTTP header */
242 #if HAVE_OPENSSL_SSL_H
244 COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
249 p = tcpip_type (s, flags, protocol, 0);
255 sp = (tcpip_state *) p->cprivate;
257 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
259 /* note: we don't handle already opened socket in SSL mode - yet */
265 /* resolve using getaddrinfo */
266 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
268 struct addrinfo hints, *res;
273 hints.ai_family = AF_UNSPEC;
274 hints.ai_socktype = SOCK_STREAM;
275 hints.ai_protocol = 0;
276 hints.ai_addrlen = 0;
277 hints.ai_addr = NULL;
278 hints.ai_canonname = NULL;
279 hints.ai_next = NULL;
281 strncpy(host, str, sizeof(host)-1);
282 host[sizeof(host)-1] = 0;
283 if ((p = strchr(host, '/')))
285 if ((p = strrchr(host, ':')))
291 if (!strcmp("@", host))
293 hints.ai_flags = AI_PASSIVE;
294 error = getaddrinfo(0, port, &hints, &res);
298 error = getaddrinfo(host, port, &hints, &res);
306 /* gethostbyname .. old systems */
307 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
312 short int port = default_port;
314 unsigned long tmpadd;
318 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
319 add->sin_family = AF_INET;
320 strncpy(buf, str, sizeof(buf)-1);
321 buf[sizeof(buf)-1] = 0;
322 if ((p = strchr(buf, '/')))
324 if ((p = strrchr(buf, ':')))
329 add->sin_port = htons(port);
330 if (!strcmp("@", buf))
332 add->sin_addr.s_addr = INADDR_ANY;
334 else if ((tmpadd = inet_addr(buf)) != -1)
336 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
338 else if ((hp = gethostbyname(buf)))
340 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
341 sizeof(struct in_addr));
350 void *tcpip_straddr(COMSTACK h, const char *str)
352 tcpip_state *sp = (tcpip_state *)h->cprivate;
353 const char *port = "210";
354 if (h->protocol == PROTO_HTTP)
360 freeaddrinfo(sp->ai);
361 sp->ai = tcpip_getaddrinfo(str, port);
362 if (sp->ai && h->state == CS_ST_UNBND)
365 struct addrinfo *ai = sp->ai;
366 for (; ai; ai = ai->ai_next)
368 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
376 if (!tcpip_set_blocking(h, h->flags))
382 void *tcpip_straddr(COMSTACK h, const char *str)
384 tcpip_state *sp = (tcpip_state *)h->cprivate;
386 if (h->protocol == PROTO_HTTP)
391 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
393 if (h->state == CS_ST_UNBND)
396 s = socket(AF_INET, SOCK_STREAM, 0);
401 if (!tcpip_set_blocking(h, h->flags))
408 int tcpip_more(COMSTACK h)
410 tcpip_state *sp = (tcpip_state *)h->cprivate;
412 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
416 * connect(2) will block (sometimes) - nothing we can do short of doing
417 * weird things like spawning subprocesses or threading or some weird junk
420 int tcpip_connect(COMSTACK h, void *address)
423 tcpip_state *sp = (tcpip_state *)h->cprivate;
425 struct sockaddr_in *add = (struct sockaddr_in *) address;
430 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
432 TRC(fprintf(stderr, "tcpip_connect\n"));
434 if (h->state != CS_ST_UNBND)
436 h->cerrno = CSOUTSTATE;
440 if (sp->ai != (struct addrinfo *) address)
442 h->cerrno = CSOUTSTATE;
447 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
448 * This gives the connect a chance to negotiate with the other side
451 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
456 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
458 recbuflen *= 10; /* lets be optimistic */
459 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
464 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
469 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
474 r = connect(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
475 freeaddrinfo(sp->ai);
478 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
483 if (WSAGetLastError() == WSAEWOULDBLOCK)
485 h->event = CS_CONNECT;
486 h->state = CS_ST_CONNECTING;
487 h->io_pending = CS_WANT_WRITE;
491 if (yaz_errno() == EINPROGRESS)
493 h->event = CS_CONNECT;
494 h->state = CS_ST_CONNECTING;
495 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
502 h->event = CS_CONNECT;
503 h->state = CS_ST_CONNECTING;
505 return tcpip_rcvconnect (h);
511 int tcpip_rcvconnect(COMSTACK h)
513 #if HAVE_OPENSSL_SSL_H
514 tcpip_state *sp = (tcpip_state *)h->cprivate;
516 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
518 if (h->state == CS_ST_DATAXFER)
520 if (h->state != CS_ST_CONNECTING)
522 h->cerrno = CSOUTSTATE;
525 #if HAVE_OPENSSL_SSL_H
526 if (h->type == ssl_type && !sp->ctx)
529 SSL_load_error_strings();
531 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
534 h->cerrno = CSERRORSSL;
544 sp->ssl = SSL_new (sp->ctx);
545 SSL_set_fd (sp->ssl, h->iofile);
547 res = SSL_connect (sp->ssl);
550 int err = SSL_get_error(sp->ssl, res);
551 if (err == SSL_ERROR_WANT_READ)
553 h->io_pending = CS_WANT_READ;
556 if (err == SSL_ERROR_WANT_WRITE)
558 h->io_pending = CS_WANT_WRITE;
561 h->cerrno = CSERRORSSL;
567 h->state = CS_ST_DATAXFER;
571 #define CERTF "ztest.pem"
572 #define KEYF "ztest.pem"
574 static void tcpip_setsockopt (int fd)
580 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
582 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
584 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
586 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
588 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
590 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
595 static int tcpip_bind(COMSTACK h, void *address, int mode)
598 tcpip_state *sp = (tcpip_state *)h->cprivate;
601 struct sockaddr *addr = (struct sockaddr *)address;
610 if (sp->ai != (struct addrinfo *) address)
612 h->cerrno = CSOUTSTATE;
617 #if HAVE_OPENSSL_SSL_H
618 if (h->type == ssl_type && !sp->ctx)
621 SSL_load_error_strings();
623 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
626 h->cerrno = CSERRORSSL;
635 res = SSL_CTX_use_certificate_chain_file(sp->ctx, sp->cert_fname);
638 ERR_print_errors_fp(stderr);
641 res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
645 ERR_print_errors_fp(stderr);
648 res = SSL_CTX_check_private_key (sp->ctx);
651 ERR_print_errors_fp(stderr);
655 TRC (fprintf (stderr, "ssl_bind\n"));
659 TRC (fprintf (stderr, "tcpip_bind\n"));
662 TRC (fprintf (stderr, "tcpip_bind\n"));
665 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
666 &one, sizeof(one)) < 0)
672 tcpip_setsockopt(h->iofile);
674 r = bind(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
675 freeaddrinfo(sp->ai);
678 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
685 /* Allow a maximum-sized backlog of waiting-to-connect clients */
686 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
691 h->state = CS_ST_IDLE;
692 h->event = CS_LISTEN;
696 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
697 int (*check_ip)(void *cd, const char *a, int len, int t),
700 struct sockaddr_in addr;
701 YAZ_SOCKLEN_T len = sizeof(addr);
703 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
704 if (h->state != CS_ST_IDLE)
706 h->cerrno = CSOUTSTATE;
709 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
714 WSAGetLastError() == WSAEWOULDBLOCK
716 yaz_errno() == EWOULDBLOCK
718 #if EAGAIN != EWOULDBLOCK
719 || yaz_errno() == EAGAIN
724 h->cerrno = CSNODATA;
729 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
730 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
733 if (check_ip && (*check_ip)(cd, (const char *) &addr,
734 sizeof(addr), AF_INET))
738 closesocket(h->newfd);
745 h->state = CS_ST_INCON;
746 tcpip_setsockopt (h->newfd);
750 COMSTACK tcpip_accept(COMSTACK h)
753 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
755 unsigned long tru = 1;
758 TRC(fprintf(stderr, "tcpip_accept\n"));
759 if (h->state == CS_ST_INCON)
761 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
765 closesocket(h->newfd);
772 memcpy(cnew, h, sizeof(*h));
773 cnew->iofile = h->newfd;
774 cnew->io_pending = 0;
775 if (!(state = (tcpip_state *)
776 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
782 closesocket(h->newfd);
790 if (!tcpip_set_blocking(cnew, cnew->flags))
796 closesocket(h->newfd);
808 state->altsize = state->altlen = 0;
809 state->towrite = state->written = -1;
810 state->complete = st->complete;
814 cnew->state = CS_ST_ACCEPT;
815 h->state = CS_ST_IDLE;
817 #if HAVE_OPENSSL_SSL_H
818 state->ctx = st->ctx;
819 state->ctx_alloc = 0;
820 state->ssl = st->ssl;
823 state->ssl = SSL_new (state->ctx);
824 SSL_set_fd (state->ssl, cnew->iofile);
826 state->connect_request_buf = 0;
827 state->connect_response_buf = 0;
831 if (h->state == CS_ST_ACCEPT)
833 #if HAVE_OPENSSL_SSL_H
834 tcpip_state *state = (tcpip_state *)h->cprivate;
837 int res = SSL_accept (state->ssl);
838 TRC(fprintf(stderr, "SSL_accept\n"));
841 int err = SSL_get_error(state->ssl, res);
842 if (err == SSL_ERROR_WANT_READ)
844 h->io_pending = CS_WANT_READ;
847 if (err == SSL_ERROR_WANT_WRITE)
849 h->io_pending = CS_WANT_WRITE;
860 h->cerrno = CSOUTSTATE;
864 h->state = CS_ST_DATAXFER;
869 #define CS_TCPIP_BUFCHUNK 4096
872 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
873 * 0=connection closed.
875 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
877 tcpip_state *sp = (tcpip_state *)h->cprivate;
879 int tmpi, berlen, rest, req, tomove;
880 int hasread = 0, res;
882 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
883 if (sp->altlen) /* switch buffers */
885 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
886 (unsigned) sp->altbuf));
890 *bufsize = sp->altsize;
891 hasread = sp->altlen;
897 while (!(berlen = (*sp->complete)(*buf, hasread)))
901 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
907 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
908 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
915 /* unfortunatly, sun sometimes forgets to set errno in recv
916 when EWOULDBLOCK etc. would be required (res = -1) */
918 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
919 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
922 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
923 strerror(yaz_errno())));
925 if (WSAGetLastError() == WSAEWOULDBLOCK)
927 h->io_pending = CS_WANT_READ;
936 if (yaz_errno() == EWOULDBLOCK
938 #if EAGAIN != EWOULDBLOCK
939 || yaz_errno() == EAGAIN
942 || yaz_errno() == EINPROGRESS
944 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
948 h->io_pending = CS_WANT_READ;
951 else if (yaz_errno() == 0)
963 if (hasread > h->max_recv_bytes)
965 h->cerrno = CSBUFSIZE;
969 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
971 /* move surplus buffer (or everything if we didn't get a BER rec.) */
972 if (hasread > berlen)
974 tomove = req = hasread - berlen;
975 rest = tomove % CS_TCPIP_BUFCHUNK;
977 req += CS_TCPIP_BUFCHUNK - rest;
980 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
985 } else if (sp->altsize < req)
986 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
991 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
992 (unsigned) sp->altbuf));
993 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
995 if (berlen < CS_TCPIP_BUFCHUNK - 1)
996 *(*buf + berlen) = '\0';
997 return berlen ? berlen : 1;
1001 #if HAVE_OPENSSL_SSL_H
1003 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
1004 * 0=connection closed.
1006 int ssl_get(COMSTACK h, char **buf, int *bufsize)
1008 tcpip_state *sp = (tcpip_state *)h->cprivate;
1010 int tmpi, berlen, rest, req, tomove;
1011 int hasread = 0, res;
1013 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
1014 if (sp->altlen) /* switch buffers */
1016 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
1017 (unsigned) sp->altbuf));
1021 *bufsize = sp->altsize;
1022 hasread = sp->altlen;
1028 while (!(berlen = (*sp->complete)(*buf, hasread)))
1032 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
1035 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
1036 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
1038 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
1039 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
1042 int ssl_err = SSL_get_error(sp->ssl, res);
1043 if (ssl_err == SSL_ERROR_WANT_READ)
1045 h->io_pending = CS_WANT_READ;
1048 if (ssl_err == SSL_ERROR_WANT_WRITE)
1050 h->io_pending = CS_WANT_WRITE;
1055 h->cerrno = CSERRORSSL;
1060 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1062 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1063 if (hasread > berlen)
1065 tomove = req = hasread - berlen;
1066 rest = tomove % CS_TCPIP_BUFCHUNK;
1068 req += CS_TCPIP_BUFCHUNK - rest;
1071 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1073 } else if (sp->altsize < req)
1074 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1076 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1077 (unsigned) sp->altbuf));
1078 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1080 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1081 *(*buf + berlen) = '\0';
1082 return berlen ? berlen : 1;
1087 * Returns 1, 0 or -1
1088 * In nonblocking mode, you must call again with same buffer while
1089 * return value is 1.
1091 int tcpip_put(COMSTACK h, char *buf, int size)
1094 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1096 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
1099 if (state->towrite < 0)
1101 state->towrite = size;
1104 else if (state->towrite != size)
1106 h->cerrno = CSWRONGBUF;
1109 while (state->towrite > state->written)
1112 send(h->iofile, buf + state->written, size -
1123 WSAGetLastError() == WSAEWOULDBLOCK
1125 yaz_errno() == EWOULDBLOCK
1127 #if EAGAIN != EWOULDBLOCK
1128 || yaz_errno() == EAGAIN
1132 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1134 || yaz_errno() == EINPROGRESS
1138 TRC(fprintf(stderr, " Flow control stop\n"));
1139 h->io_pending = CS_WANT_WRITE;
1142 h->cerrno = CSYSERR;
1145 state->written += res;
1146 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1147 res, state->written, size));
1149 state->towrite = state->written = -1;
1150 TRC(fprintf(stderr, " Ok\n"));
1155 #if HAVE_OPENSSL_SSL_H
1157 * Returns 1, 0 or -1
1158 * In nonblocking mode, you must call again with same buffer while
1159 * return value is 1.
1161 int ssl_put(COMSTACK h, char *buf, int size)
1164 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1166 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1169 if (state->towrite < 0)
1171 state->towrite = size;
1174 else if (state->towrite != size)
1176 h->cerrno = CSWRONGBUF;
1179 while (state->towrite > state->written)
1181 res = SSL_write (state->ssl, buf + state->written,
1182 size - state->written);
1185 int ssl_err = SSL_get_error(state->ssl, res);
1186 if (ssl_err == SSL_ERROR_WANT_READ)
1188 h->io_pending = CS_WANT_READ;
1191 if (ssl_err == SSL_ERROR_WANT_WRITE)
1193 h->io_pending = CS_WANT_WRITE;
1196 h->cerrno = CSERRORSSL;
1199 state->written += res;
1200 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1201 res, state->written, size));
1203 state->towrite = state->written = -1;
1204 TRC(fprintf(stderr, " Ok\n"));
1209 int tcpip_close(COMSTACK h)
1211 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1213 TRC(fprintf(stderr, "tcpip_close\n"));
1214 if (h->iofile != -1)
1216 #if HAVE_OPENSSL_SSL_H
1219 SSL_shutdown (sp->ssl);
1223 closesocket(h->iofile);
1230 #if HAVE_OPENSSL_SSL_H
1233 TRC (fprintf(stderr, "SSL_free\n"));
1238 SSL_CTX_free (sp->ctx_alloc);
1240 #if HAVE_GETADDRINFO
1242 freeaddrinfo(sp->ai);
1244 xfree(sp->connect_request_buf);
1245 xfree(sp->connect_response_buf);
1251 char *tcpip_addrstr(COMSTACK h)
1253 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1254 char *r = 0, *buf = sp->buf;
1256 #if HAVE_GETADDRINFO
1258 struct sockaddr_storage addr;
1259 YAZ_SOCKLEN_T len = sizeof(addr);
1261 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1263 h->cerrno = CSYSERR;
1266 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
1268 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1277 struct sockaddr_in addr;
1278 YAZ_SOCKLEN_T len = sizeof(addr);
1279 struct hostent *host;
1281 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1283 h->cerrno = CSYSERR;
1286 if (!(h->flags & CS_FLAGS_NUMERICHOST))
1288 if ((host = gethostbyaddr((char*)&addr.sin_addr,
1289 sizeof(addr.sin_addr),
1291 r = (char*) host->h_name;
1294 r = inet_ntoa(addr.sin_addr);
1297 if (h->protocol == PROTO_HTTP)
1298 sprintf(buf, "http:%s", r);
1300 sprintf(buf, "tcp:%s", r);
1301 #if HAVE_OPENSSL_SSL_H
1304 if (h->protocol == PROTO_HTTP)
1305 sprintf(buf, "https:%s", r);
1307 sprintf(buf, "ssl:%s", r);
1313 int static tcpip_set_blocking(COMSTACK p, int flags)
1318 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1319 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1322 flag = fcntl(p->iofile, F_GETFL, 0);
1323 if (flags & CS_FLAGS_BLOCKING)
1324 flag = flag & ~O_NONBLOCK; /* blocking */
1327 flag = flag | O_NONBLOCK; /* non-blocking */
1328 signal(SIGPIPE, SIG_IGN);
1330 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1337 #if HAVE_OPENSSL_SSL_H
1338 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1340 struct tcpip_state *sp;
1341 if (!cs || cs->type != ssl_type)
1343 sp = (struct tcpip_state *) cs->cprivate;
1346 sp->ctx = (SSL_CTX *) ctx;
1350 void *cs_get_ssl(COMSTACK cs)
1352 struct tcpip_state *sp;
1353 if (!cs || cs->type != ssl_type)
1355 sp = (struct tcpip_state *) cs->cprivate;
1359 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1361 struct tcpip_state *sp;
1362 if (!cs || cs->type != ssl_type)
1364 sp = (struct tcpip_state *) cs->cprivate;
1365 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1366 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1370 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1372 SSL *ssl = (SSL *) cs_get_ssl(cs);
1375 X509 *server_cert = SSL_get_peer_certificate (ssl);
1378 BIO *bio = BIO_new(BIO_s_mem());
1380 /* get PEM buffer in memory */
1381 PEM_write_bio_X509(bio, server_cert);
1382 *len = BIO_get_mem_data(bio, &pem_buf);
1383 *buf = (char *) xmalloc(*len);
1384 memcpy(*buf, pem_buf, *len);
1392 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1397 void *cs_get_ssl(COMSTACK cs)
1402 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1407 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1414 static int tcpip_put_connect(COMSTACK h, char *buf, int size)
1416 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1418 int r = tcpip_put(h, state->connect_request_buf,
1419 state->connect_request_len);
1423 h->f_put = tcpip_put; /* switch to normal tcpip put */
1424 r = tcpip_put(h, buf, size);
1429 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize)
1431 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1434 r = tcpip_get(h, &state->connect_response_buf,
1435 &state->connect_response_len);
1438 /* got the connect response completely */
1439 state->complete = cs_complete_auto; /* switch to normal tcpip get */
1440 h->f_get = tcpip_get;
1441 return tcpip_get(h, buf, bufsize);
1448 * indent-tabs-mode: nil
1450 * vim: shiftwidth=4 tabstop=8 expandtab