2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.13 2005-01-15 19:47:14 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>
35 #include <netinet/tcp.h>
38 static int tcpip_close(COMSTACK h);
39 static int tcpip_put(COMSTACK h, char *buf, int size);
40 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
41 static int tcpip_connect(COMSTACK h, void *address);
42 static int tcpip_more(COMSTACK h);
43 static int tcpip_rcvconnect(COMSTACK h);
44 static int tcpip_bind(COMSTACK h, void *address, int mode);
45 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
46 int (*check_ip)(void *cd, const char *a, int len, int type),
48 static int tcpip_set_blocking(COMSTACK p, int blocking);
50 #if HAVE_OPENSSL_SSL_H
51 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
52 static int ssl_put(COMSTACK h, char *buf, int size);
55 static COMSTACK tcpip_accept(COMSTACK h);
56 static char *tcpip_addrstr(COMSTACK h);
57 static void *tcpip_straddr(COMSTACK h, const char *str);
66 #define YAZ_SOCKLEN_T int
69 /* this state is used for both SSL and straight TCP/IP */
70 typedef struct tcpip_state
72 char *altbuf; /* alternate buffer for surplus data */
73 int altsize; /* size as xmalloced */
74 int altlen; /* length of data or 0 if none */
76 int written; /* -1 if we aren't writing */
77 int towrite; /* to verify against user input */
78 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
79 struct sockaddr_in addr; /* returned by cs_straddr */
80 char buf[128]; /* returned by cs_addrstr */
81 #if HAVE_OPENSSL_SSL_H
82 SSL_CTX *ctx; /* current CTX. */
83 SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
90 static int tcpip_init (void)
92 static int initialized = 0;
98 requested = MAKEWORD(1, 1);
99 if (WSAStartup(requested, &wd))
106 static int tcpip_init (void)
113 * This function is always called through the cs_create() macro.
114 * s >= 0: socket has already been established for us.
116 COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
122 unsigned long tru = 1;
129 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
135 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
137 if (!(sp = (struct tcpip_state *)(p->cprivate =
138 xmalloc(sizeof(tcpip_state)))))
141 if (!((p->blocking = blocking)&1))
144 if (ioctlsocket(s, FIONBIO, &tru) < 0)
147 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
150 signal (SIGPIPE, SIG_IGN);
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;
173 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
178 #if HAVE_OPENSSL_SSL_H
179 sp->ctx = sp->ctx_alloc = 0;
181 strcpy(sp->cert_fname, "yaz.pem");
185 sp->altsize = sp->altlen = 0;
186 sp->towrite = sp->written = -1;
187 if (protocol == PROTO_WAIS)
188 sp->complete = completeWAIS;
190 sp->complete = cs_complete_auto;
192 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
193 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
198 #if HAVE_OPENSSL_SSL_H
200 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
205 p = tcpip_type (s, blocking, protocol, 0);
211 sp = (tcpip_state *) p->cprivate;
213 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
215 /* note: we don't handle already opened socket in SSL mode - yet */
220 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
225 short int port = default_port;
230 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
231 add->sin_family = AF_INET;
232 strncpy(buf, str, 511);
234 if ((p = strchr(buf, '/')))
236 if ((p = strchr(buf, ':')))
241 add->sin_port = htons(port);
242 if (!strcmp("@", buf))
243 add->sin_addr.s_addr = INADDR_ANY;
244 else if ((hp = gethostbyname(buf)))
245 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
246 sizeof(struct in_addr));
247 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
248 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
254 void *tcpip_straddr(COMSTACK h, const char *str)
256 tcpip_state *sp = (tcpip_state *)h->cprivate;
259 if (h->protocol == PROTO_HTTP)
262 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
267 struct sockaddr_in *tcpip_strtoaddr(const char *str)
269 static struct sockaddr_in add;
271 if (!tcpip_strtoaddr_ex (str, &add, 210))
276 int tcpip_more(COMSTACK h)
278 tcpip_state *sp = (tcpip_state *)h->cprivate;
280 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
285 * connect(2) will block (sometimes) - nothing we can do short of doing
286 * weird things like spawning subprocesses or threading or some weird junk
289 int tcpip_connect(COMSTACK h, void *address)
291 struct sockaddr_in *add = (struct sockaddr_in *)address;
295 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
297 TRC(fprintf(stderr, "tcpip_connect\n"));
299 if (h->state != CS_ST_UNBND)
301 h->cerrno = CSOUTSTATE;
305 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
306 * This gives the connect a chance to negotiate with the other side
309 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
314 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
316 recbuflen *= 10; /* lets be optimistic */
317 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
322 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
327 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
330 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
334 if (WSAGetLastError() == WSAEWOULDBLOCK)
336 h->event = CS_CONNECT;
337 h->state = CS_ST_CONNECTING;
338 h->io_pending = CS_WANT_WRITE;
342 if (yaz_errno() == EINPROGRESS)
344 h->event = CS_CONNECT;
345 h->state = CS_ST_CONNECTING;
346 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
353 h->event = CS_CONNECT;
354 h->state = CS_ST_CONNECTING;
356 return tcpip_rcvconnect (h);
362 int tcpip_rcvconnect(COMSTACK h)
364 #if HAVE_OPENSSL_SSL_H
365 tcpip_state *sp = (tcpip_state *)h->cprivate;
367 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
369 if (h->state == CS_ST_DATAXFER)
371 if (h->state != CS_ST_CONNECTING)
373 h->cerrno = CSOUTSTATE;
376 #if HAVE_OPENSSL_SSL_H
377 if (h->type == ssl_type && !sp->ctx)
379 SSL_load_error_strings();
380 SSLeay_add_all_algorithms();
382 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
385 h->cerrno = CSERRORSSL;
395 sp->ssl = SSL_new (sp->ctx);
396 SSL_set_fd (sp->ssl, h->iofile);
398 res = SSL_connect (sp->ssl);
401 int err = SSL_get_error(sp->ssl, res);
402 if (err == SSL_ERROR_WANT_READ)
404 h->io_pending = CS_WANT_READ;
407 if (err == SSL_ERROR_WANT_WRITE)
409 h->io_pending = CS_WANT_WRITE;
412 h->cerrno = CSERRORSSL;
418 h->state = CS_ST_DATAXFER;
422 #define CERTF "ztest.pem"
423 #define KEYF "ztest.pem"
425 static void tcpip_setsockopt (int fd)
431 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
433 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
435 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
437 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
439 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
441 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
446 static int tcpip_bind(COMSTACK h, void *address, int mode)
448 struct sockaddr *addr = (struct sockaddr *)address;
452 unsigned long one = 1;
455 #if HAVE_OPENSSL_SSL_H
456 tcpip_state *sp = (tcpip_state *)h->cprivate;
457 if (h->type == ssl_type && !sp->ctx)
459 SSL_load_error_strings();
460 SSLeay_add_all_algorithms();
462 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
465 h->cerrno = CSERRORSSL;
474 res = SSL_CTX_use_certificate_file (sp->ctx, sp->cert_fname,
478 ERR_print_errors_fp(stderr);
481 res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
485 ERR_print_errors_fp(stderr);
488 res = SSL_CTX_check_private_key (sp->ctx);
491 ERR_print_errors_fp(stderr);
495 TRC (fprintf (stderr, "ssl_bind\n"));
499 TRC (fprintf (stderr, "tcpip_bind\n"));
502 TRC (fprintf (stderr, "tcpip_bind\n"));
505 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
506 &one, sizeof(one)) < 0)
512 tcpip_setsockopt(h->iofile);
513 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)))
518 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
523 h->state = CS_ST_IDLE;
524 h->event = CS_LISTEN;
528 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
529 int (*check_ip)(void *cd, const char *a, int len, int t),
532 struct sockaddr_in addr;
533 YAZ_SOCKLEN_T len = sizeof(addr);
535 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
536 if (h->state != CS_ST_IDLE)
538 h->cerrno = CSOUTSTATE;
541 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
546 WSAGetLastError() == WSAEWOULDBLOCK
548 yaz_errno() == EWOULDBLOCK
550 #if EAGAIN != EWOULDBLOCK
551 || yaz_errno() == EAGAIN
556 h->cerrno = CSNODATA;
561 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
562 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
565 if (check_ip && (*check_ip)(cd, (const char *) &addr,
566 sizeof(addr), AF_INET))
570 closesocket(h->newfd);
577 h->state = CS_ST_INCON;
578 tcpip_setsockopt (h->newfd);
582 COMSTACK tcpip_accept(COMSTACK h)
585 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
587 unsigned long tru = 1;
590 TRC(fprintf(stderr, "tcpip_accept\n"));
591 if (h->state == CS_ST_INCON)
593 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
597 closesocket(h->newfd);
604 memcpy(cnew, h, sizeof(*h));
605 cnew->iofile = h->newfd;
606 cnew->io_pending = 0;
607 if (!(state = (tcpip_state *)
608 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
614 closesocket(h->newfd);
622 if (!(cnew->blocking&1) &&
624 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
626 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
634 closesocket(h->newfd);
646 state->altsize = state->altlen = 0;
647 state->towrite = state->written = -1;
648 state->complete = st->complete;
649 cnew->state = CS_ST_ACCEPT;
650 h->state = CS_ST_IDLE;
652 #if HAVE_OPENSSL_SSL_H
653 state->ctx = st->ctx;
654 state->ctx_alloc = 0;
655 state->ssl = st->ssl;
658 state->ssl = SSL_new (state->ctx);
659 SSL_set_fd (state->ssl, cnew->iofile);
664 if (h->state == CS_ST_ACCEPT)
666 #if HAVE_OPENSSL_SSL_H
667 tcpip_state *state = (tcpip_state *)h->cprivate;
670 int res = SSL_accept (state->ssl);
671 TRC(fprintf(stderr, "SSL_accept\n"));
674 int err = SSL_get_error(state->ssl, res);
675 if (err == SSL_ERROR_WANT_READ)
677 h->io_pending = CS_WANT_READ;
680 if (err == SSL_ERROR_WANT_WRITE)
682 h->io_pending = CS_WANT_WRITE;
693 h->cerrno = CSOUTSTATE;
697 h->state = CS_ST_DATAXFER;
702 #define CS_TCPIP_BUFCHUNK 4096
705 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
706 * 0=connection closed.
708 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
710 tcpip_state *sp = (tcpip_state *)h->cprivate;
712 int tmpi, berlen, rest, req, tomove;
713 int hasread = 0, res;
715 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
716 if (sp->altlen) /* switch buffers */
718 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
719 (unsigned) sp->altbuf));
723 *bufsize = sp->altsize;
724 hasread = sp->altlen;
730 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
734 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
737 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
738 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
742 /* unfortunatly, sun sometimes forgets to set errno in recv
743 when EWOULDBLOCK etc. would be required (res = -1) */
745 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
746 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
749 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
750 strerror(yaz_errno())));
752 if (WSAGetLastError() == WSAEWOULDBLOCK)
754 h->io_pending = CS_WANT_READ;
760 if (yaz_errno() == EWOULDBLOCK
762 #if EAGAIN != EWOULDBLOCK
763 || yaz_errno() == EAGAIN
766 || yaz_errno() == EINPROGRESS
768 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
772 h->io_pending = CS_WANT_READ;
775 else if (yaz_errno() == 0)
785 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
787 /* move surplus buffer (or everything if we didn't get a BER rec.) */
788 if (hasread > berlen)
790 tomove = req = hasread - berlen;
791 rest = tomove % CS_TCPIP_BUFCHUNK;
793 req += CS_TCPIP_BUFCHUNK - rest;
796 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
798 } else if (sp->altsize < req)
799 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
801 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
802 (unsigned) sp->altbuf));
803 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
805 if (berlen < CS_TCPIP_BUFCHUNK - 1)
806 *(*buf + berlen) = '\0';
807 return berlen ? berlen : 1;
811 #if HAVE_OPENSSL_SSL_H
813 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
814 * 0=connection closed.
816 int ssl_get(COMSTACK h, char **buf, int *bufsize)
818 tcpip_state *sp = (tcpip_state *)h->cprivate;
820 int tmpi, berlen, rest, req, tomove;
821 int hasread = 0, res;
823 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
824 if (sp->altlen) /* switch buffers */
826 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
827 (unsigned) sp->altbuf));
831 *bufsize = sp->altsize;
832 hasread = sp->altlen;
838 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
842 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
845 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
846 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
848 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
849 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
852 int ssl_err = SSL_get_error(sp->ssl, res);
853 if (ssl_err == SSL_ERROR_WANT_READ)
855 h->io_pending = CS_WANT_READ;
858 if (ssl_err == SSL_ERROR_WANT_WRITE)
860 h->io_pending = CS_WANT_WRITE;
865 h->cerrno = CSERRORSSL;
870 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
872 /* move surplus buffer (or everything if we didn't get a BER rec.) */
873 if (hasread > berlen)
875 tomove = req = hasread - berlen;
876 rest = tomove % CS_TCPIP_BUFCHUNK;
878 req += CS_TCPIP_BUFCHUNK - rest;
881 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
883 } else if (sp->altsize < req)
884 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
886 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
887 (unsigned) sp->altbuf));
888 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
890 if (berlen < CS_TCPIP_BUFCHUNK - 1)
891 *(*buf + berlen) = '\0';
892 return berlen ? berlen : 1;
898 * In nonblocking mode, you must call again with same buffer while
901 int tcpip_put(COMSTACK h, char *buf, int size)
904 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
906 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
909 if (state->towrite < 0)
911 state->towrite = size;
914 else if (state->towrite != size)
916 h->cerrno = CSWRONGBUF;
919 while (state->towrite > state->written)
922 send(h->iofile, buf + state->written, size -
933 WSAGetLastError() == WSAEWOULDBLOCK
935 yaz_errno() == EWOULDBLOCK
937 #if EAGAIN != EWOULDBLOCK
938 || yaz_errno() == EAGAIN
942 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
944 || yaz_errno() == EINPROGRESS
948 TRC(fprintf(stderr, " Flow control stop\n"));
949 h->io_pending = CS_WANT_WRITE;
955 state->written += res;
956 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
957 res, state->written, size));
959 state->towrite = state->written = -1;
960 TRC(fprintf(stderr, " Ok\n"));
965 #if HAVE_OPENSSL_SSL_H
968 * In nonblocking mode, you must call again with same buffer while
971 int ssl_put(COMSTACK h, char *buf, int size)
974 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
976 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
979 if (state->towrite < 0)
981 state->towrite = size;
984 else if (state->towrite != size)
986 h->cerrno = CSWRONGBUF;
989 while (state->towrite > state->written)
991 res = SSL_write (state->ssl, buf + state->written,
992 size - state->written);
995 int ssl_err = SSL_get_error(state->ssl, res);
996 if (ssl_err == SSL_ERROR_WANT_READ)
998 h->io_pending = CS_WANT_READ;
1001 if (ssl_err == SSL_ERROR_WANT_WRITE)
1003 h->io_pending = CS_WANT_WRITE;
1006 h->cerrno = CSERRORSSL;
1009 state->written += res;
1010 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1011 res, state->written, size));
1013 state->towrite = state->written = -1;
1014 TRC(fprintf(stderr, " Ok\n"));
1019 int tcpip_close(COMSTACK h)
1021 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1023 TRC(fprintf(stderr, "tcpip_close\n"));
1024 if (h->iofile != -1)
1026 #if HAVE_OPENSSL_SSL_H
1029 SSL_shutdown (sp->ssl);
1033 closesocket(h->iofile);
1040 #if HAVE_OPENSSL_SSL_H
1043 TRC (fprintf(stderr, "SSL_free\n"));
1048 SSL_CTX_free (sp->ctx_alloc);
1055 char *tcpip_addrstr(COMSTACK h)
1057 struct sockaddr_in addr;
1058 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1059 char *r = 0, *buf = sp->buf;
1061 struct hostent *host;
1064 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1066 h->cerrno = CSYSERR;
1069 if (!(h->blocking&2)) {
1070 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1072 r = (char*) host->h_name;
1075 r = inet_ntoa(addr.sin_addr);
1076 if (h->protocol == PROTO_HTTP)
1077 sprintf(buf, "http:%s", r);
1079 sprintf(buf, "tcp:%s", r);
1080 #if HAVE_OPENSSL_SSL_H
1083 if (h->protocol == PROTO_HTTP)
1084 sprintf(buf, "https:%s", r);
1086 sprintf(buf, "ssl:%s", r);
1092 int static tcpip_set_blocking(COMSTACK p, int blocking)
1096 if (p->blocking == blocking)
1100 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1103 flag = fcntl(p->iofile, F_GETFL, 0);
1105 flag = flag & ~O_NONBLOCK;
1107 flag = flag | O_NONBLOCK;
1108 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1111 p->blocking = blocking;
1115 #if HAVE_OPENSSL_SSL_H
1116 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1118 struct tcpip_state *sp;
1119 if (!cs || cs->type != ssl_type)
1121 sp = (struct tcpip_state *) cs->cprivate;
1124 sp->ctx = (SSL_CTX *) ctx;
1128 void *cs_get_ssl(COMSTACK cs)
1130 struct tcpip_state *sp;
1131 if (!cs || cs->type != ssl_type)
1133 sp = (struct tcpip_state *) cs->cprivate;
1137 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1139 struct tcpip_state *sp;
1140 if (!cs || cs->type != ssl_type)
1142 sp = (struct tcpip_state *) cs->cprivate;
1143 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1144 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1148 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1150 SSL *ssl = (SSL *) cs_get_ssl(cs);
1153 X509 *server_cert = SSL_get_peer_certificate (ssl);
1156 BIO *bio = BIO_new(BIO_s_mem());
1158 /* get PEM buffer in memory */
1159 PEM_write_bio_X509(bio, server_cert);
1160 *len = BIO_get_mem_data(bio, &pem_buf);
1161 *buf = (char *) xmalloc(*len);
1162 memcpy(*buf, pem_buf, *len);
1170 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1175 void *cs_get_ssl(COMSTACK cs)
1180 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1185 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)