2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.5 2004-04-29 21:19:23 adam Exp $
20 #if HAVE_OPENSSL_SSL_H
21 #include <openssl/ssl.h>
22 #include <openssl/err.h>
25 #include <yaz/comstack.h>
26 #include <yaz/tcpip.h>
32 #include <netinet/tcp.h>
35 static int tcpip_close(COMSTACK h);
36 static int tcpip_put(COMSTACK h, char *buf, int size);
37 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
38 static int tcpip_connect(COMSTACK h, void *address);
39 static int tcpip_more(COMSTACK h);
40 static int tcpip_rcvconnect(COMSTACK h);
41 static int tcpip_bind(COMSTACK h, void *address, int mode);
42 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
43 int (*check_ip)(void *cd, const char *a, int len, int type),
45 static int tcpip_set_blocking(COMSTACK p, int blocking);
47 #if HAVE_OPENSSL_SSL_H
48 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
49 static int ssl_put(COMSTACK h, char *buf, int size);
52 static COMSTACK tcpip_accept(COMSTACK h);
53 static char *tcpip_addrstr(COMSTACK h);
54 static void *tcpip_straddr(COMSTACK h, const char *str);
63 #define YAZ_SOCKLEN_T int
66 /* this state is used for both SSL and straight TCP/IP */
67 typedef struct tcpip_state
69 char *altbuf; /* alternate buffer for surplus data */
70 int altsize; /* size as xmalloced */
71 int altlen; /* length of data or 0 if none */
73 int written; /* -1 if we aren't writing */
74 int towrite; /* to verify against user input */
75 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
76 struct sockaddr_in addr; /* returned by cs_straddr */
77 char buf[128]; /* returned by cs_addrstr */
78 #if HAVE_OPENSSL_SSL_H
79 SSL_CTX *ctx; /* current CTX. */
80 SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
86 static int tcpip_init (void)
88 static int initialized = 0;
94 requested = MAKEWORD(1, 1);
95 if (WSAStartup(requested, &wd))
102 static int tcpip_init (void)
109 * This function is always called through the cs_create() macro.
110 * s >= 0: socket has already been established for us.
112 COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
118 unsigned long tru = 1;
125 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
131 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
133 if (!(state = (struct tcpip_state *)(p->cprivate =
134 xmalloc(sizeof(tcpip_state)))))
137 if (!((p->blocking = blocking)&1))
140 if (ioctlsocket(s, FIONBIO, &tru) < 0)
143 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
146 signal (SIGPIPE, SIG_IGN);
153 p->type = tcpip_type;
154 p->protocol = (enum oid_proto) protocol;
156 p->f_connect = tcpip_connect;
157 p->f_rcvconnect = tcpip_rcvconnect;
158 p->f_get = tcpip_get;
159 p->f_put = tcpip_put;
160 p->f_close = tcpip_close;
161 p->f_more = tcpip_more;
162 p->f_bind = tcpip_bind;
163 p->f_listen = tcpip_listen;
164 p->f_accept = tcpip_accept;
165 p->f_addrstr = tcpip_addrstr;
166 p->f_straddr = tcpip_straddr;
167 p->f_set_blocking = tcpip_set_blocking;
169 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
174 #if HAVE_OPENSSL_SSL_H
175 state->ctx = state->ctx_alloc = 0;
180 state->altsize = state->altlen = 0;
181 state->towrite = state->written = -1;
182 if (protocol == PROTO_WAIS)
183 state->complete = completeWAIS;
185 state->complete = cs_complete_auto;
187 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
188 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
193 #if HAVE_OPENSSL_SSL_H
195 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
200 p = tcpip_type (s, blocking, protocol, 0);
206 state = (tcpip_state *) p->cprivate;
208 state->ctx = vp; /* may be NULL */
210 /* note: we don't handle already opened socket in SSL mode - yet */
215 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
220 short int port = default_port;
225 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
226 add->sin_family = AF_INET;
227 strncpy(buf, str, 511);
229 if ((p = strchr(buf, '/')))
231 if ((p = strchr(buf, ':')))
236 add->sin_port = htons(port);
237 if (!strcmp("@", buf))
238 add->sin_addr.s_addr = INADDR_ANY;
239 else if ((hp = gethostbyname(buf)))
240 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
241 sizeof(struct in_addr));
242 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
243 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
249 void *tcpip_straddr(COMSTACK h, const char *str)
251 tcpip_state *sp = (tcpip_state *)h->cprivate;
254 if (h->protocol == PROTO_HTTP)
257 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
262 struct sockaddr_in *tcpip_strtoaddr(const char *str)
264 static struct sockaddr_in add;
266 if (!tcpip_strtoaddr_ex (str, &add, 210))
271 int tcpip_more(COMSTACK h)
273 tcpip_state *sp = (tcpip_state *)h->cprivate;
275 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
280 * connect(2) will block (sometimes) - nothing we can do short of doing
281 * weird things like spawning subprocesses or threading or some weird junk
284 int tcpip_connect(COMSTACK h, void *address)
286 struct sockaddr_in *add = (struct sockaddr_in *)address;
290 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
292 TRC(fprintf(stderr, "tcpip_connect\n"));
294 if (h->state != CS_ST_UNBND)
296 h->cerrno = CSOUTSTATE;
300 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
301 * This gives the connect a chance to negotiate with the other side
304 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
309 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
311 recbuflen *= 10; /* lets be optimistic */
312 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
317 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
322 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
325 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
329 if (WSAGetLastError() == WSAEWOULDBLOCK)
331 h->event = CS_CONNECT;
332 h->state = CS_ST_CONNECTING;
333 h->io_pending = CS_WANT_WRITE;
337 if (yaz_errno() == EINPROGRESS)
339 h->event = CS_CONNECT;
340 h->state = CS_ST_CONNECTING;
341 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
348 h->event = CS_CONNECT;
349 h->state = CS_ST_CONNECTING;
351 return tcpip_rcvconnect (h);
357 int tcpip_rcvconnect(COMSTACK h)
359 #if HAVE_OPENSSL_SSL_H
360 tcpip_state *sp = (tcpip_state *)h->cprivate;
362 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
364 if (h->state == CS_ST_DATAXFER)
366 if (h->state != CS_ST_CONNECTING)
368 h->cerrno = CSOUTSTATE;
371 #if HAVE_OPENSSL_SSL_H
372 if (h->type == ssl_type && !sp->ctx)
374 SSL_load_error_strings();
375 SSLeay_add_all_algorithms();
377 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
380 h->cerrno = CSERRORSSL;
390 sp->ssl = SSL_new (sp->ctx);
391 SSL_set_fd (sp->ssl, h->iofile);
393 res = SSL_connect (sp->ssl);
396 int err = SSL_get_error(sp->ssl, res);
397 if (err == SSL_ERROR_WANT_READ)
399 h->io_pending = CS_WANT_READ;
402 if (err == SSL_ERROR_WANT_WRITE)
404 h->io_pending = CS_WANT_WRITE;
407 h->cerrno = CSERRORSSL;
413 h->state = CS_ST_DATAXFER;
417 #define CERTF "ztest.pem"
418 #define KEYF "ztest.pem"
420 static void tcpip_setsockopt (int fd)
426 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
428 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
430 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
432 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
434 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
436 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
441 static int tcpip_bind(COMSTACK h, void *address, int mode)
443 struct sockaddr *addr = (struct sockaddr *)address;
447 unsigned long one = 1;
450 #if HAVE_OPENSSL_SSL_H
451 tcpip_state *sp = (tcpip_state *)h->cprivate;
452 if (h->type == ssl_type && !sp->ctx)
454 SSL_load_error_strings();
455 SSLeay_add_all_algorithms();
457 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
460 h->cerrno = CSERRORSSL;
469 res = SSL_CTX_use_certificate_file (sp->ctx, CERTF,
473 ERR_print_errors_fp(stderr);
476 res = SSL_CTX_use_PrivateKey_file (sp->ctx, KEYF,
480 ERR_print_errors_fp(stderr);
483 res = SSL_CTX_check_private_key (sp->ctx);
486 ERR_print_errors_fp(stderr);
490 TRC (fprintf (stderr, "ssl_bind\n"));
494 TRC (fprintf (stderr, "tcpip_bind\n"));
497 TRC (fprintf (stderr, "tcpip_bind\n"));
500 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
501 &one, sizeof(one)) < 0)
507 tcpip_setsockopt(h->iofile);
508 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)))
513 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
518 h->state = CS_ST_IDLE;
519 h->event = CS_LISTEN;
523 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
524 int (*check_ip)(void *cd, const char *a, int len, int t),
527 struct sockaddr_in addr;
528 YAZ_SOCKLEN_T len = sizeof(addr);
530 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
531 if (h->state != CS_ST_IDLE)
533 h->cerrno = CSOUTSTATE;
536 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
541 WSAGetLastError() == WSAEWOULDBLOCK
543 yaz_errno() == EWOULDBLOCK
545 #if EAGAIN != EWOULDBLOCK
546 || yaz_errno() == EAGAIN
551 h->cerrno = CSNODATA;
556 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
557 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
560 if (check_ip && (*check_ip)(cd, (const char *) &addr,
561 sizeof(addr), AF_INET))
565 closesocket(h->newfd);
572 h->state = CS_ST_INCON;
573 tcpip_setsockopt (h->newfd);
577 COMSTACK tcpip_accept(COMSTACK h)
580 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
582 unsigned long tru = 1;
585 TRC(fprintf(stderr, "tcpip_accept\n"));
586 if (h->state == CS_ST_INCON)
588 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
592 closesocket(h->newfd);
599 memcpy(cnew, h, sizeof(*h));
600 cnew->iofile = h->newfd;
601 cnew->io_pending = 0;
602 if (!(state = (tcpip_state *)
603 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
609 closesocket(h->newfd);
617 if (!(cnew->blocking&1) &&
619 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
621 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
629 closesocket(h->newfd);
641 state->altsize = state->altlen = 0;
642 state->towrite = state->written = -1;
643 state->complete = st->complete;
644 cnew->state = CS_ST_ACCEPT;
645 h->state = CS_ST_IDLE;
647 #if HAVE_OPENSSL_SSL_H
648 state->ctx = st->ctx;
649 state->ctx_alloc = 0;
650 state->ssl = st->ssl;
653 state->ssl = SSL_new (state->ctx);
654 SSL_set_fd (state->ssl, cnew->iofile);
659 if (h->state == CS_ST_ACCEPT)
661 #if HAVE_OPENSSL_SSL_H
662 tcpip_state *state = (tcpip_state *)h->cprivate;
665 int res = SSL_accept (state->ssl);
666 TRC(fprintf(stderr, "SSL_accept\n"));
669 int err = SSL_get_error(state->ssl, res);
670 if (err == SSL_ERROR_WANT_READ)
672 h->io_pending = CS_WANT_READ;
675 if (err == SSL_ERROR_WANT_WRITE)
677 h->io_pending = CS_WANT_WRITE;
688 h->cerrno = CSOUTSTATE;
692 h->state = CS_ST_DATAXFER;
697 #define CS_TCPIP_BUFCHUNK 4096
700 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
701 * 0=connection closed.
703 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
705 tcpip_state *sp = (tcpip_state *)h->cprivate;
707 int tmpi, berlen, rest, req, tomove;
708 int hasread = 0, res;
710 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
711 if (sp->altlen) /* switch buffers */
713 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
714 (unsigned) sp->altbuf));
718 *bufsize = sp->altsize;
719 hasread = sp->altlen;
725 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
729 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
732 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
733 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
737 /* unfortunatly, sun sometimes forgets to set errno in recv
738 when EWOULDBLOCK etc. would be required (res = -1) */
740 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
741 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
744 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
745 strerror(yaz_errno())));
747 if (WSAGetLastError() == WSAEWOULDBLOCK)
749 h->io_pending = CS_WANT_READ;
755 if (yaz_errno() == EWOULDBLOCK
757 #if EAGAIN != EWOULDBLOCK
758 || yaz_errno() == EAGAIN
761 || yaz_errno() == EINPROGRESS
763 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
767 h->io_pending = CS_WANT_READ;
770 else if (yaz_errno() == 0)
780 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
782 /* move surplus buffer (or everything if we didn't get a BER rec.) */
783 if (hasread > berlen)
785 tomove = req = hasread - berlen;
786 rest = tomove % CS_TCPIP_BUFCHUNK;
788 req += CS_TCPIP_BUFCHUNK - rest;
791 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
793 } else if (sp->altsize < req)
794 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
796 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
797 (unsigned) sp->altbuf));
798 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
800 if (berlen < CS_TCPIP_BUFCHUNK - 1)
801 *(*buf + berlen) = '\0';
802 return berlen ? berlen : 1;
806 #if HAVE_OPENSSL_SSL_H
808 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
809 * 0=connection closed.
811 int ssl_get(COMSTACK h, char **buf, int *bufsize)
813 tcpip_state *sp = (tcpip_state *)h->cprivate;
815 int tmpi, berlen, rest, req, tomove;
816 int hasread = 0, res;
818 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
819 if (sp->altlen) /* switch buffers */
821 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
822 (unsigned) sp->altbuf));
826 *bufsize = sp->altsize;
827 hasread = sp->altlen;
833 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
837 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
840 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
841 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
843 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
844 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
847 int ssl_err = SSL_get_error(sp->ssl, res);
848 if (ssl_err == SSL_ERROR_WANT_READ)
850 h->io_pending = CS_WANT_READ;
853 if (ssl_err == SSL_ERROR_WANT_WRITE)
855 h->io_pending = CS_WANT_WRITE;
860 h->cerrno = CSERRORSSL;
865 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
867 /* move surplus buffer (or everything if we didn't get a BER rec.) */
868 if (hasread > berlen)
870 tomove = req = hasread - berlen;
871 rest = tomove % CS_TCPIP_BUFCHUNK;
873 req += CS_TCPIP_BUFCHUNK - rest;
876 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
878 } else if (sp->altsize < req)
879 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
881 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
882 (unsigned) sp->altbuf));
883 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
885 if (berlen < CS_TCPIP_BUFCHUNK - 1)
886 *(*buf + berlen) = '\0';
887 return berlen ? berlen : 1;
893 * In nonblocking mode, you must call again with same buffer while
896 int tcpip_put(COMSTACK h, char *buf, int size)
899 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
901 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
904 if (state->towrite < 0)
906 state->towrite = size;
909 else if (state->towrite != size)
911 h->cerrno = CSWRONGBUF;
914 while (state->towrite > state->written)
917 send(h->iofile, buf + state->written, size -
928 WSAGetLastError() == WSAEWOULDBLOCK
930 yaz_errno() == EWOULDBLOCK
932 #if EAGAIN != EWOULDBLOCK
933 || yaz_errno() == EAGAIN
937 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
939 || yaz_errno() == EINPROGRESS
943 TRC(fprintf(stderr, " Flow control stop\n"));
944 h->io_pending = CS_WANT_WRITE;
950 state->written += res;
951 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
952 res, state->written, size));
954 state->towrite = state->written = -1;
955 TRC(fprintf(stderr, " Ok\n"));
960 #if HAVE_OPENSSL_SSL_H
963 * In nonblocking mode, you must call again with same buffer while
966 int ssl_put(COMSTACK h, char *buf, int size)
969 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
971 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
974 if (state->towrite < 0)
976 state->towrite = size;
979 else if (state->towrite != size)
981 h->cerrno = CSWRONGBUF;
984 while (state->towrite > state->written)
986 res = SSL_write (state->ssl, buf + state->written,
987 size - state->written);
990 int ssl_err = SSL_get_error(state->ssl, res);
991 if (ssl_err == SSL_ERROR_WANT_READ)
993 h->io_pending = CS_WANT_READ;
996 if (ssl_err == SSL_ERROR_WANT_WRITE)
998 h->io_pending = CS_WANT_WRITE;
1001 h->cerrno = CSERRORSSL;
1004 state->written += res;
1005 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1006 res, state->written, size));
1008 state->towrite = state->written = -1;
1009 TRC(fprintf(stderr, " Ok\n"));
1014 int tcpip_close(COMSTACK h)
1016 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1018 TRC(fprintf(stderr, "tcpip_close\n"));
1019 if (h->iofile != -1)
1021 #if HAVE_OPENSSL_SSL_H
1024 SSL_shutdown (sp->ssl);
1028 closesocket(h->iofile);
1035 #if HAVE_OPENSSL_SSL_H
1038 TRC (fprintf(stderr, "SSL_free\n"));
1043 SSL_CTX_free (sp->ctx_alloc);
1050 char *tcpip_addrstr(COMSTACK h)
1052 struct sockaddr_in addr;
1053 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1054 char *r = 0, *buf = sp->buf;
1056 struct hostent *host;
1059 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1061 h->cerrno = CSYSERR;
1064 if (!(h->blocking&2)) {
1065 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1067 r = (char*) host->h_name;
1070 r = inet_ntoa(addr.sin_addr);
1071 if (h->protocol == PROTO_HTTP)
1072 sprintf(buf, "http:%s", r);
1074 sprintf(buf, "tcp:%s", r);
1075 #if HAVE_OPENSSL_SSL_H
1078 if (h->protocol == PROTO_HTTP)
1079 sprintf(buf, "https:%s", r);
1081 sprintf(buf, "ssl:%s", r);
1087 int static tcpip_set_blocking(COMSTACK p, int blocking)
1091 if (p->blocking == blocking)
1095 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1098 flag = fcntl(p->iofile, F_GETFL, 0);
1100 flag = flag & ~O_NONBLOCK;
1102 flag = flag | O_NONBLOCK;
1103 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1106 p->blocking = blocking;
1110 #if HAVE_OPENSSL_SSL_H
1111 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1113 struct tcpip_state *state;
1114 if (!cs || cs->type != ssl_type)
1116 state = (struct tcpip_state *) cs->cprivate;
1117 if (state->ctx_alloc)
1123 void *cs_get_ssl(COMSTACK cs)
1125 struct tcpip_state *state;
1126 if (!cs || cs->type != ssl_type)
1128 state = (struct tcpip_state *) cs->cprivate;
1132 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1134 SSL *ssl = cs_get_ssl(cs);
1137 X509 *server_cert = SSL_get_peer_certificate (ssl);
1140 BIO *bio = BIO_new(BIO_s_mem());
1142 /* get PEM buffer in memory */
1143 PEM_write_bio_X509(bio, server_cert);
1144 *len = BIO_get_mem_data(bio, &pem_buf);
1145 *buf = xmalloc(*len);
1146 memcpy(*buf, pem_buf, *len);
1154 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1159 void *cs_get_ssl(COMSTACK cs)
1164 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)