2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.15 2005-02-01 14:37:47 adam Exp $
9 * \brief Implements TCP/IP + SSL COMSTACK.
19 #include <sys/types.h>
31 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <netinet/tcp.h>
38 #include <sys/socket.h>
41 #include <sys/select.h>
47 #if HAVE_OPENSSL_SSL_H
48 #include <openssl/ssl.h>
49 #include <openssl/err.h>
52 #include <yaz/comstack.h>
53 #include <yaz/tcpip.h>
56 static int tcpip_close(COMSTACK h);
57 static int tcpip_put(COMSTACK h, char *buf, int size);
58 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
59 static int tcpip_connect(COMSTACK h, void *address);
60 static int tcpip_more(COMSTACK h);
61 static int tcpip_rcvconnect(COMSTACK h);
62 static int tcpip_bind(COMSTACK h, void *address, int mode);
63 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
64 int (*check_ip)(void *cd, const char *a, int len, int type),
66 static int tcpip_set_blocking(COMSTACK p, int blocking);
68 #if HAVE_OPENSSL_SSL_H
69 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
70 static int ssl_put(COMSTACK h, char *buf, int size);
73 static COMSTACK tcpip_accept(COMSTACK h);
74 static char *tcpip_addrstr(COMSTACK h);
75 static void *tcpip_straddr(COMSTACK h, const char *str);
84 #define YAZ_SOCKLEN_T int
87 /* this state is used for both SSL and straight TCP/IP */
88 typedef struct tcpip_state
90 char *altbuf; /* alternate buffer for surplus data */
91 int altsize; /* size as xmalloced */
92 int altlen; /* length of data or 0 if none */
94 int written; /* -1 if we aren't writing */
95 int towrite; /* to verify against user input */
96 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
97 struct sockaddr_in addr; /* returned by cs_straddr */
98 char buf[128]; /* returned by cs_addrstr */
99 #if HAVE_OPENSSL_SSL_H
100 SSL_CTX *ctx; /* current CTX. */
101 SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
103 char cert_fname[256];
108 static int tcpip_init (void)
110 static int initialized = 0;
116 requested = MAKEWORD(1, 1);
117 if (WSAStartup(requested, &wd))
124 static int tcpip_init (void)
131 * This function is always called through the cs_create() macro.
132 * s >= 0: socket has already been established for us.
134 COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
140 unsigned long tru = 1;
147 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
153 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
155 if (!(sp = (struct tcpip_state *)(p->cprivate =
156 xmalloc(sizeof(tcpip_state)))))
159 if (!((p->blocking = blocking)&1))
162 if (ioctlsocket(s, FIONBIO, &tru) < 0)
165 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
168 signal (SIGPIPE, SIG_IGN);
175 p->type = tcpip_type;
176 p->protocol = (enum oid_proto) protocol;
178 p->f_connect = tcpip_connect;
179 p->f_rcvconnect = tcpip_rcvconnect;
180 p->f_get = tcpip_get;
181 p->f_put = tcpip_put;
182 p->f_close = tcpip_close;
183 p->f_more = tcpip_more;
184 p->f_bind = tcpip_bind;
185 p->f_listen = tcpip_listen;
186 p->f_accept = tcpip_accept;
187 p->f_addrstr = tcpip_addrstr;
188 p->f_straddr = tcpip_straddr;
189 p->f_set_blocking = tcpip_set_blocking;
191 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
197 #if HAVE_OPENSSL_SSL_H
198 sp->ctx = sp->ctx_alloc = 0;
200 strcpy(sp->cert_fname, "yaz.pem");
204 sp->altsize = sp->altlen = 0;
205 sp->towrite = sp->written = -1;
206 if (protocol == PROTO_WAIS)
207 sp->complete = completeWAIS;
209 sp->complete = cs_complete_auto;
211 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
212 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
217 #if HAVE_OPENSSL_SSL_H
219 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
224 p = tcpip_type (s, blocking, protocol, 0);
230 sp = (tcpip_state *) p->cprivate;
232 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
234 /* note: we don't handle already opened socket in SSL mode - yet */
239 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
244 short int port = default_port;
249 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
250 add->sin_family = AF_INET;
251 strncpy(buf, str, 511);
253 if ((p = strchr(buf, '/')))
255 if ((p = strchr(buf, ':')))
260 add->sin_port = htons(port);
261 if (!strcmp("@", buf))
262 add->sin_addr.s_addr = INADDR_ANY;
263 else if ((hp = gethostbyname(buf)))
264 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
265 sizeof(struct in_addr));
266 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
267 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
273 void *tcpip_straddr(COMSTACK h, const char *str)
275 tcpip_state *sp = (tcpip_state *)h->cprivate;
278 if (h->protocol == PROTO_HTTP)
281 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
286 struct sockaddr_in *tcpip_strtoaddr(const char *str)
288 static struct sockaddr_in add;
290 if (!tcpip_strtoaddr_ex (str, &add, 210))
295 int tcpip_more(COMSTACK h)
297 tcpip_state *sp = (tcpip_state *)h->cprivate;
299 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
304 * connect(2) will block (sometimes) - nothing we can do short of doing
305 * weird things like spawning subprocesses or threading or some weird junk
308 int tcpip_connect(COMSTACK h, void *address)
310 struct sockaddr_in *add = (struct sockaddr_in *)address;
314 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
316 TRC(fprintf(stderr, "tcpip_connect\n"));
318 if (h->state != CS_ST_UNBND)
320 h->cerrno = CSOUTSTATE;
324 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
325 * This gives the connect a chance to negotiate with the other side
328 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
333 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
335 recbuflen *= 10; /* lets be optimistic */
336 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
341 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
346 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
349 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
353 if (WSAGetLastError() == WSAEWOULDBLOCK)
355 h->event = CS_CONNECT;
356 h->state = CS_ST_CONNECTING;
357 h->io_pending = CS_WANT_WRITE;
361 if (yaz_errno() == EINPROGRESS)
363 h->event = CS_CONNECT;
364 h->state = CS_ST_CONNECTING;
365 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
372 h->event = CS_CONNECT;
373 h->state = CS_ST_CONNECTING;
375 return tcpip_rcvconnect (h);
381 int tcpip_rcvconnect(COMSTACK h)
383 #if HAVE_OPENSSL_SSL_H
384 tcpip_state *sp = (tcpip_state *)h->cprivate;
386 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
388 if (h->state == CS_ST_DATAXFER)
390 if (h->state != CS_ST_CONNECTING)
392 h->cerrno = CSOUTSTATE;
395 #if HAVE_OPENSSL_SSL_H
396 if (h->type == ssl_type && !sp->ctx)
398 SSL_load_error_strings();
399 SSLeay_add_all_algorithms();
401 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
404 h->cerrno = CSERRORSSL;
414 sp->ssl = SSL_new (sp->ctx);
415 SSL_set_fd (sp->ssl, h->iofile);
417 res = SSL_connect (sp->ssl);
420 int err = SSL_get_error(sp->ssl, res);
421 if (err == SSL_ERROR_WANT_READ)
423 h->io_pending = CS_WANT_READ;
426 if (err == SSL_ERROR_WANT_WRITE)
428 h->io_pending = CS_WANT_WRITE;
431 h->cerrno = CSERRORSSL;
437 h->state = CS_ST_DATAXFER;
441 #define CERTF "ztest.pem"
442 #define KEYF "ztest.pem"
444 static void tcpip_setsockopt (int fd)
450 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
452 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
454 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
456 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
458 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
460 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
465 static int tcpip_bind(COMSTACK h, void *address, int mode)
467 struct sockaddr *addr = (struct sockaddr *)address;
471 unsigned long one = 1;
474 #if HAVE_OPENSSL_SSL_H
475 tcpip_state *sp = (tcpip_state *)h->cprivate;
476 if (h->type == ssl_type && !sp->ctx)
478 SSL_load_error_strings();
479 SSLeay_add_all_algorithms();
481 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
484 h->cerrno = CSERRORSSL;
493 res = SSL_CTX_use_certificate_file (sp->ctx, sp->cert_fname,
497 ERR_print_errors_fp(stderr);
500 res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
504 ERR_print_errors_fp(stderr);
507 res = SSL_CTX_check_private_key (sp->ctx);
510 ERR_print_errors_fp(stderr);
514 TRC (fprintf (stderr, "ssl_bind\n"));
518 TRC (fprintf (stderr, "tcpip_bind\n"));
521 TRC (fprintf (stderr, "tcpip_bind\n"));
524 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
525 &one, sizeof(one)) < 0)
531 tcpip_setsockopt(h->iofile);
532 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)))
537 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
542 h->state = CS_ST_IDLE;
543 h->event = CS_LISTEN;
547 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
548 int (*check_ip)(void *cd, const char *a, int len, int t),
551 struct sockaddr_in addr;
552 YAZ_SOCKLEN_T len = sizeof(addr);
554 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
555 if (h->state != CS_ST_IDLE)
557 h->cerrno = CSOUTSTATE;
560 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
565 WSAGetLastError() == WSAEWOULDBLOCK
567 yaz_errno() == EWOULDBLOCK
569 #if EAGAIN != EWOULDBLOCK
570 || yaz_errno() == EAGAIN
575 h->cerrno = CSNODATA;
580 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
581 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
584 if (check_ip && (*check_ip)(cd, (const char *) &addr,
585 sizeof(addr), AF_INET))
589 closesocket(h->newfd);
596 h->state = CS_ST_INCON;
597 tcpip_setsockopt (h->newfd);
601 COMSTACK tcpip_accept(COMSTACK h)
604 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
606 unsigned long tru = 1;
609 TRC(fprintf(stderr, "tcpip_accept\n"));
610 if (h->state == CS_ST_INCON)
612 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
616 closesocket(h->newfd);
623 memcpy(cnew, h, sizeof(*h));
624 cnew->iofile = h->newfd;
625 cnew->io_pending = 0;
626 if (!(state = (tcpip_state *)
627 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
633 closesocket(h->newfd);
641 if (!(cnew->blocking&1) &&
643 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
645 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
653 closesocket(h->newfd);
665 state->altsize = state->altlen = 0;
666 state->towrite = state->written = -1;
667 state->complete = st->complete;
668 cnew->state = CS_ST_ACCEPT;
669 h->state = CS_ST_IDLE;
671 #if HAVE_OPENSSL_SSL_H
672 state->ctx = st->ctx;
673 state->ctx_alloc = 0;
674 state->ssl = st->ssl;
677 state->ssl = SSL_new (state->ctx);
678 SSL_set_fd (state->ssl, cnew->iofile);
683 if (h->state == CS_ST_ACCEPT)
685 #if HAVE_OPENSSL_SSL_H
686 tcpip_state *state = (tcpip_state *)h->cprivate;
689 int res = SSL_accept (state->ssl);
690 TRC(fprintf(stderr, "SSL_accept\n"));
693 int err = SSL_get_error(state->ssl, res);
694 if (err == SSL_ERROR_WANT_READ)
696 h->io_pending = CS_WANT_READ;
699 if (err == SSL_ERROR_WANT_WRITE)
701 h->io_pending = CS_WANT_WRITE;
712 h->cerrno = CSOUTSTATE;
716 h->state = CS_ST_DATAXFER;
721 #define CS_TCPIP_BUFCHUNK 4096
724 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
725 * 0=connection closed.
727 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
729 tcpip_state *sp = (tcpip_state *)h->cprivate;
731 int tmpi, berlen, rest, req, tomove;
732 int hasread = 0, res;
734 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
735 if (sp->altlen) /* switch buffers */
737 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
738 (unsigned) sp->altbuf));
742 *bufsize = sp->altsize;
743 hasread = sp->altlen;
749 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
753 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
756 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
757 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
761 /* unfortunatly, sun sometimes forgets to set errno in recv
762 when EWOULDBLOCK etc. would be required (res = -1) */
764 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
765 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
768 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
769 strerror(yaz_errno())));
771 if (WSAGetLastError() == WSAEWOULDBLOCK)
773 h->io_pending = CS_WANT_READ;
779 if (yaz_errno() == EWOULDBLOCK
781 #if EAGAIN != EWOULDBLOCK
782 || yaz_errno() == EAGAIN
785 || yaz_errno() == EINPROGRESS
787 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
791 h->io_pending = CS_WANT_READ;
794 else if (yaz_errno() == 0)
804 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
806 /* move surplus buffer (or everything if we didn't get a BER rec.) */
807 if (hasread > berlen)
809 tomove = req = hasread - berlen;
810 rest = tomove % CS_TCPIP_BUFCHUNK;
812 req += CS_TCPIP_BUFCHUNK - rest;
815 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
817 } else if (sp->altsize < req)
818 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
820 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
821 (unsigned) sp->altbuf));
822 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
824 if (berlen < CS_TCPIP_BUFCHUNK - 1)
825 *(*buf + berlen) = '\0';
826 return berlen ? berlen : 1;
830 #if HAVE_OPENSSL_SSL_H
832 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
833 * 0=connection closed.
835 int ssl_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, "ssl_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)))
864 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
865 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
867 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
868 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
871 int ssl_err = SSL_get_error(sp->ssl, res);
872 if (ssl_err == SSL_ERROR_WANT_READ)
874 h->io_pending = CS_WANT_READ;
877 if (ssl_err == SSL_ERROR_WANT_WRITE)
879 h->io_pending = CS_WANT_WRITE;
884 h->cerrno = CSERRORSSL;
889 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
891 /* move surplus buffer (or everything if we didn't get a BER rec.) */
892 if (hasread > berlen)
894 tomove = req = hasread - berlen;
895 rest = tomove % CS_TCPIP_BUFCHUNK;
897 req += CS_TCPIP_BUFCHUNK - rest;
900 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
902 } else if (sp->altsize < req)
903 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
905 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
906 (unsigned) sp->altbuf));
907 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
909 if (berlen < CS_TCPIP_BUFCHUNK - 1)
910 *(*buf + berlen) = '\0';
911 return berlen ? berlen : 1;
917 * In nonblocking mode, you must call again with same buffer while
920 int tcpip_put(COMSTACK h, char *buf, int size)
923 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
925 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
928 if (state->towrite < 0)
930 state->towrite = size;
933 else if (state->towrite != size)
935 h->cerrno = CSWRONGBUF;
938 while (state->towrite > state->written)
941 send(h->iofile, buf + state->written, size -
952 WSAGetLastError() == WSAEWOULDBLOCK
954 yaz_errno() == EWOULDBLOCK
956 #if EAGAIN != EWOULDBLOCK
957 || yaz_errno() == EAGAIN
961 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
963 || yaz_errno() == EINPROGRESS
967 TRC(fprintf(stderr, " Flow control stop\n"));
968 h->io_pending = CS_WANT_WRITE;
974 state->written += res;
975 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
976 res, state->written, size));
978 state->towrite = state->written = -1;
979 TRC(fprintf(stderr, " Ok\n"));
984 #if HAVE_OPENSSL_SSL_H
987 * In nonblocking mode, you must call again with same buffer while
990 int ssl_put(COMSTACK h, char *buf, int size)
993 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
995 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
998 if (state->towrite < 0)
1000 state->towrite = size;
1003 else if (state->towrite != size)
1005 h->cerrno = CSWRONGBUF;
1008 while (state->towrite > state->written)
1010 res = SSL_write (state->ssl, buf + state->written,
1011 size - state->written);
1014 int ssl_err = SSL_get_error(state->ssl, res);
1015 if (ssl_err == SSL_ERROR_WANT_READ)
1017 h->io_pending = CS_WANT_READ;
1020 if (ssl_err == SSL_ERROR_WANT_WRITE)
1022 h->io_pending = CS_WANT_WRITE;
1025 h->cerrno = CSERRORSSL;
1028 state->written += res;
1029 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1030 res, state->written, size));
1032 state->towrite = state->written = -1;
1033 TRC(fprintf(stderr, " Ok\n"));
1038 int tcpip_close(COMSTACK h)
1040 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1042 TRC(fprintf(stderr, "tcpip_close\n"));
1043 if (h->iofile != -1)
1045 #if HAVE_OPENSSL_SSL_H
1048 SSL_shutdown (sp->ssl);
1052 closesocket(h->iofile);
1059 #if HAVE_OPENSSL_SSL_H
1062 TRC (fprintf(stderr, "SSL_free\n"));
1067 SSL_CTX_free (sp->ctx_alloc);
1074 char *tcpip_addrstr(COMSTACK h)
1076 struct sockaddr_in addr;
1077 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1078 char *r = 0, *buf = sp->buf;
1080 struct hostent *host;
1083 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1085 h->cerrno = CSYSERR;
1088 if (!(h->blocking&2)) {
1089 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1091 r = (char*) host->h_name;
1094 r = inet_ntoa(addr.sin_addr);
1095 if (h->protocol == PROTO_HTTP)
1096 sprintf(buf, "http:%s", r);
1098 sprintf(buf, "tcp:%s", r);
1099 #if HAVE_OPENSSL_SSL_H
1102 if (h->protocol == PROTO_HTTP)
1103 sprintf(buf, "https:%s", r);
1105 sprintf(buf, "ssl:%s", r);
1111 int static tcpip_set_blocking(COMSTACK p, int blocking)
1115 if (p->blocking == blocking)
1119 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1122 flag = fcntl(p->iofile, F_GETFL, 0);
1124 flag = flag & ~O_NONBLOCK;
1126 flag = flag | O_NONBLOCK;
1127 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1130 p->blocking = blocking;
1134 #if HAVE_OPENSSL_SSL_H
1135 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1137 struct tcpip_state *sp;
1138 if (!cs || cs->type != ssl_type)
1140 sp = (struct tcpip_state *) cs->cprivate;
1143 sp->ctx = (SSL_CTX *) ctx;
1147 void *cs_get_ssl(COMSTACK cs)
1149 struct tcpip_state *sp;
1150 if (!cs || cs->type != ssl_type)
1152 sp = (struct tcpip_state *) cs->cprivate;
1156 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1158 struct tcpip_state *sp;
1159 if (!cs || cs->type != ssl_type)
1161 sp = (struct tcpip_state *) cs->cprivate;
1162 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1163 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1167 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1169 SSL *ssl = (SSL *) cs_get_ssl(cs);
1172 X509 *server_cert = SSL_get_peer_certificate (ssl);
1175 BIO *bio = BIO_new(BIO_s_mem());
1177 /* get PEM buffer in memory */
1178 PEM_write_bio_X509(bio, server_cert);
1179 *len = BIO_get_mem_data(bio, &pem_buf);
1180 *buf = (char *) xmalloc(*len);
1181 memcpy(*buf, pem_buf, *len);
1189 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1194 void *cs_get_ssl(COMSTACK cs)
1199 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1204 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)