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 */
244 COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
252 p = tcpip_type(s, flags, protocol, 0);
258 sp = (tcpip_state *) p->cprivate;
260 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
262 /* note: we don't handle already opened socket in SSL mode - yet */
268 static int ssl_check_error(COMSTACK h, tcpip_state *sp, int res)
270 #if HAVE_OPENSSL_SSL_H
271 int err = SSL_get_error(sp->ssl, res);
272 TRC(fprintf(stderr, "got err=%d\n", err));
273 if (err == SSL_ERROR_WANT_READ)
275 TRC(fprintf(stderr, " -> SSL_ERROR_WANT_READ\n"));
276 h->io_pending = CS_WANT_READ;
279 if (err == SSL_ERROR_WANT_WRITE)
281 TRC(fprintf(stderr, " -> SSL_ERROR_WANT_WRITE\n"));
282 h->io_pending = CS_WANT_WRITE;
286 int tls_error = sp->ssl->last_error;
287 TRC(fprintf(stderr, "ssl_check_error error=%d fatal=%d msg=%s\n",
289 gnutls_error_is_fatal(tls_error),
290 gnutls_strerror(tls_error)));
291 if (tls_error == GNUTLS_E_AGAIN || tls_error == GNUTLS_E_INTERRUPTED)
293 int dir = gnutls_record_get_direction(sp->ssl->gnutls_state);
294 TRC(fprintf(stderr, " -> incomplete dir=%d\n", dir));
295 h->io_pending = dir ? CS_WANT_WRITE : CS_WANT_READ;
299 h->cerrno = CSERRORSSL;
305 /* resolve using getaddrinfo */
306 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
308 struct addrinfo hints, *res;
313 hints.ai_family = AF_UNSPEC;
314 hints.ai_socktype = SOCK_STREAM;
315 hints.ai_protocol = 0;
316 hints.ai_addrlen = 0;
317 hints.ai_addr = NULL;
318 hints.ai_canonname = NULL;
319 hints.ai_next = NULL;
321 strncpy(host, str, sizeof(host)-1);
322 host[sizeof(host)-1] = 0;
323 if ((p = strchr(host, '/')))
325 if ((p = strrchr(host, ':')))
331 if (!strcmp("@", host))
333 hints.ai_flags = AI_PASSIVE;
334 error = getaddrinfo(0, port, &hints, &res);
338 error = getaddrinfo(host, port, &hints, &res);
346 /* gethostbyname .. old systems */
347 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
352 short int port = default_port;
354 unsigned long tmpadd;
358 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
359 add->sin_family = AF_INET;
360 strncpy(buf, str, sizeof(buf)-1);
361 buf[sizeof(buf)-1] = 0;
362 if ((p = strchr(buf, '/')))
364 if ((p = strrchr(buf, ':')))
369 add->sin_port = htons(port);
370 if (!strcmp("@", buf))
372 add->sin_addr.s_addr = INADDR_ANY;
374 else if ((tmpadd = inet_addr(buf)) != -1)
376 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
378 else if ((hp = gethostbyname(buf)))
380 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
381 sizeof(struct in_addr));
390 void *tcpip_straddr(COMSTACK h, const char *str)
392 tcpip_state *sp = (tcpip_state *)h->cprivate;
393 const char *port = "210";
394 if (h->protocol == PROTO_HTTP)
400 freeaddrinfo(sp->ai);
401 sp->ai = tcpip_getaddrinfo(str, port);
402 if (sp->ai && h->state == CS_ST_UNBND)
405 struct addrinfo *ai = sp->ai;
406 for (; ai; ai = ai->ai_next)
408 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
416 if (!tcpip_set_blocking(h, h->flags))
422 void *tcpip_straddr(COMSTACK h, const char *str)
424 tcpip_state *sp = (tcpip_state *)h->cprivate;
426 if (h->protocol == PROTO_HTTP)
431 if (!tcpip_strtoaddr_ex(str, &sp->addr, port))
433 if (h->state == CS_ST_UNBND)
436 s = socket(AF_INET, SOCK_STREAM, 0);
441 if (!tcpip_set_blocking(h, h->flags))
448 int tcpip_more(COMSTACK h)
450 tcpip_state *sp = (tcpip_state *)h->cprivate;
452 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
456 * connect(2) will block (sometimes) - nothing we can do short of doing
457 * weird things like spawning subprocesses or threading or some weird junk
460 int tcpip_connect(COMSTACK h, void *address)
463 tcpip_state *sp = (tcpip_state *)h->cprivate;
465 struct sockaddr_in *add = (struct sockaddr_in *) address;
470 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
472 TRC(fprintf(stderr, "tcpip_connect\n"));
474 if (h->state != CS_ST_UNBND)
476 h->cerrno = CSOUTSTATE;
480 if (sp->ai != (struct addrinfo *) address)
482 h->cerrno = CSOUTSTATE;
487 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
488 * This gives the connect a chance to negotiate with the other side
491 if (getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
496 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
498 recbuflen *= 10; /* lets be optimistic */
499 if (setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
504 if (getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
509 TRC(fprintf(stderr, "New Size of TCP Receive Buffer = %d\n",
514 r = connect(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
515 freeaddrinfo(sp->ai);
518 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
523 if (WSAGetLastError() == WSAEWOULDBLOCK)
525 h->event = CS_CONNECT;
526 h->state = CS_ST_CONNECTING;
527 h->io_pending = CS_WANT_WRITE;
531 if (yaz_errno() == EINPROGRESS)
533 h->event = CS_CONNECT;
534 h->state = CS_ST_CONNECTING;
535 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
542 h->event = CS_CONNECT;
543 h->state = CS_ST_CONNECTING;
545 return tcpip_rcvconnect(h);
551 int tcpip_rcvconnect(COMSTACK h)
554 tcpip_state *sp = (tcpip_state *)h->cprivate;
556 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
558 if (h->state == CS_ST_DATAXFER)
560 if (h->state != CS_ST_CONNECTING)
562 h->cerrno = CSOUTSTATE;
566 if (h->type == ssl_type && !sp->ctx)
569 SSL_load_error_strings();
571 sp->ctx = sp->ctx_alloc = SSL_CTX_new(SSLv23_client_method());
574 h->cerrno = CSERRORSSL;
584 sp->ssl = SSL_new(sp->ctx);
585 SSL_set_fd(sp->ssl, h->iofile);
587 res = SSL_connect(sp->ssl);
590 if (ssl_check_error(h, sp, res))
597 h->state = CS_ST_DATAXFER;
601 #define CERTF "ztest.pem"
602 #define KEYF "ztest.pem"
604 static void tcpip_setsockopt(int fd)
610 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
612 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
614 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
616 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
618 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
620 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
625 static int tcpip_bind(COMSTACK h, void *address, int mode)
628 tcpip_state *sp = (tcpip_state *)h->cprivate;
631 struct sockaddr *addr = (struct sockaddr *)address;
640 if (sp->ai != (struct addrinfo *) address)
642 h->cerrno = CSOUTSTATE;
648 if (h->type == ssl_type && !sp->ctx)
651 SSL_load_error_strings();
653 sp->ctx = sp->ctx_alloc = SSL_CTX_new(SSLv23_server_method());
656 h->cerrno = CSERRORSSL;
665 res = SSL_CTX_use_certificate_file(sp->ctx, sp->cert_fname,
669 #if HAVE_OPENSSL_SSL_H
670 ERR_print_errors_fp(stderr);
672 fprintf(stderr, " SSL_CTX_use_certificate_file %s failed\n",
677 res = SSL_CTX_use_PrivateKey_file(sp->ctx, sp->cert_fname,
681 #if HAVE_OPENSSL_SSL_H
682 ERR_print_errors_fp(stderr);
684 fprintf(stderr, " SSL_CTX_use_certificate_file %s failed\n",
689 #if HAVE_OPENSSL_SSL_H
690 res = SSL_CTX_check_private_key(sp->ctx);
693 ERR_print_errors_fp(stderr);
698 TRC(fprintf(stderr, "ssl_bind\n"));
702 TRC(fprintf(stderr, "tcpip_bind\n"));
705 TRC(fprintf(stderr, "tcpip_bind\n"));
708 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
709 &one, sizeof(one)) < 0)
715 tcpip_setsockopt(h->iofile);
717 r = bind(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
718 freeaddrinfo(sp->ai);
721 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
728 /* Allow a maximum-sized backlog of waiting-to-connect clients */
729 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
734 h->state = CS_ST_IDLE;
735 h->event = CS_LISTEN;
739 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
740 int (*check_ip)(void *cd, const char *a, int len, int t),
743 struct sockaddr_in addr;
744 YAZ_SOCKLEN_T len = sizeof(addr);
746 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
747 if (h->state != CS_ST_IDLE)
749 h->cerrno = CSOUTSTATE;
752 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
757 WSAGetLastError() == WSAEWOULDBLOCK
759 yaz_errno() == EWOULDBLOCK
761 #if EAGAIN != EWOULDBLOCK
762 || yaz_errno() == EAGAIN
767 h->cerrno = CSNODATA;
771 shutdown(h->iofile, SD_RECEIVE);
773 shutdown(h->iofile, SHUT_RD);
775 listen(h->iofile, SOMAXCONN);
780 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
781 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
784 if (check_ip && (*check_ip)(cd, (const char *) &addr,
785 sizeof(addr), AF_INET))
789 closesocket(h->newfd);
796 h->state = CS_ST_INCON;
797 tcpip_setsockopt(h->newfd);
801 COMSTACK tcpip_accept(COMSTACK h)
804 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
806 unsigned long tru = 1;
809 TRC(fprintf(stderr, "tcpip_accept\n"));
810 if (h->state == CS_ST_INCON)
812 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
816 closesocket(h->newfd);
823 memcpy(cnew, h, sizeof(*h));
824 cnew->iofile = h->newfd;
825 cnew->io_pending = 0;
826 if (!(state = (tcpip_state *)
827 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
833 closesocket(h->newfd);
841 if (!tcpip_set_blocking(cnew, cnew->flags))
847 closesocket(h->newfd);
859 state->altsize = state->altlen = 0;
860 state->towrite = state->written = -1;
861 state->complete = st->complete;
865 cnew->state = CS_ST_ACCEPT;
866 h->state = CS_ST_IDLE;
869 state->ctx = st->ctx;
870 state->ctx_alloc = 0;
871 state->ssl = st->ssl;
874 state->ssl = SSL_new(state->ctx);
875 SSL_set_fd(state->ssl, cnew->iofile);
878 state->connect_request_buf = 0;
879 state->connect_response_buf = 0;
882 if (h->state == CS_ST_ACCEPT)
886 tcpip_state *state = (tcpip_state *)h->cprivate;
891 res = SSL_accept(state->ssl);
892 TRC(fprintf(stderr, "SSL_accept res=%d\n", res));
895 if (ssl_check_error(h, state, res))
902 TRC(fprintf(stderr, "SSL_accept complete\n"));
908 h->cerrno = CSOUTSTATE;
912 h->state = CS_ST_DATAXFER;
917 #define CS_TCPIP_BUFCHUNK 4096
920 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
921 * 0=connection closed.
923 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
925 tcpip_state *sp = (tcpip_state *)h->cprivate;
927 int tmpi, berlen, rest, req, tomove;
928 int hasread = 0, res;
930 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
931 if (sp->altlen) /* switch buffers */
933 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
934 (unsigned) sp->altbuf));
938 *bufsize = sp->altsize;
939 hasread = sp->altlen;
945 while (!(berlen = (*sp->complete)(*buf, hasread)))
949 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
955 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
956 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
963 /* unfortunatly, sun sometimes forgets to set errno in recv
964 when EWOULDBLOCK etc. would be required (res = -1) */
966 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
967 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
970 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
971 strerror(yaz_errno())));
973 if (WSAGetLastError() == WSAEWOULDBLOCK)
975 h->io_pending = CS_WANT_READ;
984 if (yaz_errno() == EWOULDBLOCK
986 #if EAGAIN != EWOULDBLOCK
987 || yaz_errno() == EAGAIN
990 || yaz_errno() == EINPROGRESS
992 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
996 h->io_pending = CS_WANT_READ;
999 else if (yaz_errno() == 0)
1003 h->cerrno = CSYSERR;
1011 if (hasread > h->max_recv_bytes)
1013 h->cerrno = CSBUFSIZE;
1017 TRC(fprintf(stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1019 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1020 if (hasread > berlen)
1022 tomove = req = hasread - berlen;
1023 rest = tomove % CS_TCPIP_BUFCHUNK;
1025 req += CS_TCPIP_BUFCHUNK - rest;
1028 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1030 h->cerrno = CSYSERR;
1033 } else if (sp->altsize < req)
1034 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1036 h->cerrno = CSYSERR;
1039 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1040 (unsigned) sp->altbuf));
1041 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1043 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1044 *(*buf + berlen) = '\0';
1045 return berlen ? berlen : 1;
1051 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
1052 * 0=connection closed.
1054 int ssl_get(COMSTACK h, char **buf, int *bufsize)
1056 tcpip_state *sp = (tcpip_state *)h->cprivate;
1058 int tmpi, berlen, rest, req, tomove;
1059 int hasread = 0, res;
1061 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
1062 if (sp->altlen) /* switch buffers */
1064 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
1065 (unsigned) sp->altbuf));
1069 *bufsize = sp->altsize;
1070 hasread = sp->altlen;
1076 while (!(berlen = (*sp->complete)(*buf, hasread)))
1080 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
1083 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
1084 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
1086 res = SSL_read(sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
1087 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
1090 if (ssl_check_error(h, sp, res))
1096 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1098 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1099 if (hasread > berlen)
1101 tomove = req = hasread - berlen;
1102 rest = tomove % CS_TCPIP_BUFCHUNK;
1104 req += CS_TCPIP_BUFCHUNK - rest;
1107 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1109 } else if (sp->altsize < req)
1110 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1112 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1113 (unsigned) sp->altbuf));
1114 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1116 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1117 *(*buf + berlen) = '\0';
1118 return berlen ? berlen : 1;
1123 * Returns 1, 0 or -1
1124 * In nonblocking mode, you must call again with same buffer while
1125 * return value is 1.
1127 int tcpip_put(COMSTACK h, char *buf, int size)
1130 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1132 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
1135 if (state->towrite < 0)
1137 state->towrite = size;
1140 else if (state->towrite != size)
1142 h->cerrno = CSWRONGBUF;
1145 while (state->towrite > state->written)
1148 send(h->iofile, buf + state->written, size -
1159 WSAGetLastError() == WSAEWOULDBLOCK
1161 yaz_errno() == EWOULDBLOCK
1163 #if EAGAIN != EWOULDBLOCK
1164 || yaz_errno() == EAGAIN
1168 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1170 || yaz_errno() == EINPROGRESS
1174 TRC(fprintf(stderr, " Flow control stop\n"));
1175 h->io_pending = CS_WANT_WRITE;
1178 h->cerrno = CSYSERR;
1181 state->written += res;
1182 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1183 res, state->written, size));
1185 state->towrite = state->written = -1;
1186 TRC(fprintf(stderr, " Ok\n"));
1193 * Returns 1, 0 or -1
1194 * In nonblocking mode, you must call again with same buffer while
1195 * return value is 1.
1197 int ssl_put(COMSTACK h, char *buf, int size)
1200 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1202 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1205 if (state->towrite < 0)
1207 state->towrite = size;
1210 else if (state->towrite != size)
1212 h->cerrno = CSWRONGBUF;
1215 while (state->towrite > state->written)
1217 res = SSL_write(state->ssl, buf + state->written,
1218 size - state->written);
1221 if (ssl_check_error(h, state, res))
1225 state->written += res;
1226 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1227 res, state->written, size));
1229 state->towrite = state->written = -1;
1230 TRC(fprintf(stderr, " Ok\n"));
1235 int tcpip_close(COMSTACK h)
1237 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1239 TRC(fprintf(stderr, "tcpip_close\n"));
1240 if (h->iofile != -1)
1245 SSL_shutdown(sp->ssl);
1249 closesocket(h->iofile);
1259 TRC(fprintf(stderr, "SSL_free\n"));
1264 SSL_CTX_free(sp->ctx_alloc);
1266 #if HAVE_GETADDRINFO
1268 freeaddrinfo(sp->ai);
1270 xfree(sp->connect_request_buf);
1271 xfree(sp->connect_response_buf);
1277 char *tcpip_addrstr(COMSTACK h)
1279 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1280 char *r = 0, *buf = sp->buf;
1282 #if HAVE_GETADDRINFO
1284 struct sockaddr_storage addr;
1285 YAZ_SOCKLEN_T len = sizeof(addr);
1287 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1289 h->cerrno = CSYSERR;
1292 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
1294 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1303 struct sockaddr_in addr;
1304 YAZ_SOCKLEN_T len = sizeof(addr);
1305 struct hostent *host;
1307 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1309 h->cerrno = CSYSERR;
1312 if (!(h->flags & CS_FLAGS_NUMERICHOST))
1314 if ((host = gethostbyaddr((char*)&addr.sin_addr,
1315 sizeof(addr.sin_addr),
1317 r = (char*) host->h_name;
1320 r = inet_ntoa(addr.sin_addr);
1323 if (h->protocol == PROTO_HTTP)
1324 sprintf(buf, "http:%s", r);
1326 sprintf(buf, "tcp:%s", r);
1330 if (h->protocol == PROTO_HTTP)
1331 sprintf(buf, "https:%s", r);
1333 sprintf(buf, "ssl:%s", r);
1339 int static tcpip_set_blocking(COMSTACK p, int flags)
1344 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1345 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1348 flag = fcntl(p->iofile, F_GETFL, 0);
1349 if (flags & CS_FLAGS_BLOCKING)
1350 flag = flag & ~O_NONBLOCK; /* blocking */
1353 flag = flag | O_NONBLOCK; /* non-blocking */
1354 signal(SIGPIPE, SIG_IGN);
1356 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1363 void cs_print_session_info(COMSTACK cs)
1366 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
1367 SSL *ssl = (SSL *) sp->ssl;
1370 gnutls_session_t session = ssl->gnutls_state;
1371 if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509)
1373 printf("X509 certificate\n");
1376 #if HAVE_OPENSSL_SSL_H
1377 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
1378 SSL *ssl = (SSL *) sp->ssl;
1381 X509 *server_cert = SSL_get_peer_certificate(ssl);
1387 BIO *bio = BIO_new(BIO_s_mem());
1389 /* get PEM buffer in memory */
1390 PEM_write_bio_X509(bio, server_cert);
1391 pem_len = BIO_get_mem_data(bio, &pem_buf);
1392 fwrite(pem_buf, pem_len, 1, stdout);
1394 /* print all info on screen .. */
1395 X509_print_fp(stdout, server_cert);
1398 X509_free(server_cert);
1404 void *cs_get_ssl(COMSTACK cs)
1406 #if HAVE_OPENSSL_SSL_H
1407 struct tcpip_state *sp;
1408 if (!cs || cs->type != ssl_type)
1410 sp = (struct tcpip_state *) cs->cprivate;
1418 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1420 struct tcpip_state *sp;
1421 if (!cs || cs->type != ssl_type)
1423 sp = (struct tcpip_state *) cs->cprivate;
1426 sp->ctx = (SSL_CTX *) ctx;
1430 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1432 struct tcpip_state *sp;
1433 if (!cs || cs->type != ssl_type)
1435 sp = (struct tcpip_state *) cs->cprivate;
1436 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1437 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1441 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1443 SSL *ssl = (SSL *) cs_get_ssl(cs);
1446 #if HAVE_OPENSSL_SSL_H
1447 X509 *server_cert = SSL_get_peer_certificate(ssl);
1450 BIO *bio = BIO_new(BIO_s_mem());
1452 /* get PEM buffer in memory */
1453 PEM_write_bio_X509(bio, server_cert);
1454 *len = BIO_get_mem_data(bio, &pem_buf);
1455 *buf = (char *) xmalloc(*len);
1456 memcpy(*buf, pem_buf, *len);
1465 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1470 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1475 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1482 static int tcpip_put_connect(COMSTACK h, char *buf, int size)
1484 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1486 int r = tcpip_put(h, state->connect_request_buf,
1487 state->connect_request_len);
1491 h->f_put = tcpip_put; /* switch to normal tcpip put */
1492 r = tcpip_put(h, buf, size);
1497 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize)
1499 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1502 r = tcpip_get(h, &state->connect_response_buf,
1503 &state->connect_response_len);
1506 /* got the connect response completely */
1507 state->complete = cs_complete_auto; /* switch to normal tcpip get */
1508 h->f_get = tcpip_get;
1509 return tcpip_get(h, buf, bufsize);
1516 * indent-tabs-mode: nil
1518 * vim: shiftwidth=4 tabstop=8 expandtab