2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.11 2004-10-15 00:19:01 adam Exp $
9 * \brief Implements TCP/IP + SSL COMSTACK.
24 #if HAVE_OPENSSL_SSL_H
25 #include <openssl/ssl.h>
26 #include <openssl/err.h>
29 #include <yaz/comstack.h>
30 #include <yaz/tcpip.h>
36 #include <netinet/tcp.h>
39 static int tcpip_close(COMSTACK h);
40 static int tcpip_put(COMSTACK h, char *buf, int size);
41 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
42 static int tcpip_connect(COMSTACK h, void *address);
43 static int tcpip_more(COMSTACK h);
44 static int tcpip_rcvconnect(COMSTACK h);
45 static int tcpip_bind(COMSTACK h, void *address, int mode);
46 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
47 int (*check_ip)(void *cd, const char *a, int len, int type),
49 static int tcpip_set_blocking(COMSTACK p, int blocking);
51 #if HAVE_OPENSSL_SSL_H
52 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
53 static int ssl_put(COMSTACK h, char *buf, int size);
56 static COMSTACK tcpip_accept(COMSTACK h);
57 static char *tcpip_addrstr(COMSTACK h);
58 static void *tcpip_straddr(COMSTACK h, const char *str);
67 #define YAZ_SOCKLEN_T int
70 /* this state is used for both SSL and straight TCP/IP */
71 typedef struct tcpip_state
73 char *altbuf; /* alternate buffer for surplus data */
74 int altsize; /* size as xmalloced */
75 int altlen; /* length of data or 0 if none */
77 int written; /* -1 if we aren't writing */
78 int towrite; /* to verify against user input */
79 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
80 struct sockaddr_in addr; /* returned by cs_straddr */
81 char buf[128]; /* returned by cs_addrstr */
82 #if HAVE_OPENSSL_SSL_H
83 SSL_CTX *ctx; /* current CTX. */
84 SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
91 static int tcpip_init (void)
93 static int initialized = 0;
99 requested = MAKEWORD(1, 1);
100 if (WSAStartup(requested, &wd))
107 static int tcpip_init (void)
114 * This function is always called through the cs_create() macro.
115 * s >= 0: socket has already been established for us.
117 COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
123 unsigned long tru = 1;
130 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
136 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
138 if (!(sp = (struct tcpip_state *)(p->cprivate =
139 xmalloc(sizeof(tcpip_state)))))
142 if (!((p->blocking = blocking)&1))
145 if (ioctlsocket(s, FIONBIO, &tru) < 0)
148 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
151 signal (SIGPIPE, SIG_IGN);
158 p->type = tcpip_type;
159 p->protocol = (enum oid_proto) protocol;
161 p->f_connect = tcpip_connect;
162 p->f_rcvconnect = tcpip_rcvconnect;
163 p->f_get = tcpip_get;
164 p->f_put = tcpip_put;
165 p->f_close = tcpip_close;
166 p->f_more = tcpip_more;
167 p->f_bind = tcpip_bind;
168 p->f_listen = tcpip_listen;
169 p->f_accept = tcpip_accept;
170 p->f_addrstr = tcpip_addrstr;
171 p->f_straddr = tcpip_straddr;
172 p->f_set_blocking = tcpip_set_blocking;
174 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
179 #if HAVE_OPENSSL_SSL_H
180 sp->ctx = sp->ctx_alloc = 0;
182 strcpy(sp->cert_fname, "yaz.pem");
186 sp->altsize = sp->altlen = 0;
187 sp->towrite = sp->written = -1;
188 if (protocol == PROTO_WAIS)
189 sp->complete = completeWAIS;
191 sp->complete = cs_complete_auto;
193 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
194 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
199 #if HAVE_OPENSSL_SSL_H
201 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
206 p = tcpip_type (s, blocking, protocol, 0);
212 sp = (tcpip_state *) p->cprivate;
214 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
216 /* note: we don't handle already opened socket in SSL mode - yet */
221 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
226 short int port = default_port;
231 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
232 add->sin_family = AF_INET;
233 strncpy(buf, str, 511);
235 if ((p = strchr(buf, '/')))
237 if ((p = strchr(buf, ':')))
242 add->sin_port = htons(port);
243 if (!strcmp("@", buf))
244 add->sin_addr.s_addr = INADDR_ANY;
245 else if ((hp = gethostbyname(buf)))
246 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
247 sizeof(struct in_addr));
248 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
249 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
255 void *tcpip_straddr(COMSTACK h, const char *str)
257 tcpip_state *sp = (tcpip_state *)h->cprivate;
260 if (h->protocol == PROTO_HTTP)
263 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
268 struct sockaddr_in *tcpip_strtoaddr(const char *str)
270 static struct sockaddr_in add;
272 if (!tcpip_strtoaddr_ex (str, &add, 210))
277 int tcpip_more(COMSTACK h)
279 tcpip_state *sp = (tcpip_state *)h->cprivate;
281 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
286 * connect(2) will block (sometimes) - nothing we can do short of doing
287 * weird things like spawning subprocesses or threading or some weird junk
290 int tcpip_connect(COMSTACK h, void *address)
292 struct sockaddr_in *add = (struct sockaddr_in *)address;
296 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
298 TRC(fprintf(stderr, "tcpip_connect\n"));
300 if (h->state != CS_ST_UNBND)
302 h->cerrno = CSOUTSTATE;
306 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
307 * This gives the connect a chance to negotiate with the other side
310 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
315 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
317 recbuflen *= 10; /* lets be optimistic */
318 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
323 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
328 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
331 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
335 if (WSAGetLastError() == WSAEWOULDBLOCK)
337 h->event = CS_CONNECT;
338 h->state = CS_ST_CONNECTING;
339 h->io_pending = CS_WANT_WRITE;
343 if (yaz_errno() == EINPROGRESS)
345 h->event = CS_CONNECT;
346 h->state = CS_ST_CONNECTING;
347 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
354 h->event = CS_CONNECT;
355 h->state = CS_ST_CONNECTING;
357 return tcpip_rcvconnect (h);
363 int tcpip_rcvconnect(COMSTACK h)
365 #if HAVE_OPENSSL_SSL_H
366 tcpip_state *sp = (tcpip_state *)h->cprivate;
368 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
370 if (h->state == CS_ST_DATAXFER)
372 if (h->state != CS_ST_CONNECTING)
374 h->cerrno = CSOUTSTATE;
377 #if HAVE_OPENSSL_SSL_H
378 if (h->type == ssl_type && !sp->ctx)
380 SSL_load_error_strings();
381 SSLeay_add_all_algorithms();
383 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
386 h->cerrno = CSERRORSSL;
396 sp->ssl = SSL_new (sp->ctx);
397 SSL_set_fd (sp->ssl, h->iofile);
399 res = SSL_connect (sp->ssl);
402 int err = SSL_get_error(sp->ssl, res);
403 if (err == SSL_ERROR_WANT_READ)
405 h->io_pending = CS_WANT_READ;
408 if (err == SSL_ERROR_WANT_WRITE)
410 h->io_pending = CS_WANT_WRITE;
413 h->cerrno = CSERRORSSL;
419 h->state = CS_ST_DATAXFER;
423 #define CERTF "ztest.pem"
424 #define KEYF "ztest.pem"
426 static void tcpip_setsockopt (int fd)
432 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
434 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
436 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
438 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
440 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
442 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
447 static int tcpip_bind(COMSTACK h, void *address, int mode)
449 struct sockaddr *addr = (struct sockaddr *)address;
453 unsigned long one = 1;
456 #if HAVE_OPENSSL_SSL_H
457 tcpip_state *sp = (tcpip_state *)h->cprivate;
458 if (h->type == ssl_type && !sp->ctx)
460 SSL_load_error_strings();
461 SSLeay_add_all_algorithms();
463 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
466 h->cerrno = CSERRORSSL;
475 res = SSL_CTX_use_certificate_file (sp->ctx, sp->cert_fname,
479 ERR_print_errors_fp(stderr);
482 res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
486 ERR_print_errors_fp(stderr);
489 res = SSL_CTX_check_private_key (sp->ctx);
492 ERR_print_errors_fp(stderr);
496 TRC (fprintf (stderr, "ssl_bind\n"));
500 TRC (fprintf (stderr, "tcpip_bind\n"));
503 TRC (fprintf (stderr, "tcpip_bind\n"));
506 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
507 &one, sizeof(one)) < 0)
513 tcpip_setsockopt(h->iofile);
514 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)))
519 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
524 h->state = CS_ST_IDLE;
525 h->event = CS_LISTEN;
529 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
530 int (*check_ip)(void *cd, const char *a, int len, int t),
533 struct sockaddr_in addr;
534 YAZ_SOCKLEN_T len = sizeof(addr);
536 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
537 if (h->state != CS_ST_IDLE)
539 h->cerrno = CSOUTSTATE;
542 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
547 WSAGetLastError() == WSAEWOULDBLOCK
549 yaz_errno() == EWOULDBLOCK
551 #if EAGAIN != EWOULDBLOCK
552 || yaz_errno() == EAGAIN
557 h->cerrno = CSNODATA;
562 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
563 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
566 if (check_ip && (*check_ip)(cd, (const char *) &addr,
567 sizeof(addr), AF_INET))
571 closesocket(h->newfd);
578 h->state = CS_ST_INCON;
579 tcpip_setsockopt (h->newfd);
583 COMSTACK tcpip_accept(COMSTACK h)
586 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
588 unsigned long tru = 1;
591 TRC(fprintf(stderr, "tcpip_accept\n"));
592 if (h->state == CS_ST_INCON)
594 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
598 closesocket(h->newfd);
605 memcpy(cnew, h, sizeof(*h));
606 cnew->iofile = h->newfd;
607 cnew->io_pending = 0;
608 if (!(state = (tcpip_state *)
609 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
615 closesocket(h->newfd);
623 if (!(cnew->blocking&1) &&
625 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
627 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
635 closesocket(h->newfd);
647 state->altsize = state->altlen = 0;
648 state->towrite = state->written = -1;
649 state->complete = st->complete;
650 cnew->state = CS_ST_ACCEPT;
651 h->state = CS_ST_IDLE;
653 #if HAVE_OPENSSL_SSL_H
654 state->ctx = st->ctx;
655 state->ctx_alloc = 0;
656 state->ssl = st->ssl;
659 state->ssl = SSL_new (state->ctx);
660 SSL_set_fd (state->ssl, cnew->iofile);
665 if (h->state == CS_ST_ACCEPT)
667 #if HAVE_OPENSSL_SSL_H
668 tcpip_state *state = (tcpip_state *)h->cprivate;
671 int res = SSL_accept (state->ssl);
672 TRC(fprintf(stderr, "SSL_accept\n"));
675 int err = SSL_get_error(state->ssl, res);
676 if (err == SSL_ERROR_WANT_READ)
678 h->io_pending = CS_WANT_READ;
681 if (err == SSL_ERROR_WANT_WRITE)
683 h->io_pending = CS_WANT_WRITE;
694 h->cerrno = CSOUTSTATE;
698 h->state = CS_ST_DATAXFER;
703 #define CS_TCPIP_BUFCHUNK 4096
706 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
707 * 0=connection closed.
709 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
711 tcpip_state *sp = (tcpip_state *)h->cprivate;
713 int tmpi, berlen, rest, req, tomove;
714 int hasread = 0, res;
716 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
717 if (sp->altlen) /* switch buffers */
719 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
720 (unsigned) sp->altbuf));
724 *bufsize = sp->altsize;
725 hasread = sp->altlen;
731 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
735 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
738 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
739 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
743 /* unfortunatly, sun sometimes forgets to set errno in recv
744 when EWOULDBLOCK etc. would be required (res = -1) */
746 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
747 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
750 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
751 strerror(yaz_errno())));
753 if (WSAGetLastError() == WSAEWOULDBLOCK)
755 h->io_pending = CS_WANT_READ;
761 if (yaz_errno() == EWOULDBLOCK
763 #if EAGAIN != EWOULDBLOCK
764 || yaz_errno() == EAGAIN
767 || yaz_errno() == EINPROGRESS
769 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
773 h->io_pending = CS_WANT_READ;
776 else if (yaz_errno() == 0)
786 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
788 /* move surplus buffer (or everything if we didn't get a BER rec.) */
789 if (hasread > berlen)
791 tomove = req = hasread - berlen;
792 rest = tomove % CS_TCPIP_BUFCHUNK;
794 req += CS_TCPIP_BUFCHUNK - rest;
797 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
799 } else if (sp->altsize < req)
800 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
802 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
803 (unsigned) sp->altbuf));
804 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
806 if (berlen < CS_TCPIP_BUFCHUNK - 1)
807 *(*buf + berlen) = '\0';
808 return berlen ? berlen : 1;
812 #if HAVE_OPENSSL_SSL_H
814 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
815 * 0=connection closed.
817 int ssl_get(COMSTACK h, char **buf, int *bufsize)
819 tcpip_state *sp = (tcpip_state *)h->cprivate;
821 int tmpi, berlen, rest, req, tomove;
822 int hasread = 0, res;
824 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
825 if (sp->altlen) /* switch buffers */
827 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
828 (unsigned) sp->altbuf));
832 *bufsize = sp->altsize;
833 hasread = sp->altlen;
839 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
843 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
846 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
847 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
849 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
850 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
853 int ssl_err = SSL_get_error(sp->ssl, res);
854 if (ssl_err == SSL_ERROR_WANT_READ)
856 h->io_pending = CS_WANT_READ;
859 if (ssl_err == SSL_ERROR_WANT_WRITE)
861 h->io_pending = CS_WANT_WRITE;
866 h->cerrno = CSERRORSSL;
871 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
873 /* move surplus buffer (or everything if we didn't get a BER rec.) */
874 if (hasread > berlen)
876 tomove = req = hasread - berlen;
877 rest = tomove % CS_TCPIP_BUFCHUNK;
879 req += CS_TCPIP_BUFCHUNK - rest;
882 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
884 } else if (sp->altsize < req)
885 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
887 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
888 (unsigned) sp->altbuf));
889 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
891 if (berlen < CS_TCPIP_BUFCHUNK - 1)
892 *(*buf + berlen) = '\0';
893 return berlen ? berlen : 1;
899 * In nonblocking mode, you must call again with same buffer while
902 int tcpip_put(COMSTACK h, char *buf, int size)
905 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
907 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
910 if (state->towrite < 0)
912 state->towrite = size;
915 else if (state->towrite != size)
917 h->cerrno = CSWRONGBUF;
920 while (state->towrite > state->written)
923 send(h->iofile, buf + state->written, size -
934 WSAGetLastError() == WSAEWOULDBLOCK
936 yaz_errno() == EWOULDBLOCK
938 #if EAGAIN != EWOULDBLOCK
939 || yaz_errno() == EAGAIN
943 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
945 || yaz_errno() == EINPROGRESS
949 TRC(fprintf(stderr, " Flow control stop\n"));
950 h->io_pending = CS_WANT_WRITE;
956 state->written += res;
957 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
958 res, state->written, size));
960 state->towrite = state->written = -1;
961 TRC(fprintf(stderr, " Ok\n"));
966 #if HAVE_OPENSSL_SSL_H
969 * In nonblocking mode, you must call again with same buffer while
972 int ssl_put(COMSTACK h, char *buf, int size)
975 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
977 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
980 if (state->towrite < 0)
982 state->towrite = size;
985 else if (state->towrite != size)
987 h->cerrno = CSWRONGBUF;
990 while (state->towrite > state->written)
992 res = SSL_write (state->ssl, buf + state->written,
993 size - state->written);
996 int ssl_err = SSL_get_error(state->ssl, res);
997 if (ssl_err == SSL_ERROR_WANT_READ)
999 h->io_pending = CS_WANT_READ;
1002 if (ssl_err == SSL_ERROR_WANT_WRITE)
1004 h->io_pending = CS_WANT_WRITE;
1007 h->cerrno = CSERRORSSL;
1010 state->written += res;
1011 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1012 res, state->written, size));
1014 state->towrite = state->written = -1;
1015 TRC(fprintf(stderr, " Ok\n"));
1020 int tcpip_close(COMSTACK h)
1022 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1024 TRC(fprintf(stderr, "tcpip_close\n"));
1025 if (h->iofile != -1)
1027 #if HAVE_OPENSSL_SSL_H
1030 SSL_shutdown (sp->ssl);
1034 closesocket(h->iofile);
1041 #if HAVE_OPENSSL_SSL_H
1044 TRC (fprintf(stderr, "SSL_free\n"));
1049 SSL_CTX_free (sp->ctx_alloc);
1056 char *tcpip_addrstr(COMSTACK h)
1058 struct sockaddr_in addr;
1059 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1060 char *r = 0, *buf = sp->buf;
1062 struct hostent *host;
1065 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1067 h->cerrno = CSYSERR;
1070 if (!(h->blocking&2)) {
1071 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1073 r = (char*) host->h_name;
1076 r = inet_ntoa(addr.sin_addr);
1077 if (h->protocol == PROTO_HTTP)
1078 sprintf(buf, "http:%s", r);
1080 sprintf(buf, "tcp:%s", r);
1081 #if HAVE_OPENSSL_SSL_H
1084 if (h->protocol == PROTO_HTTP)
1085 sprintf(buf, "https:%s", r);
1087 sprintf(buf, "ssl:%s", r);
1093 int static tcpip_set_blocking(COMSTACK p, int blocking)
1097 if (p->blocking == blocking)
1101 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1104 flag = fcntl(p->iofile, F_GETFL, 0);
1106 flag = flag & ~O_NONBLOCK;
1108 flag = flag | O_NONBLOCK;
1109 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1112 p->blocking = blocking;
1116 #if HAVE_OPENSSL_SSL_H
1117 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1119 struct tcpip_state *sp;
1120 if (!cs || cs->type != ssl_type)
1122 sp = (struct tcpip_state *) cs->cprivate;
1125 sp->ctx = (SSL_CTX *) ctx;
1129 void *cs_get_ssl(COMSTACK cs)
1131 struct tcpip_state *sp;
1132 if (!cs || cs->type != ssl_type)
1134 sp = (struct tcpip_state *) cs->cprivate;
1138 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1140 struct tcpip_state *sp;
1141 if (!cs || cs->type != ssl_type)
1143 sp = (struct tcpip_state *) cs->cprivate;
1144 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1145 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1149 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1151 SSL *ssl = (SSL *) cs_get_ssl(cs);
1154 X509 *server_cert = SSL_get_peer_certificate (ssl);
1157 BIO *bio = BIO_new(BIO_s_mem());
1159 /* get PEM buffer in memory */
1160 PEM_write_bio_X509(bio, server_cert);
1161 *len = BIO_get_mem_data(bio, &pem_buf);
1162 *buf = (char *) xmalloc(*len);
1163 memcpy(*buf, pem_buf, *len);
1171 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1176 void *cs_get_ssl(COMSTACK cs)
1181 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1186 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)