2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.14 2005-01-16 21:51:50 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 */
196 #if HAVE_OPENSSL_SSL_H
197 sp->ctx = sp->ctx_alloc = 0;
199 strcpy(sp->cert_fname, "yaz.pem");
203 sp->altsize = sp->altlen = 0;
204 sp->towrite = sp->written = -1;
205 if (protocol == PROTO_WAIS)
206 sp->complete = completeWAIS;
208 sp->complete = cs_complete_auto;
210 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
211 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
216 #if HAVE_OPENSSL_SSL_H
218 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
223 p = tcpip_type (s, blocking, protocol, 0);
229 sp = (tcpip_state *) p->cprivate;
231 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
233 /* note: we don't handle already opened socket in SSL mode - yet */
238 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
243 short int port = default_port;
248 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
249 add->sin_family = AF_INET;
250 strncpy(buf, str, 511);
252 if ((p = strchr(buf, '/')))
254 if ((p = strchr(buf, ':')))
259 add->sin_port = htons(port);
260 if (!strcmp("@", buf))
261 add->sin_addr.s_addr = INADDR_ANY;
262 else if ((hp = gethostbyname(buf)))
263 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
264 sizeof(struct in_addr));
265 else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
266 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
272 void *tcpip_straddr(COMSTACK h, const char *str)
274 tcpip_state *sp = (tcpip_state *)h->cprivate;
277 if (h->protocol == PROTO_HTTP)
280 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
285 struct sockaddr_in *tcpip_strtoaddr(const char *str)
287 static struct sockaddr_in add;
289 if (!tcpip_strtoaddr_ex (str, &add, 210))
294 int tcpip_more(COMSTACK h)
296 tcpip_state *sp = (tcpip_state *)h->cprivate;
298 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
303 * connect(2) will block (sometimes) - nothing we can do short of doing
304 * weird things like spawning subprocesses or threading or some weird junk
307 int tcpip_connect(COMSTACK h, void *address)
309 struct sockaddr_in *add = (struct sockaddr_in *)address;
313 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
315 TRC(fprintf(stderr, "tcpip_connect\n"));
317 if (h->state != CS_ST_UNBND)
319 h->cerrno = CSOUTSTATE;
323 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
324 * This gives the connect a chance to negotiate with the other side
327 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
332 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
334 recbuflen *= 10; /* lets be optimistic */
335 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
340 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
345 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
348 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
352 if (WSAGetLastError() == WSAEWOULDBLOCK)
354 h->event = CS_CONNECT;
355 h->state = CS_ST_CONNECTING;
356 h->io_pending = CS_WANT_WRITE;
360 if (yaz_errno() == EINPROGRESS)
362 h->event = CS_CONNECT;
363 h->state = CS_ST_CONNECTING;
364 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
371 h->event = CS_CONNECT;
372 h->state = CS_ST_CONNECTING;
374 return tcpip_rcvconnect (h);
380 int tcpip_rcvconnect(COMSTACK h)
382 #if HAVE_OPENSSL_SSL_H
383 tcpip_state *sp = (tcpip_state *)h->cprivate;
385 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
387 if (h->state == CS_ST_DATAXFER)
389 if (h->state != CS_ST_CONNECTING)
391 h->cerrno = CSOUTSTATE;
394 #if HAVE_OPENSSL_SSL_H
395 if (h->type == ssl_type && !sp->ctx)
397 SSL_load_error_strings();
398 SSLeay_add_all_algorithms();
400 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
403 h->cerrno = CSERRORSSL;
413 sp->ssl = SSL_new (sp->ctx);
414 SSL_set_fd (sp->ssl, h->iofile);
416 res = SSL_connect (sp->ssl);
419 int err = SSL_get_error(sp->ssl, res);
420 if (err == SSL_ERROR_WANT_READ)
422 h->io_pending = CS_WANT_READ;
425 if (err == SSL_ERROR_WANT_WRITE)
427 h->io_pending = CS_WANT_WRITE;
430 h->cerrno = CSERRORSSL;
436 h->state = CS_ST_DATAXFER;
440 #define CERTF "ztest.pem"
441 #define KEYF "ztest.pem"
443 static void tcpip_setsockopt (int fd)
449 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
451 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
453 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
455 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
457 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
459 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
464 static int tcpip_bind(COMSTACK h, void *address, int mode)
466 struct sockaddr *addr = (struct sockaddr *)address;
470 unsigned long one = 1;
473 #if HAVE_OPENSSL_SSL_H
474 tcpip_state *sp = (tcpip_state *)h->cprivate;
475 if (h->type == ssl_type && !sp->ctx)
477 SSL_load_error_strings();
478 SSLeay_add_all_algorithms();
480 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
483 h->cerrno = CSERRORSSL;
492 res = SSL_CTX_use_certificate_file (sp->ctx, sp->cert_fname,
496 ERR_print_errors_fp(stderr);
499 res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
503 ERR_print_errors_fp(stderr);
506 res = SSL_CTX_check_private_key (sp->ctx);
509 ERR_print_errors_fp(stderr);
513 TRC (fprintf (stderr, "ssl_bind\n"));
517 TRC (fprintf (stderr, "tcpip_bind\n"));
520 TRC (fprintf (stderr, "tcpip_bind\n"));
523 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
524 &one, sizeof(one)) < 0)
530 tcpip_setsockopt(h->iofile);
531 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)))
536 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
541 h->state = CS_ST_IDLE;
542 h->event = CS_LISTEN;
546 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
547 int (*check_ip)(void *cd, const char *a, int len, int t),
550 struct sockaddr_in addr;
551 YAZ_SOCKLEN_T len = sizeof(addr);
553 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
554 if (h->state != CS_ST_IDLE)
556 h->cerrno = CSOUTSTATE;
559 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
564 WSAGetLastError() == WSAEWOULDBLOCK
566 yaz_errno() == EWOULDBLOCK
568 #if EAGAIN != EWOULDBLOCK
569 || yaz_errno() == EAGAIN
574 h->cerrno = CSNODATA;
579 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
580 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
583 if (check_ip && (*check_ip)(cd, (const char *) &addr,
584 sizeof(addr), AF_INET))
588 closesocket(h->newfd);
595 h->state = CS_ST_INCON;
596 tcpip_setsockopt (h->newfd);
600 COMSTACK tcpip_accept(COMSTACK h)
603 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
605 unsigned long tru = 1;
608 TRC(fprintf(stderr, "tcpip_accept\n"));
609 if (h->state == CS_ST_INCON)
611 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
615 closesocket(h->newfd);
622 memcpy(cnew, h, sizeof(*h));
623 cnew->iofile = h->newfd;
624 cnew->io_pending = 0;
625 if (!(state = (tcpip_state *)
626 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
632 closesocket(h->newfd);
640 if (!(cnew->blocking&1) &&
642 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
644 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
652 closesocket(h->newfd);
664 state->altsize = state->altlen = 0;
665 state->towrite = state->written = -1;
666 state->complete = st->complete;
667 cnew->state = CS_ST_ACCEPT;
668 h->state = CS_ST_IDLE;
670 #if HAVE_OPENSSL_SSL_H
671 state->ctx = st->ctx;
672 state->ctx_alloc = 0;
673 state->ssl = st->ssl;
676 state->ssl = SSL_new (state->ctx);
677 SSL_set_fd (state->ssl, cnew->iofile);
682 if (h->state == CS_ST_ACCEPT)
684 #if HAVE_OPENSSL_SSL_H
685 tcpip_state *state = (tcpip_state *)h->cprivate;
688 int res = SSL_accept (state->ssl);
689 TRC(fprintf(stderr, "SSL_accept\n"));
692 int err = SSL_get_error(state->ssl, res);
693 if (err == SSL_ERROR_WANT_READ)
695 h->io_pending = CS_WANT_READ;
698 if (err == SSL_ERROR_WANT_WRITE)
700 h->io_pending = CS_WANT_WRITE;
711 h->cerrno = CSOUTSTATE;
715 h->state = CS_ST_DATAXFER;
720 #define CS_TCPIP_BUFCHUNK 4096
723 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
724 * 0=connection closed.
726 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
728 tcpip_state *sp = (tcpip_state *)h->cprivate;
730 int tmpi, berlen, rest, req, tomove;
731 int hasread = 0, res;
733 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
734 if (sp->altlen) /* switch buffers */
736 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
737 (unsigned) sp->altbuf));
741 *bufsize = sp->altsize;
742 hasread = sp->altlen;
748 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
752 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
755 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
756 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
760 /* unfortunatly, sun sometimes forgets to set errno in recv
761 when EWOULDBLOCK etc. would be required (res = -1) */
763 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
764 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
767 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
768 strerror(yaz_errno())));
770 if (WSAGetLastError() == WSAEWOULDBLOCK)
772 h->io_pending = CS_WANT_READ;
778 if (yaz_errno() == EWOULDBLOCK
780 #if EAGAIN != EWOULDBLOCK
781 || yaz_errno() == EAGAIN
784 || yaz_errno() == EINPROGRESS
786 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
790 h->io_pending = CS_WANT_READ;
793 else if (yaz_errno() == 0)
803 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
805 /* move surplus buffer (or everything if we didn't get a BER rec.) */
806 if (hasread > berlen)
808 tomove = req = hasread - berlen;
809 rest = tomove % CS_TCPIP_BUFCHUNK;
811 req += CS_TCPIP_BUFCHUNK - rest;
814 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
816 } else if (sp->altsize < req)
817 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
819 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
820 (unsigned) sp->altbuf));
821 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
823 if (berlen < CS_TCPIP_BUFCHUNK - 1)
824 *(*buf + berlen) = '\0';
825 return berlen ? berlen : 1;
829 #if HAVE_OPENSSL_SSL_H
831 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
832 * 0=connection closed.
834 int ssl_get(COMSTACK h, char **buf, int *bufsize)
836 tcpip_state *sp = (tcpip_state *)h->cprivate;
838 int tmpi, berlen, rest, req, tomove;
839 int hasread = 0, res;
841 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
842 if (sp->altlen) /* switch buffers */
844 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
845 (unsigned) sp->altbuf));
849 *bufsize = sp->altsize;
850 hasread = sp->altlen;
856 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
860 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
863 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
864 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
866 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
867 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
870 int ssl_err = SSL_get_error(sp->ssl, res);
871 if (ssl_err == SSL_ERROR_WANT_READ)
873 h->io_pending = CS_WANT_READ;
876 if (ssl_err == SSL_ERROR_WANT_WRITE)
878 h->io_pending = CS_WANT_WRITE;
883 h->cerrno = CSERRORSSL;
888 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
890 /* move surplus buffer (or everything if we didn't get a BER rec.) */
891 if (hasread > berlen)
893 tomove = req = hasread - berlen;
894 rest = tomove % CS_TCPIP_BUFCHUNK;
896 req += CS_TCPIP_BUFCHUNK - rest;
899 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
901 } else if (sp->altsize < req)
902 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
904 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
905 (unsigned) sp->altbuf));
906 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
908 if (berlen < CS_TCPIP_BUFCHUNK - 1)
909 *(*buf + berlen) = '\0';
910 return berlen ? berlen : 1;
916 * In nonblocking mode, you must call again with same buffer while
919 int tcpip_put(COMSTACK h, char *buf, int size)
922 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
924 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
927 if (state->towrite < 0)
929 state->towrite = size;
932 else if (state->towrite != size)
934 h->cerrno = CSWRONGBUF;
937 while (state->towrite > state->written)
940 send(h->iofile, buf + state->written, size -
951 WSAGetLastError() == WSAEWOULDBLOCK
953 yaz_errno() == EWOULDBLOCK
955 #if EAGAIN != EWOULDBLOCK
956 || yaz_errno() == EAGAIN
960 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
962 || yaz_errno() == EINPROGRESS
966 TRC(fprintf(stderr, " Flow control stop\n"));
967 h->io_pending = CS_WANT_WRITE;
973 state->written += res;
974 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
975 res, state->written, size));
977 state->towrite = state->written = -1;
978 TRC(fprintf(stderr, " Ok\n"));
983 #if HAVE_OPENSSL_SSL_H
986 * In nonblocking mode, you must call again with same buffer while
989 int ssl_put(COMSTACK h, char *buf, int size)
992 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
994 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
997 if (state->towrite < 0)
999 state->towrite = size;
1002 else if (state->towrite != size)
1004 h->cerrno = CSWRONGBUF;
1007 while (state->towrite > state->written)
1009 res = SSL_write (state->ssl, buf + state->written,
1010 size - state->written);
1013 int ssl_err = SSL_get_error(state->ssl, res);
1014 if (ssl_err == SSL_ERROR_WANT_READ)
1016 h->io_pending = CS_WANT_READ;
1019 if (ssl_err == SSL_ERROR_WANT_WRITE)
1021 h->io_pending = CS_WANT_WRITE;
1024 h->cerrno = CSERRORSSL;
1027 state->written += res;
1028 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1029 res, state->written, size));
1031 state->towrite = state->written = -1;
1032 TRC(fprintf(stderr, " Ok\n"));
1037 int tcpip_close(COMSTACK h)
1039 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1041 TRC(fprintf(stderr, "tcpip_close\n"));
1042 if (h->iofile != -1)
1044 #if HAVE_OPENSSL_SSL_H
1047 SSL_shutdown (sp->ssl);
1051 closesocket(h->iofile);
1058 #if HAVE_OPENSSL_SSL_H
1061 TRC (fprintf(stderr, "SSL_free\n"));
1066 SSL_CTX_free (sp->ctx_alloc);
1073 char *tcpip_addrstr(COMSTACK h)
1075 struct sockaddr_in addr;
1076 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1077 char *r = 0, *buf = sp->buf;
1079 struct hostent *host;
1082 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1084 h->cerrno = CSYSERR;
1087 if (!(h->blocking&2)) {
1088 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1090 r = (char*) host->h_name;
1093 r = inet_ntoa(addr.sin_addr);
1094 if (h->protocol == PROTO_HTTP)
1095 sprintf(buf, "http:%s", r);
1097 sprintf(buf, "tcp:%s", r);
1098 #if HAVE_OPENSSL_SSL_H
1101 if (h->protocol == PROTO_HTTP)
1102 sprintf(buf, "https:%s", r);
1104 sprintf(buf, "ssl:%s", r);
1110 int static tcpip_set_blocking(COMSTACK p, int blocking)
1114 if (p->blocking == blocking)
1118 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1121 flag = fcntl(p->iofile, F_GETFL, 0);
1123 flag = flag & ~O_NONBLOCK;
1125 flag = flag | O_NONBLOCK;
1126 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1129 p->blocking = blocking;
1133 #if HAVE_OPENSSL_SSL_H
1134 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1136 struct tcpip_state *sp;
1137 if (!cs || cs->type != ssl_type)
1139 sp = (struct tcpip_state *) cs->cprivate;
1142 sp->ctx = (SSL_CTX *) ctx;
1146 void *cs_get_ssl(COMSTACK cs)
1148 struct tcpip_state *sp;
1149 if (!cs || cs->type != ssl_type)
1151 sp = (struct tcpip_state *) cs->cprivate;
1155 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1157 struct tcpip_state *sp;
1158 if (!cs || cs->type != ssl_type)
1160 sp = (struct tcpip_state *) cs->cprivate;
1161 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1162 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1166 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1168 SSL *ssl = (SSL *) cs_get_ssl(cs);
1171 X509 *server_cert = SSL_get_peer_certificate (ssl);
1174 BIO *bio = BIO_new(BIO_s_mem());
1176 /* get PEM buffer in memory */
1177 PEM_write_bio_X509(bio, server_cert);
1178 *len = BIO_get_mem_data(bio, &pem_buf);
1179 *buf = (char *) xmalloc(*len);
1180 memcpy(*buf, pem_buf, *len);
1188 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1193 void *cs_get_ssl(COMSTACK cs)
1198 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1203 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)