2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.10 2004-09-21 14:59:01 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 */
87 static int tcpip_init (void)
89 static int initialized = 0;
95 requested = MAKEWORD(1, 1);
96 if (WSAStartup(requested, &wd))
103 static int tcpip_init (void)
110 * This function is always called through the cs_create() macro.
111 * s >= 0: socket has already been established for us.
113 COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
119 unsigned long tru = 1;
126 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
132 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
134 if (!(sp = (struct tcpip_state *)(p->cprivate =
135 xmalloc(sizeof(tcpip_state)))))
138 if (!((p->blocking = blocking)&1))
141 if (ioctlsocket(s, FIONBIO, &tru) < 0)
144 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
147 signal (SIGPIPE, SIG_IGN);
154 p->type = tcpip_type;
155 p->protocol = (enum oid_proto) protocol;
157 p->f_connect = tcpip_connect;
158 p->f_rcvconnect = tcpip_rcvconnect;
159 p->f_get = tcpip_get;
160 p->f_put = tcpip_put;
161 p->f_close = tcpip_close;
162 p->f_more = tcpip_more;
163 p->f_bind = tcpip_bind;
164 p->f_listen = tcpip_listen;
165 p->f_accept = tcpip_accept;
166 p->f_addrstr = tcpip_addrstr;
167 p->f_straddr = tcpip_straddr;
168 p->f_set_blocking = tcpip_set_blocking;
170 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
175 #if HAVE_OPENSSL_SSL_H
176 sp->ctx = sp->ctx_alloc = 0;
178 strcpy(sp->cert_fname, "yaz.pem");
182 sp->altsize = sp->altlen = 0;
183 sp->towrite = sp->written = -1;
184 if (protocol == PROTO_WAIS)
185 sp->complete = completeWAIS;
187 sp->complete = cs_complete_auto;
189 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
190 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
195 #if HAVE_OPENSSL_SSL_H
197 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
202 p = tcpip_type (s, blocking, protocol, 0);
208 sp = (tcpip_state *) p->cprivate;
210 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
212 /* note: we don't handle already opened socket in SSL mode - yet */
217 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
222 short int port = default_port;
227 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
228 add->sin_family = AF_INET;
229 strncpy(buf, str, 511);
231 if ((p = strchr(buf, '/')))
233 if ((p = strchr(buf, ':')))
238 add->sin_port = htons(port);
239 if (!strcmp("@", buf))
240 add->sin_addr.s_addr = INADDR_ANY;
241 else if ((hp = gethostbyname(buf)))
242 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
243 sizeof(struct in_addr));
244 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
245 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
251 void *tcpip_straddr(COMSTACK h, const char *str)
253 tcpip_state *sp = (tcpip_state *)h->cprivate;
256 if (h->protocol == PROTO_HTTP)
259 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
264 struct sockaddr_in *tcpip_strtoaddr(const char *str)
266 static struct sockaddr_in add;
268 if (!tcpip_strtoaddr_ex (str, &add, 210))
273 int tcpip_more(COMSTACK h)
275 tcpip_state *sp = (tcpip_state *)h->cprivate;
277 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
282 * connect(2) will block (sometimes) - nothing we can do short of doing
283 * weird things like spawning subprocesses or threading or some weird junk
286 int tcpip_connect(COMSTACK h, void *address)
288 struct sockaddr_in *add = (struct sockaddr_in *)address;
292 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
294 TRC(fprintf(stderr, "tcpip_connect\n"));
296 if (h->state != CS_ST_UNBND)
298 h->cerrno = CSOUTSTATE;
302 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
303 * This gives the connect a chance to negotiate with the other side
306 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
311 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
313 recbuflen *= 10; /* lets be optimistic */
314 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
319 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
324 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
327 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
331 if (WSAGetLastError() == WSAEWOULDBLOCK)
333 h->event = CS_CONNECT;
334 h->state = CS_ST_CONNECTING;
335 h->io_pending = CS_WANT_WRITE;
339 if (yaz_errno() == EINPROGRESS)
341 h->event = CS_CONNECT;
342 h->state = CS_ST_CONNECTING;
343 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
350 h->event = CS_CONNECT;
351 h->state = CS_ST_CONNECTING;
353 return tcpip_rcvconnect (h);
359 int tcpip_rcvconnect(COMSTACK h)
361 #if HAVE_OPENSSL_SSL_H
362 tcpip_state *sp = (tcpip_state *)h->cprivate;
364 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
366 if (h->state == CS_ST_DATAXFER)
368 if (h->state != CS_ST_CONNECTING)
370 h->cerrno = CSOUTSTATE;
373 #if HAVE_OPENSSL_SSL_H
374 if (h->type == ssl_type && !sp->ctx)
376 SSL_load_error_strings();
377 SSLeay_add_all_algorithms();
379 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
382 h->cerrno = CSERRORSSL;
392 sp->ssl = SSL_new (sp->ctx);
393 SSL_set_fd (sp->ssl, h->iofile);
395 res = SSL_connect (sp->ssl);
398 int err = SSL_get_error(sp->ssl, res);
399 if (err == SSL_ERROR_WANT_READ)
401 h->io_pending = CS_WANT_READ;
404 if (err == SSL_ERROR_WANT_WRITE)
406 h->io_pending = CS_WANT_WRITE;
409 h->cerrno = CSERRORSSL;
415 h->state = CS_ST_DATAXFER;
419 #define CERTF "ztest.pem"
420 #define KEYF "ztest.pem"
422 static void tcpip_setsockopt (int fd)
428 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
430 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
432 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
434 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
436 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
438 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
443 static int tcpip_bind(COMSTACK h, void *address, int mode)
445 struct sockaddr *addr = (struct sockaddr *)address;
449 unsigned long one = 1;
452 #if HAVE_OPENSSL_SSL_H
453 tcpip_state *sp = (tcpip_state *)h->cprivate;
454 if (h->type == ssl_type && !sp->ctx)
456 SSL_load_error_strings();
457 SSLeay_add_all_algorithms();
459 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
462 h->cerrno = CSERRORSSL;
471 res = SSL_CTX_use_certificate_file (sp->ctx, sp->cert_fname,
475 ERR_print_errors_fp(stderr);
478 res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
482 ERR_print_errors_fp(stderr);
485 res = SSL_CTX_check_private_key (sp->ctx);
488 ERR_print_errors_fp(stderr);
492 TRC (fprintf (stderr, "ssl_bind\n"));
496 TRC (fprintf (stderr, "tcpip_bind\n"));
499 TRC (fprintf (stderr, "tcpip_bind\n"));
502 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
503 &one, sizeof(one)) < 0)
509 tcpip_setsockopt(h->iofile);
510 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)))
515 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
520 h->state = CS_ST_IDLE;
521 h->event = CS_LISTEN;
525 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
526 int (*check_ip)(void *cd, const char *a, int len, int t),
529 struct sockaddr_in addr;
530 YAZ_SOCKLEN_T len = sizeof(addr);
532 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
533 if (h->state != CS_ST_IDLE)
535 h->cerrno = CSOUTSTATE;
538 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
543 WSAGetLastError() == WSAEWOULDBLOCK
545 yaz_errno() == EWOULDBLOCK
547 #if EAGAIN != EWOULDBLOCK
548 || yaz_errno() == EAGAIN
553 h->cerrno = CSNODATA;
558 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
559 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
562 if (check_ip && (*check_ip)(cd, (const char *) &addr,
563 sizeof(addr), AF_INET))
567 closesocket(h->newfd);
574 h->state = CS_ST_INCON;
575 tcpip_setsockopt (h->newfd);
579 COMSTACK tcpip_accept(COMSTACK h)
582 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
584 unsigned long tru = 1;
587 TRC(fprintf(stderr, "tcpip_accept\n"));
588 if (h->state == CS_ST_INCON)
590 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
594 closesocket(h->newfd);
601 memcpy(cnew, h, sizeof(*h));
602 cnew->iofile = h->newfd;
603 cnew->io_pending = 0;
604 if (!(state = (tcpip_state *)
605 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
611 closesocket(h->newfd);
619 if (!(cnew->blocking&1) &&
621 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
623 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
631 closesocket(h->newfd);
643 state->altsize = state->altlen = 0;
644 state->towrite = state->written = -1;
645 state->complete = st->complete;
646 cnew->state = CS_ST_ACCEPT;
647 h->state = CS_ST_IDLE;
649 #if HAVE_OPENSSL_SSL_H
650 state->ctx = st->ctx;
651 state->ctx_alloc = 0;
652 state->ssl = st->ssl;
655 state->ssl = SSL_new (state->ctx);
656 SSL_set_fd (state->ssl, cnew->iofile);
661 if (h->state == CS_ST_ACCEPT)
663 #if HAVE_OPENSSL_SSL_H
664 tcpip_state *state = (tcpip_state *)h->cprivate;
667 int res = SSL_accept (state->ssl);
668 TRC(fprintf(stderr, "SSL_accept\n"));
671 int err = SSL_get_error(state->ssl, res);
672 if (err == SSL_ERROR_WANT_READ)
674 h->io_pending = CS_WANT_READ;
677 if (err == SSL_ERROR_WANT_WRITE)
679 h->io_pending = CS_WANT_WRITE;
690 h->cerrno = CSOUTSTATE;
694 h->state = CS_ST_DATAXFER;
699 #define CS_TCPIP_BUFCHUNK 4096
702 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
703 * 0=connection closed.
705 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
707 tcpip_state *sp = (tcpip_state *)h->cprivate;
709 int tmpi, berlen, rest, req, tomove;
710 int hasread = 0, res;
712 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
713 if (sp->altlen) /* switch buffers */
715 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
716 (unsigned) sp->altbuf));
720 *bufsize = sp->altsize;
721 hasread = sp->altlen;
727 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
731 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
734 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
735 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
739 /* unfortunatly, sun sometimes forgets to set errno in recv
740 when EWOULDBLOCK etc. would be required (res = -1) */
742 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
743 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
746 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
747 strerror(yaz_errno())));
749 if (WSAGetLastError() == WSAEWOULDBLOCK)
751 h->io_pending = CS_WANT_READ;
757 if (yaz_errno() == EWOULDBLOCK
759 #if EAGAIN != EWOULDBLOCK
760 || yaz_errno() == EAGAIN
763 || yaz_errno() == EINPROGRESS
765 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
769 h->io_pending = CS_WANT_READ;
772 else if (yaz_errno() == 0)
782 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
784 /* move surplus buffer (or everything if we didn't get a BER rec.) */
785 if (hasread > berlen)
787 tomove = req = hasread - berlen;
788 rest = tomove % CS_TCPIP_BUFCHUNK;
790 req += CS_TCPIP_BUFCHUNK - rest;
793 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
795 } else if (sp->altsize < req)
796 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
798 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
799 (unsigned) sp->altbuf));
800 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
802 if (berlen < CS_TCPIP_BUFCHUNK - 1)
803 *(*buf + berlen) = '\0';
804 return berlen ? berlen : 1;
808 #if HAVE_OPENSSL_SSL_H
810 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
811 * 0=connection closed.
813 int ssl_get(COMSTACK h, char **buf, int *bufsize)
815 tcpip_state *sp = (tcpip_state *)h->cprivate;
817 int tmpi, berlen, rest, req, tomove;
818 int hasread = 0, res;
820 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
821 if (sp->altlen) /* switch buffers */
823 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
824 (unsigned) sp->altbuf));
828 *bufsize = sp->altsize;
829 hasread = sp->altlen;
835 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
839 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
842 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
843 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
845 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
846 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
849 int ssl_err = SSL_get_error(sp->ssl, res);
850 if (ssl_err == SSL_ERROR_WANT_READ)
852 h->io_pending = CS_WANT_READ;
855 if (ssl_err == SSL_ERROR_WANT_WRITE)
857 h->io_pending = CS_WANT_WRITE;
862 h->cerrno = CSERRORSSL;
867 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
869 /* move surplus buffer (or everything if we didn't get a BER rec.) */
870 if (hasread > berlen)
872 tomove = req = hasread - berlen;
873 rest = tomove % CS_TCPIP_BUFCHUNK;
875 req += CS_TCPIP_BUFCHUNK - rest;
878 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
880 } else if (sp->altsize < req)
881 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
883 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
884 (unsigned) sp->altbuf));
885 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
887 if (berlen < CS_TCPIP_BUFCHUNK - 1)
888 *(*buf + berlen) = '\0';
889 return berlen ? berlen : 1;
895 * In nonblocking mode, you must call again with same buffer while
898 int tcpip_put(COMSTACK h, char *buf, int size)
901 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
903 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
906 if (state->towrite < 0)
908 state->towrite = size;
911 else if (state->towrite != size)
913 h->cerrno = CSWRONGBUF;
916 while (state->towrite > state->written)
919 send(h->iofile, buf + state->written, size -
930 WSAGetLastError() == WSAEWOULDBLOCK
932 yaz_errno() == EWOULDBLOCK
934 #if EAGAIN != EWOULDBLOCK
935 || yaz_errno() == EAGAIN
939 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
941 || yaz_errno() == EINPROGRESS
945 TRC(fprintf(stderr, " Flow control stop\n"));
946 h->io_pending = CS_WANT_WRITE;
952 state->written += res;
953 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
954 res, state->written, size));
956 state->towrite = state->written = -1;
957 TRC(fprintf(stderr, " Ok\n"));
962 #if HAVE_OPENSSL_SSL_H
965 * In nonblocking mode, you must call again with same buffer while
968 int ssl_put(COMSTACK h, char *buf, int size)
971 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
973 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
976 if (state->towrite < 0)
978 state->towrite = size;
981 else if (state->towrite != size)
983 h->cerrno = CSWRONGBUF;
986 while (state->towrite > state->written)
988 res = SSL_write (state->ssl, buf + state->written,
989 size - state->written);
992 int ssl_err = SSL_get_error(state->ssl, res);
993 if (ssl_err == SSL_ERROR_WANT_READ)
995 h->io_pending = CS_WANT_READ;
998 if (ssl_err == SSL_ERROR_WANT_WRITE)
1000 h->io_pending = CS_WANT_WRITE;
1003 h->cerrno = CSERRORSSL;
1006 state->written += res;
1007 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1008 res, state->written, size));
1010 state->towrite = state->written = -1;
1011 TRC(fprintf(stderr, " Ok\n"));
1016 int tcpip_close(COMSTACK h)
1018 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1020 TRC(fprintf(stderr, "tcpip_close\n"));
1021 if (h->iofile != -1)
1023 #if HAVE_OPENSSL_SSL_H
1026 SSL_shutdown (sp->ssl);
1030 closesocket(h->iofile);
1037 #if HAVE_OPENSSL_SSL_H
1040 TRC (fprintf(stderr, "SSL_free\n"));
1045 SSL_CTX_free (sp->ctx_alloc);
1052 char *tcpip_addrstr(COMSTACK h)
1054 struct sockaddr_in addr;
1055 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1056 char *r = 0, *buf = sp->buf;
1058 struct hostent *host;
1061 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1063 h->cerrno = CSYSERR;
1066 if (!(h->blocking&2)) {
1067 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1069 r = (char*) host->h_name;
1072 r = inet_ntoa(addr.sin_addr);
1073 if (h->protocol == PROTO_HTTP)
1074 sprintf(buf, "http:%s", r);
1076 sprintf(buf, "tcp:%s", r);
1077 #if HAVE_OPENSSL_SSL_H
1080 if (h->protocol == PROTO_HTTP)
1081 sprintf(buf, "https:%s", r);
1083 sprintf(buf, "ssl:%s", r);
1089 int static tcpip_set_blocking(COMSTACK p, int blocking)
1093 if (p->blocking == blocking)
1097 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1100 flag = fcntl(p->iofile, F_GETFL, 0);
1102 flag = flag & ~O_NONBLOCK;
1104 flag = flag | O_NONBLOCK;
1105 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1108 p->blocking = blocking;
1112 #if HAVE_OPENSSL_SSL_H
1113 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1115 struct tcpip_state *sp;
1116 if (!cs || cs->type != ssl_type)
1118 sp = (struct tcpip_state *) cs->cprivate;
1121 sp->ctx = (SSL_CTX *) ctx;
1125 void *cs_get_ssl(COMSTACK cs)
1127 struct tcpip_state *sp;
1128 if (!cs || cs->type != ssl_type)
1130 sp = (struct tcpip_state *) cs->cprivate;
1134 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1136 struct tcpip_state *sp;
1137 if (!cs || cs->type != ssl_type)
1139 sp = (struct tcpip_state *) cs->cprivate;
1140 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1141 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1145 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1147 SSL *ssl = (SSL *) cs_get_ssl(cs);
1150 X509 *server_cert = SSL_get_peer_certificate (ssl);
1153 BIO *bio = BIO_new(BIO_s_mem());
1155 /* get PEM buffer in memory */
1156 PEM_write_bio_X509(bio, server_cert);
1157 *len = BIO_get_mem_data(bio, &pem_buf);
1158 *buf = (char *) xmalloc(*len);
1159 memcpy(*buf, pem_buf, *len);
1167 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1172 void *cs_get_ssl(COMSTACK cs)
1177 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1182 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)