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>
56 #if HAVE_OPENSSL_SSL_H
57 #include <openssl/ssl.h>
58 #include <openssl/err.h>
62 #include <yaz/comstack.h>
63 #include <yaz/tcpip.h>
66 static int tcpip_close(COMSTACK h);
67 static int tcpip_put(COMSTACK h, char *buf, int size);
68 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
69 static int tcpip_put_connect(COMSTACK h, char *buf, int size);
70 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize);
71 static int tcpip_connect(COMSTACK h, void *address);
72 static int tcpip_more(COMSTACK h);
73 static int tcpip_rcvconnect(COMSTACK h);
74 static int tcpip_bind(COMSTACK h, void *address, int mode);
75 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
76 int (*check_ip)(void *cd, const char *a, int len, int type),
78 static int tcpip_set_blocking(COMSTACK p, int blocking);
81 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
82 static int ssl_put(COMSTACK h, char *buf, int size);
85 static COMSTACK tcpip_accept(COMSTACK h);
86 static char *tcpip_addrstr(COMSTACK h);
87 static void *tcpip_straddr(COMSTACK h, const char *str);
96 #define YAZ_SOCKLEN_T int
99 /* this state is used for both SSL and straight TCP/IP */
100 typedef struct tcpip_state
102 char *altbuf; /* alternate buffer for surplus data */
103 int altsize; /* size as xmalloced */
104 int altlen; /* length of data or 0 if none */
106 int written; /* -1 if we aren't writing */
107 int towrite; /* to verify against user input */
108 int (*complete)(const char *buf, int len); /* length/complete. */
112 struct sockaddr_in addr; /* returned by cs_straddr */
114 char buf[128]; /* returned by cs_addrstr */
116 SSL_CTX *ctx; /* current CTX. */
117 SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
119 char cert_fname[256];
121 char *connect_request_buf;
122 int connect_request_len;
123 char *connect_response_buf;
124 int connect_response_len;
128 static int tcpip_init (void)
130 static int initialized = 0;
136 requested = MAKEWORD(1, 1);
137 if (WSAStartup(requested, &wd))
144 static int tcpip_init (void)
151 * This function is always called through the cs_create() macro.
152 * s >= 0: socket has already been established for us.
154 COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
161 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
163 if (!(sp = (struct tcpip_state *)(p->cprivate =
164 xmalloc(sizeof(tcpip_state)))))
171 p->type = tcpip_type;
172 p->protocol = (enum oid_proto) protocol;
174 p->f_connect = tcpip_connect;
175 p->f_rcvconnect = tcpip_rcvconnect;
176 p->f_get = tcpip_get;
177 p->f_put = tcpip_put;
178 p->f_close = tcpip_close;
179 p->f_more = tcpip_more;
180 p->f_bind = tcpip_bind;
181 p->f_listen = tcpip_listen;
182 p->f_accept = tcpip_accept;
183 p->f_addrstr = tcpip_addrstr;
184 p->f_straddr = tcpip_straddr;
185 p->f_set_blocking = tcpip_set_blocking;
186 p->max_recv_bytes = 5000000;
188 p->state = s < 0 ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
195 sp->ctx = sp->ctx_alloc = 0;
197 strcpy(sp->cert_fname, "yaz.pem");
204 sp->altsize = sp->altlen = 0;
205 sp->towrite = sp->written = -1;
206 if (protocol == PROTO_WAIS)
207 sp->complete = completeWAIS;
209 sp->complete = cs_complete_auto;
211 sp->connect_request_buf = 0;
212 sp->connect_request_len = 0;
213 sp->connect_response_buf = 0;
214 sp->connect_response_len = 0;
216 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
217 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
222 COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
223 const char *connect_host)
225 COMSTACK p = tcpip_type(s, flags, protocol, 0);
230 tcpip_state *sp = (tcpip_state *) p->cprivate;
231 sp->connect_request_buf = (char *) xmalloc(strlen(connect_host) + 30);
232 sprintf(sp->connect_request_buf, "CONNECT %s HTTP/1.0\r\n\r\n",
234 sp->connect_request_len = strlen(sp->connect_request_buf);
235 p->f_put = tcpip_put_connect;
236 p->f_get = tcpip_get_connect;
237 sp->complete = cs_complete_auto_head; /* only want HTTP header */
245 COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
250 p = tcpip_type (s, flags, protocol, 0);
256 sp = (tcpip_state *) p->cprivate;
258 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
260 /* note: we don't handle already opened socket in SSL mode - yet */
266 /* resolve using getaddrinfo */
267 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
269 struct addrinfo hints, *res;
274 hints.ai_family = AF_UNSPEC;
275 hints.ai_socktype = SOCK_STREAM;
276 hints.ai_protocol = 0;
277 hints.ai_addrlen = 0;
278 hints.ai_addr = NULL;
279 hints.ai_canonname = NULL;
280 hints.ai_next = NULL;
282 strncpy(host, str, sizeof(host)-1);
283 host[sizeof(host)-1] = 0;
284 if ((p = strchr(host, '/')))
286 if ((p = strrchr(host, ':')))
292 if (!strcmp("@", host))
294 hints.ai_flags = AI_PASSIVE;
295 error = getaddrinfo(0, port, &hints, &res);
299 error = getaddrinfo(host, port, &hints, &res);
307 /* gethostbyname .. old systems */
308 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
313 short int port = default_port;
315 unsigned long tmpadd;
319 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
320 add->sin_family = AF_INET;
321 strncpy(buf, str, sizeof(buf)-1);
322 buf[sizeof(buf)-1] = 0;
323 if ((p = strchr(buf, '/')))
325 if ((p = strrchr(buf, ':')))
330 add->sin_port = htons(port);
331 if (!strcmp("@", buf))
333 add->sin_addr.s_addr = INADDR_ANY;
335 else if ((tmpadd = inet_addr(buf)) != -1)
337 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
339 else if ((hp = gethostbyname(buf)))
341 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
342 sizeof(struct in_addr));
351 void *tcpip_straddr(COMSTACK h, const char *str)
353 tcpip_state *sp = (tcpip_state *)h->cprivate;
354 const char *port = "210";
355 if (h->protocol == PROTO_HTTP)
361 freeaddrinfo(sp->ai);
362 sp->ai = tcpip_getaddrinfo(str, port);
363 if (sp->ai && h->state == CS_ST_UNBND)
366 struct addrinfo *ai = sp->ai;
367 for (; ai; ai = ai->ai_next)
369 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
377 if (!tcpip_set_blocking(h, h->flags))
383 void *tcpip_straddr(COMSTACK h, const char *str)
385 tcpip_state *sp = (tcpip_state *)h->cprivate;
387 if (h->protocol == PROTO_HTTP)
392 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
394 if (h->state == CS_ST_UNBND)
397 s = socket(AF_INET, SOCK_STREAM, 0);
402 if (!tcpip_set_blocking(h, h->flags))
409 int tcpip_more(COMSTACK h)
411 tcpip_state *sp = (tcpip_state *)h->cprivate;
413 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
417 * connect(2) will block (sometimes) - nothing we can do short of doing
418 * weird things like spawning subprocesses or threading or some weird junk
421 int tcpip_connect(COMSTACK h, void *address)
424 tcpip_state *sp = (tcpip_state *)h->cprivate;
426 struct sockaddr_in *add = (struct sockaddr_in *) address;
431 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
433 TRC(fprintf(stderr, "tcpip_connect\n"));
435 if (h->state != CS_ST_UNBND)
437 h->cerrno = CSOUTSTATE;
441 if (sp->ai != (struct addrinfo *) address)
443 h->cerrno = CSOUTSTATE;
448 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
449 * This gives the connect a chance to negotiate with the other side
452 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
457 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
459 recbuflen *= 10; /* lets be optimistic */
460 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
465 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
470 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
475 r = connect(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
476 freeaddrinfo(sp->ai);
479 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
484 if (WSAGetLastError() == WSAEWOULDBLOCK)
486 h->event = CS_CONNECT;
487 h->state = CS_ST_CONNECTING;
488 h->io_pending = CS_WANT_WRITE;
492 if (yaz_errno() == EINPROGRESS)
494 h->event = CS_CONNECT;
495 h->state = CS_ST_CONNECTING;
496 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
503 h->event = CS_CONNECT;
504 h->state = CS_ST_CONNECTING;
506 return tcpip_rcvconnect (h);
512 int tcpip_rcvconnect(COMSTACK h)
515 tcpip_state *sp = (tcpip_state *)h->cprivate;
517 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
519 if (h->state == CS_ST_DATAXFER)
521 if (h->state != CS_ST_CONNECTING)
523 h->cerrno = CSOUTSTATE;
527 if (h->type == ssl_type && !sp->ctx)
530 SSL_load_error_strings();
532 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_client_method());
535 h->cerrno = CSERRORSSL;
545 sp->ssl = SSL_new (sp->ctx);
546 SSL_set_fd (sp->ssl, h->iofile);
548 res = SSL_connect(sp->ssl);
549 #if HAVE_OPENSSL_SSL_H
552 int err = SSL_get_error(sp->ssl, res);
553 if (err == SSL_ERROR_WANT_READ)
555 h->io_pending = CS_WANT_READ;
558 if (err == SSL_ERROR_WANT_WRITE)
560 h->io_pending = CS_WANT_WRITE;
563 h->cerrno = CSERRORSSL;
567 TRC(fprintf(stderr, "SSL_connect res=%d last_error=%d\n",
568 res, sp->ssl->last_error));
569 if (res == 0 && sp->ssl->last_error == GNUTLS_E_AGAIN)
571 h->io_pending = CS_WANT_READ;
576 h->cerrno = CSERRORSSL;
583 h->state = CS_ST_DATAXFER;
587 #define CERTF "ztest.pem"
588 #define KEYF "ztest.pem"
590 static void tcpip_setsockopt (int fd)
596 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
598 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
600 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
602 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
604 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
606 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
611 static int tcpip_bind(COMSTACK h, void *address, int mode)
614 tcpip_state *sp = (tcpip_state *)h->cprivate;
617 struct sockaddr *addr = (struct sockaddr *)address;
626 if (sp->ai != (struct addrinfo *) address)
628 h->cerrno = CSOUTSTATE;
634 if (h->type == ssl_type && !sp->ctx)
637 SSL_load_error_strings();
639 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_server_method());
642 h->cerrno = CSERRORSSL;
651 res = SSL_CTX_use_certificate_file(sp->ctx, sp->cert_fname,
655 #if HAVE_OPENSSL_SSL_H
656 ERR_print_errors_fp(stderr);
658 fprintf(stderr, " SSL_CTX_use_certificate_file %s failed\n",
663 res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
667 #if HAVE_OPENSSL_SSL_H
668 ERR_print_errors_fp(stderr);
670 fprintf(stderr, " SSL_CTX_use_certificate_file %s failed\n",
675 #if HAVE_OPENSSL_SSL_H
676 res = SSL_CTX_check_private_key (sp->ctx);
679 ERR_print_errors_fp(stderr);
684 TRC (fprintf (stderr, "ssl_bind\n"));
688 TRC (fprintf (stderr, "tcpip_bind\n"));
691 TRC (fprintf (stderr, "tcpip_bind\n"));
694 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
695 &one, sizeof(one)) < 0)
701 tcpip_setsockopt(h->iofile);
703 r = bind(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
704 freeaddrinfo(sp->ai);
707 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
714 /* Allow a maximum-sized backlog of waiting-to-connect clients */
715 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
720 h->state = CS_ST_IDLE;
721 h->event = CS_LISTEN;
725 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
726 int (*check_ip)(void *cd, const char *a, int len, int t),
729 struct sockaddr_in addr;
730 YAZ_SOCKLEN_T len = sizeof(addr);
732 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
733 if (h->state != CS_ST_IDLE)
735 h->cerrno = CSOUTSTATE;
738 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
743 WSAGetLastError() == WSAEWOULDBLOCK
745 yaz_errno() == EWOULDBLOCK
747 #if EAGAIN != EWOULDBLOCK
748 || yaz_errno() == EAGAIN
753 h->cerrno = CSNODATA;
757 shutdown(h->iofile, SD_RECEIVE);
759 shutdown(h->iofile, SHUT_RD);
761 listen(h->iofile, SOMAXCONN);
766 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
767 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
770 if (check_ip && (*check_ip)(cd, (const char *) &addr,
771 sizeof(addr), AF_INET))
775 closesocket(h->newfd);
782 h->state = CS_ST_INCON;
783 tcpip_setsockopt (h->newfd);
787 COMSTACK tcpip_accept(COMSTACK h)
790 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
792 unsigned long tru = 1;
795 TRC(fprintf(stderr, "tcpip_accept\n"));
796 if (h->state == CS_ST_INCON)
798 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
802 closesocket(h->newfd);
809 memcpy(cnew, h, sizeof(*h));
810 cnew->iofile = h->newfd;
811 cnew->io_pending = 0;
812 if (!(state = (tcpip_state *)
813 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
819 closesocket(h->newfd);
827 if (!tcpip_set_blocking(cnew, cnew->flags))
833 closesocket(h->newfd);
845 state->altsize = state->altlen = 0;
846 state->towrite = state->written = -1;
847 state->complete = st->complete;
851 cnew->state = CS_ST_ACCEPT;
852 h->state = CS_ST_IDLE;
855 state->ctx = st->ctx;
856 state->ctx_alloc = 0;
857 state->ssl = st->ssl;
860 state->ssl = SSL_new (state->ctx);
861 SSL_set_fd (state->ssl, cnew->iofile);
864 state->connect_request_buf = 0;
865 state->connect_response_buf = 0;
868 if (h->state == CS_ST_ACCEPT)
871 tcpip_state *state = (tcpip_state *)h->cprivate;
875 TRC(fprintf(stderr, "SSL_accept\n"));
876 res = SSL_accept (state->ssl);
877 #if HAVE_OPENSSL_SSL_H
880 int err = SSL_get_error(state->ssl, res);
881 if (err == SSL_ERROR_WANT_READ)
883 h->io_pending = CS_WANT_READ;
886 if (err == SSL_ERROR_WANT_WRITE)
888 h->io_pending = CS_WANT_WRITE;
895 TRC(fprintf(stderr, "SSL_accept res=%d last_error=%d\n",
896 res, state->ssl->last_error));
897 if (res == 0 && state->ssl->last_error == GNUTLS_E_AGAIN)
899 h->io_pending = CS_WANT_READ;
902 else if (res <= 0) /* assume real error */
913 h->cerrno = CSOUTSTATE;
917 h->state = CS_ST_DATAXFER;
922 #define CS_TCPIP_BUFCHUNK 4096
925 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
926 * 0=connection closed.
928 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
930 tcpip_state *sp = (tcpip_state *)h->cprivate;
932 int tmpi, berlen, rest, req, tomove;
933 int hasread = 0, res;
935 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
936 if (sp->altlen) /* switch buffers */
938 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
939 (unsigned) sp->altbuf));
943 *bufsize = sp->altsize;
944 hasread = sp->altlen;
950 while (!(berlen = (*sp->complete)(*buf, hasread)))
954 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
960 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
961 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
968 /* unfortunatly, sun sometimes forgets to set errno in recv
969 when EWOULDBLOCK etc. would be required (res = -1) */
971 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
972 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
975 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
976 strerror(yaz_errno())));
978 if (WSAGetLastError() == WSAEWOULDBLOCK)
980 h->io_pending = CS_WANT_READ;
989 if (yaz_errno() == EWOULDBLOCK
991 #if EAGAIN != EWOULDBLOCK
992 || yaz_errno() == EAGAIN
995 || yaz_errno() == EINPROGRESS
997 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
1001 h->io_pending = CS_WANT_READ;
1004 else if (yaz_errno() == 0)
1008 h->cerrno = CSYSERR;
1016 if (hasread > h->max_recv_bytes)
1018 h->cerrno = CSBUFSIZE;
1022 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1024 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1025 if (hasread > berlen)
1027 tomove = req = hasread - berlen;
1028 rest = tomove % CS_TCPIP_BUFCHUNK;
1030 req += CS_TCPIP_BUFCHUNK - rest;
1033 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1035 h->cerrno = CSYSERR;
1038 } else if (sp->altsize < req)
1039 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1041 h->cerrno = CSYSERR;
1044 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1045 (unsigned) sp->altbuf));
1046 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1048 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1049 *(*buf + berlen) = '\0';
1050 return berlen ? berlen : 1;
1056 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
1057 * 0=connection closed.
1059 int ssl_get(COMSTACK h, char **buf, int *bufsize)
1061 tcpip_state *sp = (tcpip_state *)h->cprivate;
1063 int tmpi, berlen, rest, req, tomove;
1064 int hasread = 0, res;
1066 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
1067 if (sp->altlen) /* switch buffers */
1069 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
1070 (unsigned) sp->altbuf));
1074 *bufsize = sp->altsize;
1075 hasread = sp->altlen;
1081 while (!(berlen = (*sp->complete)(*buf, hasread)))
1085 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
1088 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
1089 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
1091 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
1092 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
1095 int ssl_err = SSL_get_error(sp->ssl, res);
1096 if (ssl_err == SSL_ERROR_WANT_READ)
1098 h->io_pending = CS_WANT_READ;
1101 if (ssl_err == SSL_ERROR_WANT_WRITE)
1103 h->io_pending = CS_WANT_WRITE;
1108 h->cerrno = CSERRORSSL;
1113 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1115 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1116 if (hasread > berlen)
1118 tomove = req = hasread - berlen;
1119 rest = tomove % CS_TCPIP_BUFCHUNK;
1121 req += CS_TCPIP_BUFCHUNK - rest;
1124 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1126 } else if (sp->altsize < req)
1127 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1129 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1130 (unsigned) sp->altbuf));
1131 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1133 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1134 *(*buf + berlen) = '\0';
1135 return berlen ? berlen : 1;
1140 * Returns 1, 0 or -1
1141 * In nonblocking mode, you must call again with same buffer while
1142 * return value is 1.
1144 int tcpip_put(COMSTACK h, char *buf, int size)
1147 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1149 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
1152 if (state->towrite < 0)
1154 state->towrite = size;
1157 else if (state->towrite != size)
1159 h->cerrno = CSWRONGBUF;
1162 while (state->towrite > state->written)
1165 send(h->iofile, buf + state->written, size -
1176 WSAGetLastError() == WSAEWOULDBLOCK
1178 yaz_errno() == EWOULDBLOCK
1180 #if EAGAIN != EWOULDBLOCK
1181 || yaz_errno() == EAGAIN
1185 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1187 || yaz_errno() == EINPROGRESS
1191 TRC(fprintf(stderr, " Flow control stop\n"));
1192 h->io_pending = CS_WANT_WRITE;
1195 h->cerrno = CSYSERR;
1198 state->written += res;
1199 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1200 res, state->written, size));
1202 state->towrite = state->written = -1;
1203 TRC(fprintf(stderr, " Ok\n"));
1210 * Returns 1, 0 or -1
1211 * In nonblocking mode, you must call again with same buffer while
1212 * return value is 1.
1214 int ssl_put(COMSTACK h, char *buf, int size)
1217 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1219 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1222 if (state->towrite < 0)
1224 state->towrite = size;
1227 else if (state->towrite != size)
1229 h->cerrno = CSWRONGBUF;
1232 while (state->towrite > state->written)
1234 res = SSL_write (state->ssl, buf + state->written,
1235 size - state->written);
1238 int ssl_err = SSL_get_error(state->ssl, res);
1239 if (ssl_err == SSL_ERROR_WANT_READ)
1241 h->io_pending = CS_WANT_READ;
1244 if (ssl_err == SSL_ERROR_WANT_WRITE)
1246 h->io_pending = CS_WANT_WRITE;
1249 h->cerrno = CSERRORSSL;
1252 state->written += res;
1253 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1254 res, state->written, size));
1256 state->towrite = state->written = -1;
1257 TRC(fprintf(stderr, " Ok\n"));
1262 int tcpip_close(COMSTACK h)
1264 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1266 TRC(fprintf(stderr, "tcpip_close\n"));
1267 if (h->iofile != -1)
1272 SSL_shutdown (sp->ssl);
1276 closesocket(h->iofile);
1286 TRC (fprintf(stderr, "SSL_free\n"));
1291 SSL_CTX_free (sp->ctx_alloc);
1293 #if HAVE_GETADDRINFO
1295 freeaddrinfo(sp->ai);
1297 xfree(sp->connect_request_buf);
1298 xfree(sp->connect_response_buf);
1304 char *tcpip_addrstr(COMSTACK h)
1306 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1307 char *r = 0, *buf = sp->buf;
1309 #if HAVE_GETADDRINFO
1311 struct sockaddr_storage addr;
1312 YAZ_SOCKLEN_T len = sizeof(addr);
1314 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1316 h->cerrno = CSYSERR;
1319 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
1321 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1330 struct sockaddr_in addr;
1331 YAZ_SOCKLEN_T len = sizeof(addr);
1332 struct hostent *host;
1334 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1336 h->cerrno = CSYSERR;
1339 if (!(h->flags & CS_FLAGS_NUMERICHOST))
1341 if ((host = gethostbyaddr((char*)&addr.sin_addr,
1342 sizeof(addr.sin_addr),
1344 r = (char*) host->h_name;
1347 r = inet_ntoa(addr.sin_addr);
1350 if (h->protocol == PROTO_HTTP)
1351 sprintf(buf, "http:%s", r);
1353 sprintf(buf, "tcp:%s", r);
1357 if (h->protocol == PROTO_HTTP)
1358 sprintf(buf, "https:%s", r);
1360 sprintf(buf, "ssl:%s", r);
1366 int static tcpip_set_blocking(COMSTACK p, int flags)
1371 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1372 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1375 flag = fcntl(p->iofile, F_GETFL, 0);
1376 if (flags & CS_FLAGS_BLOCKING)
1377 flag = flag & ~O_NONBLOCK; /* blocking */
1380 flag = flag | O_NONBLOCK; /* non-blocking */
1381 signal(SIGPIPE, SIG_IGN);
1383 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1391 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1393 struct tcpip_state *sp;
1394 if (!cs || cs->type != ssl_type)
1396 sp = (struct tcpip_state *) cs->cprivate;
1399 sp->ctx = (SSL_CTX *) ctx;
1403 void *cs_get_ssl(COMSTACK cs)
1405 struct tcpip_state *sp;
1406 if (!cs || cs->type != ssl_type)
1408 sp = (struct tcpip_state *) cs->cprivate;
1412 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1414 struct tcpip_state *sp;
1415 if (!cs || cs->type != ssl_type)
1417 sp = (struct tcpip_state *) cs->cprivate;
1418 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1419 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1423 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1425 SSL *ssl = (SSL *) cs_get_ssl(cs);
1428 #if HAVE_OPENSSL_SSL_H
1429 X509 *server_cert = SSL_get_peer_certificate (ssl);
1432 BIO *bio = BIO_new(BIO_s_mem());
1434 /* get PEM buffer in memory */
1435 PEM_write_bio_X509(bio, server_cert);
1436 *len = BIO_get_mem_data(bio, &pem_buf);
1437 *buf = (char *) xmalloc(*len);
1438 memcpy(*buf, pem_buf, *len);
1447 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1452 void *cs_get_ssl(COMSTACK cs)
1457 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1462 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1469 static int tcpip_put_connect(COMSTACK h, char *buf, int size)
1471 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1473 int r = tcpip_put(h, state->connect_request_buf,
1474 state->connect_request_len);
1478 h->f_put = tcpip_put; /* switch to normal tcpip put */
1479 r = tcpip_put(h, buf, size);
1484 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize)
1486 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1489 r = tcpip_get(h, &state->connect_response_buf,
1490 &state->connect_response_len);
1493 /* got the connect response completely */
1494 state->complete = cs_complete_auto; /* switch to normal tcpip get */
1495 h->f_get = tcpip_get;
1496 return tcpip_get(h, buf, bufsize);
1503 * indent-tabs-mode: nil
1505 * vim: shiftwidth=4 tabstop=8 expandtab