2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.29 2006-09-06 16:03:52 adam Exp $
9 * \brief Implements TCP/IP + SSL COMSTACK.
19 #include <sys/types.h>
31 #define HAVE_GETADDRINFO 1
33 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <netinet/tcp.h>
40 #include <sys/socket.h>
43 #include <sys/select.h>
49 #if HAVE_OPENSSL_SSL_H
50 #include <openssl/ssl.h>
51 #include <openssl/err.h>
54 #include <yaz/comstack.h>
55 #include <yaz/tcpip.h>
58 static int tcpip_close(COMSTACK h);
59 static int tcpip_put(COMSTACK h, char *buf, int size);
60 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
61 static int tcpip_connect(COMSTACK h, void *address);
62 static int tcpip_more(COMSTACK h);
63 static int tcpip_rcvconnect(COMSTACK h);
64 static int tcpip_bind(COMSTACK h, void *address, int mode);
65 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
66 int (*check_ip)(void *cd, const char *a, int len, int type),
68 static int tcpip_set_blocking(COMSTACK p, int blocking);
70 #if HAVE_OPENSSL_SSL_H
71 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
72 static int ssl_put(COMSTACK h, char *buf, int size);
75 static COMSTACK tcpip_accept(COMSTACK h);
76 static char *tcpip_addrstr(COMSTACK h);
77 static void *tcpip_straddr(COMSTACK h, const char *str);
86 #define YAZ_SOCKLEN_T int
89 /* this state is used for both SSL and straight TCP/IP */
90 typedef struct tcpip_state
92 char *altbuf; /* alternate buffer for surplus data */
93 int altsize; /* size as xmalloced */
94 int altlen; /* length of data or 0 if none */
96 int written; /* -1 if we aren't writing */
97 int towrite; /* to verify against user input */
98 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
102 struct sockaddr_in addr; /* returned by cs_straddr */
104 char buf[128]; /* returned by cs_addrstr */
105 #if HAVE_OPENSSL_SSL_H
106 SSL_CTX *ctx; /* current CTX. */
107 SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
109 char cert_fname[256];
114 static int tcpip_init (void)
116 static int initialized = 0;
122 requested = MAKEWORD(1, 1);
123 if (WSAStartup(requested, &wd))
130 static int tcpip_init (void)
137 * This function is always called through the cs_create() macro.
138 * s >= 0: socket has already been established for us.
140 COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
147 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
149 if (!(sp = (struct tcpip_state *)(p->cprivate =
150 xmalloc(sizeof(tcpip_state)))))
157 p->type = tcpip_type;
158 p->protocol = (enum oid_proto) protocol;
160 p->f_connect = tcpip_connect;
161 p->f_rcvconnect = tcpip_rcvconnect;
162 p->f_get = tcpip_get;
163 p->f_put = tcpip_put;
164 p->f_close = tcpip_close;
165 p->f_more = tcpip_more;
166 p->f_bind = tcpip_bind;
167 p->f_listen = tcpip_listen;
168 p->f_accept = tcpip_accept;
169 p->f_addrstr = tcpip_addrstr;
170 p->f_straddr = tcpip_straddr;
171 p->f_set_blocking = tcpip_set_blocking;
172 p->max_recv_bytes = 5000000;
174 p->state = s < 0 ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
180 #if HAVE_OPENSSL_SSL_H
181 sp->ctx = sp->ctx_alloc = 0;
183 strcpy(sp->cert_fname, "yaz.pem");
190 sp->altsize = sp->altlen = 0;
191 sp->towrite = sp->written = -1;
192 if (protocol == PROTO_WAIS)
193 sp->complete = completeWAIS;
195 sp->complete = cs_complete_auto;
197 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
198 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
203 #if HAVE_OPENSSL_SSL_H
205 COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
210 p = tcpip_type (s, flags, protocol, 0);
216 sp = (tcpip_state *) p->cprivate;
218 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
220 /* note: we don't handle already opened socket in SSL mode - yet */
226 /* resolve using getaddrinfo */
227 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
229 struct addrinfo hints, *res;
234 hints.ai_family = AF_UNSPEC;
235 hints.ai_socktype = SOCK_STREAM;
236 hints.ai_protocol = 0;
237 hints.ai_addrlen = 0;
238 hints.ai_addr = NULL;
239 hints.ai_canonname = NULL;
240 hints.ai_next = NULL;
242 strncpy(host, str, sizeof(host)-1);
243 host[sizeof(host)-1] = 0;
244 if ((p = strchr(host, '/')))
246 if ((p = strrchr(host, ':')))
252 if (!strcmp("@", host))
254 hints.ai_flags = AI_PASSIVE;
255 error = getaddrinfo(0, port, &hints, &res);
259 error = getaddrinfo(host, port, &hints, &res);
267 /* gethostbyname .. old systems */
268 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
273 short int port = default_port;
275 unsigned long tmpadd;
279 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
280 add->sin_family = AF_INET;
281 strncpy(buf, str, sizeof(buf)-1);
282 buf[sizeof(buf)-1] = 0;
283 if ((p = strchr(buf, '/')))
285 if ((p = strrchr(buf, ':')))
290 add->sin_port = htons(port);
291 if (!strcmp("@", buf))
293 add->sin_addr.s_addr = INADDR_ANY;
295 else if ((tmpadd = inet_addr(buf)) != -1)
297 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
299 else if ((hp = gethostbyname(buf)))
301 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
302 sizeof(struct in_addr));
311 void *tcpip_straddr(COMSTACK h, const char *str)
313 tcpip_state *sp = (tcpip_state *)h->cprivate;
314 const char *port = "210";
315 if (h->protocol == PROTO_HTTP)
321 freeaddrinfo(sp->ai);
322 sp->ai = tcpip_getaddrinfo(str, port);
323 if (sp->ai && h->state == CS_ST_UNBND)
326 struct addrinfo *ai = sp->ai;
327 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
332 if (!tcpip_set_blocking(h, h->flags))
338 void *tcpip_straddr(COMSTACK h, const char *str)
340 tcpip_state *sp = (tcpip_state *)h->cprivate;
342 if (h->protocol == PROTO_HTTP)
347 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
349 if (h->state == CS_ST_UNBND)
352 s = socket(AF_INET, SOCK_STREAM, 0);
357 if (!tcpip_set_blocking(h, h->flags))
364 int tcpip_more(COMSTACK h)
366 tcpip_state *sp = (tcpip_state *)h->cprivate;
368 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
373 * connect(2) will block (sometimes) - nothing we can do short of doing
374 * weird things like spawning subprocesses or threading or some weird junk
377 int tcpip_connect(COMSTACK h, void *address)
380 tcpip_state *sp = (tcpip_state *)h->cprivate;
382 struct sockaddr_in *add = (struct sockaddr_in *) address;
387 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
389 TRC(fprintf(stderr, "tcpip_connect\n"));
391 if (h->state != CS_ST_UNBND)
393 h->cerrno = CSOUTSTATE;
397 if (sp->ai != (struct addrinfo *) address)
399 h->cerrno = CSOUTSTATE;
404 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
405 * This gives the connect a chance to negotiate with the other side
408 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
413 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
415 recbuflen *= 10; /* lets be optimistic */
416 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
421 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
426 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
431 r = connect(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
432 freeaddrinfo(sp->ai);
435 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
440 if (WSAGetLastError() == WSAEWOULDBLOCK)
442 h->event = CS_CONNECT;
443 h->state = CS_ST_CONNECTING;
444 h->io_pending = CS_WANT_WRITE;
448 if (yaz_errno() == EINPROGRESS)
450 h->event = CS_CONNECT;
451 h->state = CS_ST_CONNECTING;
452 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
459 h->event = CS_CONNECT;
460 h->state = CS_ST_CONNECTING;
462 return tcpip_rcvconnect (h);
468 int tcpip_rcvconnect(COMSTACK h)
470 #if HAVE_OPENSSL_SSL_H
471 tcpip_state *sp = (tcpip_state *)h->cprivate;
473 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
475 if (h->state == CS_ST_DATAXFER)
477 if (h->state != CS_ST_CONNECTING)
479 h->cerrno = CSOUTSTATE;
482 #if HAVE_OPENSSL_SSL_H
483 if (h->type == ssl_type && !sp->ctx)
485 SSL_load_error_strings();
486 SSLeay_add_all_algorithms();
488 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
491 h->cerrno = CSERRORSSL;
501 sp->ssl = SSL_new (sp->ctx);
502 SSL_set_fd (sp->ssl, h->iofile);
504 res = SSL_connect (sp->ssl);
507 int err = SSL_get_error(sp->ssl, res);
508 if (err == SSL_ERROR_WANT_READ)
510 h->io_pending = CS_WANT_READ;
513 if (err == SSL_ERROR_WANT_WRITE)
515 h->io_pending = CS_WANT_WRITE;
518 h->cerrno = CSERRORSSL;
524 h->state = CS_ST_DATAXFER;
528 #define CERTF "ztest.pem"
529 #define KEYF "ztest.pem"
531 static void tcpip_setsockopt (int fd)
537 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
539 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
541 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
543 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
545 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
547 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
552 static int tcpip_bind(COMSTACK h, void *address, int mode)
555 tcpip_state *sp = (tcpip_state *)h->cprivate;
558 struct sockaddr *addr = (struct sockaddr *)address;
563 unsigned long one = 1;
567 if (sp->ai != (struct addrinfo *) address)
569 h->cerrno = CSOUTSTATE;
574 #if HAVE_OPENSSL_SSL_H
575 if (h->type == ssl_type && !sp->ctx)
577 SSL_load_error_strings();
578 SSLeay_add_all_algorithms();
580 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
583 h->cerrno = CSERRORSSL;
592 res = SSL_CTX_use_certificate_chain_file(sp->ctx, sp->cert_fname);
595 ERR_print_errors_fp(stderr);
598 res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
602 ERR_print_errors_fp(stderr);
605 res = SSL_CTX_check_private_key (sp->ctx);
608 ERR_print_errors_fp(stderr);
612 TRC (fprintf (stderr, "ssl_bind\n"));
616 TRC (fprintf (stderr, "tcpip_bind\n"));
619 TRC (fprintf (stderr, "tcpip_bind\n"));
622 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
623 &one, sizeof(one)) < 0)
629 tcpip_setsockopt(h->iofile);
631 r = bind(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
632 freeaddrinfo(sp->ai);
635 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
642 /* Allow a maximum-sized backlog of waiting-to-connect clients */
643 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
648 h->state = CS_ST_IDLE;
649 h->event = CS_LISTEN;
653 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
654 int (*check_ip)(void *cd, const char *a, int len, int t),
657 struct sockaddr_in addr;
658 YAZ_SOCKLEN_T len = sizeof(addr);
660 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
661 if (h->state != CS_ST_IDLE)
663 h->cerrno = CSOUTSTATE;
666 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
671 WSAGetLastError() == WSAEWOULDBLOCK
673 yaz_errno() == EWOULDBLOCK
675 #if EAGAIN != EWOULDBLOCK
676 || yaz_errno() == EAGAIN
681 h->cerrno = CSNODATA;
686 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
687 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
690 if (check_ip && (*check_ip)(cd, (const char *) &addr,
691 sizeof(addr), AF_INET))
695 closesocket(h->newfd);
702 h->state = CS_ST_INCON;
703 tcpip_setsockopt (h->newfd);
707 COMSTACK tcpip_accept(COMSTACK h)
710 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
712 unsigned long tru = 1;
715 TRC(fprintf(stderr, "tcpip_accept\n"));
716 if (h->state == CS_ST_INCON)
718 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
722 closesocket(h->newfd);
729 memcpy(cnew, h, sizeof(*h));
730 cnew->iofile = h->newfd;
731 cnew->io_pending = 0;
732 if (!(state = (tcpip_state *)
733 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
739 closesocket(h->newfd);
747 if (!tcpip_set_blocking(cnew, cnew->flags))
753 closesocket(h->newfd);
765 state->altsize = state->altlen = 0;
766 state->towrite = state->written = -1;
767 state->complete = st->complete;
771 cnew->state = CS_ST_ACCEPT;
772 h->state = CS_ST_IDLE;
774 #if HAVE_OPENSSL_SSL_H
775 state->ctx = st->ctx;
776 state->ctx_alloc = 0;
777 state->ssl = st->ssl;
780 state->ssl = SSL_new (state->ctx);
781 SSL_set_fd (state->ssl, cnew->iofile);
786 if (h->state == CS_ST_ACCEPT)
788 #if HAVE_OPENSSL_SSL_H
789 tcpip_state *state = (tcpip_state *)h->cprivate;
792 int res = SSL_accept (state->ssl);
793 TRC(fprintf(stderr, "SSL_accept\n"));
796 int err = SSL_get_error(state->ssl, res);
797 if (err == SSL_ERROR_WANT_READ)
799 h->io_pending = CS_WANT_READ;
802 if (err == SSL_ERROR_WANT_WRITE)
804 h->io_pending = CS_WANT_WRITE;
815 h->cerrno = CSOUTSTATE;
819 h->state = CS_ST_DATAXFER;
824 #define CS_TCPIP_BUFCHUNK 4096
827 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
828 * 0=connection closed.
830 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
832 tcpip_state *sp = (tcpip_state *)h->cprivate;
834 int tmpi, berlen, rest, req, tomove;
835 int hasread = 0, res;
837 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
838 if (sp->altlen) /* switch buffers */
840 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
841 (unsigned) sp->altbuf));
845 *bufsize = sp->altsize;
846 hasread = sp->altlen;
852 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
856 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
862 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
863 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
870 /* unfortunatly, sun sometimes forgets to set errno in recv
871 when EWOULDBLOCK etc. would be required (res = -1) */
873 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
874 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
877 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
878 strerror(yaz_errno())));
880 if (WSAGetLastError() == WSAEWOULDBLOCK)
882 h->io_pending = CS_WANT_READ;
891 if (yaz_errno() == EWOULDBLOCK
893 #if EAGAIN != EWOULDBLOCK
894 || yaz_errno() == EAGAIN
897 || yaz_errno() == EINPROGRESS
899 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
903 h->io_pending = CS_WANT_READ;
906 else if (yaz_errno() == 0)
918 if (hasread > h->max_recv_bytes)
920 h->cerrno = CSBUFSIZE;
924 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
926 /* move surplus buffer (or everything if we didn't get a BER rec.) */
927 if (hasread > berlen)
929 tomove = req = hasread - berlen;
930 rest = tomove % CS_TCPIP_BUFCHUNK;
932 req += CS_TCPIP_BUFCHUNK - rest;
935 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
940 } else if (sp->altsize < req)
941 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
946 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
947 (unsigned) sp->altbuf));
948 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
950 if (berlen < CS_TCPIP_BUFCHUNK - 1)
951 *(*buf + berlen) = '\0';
952 return berlen ? berlen : 1;
956 #if HAVE_OPENSSL_SSL_H
958 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
959 * 0=connection closed.
961 int ssl_get(COMSTACK h, char **buf, int *bufsize)
963 tcpip_state *sp = (tcpip_state *)h->cprivate;
965 int tmpi, berlen, rest, req, tomove;
966 int hasread = 0, res;
968 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
969 if (sp->altlen) /* switch buffers */
971 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
972 (unsigned) sp->altbuf));
976 *bufsize = sp->altsize;
977 hasread = sp->altlen;
983 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
987 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
990 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
991 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
993 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
994 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
997 int ssl_err = SSL_get_error(sp->ssl, res);
998 if (ssl_err == SSL_ERROR_WANT_READ)
1000 h->io_pending = CS_WANT_READ;
1003 if (ssl_err == SSL_ERROR_WANT_WRITE)
1005 h->io_pending = CS_WANT_WRITE;
1010 h->cerrno = CSERRORSSL;
1015 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1017 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1018 if (hasread > berlen)
1020 tomove = req = hasread - berlen;
1021 rest = tomove % CS_TCPIP_BUFCHUNK;
1023 req += CS_TCPIP_BUFCHUNK - rest;
1026 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1028 } else if (sp->altsize < req)
1029 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1031 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1032 (unsigned) sp->altbuf));
1033 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1035 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1036 *(*buf + berlen) = '\0';
1037 return berlen ? berlen : 1;
1042 * Returns 1, 0 or -1
1043 * In nonblocking mode, you must call again with same buffer while
1044 * return value is 1.
1046 int tcpip_put(COMSTACK h, char *buf, int size)
1049 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1051 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
1054 if (state->towrite < 0)
1056 state->towrite = size;
1059 else if (state->towrite != size)
1061 h->cerrno = CSWRONGBUF;
1064 while (state->towrite > state->written)
1067 send(h->iofile, buf + state->written, size -
1078 WSAGetLastError() == WSAEWOULDBLOCK
1080 yaz_errno() == EWOULDBLOCK
1082 #if EAGAIN != EWOULDBLOCK
1083 || yaz_errno() == EAGAIN
1087 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1089 || yaz_errno() == EINPROGRESS
1093 TRC(fprintf(stderr, " Flow control stop\n"));
1094 h->io_pending = CS_WANT_WRITE;
1097 h->cerrno = CSYSERR;
1100 state->written += res;
1101 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1102 res, state->written, size));
1104 state->towrite = state->written = -1;
1105 TRC(fprintf(stderr, " Ok\n"));
1110 #if HAVE_OPENSSL_SSL_H
1112 * Returns 1, 0 or -1
1113 * In nonblocking mode, you must call again with same buffer while
1114 * return value is 1.
1116 int ssl_put(COMSTACK h, char *buf, int size)
1119 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1121 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1124 if (state->towrite < 0)
1126 state->towrite = size;
1129 else if (state->towrite != size)
1131 h->cerrno = CSWRONGBUF;
1134 while (state->towrite > state->written)
1136 res = SSL_write (state->ssl, buf + state->written,
1137 size - state->written);
1140 int ssl_err = SSL_get_error(state->ssl, res);
1141 if (ssl_err == SSL_ERROR_WANT_READ)
1143 h->io_pending = CS_WANT_READ;
1146 if (ssl_err == SSL_ERROR_WANT_WRITE)
1148 h->io_pending = CS_WANT_WRITE;
1151 h->cerrno = CSERRORSSL;
1154 state->written += res;
1155 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1156 res, state->written, size));
1158 state->towrite = state->written = -1;
1159 TRC(fprintf(stderr, " Ok\n"));
1164 int tcpip_close(COMSTACK h)
1166 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1168 TRC(fprintf(stderr, "tcpip_close\n"));
1169 if (h->iofile != -1)
1171 #if HAVE_OPENSSL_SSL_H
1174 SSL_shutdown (sp->ssl);
1178 closesocket(h->iofile);
1185 #if HAVE_OPENSSL_SSL_H
1188 TRC (fprintf(stderr, "SSL_free\n"));
1193 SSL_CTX_free (sp->ctx_alloc);
1195 #if HAVE_GETADDRINFO
1197 freeaddrinfo(sp->ai);
1204 char *tcpip_addrstr(COMSTACK h)
1206 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1207 char *r = 0, *buf = sp->buf;
1209 #if HAVE_GETADDRINFO
1211 struct sockaddr_storage addr;
1212 YAZ_SOCKLEN_T len = sizeof(addr);
1214 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1216 h->cerrno = CSYSERR;
1219 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
1221 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1230 struct sockaddr_in addr;
1231 YAZ_SOCKLEN_T len = sizeof(addr);
1232 struct hostent *host;
1234 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1236 h->cerrno = CSYSERR;
1239 if (!(h->flags & CS_FLAGS_NUMERICHOST))
1241 if ((host = gethostbyaddr((char*)&addr.sin_addr,
1242 sizeof(addr.sin_addr),
1244 r = (char*) host->h_name;
1247 r = inet_ntoa(addr.sin_addr);
1250 if (h->protocol == PROTO_HTTP)
1251 sprintf(buf, "http:%s", r);
1253 sprintf(buf, "tcp:%s", r);
1254 #if HAVE_OPENSSL_SSL_H
1257 if (h->protocol == PROTO_HTTP)
1258 sprintf(buf, "https:%s", r);
1260 sprintf(buf, "ssl:%s", r);
1266 int static tcpip_set_blocking(COMSTACK p, int flags)
1271 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1272 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1275 flag = fcntl(p->iofile, F_GETFL, 0);
1276 if (flags & CS_FLAGS_BLOCKING)
1277 flag = flag & ~O_NONBLOCK; /* blocking */
1280 flag = flag | O_NONBLOCK; /* non-blocking */
1281 signal(SIGPIPE, SIG_IGN);
1283 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1290 #if HAVE_OPENSSL_SSL_H
1291 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1293 struct tcpip_state *sp;
1294 if (!cs || cs->type != ssl_type)
1296 sp = (struct tcpip_state *) cs->cprivate;
1299 sp->ctx = (SSL_CTX *) ctx;
1303 void *cs_get_ssl(COMSTACK cs)
1305 struct tcpip_state *sp;
1306 if (!cs || cs->type != ssl_type)
1308 sp = (struct tcpip_state *) cs->cprivate;
1312 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1314 struct tcpip_state *sp;
1315 if (!cs || cs->type != ssl_type)
1317 sp = (struct tcpip_state *) cs->cprivate;
1318 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1319 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1323 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1325 SSL *ssl = (SSL *) cs_get_ssl(cs);
1328 X509 *server_cert = SSL_get_peer_certificate (ssl);
1331 BIO *bio = BIO_new(BIO_s_mem());
1333 /* get PEM buffer in memory */
1334 PEM_write_bio_X509(bio, server_cert);
1335 *len = BIO_get_mem_data(bio, &pem_buf);
1336 *buf = (char *) xmalloc(*len);
1337 memcpy(*buf, pem_buf, *len);
1345 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1350 void *cs_get_ssl(COMSTACK cs)
1355 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1360 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1369 * indent-tabs-mode: nil
1371 * vim: shiftwidth=4 tabstop=8 expandtab