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>
65 #include <yaz/comstack.h>
66 #include <yaz/tcpip.h>
67 #include <yaz/errno.h>
69 static void tcpip_close(COMSTACK h);
70 static int tcpip_put(COMSTACK h, char *buf, int size);
71 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
72 static int tcpip_put_connect(COMSTACK h, char *buf, int size);
73 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize);
74 static int tcpip_connect(COMSTACK h, void *address);
75 static int tcpip_more(COMSTACK h);
76 static int tcpip_rcvconnect(COMSTACK h);
77 static int tcpip_bind(COMSTACK h, void *address, int mode);
78 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
79 int (*check_ip)(void *cd, const char *a, int len, int type),
81 static int tcpip_set_blocking(COMSTACK p, int blocking);
84 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
85 static int ssl_put(COMSTACK h, char *buf, int size);
88 static COMSTACK tcpip_accept(COMSTACK h);
89 static const char *tcpip_addrstr(COMSTACK h);
90 static void *tcpip_straddr(COMSTACK h, const char *str);
99 #define YAZ_SOCKLEN_T int
103 struct tcpip_cred_ptr {
104 gnutls_certificate_credentials_t xcred;
109 /* this state is used for both SSL and straight TCP/IP */
110 typedef struct tcpip_state
112 char *altbuf; /* alternate buffer for surplus data */
113 int altsize; /* size as xmalloced */
114 int altlen; /* length of data or 0 if none */
116 int written; /* -1 if we aren't writing */
117 int towrite; /* to verify against user input */
118 int (*complete)(const char *buf, int len); /* length/complete. */
121 struct addrinfo *ai_connect;
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;
137 static int tcpip_init(void)
140 static int initialized = 0;
149 requested = MAKEWORD(1, 1);
150 if (WSAStartup(requested, &wd))
159 * This function is always called through the cs_create() macro.
160 * s >= 0: socket has already been established for us.
162 COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
169 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
171 if (!(sp = (struct tcpip_state *)(p->cprivate =
172 xmalloc(sizeof(tcpip_state)))))
179 p->type = tcpip_type;
180 p->protocol = (enum oid_proto) protocol;
182 p->f_connect = tcpip_connect;
183 p->f_rcvconnect = tcpip_rcvconnect;
184 p->f_get = tcpip_get;
185 p->f_put = tcpip_put;
186 p->f_close = tcpip_close;
187 p->f_more = tcpip_more;
188 p->f_bind = tcpip_bind;
189 p->f_listen = tcpip_listen;
190 p->f_accept = tcpip_accept;
191 p->f_addrstr = tcpip_addrstr;
192 p->f_straddr = tcpip_straddr;
193 p->f_set_blocking = tcpip_set_blocking;
194 p->max_recv_bytes = 128 * 1024 * 1024;
196 p->state = s < 0 ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
204 strcpy(sp->cert_fname, "yaz.pem");
211 sp->altsize = sp->altlen = 0;
212 sp->towrite = sp->written = -1;
213 sp->complete = cs_complete_auto;
215 sp->connect_request_buf = 0;
216 sp->connect_request_len = 0;
217 sp->connect_response_buf = 0;
218 sp->connect_response_len = 0;
220 TRC(fprintf(stderr, "Created new TCPIP comstack h=%p\n", p));
225 COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
226 const char *connect_host)
228 COMSTACK p = tcpip_type(s, flags, protocol, 0);
233 tcpip_state *sp = (tcpip_state *) p->cprivate;
234 sp->connect_request_buf = (char *) xmalloc(strlen(connect_host) + 30);
235 sprintf(sp->connect_request_buf, "CONNECT %s HTTP/1.0\r\n\r\n",
237 sp->connect_request_len = strlen(sp->connect_request_buf);
238 p->f_put = tcpip_put_connect;
239 p->f_get = tcpip_get_connect;
240 sp->complete = cs_complete_auto_head; /* only want HTTP header */
246 static void tcpip_create_cred(COMSTACK cs)
248 tcpip_state *sp = (tcpip_state *) cs->cprivate;
249 sp->cred_ptr = (struct tcpip_cred_ptr *) xmalloc(sizeof(*sp->cred_ptr));
250 sp->cred_ptr->ref = 1;
251 gnutls_certificate_allocate_credentials(&sp->cred_ptr->xcred);
256 COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
262 p = tcpip_type(s, flags, protocol, 0);
268 sp = (tcpip_state *) p->cprivate;
270 sp->session = (gnutls_session_t) vp;
271 /* note: we don't handle already opened socket in SSL mode - yet */
279 static int ssl_check_error(COMSTACK h, tcpip_state *sp, int res)
281 TRC(fprintf(stderr, "ssl_check_error error=%d fatal=%d msg=%s\n",
283 gnutls_error_is_fatal(res),
284 gnutls_strerror(res)));
285 if (res == GNUTLS_E_AGAIN || res == GNUTLS_E_INTERRUPTED)
287 int dir = gnutls_record_get_direction(sp->session);
288 TRC(fprintf(stderr, " -> incomplete dir=%d\n", dir));
289 h->io_pending = dir ? CS_WANT_WRITE : CS_WANT_READ;
292 h->cerrno = CSERRORSSL;
298 /* resolve using getaddrinfo */
299 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port,
302 struct addrinfo hints, *res;
307 hints.ai_family = AF_UNSPEC;
308 hints.ai_socktype = SOCK_STREAM;
309 hints.ai_protocol = 0;
310 hints.ai_addrlen = 0;
311 hints.ai_addr = NULL;
312 hints.ai_canonname = NULL;
313 hints.ai_next = NULL;
315 strncpy(host, str, sizeof(host)-1);
316 host[sizeof(host)-1] = 0;
317 if ((p = strchr(host, '/')))
319 if ((p = strrchr(host, ':')))
325 if (!strcmp("@", host))
327 hints.ai_flags = AI_PASSIVE;
328 hints.ai_family = AF_UNSPEC;
329 error = getaddrinfo(0, port, &hints, &res);
332 else if (!strcmp("@4", host))
334 hints.ai_flags = AI_PASSIVE;
335 hints.ai_family = AF_INET;
336 error = getaddrinfo(0, port, &hints, &res);
339 else if (!strcmp("@6", host))
341 hints.ai_flags = AI_PASSIVE;
342 hints.ai_family = AF_INET6;
343 error = getaddrinfo(0, port, &hints, &res);
348 error = getaddrinfo(host, port, &hints, &res);
357 /* gethostbyname .. old systems */
358 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
363 short int port = default_port;
365 unsigned long tmpadd;
369 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
370 add->sin_family = AF_INET;
371 strncpy(buf, str, sizeof(buf)-1);
372 buf[sizeof(buf)-1] = 0;
373 if ((p = strchr(buf, '/')))
375 if ((p = strrchr(buf, ':')))
380 add->sin_port = htons(port);
381 if (!strcmp("@", buf))
383 add->sin_addr.s_addr = INADDR_ANY;
385 else if ((tmpadd = inet_addr(buf)) != -1)
387 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
389 else if ((hp = gethostbyname(buf)))
391 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
392 sizeof(struct in_addr));
400 void *tcpip_straddr(COMSTACK h, const char *str)
402 tcpip_state *sp = (tcpip_state *)h->cprivate;
403 const char *port = "210";
404 struct addrinfo *ai = 0;
406 if (h->protocol == PROTO_HTTP)
408 if (h->type == ssl_type)
417 freeaddrinfo(sp->ai);
418 sp->ai = tcpip_getaddrinfo(str, port, &ipv6_only);
419 if (sp->ai && h->state == CS_ST_UNBND)
424 for (ai = sp->ai; ai; ai = ai->ai_next)
426 if (ai->ai_family == AF_INET6)
428 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
436 for (ai = sp->ai; ai; ai = ai->ai_next)
438 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
445 TRC(fprintf(stderr, "First socket fd=%d\n", s));
448 if (ai->ai_family == AF_INET6 && ipv6_only >= 0 &&
449 setsockopt(h->iofile,
451 IPV6_V6ONLY, &ipv6_only, sizeof(ipv6_only)))
453 if (!tcpip_set_blocking(h, h->flags))
459 void *tcpip_straddr(COMSTACK h, const char *str)
461 tcpip_state *sp = (tcpip_state *)h->cprivate;
463 if (h->protocol == PROTO_HTTP)
465 if (h->type == ssl_type)
473 if (!tcpip_strtoaddr_ex(str, &sp->addr, port))
475 if (h->state == CS_ST_UNBND)
478 s = socket(AF_INET, SOCK_STREAM, 0);
483 if (!tcpip_set_blocking(h, h->flags))
490 int tcpip_more(COMSTACK h)
492 tcpip_state *sp = (tcpip_state *)h->cprivate;
494 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
497 static int cont_connect(COMSTACK h)
500 tcpip_state *sp = (tcpip_state *)h->cprivate;
501 struct addrinfo *ai = sp->ai_connect;
502 while (ai && (ai = ai->ai_next))
505 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
509 if (h->type == ssl_type && sp->session)
511 gnutls_bye(sp->session, GNUTLS_SHUT_WR);
512 gnutls_deinit(sp->session);
517 closesocket(h->iofile);
521 TRC(fprintf(stderr, "Other socket call fd=%d\n", s));
522 h->state = CS_ST_UNBND;
524 tcpip_set_blocking(h, h->flags);
525 return tcpip_connect(h, ai);
535 * connect(2) will block (sometimes) - nothing we can do short of doing
536 * weird things like spawning subprocesses or threading or some weird junk
539 int tcpip_connect(COMSTACK h, void *address)
542 struct addrinfo *ai = (struct addrinfo *) address;
543 tcpip_state *sp = (tcpip_state *)h->cprivate;
545 struct sockaddr_in *add = (struct sockaddr_in *) address;
548 TRC(fprintf(stderr, "tcpip_connect h=%p\n", h));
550 if (h->state != CS_ST_UNBND)
552 h->cerrno = CSOUTSTATE;
556 r = connect(h->iofile, ai->ai_addr, ai->ai_addrlen);
559 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
564 if (WSAGetLastError() == WSAEWOULDBLOCK)
566 h->event = CS_CONNECT;
567 h->state = CS_ST_CONNECTING;
568 h->io_pending = CS_WANT_WRITE;
572 if (yaz_errno() == EINPROGRESS)
574 TRC(fprintf(stderr, "Pending fd=%d\n", h->iofile));
575 h->event = CS_CONNECT;
576 h->state = CS_ST_CONNECTING;
577 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
581 return cont_connect(h);
583 h->event = CS_CONNECT;
584 h->state = CS_ST_CONNECTING;
586 return tcpip_rcvconnect(h);
592 int tcpip_rcvconnect(COMSTACK h)
595 tcpip_state *sp = (tcpip_state *)h->cprivate;
597 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
599 if (h->state == CS_ST_DATAXFER)
601 if (h->state != CS_ST_CONNECTING)
603 h->cerrno = CSOUTSTATE;
607 if (h->type == ssl_type && !sp->session)
609 tcpip_create_cred(h);
610 gnutls_init(&sp->session, GNUTLS_CLIENT);
611 gnutls_set_default_priority(sp->session);
612 gnutls_credentials_set (sp->session, GNUTLS_CRD_CERTIFICATE,
613 sp->cred_ptr->xcred);
614 /* cast to intermediate size_t to avoid GCC warning. */
615 gnutls_transport_set_ptr(sp->session,
616 (gnutls_transport_ptr_t)
621 int res = gnutls_handshake(sp->session);
624 if (ssl_check_error(h, sp, res))
626 return cont_connect(h);
631 h->state = CS_ST_DATAXFER;
635 #define CERTF "ztest.pem"
636 #define KEYF "ztest.pem"
638 static int tcpip_bind(COMSTACK h, void *address, int mode)
641 tcpip_state *sp = (tcpip_state *)h->cprivate;
643 struct addrinfo *ai = (struct addrinfo *) address;
645 struct sockaddr *addr = (struct sockaddr *)address;
654 if (h->type == ssl_type && !sp->session)
657 tcpip_create_cred(h);
658 res = gnutls_certificate_set_x509_key_file(sp->cred_ptr->xcred,
661 GNUTLS_X509_FMT_PEM);
662 if (res != GNUTLS_E_SUCCESS)
664 fprintf(stderr, "Error 1\n");
665 h->cerrno = CSERRORSSL;
670 TRC(fprintf(stderr, "tcpip_bind\n"));
673 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
674 &one, sizeof(one)) < 0)
681 r = bind(h->iofile, ai->ai_addr, ai->ai_addrlen);
682 freeaddrinfo(sp->ai);
685 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
692 /* Allow a maximum-sized backlog of waiting-to-connect clients */
693 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
698 h->state = CS_ST_IDLE;
699 h->event = CS_LISTEN;
703 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
704 int (*check_ip)(void *cd, const char *a, int len, int t),
708 /* we don't get peer address on Windows (via accept) */
710 struct sockaddr_in addr;
711 YAZ_SOCKLEN_T len = sizeof(addr);
714 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
715 if (h->state != CS_ST_IDLE)
717 h->cerrno = CSOUTSTATE;
721 h->newfd = accept(h->iofile, 0, 0);
723 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
729 WSAGetLastError() == WSAEWOULDBLOCK
731 yaz_errno() == EWOULDBLOCK
733 #if EAGAIN != EWOULDBLOCK
734 || yaz_errno() == EAGAIN
739 h->cerrno = CSNODATA;
743 shutdown(h->iofile, SD_RECEIVE);
745 shutdown(h->iofile, SHUT_RD);
747 listen(h->iofile, SOMAXCONN);
756 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
757 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
760 if (check_ip && (*check_ip)(cd, (const char *) &addr,
761 sizeof(addr), AF_INET))
765 closesocket(h->newfd);
773 h->state = CS_ST_INCON;
777 COMSTACK tcpip_accept(COMSTACK h)
781 unsigned long tru = 1;
784 TRC(fprintf(stderr, "tcpip_accept h=%p pid=%d\n", h, getpid()));
785 if (h->state == CS_ST_INCON)
787 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
788 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
792 closesocket(h->newfd);
799 memcpy(cnew, h, sizeof(*h));
800 cnew->iofile = h->newfd;
801 cnew->io_pending = 0;
803 if (!(state = (tcpip_state *)
804 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
810 closesocket(h->newfd);
818 if (!tcpip_set_blocking(cnew, cnew->flags))
824 closesocket(h->newfd);
836 state->altsize = state->altlen = 0;
837 state->towrite = state->written = -1;
838 state->complete = st->complete;
842 cnew->state = CS_ST_ACCEPT;
843 h->state = CS_ST_IDLE;
846 state->cred_ptr = st->cred_ptr;
852 (state->cred_ptr->ref)++;
853 gnutls_init(&state->session, GNUTLS_SERVER);
860 res = gnutls_set_default_priority(state->session);
861 if (res != GNUTLS_E_SUCCESS)
867 res = gnutls_credentials_set(state->session,
868 GNUTLS_CRD_CERTIFICATE,
869 st->cred_ptr->xcred);
870 if (res != GNUTLS_E_SUCCESS)
876 /* cast to intermediate size_t to avoid GCC warning. */
877 gnutls_transport_set_ptr(state->session,
878 (gnutls_transport_ptr_t)
879 (size_t) cnew->iofile);
882 state->connect_request_buf = 0;
883 state->connect_response_buf = 0;
886 if (h->state == CS_ST_ACCEPT)
889 tcpip_state *state = (tcpip_state *)h->cprivate;
892 int res = gnutls_handshake(state->session);
895 if (ssl_check_error(h, state, res))
897 TRC(fprintf(stderr, "gnutls_handshake int in tcpip_accept\n"));
900 TRC(fprintf(stderr, "gnutls_handshake failed in tcpip_accept\n"));
904 TRC(fprintf(stderr, "SSL_accept complete. gnutls\n"));
910 h->cerrno = CSOUTSTATE;
914 h->state = CS_ST_DATAXFER;
919 #define CS_TCPIP_BUFCHUNK 4096
922 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
923 * 0=connection closed.
925 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
927 tcpip_state *sp = (tcpip_state *)h->cprivate;
929 int tmpi, berlen, rest, req, tomove;
930 int hasread = 0, res;
932 TRC(fprintf(stderr, "tcpip_get: h=%p bufsize=%d\n", h, *bufsize));
933 if (sp->altlen) /* switch buffers */
935 TRC(fprintf(stderr, " %d bytes in altbuf (%p)\n", sp->altlen,
940 *bufsize = sp->altsize;
941 hasread = sp->altlen;
947 while (!(berlen = (*sp->complete)(*buf, hasread)))
951 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
957 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
958 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
965 /* unfortunatly, sun sometimes forgets to set errno in recv
966 when EWOULDBLOCK etc. would be required (res = -1) */
968 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
969 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
972 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
973 strerror(yaz_errno())));
975 if (WSAGetLastError() == WSAEWOULDBLOCK)
977 h->io_pending = CS_WANT_READ;
986 if (yaz_errno() == EWOULDBLOCK
988 #if EAGAIN != EWOULDBLOCK
989 || yaz_errno() == EAGAIN
992 || yaz_errno() == EINPROGRESS
994 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
998 h->io_pending = CS_WANT_READ;
1001 else if (yaz_errno() == 0)
1005 h->cerrno = CSYSERR;
1013 if (hasread > h->max_recv_bytes)
1015 h->cerrno = CSBUFSIZE;
1019 TRC(fprintf(stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1021 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1022 if (hasread > berlen)
1024 tomove = req = hasread - berlen;
1025 rest = tomove % CS_TCPIP_BUFCHUNK;
1027 req += CS_TCPIP_BUFCHUNK - rest;
1030 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1032 h->cerrno = CSYSERR;
1035 } else if (sp->altsize < req)
1036 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1038 h->cerrno = CSYSERR;
1041 TRC(fprintf(stderr, " Moving %d bytes to altbuf(%p)\n", tomove,
1043 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1045 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1046 *(*buf + berlen) = '\0';
1047 return berlen ? berlen : 1;
1053 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
1054 * 0=connection closed.
1056 int ssl_get(COMSTACK h, char **buf, int *bufsize)
1058 tcpip_state *sp = (tcpip_state *)h->cprivate;
1060 int tmpi, berlen, rest, req, tomove;
1061 int hasread = 0, res;
1063 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
1064 if (sp->altlen) /* switch buffers */
1066 TRC(fprintf(stderr, " %d bytes in altbuf (%p)\n", sp->altlen,
1071 *bufsize = sp->altsize;
1072 hasread = sp->altlen;
1078 while (!(berlen = (*sp->complete)(*buf, hasread)))
1082 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
1085 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
1086 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
1088 res = gnutls_record_recv(sp->session, *buf + hasread,
1092 TRC(fprintf(stderr, "gnutls_record_recv returned 0\n"));
1097 if (ssl_check_error(h, sp, res))
1103 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1105 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1106 if (hasread > berlen)
1108 tomove = req = hasread - berlen;
1109 rest = tomove % CS_TCPIP_BUFCHUNK;
1111 req += CS_TCPIP_BUFCHUNK - rest;
1114 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1116 } else if (sp->altsize < req)
1117 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1119 TRC(fprintf(stderr, " Moving %d bytes to altbuf(%p)\n", tomove,
1121 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1123 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1124 *(*buf + berlen) = '\0';
1125 return berlen ? berlen : 1;
1130 * Returns 1, 0 or -1
1131 * In nonblocking mode, you must call again with same buffer while
1132 * return value is 1.
1134 int tcpip_put(COMSTACK h, char *buf, int size)
1137 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1139 TRC(fprintf(stderr, "tcpip_put: h=%p size=%d\n", h, size));
1142 if (state->towrite < 0)
1144 state->towrite = size;
1147 else if (state->towrite != size)
1149 h->cerrno = CSWRONGBUF;
1152 while (state->towrite > state->written)
1155 send(h->iofile, buf + state->written, size -
1166 WSAGetLastError() == WSAEWOULDBLOCK
1168 yaz_errno() == EWOULDBLOCK
1170 #if EAGAIN != EWOULDBLOCK
1171 || yaz_errno() == EAGAIN
1175 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1177 || yaz_errno() == EINPROGRESS
1181 TRC(fprintf(stderr, " Flow control stop\n"));
1182 h->io_pending = CS_WANT_WRITE;
1185 return cont_connect(h);
1187 state->written += res;
1188 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1189 res, state->written, size));
1191 state->towrite = state->written = -1;
1192 TRC(fprintf(stderr, " Ok\n"));
1199 * Returns 1, 0 or -1
1200 * In nonblocking mode, you must call again with same buffer while
1201 * return value is 1.
1203 int ssl_put(COMSTACK h, char *buf, int size)
1206 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1208 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1211 if (state->towrite < 0)
1213 state->towrite = size;
1216 else if (state->towrite != size)
1218 h->cerrno = CSWRONGBUF;
1221 while (state->towrite > state->written)
1223 res = gnutls_record_send(state->session, buf + state->written,
1224 size - state->written);
1227 if (ssl_check_error(h, state, res))
1231 state->written += res;
1232 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1233 res, state->written, size));
1235 state->towrite = state->written = -1;
1236 TRC(fprintf(stderr, " Ok\n"));
1241 void tcpip_close(COMSTACK h)
1243 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1245 TRC(fprintf(stderr, "tcpip_close: h=%p pid=%d\n", h, getpid()));
1246 if (h->iofile != -1)
1250 gnutls_bye(sp->session, GNUTLS_SHUT_WR);
1253 closesocket(h->iofile);
1263 gnutls_deinit(sp->session);
1267 assert(sp->cred_ptr->ref > 0);
1269 if (--(sp->cred_ptr->ref) == 0)
1271 TRC(fprintf(stderr, "Removed credentials %p pid=%d\n",
1272 sp->cred_ptr->xcred, getpid()));
1273 gnutls_certificate_free_credentials(sp->cred_ptr->xcred);
1274 xfree(sp->cred_ptr);
1279 #if HAVE_GETADDRINFO
1281 freeaddrinfo(sp->ai);
1283 xfree(sp->connect_request_buf);
1284 xfree(sp->connect_response_buf);
1289 const char *tcpip_addrstr(COMSTACK h)
1291 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1292 char *r = 0, *buf = sp->buf;
1294 #if HAVE_GETADDRINFO
1296 struct sockaddr_storage addr;
1297 YAZ_SOCKLEN_T len = sizeof(addr);
1299 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1301 h->cerrno = CSYSERR;
1304 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
1306 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1315 struct sockaddr_in addr;
1316 YAZ_SOCKLEN_T len = sizeof(addr);
1317 struct hostent *host;
1319 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1321 h->cerrno = CSYSERR;
1324 if (!(h->flags & CS_FLAGS_NUMERICHOST))
1326 if ((host = gethostbyaddr((char*)&addr.sin_addr,
1327 sizeof(addr.sin_addr),
1329 r = (char*) host->h_name;
1332 r = inet_ntoa(addr.sin_addr);
1335 if (h->protocol == PROTO_HTTP)
1336 sprintf(buf, "http:%s", r);
1338 sprintf(buf, "tcp:%s", r);
1342 if (h->protocol == PROTO_HTTP)
1343 sprintf(buf, "https:%s", r);
1345 sprintf(buf, "ssl:%s", r);
1351 static int tcpip_set_blocking(COMSTACK p, int flags)
1356 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1357 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1360 flag = fcntl(p->iofile, F_GETFL, 0);
1361 if (flags & CS_FLAGS_BLOCKING)
1362 flag = flag & ~O_NONBLOCK; /* blocking */
1365 flag = flag | O_NONBLOCK; /* non-blocking */
1366 signal(SIGPIPE, SIG_IGN);
1368 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1377 /* gnutls_x509_crt_print appeared in 1.7.6. Memory leaks were fixed in 1.7.9.
1378 GNUTLS_CRT_PRINT_FULL appeared in 2.4.0. */
1379 #if GNUTLS_VERSION_NUMBER >= 0x020400
1380 #define USE_GNUTLS_X509_CRT_PRINT 1
1382 #define USE_GNUTLS_X509_CRT_PRINT 0
1386 #if USE_GNUTLS_X509_CRT_PRINT
1388 static const char *bin2hex(const void *bin, size_t bin_size)
1390 static char printable[110];
1391 const unsigned char *_bin = bin;
1397 for (i = 0; i < bin_size; i++)
1399 sprintf(print, "%.2x ", _bin[i]);
1405 static void x509_crt_print(gnutls_x509_crt_t cert)
1407 time_t expiration_time, activation_time;
1411 unsigned int algo, bits;
1413 expiration_time = gnutls_x509_crt_get_expiration_time(cert);
1414 activation_time = gnutls_x509_crt_get_activation_time(cert);
1416 printf("\tCertificate is valid since: %s", ctime(&activation_time));
1417 printf("\tCertificate expires: %s", ctime(&expiration_time));
1419 /* Print the serial number of the certificate. */
1420 size = sizeof(serial);
1421 gnutls_x509_crt_get_serial(cert, serial, &size);
1423 printf("\tCertificate serial number: %s\n", bin2hex(serial, size));
1425 /* Extract some of the public key algorithm's parameters
1427 algo = gnutls_x509_crt_get_pk_algorithm(cert, &bits);
1429 printf("Certificate public key: %s", gnutls_pk_algorithm_get_name(algo));
1431 /* Print the version of the X.509 certificate. */
1432 printf("\tCertificate version: #%d\n", gnutls_x509_crt_get_version(cert));
1435 gnutls_x509_crt_get_dn(cert, dn, &size);
1436 printf("\tDN: %s\n", dn);
1439 gnutls_x509_crt_get_issuer_dn(cert, dn, &size);
1440 printf("\tIssuer's DN: %s\n", dn);
1445 void cs_print_session_info(COMSTACK cs)
1448 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
1449 if (cs->type == ssl_type && sp->session)
1451 const gnutls_datum_t *cert_list;
1452 unsigned i, cert_list_size;
1453 if (gnutls_certificate_type_get(sp->session) != GNUTLS_CRT_X509)
1455 printf("X509 certificate\n");
1456 cert_list = gnutls_certificate_get_peers(sp->session,
1458 printf("Peer provided %u certificates\n", cert_list_size);
1459 for (i = 0; i < cert_list_size; i++)
1461 gnutls_x509_crt_t cert;
1462 #if USE_GNUTLS_X509_CRT_PRINT
1464 gnutls_datum_t cinfo;
1466 gnutls_x509_crt_init(&cert);
1467 gnutls_x509_crt_import(cert, &cert_list[i], GNUTLS_X509_FMT_DER);
1468 printf("Certificate info %d:\n", i + 1);
1469 #if USE_GNUTLS_X509_CRT_PRINT
1470 ret = gnutls_x509_crt_print(cert, GNUTLS_CRT_PRINT_FULL,
1474 printf("\t%s\n", cinfo.data);
1475 gnutls_free(cinfo.data);
1478 x509_crt_print(cert);
1480 gnutls_x509_crt_deinit(cert);
1487 void *cs_get_ssl(COMSTACK cs)
1489 /* doesn't do anything for GNUTLS */
1493 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1496 if (cs && cs->type == ssl_type)
1498 /* doesn't do anything for GNUTLS */
1505 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1508 if (cs && cs->type == ssl_type)
1510 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
1511 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1512 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1519 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1523 #if USE_GNUTLS_X509_CRT_PRINT
1524 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
1525 if (cs->type == ssl_type && sp->session)
1527 const gnutls_datum_t *cert_list;
1528 unsigned cert_list_size;
1529 if (gnutls_certificate_type_get(sp->session) != GNUTLS_CRT_X509)
1531 cert_list = gnutls_certificate_get_peers(sp->session, &cert_list_size);
1532 if (cert_list_size > 0)
1534 gnutls_x509_crt_t cert;
1536 gnutls_datum_t cinfo;
1538 gnutls_x509_crt_init(&cert);
1539 gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
1541 ret = gnutls_x509_crt_print(cert, GNUTLS_CRT_PRINT_FULL, &cinfo);
1544 *buf = xstrdup((char *) cinfo.data);
1545 *len = strlen(*buf);
1546 gnutls_free(cinfo.data);
1547 gnutls_x509_crt_deinit(cert);
1550 gnutls_x509_crt_deinit(cert);
1558 static int tcpip_put_connect(COMSTACK h, char *buf, int size)
1560 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1562 int r = tcpip_put(h, state->connect_request_buf,
1563 state->connect_request_len);
1567 h->f_put = tcpip_put; /* switch to normal tcpip put */
1568 r = tcpip_put(h, buf, size);
1573 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize)
1575 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1578 r = tcpip_get(h, &state->connect_response_buf,
1579 &state->connect_response_len);
1582 /* got the connect response completely */
1583 state->complete = cs_complete_auto; /* switch to normal tcpip get */
1584 h->f_get = tcpip_get;
1585 return tcpip_get(h, buf, bufsize);
1592 * c-file-style: "Stroustrup"
1593 * indent-tabs-mode: nil
1595 * vim: shiftwidth=4 tabstop=8 expandtab