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);
551 int err = SSL_get_error(sp->ssl, res);
552 if (err == SSL_ERROR_WANT_READ)
554 h->io_pending = CS_WANT_READ;
557 if (err == SSL_ERROR_WANT_WRITE)
559 h->io_pending = CS_WANT_WRITE;
562 h->cerrno = CSERRORSSL;
568 h->state = CS_ST_DATAXFER;
572 #define CERTF "ztest.pem"
573 #define KEYF "ztest.pem"
575 static void tcpip_setsockopt (int fd)
581 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
583 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
585 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
587 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
589 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
591 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
596 static int tcpip_bind(COMSTACK h, void *address, int mode)
599 tcpip_state *sp = (tcpip_state *)h->cprivate;
602 struct sockaddr *addr = (struct sockaddr *)address;
611 if (sp->ai != (struct addrinfo *) address)
613 h->cerrno = CSOUTSTATE;
619 if (h->type == ssl_type && !sp->ctx)
622 SSL_load_error_strings();
624 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_server_method());
627 h->cerrno = CSERRORSSL;
636 res = SSL_CTX_use_certificate_file(sp->ctx, sp->cert_fname,
640 #if HAVE_OPENSSL_SSL_H
641 ERR_print_errors_fp(stderr);
643 fprintf(stderr, " SSL_CTX_use_certificate_file %s failed\n",
648 res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
652 #if HAVE_OPENSSL_SSL_H
653 ERR_print_errors_fp(stderr);
655 fprintf(stderr, " SSL_CTX_use_certificate_file %s failed\n",
660 #if HAVE_OPENSSL_SSL_H
661 res = SSL_CTX_check_private_key (sp->ctx);
664 ERR_print_errors_fp(stderr);
669 TRC (fprintf (stderr, "ssl_bind\n"));
673 TRC (fprintf (stderr, "tcpip_bind\n"));
676 TRC (fprintf (stderr, "tcpip_bind\n"));
679 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
680 &one, sizeof(one)) < 0)
686 tcpip_setsockopt(h->iofile);
688 r = bind(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
689 freeaddrinfo(sp->ai);
692 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
699 /* Allow a maximum-sized backlog of waiting-to-connect clients */
700 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
705 h->state = CS_ST_IDLE;
706 h->event = CS_LISTEN;
710 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
711 int (*check_ip)(void *cd, const char *a, int len, int t),
714 struct sockaddr_in addr;
715 YAZ_SOCKLEN_T len = sizeof(addr);
717 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
718 if (h->state != CS_ST_IDLE)
720 h->cerrno = CSOUTSTATE;
723 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
728 WSAGetLastError() == WSAEWOULDBLOCK
730 yaz_errno() == EWOULDBLOCK
732 #if EAGAIN != EWOULDBLOCK
733 || yaz_errno() == EAGAIN
738 h->cerrno = CSNODATA;
742 shutdown(h->iofile, SD_RECEIVE);
744 shutdown(h->iofile, SHUT_RD);
746 listen(h->iofile, SOMAXCONN);
751 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
752 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
755 if (check_ip && (*check_ip)(cd, (const char *) &addr,
756 sizeof(addr), AF_INET))
760 closesocket(h->newfd);
767 h->state = CS_ST_INCON;
768 tcpip_setsockopt (h->newfd);
772 COMSTACK tcpip_accept(COMSTACK h)
775 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
777 unsigned long tru = 1;
780 TRC(fprintf(stderr, "tcpip_accept\n"));
781 if (h->state == CS_ST_INCON)
783 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
787 closesocket(h->newfd);
794 memcpy(cnew, h, sizeof(*h));
795 cnew->iofile = h->newfd;
796 cnew->io_pending = 0;
797 if (!(state = (tcpip_state *)
798 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
804 closesocket(h->newfd);
812 if (!tcpip_set_blocking(cnew, cnew->flags))
818 closesocket(h->newfd);
830 state->altsize = state->altlen = 0;
831 state->towrite = state->written = -1;
832 state->complete = st->complete;
836 cnew->state = CS_ST_ACCEPT;
837 h->state = CS_ST_IDLE;
840 state->ctx = st->ctx;
841 state->ctx_alloc = 0;
842 state->ssl = st->ssl;
845 state->ssl = SSL_new (state->ctx);
846 SSL_set_fd (state->ssl, cnew->iofile);
849 state->connect_request_buf = 0;
850 state->connect_response_buf = 0;
853 if (h->state == CS_ST_ACCEPT)
856 tcpip_state *state = (tcpip_state *)h->cprivate;
859 int res = SSL_accept (state->ssl);
860 TRC(fprintf(stderr, "SSL_accept\n"));
863 int err = SSL_get_error(state->ssl, res);
864 if (err == SSL_ERROR_WANT_READ)
866 h->io_pending = CS_WANT_READ;
869 if (err == SSL_ERROR_WANT_WRITE)
871 h->io_pending = CS_WANT_WRITE;
882 h->cerrno = CSOUTSTATE;
886 h->state = CS_ST_DATAXFER;
891 #define CS_TCPIP_BUFCHUNK 4096
894 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
895 * 0=connection closed.
897 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
899 tcpip_state *sp = (tcpip_state *)h->cprivate;
901 int tmpi, berlen, rest, req, tomove;
902 int hasread = 0, res;
904 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
905 if (sp->altlen) /* switch buffers */
907 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
908 (unsigned) sp->altbuf));
912 *bufsize = sp->altsize;
913 hasread = sp->altlen;
919 while (!(berlen = (*sp->complete)(*buf, hasread)))
923 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
929 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
930 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
937 /* unfortunatly, sun sometimes forgets to set errno in recv
938 when EWOULDBLOCK etc. would be required (res = -1) */
940 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
941 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
944 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
945 strerror(yaz_errno())));
947 if (WSAGetLastError() == WSAEWOULDBLOCK)
949 h->io_pending = CS_WANT_READ;
958 if (yaz_errno() == EWOULDBLOCK
960 #if EAGAIN != EWOULDBLOCK
961 || yaz_errno() == EAGAIN
964 || yaz_errno() == EINPROGRESS
966 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
970 h->io_pending = CS_WANT_READ;
973 else if (yaz_errno() == 0)
985 if (hasread > h->max_recv_bytes)
987 h->cerrno = CSBUFSIZE;
991 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
993 /* move surplus buffer (or everything if we didn't get a BER rec.) */
994 if (hasread > berlen)
996 tomove = req = hasread - berlen;
997 rest = tomove % CS_TCPIP_BUFCHUNK;
999 req += CS_TCPIP_BUFCHUNK - rest;
1002 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1004 h->cerrno = CSYSERR;
1007 } else if (sp->altsize < req)
1008 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1010 h->cerrno = CSYSERR;
1013 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1014 (unsigned) sp->altbuf));
1015 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1017 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1018 *(*buf + berlen) = '\0';
1019 return berlen ? berlen : 1;
1025 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
1026 * 0=connection closed.
1028 int ssl_get(COMSTACK h, char **buf, int *bufsize)
1030 tcpip_state *sp = (tcpip_state *)h->cprivate;
1032 int tmpi, berlen, rest, req, tomove;
1033 int hasread = 0, res;
1035 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
1036 if (sp->altlen) /* switch buffers */
1038 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
1039 (unsigned) sp->altbuf));
1043 *bufsize = sp->altsize;
1044 hasread = sp->altlen;
1050 while (!(berlen = (*sp->complete)(*buf, hasread)))
1054 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
1057 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
1058 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
1060 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
1061 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
1064 int ssl_err = SSL_get_error(sp->ssl, res);
1065 if (ssl_err == SSL_ERROR_WANT_READ)
1067 h->io_pending = CS_WANT_READ;
1070 if (ssl_err == SSL_ERROR_WANT_WRITE)
1072 h->io_pending = CS_WANT_WRITE;
1077 h->cerrno = CSERRORSSL;
1082 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1084 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1085 if (hasread > berlen)
1087 tomove = req = hasread - berlen;
1088 rest = tomove % CS_TCPIP_BUFCHUNK;
1090 req += CS_TCPIP_BUFCHUNK - rest;
1093 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1095 } else if (sp->altsize < req)
1096 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1098 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1099 (unsigned) sp->altbuf));
1100 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1102 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1103 *(*buf + berlen) = '\0';
1104 return berlen ? berlen : 1;
1109 * Returns 1, 0 or -1
1110 * In nonblocking mode, you must call again with same buffer while
1111 * return value is 1.
1113 int tcpip_put(COMSTACK h, char *buf, int size)
1116 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1118 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
1121 if (state->towrite < 0)
1123 state->towrite = size;
1126 else if (state->towrite != size)
1128 h->cerrno = CSWRONGBUF;
1131 while (state->towrite > state->written)
1134 send(h->iofile, buf + state->written, size -
1145 WSAGetLastError() == WSAEWOULDBLOCK
1147 yaz_errno() == EWOULDBLOCK
1149 #if EAGAIN != EWOULDBLOCK
1150 || yaz_errno() == EAGAIN
1154 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1156 || yaz_errno() == EINPROGRESS
1160 TRC(fprintf(stderr, " Flow control stop\n"));
1161 h->io_pending = CS_WANT_WRITE;
1164 h->cerrno = CSYSERR;
1167 state->written += res;
1168 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1169 res, state->written, size));
1171 state->towrite = state->written = -1;
1172 TRC(fprintf(stderr, " Ok\n"));
1179 * Returns 1, 0 or -1
1180 * In nonblocking mode, you must call again with same buffer while
1181 * return value is 1.
1183 int ssl_put(COMSTACK h, char *buf, int size)
1186 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1188 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1191 if (state->towrite < 0)
1193 state->towrite = size;
1196 else if (state->towrite != size)
1198 h->cerrno = CSWRONGBUF;
1201 while (state->towrite > state->written)
1203 res = SSL_write (state->ssl, buf + state->written,
1204 size - state->written);
1207 int ssl_err = SSL_get_error(state->ssl, res);
1208 if (ssl_err == SSL_ERROR_WANT_READ)
1210 h->io_pending = CS_WANT_READ;
1213 if (ssl_err == SSL_ERROR_WANT_WRITE)
1215 h->io_pending = CS_WANT_WRITE;
1218 h->cerrno = CSERRORSSL;
1221 state->written += res;
1222 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1223 res, state->written, size));
1225 state->towrite = state->written = -1;
1226 TRC(fprintf(stderr, " Ok\n"));
1231 int tcpip_close(COMSTACK h)
1233 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1235 TRC(fprintf(stderr, "tcpip_close\n"));
1236 if (h->iofile != -1)
1241 SSL_shutdown (sp->ssl);
1245 closesocket(h->iofile);
1255 TRC (fprintf(stderr, "SSL_free\n"));
1260 SSL_CTX_free (sp->ctx_alloc);
1262 #if HAVE_GETADDRINFO
1264 freeaddrinfo(sp->ai);
1266 xfree(sp->connect_request_buf);
1267 xfree(sp->connect_response_buf);
1273 char *tcpip_addrstr(COMSTACK h)
1275 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1276 char *r = 0, *buf = sp->buf;
1278 #if HAVE_GETADDRINFO
1280 struct sockaddr_storage addr;
1281 YAZ_SOCKLEN_T len = sizeof(addr);
1283 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1285 h->cerrno = CSYSERR;
1288 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
1290 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1299 struct sockaddr_in addr;
1300 YAZ_SOCKLEN_T len = sizeof(addr);
1301 struct hostent *host;
1303 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1305 h->cerrno = CSYSERR;
1308 if (!(h->flags & CS_FLAGS_NUMERICHOST))
1310 if ((host = gethostbyaddr((char*)&addr.sin_addr,
1311 sizeof(addr.sin_addr),
1313 r = (char*) host->h_name;
1316 r = inet_ntoa(addr.sin_addr);
1319 if (h->protocol == PROTO_HTTP)
1320 sprintf(buf, "http:%s", r);
1322 sprintf(buf, "tcp:%s", r);
1326 if (h->protocol == PROTO_HTTP)
1327 sprintf(buf, "https:%s", r);
1329 sprintf(buf, "ssl:%s", r);
1335 int static tcpip_set_blocking(COMSTACK p, int flags)
1340 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1341 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1344 flag = fcntl(p->iofile, F_GETFL, 0);
1345 if (flags & CS_FLAGS_BLOCKING)
1346 flag = flag & ~O_NONBLOCK; /* blocking */
1349 flag = flag | O_NONBLOCK; /* non-blocking */
1350 signal(SIGPIPE, SIG_IGN);
1352 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1360 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1362 struct tcpip_state *sp;
1363 if (!cs || cs->type != ssl_type)
1365 sp = (struct tcpip_state *) cs->cprivate;
1368 sp->ctx = (SSL_CTX *) ctx;
1372 void *cs_get_ssl(COMSTACK cs)
1374 struct tcpip_state *sp;
1375 if (!cs || cs->type != ssl_type)
1377 sp = (struct tcpip_state *) cs->cprivate;
1381 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1383 struct tcpip_state *sp;
1384 if (!cs || cs->type != ssl_type)
1386 sp = (struct tcpip_state *) cs->cprivate;
1387 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1388 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1392 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1394 SSL *ssl = (SSL *) cs_get_ssl(cs);
1397 X509 *server_cert = SSL_get_peer_certificate (ssl);
1398 #if HAVE_OPENSSL_SSL_H
1401 BIO *bio = BIO_new(BIO_s_mem());
1403 /* get PEM buffer in memory */
1404 PEM_write_bio_X509(bio, server_cert);
1405 *len = BIO_get_mem_data(bio, &pem_buf);
1406 *buf = (char *) xmalloc(*len);
1407 memcpy(*buf, pem_buf, *len);
1416 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1421 void *cs_get_ssl(COMSTACK cs)
1426 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1431 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1438 static int tcpip_put_connect(COMSTACK h, char *buf, int size)
1440 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1442 int r = tcpip_put(h, state->connect_request_buf,
1443 state->connect_request_len);
1447 h->f_put = tcpip_put; /* switch to normal tcpip put */
1448 r = tcpip_put(h, buf, size);
1453 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize)
1455 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1458 r = tcpip_get(h, &state->connect_response_buf,
1459 &state->connect_response_len);
1462 /* got the connect response completely */
1463 state->complete = cs_complete_auto; /* switch to normal tcpip get */
1464 h->f_get = tcpip_get;
1465 return tcpip_get(h, buf, bufsize);
1472 * indent-tabs-mode: nil
1474 * vim: shiftwidth=4 tabstop=8 expandtab