2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.22 2006-08-30 12:55:12 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;
190 p->max_recv_bytes = 5000000;
192 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
198 #if HAVE_OPENSSL_SSL_H
199 sp->ctx = sp->ctx_alloc = 0;
201 strcpy(sp->cert_fname, "yaz.pem");
205 sp->altsize = sp->altlen = 0;
206 sp->towrite = sp->written = -1;
207 if (protocol == PROTO_WAIS)
208 sp->complete = completeWAIS;
210 sp->complete = cs_complete_auto;
212 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
213 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
218 #if HAVE_OPENSSL_SSL_H
220 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
225 p = tcpip_type (s, blocking, protocol, 0);
231 sp = (tcpip_state *) p->cprivate;
233 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
235 /* note: we don't handle already opened socket in SSL mode - yet */
240 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
244 #if HAVE_GETHOSTBYNAME_R
250 short int port = default_port;
252 unsigned long tmpadd;
258 #define INADDR_NONE -1
263 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
264 add->sin_family = AF_INET;
265 strncpy(buf, str, 511);
267 if ((p = strchr(buf, '/')))
269 if ((p = strchr(buf, ':')))
274 add->sin_port = htons(port);
275 if (!strcmp("@", buf))
277 add->sin_addr.s_addr = INADDR_ANY;
279 else if ((tmpadd = inet_addr(buf)) != INADDR_NONE)
281 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
283 #if HAVE_GETHOSTBYNAME_R
284 else if (gethostbyname_r(buf, &h, hbuf, sizeof(hbuf), &hp, &h_error) == 0)
286 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
287 sizeof(struct in_addr));
290 else if ((hp = gethostbyname(buf)))
292 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
293 sizeof(struct in_addr));
301 void *tcpip_straddr(COMSTACK h, const char *str)
303 tcpip_state *sp = (tcpip_state *)h->cprivate;
306 if (h->protocol == PROTO_HTTP)
309 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
314 struct sockaddr_in *tcpip_strtoaddr(const char *str)
316 static struct sockaddr_in add;
318 if (!tcpip_strtoaddr_ex (str, &add, 210))
323 int tcpip_more(COMSTACK h)
325 tcpip_state *sp = (tcpip_state *)h->cprivate;
327 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
332 * connect(2) will block (sometimes) - nothing we can do short of doing
333 * weird things like spawning subprocesses or threading or some weird junk
336 int tcpip_connect(COMSTACK h, void *address)
338 struct sockaddr_in *add = (struct sockaddr_in *)address;
342 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
344 TRC(fprintf(stderr, "tcpip_connect\n"));
346 if (h->state != CS_ST_UNBND)
348 h->cerrno = CSOUTSTATE;
352 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
353 * This gives the connect a chance to negotiate with the other side
356 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
361 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
363 recbuflen *= 10; /* lets be optimistic */
364 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
369 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
374 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
377 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
381 if (WSAGetLastError() == WSAEWOULDBLOCK)
383 h->event = CS_CONNECT;
384 h->state = CS_ST_CONNECTING;
385 h->io_pending = CS_WANT_WRITE;
389 if (yaz_errno() == EINPROGRESS)
391 h->event = CS_CONNECT;
392 h->state = CS_ST_CONNECTING;
393 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
400 h->event = CS_CONNECT;
401 h->state = CS_ST_CONNECTING;
403 return tcpip_rcvconnect (h);
409 int tcpip_rcvconnect(COMSTACK h)
411 #if HAVE_OPENSSL_SSL_H
412 tcpip_state *sp = (tcpip_state *)h->cprivate;
414 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
416 if (h->state == CS_ST_DATAXFER)
418 if (h->state != CS_ST_CONNECTING)
420 h->cerrno = CSOUTSTATE;
423 #if HAVE_OPENSSL_SSL_H
424 if (h->type == ssl_type && !sp->ctx)
426 SSL_load_error_strings();
427 SSLeay_add_all_algorithms();
429 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
432 h->cerrno = CSERRORSSL;
442 sp->ssl = SSL_new (sp->ctx);
443 SSL_set_fd (sp->ssl, h->iofile);
445 res = SSL_connect (sp->ssl);
448 int err = SSL_get_error(sp->ssl, res);
449 if (err == SSL_ERROR_WANT_READ)
451 h->io_pending = CS_WANT_READ;
454 if (err == SSL_ERROR_WANT_WRITE)
456 h->io_pending = CS_WANT_WRITE;
459 h->cerrno = CSERRORSSL;
465 h->state = CS_ST_DATAXFER;
469 #define CERTF "ztest.pem"
470 #define KEYF "ztest.pem"
472 static void tcpip_setsockopt (int fd)
478 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
480 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
482 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
484 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
486 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
488 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
493 static int tcpip_bind(COMSTACK h, void *address, int mode)
495 struct sockaddr *addr = (struct sockaddr *)address;
499 unsigned long one = 1;
502 #if HAVE_OPENSSL_SSL_H
503 tcpip_state *sp = (tcpip_state *)h->cprivate;
504 if (h->type == ssl_type && !sp->ctx)
506 SSL_load_error_strings();
507 SSLeay_add_all_algorithms();
509 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
512 h->cerrno = CSERRORSSL;
521 res = SSL_CTX_use_certificate_chain_file(sp->ctx, sp->cert_fname);
524 ERR_print_errors_fp(stderr);
527 res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
531 ERR_print_errors_fp(stderr);
534 res = SSL_CTX_check_private_key (sp->ctx);
537 ERR_print_errors_fp(stderr);
541 TRC (fprintf (stderr, "ssl_bind\n"));
545 TRC (fprintf (stderr, "tcpip_bind\n"));
548 TRC (fprintf (stderr, "tcpip_bind\n"));
551 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
552 &one, sizeof(one)) < 0)
558 tcpip_setsockopt(h->iofile);
559 if (bind(h->iofile, addr, sizeof(struct sockaddr_in)))
564 /* Allow a maximum-sized backlog of waiting-to-connect clients */
565 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
570 h->state = CS_ST_IDLE;
571 h->event = CS_LISTEN;
575 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
576 int (*check_ip)(void *cd, const char *a, int len, int t),
579 struct sockaddr_in addr;
580 YAZ_SOCKLEN_T len = sizeof(addr);
582 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
583 if (h->state != CS_ST_IDLE)
585 h->cerrno = CSOUTSTATE;
588 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
593 WSAGetLastError() == WSAEWOULDBLOCK
595 yaz_errno() == EWOULDBLOCK
597 #if EAGAIN != EWOULDBLOCK
598 || yaz_errno() == EAGAIN
603 h->cerrno = CSNODATA;
608 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
609 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
612 if (check_ip && (*check_ip)(cd, (const char *) &addr,
613 sizeof(addr), AF_INET))
617 closesocket(h->newfd);
624 h->state = CS_ST_INCON;
625 tcpip_setsockopt (h->newfd);
629 COMSTACK tcpip_accept(COMSTACK h)
632 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
634 unsigned long tru = 1;
637 TRC(fprintf(stderr, "tcpip_accept\n"));
638 if (h->state == CS_ST_INCON)
640 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
644 closesocket(h->newfd);
651 memcpy(cnew, h, sizeof(*h));
652 cnew->iofile = h->newfd;
653 cnew->io_pending = 0;
654 if (!(state = (tcpip_state *)
655 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
661 closesocket(h->newfd);
669 if (!(cnew->blocking&1) &&
671 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
673 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
681 closesocket(h->newfd);
693 state->altsize = state->altlen = 0;
694 state->towrite = state->written = -1;
695 state->complete = st->complete;
696 cnew->state = CS_ST_ACCEPT;
697 h->state = CS_ST_IDLE;
699 #if HAVE_OPENSSL_SSL_H
700 state->ctx = st->ctx;
701 state->ctx_alloc = 0;
702 state->ssl = st->ssl;
705 state->ssl = SSL_new (state->ctx);
706 SSL_set_fd (state->ssl, cnew->iofile);
711 if (h->state == CS_ST_ACCEPT)
713 #if HAVE_OPENSSL_SSL_H
714 tcpip_state *state = (tcpip_state *)h->cprivate;
717 int res = SSL_accept (state->ssl);
718 TRC(fprintf(stderr, "SSL_accept\n"));
721 int err = SSL_get_error(state->ssl, res);
722 if (err == SSL_ERROR_WANT_READ)
724 h->io_pending = CS_WANT_READ;
727 if (err == SSL_ERROR_WANT_WRITE)
729 h->io_pending = CS_WANT_WRITE;
740 h->cerrno = CSOUTSTATE;
744 h->state = CS_ST_DATAXFER;
749 #define CS_TCPIP_BUFCHUNK 4096
752 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
753 * 0=connection closed.
755 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
757 tcpip_state *sp = (tcpip_state *)h->cprivate;
759 int tmpi, berlen, rest, req, tomove;
760 int hasread = 0, res;
762 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
763 if (sp->altlen) /* switch buffers */
765 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
766 (unsigned) sp->altbuf));
770 *bufsize = sp->altsize;
771 hasread = sp->altlen;
777 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
781 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
787 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
788 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
795 /* unfortunatly, sun sometimes forgets to set errno in recv
796 when EWOULDBLOCK etc. would be required (res = -1) */
798 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
799 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
802 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
803 strerror(yaz_errno())));
805 if (WSAGetLastError() == WSAEWOULDBLOCK)
807 h->io_pending = CS_WANT_READ;
813 if (yaz_errno() == EWOULDBLOCK
815 #if EAGAIN != EWOULDBLOCK
816 || yaz_errno() == EAGAIN
819 || yaz_errno() == EINPROGRESS
821 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
825 h->io_pending = CS_WANT_READ;
828 else if (yaz_errno() == 0)
837 if (hasread > h->max_recv_bytes)
839 h->cerrno = CSBUFSIZE;
843 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
845 /* move surplus buffer (or everything if we didn't get a BER rec.) */
846 if (hasread > berlen)
848 tomove = req = hasread - berlen;
849 rest = tomove % CS_TCPIP_BUFCHUNK;
851 req += CS_TCPIP_BUFCHUNK - rest;
854 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
856 } else if (sp->altsize < req)
857 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
859 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
860 (unsigned) sp->altbuf));
861 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
863 if (berlen < CS_TCPIP_BUFCHUNK - 1)
864 *(*buf + berlen) = '\0';
865 return berlen ? berlen : 1;
869 #if HAVE_OPENSSL_SSL_H
871 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
872 * 0=connection closed.
874 int ssl_get(COMSTACK h, char **buf, int *bufsize)
876 tcpip_state *sp = (tcpip_state *)h->cprivate;
878 int tmpi, berlen, rest, req, tomove;
879 int hasread = 0, res;
881 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
882 if (sp->altlen) /* switch buffers */
884 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
885 (unsigned) sp->altbuf));
889 *bufsize = sp->altsize;
890 hasread = sp->altlen;
896 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
900 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
903 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
904 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
906 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
907 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
910 int ssl_err = SSL_get_error(sp->ssl, res);
911 if (ssl_err == SSL_ERROR_WANT_READ)
913 h->io_pending = CS_WANT_READ;
916 if (ssl_err == SSL_ERROR_WANT_WRITE)
918 h->io_pending = CS_WANT_WRITE;
923 h->cerrno = CSERRORSSL;
928 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
930 /* move surplus buffer (or everything if we didn't get a BER rec.) */
931 if (hasread > berlen)
933 tomove = req = hasread - berlen;
934 rest = tomove % CS_TCPIP_BUFCHUNK;
936 req += CS_TCPIP_BUFCHUNK - rest;
939 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
941 } else if (sp->altsize < req)
942 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
944 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
945 (unsigned) sp->altbuf));
946 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
948 if (berlen < CS_TCPIP_BUFCHUNK - 1)
949 *(*buf + berlen) = '\0';
950 return berlen ? berlen : 1;
956 * In nonblocking mode, you must call again with same buffer while
959 int tcpip_put(COMSTACK h, char *buf, int size)
962 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
964 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
967 if (state->towrite < 0)
969 state->towrite = size;
972 else if (state->towrite != size)
974 h->cerrno = CSWRONGBUF;
977 while (state->towrite > state->written)
980 send(h->iofile, buf + state->written, size -
991 WSAGetLastError() == WSAEWOULDBLOCK
993 yaz_errno() == EWOULDBLOCK
995 #if EAGAIN != EWOULDBLOCK
996 || yaz_errno() == EAGAIN
1000 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1002 || yaz_errno() == EINPROGRESS
1006 TRC(fprintf(stderr, " Flow control stop\n"));
1007 h->io_pending = CS_WANT_WRITE;
1010 h->cerrno = CSYSERR;
1013 state->written += res;
1014 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1015 res, state->written, size));
1017 state->towrite = state->written = -1;
1018 TRC(fprintf(stderr, " Ok\n"));
1023 #if HAVE_OPENSSL_SSL_H
1025 * Returns 1, 0 or -1
1026 * In nonblocking mode, you must call again with same buffer while
1027 * return value is 1.
1029 int ssl_put(COMSTACK h, char *buf, int size)
1032 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1034 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1037 if (state->towrite < 0)
1039 state->towrite = size;
1042 else if (state->towrite != size)
1044 h->cerrno = CSWRONGBUF;
1047 while (state->towrite > state->written)
1049 res = SSL_write (state->ssl, buf + state->written,
1050 size - state->written);
1053 int ssl_err = SSL_get_error(state->ssl, res);
1054 if (ssl_err == SSL_ERROR_WANT_READ)
1056 h->io_pending = CS_WANT_READ;
1059 if (ssl_err == SSL_ERROR_WANT_WRITE)
1061 h->io_pending = CS_WANT_WRITE;
1064 h->cerrno = CSERRORSSL;
1067 state->written += res;
1068 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1069 res, state->written, size));
1071 state->towrite = state->written = -1;
1072 TRC(fprintf(stderr, " Ok\n"));
1077 int tcpip_close(COMSTACK h)
1079 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1081 TRC(fprintf(stderr, "tcpip_close\n"));
1082 if (h->iofile != -1)
1084 #if HAVE_OPENSSL_SSL_H
1087 SSL_shutdown (sp->ssl);
1091 closesocket(h->iofile);
1098 #if HAVE_OPENSSL_SSL_H
1101 TRC (fprintf(stderr, "SSL_free\n"));
1106 SSL_CTX_free (sp->ctx_alloc);
1113 char *tcpip_addrstr(COMSTACK h)
1115 struct sockaddr_in addr;
1116 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1117 char *r = 0, *buf = sp->buf;
1119 struct hostent *host;
1122 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1124 h->cerrno = CSYSERR;
1127 if (!(h->blocking&2)) {
1128 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1130 r = (char*) host->h_name;
1133 r = inet_ntoa(addr.sin_addr);
1134 if (h->protocol == PROTO_HTTP)
1135 sprintf(buf, "http:%s", r);
1137 sprintf(buf, "tcp:%s", r);
1138 #if HAVE_OPENSSL_SSL_H
1141 if (h->protocol == PROTO_HTTP)
1142 sprintf(buf, "https:%s", r);
1144 sprintf(buf, "ssl:%s", r);
1150 int static tcpip_set_blocking(COMSTACK p, int blocking)
1154 if (p->blocking == blocking)
1158 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1161 flag = fcntl(p->iofile, F_GETFL, 0);
1163 flag = flag & ~O_NONBLOCK;
1165 flag = flag | O_NONBLOCK;
1166 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1169 p->blocking = blocking;
1173 #if HAVE_OPENSSL_SSL_H
1174 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1176 struct tcpip_state *sp;
1177 if (!cs || cs->type != ssl_type)
1179 sp = (struct tcpip_state *) cs->cprivate;
1182 sp->ctx = (SSL_CTX *) ctx;
1186 void *cs_get_ssl(COMSTACK cs)
1188 struct tcpip_state *sp;
1189 if (!cs || cs->type != ssl_type)
1191 sp = (struct tcpip_state *) cs->cprivate;
1195 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1197 struct tcpip_state *sp;
1198 if (!cs || cs->type != ssl_type)
1200 sp = (struct tcpip_state *) cs->cprivate;
1201 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1202 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1206 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1208 SSL *ssl = (SSL *) cs_get_ssl(cs);
1211 X509 *server_cert = SSL_get_peer_certificate (ssl);
1214 BIO *bio = BIO_new(BIO_s_mem());
1216 /* get PEM buffer in memory */
1217 PEM_write_bio_X509(bio, server_cert);
1218 *len = BIO_get_mem_data(bio, &pem_buf);
1219 *buf = (char *) xmalloc(*len);
1220 memcpy(*buf, pem_buf, *len);
1228 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1233 void *cs_get_ssl(COMSTACK cs)
1238 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1243 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1252 * indent-tabs-mode: nil
1254 * vim: shiftwidth=4 tabstop=8 expandtab