2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tcpip.c,v 1.24 2006-08-30 19:26:43 adam Exp $
9 * \brief Implements TCP/IP + SSL COMSTACK.
19 #include <sys/types.h>
31 #define HAVE_GETADDRINFO 1
33 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <netinet/tcp.h>
40 #include <sys/socket.h>
43 #include <sys/select.h>
49 #if HAVE_OPENSSL_SSL_H
50 #include <openssl/ssl.h>
51 #include <openssl/err.h>
54 #include <yaz/comstack.h>
55 #include <yaz/tcpip.h>
58 static int tcpip_close(COMSTACK h);
59 static int tcpip_put(COMSTACK h, char *buf, int size);
60 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
61 static int tcpip_connect(COMSTACK h, void *address);
62 static int tcpip_more(COMSTACK h);
63 static int tcpip_rcvconnect(COMSTACK h);
64 static int tcpip_bind(COMSTACK h, void *address, int mode);
65 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
66 int (*check_ip)(void *cd, const char *a, int len, int type),
68 static int tcpip_set_blocking(COMSTACK p, int blocking);
70 #if HAVE_OPENSSL_SSL_H
71 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
72 static int ssl_put(COMSTACK h, char *buf, int size);
75 static COMSTACK tcpip_accept(COMSTACK h);
76 static char *tcpip_addrstr(COMSTACK h);
77 static void *tcpip_straddr(COMSTACK h, const char *str);
86 #define YAZ_SOCKLEN_T int
89 /* this state is used for both SSL and straight TCP/IP */
90 typedef struct tcpip_state
92 char *altbuf; /* alternate buffer for surplus data */
93 int altsize; /* size as xmalloced */
94 int altlen; /* length of data or 0 if none */
96 int written; /* -1 if we aren't writing */
97 int towrite; /* to verify against user input */
98 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
100 struct addrinfo *res;
102 struct sockaddr_in addr; /* returned by cs_straddr */
104 char buf[128]; /* returned by cs_addrstr */
105 #if HAVE_OPENSSL_SSL_H
106 SSL_CTX *ctx; /* current CTX. */
107 SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
109 char cert_fname[256];
114 static int tcpip_init (void)
116 static int initialized = 0;
122 requested = MAKEWORD(1, 1);
123 if (WSAStartup(requested, &wd))
130 static int tcpip_init (void)
137 * This function is always called through the cs_create() macro.
138 * s >= 0: socket has already been established for us.
140 COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
146 unsigned long tru = 1;
153 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
159 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
161 if (!(sp = (struct tcpip_state *)(p->cprivate =
162 xmalloc(sizeof(tcpip_state)))))
165 if (!((p->blocking = blocking)&1))
168 if (ioctlsocket(s, FIONBIO, &tru) < 0)
171 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
174 signal (SIGPIPE, SIG_IGN);
181 p->type = tcpip_type;
182 p->protocol = (enum oid_proto) protocol;
184 p->f_connect = tcpip_connect;
185 p->f_rcvconnect = tcpip_rcvconnect;
186 p->f_get = tcpip_get;
187 p->f_put = tcpip_put;
188 p->f_close = tcpip_close;
189 p->f_more = tcpip_more;
190 p->f_bind = tcpip_bind;
191 p->f_listen = tcpip_listen;
192 p->f_accept = tcpip_accept;
193 p->f_addrstr = tcpip_addrstr;
194 p->f_straddr = tcpip_straddr;
195 p->f_set_blocking = tcpip_set_blocking;
196 p->max_recv_bytes = 5000000;
198 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
204 #if HAVE_OPENSSL_SSL_H
205 sp->ctx = sp->ctx_alloc = 0;
207 strcpy(sp->cert_fname, "yaz.pem");
214 sp->altsize = sp->altlen = 0;
215 sp->towrite = sp->written = -1;
216 if (protocol == PROTO_WAIS)
217 sp->complete = completeWAIS;
219 sp->complete = cs_complete_auto;
221 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
222 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
227 #if HAVE_OPENSSL_SSL_H
229 COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
234 p = tcpip_type (s, blocking, protocol, 0);
240 sp = (tcpip_state *) p->cprivate;
242 sp->ctx = (SSL_CTX *) vp; /* may be NULL */
244 /* note: we don't handle already opened socket in SSL mode - yet */
250 /* resolve using getaddrinfo */
251 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
253 struct addrinfo hints, *res;
258 hints.ai_family = AF_UNSPEC;
259 hints.ai_socktype = SOCK_STREAM;
260 hints.ai_protocol = 0;
261 hints.ai_addrlen = 0;
262 hints.ai_addr = NULL;
263 hints.ai_canonname = NULL;
264 hints.ai_next = NULL;
266 strncpy(host, str, sizeof(host)-1);
267 host[sizeof(host)-1] = 0;
268 if ((p = strchr(host, '/')))
270 if ((p = strchr(host, ':')))
276 if (!strcmp("@", host))
278 hints.ai_flags = AI_PASSIVE;
279 error = getaddrinfo(0, port, &hints, &res);
283 error = getaddrinfo(host, port, &hints, &res);
291 /* gethostbyname .. old systems */
292 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
297 short int port = default_port;
299 unsigned long tmpadd;
303 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
304 add->sin_family = AF_INET;
305 strncpy(buf, str, sizeof(buf)-1);
306 buf[sizeof(buf)-1] = 0;
307 if ((p = strchr(buf, '/')))
309 if ((p = strchr(buf, ':')))
314 add->sin_port = htons(port);
315 if (!strcmp("@", buf))
317 add->sin_addr.s_addr = INADDR_ANY;
319 else if ((tmpadd = inet_addr(buf)) != -1)
321 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
323 else if ((hp = gethostbyname(buf)))
325 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
326 sizeof(struct in_addr));
335 void *tcpip_straddr(COMSTACK h, const char *str)
337 tcpip_state *sp = (tcpip_state *)h->cprivate;
338 const char *port = "210";
339 if (h->protocol == PROTO_HTTP)
345 freeaddrinfo(sp->res);
346 sp->res = tcpip_getaddrinfo(str, port);
350 void *tcpip_straddr(COMSTACK h, const char *str)
352 tcpip_state *sp = (tcpip_state *)h->cprivate;
354 if (h->protocol == PROTO_HTTP)
359 if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
365 int tcpip_more(COMSTACK h)
367 tcpip_state *sp = (tcpip_state *)h->cprivate;
369 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
374 * connect(2) will block (sometimes) - nothing we can do short of doing
375 * weird things like spawning subprocesses or threading or some weird junk
378 int tcpip_connect(COMSTACK h, void *address)
381 struct addrinfo *ai = (struct addrinfo *) address;
383 struct sockaddr_in *add = (struct sockaddr_in *) address;
388 YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
390 TRC(fprintf(stderr, "tcpip_connect\n"));
392 if (h->state != CS_ST_UNBND)
394 h->cerrno = CSOUTSTATE;
398 /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
399 * This gives the connect a chance to negotiate with the other side
402 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
407 TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
409 recbuflen *= 10; /* lets be optimistic */
410 if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
415 if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
420 TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
425 r = connect(h->iofile, ai->ai_addr, ai->ai_addrlen);
427 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
432 if (WSAGetLastError() == WSAEWOULDBLOCK)
434 h->event = CS_CONNECT;
435 h->state = CS_ST_CONNECTING;
436 h->io_pending = CS_WANT_WRITE;
440 if (yaz_errno() == EINPROGRESS)
442 h->event = CS_CONNECT;
443 h->state = CS_ST_CONNECTING;
444 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
451 h->event = CS_CONNECT;
452 h->state = CS_ST_CONNECTING;
454 return tcpip_rcvconnect (h);
460 int tcpip_rcvconnect(COMSTACK h)
462 #if HAVE_OPENSSL_SSL_H
463 tcpip_state *sp = (tcpip_state *)h->cprivate;
465 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
467 if (h->state == CS_ST_DATAXFER)
469 if (h->state != CS_ST_CONNECTING)
471 h->cerrno = CSOUTSTATE;
474 #if HAVE_OPENSSL_SSL_H
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;
493 sp->ssl = SSL_new (sp->ctx);
494 SSL_set_fd (sp->ssl, h->iofile);
496 res = SSL_connect (sp->ssl);
499 int err = SSL_get_error(sp->ssl, res);
500 if (err == SSL_ERROR_WANT_READ)
502 h->io_pending = CS_WANT_READ;
505 if (err == SSL_ERROR_WANT_WRITE)
507 h->io_pending = CS_WANT_WRITE;
510 h->cerrno = CSERRORSSL;
516 h->state = CS_ST_DATAXFER;
520 #define CERTF "ztest.pem"
521 #define KEYF "ztest.pem"
523 static void tcpip_setsockopt (int fd)
529 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
531 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
533 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
535 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
537 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
539 yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
544 static int tcpip_bind(COMSTACK h, void *address, int mode)
548 struct addrinfo *ai = (struct addrinfo *)address;
550 struct sockaddr *addr = (struct sockaddr *)address;
555 unsigned long one = 1;
558 #if HAVE_OPENSSL_SSL_H
559 tcpip_state *sp = (tcpip_state *)h->cprivate;
560 if (h->type == ssl_type && !sp->ctx)
562 SSL_load_error_strings();
563 SSLeay_add_all_algorithms();
565 sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
568 h->cerrno = CSERRORSSL;
577 res = SSL_CTX_use_certificate_chain_file(sp->ctx, sp->cert_fname);
580 ERR_print_errors_fp(stderr);
583 res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
587 ERR_print_errors_fp(stderr);
590 res = SSL_CTX_check_private_key (sp->ctx);
593 ERR_print_errors_fp(stderr);
597 TRC (fprintf (stderr, "ssl_bind\n"));
601 TRC (fprintf (stderr, "tcpip_bind\n"));
604 TRC (fprintf (stderr, "tcpip_bind\n"));
607 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
608 &one, sizeof(one)) < 0)
614 tcpip_setsockopt(h->iofile);
616 r = bind(h->iofile, ai->ai_addr, ai->ai_addrlen);
618 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
625 /* Allow a maximum-sized backlog of waiting-to-connect clients */
626 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
631 h->state = CS_ST_IDLE;
632 h->event = CS_LISTEN;
636 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
637 int (*check_ip)(void *cd, const char *a, int len, int t),
640 struct sockaddr_in addr;
641 YAZ_SOCKLEN_T len = sizeof(addr);
643 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
644 if (h->state != CS_ST_IDLE)
646 h->cerrno = CSOUTSTATE;
649 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
654 WSAGetLastError() == WSAEWOULDBLOCK
656 yaz_errno() == EWOULDBLOCK
658 #if EAGAIN != EWOULDBLOCK
659 || yaz_errno() == EAGAIN
664 h->cerrno = CSNODATA;
669 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
670 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
673 if (check_ip && (*check_ip)(cd, (const char *) &addr,
674 sizeof(addr), AF_INET))
678 closesocket(h->newfd);
685 h->state = CS_ST_INCON;
686 tcpip_setsockopt (h->newfd);
690 COMSTACK tcpip_accept(COMSTACK h)
693 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
695 unsigned long tru = 1;
698 TRC(fprintf(stderr, "tcpip_accept\n"));
699 if (h->state == CS_ST_INCON)
701 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
705 closesocket(h->newfd);
712 memcpy(cnew, h, sizeof(*h));
713 cnew->iofile = h->newfd;
714 cnew->io_pending = 0;
715 if (!(state = (tcpip_state *)
716 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
722 closesocket(h->newfd);
730 if (!(cnew->blocking&1) &&
732 (ioctlsocket(cnew->iofile, FIONBIO, &tru) < 0)
734 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
742 closesocket(h->newfd);
754 state->altsize = state->altlen = 0;
755 state->towrite = state->written = -1;
756 state->complete = st->complete;
757 cnew->state = CS_ST_ACCEPT;
758 h->state = CS_ST_IDLE;
760 #if HAVE_OPENSSL_SSL_H
761 state->ctx = st->ctx;
762 state->ctx_alloc = 0;
763 state->ssl = st->ssl;
766 state->ssl = SSL_new (state->ctx);
767 SSL_set_fd (state->ssl, cnew->iofile);
772 if (h->state == CS_ST_ACCEPT)
774 #if HAVE_OPENSSL_SSL_H
775 tcpip_state *state = (tcpip_state *)h->cprivate;
778 int res = SSL_accept (state->ssl);
779 TRC(fprintf(stderr, "SSL_accept\n"));
782 int err = SSL_get_error(state->ssl, res);
783 if (err == SSL_ERROR_WANT_READ)
785 h->io_pending = CS_WANT_READ;
788 if (err == SSL_ERROR_WANT_WRITE)
790 h->io_pending = CS_WANT_WRITE;
801 h->cerrno = CSOUTSTATE;
805 h->state = CS_ST_DATAXFER;
810 #define CS_TCPIP_BUFCHUNK 4096
813 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
814 * 0=connection closed.
816 int tcpip_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, "tcpip_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)))
848 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
849 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
856 /* unfortunatly, sun sometimes forgets to set errno in recv
857 when EWOULDBLOCK etc. would be required (res = -1) */
859 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
860 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
863 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
864 strerror(yaz_errno())));
866 if (WSAGetLastError() == WSAEWOULDBLOCK)
868 h->io_pending = CS_WANT_READ;
874 if (yaz_errno() == EWOULDBLOCK
876 #if EAGAIN != EWOULDBLOCK
877 || yaz_errno() == EAGAIN
880 || yaz_errno() == EINPROGRESS
882 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
886 h->io_pending = CS_WANT_READ;
889 else if (yaz_errno() == 0)
898 if (hasread > h->max_recv_bytes)
900 h->cerrno = CSBUFSIZE;
904 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
906 /* move surplus buffer (or everything if we didn't get a BER rec.) */
907 if (hasread > berlen)
909 tomove = req = hasread - berlen;
910 rest = tomove % CS_TCPIP_BUFCHUNK;
912 req += CS_TCPIP_BUFCHUNK - rest;
915 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
917 } else if (sp->altsize < req)
918 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
920 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
921 (unsigned) sp->altbuf));
922 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
924 if (berlen < CS_TCPIP_BUFCHUNK - 1)
925 *(*buf + berlen) = '\0';
926 return berlen ? berlen : 1;
930 #if HAVE_OPENSSL_SSL_H
932 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
933 * 0=connection closed.
935 int ssl_get(COMSTACK h, char **buf, int *bufsize)
937 tcpip_state *sp = (tcpip_state *)h->cprivate;
939 int tmpi, berlen, rest, req, tomove;
940 int hasread = 0, res;
942 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
943 if (sp->altlen) /* switch buffers */
945 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
946 (unsigned) sp->altbuf));
950 *bufsize = sp->altsize;
951 hasread = sp->altlen;
957 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
961 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
964 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
965 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
967 res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
968 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
971 int ssl_err = SSL_get_error(sp->ssl, res);
972 if (ssl_err == SSL_ERROR_WANT_READ)
974 h->io_pending = CS_WANT_READ;
977 if (ssl_err == SSL_ERROR_WANT_WRITE)
979 h->io_pending = CS_WANT_WRITE;
984 h->cerrno = CSERRORSSL;
989 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
991 /* move surplus buffer (or everything if we didn't get a BER rec.) */
992 if (hasread > berlen)
994 tomove = req = hasread - berlen;
995 rest = tomove % CS_TCPIP_BUFCHUNK;
997 req += CS_TCPIP_BUFCHUNK - rest;
1000 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1002 } else if (sp->altsize < req)
1003 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1005 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
1006 (unsigned) sp->altbuf));
1007 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1009 if (berlen < CS_TCPIP_BUFCHUNK - 1)
1010 *(*buf + berlen) = '\0';
1011 return berlen ? berlen : 1;
1016 * Returns 1, 0 or -1
1017 * In nonblocking mode, you must call again with same buffer while
1018 * return value is 1.
1020 int tcpip_put(COMSTACK h, char *buf, int size)
1023 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1025 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
1028 if (state->towrite < 0)
1030 state->towrite = size;
1033 else if (state->towrite != size)
1035 h->cerrno = CSWRONGBUF;
1038 while (state->towrite > state->written)
1041 send(h->iofile, buf + state->written, size -
1052 WSAGetLastError() == WSAEWOULDBLOCK
1054 yaz_errno() == EWOULDBLOCK
1056 #if EAGAIN != EWOULDBLOCK
1057 || yaz_errno() == EAGAIN
1061 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1063 || yaz_errno() == EINPROGRESS
1067 TRC(fprintf(stderr, " Flow control stop\n"));
1068 h->io_pending = CS_WANT_WRITE;
1071 h->cerrno = CSYSERR;
1074 state->written += res;
1075 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1076 res, state->written, size));
1078 state->towrite = state->written = -1;
1079 TRC(fprintf(stderr, " Ok\n"));
1084 #if HAVE_OPENSSL_SSL_H
1086 * Returns 1, 0 or -1
1087 * In nonblocking mode, you must call again with same buffer while
1088 * return value is 1.
1090 int ssl_put(COMSTACK h, char *buf, int size)
1093 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1095 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1098 if (state->towrite < 0)
1100 state->towrite = size;
1103 else if (state->towrite != size)
1105 h->cerrno = CSWRONGBUF;
1108 while (state->towrite > state->written)
1110 res = SSL_write (state->ssl, buf + state->written,
1111 size - state->written);
1114 int ssl_err = SSL_get_error(state->ssl, res);
1115 if (ssl_err == SSL_ERROR_WANT_READ)
1117 h->io_pending = CS_WANT_READ;
1120 if (ssl_err == SSL_ERROR_WANT_WRITE)
1122 h->io_pending = CS_WANT_WRITE;
1125 h->cerrno = CSERRORSSL;
1128 state->written += res;
1129 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
1130 res, state->written, size));
1132 state->towrite = state->written = -1;
1133 TRC(fprintf(stderr, " Ok\n"));
1138 int tcpip_close(COMSTACK h)
1140 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1142 TRC(fprintf(stderr, "tcpip_close\n"));
1143 if (h->iofile != -1)
1145 #if HAVE_OPENSSL_SSL_H
1148 SSL_shutdown (sp->ssl);
1152 closesocket(h->iofile);
1159 #if HAVE_OPENSSL_SSL_H
1162 TRC (fprintf(stderr, "SSL_free\n"));
1167 SSL_CTX_free (sp->ctx_alloc);
1169 #if HAVE_GETADDRINFO
1171 freeaddrinfo(sp->res);
1178 char *tcpip_addrstr(COMSTACK h)
1180 struct sockaddr_in addr;
1181 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1182 char *r = 0, *buf = sp->buf;
1184 struct hostent *host;
1187 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1189 h->cerrno = CSYSERR;
1192 if (!(h->blocking&2)) {
1193 if ((host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr),
1195 r = (char*) host->h_name;
1198 r = inet_ntoa(addr.sin_addr);
1199 if (h->protocol == PROTO_HTTP)
1200 sprintf(buf, "http:%s", r);
1202 sprintf(buf, "tcp:%s", r);
1203 #if HAVE_OPENSSL_SSL_H
1206 if (h->protocol == PROTO_HTTP)
1207 sprintf(buf, "https:%s", r);
1209 sprintf(buf, "ssl:%s", r);
1215 int static tcpip_set_blocking(COMSTACK p, int blocking)
1219 if (p->blocking == blocking)
1223 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1226 flag = fcntl(p->iofile, F_GETFL, 0);
1228 flag = flag & ~O_NONBLOCK;
1230 flag = flag | O_NONBLOCK;
1231 if (fcntl(p->iofile, F_SETFL, flag) < 0)
1234 p->blocking = blocking;
1238 #if HAVE_OPENSSL_SSL_H
1239 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1241 struct tcpip_state *sp;
1242 if (!cs || cs->type != ssl_type)
1244 sp = (struct tcpip_state *) cs->cprivate;
1247 sp->ctx = (SSL_CTX *) ctx;
1251 void *cs_get_ssl(COMSTACK cs)
1253 struct tcpip_state *sp;
1254 if (!cs || cs->type != ssl_type)
1256 sp = (struct tcpip_state *) cs->cprivate;
1260 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1262 struct tcpip_state *sp;
1263 if (!cs || cs->type != ssl_type)
1265 sp = (struct tcpip_state *) cs->cprivate;
1266 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1267 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1271 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1273 SSL *ssl = (SSL *) cs_get_ssl(cs);
1276 X509 *server_cert = SSL_get_peer_certificate (ssl);
1279 BIO *bio = BIO_new(BIO_s_mem());
1281 /* get PEM buffer in memory */
1282 PEM_write_bio_X509(bio, server_cert);
1283 *len = BIO_get_mem_data(bio, &pem_buf);
1284 *buf = (char *) xmalloc(*len);
1285 memcpy(*buf, pem_buf, *len);
1293 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1298 void *cs_get_ssl(COMSTACK cs)
1303 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1308 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1317 * indent-tabs-mode: nil
1319 * vim: shiftwidth=4 tabstop=8 expandtab