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;
147 requested = MAKEWORD(1, 1);
148 if (WSAStartup(requested, &wd))
155 static int tcpip_init(void)
163 * This function is always called through the cs_create() macro.
164 * s >= 0: socket has already been established for us.
166 COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
173 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
175 if (!(sp = (struct tcpip_state *)(p->cprivate =
176 xmalloc(sizeof(tcpip_state)))))
183 p->type = tcpip_type;
184 p->protocol = (enum oid_proto) protocol;
186 p->f_connect = tcpip_connect;
187 p->f_rcvconnect = tcpip_rcvconnect;
188 p->f_get = tcpip_get;
189 p->f_put = tcpip_put;
190 p->f_close = tcpip_close;
191 p->f_more = tcpip_more;
192 p->f_bind = tcpip_bind;
193 p->f_listen = tcpip_listen;
194 p->f_accept = tcpip_accept;
195 p->f_addrstr = tcpip_addrstr;
196 p->f_straddr = tcpip_straddr;
197 p->f_set_blocking = tcpip_set_blocking;
198 p->max_recv_bytes = 128 * 1024 * 1024;
200 p->state = s < 0 ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
208 strcpy(sp->cert_fname, "yaz.pem");
215 sp->altsize = sp->altlen = 0;
216 sp->towrite = sp->written = -1;
217 sp->complete = cs_complete_auto;
219 sp->connect_request_buf = 0;
220 sp->connect_request_len = 0;
221 sp->connect_response_buf = 0;
222 sp->connect_response_len = 0;
224 TRC(fprintf(stderr, "Created new TCPIP comstack h=%p\n", p));
229 COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
230 const char *connect_host)
232 COMSTACK p = tcpip_type(s, flags, protocol, 0);
237 tcpip_state *sp = (tcpip_state *) p->cprivate;
238 sp->connect_request_buf = (char *) xmalloc(strlen(connect_host) + 30);
239 sprintf(sp->connect_request_buf, "CONNECT %s HTTP/1.0\r\n\r\n",
241 sp->connect_request_len = strlen(sp->connect_request_buf);
242 p->f_put = tcpip_put_connect;
243 p->f_get = tcpip_get_connect;
244 sp->complete = cs_complete_auto_head; /* only want HTTP header */
250 static void tcpip_create_cred(COMSTACK cs)
252 tcpip_state *sp = (tcpip_state *) cs->cprivate;
253 sp->cred_ptr = (struct tcpip_cred_ptr *) xmalloc(sizeof(*sp->cred_ptr));
254 sp->cred_ptr->ref = 1;
255 gnutls_certificate_allocate_credentials(&sp->cred_ptr->xcred);
260 COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
268 p = tcpip_type(s, flags, protocol, 0);
274 sp = (tcpip_state *) p->cprivate;
277 sp->session = (gnutls_session_t) vp;
279 /* note: we don't handle already opened socket in SSL mode - yet */
285 static int ssl_check_error(COMSTACK h, tcpip_state *sp, int res)
288 TRC(fprintf(stderr, "ssl_check_error error=%d fatal=%d msg=%s\n",
290 gnutls_error_is_fatal(res),
291 gnutls_strerror(res)));
292 if (res == GNUTLS_E_AGAIN || res == GNUTLS_E_INTERRUPTED)
294 int dir = gnutls_record_get_direction(sp->session);
295 TRC(fprintf(stderr, " -> incomplete dir=%d\n", dir));
296 h->io_pending = dir ? CS_WANT_WRITE : CS_WANT_READ;
300 h->cerrno = CSERRORSSL;
306 /* resolve using getaddrinfo */
307 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port,
310 struct addrinfo hints, *res;
315 hints.ai_family = AF_UNSPEC;
316 hints.ai_socktype = SOCK_STREAM;
317 hints.ai_protocol = 0;
318 hints.ai_addrlen = 0;
319 hints.ai_addr = NULL;
320 hints.ai_canonname = NULL;
321 hints.ai_next = NULL;
323 strncpy(host, str, sizeof(host)-1);
324 host[sizeof(host)-1] = 0;
325 if ((p = strchr(host, '/')))
327 if ((p = strrchr(host, ':')))
333 if (!strcmp("@", host))
335 hints.ai_flags = AI_PASSIVE;
336 hints.ai_family = AF_UNSPEC;
337 error = getaddrinfo(0, port, &hints, &res);
340 else if (!strcmp("@4", host))
342 hints.ai_flags = AI_PASSIVE;
343 hints.ai_family = AF_INET;
344 error = getaddrinfo(0, port, &hints, &res);
347 else if (!strcmp("@6", host))
349 hints.ai_flags = AI_PASSIVE;
350 hints.ai_family = AF_INET6;
351 error = getaddrinfo(0, port, &hints, &res);
356 error = getaddrinfo(host, port, &hints, &res);
365 /* gethostbyname .. old systems */
366 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
371 short int port = default_port;
373 unsigned long tmpadd;
377 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
378 add->sin_family = AF_INET;
379 strncpy(buf, str, sizeof(buf)-1);
380 buf[sizeof(buf)-1] = 0;
381 if ((p = strchr(buf, '/')))
383 if ((p = strrchr(buf, ':')))
388 add->sin_port = htons(port);
389 if (!strcmp("@", buf))
391 add->sin_addr.s_addr = INADDR_ANY;
393 else if ((tmpadd = inet_addr(buf)) != -1)
395 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
397 else if ((hp = gethostbyname(buf)))
399 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
400 sizeof(struct in_addr));
408 void *tcpip_straddr(COMSTACK h, const char *str)
410 tcpip_state *sp = (tcpip_state *)h->cprivate;
411 const char *port = "210";
412 struct addrinfo *ai = 0;
414 if (h->protocol == PROTO_HTTP)
416 if (h->type == ssl_type)
425 freeaddrinfo(sp->ai);
426 sp->ai = tcpip_getaddrinfo(str, port, &ipv6_only);
427 if (sp->ai && h->state == CS_ST_UNBND)
430 for (ai = sp->ai; ai; ai = ai->ai_next)
432 if (ai->ai_family == AF_INET6)
434 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
441 for (ai = sp->ai; ai; ai = ai->ai_next)
443 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
452 if (ai->ai_family == AF_INET6 && ipv6_only >= 0 &&
453 setsockopt(h->iofile,
455 IPV6_V6ONLY, &ipv6_only, sizeof(ipv6_only)))
457 if (!tcpip_set_blocking(h, h->flags))
463 void *tcpip_straddr(COMSTACK h, const char *str)
465 tcpip_state *sp = (tcpip_state *)h->cprivate;
467 if (h->protocol == PROTO_HTTP)
469 if (h->type == ssl_type)
477 if (!tcpip_strtoaddr_ex(str, &sp->addr, port))
479 if (h->state == CS_ST_UNBND)
482 s = socket(AF_INET, SOCK_STREAM, 0);
487 if (!tcpip_set_blocking(h, h->flags))
494 int tcpip_more(COMSTACK h)
496 tcpip_state *sp = (tcpip_state *)h->cprivate;
498 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
502 * connect(2) will block (sometimes) - nothing we can do short of doing
503 * weird things like spawning subprocesses or threading or some weird junk
506 int tcpip_connect(COMSTACK h, void *address)
509 struct addrinfo *ai = (struct addrinfo *) address;
510 tcpip_state *sp = (tcpip_state *)h->cprivate;
512 struct sockaddr_in *add = (struct sockaddr_in *) address;
515 TRC(fprintf(stderr, "tcpip_connect h=%p\n", h));
517 if (h->state != CS_ST_UNBND)
519 h->cerrno = CSOUTSTATE;
523 r = connect(h->iofile, ai->ai_addr, ai->ai_addrlen);
524 freeaddrinfo(sp->ai);
527 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
532 if (WSAGetLastError() == WSAEWOULDBLOCK)
534 h->event = CS_CONNECT;
535 h->state = CS_ST_CONNECTING;
536 h->io_pending = CS_WANT_WRITE;
540 if (yaz_errno() == EINPROGRESS)
542 h->event = CS_CONNECT;
543 h->state = CS_ST_CONNECTING;
544 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
551 h->event = CS_CONNECT;
552 h->state = CS_ST_CONNECTING;
554 return tcpip_rcvconnect(h);
560 int tcpip_rcvconnect(COMSTACK h)
563 tcpip_state *sp = (tcpip_state *)h->cprivate;
565 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
567 if (h->state == CS_ST_DATAXFER)
569 if (h->state != CS_ST_CONNECTING)
571 h->cerrno = CSOUTSTATE;
575 if (h->type == ssl_type && !sp->session)
577 tcpip_create_cred(h);
578 gnutls_init(&sp->session, GNUTLS_CLIENT);
579 gnutls_set_default_priority(sp->session);
580 gnutls_credentials_set (sp->session, GNUTLS_CRD_CERTIFICATE,
581 sp->cred_ptr->xcred);
582 /* cast to intermediate size_t to avoid GCC warning. */
583 gnutls_transport_set_ptr(sp->session,
584 (gnutls_transport_ptr_t)
589 int res = gnutls_handshake(sp->session);
592 if (ssl_check_error(h, sp, res))
599 h->state = CS_ST_DATAXFER;
603 #define CERTF "ztest.pem"
604 #define KEYF "ztest.pem"
606 static int tcpip_bind(COMSTACK h, void *address, int mode)
609 tcpip_state *sp = (tcpip_state *)h->cprivate;
611 struct addrinfo *ai = (struct addrinfo *) address;
613 struct sockaddr *addr = (struct sockaddr *)address;
622 if (h->type == ssl_type && !sp->session)
625 gnutls_global_init();
627 tcpip_create_cred(h);
629 res = gnutls_certificate_set_x509_key_file(sp->cred_ptr->xcred,
632 GNUTLS_X509_FMT_PEM);
633 if (res != GNUTLS_E_SUCCESS)
635 h->cerrno = CSERRORSSL;
640 TRC(fprintf(stderr, "tcpip_bind\n"));
643 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
644 &one, sizeof(one)) < 0)
651 r = bind(h->iofile, ai->ai_addr, ai->ai_addrlen);
652 freeaddrinfo(sp->ai);
655 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
662 /* Allow a maximum-sized backlog of waiting-to-connect clients */
663 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
668 h->state = CS_ST_IDLE;
669 h->event = CS_LISTEN;
673 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
674 int (*check_ip)(void *cd, const char *a, int len, int t),
678 /* we don't get peer address on Windows (via accept) */
680 struct sockaddr_in addr;
681 YAZ_SOCKLEN_T len = sizeof(addr);
684 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
685 if (h->state != CS_ST_IDLE)
687 h->cerrno = CSOUTSTATE;
691 h->newfd = accept(h->iofile, 0, 0);
693 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
699 WSAGetLastError() == WSAEWOULDBLOCK
701 yaz_errno() == EWOULDBLOCK
703 #if EAGAIN != EWOULDBLOCK
704 || yaz_errno() == EAGAIN
709 h->cerrno = CSNODATA;
713 shutdown(h->iofile, SD_RECEIVE);
715 shutdown(h->iofile, SHUT_RD);
717 listen(h->iofile, SOMAXCONN);
726 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
727 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
730 if (check_ip && (*check_ip)(cd, (const char *) &addr,
731 sizeof(addr), AF_INET))
735 closesocket(h->newfd);
743 h->state = CS_ST_INCON;
747 COMSTACK tcpip_accept(COMSTACK h)
751 unsigned long tru = 1;
754 TRC(fprintf(stderr, "tcpip_accept h=%p pid=%d\n", h, getpid()));
755 if (h->state == CS_ST_INCON)
757 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
758 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
762 closesocket(h->newfd);
769 memcpy(cnew, h, sizeof(*h));
770 cnew->iofile = h->newfd;
771 cnew->io_pending = 0;
773 if (!(state = (tcpip_state *)
774 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
780 closesocket(h->newfd);
788 if (!tcpip_set_blocking(cnew, cnew->flags))
794 closesocket(h->newfd);
806 state->altsize = state->altlen = 0;
807 state->towrite = state->written = -1;
808 state->complete = st->complete;
812 cnew->state = CS_ST_ACCEPT;
813 h->state = CS_ST_IDLE;
816 state->cred_ptr = st->cred_ptr;
822 (state->cred_ptr->ref)++;
823 gnutls_init(&state->session, GNUTLS_SERVER);
830 res = gnutls_set_default_priority(state->session);
831 if (res != GNUTLS_E_SUCCESS)
837 res = gnutls_credentials_set(state->session,
838 GNUTLS_CRD_CERTIFICATE,
839 st->cred_ptr->xcred);
840 if (res != GNUTLS_E_SUCCESS)
846 /* cast to intermediate size_t to avoid GCC warning. */
847 gnutls_transport_set_ptr(state->session,
848 (gnutls_transport_ptr_t)
849 (size_t) cnew->iofile);
852 state->connect_request_buf = 0;
853 state->connect_response_buf = 0;
856 if (h->state == CS_ST_ACCEPT)
859 tcpip_state *state = (tcpip_state *)h->cprivate;
862 int res = gnutls_handshake(state->session);
865 if (ssl_check_error(h, state, res))
867 TRC(fprintf(stderr, "gnutls_handshake int in tcpip_accept\n"));
870 TRC(fprintf(stderr, "gnutls_handshake failed in tcpip_accept\n"));
874 TRC(fprintf(stderr, "SSL_accept complete. gnutls\n"));
880 h->cerrno = CSOUTSTATE;
884 h->state = CS_ST_DATAXFER;
889 #define CS_TCPIP_BUFCHUNK 4096
892 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
893 * 0=connection closed.
895 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
897 tcpip_state *sp = (tcpip_state *)h->cprivate;
899 int tmpi, berlen, rest, req, tomove;
900 int hasread = 0, res;
902 TRC(fprintf(stderr, "tcpip_get: h=%p bufsize=%d\n", h, *bufsize));
903 if (sp->altlen) /* switch buffers */
905 TRC(fprintf(stderr, " %d bytes in altbuf (%p)\n", sp->altlen,
910 *bufsize = sp->altsize;
911 hasread = sp->altlen;
917 while (!(berlen = (*sp->complete)(*buf, hasread)))
921 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
927 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
928 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
935 /* unfortunatly, sun sometimes forgets to set errno in recv
936 when EWOULDBLOCK etc. would be required (res = -1) */
938 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
939 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
942 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
943 strerror(yaz_errno())));
945 if (WSAGetLastError() == WSAEWOULDBLOCK)
947 h->io_pending = CS_WANT_READ;
956 if (yaz_errno() == EWOULDBLOCK
958 #if EAGAIN != EWOULDBLOCK
959 || yaz_errno() == EAGAIN
962 || yaz_errno() == EINPROGRESS
964 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
968 h->io_pending = CS_WANT_READ;
971 else if (yaz_errno() == 0)
983 if (hasread > h->max_recv_bytes)
985 h->cerrno = CSBUFSIZE;
989 TRC(fprintf(stderr, " Out of read loop with hasread=%d, berlen=%d\n",
991 /* move surplus buffer (or everything if we didn't get a BER rec.) */
992 if (hasread > berlen)
994 tomove = req = hasread - berlen;
995 rest = tomove % CS_TCPIP_BUFCHUNK;
997 req += CS_TCPIP_BUFCHUNK - rest;
1000 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1002 h->cerrno = CSYSERR;
1005 } else if (sp->altsize < req)
1006 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1008 h->cerrno = CSYSERR;
1011 TRC(fprintf(stderr, " Moving %d bytes to altbuf(%p)\n", tomove,
1013 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1015 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1016 *(*buf + berlen) = '\0';
1017 return berlen ? berlen : 1;
1023 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
1024 * 0=connection closed.
1026 int ssl_get(COMSTACK h, char **buf, int *bufsize)
1028 tcpip_state *sp = (tcpip_state *)h->cprivate;
1030 int tmpi, berlen, rest, req, tomove;
1031 int hasread = 0, res;
1033 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
1034 if (sp->altlen) /* switch buffers */
1036 TRC(fprintf(stderr, " %d bytes in altbuf (%p)\n", sp->altlen,
1041 *bufsize = sp->altsize;
1042 hasread = sp->altlen;
1048 while (!(berlen = (*sp->complete)(*buf, hasread)))
1052 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
1055 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
1056 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
1059 res = gnutls_record_recv(sp->session, *buf + hasread,
1063 TRC(fprintf(stderr, "gnutls_record_recv returned 0\n"));
1068 if (ssl_check_error(h, sp, res))
1073 res = SSL_read(sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
1074 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
1077 if (ssl_check_error(h, sp, res))
1084 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1086 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1087 if (hasread > berlen)
1089 tomove = req = hasread - berlen;
1090 rest = tomove % CS_TCPIP_BUFCHUNK;
1092 req += CS_TCPIP_BUFCHUNK - rest;
1095 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1097 } else if (sp->altsize < req)
1098 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1100 TRC(fprintf(stderr, " Moving %d bytes to altbuf(%p)\n", tomove,
1102 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1104 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1105 *(*buf + berlen) = '\0';
1106 return berlen ? berlen : 1;
1111 * Returns 1, 0 or -1
1112 * In nonblocking mode, you must call again with same buffer while
1113 * return value is 1.
1115 int tcpip_put(COMSTACK h, char *buf, int size)
1118 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1120 TRC(fprintf(stderr, "tcpip_put: h=%p size=%d\n", h, size));
1123 if (state->towrite < 0)
1125 state->towrite = size;
1128 else if (state->towrite != size)
1130 h->cerrno = CSWRONGBUF;
1133 while (state->towrite > state->written)
1136 send(h->iofile, buf + state->written, size -
1147 WSAGetLastError() == WSAEWOULDBLOCK
1149 yaz_errno() == EWOULDBLOCK
1151 #if EAGAIN != EWOULDBLOCK
1152 || yaz_errno() == EAGAIN
1156 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1158 || yaz_errno() == EINPROGRESS
1162 TRC(fprintf(stderr, " Flow control stop\n"));
1163 h->io_pending = CS_WANT_WRITE;
1166 h->cerrno = CSYSERR;
1169 state->written += res;
1170 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1171 res, state->written, size));
1173 state->towrite = state->written = -1;
1174 TRC(fprintf(stderr, " Ok\n"));
1181 * Returns 1, 0 or -1
1182 * In nonblocking mode, you must call again with same buffer while
1183 * return value is 1.
1185 int ssl_put(COMSTACK h, char *buf, int size)
1188 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1190 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1193 if (state->towrite < 0)
1195 state->towrite = size;
1198 else if (state->towrite != size)
1200 h->cerrno = CSWRONGBUF;
1203 while (state->towrite > state->written)
1206 res = gnutls_record_send(state->session, buf + state->written,
1207 size - state->written);
1210 if (ssl_check_error(h, state, res))
1215 res = SSL_write(state->ssl, buf + state->written,
1216 size - state->written);
1219 if (ssl_check_error(h, state, res))
1224 state->written += res;
1225 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1226 res, state->written, size));
1228 state->towrite = state->written = -1;
1229 TRC(fprintf(stderr, " Ok\n"));
1234 void tcpip_close(COMSTACK h)
1236 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1238 TRC(fprintf(stderr, "tcpip_close: h=%p pid=%d\n", h, getpid()));
1239 if (h->iofile != -1)
1243 gnutls_bye(sp->session, GNUTLS_SHUT_WR);
1246 closesocket(h->iofile);
1256 gnutls_deinit(sp->session);
1260 assert(sp->cred_ptr->ref > 0);
1262 if (--(sp->cred_ptr->ref) == 0)
1264 TRC(fprintf(stderr, "Removed credentials %p pid=%d\n",
1265 sp->cred_ptr->xcred, getpid()));
1266 gnutls_certificate_free_credentials(sp->cred_ptr->xcred);
1267 xfree(sp->cred_ptr);
1272 #if HAVE_GETADDRINFO
1274 freeaddrinfo(sp->ai);
1276 xfree(sp->connect_request_buf);
1277 xfree(sp->connect_response_buf);
1282 const char *tcpip_addrstr(COMSTACK h)
1284 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1285 char *r = 0, *buf = sp->buf;
1287 #if HAVE_GETADDRINFO
1289 struct sockaddr_storage addr;
1290 YAZ_SOCKLEN_T len = sizeof(addr);
1292 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1294 h->cerrno = CSYSERR;
1297 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
1299 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1308 struct sockaddr_in addr;
1309 YAZ_SOCKLEN_T len = sizeof(addr);
1310 struct hostent *host;
1312 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1314 h->cerrno = CSYSERR;
1317 if (!(h->flags & CS_FLAGS_NUMERICHOST))
1319 if ((host = gethostbyaddr((char*)&addr.sin_addr,
1320 sizeof(addr.sin_addr),
1322 r = (char*) host->h_name;
1325 r = inet_ntoa(addr.sin_addr);
1328 if (h->protocol == PROTO_HTTP)
1329 sprintf(buf, "http:%s", r);
1331 sprintf(buf, "tcp:%s", r);
1335 if (h->protocol == PROTO_HTTP)
1336 sprintf(buf, "https:%s", r);
1338 sprintf(buf, "ssl:%s", r);
1344 static int tcpip_set_blocking(COMSTACK p, int flags)
1349 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1350 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1353 flag = fcntl(p->iofile, F_GETFL, 0);
1354 if (flags & CS_FLAGS_BLOCKING)
1355 flag = flag & ~O_NONBLOCK; /* blocking */
1358 flag = flag | O_NONBLOCK; /* non-blocking */
1359 signal(SIGPIPE, SIG_IGN);
1361 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1370 /* gnutls_x509_crt_print appeared in 1.7.6. Memory leaks were fixed in 1.7.9.
1371 GNUTLS_CRT_PRINT_FULL appeared in 2.4.0. */
1372 #if GNUTLS_VERSION_NUMBER >= 0x020400
1373 #define USE_GNUTLS_X509_CRT_PRINT 1
1375 #define USE_GNUTLS_X509_CRT_PRINT 0
1379 #if USE_GNUTLS_X509_CRT_PRINT
1381 static const char *bin2hex(const void *bin, size_t bin_size)
1383 static char printable[110];
1384 const unsigned char *_bin = bin;
1390 for (i = 0; i < bin_size; i++)
1392 sprintf(print, "%.2x ", _bin[i]);
1398 static void x509_crt_print(gnutls_x509_crt_t cert)
1400 time_t expiration_time, activation_time;
1404 unsigned int algo, bits;
1406 expiration_time = gnutls_x509_crt_get_expiration_time(cert);
1407 activation_time = gnutls_x509_crt_get_activation_time(cert);
1409 printf("\tCertificate is valid since: %s", ctime(&activation_time));
1410 printf("\tCertificate expires: %s", ctime(&expiration_time));
1412 /* Print the serial number of the certificate. */
1413 size = sizeof(serial);
1414 gnutls_x509_crt_get_serial(cert, serial, &size);
1416 printf("\tCertificate serial number: %s\n", bin2hex(serial, size));
1418 /* Extract some of the public key algorithm's parameters
1420 algo = gnutls_x509_crt_get_pk_algorithm(cert, &bits);
1422 printf("Certificate public key: %s", gnutls_pk_algorithm_get_name(algo));
1424 /* Print the version of the X.509 certificate. */
1425 printf("\tCertificate version: #%d\n", gnutls_x509_crt_get_version(cert));
1428 gnutls_x509_crt_get_dn(cert, dn, &size);
1429 printf("\tDN: %s\n", dn);
1432 gnutls_x509_crt_get_issuer_dn(cert, dn, &size);
1433 printf("\tIssuer's DN: %s\n", dn);
1438 void cs_print_session_info(COMSTACK cs)
1441 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
1442 if (cs->type == ssl_type && sp->session)
1444 const gnutls_datum_t *cert_list;
1445 unsigned i, cert_list_size;
1446 if (gnutls_certificate_type_get(sp->session) != GNUTLS_CRT_X509)
1448 printf("X509 certificate\n");
1449 cert_list = gnutls_certificate_get_peers(sp->session,
1451 printf("Peer provided %u certificates\n", cert_list_size);
1452 for (i = 0; i < cert_list_size; i++)
1454 gnutls_x509_crt_t cert;
1455 #if USE_GNUTLS_X509_CRT_PRINT
1457 gnutls_datum_t cinfo;
1459 gnutls_x509_crt_init(&cert);
1460 gnutls_x509_crt_import(cert, &cert_list[i], GNUTLS_X509_FMT_DER);
1461 printf("Certificate info %d:\n", i + 1);
1462 #if USE_GNUTLS_X509_CRT_PRINT
1463 ret = gnutls_x509_crt_print(cert, GNUTLS_CRT_PRINT_FULL,
1467 printf("\t%s\n", cinfo.data);
1468 gnutls_free(cinfo.data);
1471 x509_crt_print(cert);
1473 gnutls_x509_crt_deinit(cert);
1480 void *cs_get_ssl(COMSTACK cs)
1482 /* doesn't do anything for GNUTLS */
1486 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1489 if (cs && cs->type == ssl_type)
1491 /* doesn't do anything for GNUTLS */
1498 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1501 if (cs && cs->type == ssl_type)
1503 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
1504 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1505 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1512 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1516 #if USE_GNUTLS_X509_CRT_PRINT
1517 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
1518 if (cs->type == ssl_type && sp->session)
1520 const gnutls_datum_t *cert_list;
1521 unsigned cert_list_size;
1522 if (gnutls_certificate_type_get(sp->session) != GNUTLS_CRT_X509)
1524 cert_list = gnutls_certificate_get_peers(sp->session, &cert_list_size);
1525 if (cert_list_size > 0)
1527 gnutls_x509_crt_t cert;
1529 gnutls_datum_t cinfo;
1531 gnutls_x509_crt_init(&cert);
1532 gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
1534 ret = gnutls_x509_crt_print(cert, GNUTLS_CRT_PRINT_FULL, &cinfo);
1537 *buf = xstrdup((char *) cinfo.data);
1538 *len = strlen(*buf);
1539 gnutls_free(cinfo.data);
1540 gnutls_x509_crt_deinit(cert);
1543 gnutls_x509_crt_deinit(cert);
1551 static int tcpip_put_connect(COMSTACK h, char *buf, int size)
1553 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1555 int r = tcpip_put(h, state->connect_request_buf,
1556 state->connect_request_len);
1560 h->f_put = tcpip_put; /* switch to normal tcpip put */
1561 r = tcpip_put(h, buf, size);
1566 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize)
1568 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1571 r = tcpip_get(h, &state->connect_response_buf,
1572 &state->connect_response_len);
1575 /* got the connect response completely */
1576 state->complete = cs_complete_auto; /* switch to normal tcpip get */
1577 h->f_get = tcpip_get;
1578 return tcpip_get(h, buf, bufsize);
1585 * c-file-style: "Stroustrup"
1586 * indent-tabs-mode: nil
1588 * vim: shiftwidth=4 tabstop=8 expandtab