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>
52 #include <gnutls/openssl.h>
56 #if HAVE_OPENSSL_SSL_H
57 #include <openssl/ssl.h>
58 #include <openssl/err.h>
62 #include <yaz/comstack.h>
63 #include <yaz/tcpip.h>
66 static int tcpip_close(COMSTACK h);
67 static int tcpip_put(COMSTACK h, char *buf, int size);
68 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
69 static int tcpip_put_connect(COMSTACK h, char *buf, int size);
70 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize);
71 static int tcpip_connect(COMSTACK h, void *address);
72 static int tcpip_more(COMSTACK h);
73 static int tcpip_rcvconnect(COMSTACK h);
74 static int tcpip_bind(COMSTACK h, void *address, int mode);
75 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
76 int (*check_ip)(void *cd, const char *a, int len, int type),
78 static int tcpip_set_blocking(COMSTACK p, int blocking);
81 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
82 static int ssl_put(COMSTACK h, char *buf, int size);
85 static COMSTACK tcpip_accept(COMSTACK h);
86 static char *tcpip_addrstr(COMSTACK h);
87 static void *tcpip_straddr(COMSTACK h, const char *str);
96 #define YAZ_SOCKLEN_T int
99 /* this state is used for both SSL and straight TCP/IP */
100 typedef struct tcpip_state
102 char *altbuf; /* alternate buffer for surplus data */
103 int altsize; /* size as xmalloced */
104 int altlen; /* length of data or 0 if none */
106 int written; /* -1 if we aren't writing */
107 int towrite; /* to verify against user input */
108 int (*complete)(const char *buf, int len); /* length/complete. */
112 struct sockaddr_in addr; /* returned by cs_straddr */
114 char buf[128]; /* returned by cs_addrstr */
116 SSL_CTX *ctx; /* current CTX. */
117 SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
119 char cert_fname[256];
121 char *connect_request_buf;
122 int connect_request_len;
123 char *connect_response_buf;
124 int connect_response_len;
128 static int tcpip_init(void)
130 static int initialized = 0;
136 requested = MAKEWORD(1, 1);
137 if (WSAStartup(requested, &wd))
144 static int tcpip_init(void)
151 * This function is always called through the cs_create() macro.
152 * s >= 0: socket has already been established for us.
154 COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
161 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
163 if (!(sp = (struct tcpip_state *)(p->cprivate =
164 xmalloc(sizeof(tcpip_state)))))
171 p->type = tcpip_type;
172 p->protocol = (enum oid_proto) protocol;
174 p->f_connect = tcpip_connect;
175 p->f_rcvconnect = tcpip_rcvconnect;
176 p->f_get = tcpip_get;
177 p->f_put = tcpip_put;
178 p->f_close = tcpip_close;
179 p->f_more = tcpip_more;
180 p->f_bind = tcpip_bind;
181 p->f_listen = tcpip_listen;
182 p->f_accept = tcpip_accept;
183 p->f_addrstr = tcpip_addrstr;
184 p->f_straddr = tcpip_straddr;
185 p->f_set_blocking = tcpip_set_blocking;
186 p->max_recv_bytes = 5000000;
188 p->state = s < 0 ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
195 sp->ctx = sp->ctx_alloc = 0;
197 strcpy(sp->cert_fname, "yaz.pem");
204 sp->altsize = sp->altlen = 0;
205 sp->towrite = sp->written = -1;
206 if (protocol == PROTO_WAIS)
207 sp->complete = completeWAIS;
209 sp->complete = cs_complete_auto;
211 sp->connect_request_buf = 0;
212 sp->connect_request_len = 0;
213 sp->connect_response_buf = 0;
214 sp->connect_response_len = 0;
216 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
217 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
222 COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
223 const char *connect_host)
225 COMSTACK p = tcpip_type(s, flags, protocol, 0);
230 tcpip_state *sp = (tcpip_state *) p->cprivate;
231 sp->connect_request_buf = (char *) xmalloc(strlen(connect_host) + 30);
232 sprintf(sp->connect_request_buf, "CONNECT %s HTTP/1.0\r\n\r\n",
234 sp->connect_request_len = strlen(sp->connect_request_buf);
235 p->f_put = tcpip_put_connect;
236 p->f_get = tcpip_get_connect;
237 sp->complete = cs_complete_auto_head; /* only want HTTP header */
245 COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
250 p = tcpip_type(s, flags, protocol, 0);
256 sp = (tcpip_state *) p->cprivate;
258 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
260 /* note: we don't handle already opened socket in SSL mode - yet */
264 int ssl_check_error(COMSTACK h, tcpip_state *sp, int res)
266 #if HAVE_OPENSSL_SSL_H
267 int err = SSL_get_error(sp->ssl, res);
268 TRC(fprintf(stderr, "got err=%d\n", err));
269 if (err == SSL_ERROR_WANT_READ)
271 TRC(fprintf(stderr, " -> SSL_ERROR_WANT_READ\n"));
272 h->io_pending = CS_WANT_READ;
275 if (err == SSL_ERROR_WANT_WRITE)
277 TRC(fprintf(stderr, " -> SSL_ERROR_WANT_WRITE\n"));
278 h->io_pending = CS_WANT_WRITE;
282 int tls_error = sp->ssl->last_error;
283 TRC(fprintf(stderr, "ssl_check_error error=%d fatal=%d msg=%s\n",
285 gnutls_error_is_fatal(tls_error),
286 gnutls_strerror(tls_error)));
287 if (tls_error == GNUTLS_E_AGAIN || tls_error == GNUTLS_E_INTERRUPTED)
289 int dir = gnutls_record_get_direction(sp->ssl->gnutls_state);
290 TRC(fprintf(stderr, " -> incomplete dir=%d\n", dir));
291 h->io_pending = dir ? CS_WANT_WRITE : CS_WANT_READ;
295 h->cerrno = CSERRORSSL;
301 /* resolve using getaddrinfo */
302 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
304 struct addrinfo hints, *res;
309 hints.ai_family = AF_UNSPEC;
310 hints.ai_socktype = SOCK_STREAM;
311 hints.ai_protocol = 0;
312 hints.ai_addrlen = 0;
313 hints.ai_addr = NULL;
314 hints.ai_canonname = NULL;
315 hints.ai_next = NULL;
317 strncpy(host, str, sizeof(host)-1);
318 host[sizeof(host)-1] = 0;
319 if ((p = strchr(host, '/')))
321 if ((p = strrchr(host, ':')))
327 if (!strcmp("@", host))
329 hints.ai_flags = AI_PASSIVE;
330 error = getaddrinfo(0, port, &hints, &res);
334 error = getaddrinfo(host, port, &hints, &res);
342 /* gethostbyname .. old systems */
343 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
348 short int port = default_port;
350 unsigned long tmpadd;
354 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
355 add->sin_family = AF_INET;
356 strncpy(buf, str, sizeof(buf)-1);
357 buf[sizeof(buf)-1] = 0;
358 if ((p = strchr(buf, '/')))
360 if ((p = strrchr(buf, ':')))
365 add->sin_port = htons(port);
366 if (!strcmp("@", buf))
368 add->sin_addr.s_addr = INADDR_ANY;
370 else if ((tmpadd = inet_addr(buf)) != -1)
372 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
374 else if ((hp = gethostbyname(buf)))
376 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
377 sizeof(struct in_addr));
386 void *tcpip_straddr(COMSTACK h, const char *str)
388 tcpip_state *sp = (tcpip_state *)h->cprivate;
389 const char *port = "210";
390 if (h->protocol == PROTO_HTTP)
396 freeaddrinfo(sp->ai);
397 sp->ai = tcpip_getaddrinfo(str, port);
398 if (sp->ai && h->state == CS_ST_UNBND)
401 struct addrinfo *ai = sp->ai;
402 for (; ai; ai = ai->ai_next)
404 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
412 if (!tcpip_set_blocking(h, h->flags))
418 void *tcpip_straddr(COMSTACK h, const char *str)
420 tcpip_state *sp = (tcpip_state *)h->cprivate;
422 if (h->protocol == PROTO_HTTP)
427 if (!tcpip_strtoaddr_ex(str, &sp->addr, port))
429 if (h->state == CS_ST_UNBND)
432 s = socket(AF_INET, SOCK_STREAM, 0);
437 if (!tcpip_set_blocking(h, h->flags))
444 int tcpip_more(COMSTACK h)
446 tcpip_state *sp = (tcpip_state *)h->cprivate;
448 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
452 * connect(2) will block (sometimes) - nothing we can do short of doing
453 * weird things like spawning subprocesses or threading or some weird junk
456 int tcpip_connect(COMSTACK h, void *address)
459 tcpip_state *sp = (tcpip_state *)h->cprivate;
461 struct sockaddr_in *add = (struct sockaddr_in *) address;
466 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
468 TRC(fprintf(stderr, "tcpip_connect\n"));
470 if (h->state != CS_ST_UNBND)
472 h->cerrno = CSOUTSTATE;
476 if (sp->ai != (struct addrinfo *) address)
478 h->cerrno = CSOUTSTATE;
483 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
484 * This gives the connect a chance to negotiate with the other side
487 if (getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
492 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
494 recbuflen *= 10; /* lets be optimistic */
495 if (setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
500 if (getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
505 TRC(fprintf(stderr, "New Size of TCP Receive Buffer = %d\n",
510 r = connect(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
511 freeaddrinfo(sp->ai);
514 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
519 if (WSAGetLastError() == WSAEWOULDBLOCK)
521 h->event = CS_CONNECT;
522 h->state = CS_ST_CONNECTING;
523 h->io_pending = CS_WANT_WRITE;
527 if (yaz_errno() == EINPROGRESS)
529 h->event = CS_CONNECT;
530 h->state = CS_ST_CONNECTING;
531 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
538 h->event = CS_CONNECT;
539 h->state = CS_ST_CONNECTING;
541 return tcpip_rcvconnect(h);
547 int tcpip_rcvconnect(COMSTACK h)
550 tcpip_state *sp = (tcpip_state *)h->cprivate;
552 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
554 if (h->state == CS_ST_DATAXFER)
556 if (h->state != CS_ST_CONNECTING)
558 h->cerrno = CSOUTSTATE;
562 if (h->type == ssl_type && !sp->ctx)
565 SSL_load_error_strings();
567 sp->ctx = sp->ctx_alloc = SSL_CTX_new(SSLv23_client_method());
570 h->cerrno = CSERRORSSL;
580 sp->ssl = SSL_new(sp->ctx);
581 SSL_set_fd(sp->ssl, h->iofile);
583 res = SSL_connect(sp->ssl);
586 if (ssl_check_error(h, sp, res))
593 h->state = CS_ST_DATAXFER;
597 #define CERTF "ztest.pem"
598 #define KEYF "ztest.pem"
600 static void tcpip_setsockopt(int fd)
606 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
608 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
610 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
612 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
614 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
616 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
621 static int tcpip_bind(COMSTACK h, void *address, int mode)
624 tcpip_state *sp = (tcpip_state *)h->cprivate;
627 struct sockaddr *addr = (struct sockaddr *)address;
636 if (sp->ai != (struct addrinfo *) address)
638 h->cerrno = CSOUTSTATE;
644 if (h->type == ssl_type && !sp->ctx)
647 SSL_load_error_strings();
649 sp->ctx = sp->ctx_alloc = SSL_CTX_new(SSLv23_server_method());
652 h->cerrno = CSERRORSSL;
661 res = SSL_CTX_use_certificate_file(sp->ctx, sp->cert_fname,
665 #if HAVE_OPENSSL_SSL_H
666 ERR_print_errors_fp(stderr);
668 fprintf(stderr, " SSL_CTX_use_certificate_file %s failed\n",
673 res = SSL_CTX_use_PrivateKey_file(sp->ctx, sp->cert_fname,
677 #if HAVE_OPENSSL_SSL_H
678 ERR_print_errors_fp(stderr);
680 fprintf(stderr, " SSL_CTX_use_certificate_file %s failed\n",
685 #if HAVE_OPENSSL_SSL_H
686 res = SSL_CTX_check_private_key(sp->ctx);
689 ERR_print_errors_fp(stderr);
694 TRC(fprintf(stderr, "ssl_bind\n"));
698 TRC(fprintf(stderr, "tcpip_bind\n"));
701 TRC(fprintf(stderr, "tcpip_bind\n"));
704 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
705 &one, sizeof(one)) < 0)
711 tcpip_setsockopt(h->iofile);
713 r = bind(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
714 freeaddrinfo(sp->ai);
717 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
724 /* Allow a maximum-sized backlog of waiting-to-connect clients */
725 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
730 h->state = CS_ST_IDLE;
731 h->event = CS_LISTEN;
735 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
736 int (*check_ip)(void *cd, const char *a, int len, int t),
739 struct sockaddr_in addr;
740 YAZ_SOCKLEN_T len = sizeof(addr);
742 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
743 if (h->state != CS_ST_IDLE)
745 h->cerrno = CSOUTSTATE;
748 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
753 WSAGetLastError() == WSAEWOULDBLOCK
755 yaz_errno() == EWOULDBLOCK
757 #if EAGAIN != EWOULDBLOCK
758 || yaz_errno() == EAGAIN
763 h->cerrno = CSNODATA;
767 shutdown(h->iofile, SD_RECEIVE);
769 shutdown(h->iofile, SHUT_RD);
771 listen(h->iofile, SOMAXCONN);
776 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
777 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
780 if (check_ip && (*check_ip)(cd, (const char *) &addr,
781 sizeof(addr), AF_INET))
785 closesocket(h->newfd);
792 h->state = CS_ST_INCON;
793 tcpip_setsockopt(h->newfd);
797 COMSTACK tcpip_accept(COMSTACK h)
800 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
802 unsigned long tru = 1;
805 TRC(fprintf(stderr, "tcpip_accept\n"));
806 if (h->state == CS_ST_INCON)
808 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
812 closesocket(h->newfd);
819 memcpy(cnew, h, sizeof(*h));
820 cnew->iofile = h->newfd;
821 cnew->io_pending = 0;
822 if (!(state = (tcpip_state *)
823 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
829 closesocket(h->newfd);
837 if (!tcpip_set_blocking(cnew, cnew->flags))
843 closesocket(h->newfd);
855 state->altsize = state->altlen = 0;
856 state->towrite = state->written = -1;
857 state->complete = st->complete;
861 cnew->state = CS_ST_ACCEPT;
862 h->state = CS_ST_IDLE;
865 state->ctx = st->ctx;
866 state->ctx_alloc = 0;
867 state->ssl = st->ssl;
870 state->ssl = SSL_new(state->ctx);
871 SSL_set_fd(state->ssl, cnew->iofile);
874 state->connect_request_buf = 0;
875 state->connect_response_buf = 0;
878 if (h->state == CS_ST_ACCEPT)
882 tcpip_state *state = (tcpip_state *)h->cprivate;
887 res = SSL_accept(state->ssl);
888 TRC(fprintf(stderr, "SSL_accept res=%d\n", res));
891 if (ssl_check_error(h, state, res))
898 TRC(fprintf(stderr, "SSL_accept complete\n"));
904 h->cerrno = CSOUTSTATE;
908 h->state = CS_ST_DATAXFER;
913 #define CS_TCPIP_BUFCHUNK 4096
916 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
917 * 0=connection closed.
919 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
921 tcpip_state *sp = (tcpip_state *)h->cprivate;
923 int tmpi, berlen, rest, req, tomove;
924 int hasread = 0, res;
926 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
927 if (sp->altlen) /* switch buffers */
929 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
930 (unsigned) sp->altbuf));
934 *bufsize = sp->altsize;
935 hasread = sp->altlen;
941 while (!(berlen = (*sp->complete)(*buf, hasread)))
945 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
951 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
952 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
959 /* unfortunatly, sun sometimes forgets to set errno in recv
960 when EWOULDBLOCK etc. would be required (res = -1) */
962 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
963 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
966 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
967 strerror(yaz_errno())));
969 if (WSAGetLastError() == WSAEWOULDBLOCK)
971 h->io_pending = CS_WANT_READ;
980 if (yaz_errno() == EWOULDBLOCK
982 #if EAGAIN != EWOULDBLOCK
983 || yaz_errno() == EAGAIN
986 || yaz_errno() == EINPROGRESS
988 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
992 h->io_pending = CS_WANT_READ;
995 else if (yaz_errno() == 0)
1007 if (hasread > h->max_recv_bytes)
1009 h->cerrno = CSBUFSIZE;
1013 TRC(fprintf(stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1015 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1016 if (hasread > berlen)
1018 tomove = req = hasread - berlen;
1019 rest = tomove % CS_TCPIP_BUFCHUNK;
1021 req += CS_TCPIP_BUFCHUNK - rest;
1024 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1026 h->cerrno = CSYSERR;
1029 } else if (sp->altsize < req)
1030 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1032 h->cerrno = CSYSERR;
1035 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1036 (unsigned) sp->altbuf));
1037 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1039 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1040 *(*buf + berlen) = '\0';
1041 return berlen ? berlen : 1;
1047 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
1048 * 0=connection closed.
1050 int ssl_get(COMSTACK h, char **buf, int *bufsize)
1052 tcpip_state *sp = (tcpip_state *)h->cprivate;
1054 int tmpi, berlen, rest, req, tomove;
1055 int hasread = 0, res;
1057 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
1058 if (sp->altlen) /* switch buffers */
1060 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
1061 (unsigned) sp->altbuf));
1065 *bufsize = sp->altsize;
1066 hasread = sp->altlen;
1072 while (!(berlen = (*sp->complete)(*buf, hasread)))
1076 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
1079 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
1080 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
1082 res = SSL_read(sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
1083 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
1086 if (ssl_check_error(h, sp, res))
1092 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1094 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1095 if (hasread > berlen)
1097 tomove = req = hasread - berlen;
1098 rest = tomove % CS_TCPIP_BUFCHUNK;
1100 req += CS_TCPIP_BUFCHUNK - rest;
1103 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1105 } else if (sp->altsize < req)
1106 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1108 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1109 (unsigned) sp->altbuf));
1110 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1112 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1113 *(*buf + berlen) = '\0';
1114 return berlen ? berlen : 1;
1119 * Returns 1, 0 or -1
1120 * In nonblocking mode, you must call again with same buffer while
1121 * return value is 1.
1123 int tcpip_put(COMSTACK h, char *buf, int size)
1126 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1128 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
1131 if (state->towrite < 0)
1133 state->towrite = size;
1136 else if (state->towrite != size)
1138 h->cerrno = CSWRONGBUF;
1141 while (state->towrite > state->written)
1144 send(h->iofile, buf + state->written, size -
1155 WSAGetLastError() == WSAEWOULDBLOCK
1157 yaz_errno() == EWOULDBLOCK
1159 #if EAGAIN != EWOULDBLOCK
1160 || yaz_errno() == EAGAIN
1164 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1166 || yaz_errno() == EINPROGRESS
1170 TRC(fprintf(stderr, " Flow control stop\n"));
1171 h->io_pending = CS_WANT_WRITE;
1174 h->cerrno = CSYSERR;
1177 state->written += res;
1178 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1179 res, state->written, size));
1181 state->towrite = state->written = -1;
1182 TRC(fprintf(stderr, " Ok\n"));
1189 * Returns 1, 0 or -1
1190 * In nonblocking mode, you must call again with same buffer while
1191 * return value is 1.
1193 int ssl_put(COMSTACK h, char *buf, int size)
1196 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1198 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1201 if (state->towrite < 0)
1203 state->towrite = size;
1206 else if (state->towrite != size)
1208 h->cerrno = CSWRONGBUF;
1211 while (state->towrite > state->written)
1213 res = SSL_write(state->ssl, buf + state->written,
1214 size - state->written);
1217 if (ssl_check_error(h, state, res))
1221 state->written += res;
1222 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1223 res, state->written, size));
1225 state->towrite = state->written = -1;
1226 TRC(fprintf(stderr, " Ok\n"));
1231 int tcpip_close(COMSTACK h)
1233 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1235 TRC(fprintf(stderr, "tcpip_close\n"));
1236 if (h->iofile != -1)
1241 SSL_shutdown(sp->ssl);
1245 closesocket(h->iofile);
1255 TRC(fprintf(stderr, "SSL_free\n"));
1260 SSL_CTX_free(sp->ctx_alloc);
1262 #if HAVE_GETADDRINFO
1264 freeaddrinfo(sp->ai);
1266 xfree(sp->connect_request_buf);
1267 xfree(sp->connect_response_buf);
1273 char *tcpip_addrstr(COMSTACK h)
1275 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1276 char *r = 0, *buf = sp->buf;
1278 #if HAVE_GETADDRINFO
1280 struct sockaddr_storage addr;
1281 YAZ_SOCKLEN_T len = sizeof(addr);
1283 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1285 h->cerrno = CSYSERR;
1288 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
1290 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1299 struct sockaddr_in addr;
1300 YAZ_SOCKLEN_T len = sizeof(addr);
1301 struct hostent *host;
1303 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1305 h->cerrno = CSYSERR;
1308 if (!(h->flags & CS_FLAGS_NUMERICHOST))
1310 if ((host = gethostbyaddr((char*)&addr.sin_addr,
1311 sizeof(addr.sin_addr),
1313 r = (char*) host->h_name;
1316 r = inet_ntoa(addr.sin_addr);
1319 if (h->protocol == PROTO_HTTP)
1320 sprintf(buf, "http:%s", r);
1322 sprintf(buf, "tcp:%s", r);
1326 if (h->protocol == PROTO_HTTP)
1327 sprintf(buf, "https:%s", r);
1329 sprintf(buf, "ssl:%s", r);
1335 int static tcpip_set_blocking(COMSTACK p, int flags)
1340 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1341 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1344 flag = fcntl(p->iofile, F_GETFL, 0);
1345 if (flags & CS_FLAGS_BLOCKING)
1346 flag = flag & ~O_NONBLOCK; /* blocking */
1349 flag = flag | O_NONBLOCK; /* non-blocking */
1350 signal(SIGPIPE, SIG_IGN);
1352 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1359 void *cs_get_ssl(COMSTACK cs)
1361 #if HAVE_OPENSSL_SSL_H
1362 struct tcpip_state *sp;
1363 if (!cs || cs->type != ssl_type)
1365 sp = (struct tcpip_state *) cs->cprivate;
1373 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1375 struct tcpip_state *sp;
1376 if (!cs || cs->type != ssl_type)
1378 sp = (struct tcpip_state *) cs->cprivate;
1381 sp->ctx = (SSL_CTX *) ctx;
1385 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1387 struct tcpip_state *sp;
1388 if (!cs || cs->type != ssl_type)
1390 sp = (struct tcpip_state *) cs->cprivate;
1391 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1392 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1396 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1398 SSL *ssl = (SSL *) cs_get_ssl(cs);
1401 #if HAVE_OPENSSL_SSL_H
1402 X509 *server_cert = SSL_get_peer_certificate(ssl);
1405 BIO *bio = BIO_new(BIO_s_mem());
1407 /* get PEM buffer in memory */
1408 PEM_write_bio_X509(bio, server_cert);
1409 *len = BIO_get_mem_data(bio, &pem_buf);
1410 *buf = (char *) xmalloc(*len);
1411 memcpy(*buf, pem_buf, *len);
1420 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1425 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1430 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1437 static int tcpip_put_connect(COMSTACK h, char *buf, int size)
1439 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1441 int r = tcpip_put(h, state->connect_request_buf,
1442 state->connect_request_len);
1446 h->f_put = tcpip_put; /* switch to normal tcpip put */
1447 r = tcpip_put(h, buf, size);
1452 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize)
1454 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1457 r = tcpip_get(h, &state->connect_response_buf,
1458 &state->connect_response_len);
1461 /* got the connect response completely */
1462 state->complete = cs_complete_auto; /* switch to normal tcpip get */
1463 h->f_get = tcpip_get;
1464 return tcpip_get(h, buf, bufsize);
1471 * indent-tabs-mode: nil
1473 * vim: shiftwidth=4 tabstop=8 expandtab