1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
7 * \brief Implements TCP/IP + SSL COMSTACK.
21 #include <sys/types.h>
31 /* VS 2003 or later has getaddrinfo; older versions do not */
35 #define HAVE_GETADDRINFO 1
37 #define HAVE_GETADDRINFO 0
42 #include <netinet/in.h>
48 #include <arpa/inet.h>
50 #if HAVE_NETINET_TCP_H
51 #include <netinet/tcp.h>
54 #include <sys/socket.h>
61 #include <gnutls/x509.h>
62 #include <gnutls/gnutls.h>
66 #include <yaz/comstack.h>
67 #include <yaz/tcpip.h>
68 #include <yaz/errno.h>
70 static void tcpip_close(COMSTACK h);
71 static int tcpip_put(COMSTACK h, char *buf, int size);
72 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
73 static int tcpip_put_connect(COMSTACK h, char *buf, int size);
74 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize);
75 static int tcpip_connect(COMSTACK h, void *address);
76 static int tcpip_more(COMSTACK h);
77 static int tcpip_rcvconnect(COMSTACK h);
78 static int tcpip_bind(COMSTACK h, void *address, int mode);
79 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
80 int (*check_ip)(void *cd, const char *a, int len, int type),
82 static int tcpip_set_blocking(COMSTACK p, int blocking);
85 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
86 static int ssl_put(COMSTACK h, char *buf, int size);
89 static COMSTACK tcpip_accept(COMSTACK h);
90 static const char *tcpip_addrstr(COMSTACK h);
91 static void *tcpip_straddr(COMSTACK h, const char *str);
100 #define YAZ_SOCKLEN_T int
104 struct tcpip_cred_ptr {
105 gnutls_certificate_credentials_t xcred;
110 /* this state is used for both SSL and straight TCP/IP */
111 typedef struct tcpip_state
113 char *altbuf; /* alternate buffer for surplus data */
114 int altsize; /* size as xmalloced */
115 int altlen; /* length of data or 0 if none */
117 int written; /* -1 if we aren't writing */
118 int towrite; /* to verify against user input */
119 int (*complete)(const char *buf, int len); /* length/complete. */
123 struct sockaddr_in addr; /* returned by cs_straddr */
125 char buf[128]; /* returned by cs_addrstr */
127 struct tcpip_cred_ptr *cred_ptr;
128 gnutls_session_t session;
129 char cert_fname[256];
131 char *connect_request_buf;
132 int connect_request_len;
133 char *connect_response_buf;
134 int connect_response_len;
138 static int tcpip_init(void)
140 static int initialized = 0;
146 requested = MAKEWORD(1, 1);
147 if (WSAStartup(requested, &wd))
154 static int tcpip_init(void)
161 * This function is always called through the cs_create() macro.
162 * s >= 0: socket has already been established for us.
164 COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
171 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
173 if (!(sp = (struct tcpip_state *)(p->cprivate =
174 xmalloc(sizeof(tcpip_state)))))
181 p->type = tcpip_type;
182 p->protocol = (enum oid_proto) protocol;
184 p->f_connect = tcpip_connect;
185 p->f_rcvconnect = tcpip_rcvconnect;
186 p->f_get = tcpip_get;
187 p->f_put = tcpip_put;
188 p->f_close = tcpip_close;
189 p->f_more = tcpip_more;
190 p->f_bind = tcpip_bind;
191 p->f_listen = tcpip_listen;
192 p->f_accept = tcpip_accept;
193 p->f_addrstr = tcpip_addrstr;
194 p->f_straddr = tcpip_straddr;
195 p->f_set_blocking = tcpip_set_blocking;
196 p->max_recv_bytes = 128 * 1024 * 1024;
198 p->state = s < 0 ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
206 strcpy(sp->cert_fname, "yaz.pem");
213 sp->altsize = sp->altlen = 0;
214 sp->towrite = sp->written = -1;
215 sp->complete = cs_complete_auto;
217 sp->connect_request_buf = 0;
218 sp->connect_request_len = 0;
219 sp->connect_response_buf = 0;
220 sp->connect_response_len = 0;
222 TRC(fprintf(stderr, "Created new TCPIP comstack h=%p\n", p));
227 COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
228 const char *connect_host)
230 COMSTACK p = tcpip_type(s, flags, protocol, 0);
235 tcpip_state *sp = (tcpip_state *) p->cprivate;
236 sp->connect_request_buf = (char *) xmalloc(strlen(connect_host) + 30);
237 sprintf(sp->connect_request_buf, "CONNECT %s HTTP/1.0\r\n\r\n",
239 sp->connect_request_len = strlen(sp->connect_request_buf);
240 p->f_put = tcpip_put_connect;
241 p->f_get = tcpip_get_connect;
242 sp->complete = cs_complete_auto_head; /* only want HTTP header */
248 static void tcpip_create_cred(COMSTACK cs)
250 tcpip_state *sp = (tcpip_state *) cs->cprivate;
251 sp->cred_ptr = (struct tcpip_cred_ptr *) xmalloc(sizeof(*sp->cred_ptr));
252 sp->cred_ptr->ref = 1;
253 gnutls_certificate_allocate_credentials(&sp->cred_ptr->xcred);
258 COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
266 p = tcpip_type(s, flags, protocol, 0);
272 sp = (tcpip_state *) p->cprivate;
275 sp->session = (gnutls_session_t) vp;
277 /* note: we don't handle already opened socket in SSL mode - yet */
283 static int ssl_check_error(COMSTACK h, tcpip_state *sp, int res)
286 TRC(fprintf(stderr, "ssl_check_error error=%d fatal=%d msg=%s\n",
288 gnutls_error_is_fatal(res),
289 gnutls_strerror(res)));
290 if (res == GNUTLS_E_AGAIN || res == GNUTLS_E_INTERRUPTED)
292 int dir = gnutls_record_get_direction(sp->session);
293 TRC(fprintf(stderr, " -> incomplete dir=%d\n", dir));
294 h->io_pending = dir ? CS_WANT_WRITE : CS_WANT_READ;
298 h->cerrno = CSERRORSSL;
304 /* resolve using getaddrinfo */
305 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port,
308 struct addrinfo hints, *res;
313 hints.ai_family = AF_UNSPEC;
314 hints.ai_socktype = SOCK_STREAM;
315 hints.ai_protocol = 0;
316 hints.ai_addrlen = 0;
317 hints.ai_addr = NULL;
318 hints.ai_canonname = NULL;
319 hints.ai_next = NULL;
321 strncpy(host, str, sizeof(host)-1);
322 host[sizeof(host)-1] = 0;
323 if ((p = strchr(host, '/')))
325 if ((p = strrchr(host, ':')))
331 if (!strcmp("@", host))
333 hints.ai_flags = AI_PASSIVE;
334 hints.ai_family = AF_UNSPEC;
335 error = getaddrinfo(0, port, &hints, &res);
338 else if (!strcmp("@4", host))
340 hints.ai_flags = AI_PASSIVE;
341 hints.ai_family = AF_INET;
342 error = getaddrinfo(0, port, &hints, &res);
345 else if (!strcmp("@6", host))
347 hints.ai_flags = AI_PASSIVE;
348 hints.ai_family = AF_INET6;
349 error = getaddrinfo(0, port, &hints, &res);
354 error = getaddrinfo(host, port, &hints, &res);
363 /* gethostbyname .. old systems */
364 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
369 short int port = default_port;
371 unsigned long tmpadd;
375 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
376 add->sin_family = AF_INET;
377 strncpy(buf, str, sizeof(buf)-1);
378 buf[sizeof(buf)-1] = 0;
379 if ((p = strchr(buf, '/')))
381 if ((p = strrchr(buf, ':')))
386 add->sin_port = htons(port);
387 if (!strcmp("@", buf))
389 add->sin_addr.s_addr = INADDR_ANY;
391 else if ((tmpadd = inet_addr(buf)) != -1)
393 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
395 else if ((hp = gethostbyname(buf)))
397 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
398 sizeof(struct in_addr));
406 void *tcpip_straddr(COMSTACK h, const char *str)
408 tcpip_state *sp = (tcpip_state *)h->cprivate;
409 const char *port = "210";
410 struct addrinfo *ai = 0;
412 if (h->protocol == PROTO_HTTP)
414 if (h->type == ssl_type)
423 freeaddrinfo(sp->ai);
424 sp->ai = tcpip_getaddrinfo(str, port, &ipv6_only);
425 if (sp->ai && h->state == CS_ST_UNBND)
428 for (ai = sp->ai; ai; ai = ai->ai_next)
430 if (ai->ai_family == AF_INET6)
432 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
439 for (ai = sp->ai; ai; ai = ai->ai_next)
441 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
450 if (ai->ai_family == AF_INET6 && ipv6_only >= 0 &&
451 setsockopt(h->iofile,
453 IPV6_V6ONLY, &ipv6_only, sizeof(ipv6_only)))
455 if (!tcpip_set_blocking(h, h->flags))
461 void *tcpip_straddr(COMSTACK h, const char *str)
463 tcpip_state *sp = (tcpip_state *)h->cprivate;
465 if (h->protocol == PROTO_HTTP)
467 if (h->type == ssl_type)
475 if (!tcpip_strtoaddr_ex(str, &sp->addr, port))
477 if (h->state == CS_ST_UNBND)
480 s = socket(AF_INET, SOCK_STREAM, 0);
485 if (!tcpip_set_blocking(h, h->flags))
492 int tcpip_more(COMSTACK h)
494 tcpip_state *sp = (tcpip_state *)h->cprivate;
496 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
500 * connect(2) will block (sometimes) - nothing we can do short of doing
501 * weird things like spawning subprocesses or threading or some weird junk
504 int tcpip_connect(COMSTACK h, void *address)
507 struct addrinfo *ai = (struct addrinfo *) address;
508 tcpip_state *sp = (tcpip_state *)h->cprivate;
510 struct sockaddr_in *add = (struct sockaddr_in *) address;
513 TRC(fprintf(stderr, "tcpip_connect h=%p\n", h));
515 if (h->state != CS_ST_UNBND)
517 h->cerrno = CSOUTSTATE;
521 r = connect(h->iofile, ai->ai_addr, ai->ai_addrlen);
522 freeaddrinfo(sp->ai);
525 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
530 if (WSAGetLastError() == WSAEWOULDBLOCK)
532 h->event = CS_CONNECT;
533 h->state = CS_ST_CONNECTING;
534 h->io_pending = CS_WANT_WRITE;
538 if (yaz_errno() == EINPROGRESS)
540 h->event = CS_CONNECT;
541 h->state = CS_ST_CONNECTING;
542 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
549 h->event = CS_CONNECT;
550 h->state = CS_ST_CONNECTING;
552 return tcpip_rcvconnect(h);
558 int tcpip_rcvconnect(COMSTACK h)
561 tcpip_state *sp = (tcpip_state *)h->cprivate;
563 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
565 if (h->state == CS_ST_DATAXFER)
567 if (h->state != CS_ST_CONNECTING)
569 h->cerrno = CSOUTSTATE;
573 if (h->type == ssl_type && !sp->session)
575 gnutls_global_init();
576 tcpip_create_cred(h);
577 gnutls_init(&sp->session, GNUTLS_CLIENT);
578 gnutls_set_default_priority(sp->session);
579 gnutls_credentials_set (sp->session, GNUTLS_CRD_CERTIFICATE,
580 sp->cred_ptr->xcred);
581 /* cast to intermediate size_t to avoid GCC warning. */
582 gnutls_transport_set_ptr(sp->session,
583 (gnutls_transport_ptr_t)
588 int res = gnutls_handshake(sp->session);
591 if (ssl_check_error(h, sp, res))
598 h->state = CS_ST_DATAXFER;
602 #define CERTF "ztest.pem"
603 #define KEYF "ztest.pem"
605 static int tcpip_bind(COMSTACK h, void *address, int mode)
608 tcpip_state *sp = (tcpip_state *)h->cprivate;
610 struct addrinfo *ai = (struct addrinfo *) address;
612 struct sockaddr *addr = (struct sockaddr *)address;
621 if (h->type == ssl_type && !sp->session)
624 gnutls_global_init();
626 tcpip_create_cred(h);
628 res = gnutls_certificate_set_x509_key_file(sp->cred_ptr->xcred,
631 GNUTLS_X509_FMT_PEM);
632 if (res != GNUTLS_E_SUCCESS)
634 h->cerrno = CSERRORSSL;
639 TRC(fprintf(stderr, "tcpip_bind\n"));
642 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
643 &one, sizeof(one)) < 0)
650 r = bind(h->iofile, ai->ai_addr, ai->ai_addrlen);
651 freeaddrinfo(sp->ai);
654 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
661 /* Allow a maximum-sized backlog of waiting-to-connect clients */
662 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
667 h->state = CS_ST_IDLE;
668 h->event = CS_LISTEN;
672 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
673 int (*check_ip)(void *cd, const char *a, int len, int t),
677 /* we don't get peer address on Windows (via accept) */
679 struct sockaddr_in addr;
680 YAZ_SOCKLEN_T len = sizeof(addr);
683 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
684 if (h->state != CS_ST_IDLE)
686 h->cerrno = CSOUTSTATE;
690 h->newfd = accept(h->iofile, 0, 0);
692 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
698 WSAGetLastError() == WSAEWOULDBLOCK
700 yaz_errno() == EWOULDBLOCK
702 #if EAGAIN != EWOULDBLOCK
703 || yaz_errno() == EAGAIN
708 h->cerrno = CSNODATA;
712 shutdown(h->iofile, SD_RECEIVE);
714 shutdown(h->iofile, SHUT_RD);
716 listen(h->iofile, SOMAXCONN);
725 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
726 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
729 if (check_ip && (*check_ip)(cd, (const char *) &addr,
730 sizeof(addr), AF_INET))
734 closesocket(h->newfd);
742 h->state = CS_ST_INCON;
746 COMSTACK tcpip_accept(COMSTACK h)
750 unsigned long tru = 1;
753 TRC(fprintf(stderr, "tcpip_accept h=%p pid=%d\n", h, getpid()));
754 if (h->state == CS_ST_INCON)
756 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
757 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
761 closesocket(h->newfd);
768 memcpy(cnew, h, sizeof(*h));
769 cnew->iofile = h->newfd;
770 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;
815 state->cred_ptr = st->cred_ptr;
821 (state->cred_ptr->ref)++;
822 gnutls_init(&state->session, GNUTLS_SERVER);
829 res = gnutls_set_default_priority(state->session);
830 if (res != GNUTLS_E_SUCCESS)
836 res = gnutls_credentials_set(state->session,
837 GNUTLS_CRD_CERTIFICATE,
838 st->cred_ptr->xcred);
839 if (res != GNUTLS_E_SUCCESS)
845 /* cast to intermediate size_t to avoid GCC warning. */
846 gnutls_transport_set_ptr(state->session,
847 (gnutls_transport_ptr_t)
848 (size_t) cnew->iofile);
851 state->connect_request_buf = 0;
852 state->connect_response_buf = 0;
855 if (h->state == CS_ST_ACCEPT)
858 tcpip_state *state = (tcpip_state *)h->cprivate;
861 int res = gnutls_handshake(state->session);
864 if (ssl_check_error(h, state, res))
866 TRC(fprintf(stderr, "gnutls_handshake int in tcpip_accept\n"));
869 TRC(fprintf(stderr, "gnutls_handshake failed in tcpip_accept\n"));
873 TRC(fprintf(stderr, "SSL_accept complete. gnutls\n"));
879 h->cerrno = CSOUTSTATE;
883 h->state = CS_ST_DATAXFER;
888 #define CS_TCPIP_BUFCHUNK 4096
891 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
892 * 0=connection closed.
894 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
896 tcpip_state *sp = (tcpip_state *)h->cprivate;
898 int tmpi, berlen, rest, req, tomove;
899 int hasread = 0, res;
901 TRC(fprintf(stderr, "tcpip_get: h=%p bufsize=%d\n", h, *bufsize));
902 if (sp->altlen) /* switch buffers */
904 TRC(fprintf(stderr, " %d bytes in altbuf (%p)\n", sp->altlen,
909 *bufsize = sp->altsize;
910 hasread = sp->altlen;
916 while (!(berlen = (*sp->complete)(*buf, hasread)))
920 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
926 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
927 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
934 /* unfortunatly, sun sometimes forgets to set errno in recv
935 when EWOULDBLOCK etc. would be required (res = -1) */
937 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
938 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
941 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
942 strerror(yaz_errno())));
944 if (WSAGetLastError() == WSAEWOULDBLOCK)
946 h->io_pending = CS_WANT_READ;
955 if (yaz_errno() == EWOULDBLOCK
957 #if EAGAIN != EWOULDBLOCK
958 || yaz_errno() == EAGAIN
961 || yaz_errno() == EINPROGRESS
963 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
967 h->io_pending = CS_WANT_READ;
970 else if (yaz_errno() == 0)
982 if (hasread > h->max_recv_bytes)
984 h->cerrno = CSBUFSIZE;
988 TRC(fprintf(stderr, " Out of read loop with hasread=%d, berlen=%d\n",
990 /* move surplus buffer (or everything if we didn't get a BER rec.) */
991 if (hasread > berlen)
993 tomove = req = hasread - berlen;
994 rest = tomove % CS_TCPIP_BUFCHUNK;
996 req += CS_TCPIP_BUFCHUNK - rest;
999 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1001 h->cerrno = CSYSERR;
1004 } else if (sp->altsize < req)
1005 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1007 h->cerrno = CSYSERR;
1010 TRC(fprintf(stderr, " Moving %d bytes to altbuf(%p)\n", tomove,
1012 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1014 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1015 *(*buf + berlen) = '\0';
1016 return berlen ? berlen : 1;
1022 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
1023 * 0=connection closed.
1025 int ssl_get(COMSTACK h, char **buf, int *bufsize)
1027 tcpip_state *sp = (tcpip_state *)h->cprivate;
1029 int tmpi, berlen, rest, req, tomove;
1030 int hasread = 0, res;
1032 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
1033 if (sp->altlen) /* switch buffers */
1035 TRC(fprintf(stderr, " %d bytes in altbuf (%p)\n", sp->altlen,
1040 *bufsize = sp->altsize;
1041 hasread = sp->altlen;
1047 while (!(berlen = (*sp->complete)(*buf, hasread)))
1051 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
1054 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
1055 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
1058 res = gnutls_record_recv(sp->session, *buf + hasread,
1062 TRC(fprintf(stderr, "gnutls_record_recv returned 0\n"));
1067 if (ssl_check_error(h, sp, res))
1072 res = SSL_read(sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
1073 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
1076 if (ssl_check_error(h, sp, res))
1083 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1085 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1086 if (hasread > berlen)
1088 tomove = req = hasread - berlen;
1089 rest = tomove % CS_TCPIP_BUFCHUNK;
1091 req += CS_TCPIP_BUFCHUNK - rest;
1094 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1096 } else if (sp->altsize < req)
1097 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1099 TRC(fprintf(stderr, " Moving %d bytes to altbuf(%p)\n", tomove,
1101 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1103 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1104 *(*buf + berlen) = '\0';
1105 return berlen ? berlen : 1;
1110 * Returns 1, 0 or -1
1111 * In nonblocking mode, you must call again with same buffer while
1112 * return value is 1.
1114 int tcpip_put(COMSTACK h, char *buf, int size)
1117 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1119 TRC(fprintf(stderr, "tcpip_put: h=%p size=%d\n", h, size));
1122 if (state->towrite < 0)
1124 state->towrite = size;
1127 else if (state->towrite != size)
1129 h->cerrno = CSWRONGBUF;
1132 while (state->towrite > state->written)
1135 send(h->iofile, buf + state->written, size -
1146 WSAGetLastError() == WSAEWOULDBLOCK
1148 yaz_errno() == EWOULDBLOCK
1150 #if EAGAIN != EWOULDBLOCK
1151 || yaz_errno() == EAGAIN
1155 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1157 || yaz_errno() == EINPROGRESS
1161 TRC(fprintf(stderr, " Flow control stop\n"));
1162 h->io_pending = CS_WANT_WRITE;
1165 h->cerrno = CSYSERR;
1168 state->written += res;
1169 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1170 res, state->written, size));
1172 state->towrite = state->written = -1;
1173 TRC(fprintf(stderr, " Ok\n"));
1180 * Returns 1, 0 or -1
1181 * In nonblocking mode, you must call again with same buffer while
1182 * return value is 1.
1184 int ssl_put(COMSTACK h, char *buf, int size)
1187 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1189 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1192 if (state->towrite < 0)
1194 state->towrite = size;
1197 else if (state->towrite != size)
1199 h->cerrno = CSWRONGBUF;
1202 while (state->towrite > state->written)
1205 res = gnutls_record_send(state->session, buf + state->written,
1206 size - state->written);
1209 if (ssl_check_error(h, state, res))
1214 res = SSL_write(state->ssl, buf + state->written,
1215 size - state->written);
1218 if (ssl_check_error(h, state, res))
1223 state->written += res;
1224 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1225 res, state->written, size));
1227 state->towrite = state->written = -1;
1228 TRC(fprintf(stderr, " Ok\n"));
1233 void tcpip_close(COMSTACK h)
1235 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1237 TRC(fprintf(stderr, "tcpip_close: h=%p pid=%d\n", h, getpid()));
1238 if (h->iofile != -1)
1242 gnutls_bye(sp->session, GNUTLS_SHUT_WR);
1245 closesocket(h->iofile);
1255 gnutls_deinit(sp->session);
1259 assert(sp->cred_ptr->ref > 0);
1261 if (--(sp->cred_ptr->ref) == 0)
1263 TRC(fprintf(stderr, "Removed credentials %p pid=%d\n",
1264 sp->cred_ptr->xcred, getpid()));
1265 gnutls_certificate_free_credentials(sp->cred_ptr->xcred);
1266 xfree(sp->cred_ptr);
1271 #if HAVE_GETADDRINFO
1273 freeaddrinfo(sp->ai);
1275 xfree(sp->connect_request_buf);
1276 xfree(sp->connect_response_buf);
1281 const char *tcpip_addrstr(COMSTACK h)
1283 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1284 char *r = 0, *buf = sp->buf;
1286 #if HAVE_GETADDRINFO
1288 struct sockaddr_storage addr;
1289 YAZ_SOCKLEN_T len = sizeof(addr);
1291 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1293 h->cerrno = CSYSERR;
1296 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
1298 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1307 struct sockaddr_in addr;
1308 YAZ_SOCKLEN_T len = sizeof(addr);
1309 struct hostent *host;
1311 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1313 h->cerrno = CSYSERR;
1316 if (!(h->flags & CS_FLAGS_NUMERICHOST))
1318 if ((host = gethostbyaddr((char*)&addr.sin_addr,
1319 sizeof(addr.sin_addr),
1321 r = (char*) host->h_name;
1324 r = inet_ntoa(addr.sin_addr);
1327 if (h->protocol == PROTO_HTTP)
1328 sprintf(buf, "http:%s", r);
1330 sprintf(buf, "tcp:%s", r);
1334 if (h->protocol == PROTO_HTTP)
1335 sprintf(buf, "https:%s", r);
1337 sprintf(buf, "ssl:%s", r);
1343 static int tcpip_set_blocking(COMSTACK p, int flags)
1348 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1349 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1352 flag = fcntl(p->iofile, F_GETFL, 0);
1353 if (flags & CS_FLAGS_BLOCKING)
1354 flag = flag & ~O_NONBLOCK; /* blocking */
1357 flag = flag | O_NONBLOCK; /* non-blocking */
1358 signal(SIGPIPE, SIG_IGN);
1360 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1369 /* gnutls_x509_crt_print appeared in 1.7.6. Memory leaks were fixed in 1.7.9.
1370 GNUTLS_CRT_PRINT_FULL appeared in 2.4.0. */
1371 #if GNUTLS_VERSION_NUMBER >= 0x020400
1372 #define USE_GNUTLS_X509_CRT_PRINT 1
1374 #define USE_GNUTLS_X509_CRT_PRINT 0
1378 #if USE_GNUTLS_X509_CRT_PRINT
1380 static const char *bin2hex(const void *bin, size_t bin_size)
1382 static char printable[110];
1383 const unsigned char *_bin = bin;
1389 for (i = 0; i < bin_size; i++)
1391 sprintf(print, "%.2x ", _bin[i]);
1397 static void x509_crt_print(gnutls_x509_crt_t cert)
1399 time_t expiration_time, activation_time;
1403 unsigned int algo, bits;
1405 expiration_time = gnutls_x509_crt_get_expiration_time(cert);
1406 activation_time = gnutls_x509_crt_get_activation_time(cert);
1408 printf("\tCertificate is valid since: %s", ctime(&activation_time));
1409 printf("\tCertificate expires: %s", ctime(&expiration_time));
1411 /* Print the serial number of the certificate. */
1412 size = sizeof(serial);
1413 gnutls_x509_crt_get_serial(cert, serial, &size);
1415 printf("\tCertificate serial number: %s\n", bin2hex(serial, size));
1417 /* Extract some of the public key algorithm's parameters
1419 algo = gnutls_x509_crt_get_pk_algorithm(cert, &bits);
1421 printf("Certificate public key: %s", gnutls_pk_algorithm_get_name(algo));
1423 /* Print the version of the X.509 certificate. */
1424 printf("\tCertificate version: #%d\n", gnutls_x509_crt_get_version(cert));
1427 gnutls_x509_crt_get_dn(cert, dn, &size);
1428 printf("\tDN: %s\n", dn);
1431 gnutls_x509_crt_get_issuer_dn(cert, dn, &size);
1432 printf("\tIssuer's DN: %s\n", dn);
1437 void cs_print_session_info(COMSTACK cs)
1440 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
1441 if (cs->type == ssl_type && sp->session)
1443 const gnutls_datum_t *cert_list;
1444 unsigned i, cert_list_size;
1445 if (gnutls_certificate_type_get(sp->session) != GNUTLS_CRT_X509)
1447 printf("X509 certificate\n");
1448 cert_list = gnutls_certificate_get_peers(sp->session,
1450 printf("Peer provided %u certificates\n", cert_list_size);
1451 for (i = 0; i < cert_list_size; i++)
1453 gnutls_x509_crt_t cert;
1454 #if USE_GNUTLS_X509_CRT_PRINT
1456 gnutls_datum_t cinfo;
1458 gnutls_x509_crt_init(&cert);
1459 gnutls_x509_crt_import(cert, &cert_list[i], GNUTLS_X509_FMT_DER);
1460 printf("Certificate info %d:\n", i + 1);
1461 #if USE_GNUTLS_X509_CRT_PRINT
1462 ret = gnutls_x509_crt_print(cert, GNUTLS_CRT_PRINT_FULL,
1466 printf("\t%s\n", cinfo.data);
1467 gnutls_free(cinfo.data);
1470 x509_crt_print(cert);
1472 gnutls_x509_crt_deinit(cert);
1479 void *cs_get_ssl(COMSTACK cs)
1481 /* doesn't do anything for GNUTLS */
1485 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1488 if (cs && cs->type == ssl_type)
1490 /* doesn't do anything for GNUTLS */
1497 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1500 if (cs && cs->type == ssl_type)
1502 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
1503 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1504 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1511 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1513 /* doesn't do anything for GNUTLS */
1517 static int tcpip_put_connect(COMSTACK h, char *buf, int size)
1519 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1521 int r = tcpip_put(h, state->connect_request_buf,
1522 state->connect_request_len);
1526 h->f_put = tcpip_put; /* switch to normal tcpip put */
1527 r = tcpip_put(h, buf, size);
1532 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize)
1534 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1537 r = tcpip_get(h, &state->connect_response_buf,
1538 &state->connect_response_len);
1541 /* got the connect response completely */
1542 state->complete = cs_complete_auto; /* switch to normal tcpip get */
1543 h->f_get = tcpip_get;
1544 return tcpip_get(h, buf, bufsize);
1551 * c-file-style: "Stroustrup"
1552 * indent-tabs-mode: nil
1554 * vim: shiftwidth=4 tabstop=8 expandtab