2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.31 2006-09-21 15:55:25 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 for (; ai; ai = ai->ai_next)
329 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
337 if (!tcpip_set_blocking(h, h->flags))
343 void *tcpip_straddr(COMSTACK h, const char *str)
345 tcpip_state *sp = (tcpip_state *)h->cprivate;
347 if (h->protocol == PROTO_HTTP)
352 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
354 if (h->state == CS_ST_UNBND)
357 s = socket(AF_INET, SOCK_STREAM, 0);
362 if (!tcpip_set_blocking(h, h->flags))
369 int tcpip_more(COMSTACK h)
371 tcpip_state *sp = (tcpip_state *)h->cprivate;
373 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
378 * connect(2) will block (sometimes) - nothing we can do short of doing
379 * weird things like spawning subprocesses or threading or some weird junk
382 int tcpip_connect(COMSTACK h, void *address)
385 tcpip_state *sp = (tcpip_state *)h->cprivate;
387 struct sockaddr_in *add = (struct sockaddr_in *) address;
392 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
394 TRC(fprintf(stderr, "tcpip_connect\n"));
396 if (h->state != CS_ST_UNBND)
398 h->cerrno = CSOUTSTATE;
402 if (sp->ai != (struct addrinfo *) address)
404 h->cerrno = CSOUTSTATE;
409 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
410 * This gives the connect a chance to negotiate with the other side
413 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
418 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
420 recbuflen *= 10; /* lets be optimistic */
421 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
426 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
431 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
436 r = connect(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
437 freeaddrinfo(sp->ai);
440 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
445 if (WSAGetLastError() == WSAEWOULDBLOCK)
447 h->event = CS_CONNECT;
448 h->state = CS_ST_CONNECTING;
449 h->io_pending = CS_WANT_WRITE;
453 if (yaz_errno() == EINPROGRESS)
455 h->event = CS_CONNECT;
456 h->state = CS_ST_CONNECTING;
457 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
464 h->event = CS_CONNECT;
465 h->state = CS_ST_CONNECTING;
467 return tcpip_rcvconnect (h);
473 int tcpip_rcvconnect(COMSTACK h)
475 #if HAVE_OPENSSL_SSL_H
476 tcpip_state *sp = (tcpip_state *)h->cprivate;
478 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
480 if (h->state == CS_ST_DATAXFER)
482 if (h->state != CS_ST_CONNECTING)
484 h->cerrno = CSOUTSTATE;
487 #if HAVE_OPENSSL_SSL_H
488 if (h->type == ssl_type && !sp->ctx)
490 SSL_load_error_strings();
491 SSLeay_add_all_algorithms();
493 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
496 h->cerrno = CSERRORSSL;
506 sp->ssl = SSL_new (sp->ctx);
507 SSL_set_fd (sp->ssl, h->iofile);
509 res = SSL_connect (sp->ssl);
512 int err = SSL_get_error(sp->ssl, res);
513 if (err == SSL_ERROR_WANT_READ)
515 h->io_pending = CS_WANT_READ;
518 if (err == SSL_ERROR_WANT_WRITE)
520 h->io_pending = CS_WANT_WRITE;
523 h->cerrno = CSERRORSSL;
529 h->state = CS_ST_DATAXFER;
533 #define CERTF "ztest.pem"
534 #define KEYF "ztest.pem"
536 static void tcpip_setsockopt (int fd)
542 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
544 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
546 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
548 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
550 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
552 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
557 static int tcpip_bind(COMSTACK h, void *address, int mode)
560 tcpip_state *sp = (tcpip_state *)h->cprivate;
563 struct sockaddr *addr = (struct sockaddr *)address;
572 if (sp->ai != (struct addrinfo *) address)
574 h->cerrno = CSOUTSTATE;
579 #if HAVE_OPENSSL_SSL_H
580 if (h->type == ssl_type && !sp->ctx)
582 SSL_load_error_strings();
583 SSLeay_add_all_algorithms();
585 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
588 h->cerrno = CSERRORSSL;
597 res = SSL_CTX_use_certificate_chain_file(sp->ctx, sp->cert_fname);
600 ERR_print_errors_fp(stderr);
603 res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
607 ERR_print_errors_fp(stderr);
610 res = SSL_CTX_check_private_key (sp->ctx);
613 ERR_print_errors_fp(stderr);
617 TRC (fprintf (stderr, "ssl_bind\n"));
621 TRC (fprintf (stderr, "tcpip_bind\n"));
624 TRC (fprintf (stderr, "tcpip_bind\n"));
627 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
628 &one, sizeof(one)) < 0)
634 tcpip_setsockopt(h->iofile);
636 r = bind(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
637 freeaddrinfo(sp->ai);
640 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
647 /* Allow a maximum-sized backlog of waiting-to-connect clients */
648 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
653 h->state = CS_ST_IDLE;
654 h->event = CS_LISTEN;
658 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
659 int (*check_ip)(void *cd, const char *a, int len, int t),
662 struct sockaddr_in addr;
663 YAZ_SOCKLEN_T len = sizeof(addr);
665 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
666 if (h->state != CS_ST_IDLE)
668 h->cerrno = CSOUTSTATE;
671 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
676 WSAGetLastError() == WSAEWOULDBLOCK
678 yaz_errno() == EWOULDBLOCK
680 #if EAGAIN != EWOULDBLOCK
681 || yaz_errno() == EAGAIN
686 h->cerrno = CSNODATA;
691 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
692 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
695 if (check_ip && (*check_ip)(cd, (const char *) &addr,
696 sizeof(addr), AF_INET))
700 closesocket(h->newfd);
707 h->state = CS_ST_INCON;
708 tcpip_setsockopt (h->newfd);
712 COMSTACK tcpip_accept(COMSTACK h)
715 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
717 unsigned long tru = 1;
720 TRC(fprintf(stderr, "tcpip_accept\n"));
721 if (h->state == CS_ST_INCON)
723 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
727 closesocket(h->newfd);
734 memcpy(cnew, h, sizeof(*h));
735 cnew->iofile = h->newfd;
736 cnew->io_pending = 0;
737 if (!(state = (tcpip_state *)
738 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
744 closesocket(h->newfd);
752 if (!tcpip_set_blocking(cnew, cnew->flags))
758 closesocket(h->newfd);
770 state->altsize = state->altlen = 0;
771 state->towrite = state->written = -1;
772 state->complete = st->complete;
776 cnew->state = CS_ST_ACCEPT;
777 h->state = CS_ST_IDLE;
779 #if HAVE_OPENSSL_SSL_H
780 state->ctx = st->ctx;
781 state->ctx_alloc = 0;
782 state->ssl = st->ssl;
785 state->ssl = SSL_new (state->ctx);
786 SSL_set_fd (state->ssl, cnew->iofile);
791 if (h->state == CS_ST_ACCEPT)
793 #if HAVE_OPENSSL_SSL_H
794 tcpip_state *state = (tcpip_state *)h->cprivate;
797 int res = SSL_accept (state->ssl);
798 TRC(fprintf(stderr, "SSL_accept\n"));
801 int err = SSL_get_error(state->ssl, res);
802 if (err == SSL_ERROR_WANT_READ)
804 h->io_pending = CS_WANT_READ;
807 if (err == SSL_ERROR_WANT_WRITE)
809 h->io_pending = CS_WANT_WRITE;
820 h->cerrno = CSOUTSTATE;
824 h->state = CS_ST_DATAXFER;
829 #define CS_TCPIP_BUFCHUNK 4096
832 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
833 * 0=connection closed.
835 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
837 tcpip_state *sp = (tcpip_state *)h->cprivate;
839 int tmpi, berlen, rest, req, tomove;
840 int hasread = 0, res;
842 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
843 if (sp->altlen) /* switch buffers */
845 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
846 (unsigned) sp->altbuf));
850 *bufsize = sp->altsize;
851 hasread = sp->altlen;
857 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
861 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
867 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
868 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
875 /* unfortunatly, sun sometimes forgets to set errno in recv
876 when EWOULDBLOCK etc. would be required (res = -1) */
878 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
879 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
882 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
883 strerror(yaz_errno())));
885 if (WSAGetLastError() == WSAEWOULDBLOCK)
887 h->io_pending = CS_WANT_READ;
896 if (yaz_errno() == EWOULDBLOCK
898 #if EAGAIN != EWOULDBLOCK
899 || yaz_errno() == EAGAIN
902 || yaz_errno() == EINPROGRESS
904 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
908 h->io_pending = CS_WANT_READ;
911 else if (yaz_errno() == 0)
923 if (hasread > h->max_recv_bytes)
925 h->cerrno = CSBUFSIZE;
929 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
931 /* move surplus buffer (or everything if we didn't get a BER rec.) */
932 if (hasread > berlen)
934 tomove = req = hasread - berlen;
935 rest = tomove % CS_TCPIP_BUFCHUNK;
937 req += CS_TCPIP_BUFCHUNK - rest;
940 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
945 } else if (sp->altsize < req)
946 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
951 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
952 (unsigned) sp->altbuf));
953 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
955 if (berlen < CS_TCPIP_BUFCHUNK - 1)
956 *(*buf + berlen) = '\0';
957 return berlen ? berlen : 1;
961 #if HAVE_OPENSSL_SSL_H
963 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
964 * 0=connection closed.
966 int ssl_get(COMSTACK h, char **buf, int *bufsize)
968 tcpip_state *sp = (tcpip_state *)h->cprivate;
970 int tmpi, berlen, rest, req, tomove;
971 int hasread = 0, res;
973 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
974 if (sp->altlen) /* switch buffers */
976 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
977 (unsigned) sp->altbuf));
981 *bufsize = sp->altsize;
982 hasread = sp->altlen;
988 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
992 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
995 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
996 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
998 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
999 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
1002 int ssl_err = SSL_get_error(sp->ssl, res);
1003 if (ssl_err == SSL_ERROR_WANT_READ)
1005 h->io_pending = CS_WANT_READ;
1008 if (ssl_err == SSL_ERROR_WANT_WRITE)
1010 h->io_pending = CS_WANT_WRITE;
1015 h->cerrno = CSERRORSSL;
1020 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
1022 /* move surplus buffer (or everything if we didn't get a BER rec.) */
1023 if (hasread > berlen)
1025 tomove = req = hasread - berlen;
1026 rest = tomove % CS_TCPIP_BUFCHUNK;
1028 req += CS_TCPIP_BUFCHUNK - rest;
1031 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1033 } else if (sp->altsize < req)
1034 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1036 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1037 (unsigned) sp->altbuf));
1038 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1040 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1041 *(*buf + berlen) = '\0';
1042 return berlen ? berlen : 1;
1047 * Returns 1, 0 or -1
1048 * In nonblocking mode, you must call again with same buffer while
1049 * return value is 1.
1051 int tcpip_put(COMSTACK h, char *buf, int size)
1054 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1056 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
1059 if (state->towrite < 0)
1061 state->towrite = size;
1064 else if (state->towrite != size)
1066 h->cerrno = CSWRONGBUF;
1069 while (state->towrite > state->written)
1072 send(h->iofile, buf + state->written, size -
1083 WSAGetLastError() == WSAEWOULDBLOCK
1085 yaz_errno() == EWOULDBLOCK
1087 #if EAGAIN != EWOULDBLOCK
1088 || yaz_errno() == EAGAIN
1092 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1094 || yaz_errno() == EINPROGRESS
1098 TRC(fprintf(stderr, " Flow control stop\n"));
1099 h->io_pending = CS_WANT_WRITE;
1102 h->cerrno = CSYSERR;
1105 state->written += res;
1106 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1107 res, state->written, size));
1109 state->towrite = state->written = -1;
1110 TRC(fprintf(stderr, " Ok\n"));
1115 #if HAVE_OPENSSL_SSL_H
1117 * Returns 1, 0 or -1
1118 * In nonblocking mode, you must call again with same buffer while
1119 * return value is 1.
1121 int ssl_put(COMSTACK h, char *buf, int size)
1124 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1126 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1129 if (state->towrite < 0)
1131 state->towrite = size;
1134 else if (state->towrite != size)
1136 h->cerrno = CSWRONGBUF;
1139 while (state->towrite > state->written)
1141 res = SSL_write (state->ssl, buf + state->written,
1142 size - state->written);
1145 int ssl_err = SSL_get_error(state->ssl, res);
1146 if (ssl_err == SSL_ERROR_WANT_READ)
1148 h->io_pending = CS_WANT_READ;
1151 if (ssl_err == SSL_ERROR_WANT_WRITE)
1153 h->io_pending = CS_WANT_WRITE;
1156 h->cerrno = CSERRORSSL;
1159 state->written += res;
1160 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1161 res, state->written, size));
1163 state->towrite = state->written = -1;
1164 TRC(fprintf(stderr, " Ok\n"));
1169 int tcpip_close(COMSTACK h)
1171 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1173 TRC(fprintf(stderr, "tcpip_close\n"));
1174 if (h->iofile != -1)
1176 #if HAVE_OPENSSL_SSL_H
1179 SSL_shutdown (sp->ssl);
1183 closesocket(h->iofile);
1190 #if HAVE_OPENSSL_SSL_H
1193 TRC (fprintf(stderr, "SSL_free\n"));
1198 SSL_CTX_free (sp->ctx_alloc);
1200 #if HAVE_GETADDRINFO
1202 freeaddrinfo(sp->ai);
1209 char *tcpip_addrstr(COMSTACK h)
1211 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1212 char *r = 0, *buf = sp->buf;
1214 #if HAVE_GETADDRINFO
1216 struct sockaddr_storage addr;
1217 YAZ_SOCKLEN_T len = sizeof(addr);
1219 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1221 h->cerrno = CSYSERR;
1224 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
1226 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1235 struct sockaddr_in addr;
1236 YAZ_SOCKLEN_T len = sizeof(addr);
1237 struct hostent *host;
1239 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1241 h->cerrno = CSYSERR;
1244 if (!(h->flags & CS_FLAGS_NUMERICHOST))
1246 if ((host = gethostbyaddr((char*)&addr.sin_addr,
1247 sizeof(addr.sin_addr),
1249 r = (char*) host->h_name;
1252 r = inet_ntoa(addr.sin_addr);
1255 if (h->protocol == PROTO_HTTP)
1256 sprintf(buf, "http:%s", r);
1258 sprintf(buf, "tcp:%s", r);
1259 #if HAVE_OPENSSL_SSL_H
1262 if (h->protocol == PROTO_HTTP)
1263 sprintf(buf, "https:%s", r);
1265 sprintf(buf, "ssl:%s", r);
1271 int static tcpip_set_blocking(COMSTACK p, int flags)
1276 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1277 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1280 flag = fcntl(p->iofile, F_GETFL, 0);
1281 if (flags & CS_FLAGS_BLOCKING)
1282 flag = flag & ~O_NONBLOCK; /* blocking */
1285 flag = flag | O_NONBLOCK; /* non-blocking */
1286 signal(SIGPIPE, SIG_IGN);
1288 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1295 #if HAVE_OPENSSL_SSL_H
1296 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1298 struct tcpip_state *sp;
1299 if (!cs || cs->type != ssl_type)
1301 sp = (struct tcpip_state *) cs->cprivate;
1304 sp->ctx = (SSL_CTX *) ctx;
1308 void *cs_get_ssl(COMSTACK cs)
1310 struct tcpip_state *sp;
1311 if (!cs || cs->type != ssl_type)
1313 sp = (struct tcpip_state *) cs->cprivate;
1317 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1319 struct tcpip_state *sp;
1320 if (!cs || cs->type != ssl_type)
1322 sp = (struct tcpip_state *) cs->cprivate;
1323 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1324 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1328 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1330 SSL *ssl = (SSL *) cs_get_ssl(cs);
1333 X509 *server_cert = SSL_get_peer_certificate (ssl);
1336 BIO *bio = BIO_new(BIO_s_mem());
1338 /* get PEM buffer in memory */
1339 PEM_write_bio_X509(bio, server_cert);
1340 *len = BIO_get_mem_data(bio, &pem_buf);
1341 *buf = (char *) xmalloc(*len);
1342 memcpy(*buf, pem_buf, *len);
1350 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1355 void *cs_get_ssl(COMSTACK cs)
1360 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1365 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1374 * indent-tabs-mode: nil
1376 * vim: shiftwidth=4 tabstop=8 expandtab