1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements TCP/IP + SSL COMSTACK.
17 #include <sys/types.h>
28 /* VS 2003 or later has getaddrinfo; older versions do not */
32 #define HAVE_GETADDRINFO 1
34 #define HAVE_GETADDRINFO 0
38 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <netinet/tcp.h>
45 #include <sys/socket.h>
52 #include <gnutls/openssl.h>
53 #include <gnutls/x509.h>
57 #if HAVE_OPENSSL_SSL_H
58 #include <openssl/ssl.h>
59 #include <openssl/err.h>
63 #include <yaz/comstack.h>
64 #include <yaz/tcpip.h>
67 static int tcpip_close(COMSTACK h);
68 static int tcpip_put(COMSTACK h, char *buf, int size);
69 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
70 static int tcpip_put_connect(COMSTACK h, char *buf, int size);
71 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize);
72 static int tcpip_connect(COMSTACK h, void *address);
73 static int tcpip_more(COMSTACK h);
74 static int tcpip_rcvconnect(COMSTACK h);
75 static int tcpip_bind(COMSTACK h, void *address, int mode);
76 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
77 int (*check_ip)(void *cd, const char *a, int len, int type),
79 static int tcpip_set_blocking(COMSTACK p, int blocking);
82 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
83 static int ssl_put(COMSTACK h, char *buf, int size);
86 static COMSTACK tcpip_accept(COMSTACK h);
87 static char *tcpip_addrstr(COMSTACK h);
88 static void *tcpip_straddr(COMSTACK h, const char *str);
97 #define YAZ_SOCKLEN_T int
100 /* this state is used for both SSL and straight TCP/IP */
101 typedef struct tcpip_state
103 char *altbuf; /* alternate buffer for surplus data */
104 int altsize; /* size as xmalloced */
105 int altlen; /* length of data or 0 if none */
107 int written; /* -1 if we aren't writing */
108 int towrite; /* to verify against user input */
109 int (*complete)(const char *buf, int len); /* length/complete. */
113 struct sockaddr_in addr; /* returned by cs_straddr */
115 char buf[128]; /* returned by cs_addrstr */
117 SSL_CTX *ctx; /* current CTX. */
118 SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
120 char cert_fname[256];
122 char *connect_request_buf;
123 int connect_request_len;
124 char *connect_response_buf;
125 int connect_response_len;
129 static int tcpip_init(void)
131 static int initialized = 0;
137 requested = MAKEWORD(1, 1);
138 if (WSAStartup(requested, &wd))
145 static int tcpip_init(void)
152 * This function is always called through the cs_create() macro.
153 * s >= 0: socket has already been established for us.
155 COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
162 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
164 if (!(sp = (struct tcpip_state *)(p->cprivate =
165 xmalloc(sizeof(tcpip_state)))))
172 p->type = tcpip_type;
173 p->protocol = (enum oid_proto) protocol;
175 p->f_connect = tcpip_connect;
176 p->f_rcvconnect = tcpip_rcvconnect;
177 p->f_get = tcpip_get;
178 p->f_put = tcpip_put;
179 p->f_close = tcpip_close;
180 p->f_more = tcpip_more;
181 p->f_bind = tcpip_bind;
182 p->f_listen = tcpip_listen;
183 p->f_accept = tcpip_accept;
184 p->f_addrstr = tcpip_addrstr;
185 p->f_straddr = tcpip_straddr;
186 p->f_set_blocking = tcpip_set_blocking;
187 p->max_recv_bytes = 5000000;
189 p->state = s < 0 ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
196 sp->ctx = sp->ctx_alloc = 0;
198 strcpy(sp->cert_fname, "yaz.pem");
205 sp->altsize = sp->altlen = 0;
206 sp->towrite = sp->written = -1;
207 if (protocol == PROTO_WAIS)
208 sp->complete = completeWAIS;
210 sp->complete = cs_complete_auto;
212 sp->connect_request_buf = 0;
213 sp->connect_request_len = 0;
214 sp->connect_response_buf = 0;
215 sp->connect_response_len = 0;
217 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
218 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
223 COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
224 const char *connect_host)
226 COMSTACK p = tcpip_type(s, flags, protocol, 0);
231 tcpip_state *sp = (tcpip_state *) p->cprivate;
232 sp->connect_request_buf = (char *) xmalloc(strlen(connect_host) + 30);
233 sprintf(sp->connect_request_buf, "CONNECT %s HTTP/1.0\r\n\r\n",
235 sp->connect_request_len = strlen(sp->connect_request_buf);
236 p->f_put = tcpip_put_connect;
237 p->f_get = tcpip_get_connect;
238 sp->complete = cs_complete_auto_head; /* only want HTTP header */
246 COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
251 p = tcpip_type(s, flags, protocol, 0);
257 sp = (tcpip_state *) p->cprivate;
259 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
261 /* note: we don't handle already opened socket in SSL mode - yet */
265 int ssl_check_error(COMSTACK h, tcpip_state *sp, int res)
267 #if HAVE_OPENSSL_SSL_H
268 int err = SSL_get_error(sp->ssl, res);
269 TRC(fprintf(stderr, "got err=%d\n", err));
270 if (err == SSL_ERROR_WANT_READ)
272 TRC(fprintf(stderr, " -> SSL_ERROR_WANT_READ\n"));
273 h->io_pending = CS_WANT_READ;
276 if (err == SSL_ERROR_WANT_WRITE)
278 TRC(fprintf(stderr, " -> SSL_ERROR_WANT_WRITE\n"));
279 h->io_pending = CS_WANT_WRITE;
283 int tls_error = sp->ssl->last_error;
284 TRC(fprintf(stderr, "ssl_check_error error=%d fatal=%d msg=%s\n",
286 gnutls_error_is_fatal(tls_error),
287 gnutls_strerror(tls_error)));
288 if (tls_error == GNUTLS_E_AGAIN || tls_error == GNUTLS_E_INTERRUPTED)
290 int dir = gnutls_record_get_direction(sp->ssl->gnutls_state);
291 TRC(fprintf(stderr, " -> incomplete dir=%d\n", dir));
292 h->io_pending = dir ? CS_WANT_WRITE : CS_WANT_READ;
296 h->cerrno = CSERRORSSL;
302 /* resolve using getaddrinfo */
303 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
305 struct addrinfo hints, *res;
310 hints.ai_family = AF_UNSPEC;
311 hints.ai_socktype = SOCK_STREAM;
312 hints.ai_protocol = 0;
313 hints.ai_addrlen = 0;
314 hints.ai_addr = NULL;
315 hints.ai_canonname = NULL;
316 hints.ai_next = NULL;
318 strncpy(host, str, sizeof(host)-1);
319 host[sizeof(host)-1] = 0;
320 if ((p = strchr(host, '/')))
322 if ((p = strrchr(host, ':')))
328 if (!strcmp("@", host))
330 hints.ai_flags = AI_PASSIVE;
331 error = getaddrinfo(0, port, &hints, &res);
335 error = getaddrinfo(host, port, &hints, &res);
343 /* gethostbyname .. old systems */
344 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
349 short int port = default_port;
351 unsigned long tmpadd;
355 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
356 add->sin_family = AF_INET;
357 strncpy(buf, str, sizeof(buf)-1);
358 buf[sizeof(buf)-1] = 0;
359 if ((p = strchr(buf, '/')))
361 if ((p = strrchr(buf, ':')))
366 add->sin_port = htons(port);
367 if (!strcmp("@", buf))
369 add->sin_addr.s_addr = INADDR_ANY;
371 else if ((tmpadd = inet_addr(buf)) != -1)
373 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
375 else if ((hp = gethostbyname(buf)))
377 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
378 sizeof(struct in_addr));
387 void *tcpip_straddr(COMSTACK h, const char *str)
389 tcpip_state *sp = (tcpip_state *)h->cprivate;
390 const char *port = "210";
391 if (h->protocol == PROTO_HTTP)
397 freeaddrinfo(sp->ai);
398 sp->ai = tcpip_getaddrinfo(str, port);
399 if (sp->ai && h->state == CS_ST_UNBND)
402 struct addrinfo *ai = sp->ai;
403 for (; ai; ai = ai->ai_next)
405 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
413 if (!tcpip_set_blocking(h, h->flags))
419 void *tcpip_straddr(COMSTACK h, const char *str)
421 tcpip_state *sp = (tcpip_state *)h->cprivate;
423 if (h->protocol == PROTO_HTTP)
428 if (!tcpip_strtoaddr_ex(str, &sp->addr, port))
430 if (h->state == CS_ST_UNBND)
433 s = socket(AF_INET, SOCK_STREAM, 0);
438 if (!tcpip_set_blocking(h, h->flags))
445 int tcpip_more(COMSTACK h)
447 tcpip_state *sp = (tcpip_state *)h->cprivate;
449 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
453 * connect(2) will block (sometimes) - nothing we can do short of doing
454 * weird things like spawning subprocesses or threading or some weird junk
457 int tcpip_connect(COMSTACK h, void *address)
460 tcpip_state *sp = (tcpip_state *)h->cprivate;
462 struct sockaddr_in *add = (struct sockaddr_in *) address;
467 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
469 TRC(fprintf(stderr, "tcpip_connect\n"));
471 if (h->state != CS_ST_UNBND)
473 h->cerrno = CSOUTSTATE;
477 if (sp->ai != (struct addrinfo *) address)
479 h->cerrno = CSOUTSTATE;
484 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
485 * This gives the connect a chance to negotiate with the other side
488 if (getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
493 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
495 recbuflen *= 10; /* lets be optimistic */
496 if (setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
501 if (getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
506 TRC(fprintf(stderr, "New Size of TCP Receive Buffer = %d\n",
511 r = connect(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
512 freeaddrinfo(sp->ai);
515 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
520 if (WSAGetLastError() == WSAEWOULDBLOCK)
522 h->event = CS_CONNECT;
523 h->state = CS_ST_CONNECTING;
524 h->io_pending = CS_WANT_WRITE;
528 if (yaz_errno() == EINPROGRESS)
530 h->event = CS_CONNECT;
531 h->state = CS_ST_CONNECTING;
532 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
539 h->event = CS_CONNECT;
540 h->state = CS_ST_CONNECTING;
542 return tcpip_rcvconnect(h);
548 int tcpip_rcvconnect(COMSTACK h)
551 tcpip_state *sp = (tcpip_state *)h->cprivate;
553 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
555 if (h->state == CS_ST_DATAXFER)
557 if (h->state != CS_ST_CONNECTING)
559 h->cerrno = CSOUTSTATE;
563 if (h->type == ssl_type && !sp->ctx)
566 SSL_load_error_strings();
568 sp->ctx = sp->ctx_alloc = SSL_CTX_new(SSLv23_client_method());
571 h->cerrno = CSERRORSSL;
581 sp->ssl = SSL_new(sp->ctx);
582 SSL_set_fd(sp->ssl, h->iofile);
584 res = SSL_connect(sp->ssl);
587 if (ssl_check_error(h, sp, res))
594 h->state = CS_ST_DATAXFER;
598 #define CERTF "ztest.pem"
599 #define KEYF "ztest.pem"
601 static void tcpip_setsockopt(int fd)
607 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
609 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
611 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
613 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
615 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
617 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
622 static int tcpip_bind(COMSTACK h, void *address, int mode)
625 tcpip_state *sp = (tcpip_state *)h->cprivate;
628 struct sockaddr *addr = (struct sockaddr *)address;
637 if (sp->ai != (struct addrinfo *) address)
639 h->cerrno = CSOUTSTATE;
645 if (h->type == ssl_type && !sp->ctx)
648 SSL_load_error_strings();
650 sp->ctx = sp->ctx_alloc = SSL_CTX_new(SSLv23_server_method());
653 h->cerrno = CSERRORSSL;
662 res = SSL_CTX_use_certificate_file(sp->ctx, sp->cert_fname,
666 #if HAVE_OPENSSL_SSL_H
667 ERR_print_errors_fp(stderr);
669 fprintf(stderr, " SSL_CTX_use_certificate_file %s failed\n",
674 res = SSL_CTX_use_PrivateKey_file(sp->ctx, sp->cert_fname,
678 #if HAVE_OPENSSL_SSL_H
679 ERR_print_errors_fp(stderr);
681 fprintf(stderr, " SSL_CTX_use_certificate_file %s failed\n",
686 #if HAVE_OPENSSL_SSL_H
687 res = SSL_CTX_check_private_key(sp->ctx);
690 ERR_print_errors_fp(stderr);
695 TRC(fprintf(stderr, "ssl_bind\n"));
699 TRC(fprintf(stderr, "tcpip_bind\n"));
702 TRC(fprintf(stderr, "tcpip_bind\n"));
705 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
706 &one, sizeof(one)) < 0)
712 tcpip_setsockopt(h->iofile);
714 r = bind(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
715 freeaddrinfo(sp->ai);
718 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
725 /* Allow a maximum-sized backlog of waiting-to-connect clients */
726 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
731 h->state = CS_ST_IDLE;
732 h->event = CS_LISTEN;
736 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
737 int (*check_ip)(void *cd, const char *a, int len, int t),
740 struct sockaddr_in addr;
741 YAZ_SOCKLEN_T len = sizeof(addr);
743 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
744 if (h->state != CS_ST_IDLE)
746 h->cerrno = CSOUTSTATE;
749 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
754 WSAGetLastError() == WSAEWOULDBLOCK
756 yaz_errno() == EWOULDBLOCK
758 #if EAGAIN != EWOULDBLOCK
759 || yaz_errno() == EAGAIN
764 h->cerrno = CSNODATA;
768 shutdown(h->iofile, SD_RECEIVE);
770 shutdown(h->iofile, SHUT_RD);
772 listen(h->iofile, SOMAXCONN);
777 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
778 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
781 if (check_ip && (*check_ip)(cd, (const char *) &addr,
782 sizeof(addr), AF_INET))
786 closesocket(h->newfd);
793 h->state = CS_ST_INCON;
794 tcpip_setsockopt(h->newfd);
798 COMSTACK tcpip_accept(COMSTACK h)
801 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
803 unsigned long tru = 1;
806 TRC(fprintf(stderr, "tcpip_accept\n"));
807 if (h->state == CS_ST_INCON)
809 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
813 closesocket(h->newfd);
820 memcpy(cnew, h, sizeof(*h));
821 cnew->iofile = h->newfd;
822 cnew->io_pending = 0;
823 if (!(state = (tcpip_state *)
824 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
830 closesocket(h->newfd);
838 if (!tcpip_set_blocking(cnew, cnew->flags))
844 closesocket(h->newfd);
856 state->altsize = state->altlen = 0;
857 state->towrite = state->written = -1;
858 state->complete = st->complete;
862 cnew->state = CS_ST_ACCEPT;
863 h->state = CS_ST_IDLE;
866 state->ctx = st->ctx;
867 state->ctx_alloc = 0;
868 state->ssl = st->ssl;
871 state->ssl = SSL_new(state->ctx);
872 SSL_set_fd(state->ssl, cnew->iofile);
875 state->connect_request_buf = 0;
876 state->connect_response_buf = 0;
879 if (h->state == CS_ST_ACCEPT)
883 tcpip_state *state = (tcpip_state *)h->cprivate;
888 res = SSL_accept(state->ssl);
889 TRC(fprintf(stderr, "SSL_accept res=%d\n", res));
892 if (ssl_check_error(h, state, res))
899 TRC(fprintf(stderr, "SSL_accept complete\n"));
905 h->cerrno = CSOUTSTATE;
909 h->state = CS_ST_DATAXFER;
914 #define CS_TCPIP_BUFCHUNK 4096
917 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
918 * 0=connection closed.
920 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
922 tcpip_state *sp = (tcpip_state *)h->cprivate;
924 int tmpi, berlen, rest, req, tomove;
925 int hasread = 0, res;
927 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
928 if (sp->altlen) /* switch buffers */
930 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
931 (unsigned) sp->altbuf));
935 *bufsize = sp->altsize;
936 hasread = sp->altlen;
942 while (!(berlen = (*sp->complete)(*buf, hasread)))
946 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
952 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
953 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
960 /* unfortunatly, sun sometimes forgets to set errno in recv
961 when EWOULDBLOCK etc. would be required (res = -1) */
963 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
964 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
967 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
968 strerror(yaz_errno())));
970 if (WSAGetLastError() == WSAEWOULDBLOCK)
972 h->io_pending = CS_WANT_READ;
981 if (yaz_errno() == EWOULDBLOCK
983 #if EAGAIN != EWOULDBLOCK
984 || yaz_errno() == EAGAIN
987 || yaz_errno() == EINPROGRESS
989 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
993 h->io_pending = CS_WANT_READ;
996 else if (yaz_errno() == 0)
1000 h->cerrno = CSYSERR;
1008 if (hasread > h->max_recv_bytes)
1010 h->cerrno = CSBUFSIZE;
1014 TRC(fprintf(stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1016 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1017 if (hasread > berlen)
1019 tomove = req = hasread - berlen;
1020 rest = tomove % CS_TCPIP_BUFCHUNK;
1022 req += CS_TCPIP_BUFCHUNK - rest;
1025 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1027 h->cerrno = CSYSERR;
1030 } else if (sp->altsize < req)
1031 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1033 h->cerrno = CSYSERR;
1036 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1037 (unsigned) sp->altbuf));
1038 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1040 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1041 *(*buf + berlen) = '\0';
1042 return berlen ? berlen : 1;
1048 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
1049 * 0=connection closed.
1051 int ssl_get(COMSTACK h, char **buf, int *bufsize)
1053 tcpip_state *sp = (tcpip_state *)h->cprivate;
1055 int tmpi, berlen, rest, req, tomove;
1056 int hasread = 0, res;
1058 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
1059 if (sp->altlen) /* switch buffers */
1061 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
1062 (unsigned) sp->altbuf));
1066 *bufsize = sp->altsize;
1067 hasread = sp->altlen;
1073 while (!(berlen = (*sp->complete)(*buf, hasread)))
1077 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
1080 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
1081 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
1083 res = SSL_read(sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
1084 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
1087 if (ssl_check_error(h, sp, res))
1093 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1095 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1096 if (hasread > berlen)
1098 tomove = req = hasread - berlen;
1099 rest = tomove % CS_TCPIP_BUFCHUNK;
1101 req += CS_TCPIP_BUFCHUNK - rest;
1104 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1106 } else if (sp->altsize < req)
1107 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1109 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1110 (unsigned) sp->altbuf));
1111 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1113 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1114 *(*buf + berlen) = '\0';
1115 return berlen ? berlen : 1;
1120 * Returns 1, 0 or -1
1121 * In nonblocking mode, you must call again with same buffer while
1122 * return value is 1.
1124 int tcpip_put(COMSTACK h, char *buf, int size)
1127 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1129 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
1132 if (state->towrite < 0)
1134 state->towrite = size;
1137 else if (state->towrite != size)
1139 h->cerrno = CSWRONGBUF;
1142 while (state->towrite > state->written)
1145 send(h->iofile, buf + state->written, size -
1156 WSAGetLastError() == WSAEWOULDBLOCK
1158 yaz_errno() == EWOULDBLOCK
1160 #if EAGAIN != EWOULDBLOCK
1161 || yaz_errno() == EAGAIN
1165 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1167 || yaz_errno() == EINPROGRESS
1171 TRC(fprintf(stderr, " Flow control stop\n"));
1172 h->io_pending = CS_WANT_WRITE;
1175 h->cerrno = CSYSERR;
1178 state->written += res;
1179 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1180 res, state->written, size));
1182 state->towrite = state->written = -1;
1183 TRC(fprintf(stderr, " Ok\n"));
1190 * Returns 1, 0 or -1
1191 * In nonblocking mode, you must call again with same buffer while
1192 * return value is 1.
1194 int ssl_put(COMSTACK h, char *buf, int size)
1197 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1199 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1202 if (state->towrite < 0)
1204 state->towrite = size;
1207 else if (state->towrite != size)
1209 h->cerrno = CSWRONGBUF;
1212 while (state->towrite > state->written)
1214 res = SSL_write(state->ssl, buf + state->written,
1215 size - state->written);
1218 if (ssl_check_error(h, state, res))
1222 state->written += res;
1223 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1224 res, state->written, size));
1226 state->towrite = state->written = -1;
1227 TRC(fprintf(stderr, " Ok\n"));
1232 int tcpip_close(COMSTACK h)
1234 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1236 TRC(fprintf(stderr, "tcpip_close\n"));
1237 if (h->iofile != -1)
1242 SSL_shutdown(sp->ssl);
1246 closesocket(h->iofile);
1256 TRC(fprintf(stderr, "SSL_free\n"));
1261 SSL_CTX_free(sp->ctx_alloc);
1263 #if HAVE_GETADDRINFO
1265 freeaddrinfo(sp->ai);
1267 xfree(sp->connect_request_buf);
1268 xfree(sp->connect_response_buf);
1274 char *tcpip_addrstr(COMSTACK h)
1276 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1277 char *r = 0, *buf = sp->buf;
1279 #if HAVE_GETADDRINFO
1281 struct sockaddr_storage addr;
1282 YAZ_SOCKLEN_T len = sizeof(addr);
1284 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1286 h->cerrno = CSYSERR;
1289 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
1291 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1300 struct sockaddr_in addr;
1301 YAZ_SOCKLEN_T len = sizeof(addr);
1302 struct hostent *host;
1304 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1306 h->cerrno = CSYSERR;
1309 if (!(h->flags & CS_FLAGS_NUMERICHOST))
1311 if ((host = gethostbyaddr((char*)&addr.sin_addr,
1312 sizeof(addr.sin_addr),
1314 r = (char*) host->h_name;
1317 r = inet_ntoa(addr.sin_addr);
1320 if (h->protocol == PROTO_HTTP)
1321 sprintf(buf, "http:%s", r);
1323 sprintf(buf, "tcp:%s", r);
1327 if (h->protocol == PROTO_HTTP)
1328 sprintf(buf, "https:%s", r);
1330 sprintf(buf, "ssl:%s", r);
1336 int static tcpip_set_blocking(COMSTACK p, int flags)
1341 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1342 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1345 flag = fcntl(p->iofile, F_GETFL, 0);
1346 if (flags & CS_FLAGS_BLOCKING)
1347 flag = flag & ~O_NONBLOCK; /* blocking */
1350 flag = flag | O_NONBLOCK; /* non-blocking */
1351 signal(SIGPIPE, SIG_IGN);
1353 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1360 void cs_print_session_info(COMSTACK cs)
1363 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
1364 SSL *ssl = (SSL *) sp->ssl;
1367 gnutls_session_t session = ssl->gnutls_state;
1368 if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509)
1370 printf("X509 certificate\n");
1373 #if HAVE_OPENSSL_SSL_H
1374 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
1375 SSL *ssl = (SSL *) sp->ssl;
1378 X509 *server_cert = SSL_get_peer_certificate(ssl);
1384 BIO *bio = BIO_new(BIO_s_mem());
1386 /* get PEM buffer in memory */
1387 PEM_write_bio_X509(bio, server_cert);
1388 pem_len = BIO_get_mem_data(bio, &pem_buf);
1389 fwrite(pem_buf, pem_len, 1, stdout);
1391 /* print all info on screen .. */
1392 X509_print_fp(stdout, server_cert);
1395 X509_free(server_cert);
1401 void *cs_get_ssl(COMSTACK cs)
1403 #if HAVE_OPENSSL_SSL_H
1404 struct tcpip_state *sp;
1405 if (!cs || cs->type != ssl_type)
1407 sp = (struct tcpip_state *) cs->cprivate;
1415 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1417 struct tcpip_state *sp;
1418 if (!cs || cs->type != ssl_type)
1420 sp = (struct tcpip_state *) cs->cprivate;
1423 sp->ctx = (SSL_CTX *) ctx;
1427 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1429 struct tcpip_state *sp;
1430 if (!cs || cs->type != ssl_type)
1432 sp = (struct tcpip_state *) cs->cprivate;
1433 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1434 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1438 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1440 SSL *ssl = (SSL *) cs_get_ssl(cs);
1443 #if HAVE_OPENSSL_SSL_H
1444 X509 *server_cert = SSL_get_peer_certificate(ssl);
1447 BIO *bio = BIO_new(BIO_s_mem());
1449 /* get PEM buffer in memory */
1450 PEM_write_bio_X509(bio, server_cert);
1451 *len = BIO_get_mem_data(bio, &pem_buf);
1452 *buf = (char *) xmalloc(*len);
1453 memcpy(*buf, pem_buf, *len);
1462 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1467 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1472 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1479 static int tcpip_put_connect(COMSTACK h, char *buf, int size)
1481 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1483 int r = tcpip_put(h, state->connect_request_buf,
1484 state->connect_request_len);
1488 h->f_put = tcpip_put; /* switch to normal tcpip put */
1489 r = tcpip_put(h, buf, size);
1494 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize)
1496 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1499 r = tcpip_get(h, &state->connect_response_buf,
1500 &state->connect_response_len);
1503 /* got the connect response completely */
1504 state->complete = cs_complete_auto; /* switch to normal tcpip get */
1505 h->f_get = tcpip_get;
1506 return tcpip_get(h, buf, bufsize);
1513 * indent-tabs-mode: nil
1515 * vim: shiftwidth=4 tabstop=8 expandtab