2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.33 2007-01-03 08:42:15 adam Exp $
9 * \brief Implements TCP/IP + SSL COMSTACK.
19 #include <sys/types.h>
30 /* VS 2003 or later has getaddrinfo; older versions do not */
34 #define HAVE_GETADDRINFO 1
36 #define HAVE_GETADDRINFO 0
40 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #include <netinet/tcp.h>
47 #include <sys/socket.h>
50 #include <sys/select.h>
56 #if HAVE_OPENSSL_SSL_H
57 #include <openssl/ssl.h>
58 #include <openssl/err.h>
61 #include <yaz/comstack.h>
62 #include <yaz/tcpip.h>
65 static int tcpip_close(COMSTACK h);
66 static int tcpip_put(COMSTACK h, char *buf, int size);
67 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
68 static int tcpip_connect(COMSTACK h, void *address);
69 static int tcpip_more(COMSTACK h);
70 static int tcpip_rcvconnect(COMSTACK h);
71 static int tcpip_bind(COMSTACK h, void *address, int mode);
72 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
73 int (*check_ip)(void *cd, const char *a, int len, int type),
75 static int tcpip_set_blocking(COMSTACK p, int blocking);
77 #if HAVE_OPENSSL_SSL_H
78 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
79 static int ssl_put(COMSTACK h, char *buf, int size);
82 static COMSTACK tcpip_accept(COMSTACK h);
83 static char *tcpip_addrstr(COMSTACK h);
84 static void *tcpip_straddr(COMSTACK h, const char *str);
93 #define YAZ_SOCKLEN_T int
96 /* this state is used for both SSL and straight TCP/IP */
97 typedef struct tcpip_state
99 char *altbuf; /* alternate buffer for surplus data */
100 int altsize; /* size as xmalloced */
101 int altlen; /* length of data or 0 if none */
103 int written; /* -1 if we aren't writing */
104 int towrite; /* to verify against user input */
105 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
109 struct sockaddr_in addr; /* returned by cs_straddr */
111 char buf[128]; /* returned by cs_addrstr */
112 #if HAVE_OPENSSL_SSL_H
113 SSL_CTX *ctx; /* current CTX. */
114 SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
116 char cert_fname[256];
121 static int tcpip_init (void)
123 static int initialized = 0;
129 requested = MAKEWORD(1, 1);
130 if (WSAStartup(requested, &wd))
137 static int tcpip_init (void)
144 * This function is always called through the cs_create() macro.
145 * s >= 0: socket has already been established for us.
147 COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
154 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
156 if (!(sp = (struct tcpip_state *)(p->cprivate =
157 xmalloc(sizeof(tcpip_state)))))
164 p->type = tcpip_type;
165 p->protocol = (enum oid_proto) protocol;
167 p->f_connect = tcpip_connect;
168 p->f_rcvconnect = tcpip_rcvconnect;
169 p->f_get = tcpip_get;
170 p->f_put = tcpip_put;
171 p->f_close = tcpip_close;
172 p->f_more = tcpip_more;
173 p->f_bind = tcpip_bind;
174 p->f_listen = tcpip_listen;
175 p->f_accept = tcpip_accept;
176 p->f_addrstr = tcpip_addrstr;
177 p->f_straddr = tcpip_straddr;
178 p->f_set_blocking = tcpip_set_blocking;
179 p->max_recv_bytes = 5000000;
181 p->state = s < 0 ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
187 #if HAVE_OPENSSL_SSL_H
188 sp->ctx = sp->ctx_alloc = 0;
190 strcpy(sp->cert_fname, "yaz.pem");
197 sp->altsize = sp->altlen = 0;
198 sp->towrite = sp->written = -1;
199 if (protocol == PROTO_WAIS)
200 sp->complete = completeWAIS;
202 sp->complete = cs_complete_auto;
204 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
205 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
210 #if HAVE_OPENSSL_SSL_H
212 COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
217 p = tcpip_type (s, flags, protocol, 0);
223 sp = (tcpip_state *) p->cprivate;
225 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
227 /* note: we don't handle already opened socket in SSL mode - yet */
233 /* resolve using getaddrinfo */
234 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
236 struct addrinfo hints, *res;
241 hints.ai_family = AF_UNSPEC;
242 hints.ai_socktype = SOCK_STREAM;
243 hints.ai_protocol = 0;
244 hints.ai_addrlen = 0;
245 hints.ai_addr = NULL;
246 hints.ai_canonname = NULL;
247 hints.ai_next = NULL;
249 strncpy(host, str, sizeof(host)-1);
250 host[sizeof(host)-1] = 0;
251 if ((p = strchr(host, '/')))
253 if ((p = strrchr(host, ':')))
259 if (!strcmp("@", host))
261 hints.ai_flags = AI_PASSIVE;
262 error = getaddrinfo(0, port, &hints, &res);
266 error = getaddrinfo(host, port, &hints, &res);
274 /* gethostbyname .. old systems */
275 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
280 short int port = default_port;
282 unsigned long tmpadd;
286 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
287 add->sin_family = AF_INET;
288 strncpy(buf, str, sizeof(buf)-1);
289 buf[sizeof(buf)-1] = 0;
290 if ((p = strchr(buf, '/')))
292 if ((p = strrchr(buf, ':')))
297 add->sin_port = htons(port);
298 if (!strcmp("@", buf))
300 add->sin_addr.s_addr = INADDR_ANY;
302 else if ((tmpadd = inet_addr(buf)) != -1)
304 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
306 else if ((hp = gethostbyname(buf)))
308 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
309 sizeof(struct in_addr));
318 void *tcpip_straddr(COMSTACK h, const char *str)
320 tcpip_state *sp = (tcpip_state *)h->cprivate;
321 const char *port = "210";
322 if (h->protocol == PROTO_HTTP)
328 freeaddrinfo(sp->ai);
329 sp->ai = tcpip_getaddrinfo(str, port);
330 if (sp->ai && h->state == CS_ST_UNBND)
333 struct addrinfo *ai = sp->ai;
334 for (; ai; ai = ai->ai_next)
336 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
344 if (!tcpip_set_blocking(h, h->flags))
350 void *tcpip_straddr(COMSTACK h, const char *str)
352 tcpip_state *sp = (tcpip_state *)h->cprivate;
354 if (h->protocol == PROTO_HTTP)
359 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
361 if (h->state == CS_ST_UNBND)
364 s = socket(AF_INET, SOCK_STREAM, 0);
369 if (!tcpip_set_blocking(h, h->flags))
376 int tcpip_more(COMSTACK h)
378 tcpip_state *sp = (tcpip_state *)h->cprivate;
380 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
385 * connect(2) will block (sometimes) - nothing we can do short of doing
386 * weird things like spawning subprocesses or threading or some weird junk
389 int tcpip_connect(COMSTACK h, void *address)
392 tcpip_state *sp = (tcpip_state *)h->cprivate;
394 struct sockaddr_in *add = (struct sockaddr_in *) address;
399 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
401 TRC(fprintf(stderr, "tcpip_connect\n"));
403 if (h->state != CS_ST_UNBND)
405 h->cerrno = CSOUTSTATE;
409 if (sp->ai != (struct addrinfo *) address)
411 h->cerrno = CSOUTSTATE;
416 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
417 * This gives the connect a chance to negotiate with the other side
420 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
425 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
427 recbuflen *= 10; /* lets be optimistic */
428 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
433 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
438 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
443 r = connect(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
444 freeaddrinfo(sp->ai);
447 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
452 if (WSAGetLastError() == WSAEWOULDBLOCK)
454 h->event = CS_CONNECT;
455 h->state = CS_ST_CONNECTING;
456 h->io_pending = CS_WANT_WRITE;
460 if (yaz_errno() == EINPROGRESS)
462 h->event = CS_CONNECT;
463 h->state = CS_ST_CONNECTING;
464 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
471 h->event = CS_CONNECT;
472 h->state = CS_ST_CONNECTING;
474 return tcpip_rcvconnect (h);
480 int tcpip_rcvconnect(COMSTACK h)
482 #if HAVE_OPENSSL_SSL_H
483 tcpip_state *sp = (tcpip_state *)h->cprivate;
485 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
487 if (h->state == CS_ST_DATAXFER)
489 if (h->state != CS_ST_CONNECTING)
491 h->cerrno = CSOUTSTATE;
494 #if HAVE_OPENSSL_SSL_H
495 if (h->type == ssl_type && !sp->ctx)
497 SSL_load_error_strings();
498 SSLeay_add_all_algorithms();
500 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
503 h->cerrno = CSERRORSSL;
513 sp->ssl = SSL_new (sp->ctx);
514 SSL_set_fd (sp->ssl, h->iofile);
516 res = SSL_connect (sp->ssl);
519 int err = SSL_get_error(sp->ssl, res);
520 if (err == SSL_ERROR_WANT_READ)
522 h->io_pending = CS_WANT_READ;
525 if (err == SSL_ERROR_WANT_WRITE)
527 h->io_pending = CS_WANT_WRITE;
530 h->cerrno = CSERRORSSL;
536 h->state = CS_ST_DATAXFER;
540 #define CERTF "ztest.pem"
541 #define KEYF "ztest.pem"
543 static void tcpip_setsockopt (int fd)
549 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
551 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
553 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
555 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
557 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
559 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
564 static int tcpip_bind(COMSTACK h, void *address, int mode)
567 tcpip_state *sp = (tcpip_state *)h->cprivate;
570 struct sockaddr *addr = (struct sockaddr *)address;
579 if (sp->ai != (struct addrinfo *) address)
581 h->cerrno = CSOUTSTATE;
586 #if HAVE_OPENSSL_SSL_H
587 if (h->type == ssl_type && !sp->ctx)
589 SSL_load_error_strings();
590 SSLeay_add_all_algorithms();
592 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
595 h->cerrno = CSERRORSSL;
604 res = SSL_CTX_use_certificate_chain_file(sp->ctx, sp->cert_fname);
607 ERR_print_errors_fp(stderr);
610 res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
614 ERR_print_errors_fp(stderr);
617 res = SSL_CTX_check_private_key (sp->ctx);
620 ERR_print_errors_fp(stderr);
624 TRC (fprintf (stderr, "ssl_bind\n"));
628 TRC (fprintf (stderr, "tcpip_bind\n"));
631 TRC (fprintf (stderr, "tcpip_bind\n"));
634 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
635 &one, sizeof(one)) < 0)
641 tcpip_setsockopt(h->iofile);
643 r = bind(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
644 freeaddrinfo(sp->ai);
647 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
654 /* Allow a maximum-sized backlog of waiting-to-connect clients */
655 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
660 h->state = CS_ST_IDLE;
661 h->event = CS_LISTEN;
665 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
666 int (*check_ip)(void *cd, const char *a, int len, int t),
669 struct sockaddr_in addr;
670 YAZ_SOCKLEN_T len = sizeof(addr);
672 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
673 if (h->state != CS_ST_IDLE)
675 h->cerrno = CSOUTSTATE;
678 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
683 WSAGetLastError() == WSAEWOULDBLOCK
685 yaz_errno() == EWOULDBLOCK
687 #if EAGAIN != EWOULDBLOCK
688 || yaz_errno() == EAGAIN
693 h->cerrno = CSNODATA;
698 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
699 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
702 if (check_ip && (*check_ip)(cd, (const char *) &addr,
703 sizeof(addr), AF_INET))
707 closesocket(h->newfd);
714 h->state = CS_ST_INCON;
715 tcpip_setsockopt (h->newfd);
719 COMSTACK tcpip_accept(COMSTACK h)
722 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
724 unsigned long tru = 1;
727 TRC(fprintf(stderr, "tcpip_accept\n"));
728 if (h->state == CS_ST_INCON)
730 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
734 closesocket(h->newfd);
741 memcpy(cnew, h, sizeof(*h));
742 cnew->iofile = h->newfd;
743 cnew->io_pending = 0;
744 if (!(state = (tcpip_state *)
745 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
751 closesocket(h->newfd);
759 if (!tcpip_set_blocking(cnew, cnew->flags))
765 closesocket(h->newfd);
777 state->altsize = state->altlen = 0;
778 state->towrite = state->written = -1;
779 state->complete = st->complete;
783 cnew->state = CS_ST_ACCEPT;
784 h->state = CS_ST_IDLE;
786 #if HAVE_OPENSSL_SSL_H
787 state->ctx = st->ctx;
788 state->ctx_alloc = 0;
789 state->ssl = st->ssl;
792 state->ssl = SSL_new (state->ctx);
793 SSL_set_fd (state->ssl, cnew->iofile);
798 if (h->state == CS_ST_ACCEPT)
800 #if HAVE_OPENSSL_SSL_H
801 tcpip_state *state = (tcpip_state *)h->cprivate;
804 int res = SSL_accept (state->ssl);
805 TRC(fprintf(stderr, "SSL_accept\n"));
808 int err = SSL_get_error(state->ssl, res);
809 if (err == SSL_ERROR_WANT_READ)
811 h->io_pending = CS_WANT_READ;
814 if (err == SSL_ERROR_WANT_WRITE)
816 h->io_pending = CS_WANT_WRITE;
827 h->cerrno = CSOUTSTATE;
831 h->state = CS_ST_DATAXFER;
836 #define CS_TCPIP_BUFCHUNK 4096
839 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
840 * 0=connection closed.
842 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
844 tcpip_state *sp = (tcpip_state *)h->cprivate;
846 int tmpi, berlen, rest, req, tomove;
847 int hasread = 0, res;
849 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
850 if (sp->altlen) /* switch buffers */
852 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
853 (unsigned) sp->altbuf));
857 *bufsize = sp->altsize;
858 hasread = sp->altlen;
864 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
868 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
874 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
875 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
882 /* unfortunatly, sun sometimes forgets to set errno in recv
883 when EWOULDBLOCK etc. would be required (res = -1) */
885 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
886 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
889 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
890 strerror(yaz_errno())));
892 if (WSAGetLastError() == WSAEWOULDBLOCK)
894 h->io_pending = CS_WANT_READ;
903 if (yaz_errno() == EWOULDBLOCK
905 #if EAGAIN != EWOULDBLOCK
906 || yaz_errno() == EAGAIN
909 || yaz_errno() == EINPROGRESS
911 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
915 h->io_pending = CS_WANT_READ;
918 else if (yaz_errno() == 0)
930 if (hasread > h->max_recv_bytes)
932 h->cerrno = CSBUFSIZE;
936 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
938 /* move surplus buffer (or everything if we didn't get a BER rec.) */
939 if (hasread > berlen)
941 tomove = req = hasread - berlen;
942 rest = tomove % CS_TCPIP_BUFCHUNK;
944 req += CS_TCPIP_BUFCHUNK - rest;
947 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
952 } else if (sp->altsize < req)
953 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
958 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
959 (unsigned) sp->altbuf));
960 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
962 if (berlen < CS_TCPIP_BUFCHUNK - 1)
963 *(*buf + berlen) = '\0';
964 return berlen ? berlen : 1;
968 #if HAVE_OPENSSL_SSL_H
970 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
971 * 0=connection closed.
973 int ssl_get(COMSTACK h, char **buf, int *bufsize)
975 tcpip_state *sp = (tcpip_state *)h->cprivate;
977 int tmpi, berlen, rest, req, tomove;
978 int hasread = 0, res;
980 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
981 if (sp->altlen) /* switch buffers */
983 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
984 (unsigned) sp->altbuf));
988 *bufsize = sp->altsize;
989 hasread = sp->altlen;
995 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
999 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
1002 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
1003 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
1005 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
1006 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
1009 int ssl_err = SSL_get_error(sp->ssl, res);
1010 if (ssl_err == SSL_ERROR_WANT_READ)
1012 h->io_pending = CS_WANT_READ;
1015 if (ssl_err == SSL_ERROR_WANT_WRITE)
1017 h->io_pending = CS_WANT_WRITE;
1022 h->cerrno = CSERRORSSL;
1027 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1029 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1030 if (hasread > berlen)
1032 tomove = req = hasread - berlen;
1033 rest = tomove % CS_TCPIP_BUFCHUNK;
1035 req += CS_TCPIP_BUFCHUNK - rest;
1038 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1040 } else if (sp->altsize < req)
1041 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1043 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1044 (unsigned) sp->altbuf));
1045 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1047 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1048 *(*buf + berlen) = '\0';
1049 return berlen ? berlen : 1;
1054 * Returns 1, 0 or -1
1055 * In nonblocking mode, you must call again with same buffer while
1056 * return value is 1.
1058 int tcpip_put(COMSTACK h, char *buf, int size)
1061 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1063 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
1066 if (state->towrite < 0)
1068 state->towrite = size;
1071 else if (state->towrite != size)
1073 h->cerrno = CSWRONGBUF;
1076 while (state->towrite > state->written)
1079 send(h->iofile, buf + state->written, size -
1090 WSAGetLastError() == WSAEWOULDBLOCK
1092 yaz_errno() == EWOULDBLOCK
1094 #if EAGAIN != EWOULDBLOCK
1095 || yaz_errno() == EAGAIN
1099 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1101 || yaz_errno() == EINPROGRESS
1105 TRC(fprintf(stderr, " Flow control stop\n"));
1106 h->io_pending = CS_WANT_WRITE;
1109 h->cerrno = CSYSERR;
1112 state->written += res;
1113 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1114 res, state->written, size));
1116 state->towrite = state->written = -1;
1117 TRC(fprintf(stderr, " Ok\n"));
1122 #if HAVE_OPENSSL_SSL_H
1124 * Returns 1, 0 or -1
1125 * In nonblocking mode, you must call again with same buffer while
1126 * return value is 1.
1128 int ssl_put(COMSTACK h, char *buf, int size)
1131 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1133 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1136 if (state->towrite < 0)
1138 state->towrite = size;
1141 else if (state->towrite != size)
1143 h->cerrno = CSWRONGBUF;
1146 while (state->towrite > state->written)
1148 res = SSL_write (state->ssl, buf + state->written,
1149 size - state->written);
1152 int ssl_err = SSL_get_error(state->ssl, res);
1153 if (ssl_err == SSL_ERROR_WANT_READ)
1155 h->io_pending = CS_WANT_READ;
1158 if (ssl_err == SSL_ERROR_WANT_WRITE)
1160 h->io_pending = CS_WANT_WRITE;
1163 h->cerrno = CSERRORSSL;
1166 state->written += res;
1167 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1168 res, state->written, size));
1170 state->towrite = state->written = -1;
1171 TRC(fprintf(stderr, " Ok\n"));
1176 int tcpip_close(COMSTACK h)
1178 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1180 TRC(fprintf(stderr, "tcpip_close\n"));
1181 if (h->iofile != -1)
1183 #if HAVE_OPENSSL_SSL_H
1186 SSL_shutdown (sp->ssl);
1190 closesocket(h->iofile);
1197 #if HAVE_OPENSSL_SSL_H
1200 TRC (fprintf(stderr, "SSL_free\n"));
1205 SSL_CTX_free (sp->ctx_alloc);
1207 #if HAVE_GETADDRINFO
1209 freeaddrinfo(sp->ai);
1216 char *tcpip_addrstr(COMSTACK h)
1218 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1219 char *r = 0, *buf = sp->buf;
1221 #if HAVE_GETADDRINFO
1223 struct sockaddr_storage addr;
1224 YAZ_SOCKLEN_T len = sizeof(addr);
1226 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1228 h->cerrno = CSYSERR;
1231 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
1233 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1242 struct sockaddr_in addr;
1243 YAZ_SOCKLEN_T len = sizeof(addr);
1244 struct hostent *host;
1246 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1248 h->cerrno = CSYSERR;
1251 if (!(h->flags & CS_FLAGS_NUMERICHOST))
1253 if ((host = gethostbyaddr((char*)&addr.sin_addr,
1254 sizeof(addr.sin_addr),
1256 r = (char*) host->h_name;
1259 r = inet_ntoa(addr.sin_addr);
1262 if (h->protocol == PROTO_HTTP)
1263 sprintf(buf, "http:%s", r);
1265 sprintf(buf, "tcp:%s", r);
1266 #if HAVE_OPENSSL_SSL_H
1269 if (h->protocol == PROTO_HTTP)
1270 sprintf(buf, "https:%s", r);
1272 sprintf(buf, "ssl:%s", r);
1278 int static tcpip_set_blocking(COMSTACK p, int flags)
1283 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1284 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1287 flag = fcntl(p->iofile, F_GETFL, 0);
1288 if (flags & CS_FLAGS_BLOCKING)
1289 flag = flag & ~O_NONBLOCK; /* blocking */
1292 flag = flag | O_NONBLOCK; /* non-blocking */
1293 signal(SIGPIPE, SIG_IGN);
1295 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1302 #if HAVE_OPENSSL_SSL_H
1303 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1305 struct tcpip_state *sp;
1306 if (!cs || cs->type != ssl_type)
1308 sp = (struct tcpip_state *) cs->cprivate;
1311 sp->ctx = (SSL_CTX *) ctx;
1315 void *cs_get_ssl(COMSTACK cs)
1317 struct tcpip_state *sp;
1318 if (!cs || cs->type != ssl_type)
1320 sp = (struct tcpip_state *) cs->cprivate;
1324 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1326 struct tcpip_state *sp;
1327 if (!cs || cs->type != ssl_type)
1329 sp = (struct tcpip_state *) cs->cprivate;
1330 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1331 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1335 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1337 SSL *ssl = (SSL *) cs_get_ssl(cs);
1340 X509 *server_cert = SSL_get_peer_certificate (ssl);
1343 BIO *bio = BIO_new(BIO_s_mem());
1345 /* get PEM buffer in memory */
1346 PEM_write_bio_X509(bio, server_cert);
1347 *len = BIO_get_mem_data(bio, &pem_buf);
1348 *buf = (char *) xmalloc(*len);
1349 memcpy(*buf, pem_buf, *len);
1357 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1362 void *cs_get_ssl(COMSTACK cs)
1367 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1372 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1381 * indent-tabs-mode: nil
1383 * vim: shiftwidth=4 tabstop=8 expandtab