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");
212 sp->altsize = sp->altlen = 0;
213 sp->towrite = sp->written = -1;
214 sp->complete = cs_complete_auto;
216 sp->connect_request_buf = 0;
217 sp->connect_request_len = 0;
218 sp->connect_response_buf = 0;
219 sp->connect_response_len = 0;
221 TRC(fprintf(stderr, "Created new TCPIP comstack h=%p\n", p));
226 COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
227 const char *connect_host)
229 COMSTACK p = tcpip_type(s, flags, protocol, 0);
234 tcpip_state *sp = (tcpip_state *) p->cprivate;
235 sp->connect_request_buf = (char *) xmalloc(strlen(connect_host) + 30);
236 sprintf(sp->connect_request_buf, "CONNECT %s HTTP/1.0\r\n\r\n",
238 sp->connect_request_len = strlen(sp->connect_request_buf);
239 p->f_put = tcpip_put_connect;
240 p->f_get = tcpip_get_connect;
241 sp->complete = cs_complete_auto_head; /* only want HTTP header */
247 static void tcpip_create_cred(COMSTACK cs)
249 tcpip_state *sp = (tcpip_state *) cs->cprivate;
250 sp->cred_ptr = (struct tcpip_cred_ptr *) xmalloc(sizeof(*sp->cred_ptr));
251 sp->cred_ptr->ref = 1;
252 gnutls_certificate_allocate_credentials(&sp->cred_ptr->xcred);
257 COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
263 p = tcpip_type(s, flags, protocol, 0);
269 sp = (tcpip_state *) p->cprivate;
271 sp->session = (gnutls_session_t) vp;
272 /* note: we don't handle already opened socket in SSL mode - yet */
280 static int ssl_check_error(COMSTACK h, tcpip_state *sp, int res)
282 TRC(fprintf(stderr, "ssl_check_error error=%d fatal=%d msg=%s\n",
284 gnutls_error_is_fatal(res),
285 gnutls_strerror(res)));
286 if (res == GNUTLS_E_AGAIN || res == GNUTLS_E_INTERRUPTED)
288 int dir = gnutls_record_get_direction(sp->session);
289 TRC(fprintf(stderr, " -> incomplete dir=%d\n", dir));
290 h->io_pending = dir ? CS_WANT_WRITE : CS_WANT_READ;
293 h->cerrno = CSERRORSSL;
299 /* resolve using getaddrinfo */
300 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port,
303 struct addrinfo hints, *res;
308 hints.ai_family = AF_UNSPEC;
309 hints.ai_socktype = SOCK_STREAM;
310 hints.ai_protocol = 0;
311 hints.ai_addrlen = 0;
312 hints.ai_addr = NULL;
313 hints.ai_canonname = NULL;
314 hints.ai_next = NULL;
316 strncpy(host, str, sizeof(host)-1);
317 host[sizeof(host)-1] = 0;
318 if ((p = strchr(host, '/')))
320 if ((p = strrchr(host, ':')))
326 if (!strcmp("@", host))
328 hints.ai_flags = AI_PASSIVE;
329 hints.ai_family = AF_UNSPEC;
330 error = getaddrinfo(0, port, &hints, &res);
333 else if (!strcmp("@4", host))
335 hints.ai_flags = AI_PASSIVE;
336 hints.ai_family = AF_INET;
337 error = getaddrinfo(0, port, &hints, &res);
340 else if (!strcmp("@6", host))
342 hints.ai_flags = AI_PASSIVE;
343 hints.ai_family = AF_INET6;
344 error = getaddrinfo(0, port, &hints, &res);
349 error = getaddrinfo(host, port, &hints, &res);
358 /* gethostbyname .. old systems */
359 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
364 short int port = default_port;
366 unsigned long tmpadd;
370 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
371 add->sin_family = AF_INET;
372 strncpy(buf, str, sizeof(buf)-1);
373 buf[sizeof(buf)-1] = 0;
374 if ((p = strchr(buf, '/')))
376 if ((p = strrchr(buf, ':')))
381 add->sin_port = htons(port);
382 if (!strcmp("@", buf))
384 add->sin_addr.s_addr = INADDR_ANY;
386 else if ((tmpadd = inet_addr(buf)) != -1)
388 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
390 else if ((hp = gethostbyname(buf)))
392 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
393 sizeof(struct in_addr));
401 void *tcpip_straddr(COMSTACK h, const char *str)
403 tcpip_state *sp = (tcpip_state *)h->cprivate;
404 const char *port = "210";
405 struct addrinfo *ai = 0;
407 if (h->protocol == PROTO_HTTP)
409 if (h->type == ssl_type)
418 freeaddrinfo(sp->ai);
419 sp->ai = tcpip_getaddrinfo(str, port, &ipv6_only);
420 if (sp->ai && h->state == CS_ST_UNBND)
425 for (ai = sp->ai; ai; ai = ai->ai_next)
427 if (ai->ai_family == AF_INET6)
429 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
437 for (ai = sp->ai; ai; ai = ai->ai_next)
439 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
446 TRC(fprintf(stderr, "First socket fd=%d\n", s));
449 if (ai->ai_family == AF_INET6 && ipv6_only >= 0 &&
450 setsockopt(h->iofile,
452 IPV6_V6ONLY, &ipv6_only, sizeof(ipv6_only)))
454 if (!tcpip_set_blocking(h, h->flags))
460 void *tcpip_straddr(COMSTACK h, const char *str)
462 tcpip_state *sp = (tcpip_state *)h->cprivate;
464 if (h->protocol == PROTO_HTTP)
466 if (h->type == ssl_type)
474 if (!tcpip_strtoaddr_ex(str, &sp->addr, port))
476 if (h->state == CS_ST_UNBND)
479 s = socket(AF_INET, SOCK_STREAM, 0);
484 if (!tcpip_set_blocking(h, h->flags))
491 int tcpip_more(COMSTACK h)
493 tcpip_state *sp = (tcpip_state *)h->cprivate;
495 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
498 static int cont_connect(COMSTACK h)
501 tcpip_state *sp = (tcpip_state *)h->cprivate;
502 struct addrinfo *ai = sp->ai_connect;
503 while (ai && (ai = ai->ai_next))
506 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
510 if (h->type == ssl_type && sp->session)
512 gnutls_bye(sp->session, GNUTLS_SHUT_WR);
513 gnutls_deinit(sp->session);
518 closesocket(h->iofile);
522 TRC(fprintf(stderr, "Other socket call fd=%d\n", s));
523 h->state = CS_ST_UNBND;
525 tcpip_set_blocking(h, h->flags);
526 return tcpip_connect(h, ai);
536 * connect(2) will block (sometimes) - nothing we can do short of doing
537 * weird things like spawning subprocesses or threading or some weird junk
540 int tcpip_connect(COMSTACK h, void *address)
543 struct addrinfo *ai = (struct addrinfo *) address;
544 tcpip_state *sp = (tcpip_state *)h->cprivate;
546 struct sockaddr_in *add = (struct sockaddr_in *) address;
549 TRC(fprintf(stderr, "tcpip_connect h=%p\n", h));
551 if (h->state != CS_ST_UNBND)
553 h->cerrno = CSOUTSTATE;
557 r = connect(h->iofile, ai->ai_addr, ai->ai_addrlen);
560 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
565 if (WSAGetLastError() == WSAEWOULDBLOCK)
567 h->event = CS_CONNECT;
568 h->state = CS_ST_CONNECTING;
569 h->io_pending = CS_WANT_WRITE;
573 if (yaz_errno() == EINPROGRESS)
575 TRC(fprintf(stderr, "Pending fd=%d\n", h->iofile));
576 h->event = CS_CONNECT;
577 h->state = CS_ST_CONNECTING;
578 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
582 return cont_connect(h);
584 h->event = CS_CONNECT;
585 h->state = CS_ST_CONNECTING;
587 return tcpip_rcvconnect(h);
593 int tcpip_rcvconnect(COMSTACK h)
596 tcpip_state *sp = (tcpip_state *)h->cprivate;
598 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
600 if (h->state == CS_ST_DATAXFER)
602 if (h->state != CS_ST_CONNECTING)
604 h->cerrno = CSOUTSTATE;
608 if (h->type == ssl_type && !sp->session)
610 tcpip_create_cred(h);
611 gnutls_init(&sp->session, GNUTLS_CLIENT);
612 gnutls_set_default_priority(sp->session);
613 gnutls_credentials_set (sp->session, GNUTLS_CRD_CERTIFICATE,
614 sp->cred_ptr->xcred);
615 /* cast to intermediate size_t to avoid GCC warning. */
616 gnutls_transport_set_ptr(sp->session,
617 (gnutls_transport_ptr_t)
622 int res = gnutls_handshake(sp->session);
625 if (ssl_check_error(h, sp, res))
627 return cont_connect(h);
632 h->state = CS_ST_DATAXFER;
636 #define CERTF "ztest.pem"
637 #define KEYF "ztest.pem"
639 static int tcpip_bind(COMSTACK h, void *address, int mode)
642 tcpip_state *sp = (tcpip_state *)h->cprivate;
644 struct addrinfo *ai = (struct addrinfo *) address;
646 struct sockaddr *addr = (struct sockaddr *)address;
655 if (h->type == ssl_type && !sp->session)
658 tcpip_create_cred(h);
659 res = gnutls_certificate_set_x509_key_file(sp->cred_ptr->xcred,
662 GNUTLS_X509_FMT_PEM);
663 if (res != GNUTLS_E_SUCCESS)
665 fprintf(stderr, "Error 1\n");
666 h->cerrno = CSERRORSSL;
671 TRC(fprintf(stderr, "tcpip_bind\n"));
674 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
675 &one, sizeof(one)) < 0)
682 r = bind(h->iofile, ai->ai_addr, ai->ai_addrlen);
683 freeaddrinfo(sp->ai);
686 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
693 /* Allow a maximum-sized backlog of waiting-to-connect clients */
694 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
699 h->state = CS_ST_IDLE;
700 h->event = CS_LISTEN;
704 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
705 int (*check_ip)(void *cd, const char *a, int len, int t),
709 /* we don't get peer address on Windows (via accept) */
711 struct sockaddr_in addr;
712 YAZ_SOCKLEN_T len = sizeof(addr);
715 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
716 if (h->state != CS_ST_IDLE)
718 h->cerrno = CSOUTSTATE;
722 h->newfd = accept(h->iofile, 0, 0);
724 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
730 WSAGetLastError() == WSAEWOULDBLOCK
732 yaz_errno() == EWOULDBLOCK
734 #if EAGAIN != EWOULDBLOCK
735 || yaz_errno() == EAGAIN
740 h->cerrno = CSNODATA;
744 shutdown(h->iofile, SD_RECEIVE);
746 shutdown(h->iofile, SHUT_RD);
748 listen(h->iofile, SOMAXCONN);
757 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
758 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
761 if (check_ip && (*check_ip)(cd, (const char *) &addr,
762 sizeof(addr), AF_INET))
766 closesocket(h->newfd);
774 h->state = CS_ST_INCON;
778 COMSTACK tcpip_accept(COMSTACK h)
782 unsigned long tru = 1;
785 TRC(fprintf(stderr, "tcpip_accept h=%p pid=%d\n", h, getpid()));
786 if (h->state == CS_ST_INCON)
788 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
789 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
793 closesocket(h->newfd);
800 memcpy(cnew, h, sizeof(*h));
801 cnew->iofile = h->newfd;
802 cnew->io_pending = 0;
804 if (!(state = (tcpip_state *)
805 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
811 closesocket(h->newfd);
819 if (!tcpip_set_blocking(cnew, cnew->flags))
825 closesocket(h->newfd);
837 state->altsize = state->altlen = 0;
838 state->towrite = state->written = -1;
839 state->complete = st->complete;
843 cnew->state = CS_ST_ACCEPT;
844 h->state = CS_ST_IDLE;
847 state->cred_ptr = st->cred_ptr;
853 (state->cred_ptr->ref)++;
854 gnutls_init(&state->session, GNUTLS_SERVER);
861 res = gnutls_set_default_priority(state->session);
862 if (res != GNUTLS_E_SUCCESS)
868 res = gnutls_credentials_set(state->session,
869 GNUTLS_CRD_CERTIFICATE,
870 st->cred_ptr->xcred);
871 if (res != GNUTLS_E_SUCCESS)
877 /* cast to intermediate size_t to avoid GCC warning. */
878 gnutls_transport_set_ptr(state->session,
879 (gnutls_transport_ptr_t)
880 (size_t) cnew->iofile);
883 state->connect_request_buf = 0;
884 state->connect_response_buf = 0;
887 if (h->state == CS_ST_ACCEPT)
890 tcpip_state *state = (tcpip_state *)h->cprivate;
893 int res = gnutls_handshake(state->session);
896 if (ssl_check_error(h, state, res))
898 TRC(fprintf(stderr, "gnutls_handshake int in tcpip_accept\n"));
901 TRC(fprintf(stderr, "gnutls_handshake failed in tcpip_accept\n"));
905 TRC(fprintf(stderr, "SSL_accept complete. gnutls\n"));
911 h->cerrno = CSOUTSTATE;
915 h->state = CS_ST_DATAXFER;
920 #define CS_TCPIP_BUFCHUNK 4096
923 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
924 * 0=connection closed.
926 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
928 tcpip_state *sp = (tcpip_state *)h->cprivate;
930 int tmpi, berlen, rest, req, tomove;
931 int hasread = 0, res;
933 TRC(fprintf(stderr, "tcpip_get: h=%p bufsize=%d\n", h, *bufsize));
934 if (sp->altlen) /* switch buffers */
936 TRC(fprintf(stderr, " %d bytes in altbuf (%p)\n", sp->altlen,
941 *bufsize = sp->altsize;
942 hasread = sp->altlen;
948 while (!(berlen = (*sp->complete)(*buf, hasread)))
952 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
958 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
959 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
966 /* unfortunatly, sun sometimes forgets to set errno in recv
967 when EWOULDBLOCK etc. would be required (res = -1) */
969 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
970 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
973 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
974 strerror(yaz_errno())));
976 if (WSAGetLastError() == WSAEWOULDBLOCK)
978 h->io_pending = CS_WANT_READ;
987 if (yaz_errno() == EWOULDBLOCK
989 #if EAGAIN != EWOULDBLOCK
990 || yaz_errno() == EAGAIN
993 || yaz_errno() == EINPROGRESS
995 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
999 h->io_pending = CS_WANT_READ;
1002 else if (yaz_errno() == 0)
1006 h->cerrno = CSYSERR;
1014 if (hasread > h->max_recv_bytes)
1016 h->cerrno = CSBUFSIZE;
1020 TRC(fprintf(stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1022 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1023 if (hasread > berlen)
1025 tomove = req = hasread - berlen;
1026 rest = tomove % CS_TCPIP_BUFCHUNK;
1028 req += CS_TCPIP_BUFCHUNK - rest;
1031 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1033 h->cerrno = CSYSERR;
1036 } else if (sp->altsize < req)
1037 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1039 h->cerrno = CSYSERR;
1042 TRC(fprintf(stderr, " Moving %d bytes to altbuf(%p)\n", tomove,
1044 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1046 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1047 *(*buf + berlen) = '\0';
1048 return berlen ? berlen : 1;
1054 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
1055 * 0=connection closed.
1057 int ssl_get(COMSTACK h, char **buf, int *bufsize)
1059 tcpip_state *sp = (tcpip_state *)h->cprivate;
1061 int tmpi, berlen, rest, req, tomove;
1062 int hasread = 0, res;
1064 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
1065 if (sp->altlen) /* switch buffers */
1067 TRC(fprintf(stderr, " %d bytes in altbuf (%p)\n", sp->altlen,
1072 *bufsize = sp->altsize;
1073 hasread = sp->altlen;
1079 while (!(berlen = (*sp->complete)(*buf, hasread)))
1083 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
1086 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
1087 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
1089 res = gnutls_record_recv(sp->session, *buf + hasread,
1093 TRC(fprintf(stderr, "gnutls_record_recv returned 0\n"));
1098 if (ssl_check_error(h, sp, res))
1104 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1106 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1107 if (hasread > berlen)
1109 tomove = req = hasread - berlen;
1110 rest = tomove % CS_TCPIP_BUFCHUNK;
1112 req += CS_TCPIP_BUFCHUNK - rest;
1115 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1117 } else if (sp->altsize < req)
1118 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1120 TRC(fprintf(stderr, " Moving %d bytes to altbuf(%p)\n", tomove,
1122 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1124 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1125 *(*buf + berlen) = '\0';
1126 return berlen ? berlen : 1;
1131 * Returns 1, 0 or -1
1132 * In nonblocking mode, you must call again with same buffer while
1133 * return value is 1.
1135 int tcpip_put(COMSTACK h, char *buf, int size)
1138 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1140 TRC(fprintf(stderr, "tcpip_put: h=%p size=%d\n", h, size));
1143 if (state->towrite < 0)
1145 state->towrite = size;
1148 else if (state->towrite != size)
1150 h->cerrno = CSWRONGBUF;
1153 while (state->towrite > state->written)
1156 send(h->iofile, buf + state->written, size -
1167 WSAGetLastError() == WSAEWOULDBLOCK
1169 yaz_errno() == EWOULDBLOCK
1171 #if EAGAIN != EWOULDBLOCK
1172 || yaz_errno() == EAGAIN
1176 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1178 || yaz_errno() == EINPROGRESS
1182 TRC(fprintf(stderr, " Flow control stop\n"));
1183 h->io_pending = CS_WANT_WRITE;
1186 return cont_connect(h);
1188 state->written += res;
1189 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1190 res, state->written, size));
1192 state->towrite = state->written = -1;
1193 TRC(fprintf(stderr, " Ok\n"));
1200 * Returns 1, 0 or -1
1201 * In nonblocking mode, you must call again with same buffer while
1202 * return value is 1.
1204 int ssl_put(COMSTACK h, char *buf, int size)
1207 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1209 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1212 if (state->towrite < 0)
1214 state->towrite = size;
1217 else if (state->towrite != size)
1219 h->cerrno = CSWRONGBUF;
1222 while (state->towrite > state->written)
1224 res = gnutls_record_send(state->session, buf + state->written,
1225 size - state->written);
1228 if (ssl_check_error(h, state, res))
1232 state->written += res;
1233 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1234 res, state->written, size));
1236 state->towrite = state->written = -1;
1237 TRC(fprintf(stderr, " Ok\n"));
1242 void tcpip_close(COMSTACK h)
1244 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1246 TRC(fprintf(stderr, "tcpip_close: h=%p pid=%d\n", h, getpid()));
1247 if (h->iofile != -1)
1251 gnutls_bye(sp->session, GNUTLS_SHUT_WR);
1254 closesocket(h->iofile);
1264 gnutls_deinit(sp->session);
1268 assert(sp->cred_ptr->ref > 0);
1270 if (--(sp->cred_ptr->ref) == 0)
1272 TRC(fprintf(stderr, "Removed credentials %p pid=%d\n",
1273 sp->cred_ptr->xcred, getpid()));
1274 gnutls_certificate_free_credentials(sp->cred_ptr->xcred);
1275 xfree(sp->cred_ptr);
1280 #if HAVE_GETADDRINFO
1282 freeaddrinfo(sp->ai);
1284 xfree(sp->connect_request_buf);
1285 xfree(sp->connect_response_buf);
1290 const char *tcpip_addrstr(COMSTACK h)
1292 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1293 char *r = 0, *buf = sp->buf;
1295 #if HAVE_GETADDRINFO
1297 struct sockaddr_storage addr;
1298 YAZ_SOCKLEN_T len = sizeof(addr);
1300 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1302 h->cerrno = CSYSERR;
1305 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
1307 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1316 struct sockaddr_in addr;
1317 YAZ_SOCKLEN_T len = sizeof(addr);
1318 struct hostent *host;
1320 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1322 h->cerrno = CSYSERR;
1325 if (!(h->flags & CS_FLAGS_NUMERICHOST))
1327 if ((host = gethostbyaddr((char*)&addr.sin_addr,
1328 sizeof(addr.sin_addr),
1330 r = (char*) host->h_name;
1333 r = inet_ntoa(addr.sin_addr);
1336 if (h->protocol == PROTO_HTTP)
1337 sprintf(buf, "http:%s", r);
1339 sprintf(buf, "tcp:%s", r);
1343 if (h->protocol == PROTO_HTTP)
1344 sprintf(buf, "https:%s", r);
1346 sprintf(buf, "ssl:%s", r);
1352 static int tcpip_set_blocking(COMSTACK p, int flags)
1357 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1358 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1361 flag = fcntl(p->iofile, F_GETFL, 0);
1362 if (flags & CS_FLAGS_BLOCKING)
1363 flag = flag & ~O_NONBLOCK; /* blocking */
1366 flag = flag | O_NONBLOCK; /* non-blocking */
1367 signal(SIGPIPE, SIG_IGN);
1369 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1378 /* gnutls_x509_crt_print appeared in 1.7.6. Memory leaks were fixed in 1.7.9.
1379 GNUTLS_CRT_PRINT_FULL appeared in 2.4.0. */
1380 #if GNUTLS_VERSION_NUMBER >= 0x020400
1381 #define USE_GNUTLS_X509_CRT_PRINT 1
1383 #define USE_GNUTLS_X509_CRT_PRINT 0
1387 #if USE_GNUTLS_X509_CRT_PRINT
1389 static const char *bin2hex(const void *bin, size_t bin_size)
1391 static char printable[110];
1392 const unsigned char *_bin = bin;
1398 for (i = 0; i < bin_size; i++)
1400 sprintf(print, "%.2x ", _bin[i]);
1406 static void x509_crt_print(gnutls_x509_crt_t cert)
1408 time_t expiration_time, activation_time;
1412 unsigned int algo, bits;
1414 expiration_time = gnutls_x509_crt_get_expiration_time(cert);
1415 activation_time = gnutls_x509_crt_get_activation_time(cert);
1417 printf("\tCertificate is valid since: %s", ctime(&activation_time));
1418 printf("\tCertificate expires: %s", ctime(&expiration_time));
1420 /* Print the serial number of the certificate. */
1421 size = sizeof(serial);
1422 gnutls_x509_crt_get_serial(cert, serial, &size);
1424 printf("\tCertificate serial number: %s\n", bin2hex(serial, size));
1426 /* Extract some of the public key algorithm's parameters
1428 algo = gnutls_x509_crt_get_pk_algorithm(cert, &bits);
1430 printf("Certificate public key: %s", gnutls_pk_algorithm_get_name(algo));
1432 /* Print the version of the X.509 certificate. */
1433 printf("\tCertificate version: #%d\n", gnutls_x509_crt_get_version(cert));
1436 gnutls_x509_crt_get_dn(cert, dn, &size);
1437 printf("\tDN: %s\n", dn);
1440 gnutls_x509_crt_get_issuer_dn(cert, dn, &size);
1441 printf("\tIssuer's DN: %s\n", dn);
1446 void cs_print_session_info(COMSTACK cs)
1449 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
1450 if (cs->type == ssl_type && sp->session)
1452 const gnutls_datum_t *cert_list;
1453 unsigned i, cert_list_size;
1454 if (gnutls_certificate_type_get(sp->session) != GNUTLS_CRT_X509)
1456 printf("X509 certificate\n");
1457 cert_list = gnutls_certificate_get_peers(sp->session,
1459 printf("Peer provided %u certificates\n", cert_list_size);
1460 for (i = 0; i < cert_list_size; i++)
1462 gnutls_x509_crt_t cert;
1463 #if USE_GNUTLS_X509_CRT_PRINT
1465 gnutls_datum_t cinfo;
1467 gnutls_x509_crt_init(&cert);
1468 gnutls_x509_crt_import(cert, &cert_list[i], GNUTLS_X509_FMT_DER);
1469 printf("Certificate info %d:\n", i + 1);
1470 #if USE_GNUTLS_X509_CRT_PRINT
1471 ret = gnutls_x509_crt_print(cert, GNUTLS_CRT_PRINT_FULL,
1475 printf("\t%s\n", cinfo.data);
1476 gnutls_free(cinfo.data);
1479 x509_crt_print(cert);
1481 gnutls_x509_crt_deinit(cert);
1488 void *cs_get_ssl(COMSTACK cs)
1490 /* doesn't do anything for GNUTLS */
1494 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1497 if (cs && cs->type == ssl_type)
1499 /* doesn't do anything for GNUTLS */
1506 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1509 if (cs && cs->type == ssl_type)
1511 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
1512 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1513 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1520 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1524 #if USE_GNUTLS_X509_CRT_PRINT
1525 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
1526 if (cs->type == ssl_type && sp->session)
1528 const gnutls_datum_t *cert_list;
1529 unsigned cert_list_size;
1530 if (gnutls_certificate_type_get(sp->session) != GNUTLS_CRT_X509)
1532 cert_list = gnutls_certificate_get_peers(sp->session, &cert_list_size);
1533 if (cert_list_size > 0)
1535 gnutls_x509_crt_t cert;
1537 gnutls_datum_t cinfo;
1539 gnutls_x509_crt_init(&cert);
1540 gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
1542 ret = gnutls_x509_crt_print(cert, GNUTLS_CRT_PRINT_FULL, &cinfo);
1545 *buf = xstrdup((char *) cinfo.data);
1546 *len = strlen(*buf);
1547 gnutls_free(cinfo.data);
1548 gnutls_x509_crt_deinit(cert);
1551 gnutls_x509_crt_deinit(cert);
1559 static int tcpip_put_connect(COMSTACK h, char *buf, int size)
1561 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1563 int r = tcpip_put(h, state->connect_request_buf,
1564 state->connect_request_len);
1568 h->f_put = tcpip_put; /* switch to normal tcpip put */
1569 r = tcpip_put(h, buf, size);
1574 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize)
1576 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1579 r = tcpip_get(h, &state->connect_response_buf,
1580 &state->connect_response_len);
1583 /* got the connect response completely */
1584 state->complete = cs_complete_auto; /* switch to normal tcpip get */
1585 h->f_get = tcpip_get;
1586 return tcpip_get(h, buf, bufsize);
1593 * c-file-style: "Stroustrup"
1594 * indent-tabs-mode: nil
1596 * vim: shiftwidth=4 tabstop=8 expandtab