2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.17 2005-07-19 12:40:52 mike 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 /* Allow a maximum-sized backlog of waiting-to-connect clients */
538 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
543 h->state = CS_ST_IDLE;
544 h->event = CS_LISTEN;
548 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
549 int (*check_ip)(void *cd, const char *a, int len, int t),
552 struct sockaddr_in addr;
553 YAZ_SOCKLEN_T len = sizeof(addr);
555 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
556 if (h->state != CS_ST_IDLE)
558 h->cerrno = CSOUTSTATE;
561 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
566 WSAGetLastError() == WSAEWOULDBLOCK
568 yaz_errno() == EWOULDBLOCK
570 #if EAGAIN != EWOULDBLOCK
571 || yaz_errno() == EAGAIN
576 h->cerrno = CSNODATA;
581 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
582 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
585 if (check_ip && (*check_ip)(cd, (const char *) &addr,
586 sizeof(addr), AF_INET))
590 closesocket(h->newfd);
597 h->state = CS_ST_INCON;
598 tcpip_setsockopt (h->newfd);
602 COMSTACK tcpip_accept(COMSTACK h)
605 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
607 unsigned long tru = 1;
610 TRC(fprintf(stderr, "tcpip_accept\n"));
611 if (h->state == CS_ST_INCON)
613 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
617 closesocket(h->newfd);
624 memcpy(cnew, h, sizeof(*h));
625 cnew->iofile = h->newfd;
626 cnew->io_pending = 0;
627 if (!(state = (tcpip_state *)
628 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
634 closesocket(h->newfd);
642 if (!(cnew->blocking&1) &&
644 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
646 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
654 closesocket(h->newfd);
666 state->altsize = state->altlen = 0;
667 state->towrite = state->written = -1;
668 state->complete = st->complete;
669 cnew->state = CS_ST_ACCEPT;
670 h->state = CS_ST_IDLE;
672 #if HAVE_OPENSSL_SSL_H
673 state->ctx = st->ctx;
674 state->ctx_alloc = 0;
675 state->ssl = st->ssl;
678 state->ssl = SSL_new (state->ctx);
679 SSL_set_fd (state->ssl, cnew->iofile);
684 if (h->state == CS_ST_ACCEPT)
686 #if HAVE_OPENSSL_SSL_H
687 tcpip_state *state = (tcpip_state *)h->cprivate;
690 int res = SSL_accept (state->ssl);
691 TRC(fprintf(stderr, "SSL_accept\n"));
694 int err = SSL_get_error(state->ssl, res);
695 if (err == SSL_ERROR_WANT_READ)
697 h->io_pending = CS_WANT_READ;
700 if (err == SSL_ERROR_WANT_WRITE)
702 h->io_pending = CS_WANT_WRITE;
713 h->cerrno = CSOUTSTATE;
717 h->state = CS_ST_DATAXFER;
722 #define CS_TCPIP_BUFCHUNK 4096
725 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
726 * 0=connection closed.
728 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
730 tcpip_state *sp = (tcpip_state *)h->cprivate;
732 int tmpi, berlen, rest, req, tomove;
733 int hasread = 0, res;
735 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
736 if (sp->altlen) /* switch buffers */
738 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
739 (unsigned) sp->altbuf));
743 *bufsize = sp->altsize;
744 hasread = sp->altlen;
750 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
754 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
757 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
758 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
762 /* unfortunatly, sun sometimes forgets to set errno in recv
763 when EWOULDBLOCK etc. would be required (res = -1) */
765 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
766 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
769 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
770 strerror(yaz_errno())));
772 if (WSAGetLastError() == WSAEWOULDBLOCK)
774 h->io_pending = CS_WANT_READ;
780 if (yaz_errno() == EWOULDBLOCK
782 #if EAGAIN != EWOULDBLOCK
783 || yaz_errno() == EAGAIN
786 || yaz_errno() == EINPROGRESS
788 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
792 h->io_pending = CS_WANT_READ;
795 else if (yaz_errno() == 0)
805 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
807 /* move surplus buffer (or everything if we didn't get a BER rec.) */
808 if (hasread > berlen)
810 tomove = req = hasread - berlen;
811 rest = tomove % CS_TCPIP_BUFCHUNK;
813 req += CS_TCPIP_BUFCHUNK - rest;
816 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
818 } else if (sp->altsize < req)
819 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
821 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
822 (unsigned) sp->altbuf));
823 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
825 if (berlen < CS_TCPIP_BUFCHUNK - 1)
826 *(*buf + berlen) = '\0';
827 return berlen ? berlen : 1;
831 #if HAVE_OPENSSL_SSL_H
833 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
834 * 0=connection closed.
836 int ssl_get(COMSTACK h, char **buf, int *bufsize)
838 tcpip_state *sp = (tcpip_state *)h->cprivate;
840 int tmpi, berlen, rest, req, tomove;
841 int hasread = 0, res;
843 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
844 if (sp->altlen) /* switch buffers */
846 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
847 (unsigned) sp->altbuf));
851 *bufsize = sp->altsize;
852 hasread = sp->altlen;
858 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
862 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
865 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
866 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
868 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
869 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
872 int ssl_err = SSL_get_error(sp->ssl, res);
873 if (ssl_err == SSL_ERROR_WANT_READ)
875 h->io_pending = CS_WANT_READ;
878 if (ssl_err == SSL_ERROR_WANT_WRITE)
880 h->io_pending = CS_WANT_WRITE;
885 h->cerrno = CSERRORSSL;
890 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
892 /* move surplus buffer (or everything if we didn't get a BER rec.) */
893 if (hasread > berlen)
895 tomove = req = hasread - berlen;
896 rest = tomove % CS_TCPIP_BUFCHUNK;
898 req += CS_TCPIP_BUFCHUNK - rest;
901 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
903 } else if (sp->altsize < req)
904 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
906 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
907 (unsigned) sp->altbuf));
908 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
910 if (berlen < CS_TCPIP_BUFCHUNK - 1)
911 *(*buf + berlen) = '\0';
912 return berlen ? berlen : 1;
918 * In nonblocking mode, you must call again with same buffer while
921 int tcpip_put(COMSTACK h, char *buf, int size)
924 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
926 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
929 if (state->towrite < 0)
931 state->towrite = size;
934 else if (state->towrite != size)
936 h->cerrno = CSWRONGBUF;
939 while (state->towrite > state->written)
942 send(h->iofile, buf + state->written, size -
953 WSAGetLastError() == WSAEWOULDBLOCK
955 yaz_errno() == EWOULDBLOCK
957 #if EAGAIN != EWOULDBLOCK
958 || yaz_errno() == EAGAIN
962 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
964 || yaz_errno() == EINPROGRESS
968 TRC(fprintf(stderr, " Flow control stop\n"));
969 h->io_pending = CS_WANT_WRITE;
975 state->written += res;
976 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
977 res, state->written, size));
979 state->towrite = state->written = -1;
980 TRC(fprintf(stderr, " Ok\n"));
985 #if HAVE_OPENSSL_SSL_H
988 * In nonblocking mode, you must call again with same buffer while
991 int ssl_put(COMSTACK h, char *buf, int size)
994 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
996 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
999 if (state->towrite < 0)
1001 state->towrite = size;
1004 else if (state->towrite != size)
1006 h->cerrno = CSWRONGBUF;
1009 while (state->towrite > state->written)
1011 res = SSL_write (state->ssl, buf + state->written,
1012 size - state->written);
1015 int ssl_err = SSL_get_error(state->ssl, res);
1016 if (ssl_err == SSL_ERROR_WANT_READ)
1018 h->io_pending = CS_WANT_READ;
1021 if (ssl_err == SSL_ERROR_WANT_WRITE)
1023 h->io_pending = CS_WANT_WRITE;
1026 h->cerrno = CSERRORSSL;
1029 state->written += res;
1030 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1031 res, state->written, size));
1033 state->towrite = state->written = -1;
1034 TRC(fprintf(stderr, " Ok\n"));
1039 int tcpip_close(COMSTACK h)
1041 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1043 TRC(fprintf(stderr, "tcpip_close\n"));
1044 if (h->iofile != -1)
1046 #if HAVE_OPENSSL_SSL_H
1049 SSL_shutdown (sp->ssl);
1053 closesocket(h->iofile);
1060 #if HAVE_OPENSSL_SSL_H
1063 TRC (fprintf(stderr, "SSL_free\n"));
1068 SSL_CTX_free (sp->ctx_alloc);
1075 char *tcpip_addrstr(COMSTACK h)
1077 struct sockaddr_in addr;
1078 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1079 char *r = 0, *buf = sp->buf;
1081 struct hostent *host;
1084 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1086 h->cerrno = CSYSERR;
1089 if (!(h->blocking&2)) {
1090 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1092 r = (char*) host->h_name;
1095 r = inet_ntoa(addr.sin_addr);
1096 if (h->protocol == PROTO_HTTP)
1097 sprintf(buf, "http:%s", r);
1099 sprintf(buf, "tcp:%s", r);
1100 #if HAVE_OPENSSL_SSL_H
1103 if (h->protocol == PROTO_HTTP)
1104 sprintf(buf, "https:%s", r);
1106 sprintf(buf, "ssl:%s", r);
1112 int static tcpip_set_blocking(COMSTACK p, int blocking)
1116 if (p->blocking == blocking)
1120 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1123 flag = fcntl(p->iofile, F_GETFL, 0);
1125 flag = flag & ~O_NONBLOCK;
1127 flag = flag | O_NONBLOCK;
1128 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1131 p->blocking = blocking;
1135 #if HAVE_OPENSSL_SSL_H
1136 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1138 struct tcpip_state *sp;
1139 if (!cs || cs->type != ssl_type)
1141 sp = (struct tcpip_state *) cs->cprivate;
1144 sp->ctx = (SSL_CTX *) ctx;
1148 void *cs_get_ssl(COMSTACK cs)
1150 struct tcpip_state *sp;
1151 if (!cs || cs->type != ssl_type)
1153 sp = (struct tcpip_state *) cs->cprivate;
1157 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1159 struct tcpip_state *sp;
1160 if (!cs || cs->type != ssl_type)
1162 sp = (struct tcpip_state *) cs->cprivate;
1163 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1164 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1168 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1170 SSL *ssl = (SSL *) cs_get_ssl(cs);
1173 X509 *server_cert = SSL_get_peer_certificate (ssl);
1176 BIO *bio = BIO_new(BIO_s_mem());
1178 /* get PEM buffer in memory */
1179 PEM_write_bio_X509(bio, server_cert);
1180 *len = BIO_get_mem_data(bio, &pem_buf);
1181 *buf = (char *) xmalloc(*len);
1182 memcpy(*buf, pem_buf, *len);
1190 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1195 void *cs_get_ssl(COMSTACK cs)
1200 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1205 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1214 * indent-tabs-mode: nil
1216 * vim: shiftwidth=4 tabstop=8 expandtab