1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements TCP/IP + SSL COMSTACK.
17 #include <sys/types.h>
28 /* VS 2003 or later has getaddrinfo; older versions do not */
32 #define HAVE_GETADDRINFO 1
34 #define HAVE_GETADDRINFO 0
38 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <netinet/tcp.h>
45 #include <sys/socket.h>
51 #if HAVE_OPENSSL_SSL_H
52 #include <openssl/ssl.h>
53 #include <openssl/err.h>
56 #include <yaz/comstack.h>
57 #include <yaz/tcpip.h>
60 static int tcpip_close(COMSTACK h);
61 static int tcpip_put(COMSTACK h, char *buf, int size);
62 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
63 static int tcpip_put_connect(COMSTACK h, char *buf, int size);
64 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize);
65 static int tcpip_connect(COMSTACK h, void *address);
66 static int tcpip_more(COMSTACK h);
67 static int tcpip_rcvconnect(COMSTACK h);
68 static int tcpip_bind(COMSTACK h, void *address, int mode);
69 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
70 int (*check_ip)(void *cd, const char *a, int len, int type),
72 static int tcpip_set_blocking(COMSTACK p, int blocking);
74 #if HAVE_OPENSSL_SSL_H
75 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
76 static int ssl_put(COMSTACK h, char *buf, int size);
79 static COMSTACK tcpip_accept(COMSTACK h);
80 static char *tcpip_addrstr(COMSTACK h);
81 static void *tcpip_straddr(COMSTACK h, const char *str);
90 #define YAZ_SOCKLEN_T int
93 /* this state is used for both SSL and straight TCP/IP */
94 typedef struct tcpip_state
96 char *altbuf; /* alternate buffer for surplus data */
97 int altsize; /* size as xmalloced */
98 int altlen; /* length of data or 0 if none */
100 int written; /* -1 if we aren't writing */
101 int towrite; /* to verify against user input */
102 int (*complete)(const char *buf, int len); /* length/complete. */
106 struct sockaddr_in addr; /* returned by cs_straddr */
108 char buf[128]; /* returned by cs_addrstr */
109 #if HAVE_OPENSSL_SSL_H
110 SSL_CTX *ctx; /* current CTX. */
111 SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
113 char cert_fname[256];
115 char *connect_request_buf;
116 int connect_request_len;
117 char *connect_response_buf;
118 int connect_response_len;
122 static int tcpip_init (void)
124 static int initialized = 0;
130 requested = MAKEWORD(1, 1);
131 if (WSAStartup(requested, &wd))
138 static int tcpip_init (void)
145 * This function is always called through the cs_create() macro.
146 * s >= 0: socket has already been established for us.
148 COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
155 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
157 if (!(sp = (struct tcpip_state *)(p->cprivate =
158 xmalloc(sizeof(tcpip_state)))))
165 p->type = tcpip_type;
166 p->protocol = (enum oid_proto) protocol;
168 p->f_connect = tcpip_connect;
169 p->f_rcvconnect = tcpip_rcvconnect;
170 p->f_get = tcpip_get;
171 p->f_put = tcpip_put;
172 p->f_close = tcpip_close;
173 p->f_more = tcpip_more;
174 p->f_bind = tcpip_bind;
175 p->f_listen = tcpip_listen;
176 p->f_accept = tcpip_accept;
177 p->f_addrstr = tcpip_addrstr;
178 p->f_straddr = tcpip_straddr;
179 p->f_set_blocking = tcpip_set_blocking;
180 p->max_recv_bytes = 5000000;
182 p->state = s < 0 ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
188 #if HAVE_OPENSSL_SSL_H
189 sp->ctx = sp->ctx_alloc = 0;
191 strcpy(sp->cert_fname, "yaz.pem");
198 sp->altsize = sp->altlen = 0;
199 sp->towrite = sp->written = -1;
200 if (protocol == PROTO_WAIS)
201 sp->complete = completeWAIS;
203 sp->complete = cs_complete_auto;
205 sp->connect_request_buf = 0;
206 sp->connect_request_len = 0;
207 sp->connect_response_buf = 0;
208 sp->connect_response_len = 0;
210 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
211 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
216 COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
217 const char *connect_host)
219 COMSTACK p = tcpip_type(s, flags, protocol, 0);
224 tcpip_state *sp = (tcpip_state *) p->cprivate;
225 sp->connect_request_buf = (char *) xmalloc(strlen(connect_host) + 30);
226 sprintf(sp->connect_request_buf, "CONNECT %s HTTP/1.0\r\n\r\n",
228 sp->connect_request_len = strlen(sp->connect_request_buf);
229 p->f_put = tcpip_put_connect;
230 p->f_get = tcpip_get_connect;
231 sp->complete = cs_complete_auto_head; /* only want HTTP header */
237 #if HAVE_OPENSSL_SSL_H
239 COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
244 p = tcpip_type (s, flags, protocol, 0);
250 sp = (tcpip_state *) p->cprivate;
252 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
254 /* note: we don't handle already opened socket in SSL mode - yet */
260 /* resolve using getaddrinfo */
261 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
263 struct addrinfo hints, *res;
268 hints.ai_family = AF_UNSPEC;
269 hints.ai_socktype = SOCK_STREAM;
270 hints.ai_protocol = 0;
271 hints.ai_addrlen = 0;
272 hints.ai_addr = NULL;
273 hints.ai_canonname = NULL;
274 hints.ai_next = NULL;
276 strncpy(host, str, sizeof(host)-1);
277 host[sizeof(host)-1] = 0;
278 if ((p = strchr(host, '/')))
280 if ((p = strrchr(host, ':')))
286 if (!strcmp("@", host))
288 hints.ai_flags = AI_PASSIVE;
289 error = getaddrinfo(0, port, &hints, &res);
293 error = getaddrinfo(host, port, &hints, &res);
301 /* gethostbyname .. old systems */
302 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
307 short int port = default_port;
309 unsigned long tmpadd;
313 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
314 add->sin_family = AF_INET;
315 strncpy(buf, str, sizeof(buf)-1);
316 buf[sizeof(buf)-1] = 0;
317 if ((p = strchr(buf, '/')))
319 if ((p = strrchr(buf, ':')))
324 add->sin_port = htons(port);
325 if (!strcmp("@", buf))
327 add->sin_addr.s_addr = INADDR_ANY;
329 else if ((tmpadd = inet_addr(buf)) != -1)
331 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
333 else if ((hp = gethostbyname(buf)))
335 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
336 sizeof(struct in_addr));
345 void *tcpip_straddr(COMSTACK h, const char *str)
347 tcpip_state *sp = (tcpip_state *)h->cprivate;
348 const char *port = "210";
349 if (h->protocol == PROTO_HTTP)
355 freeaddrinfo(sp->ai);
356 sp->ai = tcpip_getaddrinfo(str, port);
357 if (sp->ai && h->state == CS_ST_UNBND)
360 struct addrinfo *ai = sp->ai;
361 for (; ai; ai = ai->ai_next)
363 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
371 if (!tcpip_set_blocking(h, h->flags))
377 void *tcpip_straddr(COMSTACK h, const char *str)
379 tcpip_state *sp = (tcpip_state *)h->cprivate;
381 if (h->protocol == PROTO_HTTP)
386 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
388 if (h->state == CS_ST_UNBND)
391 s = socket(AF_INET, SOCK_STREAM, 0);
396 if (!tcpip_set_blocking(h, h->flags))
403 int tcpip_more(COMSTACK h)
405 tcpip_state *sp = (tcpip_state *)h->cprivate;
407 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
411 * connect(2) will block (sometimes) - nothing we can do short of doing
412 * weird things like spawning subprocesses or threading or some weird junk
415 int tcpip_connect(COMSTACK h, void *address)
418 tcpip_state *sp = (tcpip_state *)h->cprivate;
420 struct sockaddr_in *add = (struct sockaddr_in *) address;
425 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
427 TRC(fprintf(stderr, "tcpip_connect\n"));
429 if (h->state != CS_ST_UNBND)
431 h->cerrno = CSOUTSTATE;
435 if (sp->ai != (struct addrinfo *) address)
437 h->cerrno = CSOUTSTATE;
442 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
443 * This gives the connect a chance to negotiate with the other side
446 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
451 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
453 recbuflen *= 10; /* lets be optimistic */
454 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
459 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
464 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
469 r = connect(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
470 freeaddrinfo(sp->ai);
473 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
478 if (WSAGetLastError() == WSAEWOULDBLOCK)
480 h->event = CS_CONNECT;
481 h->state = CS_ST_CONNECTING;
482 h->io_pending = CS_WANT_WRITE;
486 if (yaz_errno() == EINPROGRESS)
488 h->event = CS_CONNECT;
489 h->state = CS_ST_CONNECTING;
490 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
497 h->event = CS_CONNECT;
498 h->state = CS_ST_CONNECTING;
500 return tcpip_rcvconnect (h);
506 int tcpip_rcvconnect(COMSTACK h)
508 #if HAVE_OPENSSL_SSL_H
509 tcpip_state *sp = (tcpip_state *)h->cprivate;
511 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
513 if (h->state == CS_ST_DATAXFER)
515 if (h->state != CS_ST_CONNECTING)
517 h->cerrno = CSOUTSTATE;
520 #if HAVE_OPENSSL_SSL_H
521 if (h->type == ssl_type && !sp->ctx)
524 SSL_load_error_strings();
526 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
529 h->cerrno = CSERRORSSL;
539 sp->ssl = SSL_new (sp->ctx);
540 SSL_set_fd (sp->ssl, h->iofile);
542 res = SSL_connect (sp->ssl);
545 int err = SSL_get_error(sp->ssl, res);
546 if (err == SSL_ERROR_WANT_READ)
548 h->io_pending = CS_WANT_READ;
551 if (err == SSL_ERROR_WANT_WRITE)
553 h->io_pending = CS_WANT_WRITE;
556 h->cerrno = CSERRORSSL;
562 h->state = CS_ST_DATAXFER;
566 #define CERTF "ztest.pem"
567 #define KEYF "ztest.pem"
569 static void tcpip_setsockopt (int fd)
575 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
577 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
579 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
581 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
583 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
585 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
590 static int tcpip_bind(COMSTACK h, void *address, int mode)
593 tcpip_state *sp = (tcpip_state *)h->cprivate;
596 struct sockaddr *addr = (struct sockaddr *)address;
605 if (sp->ai != (struct addrinfo *) address)
607 h->cerrno = CSOUTSTATE;
612 #if HAVE_OPENSSL_SSL_H
613 if (h->type == ssl_type && !sp->ctx)
616 SSL_load_error_strings();
618 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
621 h->cerrno = CSERRORSSL;
630 res = SSL_CTX_use_certificate_chain_file(sp->ctx, sp->cert_fname);
633 ERR_print_errors_fp(stderr);
636 res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
640 ERR_print_errors_fp(stderr);
643 res = SSL_CTX_check_private_key (sp->ctx);
646 ERR_print_errors_fp(stderr);
650 TRC (fprintf (stderr, "ssl_bind\n"));
654 TRC (fprintf (stderr, "tcpip_bind\n"));
657 TRC (fprintf (stderr, "tcpip_bind\n"));
660 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
661 &one, sizeof(one)) < 0)
667 tcpip_setsockopt(h->iofile);
669 r = bind(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
670 freeaddrinfo(sp->ai);
673 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
680 /* Allow a maximum-sized backlog of waiting-to-connect clients */
681 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
686 h->state = CS_ST_IDLE;
687 h->event = CS_LISTEN;
691 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
692 int (*check_ip)(void *cd, const char *a, int len, int t),
695 struct sockaddr_in addr;
696 YAZ_SOCKLEN_T len = sizeof(addr);
698 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
699 if (h->state != CS_ST_IDLE)
701 h->cerrno = CSOUTSTATE;
704 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
709 WSAGetLastError() == WSAEWOULDBLOCK
711 yaz_errno() == EWOULDBLOCK
713 #if EAGAIN != EWOULDBLOCK
714 || yaz_errno() == EAGAIN
719 h->cerrno = CSNODATA;
723 shutdown(h->iofile, SD_RECEIVE);
725 shutdown(h->iofile, SHUT_RD);
727 listen(h->iofile, SOMAXCONN);
732 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
733 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
736 if (check_ip && (*check_ip)(cd, (const char *) &addr,
737 sizeof(addr), AF_INET))
741 closesocket(h->newfd);
748 h->state = CS_ST_INCON;
749 tcpip_setsockopt (h->newfd);
753 COMSTACK tcpip_accept(COMSTACK h)
756 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
758 unsigned long tru = 1;
761 TRC(fprintf(stderr, "tcpip_accept\n"));
762 if (h->state == CS_ST_INCON)
764 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
768 closesocket(h->newfd);
775 memcpy(cnew, h, sizeof(*h));
776 cnew->iofile = h->newfd;
777 cnew->io_pending = 0;
778 if (!(state = (tcpip_state *)
779 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
785 closesocket(h->newfd);
793 if (!tcpip_set_blocking(cnew, cnew->flags))
799 closesocket(h->newfd);
811 state->altsize = state->altlen = 0;
812 state->towrite = state->written = -1;
813 state->complete = st->complete;
817 cnew->state = CS_ST_ACCEPT;
818 h->state = CS_ST_IDLE;
820 #if HAVE_OPENSSL_SSL_H
821 state->ctx = st->ctx;
822 state->ctx_alloc = 0;
823 state->ssl = st->ssl;
826 state->ssl = SSL_new (state->ctx);
827 SSL_set_fd (state->ssl, cnew->iofile);
830 state->connect_request_buf = 0;
831 state->connect_response_buf = 0;
834 if (h->state == CS_ST_ACCEPT)
836 #if HAVE_OPENSSL_SSL_H
837 tcpip_state *state = (tcpip_state *)h->cprivate;
840 int res = SSL_accept (state->ssl);
841 TRC(fprintf(stderr, "SSL_accept\n"));
844 int err = SSL_get_error(state->ssl, res);
845 if (err == SSL_ERROR_WANT_READ)
847 h->io_pending = CS_WANT_READ;
850 if (err == SSL_ERROR_WANT_WRITE)
852 h->io_pending = CS_WANT_WRITE;
863 h->cerrno = CSOUTSTATE;
867 h->state = CS_ST_DATAXFER;
872 #define CS_TCPIP_BUFCHUNK 4096
875 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
876 * 0=connection closed.
878 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
880 tcpip_state *sp = (tcpip_state *)h->cprivate;
882 int tmpi, berlen, rest, req, tomove;
883 int hasread = 0, res;
885 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
886 if (sp->altlen) /* switch buffers */
888 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
889 (unsigned) sp->altbuf));
893 *bufsize = sp->altsize;
894 hasread = sp->altlen;
900 while (!(berlen = (*sp->complete)(*buf, hasread)))
904 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
910 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
911 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
918 /* unfortunatly, sun sometimes forgets to set errno in recv
919 when EWOULDBLOCK etc. would be required (res = -1) */
921 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
922 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
925 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
926 strerror(yaz_errno())));
928 if (WSAGetLastError() == WSAEWOULDBLOCK)
930 h->io_pending = CS_WANT_READ;
939 if (yaz_errno() == EWOULDBLOCK
941 #if EAGAIN != EWOULDBLOCK
942 || yaz_errno() == EAGAIN
945 || yaz_errno() == EINPROGRESS
947 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
951 h->io_pending = CS_WANT_READ;
954 else if (yaz_errno() == 0)
966 if (hasread > h->max_recv_bytes)
968 h->cerrno = CSBUFSIZE;
972 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
974 /* move surplus buffer (or everything if we didn't get a BER rec.) */
975 if (hasread > berlen)
977 tomove = req = hasread - berlen;
978 rest = tomove % CS_TCPIP_BUFCHUNK;
980 req += CS_TCPIP_BUFCHUNK - rest;
983 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
988 } else if (sp->altsize < req)
989 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
994 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
995 (unsigned) sp->altbuf));
996 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
998 if (berlen < CS_TCPIP_BUFCHUNK - 1)
999 *(*buf + berlen) = '\0';
1000 return berlen ? berlen : 1;
1004 #if HAVE_OPENSSL_SSL_H
1006 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
1007 * 0=connection closed.
1009 int ssl_get(COMSTACK h, char **buf, int *bufsize)
1011 tcpip_state *sp = (tcpip_state *)h->cprivate;
1013 int tmpi, berlen, rest, req, tomove;
1014 int hasread = 0, res;
1016 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
1017 if (sp->altlen) /* switch buffers */
1019 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
1020 (unsigned) sp->altbuf));
1024 *bufsize = sp->altsize;
1025 hasread = sp->altlen;
1031 while (!(berlen = (*sp->complete)(*buf, hasread)))
1035 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
1038 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
1039 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
1041 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
1042 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
1045 int ssl_err = SSL_get_error(sp->ssl, res);
1046 if (ssl_err == SSL_ERROR_WANT_READ)
1048 h->io_pending = CS_WANT_READ;
1051 if (ssl_err == SSL_ERROR_WANT_WRITE)
1053 h->io_pending = CS_WANT_WRITE;
1058 h->cerrno = CSERRORSSL;
1063 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1065 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1066 if (hasread > berlen)
1068 tomove = req = hasread - berlen;
1069 rest = tomove % CS_TCPIP_BUFCHUNK;
1071 req += CS_TCPIP_BUFCHUNK - rest;
1074 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1076 } else if (sp->altsize < req)
1077 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1079 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1080 (unsigned) sp->altbuf));
1081 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1083 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1084 *(*buf + berlen) = '\0';
1085 return berlen ? berlen : 1;
1090 * Returns 1, 0 or -1
1091 * In nonblocking mode, you must call again with same buffer while
1092 * return value is 1.
1094 int tcpip_put(COMSTACK h, char *buf, int size)
1097 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1099 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
1102 if (state->towrite < 0)
1104 state->towrite = size;
1107 else if (state->towrite != size)
1109 h->cerrno = CSWRONGBUF;
1112 while (state->towrite > state->written)
1115 send(h->iofile, buf + state->written, size -
1126 WSAGetLastError() == WSAEWOULDBLOCK
1128 yaz_errno() == EWOULDBLOCK
1130 #if EAGAIN != EWOULDBLOCK
1131 || yaz_errno() == EAGAIN
1135 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1137 || yaz_errno() == EINPROGRESS
1141 TRC(fprintf(stderr, " Flow control stop\n"));
1142 h->io_pending = CS_WANT_WRITE;
1145 h->cerrno = CSYSERR;
1148 state->written += res;
1149 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1150 res, state->written, size));
1152 state->towrite = state->written = -1;
1153 TRC(fprintf(stderr, " Ok\n"));
1158 #if HAVE_OPENSSL_SSL_H
1160 * Returns 1, 0 or -1
1161 * In nonblocking mode, you must call again with same buffer while
1162 * return value is 1.
1164 int ssl_put(COMSTACK h, char *buf, int size)
1167 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1169 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1172 if (state->towrite < 0)
1174 state->towrite = size;
1177 else if (state->towrite != size)
1179 h->cerrno = CSWRONGBUF;
1182 while (state->towrite > state->written)
1184 res = SSL_write (state->ssl, buf + state->written,
1185 size - state->written);
1188 int ssl_err = SSL_get_error(state->ssl, res);
1189 if (ssl_err == SSL_ERROR_WANT_READ)
1191 h->io_pending = CS_WANT_READ;
1194 if (ssl_err == SSL_ERROR_WANT_WRITE)
1196 h->io_pending = CS_WANT_WRITE;
1199 h->cerrno = CSERRORSSL;
1202 state->written += res;
1203 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1204 res, state->written, size));
1206 state->towrite = state->written = -1;
1207 TRC(fprintf(stderr, " Ok\n"));
1212 int tcpip_close(COMSTACK h)
1214 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1216 TRC(fprintf(stderr, "tcpip_close\n"));
1217 if (h->iofile != -1)
1219 #if HAVE_OPENSSL_SSL_H
1222 SSL_shutdown (sp->ssl);
1226 closesocket(h->iofile);
1233 #if HAVE_OPENSSL_SSL_H
1236 TRC (fprintf(stderr, "SSL_free\n"));
1241 SSL_CTX_free (sp->ctx_alloc);
1243 #if HAVE_GETADDRINFO
1245 freeaddrinfo(sp->ai);
1247 xfree(sp->connect_request_buf);
1248 xfree(sp->connect_response_buf);
1254 char *tcpip_addrstr(COMSTACK h)
1256 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1257 char *r = 0, *buf = sp->buf;
1259 #if HAVE_GETADDRINFO
1261 struct sockaddr_storage addr;
1262 YAZ_SOCKLEN_T len = sizeof(addr);
1264 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1266 h->cerrno = CSYSERR;
1269 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
1271 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1280 struct sockaddr_in addr;
1281 YAZ_SOCKLEN_T len = sizeof(addr);
1282 struct hostent *host;
1284 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1286 h->cerrno = CSYSERR;
1289 if (!(h->flags & CS_FLAGS_NUMERICHOST))
1291 if ((host = gethostbyaddr((char*)&addr.sin_addr,
1292 sizeof(addr.sin_addr),
1294 r = (char*) host->h_name;
1297 r = inet_ntoa(addr.sin_addr);
1300 if (h->protocol == PROTO_HTTP)
1301 sprintf(buf, "http:%s", r);
1303 sprintf(buf, "tcp:%s", r);
1304 #if HAVE_OPENSSL_SSL_H
1307 if (h->protocol == PROTO_HTTP)
1308 sprintf(buf, "https:%s", r);
1310 sprintf(buf, "ssl:%s", r);
1316 int static tcpip_set_blocking(COMSTACK p, int flags)
1321 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1322 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1325 flag = fcntl(p->iofile, F_GETFL, 0);
1326 if (flags & CS_FLAGS_BLOCKING)
1327 flag = flag & ~O_NONBLOCK; /* blocking */
1330 flag = flag | O_NONBLOCK; /* non-blocking */
1331 signal(SIGPIPE, SIG_IGN);
1333 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1340 #if HAVE_OPENSSL_SSL_H
1341 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1343 struct tcpip_state *sp;
1344 if (!cs || cs->type != ssl_type)
1346 sp = (struct tcpip_state *) cs->cprivate;
1349 sp->ctx = (SSL_CTX *) ctx;
1353 void *cs_get_ssl(COMSTACK cs)
1355 struct tcpip_state *sp;
1356 if (!cs || cs->type != ssl_type)
1358 sp = (struct tcpip_state *) cs->cprivate;
1362 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1364 struct tcpip_state *sp;
1365 if (!cs || cs->type != ssl_type)
1367 sp = (struct tcpip_state *) cs->cprivate;
1368 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1369 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1373 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1375 SSL *ssl = (SSL *) cs_get_ssl(cs);
1378 X509 *server_cert = SSL_get_peer_certificate (ssl);
1381 BIO *bio = BIO_new(BIO_s_mem());
1383 /* get PEM buffer in memory */
1384 PEM_write_bio_X509(bio, server_cert);
1385 *len = BIO_get_mem_data(bio, &pem_buf);
1386 *buf = (char *) xmalloc(*len);
1387 memcpy(*buf, pem_buf, *len);
1395 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1400 void *cs_get_ssl(COMSTACK cs)
1405 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1410 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1417 static int tcpip_put_connect(COMSTACK h, char *buf, int size)
1419 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1421 int r = tcpip_put(h, state->connect_request_buf,
1422 state->connect_request_len);
1426 h->f_put = tcpip_put; /* switch to normal tcpip put */
1427 r = tcpip_put(h, buf, size);
1432 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize)
1434 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1437 r = tcpip_get(h, &state->connect_response_buf,
1438 &state->connect_response_len);
1441 /* got the connect response completely */
1442 state->complete = cs_complete_auto; /* switch to normal tcpip get */
1443 h->f_get = tcpip_get;
1444 return tcpip_get(h, buf, bufsize);
1451 * indent-tabs-mode: nil
1453 * vim: shiftwidth=4 tabstop=8 expandtab