00001
00002
00003
00004
00010 #include <stdio.h>
00011 #include <string.h>
00012 #include <assert.h>
00013 #include <stdlib.h>
00014 #include <errno.h>
00015 #include <fcntl.h>
00016 #include <signal.h>
00017 #if HAVE_SYS_TYPES_H
00018 #include <sys/types.h>
00019 #endif
00020 #if HAVE_SYS_TIME_H
00021 #include <sys/time.h>
00022 #endif
00023 #if HAVE_UNISTD_H
00024 #include <unistd.h>
00025 #endif
00026
00027 #ifdef WIN32
00028
00029 #include <winsock2.h>
00030 #if _MSC_VER >= 1300
00031 #include <ws2tcpip.h>
00032 #define HAVE_GETADDRINFO 1
00033 #else
00034 #define HAVE_GETADDRINFO 0
00035 #endif
00036 #endif
00037
00038 #if HAVE_NETINET_IN_H
00039 #include <netinet/in.h>
00040 #endif
00041 #if HAVE_NETDB_H
00042 #include <netdb.h>
00043 #endif
00044 #if HAVE_ARPA_INET_H
00045 #include <arpa/inet.h>
00046 #endif
00047 #if HAVE_NETINET_TCP_H
00048 #include <netinet/tcp.h>
00049 #endif
00050 #if HAVE_SYS_SOCKET_H
00051 #include <sys/socket.h>
00052 #endif
00053 #if HAVE_SYS_WAIT_H
00054 #include <sys/wait.h>
00055 #endif
00056
00057 #if HAVE_GNUTLS_H
00058 #include <gnutls/x509.h>
00059 #include <gnutls/gnutls.h>
00060 #define ENABLE_SSL 1
00061 #endif
00062
00063 #if HAVE_OPENSSL_SSL_H
00064 #include <openssl/ssl.h>
00065 #include <openssl/err.h>
00066 #define ENABLE_SSL 1
00067 #endif
00068
00069 #include <yaz/comstack.h>
00070 #include <yaz/tcpip.h>
00071 #include <yaz/errno.h>
00072
00073 static void tcpip_close(COMSTACK h);
00074 static int tcpip_put(COMSTACK h, char *buf, int size);
00075 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
00076 static int tcpip_put_connect(COMSTACK h, char *buf, int size);
00077 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize);
00078 static int tcpip_connect(COMSTACK h, void *address);
00079 static int tcpip_more(COMSTACK h);
00080 static int tcpip_rcvconnect(COMSTACK h);
00081 static int tcpip_bind(COMSTACK h, void *address, int mode);
00082 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
00083 int (*check_ip)(void *cd, const char *a, int len, int type),
00084 void *cd);
00085 static int tcpip_set_blocking(COMSTACK p, int blocking);
00086
00087 #if ENABLE_SSL
00088 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
00089 static int ssl_put(COMSTACK h, char *buf, int size);
00090 #endif
00091
00092 static COMSTACK tcpip_accept(COMSTACK h);
00093 static const char *tcpip_addrstr(COMSTACK h);
00094 static void *tcpip_straddr(COMSTACK h, const char *str);
00095
00096 #if 0
00097 #define TRC(x) x
00098 #else
00099 #define TRC(X)
00100 #endif
00101
00102 #ifndef YAZ_SOCKLEN_T
00103 #define YAZ_SOCKLEN_T int
00104 #endif
00105
00106 #if HAVE_GNUTLS_H
00107 struct tcpip_cred_ptr {
00108 gnutls_certificate_credentials_t xcred;
00109 int ref;
00110 };
00111
00112 #endif
00113
00114 typedef struct tcpip_state
00115 {
00116 char *altbuf;
00117 int altsize;
00118 int altlen;
00119
00120 int written;
00121 int towrite;
00122 int (*complete)(const char *buf, int len);
00123 #if HAVE_GETADDRINFO
00124 struct addrinfo *ai;
00125 #else
00126 struct sockaddr_in addr;
00127 #endif
00128 char buf[128];
00129 #if HAVE_GNUTLS_H
00130 struct tcpip_cred_ptr *cred_ptr;
00131 gnutls_session_t session;
00132 char cert_fname[256];
00133 #elif HAVE_OPENSSL_SSL_H
00134 SSL_CTX *ctx;
00135 SSL_CTX *ctx_alloc;
00136 SSL *ssl;
00137 char cert_fname[256];
00138 #endif
00139 char *connect_request_buf;
00140 int connect_request_len;
00141 char *connect_response_buf;
00142 int connect_response_len;
00143 } tcpip_state;
00144
00145 #ifdef WIN32
00146 static int tcpip_init(void)
00147 {
00148 static int initialized = 0;
00149 if (!initialized)
00150 {
00151 WORD requested;
00152 WSADATA wd;
00153
00154 requested = MAKEWORD(1, 1);
00155 if (WSAStartup(requested, &wd))
00156 return 0;
00157 initialized = 1;
00158 }
00159 return 1;
00160 }
00161 #else
00162 static int tcpip_init(void)
00163 {
00164 return 1;
00165 }
00166 #endif
00167
00168
00169
00170
00171
00172 COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
00173 {
00174 COMSTACK p;
00175 tcpip_state *sp;
00176
00177 if (!tcpip_init())
00178 return 0;
00179 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
00180 return 0;
00181 if (!(sp = (struct tcpip_state *)(p->cprivate =
00182 xmalloc(sizeof(tcpip_state)))))
00183 return 0;
00184
00185 p->flags = flags;
00186
00187 p->io_pending = 0;
00188 p->iofile = s;
00189 p->type = tcpip_type;
00190 p->protocol = (enum oid_proto) protocol;
00191
00192 p->f_connect = tcpip_connect;
00193 p->f_rcvconnect = tcpip_rcvconnect;
00194 p->f_get = tcpip_get;
00195 p->f_put = tcpip_put;
00196 p->f_close = tcpip_close;
00197 p->f_more = tcpip_more;
00198 p->f_bind = tcpip_bind;
00199 p->f_listen = tcpip_listen;
00200 p->f_accept = tcpip_accept;
00201 p->f_addrstr = tcpip_addrstr;
00202 p->f_straddr = tcpip_straddr;
00203 p->f_set_blocking = tcpip_set_blocking;
00204 p->max_recv_bytes = 5000000;
00205
00206 p->state = s < 0 ? CS_ST_UNBND : CS_ST_IDLE;
00207 p->event = CS_NONE;
00208 p->cerrno = 0;
00209 p->user = 0;
00210
00211 #if HAVE_GNUTLS_H
00212 sp->cred_ptr = 0;
00213 sp->session = 0;
00214 strcpy(sp->cert_fname, "yaz.pem");
00215 #elif HAVE_OPENSSL_SSL_H
00216 sp->ctx = sp->ctx_alloc = 0;
00217 sp->ssl = 0;
00218 strcpy(sp->cert_fname, "yaz.pem");
00219 #endif
00220
00221 #if HAVE_GETADDRINFO
00222 sp->ai = 0;
00223 #endif
00224 sp->altbuf = 0;
00225 sp->altsize = sp->altlen = 0;
00226 sp->towrite = sp->written = -1;
00227 if (protocol == PROTO_WAIS)
00228 sp->complete = completeWAIS;
00229 else
00230 sp->complete = cs_complete_auto;
00231
00232 sp->connect_request_buf = 0;
00233 sp->connect_request_len = 0;
00234 sp->connect_response_buf = 0;
00235 sp->connect_response_len = 0;
00236
00237 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
00238 TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
00239
00240 return p;
00241 }
00242
00243 COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
00244 const char *connect_host)
00245 {
00246 COMSTACK p = tcpip_type(s, flags, protocol, 0);
00247 if (!p)
00248 return 0;
00249 if (connect_host)
00250 {
00251 tcpip_state *sp = (tcpip_state *) p->cprivate;
00252 sp->connect_request_buf = (char *) xmalloc(strlen(connect_host) + 30);
00253 sprintf(sp->connect_request_buf, "CONNECT %s HTTP/1.0\r\n\r\n",
00254 connect_host);
00255 sp->connect_request_len = strlen(sp->connect_request_buf);
00256 p->f_put = tcpip_put_connect;
00257 p->f_get = tcpip_get_connect;
00258 sp->complete = cs_complete_auto_head;
00259 }
00260 return p;
00261 }
00262
00263 #if HAVE_GNUTLS_H
00264 static void tcpip_create_cred(COMSTACK cs)
00265 {
00266 tcpip_state *sp = (tcpip_state *) cs->cprivate;
00267 sp->cred_ptr = (struct tcpip_cred_ptr *) xmalloc(sizeof(*sp->cred_ptr));
00268 sp->cred_ptr->ref = 1;
00269 gnutls_certificate_allocate_credentials(&sp->cred_ptr->xcred);
00270 }
00271
00272 #endif
00273
00274 COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
00275 {
00276 #if !ENABLE_SSL
00277 return 0;
00278 #else
00279 tcpip_state *sp;
00280 COMSTACK p;
00281
00282 p = tcpip_type(s, flags, protocol, 0);
00283 if (!p)
00284 return 0;
00285 p->f_get = ssl_get;
00286 p->f_put = ssl_put;
00287 p->type = ssl_type;
00288 sp = (tcpip_state *) p->cprivate;
00289
00290 #if HAVE_GNUTLS_H
00291 sp->session = (gnutls_session_t) vp;
00292 #elif HAVE_OPENSSL_SSL_H
00293 sp->ctx = (SSL_CTX *) vp;
00294 #endif
00295
00296 return p;
00297 #endif
00298 }
00299
00300 #if ENABLE_SSL
00301 static int ssl_check_error(COMSTACK h, tcpip_state *sp, int res)
00302 {
00303 #if HAVE_OPENSSL_SSL_H
00304 int err = SSL_get_error(sp->ssl, res);
00305 TRC(fprintf(stderr, "got err=%d\n", err));
00306 if (err == SSL_ERROR_WANT_READ)
00307 {
00308 TRC(fprintf(stderr, " -> SSL_ERROR_WANT_READ\n"));
00309 h->io_pending = CS_WANT_READ;
00310 return 1;
00311 }
00312 if (err == SSL_ERROR_WANT_WRITE)
00313 {
00314 TRC(fprintf(stderr, " -> SSL_ERROR_WANT_WRITE\n"));
00315 h->io_pending = CS_WANT_WRITE;
00316 return 1;
00317 }
00318 #elif HAVE_GNUTLS_H
00319 TRC(fprintf(stderr, "ssl_check_error error=%d fatal=%d msg=%s\n",
00320 res,
00321 gnutls_error_is_fatal(res),
00322 gnutls_strerror(res)));
00323 if (res == GNUTLS_E_AGAIN || res == GNUTLS_E_INTERRUPTED)
00324 {
00325 int dir = gnutls_record_get_direction(sp->session);
00326 TRC(fprintf(stderr, " -> incomplete dir=%d\n", dir));
00327 h->io_pending = dir ? CS_WANT_WRITE : CS_WANT_READ;
00328 return 1;
00329 }
00330 #endif
00331 h->cerrno = CSERRORSSL;
00332 return 0;
00333 }
00334 #endif
00335
00336 #if HAVE_GETADDRINFO
00337
00338 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
00339 {
00340 struct addrinfo hints, *res;
00341 int error;
00342 char host[512], *p;
00343
00344 hints.ai_flags = 0;
00345 hints.ai_family = AF_UNSPEC;
00346 hints.ai_socktype = SOCK_STREAM;
00347 hints.ai_protocol = 0;
00348 hints.ai_addrlen = 0;
00349 hints.ai_addr = NULL;
00350 hints.ai_canonname = NULL;
00351 hints.ai_next = NULL;
00352
00353 strncpy(host, str, sizeof(host)-1);
00354 host[sizeof(host)-1] = 0;
00355 if ((p = strchr(host, '/')))
00356 *p = 0;
00357 if ((p = strrchr(host, ':')))
00358 {
00359 *p = '\0';
00360 port = p+1;
00361 }
00362
00363 if (!strcmp("@", host))
00364 {
00365 hints.ai_flags = AI_PASSIVE;
00366 error = getaddrinfo(0, port, &hints, &res);
00367 }
00368 else
00369 {
00370 error = getaddrinfo(host, port, &hints, &res);
00371 }
00372 if (error)
00373 return 0;
00374 return res;
00375 }
00376
00377 #endif
00378
00379 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
00380 int default_port)
00381 {
00382 struct hostent *hp;
00383 char *p, buf[512];
00384 short int port = default_port;
00385 #ifdef WIN32
00386 unsigned long tmpadd;
00387 #else
00388 in_addr_t tmpadd;
00389 #endif
00390 TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
00391 add->sin_family = AF_INET;
00392 strncpy(buf, str, sizeof(buf)-1);
00393 buf[sizeof(buf)-1] = 0;
00394 if ((p = strchr(buf, '/')))
00395 *p = 0;
00396 if ((p = strrchr(buf, ':')))
00397 {
00398 *p = 0;
00399 port = atoi(p + 1);
00400 }
00401 add->sin_port = htons(port);
00402 if (!strcmp("@", buf))
00403 {
00404 add->sin_addr.s_addr = INADDR_ANY;
00405 }
00406 else if ((tmpadd = inet_addr(buf)) != -1)
00407 {
00408 memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
00409 }
00410 else if ((hp = gethostbyname(buf)))
00411 {
00412 memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
00413 sizeof(struct in_addr));
00414 }
00415 else
00416 return 0;
00417 return 1;
00418 }
00419
00420 #if HAVE_GETADDRINFO
00421 void *tcpip_straddr(COMSTACK h, const char *str)
00422 {
00423 tcpip_state *sp = (tcpip_state *)h->cprivate;
00424 const char *port = "210";
00425 struct addrinfo *ai = 0;
00426 if (h->protocol == PROTO_HTTP)
00427 port = "80";
00428 if (!tcpip_init())
00429 return 0;
00430
00431 if (sp->ai)
00432 freeaddrinfo(sp->ai);
00433 sp->ai = tcpip_getaddrinfo(str, port);
00434 if (sp->ai && h->state == CS_ST_UNBND)
00435 {
00436 int s = -1;
00437
00438 for (ai = sp->ai; ai; ai = ai->ai_next)
00439 {
00440 if (ai->ai_family == AF_INET6)
00441 {
00442 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
00443 if (s != -1)
00444 break;
00445 }
00446 }
00447 if (s == -1)
00448 {
00449
00450 for (ai = sp->ai; ai; ai = ai->ai_next)
00451 {
00452 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
00453 if (s != -1)
00454 break;
00455 }
00456 }
00457 if (s == -1)
00458 return 0;
00459 assert(ai);
00460 h->iofile = s;
00461
00462 if (!tcpip_set_blocking(h, h->flags))
00463 return 0;
00464 }
00465 return ai;
00466 }
00467 #else
00468 void *tcpip_straddr(COMSTACK h, const char *str)
00469 {
00470 tcpip_state *sp = (tcpip_state *)h->cprivate;
00471 int port = 210;
00472 if (h->protocol == PROTO_HTTP)
00473 port = 80;
00474
00475 if (!tcpip_init())
00476 return 0;
00477 if (!tcpip_strtoaddr_ex(str, &sp->addr, port))
00478 return 0;
00479 if (h->state == CS_ST_UNBND)
00480 {
00481 int s;
00482 s = socket(AF_INET, SOCK_STREAM, 0);
00483 if (s < 0)
00484 return 0;
00485 h->iofile = s;
00486
00487 if (!tcpip_set_blocking(h, h->flags))
00488 return 0;
00489 }
00490 return &sp->addr;
00491 }
00492 #endif
00493
00494 int tcpip_more(COMSTACK h)
00495 {
00496 tcpip_state *sp = (tcpip_state *)h->cprivate;
00497
00498 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
00499 }
00500
00501
00502
00503
00504
00505
00506 int tcpip_connect(COMSTACK h, void *address)
00507 {
00508 #if HAVE_GETADDRINFO
00509 struct addrinfo *ai = (struct addrinfo *) address;
00510 tcpip_state *sp = (tcpip_state *)h->cprivate;
00511 #else
00512 struct sockaddr_in *add = (struct sockaddr_in *) address;
00513 #endif
00514 int r;
00515 TRC(fprintf(stderr, "tcpip_connect\n"));
00516 h->io_pending = 0;
00517 if (h->state != CS_ST_UNBND)
00518 {
00519 h->cerrno = CSOUTSTATE;
00520 return -1;
00521 }
00522 #if HAVE_GETADDRINFO
00523 r = connect(h->iofile, ai->ai_addr, ai->ai_addrlen);
00524 freeaddrinfo(sp->ai);
00525 sp->ai = 0;
00526 #else
00527 r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
00528 #endif
00529 if (r < 0)
00530 {
00531 #ifdef WIN32
00532 if (WSAGetLastError() == WSAEWOULDBLOCK)
00533 {
00534 h->event = CS_CONNECT;
00535 h->state = CS_ST_CONNECTING;
00536 h->io_pending = CS_WANT_WRITE;
00537 return 1;
00538 }
00539 #else
00540 if (yaz_errno() == EINPROGRESS)
00541 {
00542 h->event = CS_CONNECT;
00543 h->state = CS_ST_CONNECTING;
00544 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
00545 return 1;
00546 }
00547 #endif
00548 h->cerrno = CSYSERR;
00549 return -1;
00550 }
00551 h->event = CS_CONNECT;
00552 h->state = CS_ST_CONNECTING;
00553
00554 return tcpip_rcvconnect(h);
00555 }
00556
00557
00558
00559
00560 int tcpip_rcvconnect(COMSTACK h)
00561 {
00562 #if ENABLE_SSL
00563 tcpip_state *sp = (tcpip_state *)h->cprivate;
00564 #endif
00565 TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
00566
00567 if (h->state == CS_ST_DATAXFER)
00568 return 0;
00569 if (h->state != CS_ST_CONNECTING)
00570 {
00571 h->cerrno = CSOUTSTATE;
00572 return -1;
00573 }
00574 #if HAVE_GNUTLS_H
00575 if (h->type == ssl_type && !sp->session)
00576 {
00577 int res;
00578 gnutls_global_init();
00579
00580 tcpip_create_cred(h);
00581
00582 gnutls_init(&sp->session, GNUTLS_CLIENT);
00583 gnutls_set_default_priority(sp->session);
00584 gnutls_credentials_set (sp->session, GNUTLS_CRD_CERTIFICATE,
00585 sp->cred_ptr->xcred);
00586
00587
00588 gnutls_transport_set_ptr(sp->session,
00589 (gnutls_transport_ptr_t)
00590 (size_t) h->iofile);
00591 res = gnutls_handshake(sp->session);
00592 if (res < 0)
00593 {
00594 if (ssl_check_error(h, sp, res))
00595 return 1;
00596 return -1;
00597 }
00598 }
00599 #elif HAVE_OPENSSL_SSL_H
00600 if (h->type == ssl_type && !sp->ctx)
00601 {
00602 SSL_library_init();
00603 SSL_load_error_strings();
00604
00605 sp->ctx = sp->ctx_alloc = SSL_CTX_new(SSLv23_client_method());
00606 if (!sp->ctx)
00607 {
00608 h->cerrno = CSERRORSSL;
00609 return -1;
00610 }
00611 }
00612 if (sp->ctx)
00613 {
00614 int res;
00615
00616 if (!sp->ssl)
00617 {
00618 sp->ssl = SSL_new(sp->ctx);
00619 SSL_set_fd(sp->ssl, h->iofile);
00620 }
00621 res = SSL_connect(sp->ssl);
00622 if (res <= 0)
00623 {
00624 if (ssl_check_error(h, sp, res))
00625 return 1;
00626 return -1;
00627 }
00628 }
00629 #endif
00630 h->event = CS_DATA;
00631 h->state = CS_ST_DATAXFER;
00632 return 0;
00633 }
00634
00635 #define CERTF "ztest.pem"
00636 #define KEYF "ztest.pem"
00637
00638 static int tcpip_bind(COMSTACK h, void *address, int mode)
00639 {
00640 int r;
00641 tcpip_state *sp = (tcpip_state *)h->cprivate;
00642 #if HAVE_GETADDRINFO
00643 struct addrinfo *ai = (struct addrinfo *) address;
00644 #else
00645 struct sockaddr *addr = (struct sockaddr *)address;
00646 #endif
00647 #ifdef WIN32
00648 BOOL one = 1;
00649 #else
00650 int one = 1;
00651 #endif
00652
00653 #if HAVE_GNUTLS_H
00654 if (h->type == ssl_type && !sp->session)
00655 {
00656 int res;
00657 gnutls_global_init();
00658
00659 tcpip_create_cred(h);
00660
00661 res = gnutls_certificate_set_x509_key_file(sp->cred_ptr->xcred,
00662 sp->cert_fname,
00663 sp->cert_fname,
00664 GNUTLS_X509_FMT_PEM);
00665 if (res != GNUTLS_E_SUCCESS)
00666 {
00667 h->cerrno = CSERRORSSL;
00668 return -1;
00669 }
00670 }
00671 #elif HAVE_OPENSSL_SSL_H
00672 if (h->type == ssl_type && !sp->ctx)
00673 {
00674 SSL_library_init();
00675 SSL_load_error_strings();
00676
00677 sp->ctx = sp->ctx_alloc = SSL_CTX_new(SSLv23_server_method());
00678 if (!sp->ctx)
00679 {
00680 h->cerrno = CSERRORSSL;
00681 return -1;
00682 }
00683 }
00684 if (sp->ctx)
00685 {
00686 if (sp->ctx_alloc)
00687 {
00688 int res;
00689 res = SSL_CTX_use_certificate_file(sp->ctx, sp->cert_fname,
00690 SSL_FILETYPE_PEM);
00691 if (res <= 0)
00692 {
00693 ERR_print_errors_fp(stderr);
00694 exit(2);
00695 }
00696 res = SSL_CTX_use_PrivateKey_file(sp->ctx, sp->cert_fname,
00697 SSL_FILETYPE_PEM);
00698 if (res <= 0)
00699 {
00700 ERR_print_errors_fp(stderr);
00701 exit(3);
00702 }
00703 res = SSL_CTX_check_private_key(sp->ctx);
00704 if (res <= 0)
00705 {
00706 ERR_print_errors_fp(stderr);
00707 exit(5);
00708 }
00709 }
00710 TRC(fprintf(stderr, "ssl_bind\n"));
00711 }
00712 else
00713 {
00714 TRC(fprintf(stderr, "tcpip_bind\n"));
00715 }
00716 #else
00717 TRC(fprintf(stderr, "tcpip_bind\n"));
00718 #endif
00719 #ifndef WIN32
00720 if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
00721 &one, sizeof(one)) < 0)
00722 {
00723 h->cerrno = CSYSERR;
00724 return -1;
00725 }
00726 #endif
00727 #if HAVE_GETADDRINFO
00728 r = bind(h->iofile, ai->ai_addr, ai->ai_addrlen);
00729 freeaddrinfo(sp->ai);
00730 sp->ai = 0;
00731 #else
00732 r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
00733 #endif
00734 if (r)
00735 {
00736 h->cerrno = CSYSERR;
00737 return -1;
00738 }
00739
00740 if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
00741 {
00742 h->cerrno = CSYSERR;
00743 return -1;
00744 }
00745 h->state = CS_ST_IDLE;
00746 h->event = CS_LISTEN;
00747 return 0;
00748 }
00749
00750 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
00751 int (*check_ip)(void *cd, const char *a, int len, int t),
00752 void *cd)
00753 {
00754 #ifdef WIN32
00755
00756 #else
00757 struct sockaddr_in addr;
00758 YAZ_SOCKLEN_T len = sizeof(addr);
00759 #endif
00760
00761 TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
00762 if (h->state != CS_ST_IDLE)
00763 {
00764 h->cerrno = CSOUTSTATE;
00765 return -1;
00766 }
00767 #ifdef WIN32
00768 h->newfd = accept(h->iofile, 0, 0);
00769 #else
00770 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
00771 #endif
00772 if (h->newfd < 0)
00773 {
00774 if (
00775 #ifdef WIN32
00776 WSAGetLastError() == WSAEWOULDBLOCK
00777 #else
00778 yaz_errno() == EWOULDBLOCK
00779 #ifdef EAGAIN
00780 #if EAGAIN != EWOULDBLOCK
00781 || yaz_errno() == EAGAIN
00782 #endif
00783 #endif
00784 #endif
00785 )
00786 h->cerrno = CSNODATA;
00787 else
00788 {
00789 #ifdef WIN32
00790 shutdown(h->iofile, SD_RECEIVE);
00791 #else
00792 shutdown(h->iofile, SHUT_RD);
00793 #endif
00794 listen(h->iofile, SOMAXCONN);
00795 h->cerrno = CSYSERR;
00796 }
00797 return -1;
00798 }
00799 #ifdef WIN32
00800 if (addrlen)
00801 *addrlen = 0;
00802 #else
00803 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
00804 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
00805 else if (addrlen)
00806 *addrlen = 0;
00807 if (check_ip && (*check_ip)(cd, (const char *) &addr,
00808 sizeof(addr), AF_INET))
00809 {
00810 h->cerrno = CSDENY;
00811 #ifdef WIN32
00812 closesocket(h->newfd);
00813 #else
00814 close(h->newfd);
00815 #endif
00816 h->newfd = -1;
00817 return -1;
00818 }
00819 #endif
00820 h->state = CS_ST_INCON;
00821 return 0;
00822 }
00823
00824 COMSTACK tcpip_accept(COMSTACK h)
00825 {
00826 COMSTACK cnew;
00827 #ifdef WIN32
00828 unsigned long tru = 1;
00829 #endif
00830
00831 TRC(fprintf(stderr, "tcpip_accept h=%p pid=%d\n", h, getpid()));
00832 if (h->state == CS_ST_INCON)
00833 {
00834 tcpip_state *state, *st = (tcpip_state *)h->cprivate;
00835 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
00836 {
00837 h->cerrno = CSYSERR;
00838 #ifdef WIN32
00839 closesocket(h->newfd);
00840 #else
00841 close(h->newfd);
00842 #endif
00843 h->newfd = -1;
00844 return 0;
00845 }
00846 memcpy(cnew, h, sizeof(*h));
00847 cnew->iofile = h->newfd;
00848 cnew->io_pending = 0;
00849
00850 if (!(state = (tcpip_state *)
00851 (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
00852 {
00853 h->cerrno = CSYSERR;
00854 if (h->newfd != -1)
00855 {
00856 #ifdef WIN32
00857 closesocket(h->newfd);
00858 #else
00859 close(h->newfd);
00860 #endif
00861 h->newfd = -1;
00862 }
00863 return 0;
00864 }
00865 if (!tcpip_set_blocking(cnew, cnew->flags))
00866 {
00867 h->cerrno = CSYSERR;
00868 if (h->newfd != -1)
00869 {
00870 #ifdef WIN32
00871 closesocket(h->newfd);
00872 #else
00873 close(h->newfd);
00874 #endif
00875 h->newfd = -1;
00876 }
00877 xfree(cnew);
00878 xfree(state);
00879 return 0;
00880 }
00881 h->newfd = -1;
00882 state->altbuf = 0;
00883 state->altsize = state->altlen = 0;
00884 state->towrite = state->written = -1;
00885 state->complete = st->complete;
00886 #if HAVE_GETADDRINFO
00887 state->ai = 0;
00888 #endif
00889 cnew->state = CS_ST_ACCEPT;
00890 h->state = CS_ST_IDLE;
00891
00892 #if HAVE_GNUTLS_H
00893 state->cred_ptr = st->cred_ptr;
00894 state->session = 0;
00895 if (st->cred_ptr)
00896 {
00897 int res;
00898
00899 (state->cred_ptr->ref)++;
00900 gnutls_init(&state->session, GNUTLS_SERVER);
00901 if (!state->session)
00902 {
00903 xfree(cnew);
00904 xfree(state);
00905 return 0;
00906 }
00907 res = gnutls_set_default_priority(state->session);
00908 if (res != GNUTLS_E_SUCCESS)
00909 {
00910 xfree(cnew);
00911 xfree(state);
00912 return 0;
00913 }
00914 res = gnutls_credentials_set(state->session,
00915 GNUTLS_CRD_CERTIFICATE,
00916 st->cred_ptr->xcred);
00917 if (res != GNUTLS_E_SUCCESS)
00918 {
00919 xfree(cnew);
00920 xfree(state);
00921 return 0;
00922 }
00923
00924 gnutls_transport_set_ptr(state->session,
00925 (gnutls_transport_ptr_t)
00926 (size_t) cnew->iofile);
00927 }
00928 #elif HAVE_OPENSSL_SSL_H
00929 state->ctx = st->ctx;
00930 state->ctx_alloc = 0;
00931 state->ssl = st->ssl;
00932 if (state->ctx)
00933 {
00934 state->ssl = SSL_new(state->ctx);
00935 SSL_set_fd(state->ssl, cnew->iofile);
00936 }
00937 #endif
00938 state->connect_request_buf = 0;
00939 state->connect_response_buf = 0;
00940 h = cnew;
00941 }
00942 if (h->state == CS_ST_ACCEPT)
00943 {
00944 #if HAVE_GNUTLS_H
00945 tcpip_state *state = (tcpip_state *)h->cprivate;
00946 if (state->session)
00947 {
00948 int res = gnutls_handshake(state->session);
00949 if (res < 0)
00950 {
00951 if (ssl_check_error(h, state, res))
00952 {
00953 TRC(fprintf(stderr, "gnutls_handshake int in tcpip_accept\n"));
00954 return h;
00955 }
00956 TRC(fprintf(stderr, "gnutls_handshake failed in tcpip_accept\n"));
00957 cs_close(h);
00958 return 0;
00959 }
00960 TRC(fprintf(stderr, "SSL_accept complete. gnutls\n"));
00961 }
00962 #elif HAVE_OPENSSL_SSL_H
00963 tcpip_state *state = (tcpip_state *)h->cprivate;
00964 if (state->ctx)
00965 {
00966 int res;
00967 errno = 0;
00968 res = SSL_accept(state->ssl);
00969 TRC(fprintf(stderr, "SSL_accept res=%d\n", res));
00970 if (res <= 0)
00971 {
00972 if (ssl_check_error(h, state, res))
00973 {
00974 return h;
00975 }
00976 cs_close(h);
00977 return 0;
00978 }
00979 TRC(fprintf(stderr, "SSL_accept complete\n"));
00980 }
00981 #endif
00982 }
00983 else
00984 {
00985 h->cerrno = CSOUTSTATE;
00986 return 0;
00987 }
00988 h->io_pending = 0;
00989 h->state = CS_ST_DATAXFER;
00990 h->event = CS_DATA;
00991 return h;
00992 }
00993
00994 #define CS_TCPIP_BUFCHUNK 4096
00995
00996
00997
00998
00999
01000 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
01001 {
01002 tcpip_state *sp = (tcpip_state *)h->cprivate;
01003 char *tmpc;
01004 int tmpi, berlen, rest, req, tomove;
01005 int hasread = 0, res;
01006
01007 TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
01008 if (sp->altlen)
01009 {
01010 TRC(fprintf(stderr, " %d bytes in altbuf (%p)\n", sp->altlen,
01011 sp->altbuf));
01012 tmpc = *buf;
01013 tmpi = *bufsize;
01014 *buf = sp->altbuf;
01015 *bufsize = sp->altsize;
01016 hasread = sp->altlen;
01017 sp->altlen = 0;
01018 sp->altbuf = tmpc;
01019 sp->altsize = tmpi;
01020 }
01021 h->io_pending = 0;
01022 while (!(berlen = (*sp->complete)(*buf, hasread)))
01023 {
01024 if (!*bufsize)
01025 {
01026 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
01027 {
01028 h->cerrno = CSYSERR;
01029 return -1;
01030 }
01031 }
01032 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
01033 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
01034 {
01035 h->cerrno = CSYSERR;
01036 return -1;
01037 }
01038 #ifdef __sun__
01039 yaz_set_errno( 0 );
01040
01041
01042 #endif
01043 res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
01044 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
01045 if (res < 0)
01046 {
01047 TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
01048 strerror(yaz_errno())));
01049 #ifdef WIN32
01050 if (WSAGetLastError() == WSAEWOULDBLOCK)
01051 {
01052 h->io_pending = CS_WANT_READ;
01053 break;
01054 }
01055 else
01056 {
01057 h->cerrno = CSYSERR;
01058 return -1;
01059 }
01060 #else
01061 if (yaz_errno() == EWOULDBLOCK
01062 #ifdef EAGAIN
01063 #if EAGAIN != EWOULDBLOCK
01064 || yaz_errno() == EAGAIN
01065 #endif
01066 #endif
01067 || yaz_errno() == EINPROGRESS
01068 #ifdef __sun__
01069 || yaz_errno() == ENOENT
01070 #endif
01071 )
01072 {
01073 h->io_pending = CS_WANT_READ;
01074 break;
01075 }
01076 else if (yaz_errno() == 0)
01077 continue;
01078 else
01079 {
01080 h->cerrno = CSYSERR;
01081 return -1;
01082 }
01083 #endif
01084 }
01085 else if (!res)
01086 return hasread;
01087 hasread += res;
01088 if (hasread > h->max_recv_bytes)
01089 {
01090 h->cerrno = CSBUFSIZE;
01091 return -1;
01092 }
01093 }
01094 TRC(fprintf(stderr, " Out of read loop with hasread=%d, berlen=%d\n",
01095 hasread, berlen));
01096
01097 if (hasread > berlen)
01098 {
01099 tomove = req = hasread - berlen;
01100 rest = tomove % CS_TCPIP_BUFCHUNK;
01101 if (rest)
01102 req += CS_TCPIP_BUFCHUNK - rest;
01103 if (!sp->altbuf)
01104 {
01105 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
01106 {
01107 h->cerrno = CSYSERR;
01108 return -1;
01109 }
01110 } else if (sp->altsize < req)
01111 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
01112 {
01113 h->cerrno = CSYSERR;
01114 return -1;
01115 }
01116 TRC(fprintf(stderr, " Moving %d bytes to altbuf(%p)\n", tomove,
01117 sp->altbuf));
01118 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
01119 }
01120 if (berlen < CS_TCPIP_BUFCHUNK - 1)
01121 *(*buf + berlen) = '\0';
01122 return berlen ? berlen : 1;
01123 }
01124
01125
01126 #if ENABLE_SSL
01127
01128
01129
01130
01131 int ssl_get(COMSTACK h, char **buf, int *bufsize)
01132 {
01133 tcpip_state *sp = (tcpip_state *)h->cprivate;
01134 char *tmpc;
01135 int tmpi, berlen, rest, req, tomove;
01136 int hasread = 0, res;
01137
01138 TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
01139 if (sp->altlen)
01140 {
01141 TRC(fprintf(stderr, " %d bytes in altbuf (%p)\n", sp->altlen,
01142 sp->altbuf));
01143 tmpc = *buf;
01144 tmpi = *bufsize;
01145 *buf = sp->altbuf;
01146 *bufsize = sp->altsize;
01147 hasread = sp->altlen;
01148 sp->altlen = 0;
01149 sp->altbuf = tmpc;
01150 sp->altsize = tmpi;
01151 }
01152 h->io_pending = 0;
01153 while (!(berlen = (*sp->complete)(*buf, hasread)))
01154 {
01155 if (!*bufsize)
01156 {
01157 if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
01158 return -1;
01159 }
01160 else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
01161 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
01162 return -1;
01163 #if HAVE_GNUTLS_H
01164 res = gnutls_record_recv(sp->session, *buf + hasread,
01165 CS_TCPIP_BUFCHUNK);
01166 if (res < 0)
01167 {
01168 if (ssl_check_error(h, sp, res))
01169 break;
01170 return -1;
01171 }
01172 #else
01173 res = SSL_read(sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
01174 TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
01175 if (res <= 0)
01176 {
01177 if (ssl_check_error(h, sp, res))
01178 break;
01179 return -1;
01180 }
01181 #endif
01182 hasread += res;
01183 }
01184 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
01185 hasread, berlen));
01186
01187 if (hasread > berlen)
01188 {
01189 tomove = req = hasread - berlen;
01190 rest = tomove % CS_TCPIP_BUFCHUNK;
01191 if (rest)
01192 req += CS_TCPIP_BUFCHUNK - rest;
01193 if (!sp->altbuf)
01194 {
01195 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
01196 return -1;
01197 } else if (sp->altsize < req)
01198 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
01199 return -1;
01200 TRC(fprintf(stderr, " Moving %d bytes to altbuf(%p)\n", tomove,
01201 sp->altbuf));
01202 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
01203 }
01204 if (berlen < CS_TCPIP_BUFCHUNK - 1)
01205 *(*buf + berlen) = '\0';
01206 return berlen ? berlen : 1;
01207 }
01208 #endif
01209
01210
01211
01212
01213
01214
01215 int tcpip_put(COMSTACK h, char *buf, int size)
01216 {
01217 int res;
01218 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
01219
01220 TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
01221 h->io_pending = 0;
01222 h->event = CS_DATA;
01223 if (state->towrite < 0)
01224 {
01225 state->towrite = size;
01226 state->written = 0;
01227 }
01228 else if (state->towrite != size)
01229 {
01230 h->cerrno = CSWRONGBUF;
01231 return -1;
01232 }
01233 while (state->towrite > state->written)
01234 {
01235 if ((res =
01236 send(h->iofile, buf + state->written, size -
01237 state->written,
01238 #ifdef MSG_NOSIGNAL
01239 MSG_NOSIGNAL
01240 #else
01241 0
01242 #endif
01243 )) < 0)
01244 {
01245 if (
01246 #ifdef WIN32
01247 WSAGetLastError() == WSAEWOULDBLOCK
01248 #else
01249 yaz_errno() == EWOULDBLOCK
01250 #ifdef EAGAIN
01251 #if EAGAIN != EWOULDBLOCK
01252 || yaz_errno() == EAGAIN
01253 #endif
01254 #endif
01255 #ifdef __sun__
01256 || yaz_errno() == ENOENT
01257 #endif
01258 || yaz_errno() == EINPROGRESS
01259 #endif
01260 )
01261 {
01262 TRC(fprintf(stderr, " Flow control stop\n"));
01263 h->io_pending = CS_WANT_WRITE;
01264 return 1;
01265 }
01266 h->cerrno = CSYSERR;
01267 return -1;
01268 }
01269 state->written += res;
01270 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
01271 res, state->written, size));
01272 }
01273 state->towrite = state->written = -1;
01274 TRC(fprintf(stderr, " Ok\n"));
01275 return 0;
01276 }
01277
01278
01279 #if ENABLE_SSL
01280
01281
01282
01283
01284
01285 int ssl_put(COMSTACK h, char *buf, int size)
01286 {
01287 int res;
01288 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
01289
01290 TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
01291 h->io_pending = 0;
01292 h->event = CS_DATA;
01293 if (state->towrite < 0)
01294 {
01295 state->towrite = size;
01296 state->written = 0;
01297 }
01298 else if (state->towrite != size)
01299 {
01300 h->cerrno = CSWRONGBUF;
01301 return -1;
01302 }
01303 while (state->towrite > state->written)
01304 {
01305 #if HAVE_GNUTLS_H
01306 res = gnutls_record_send(state->session, buf + state->written,
01307 size - state->written);
01308 if (res <= 0)
01309 {
01310 if (ssl_check_error(h, state, res))
01311 return 1;
01312 return -1;
01313 }
01314 #else
01315 res = SSL_write(state->ssl, buf + state->written,
01316 size - state->written);
01317 if (res <= 0)
01318 {
01319 if (ssl_check_error(h, state, res))
01320 return 1;
01321 return -1;
01322 }
01323 #endif
01324 state->written += res;
01325 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
01326 res, state->written, size));
01327 }
01328 state->towrite = state->written = -1;
01329 TRC(fprintf(stderr, " Ok\n"));
01330 return 0;
01331 }
01332 #endif
01333
01334 void tcpip_close(COMSTACK h)
01335 {
01336 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
01337
01338 TRC(fprintf(stderr, "tcpip_close h=%p pid=%d\n", h, getpid()));
01339 if (h->iofile != -1)
01340 {
01341 #if HAVE_GNUTLS_H
01342 if (sp->session)
01343 gnutls_bye(sp->session, GNUTLS_SHUT_RDWR);
01344 #elif HAVE_OPENSSL_SSL_H
01345 if (sp->ssl)
01346 {
01347 SSL_shutdown(sp->ssl);
01348 }
01349 #endif
01350 #ifdef WIN32
01351 closesocket(h->iofile);
01352 #else
01353 close(h->iofile);
01354 #endif
01355 }
01356 if (sp->altbuf)
01357 xfree(sp->altbuf);
01358 #if HAVE_GNUTLS_H
01359 if (sp->session)
01360 {
01361 gnutls_deinit(sp->session);
01362 }
01363 if (sp->cred_ptr)
01364 {
01365 assert(sp->cred_ptr->ref > 0);
01366
01367 if (--(sp->cred_ptr->ref) == 0)
01368 {
01369 TRC(fprintf(stderr, "Removed credentials %p pid=%d\n",
01370 sp->cred_ptr->xcred, getpid()));
01371 gnutls_certificate_free_credentials(sp->cred_ptr->xcred);
01372 xfree(sp->cred_ptr);
01373 }
01374 sp->cred_ptr = 0;
01375 }
01376 #elif HAVE_OPENSSL_SSL_H
01377 if (sp->ssl)
01378 {
01379 TRC(fprintf(stderr, "SSL_free\n"));
01380 SSL_free(sp->ssl);
01381 }
01382 sp->ssl = 0;
01383 if (sp->ctx_alloc)
01384 SSL_CTX_free(sp->ctx_alloc);
01385 #endif
01386 #if HAVE_GETADDRINFO
01387 if (sp->ai)
01388 freeaddrinfo(sp->ai);
01389 #endif
01390 xfree(sp->connect_request_buf);
01391 xfree(sp->connect_response_buf);
01392 xfree(sp);
01393 xfree(h);
01394 }
01395
01396 const char *tcpip_addrstr(COMSTACK h)
01397 {
01398 tcpip_state *sp = (struct tcpip_state *)h->cprivate;
01399 char *r = 0, *buf = sp->buf;
01400
01401 #if HAVE_GETADDRINFO
01402 char host[120];
01403 struct sockaddr_storage addr;
01404 YAZ_SOCKLEN_T len = sizeof(addr);
01405
01406 if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
01407 {
01408 h->cerrno = CSYSERR;
01409 return 0;
01410 }
01411 if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1,
01412 0, 0,
01413 (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
01414 {
01415 r = "unknown";
01416 }
01417 else
01418 r = host;
01419
01420 #else
01421
01422 struct sockaddr_in addr;
01423 YAZ_SOCKLEN_T len = sizeof(addr);
01424 struct hostent *host;
01425
01426 if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
01427 {
01428 h->cerrno = CSYSERR;
01429 return 0;
01430 }
01431 if (!(h->flags & CS_FLAGS_NUMERICHOST))
01432 {
01433 if ((host = gethostbyaddr((char*)&addr.sin_addr,
01434 sizeof(addr.sin_addr),
01435 AF_INET)))
01436 r = (char*) host->h_name;
01437 }
01438 if (!r)
01439 r = inet_ntoa(addr.sin_addr);
01440 #endif
01441
01442 if (h->protocol == PROTO_HTTP)
01443 sprintf(buf, "http:%s", r);
01444 else
01445 sprintf(buf, "tcp:%s", r);
01446 #if HAVE_GNUTLS_H
01447 if (sp->session)
01448 {
01449 if (h->protocol == PROTO_HTTP)
01450 sprintf(buf, "https:%s", r);
01451 else
01452 sprintf(buf, "ssl:%s", r);
01453 }
01454 #elif HAVE_OPENSSL_SSL_H
01455 if (sp->ctx)
01456 {
01457 if (h->protocol == PROTO_HTTP)
01458 sprintf(buf, "https:%s", r);
01459 else
01460 sprintf(buf, "ssl:%s", r);
01461 }
01462 #endif
01463 return buf;
01464 }
01465
01466 static int tcpip_set_blocking(COMSTACK p, int flags)
01467 {
01468 unsigned long flag;
01469
01470 #ifdef WIN32
01471 flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
01472 if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
01473 return 0;
01474 #else
01475 flag = fcntl(p->iofile, F_GETFL, 0);
01476 if (flags & CS_FLAGS_BLOCKING)
01477 flag = flag & ~O_NONBLOCK;
01478 else
01479 {
01480 flag = flag | O_NONBLOCK;
01481 signal(SIGPIPE, SIG_IGN);
01482 }
01483 if (fcntl(p->iofile, F_SETFL, flag) < 0)
01484 return 0;
01485 #endif
01486 p->flags = flags;
01487 return 1;
01488 }
01489
01490 void cs_print_session_info(COMSTACK cs)
01491 {
01492 #if HAVE_GNUTLS_H
01493 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
01494 if (sp->session)
01495 {
01496 if (gnutls_certificate_type_get(sp->session) != GNUTLS_CRT_X509)
01497 return;
01498 printf("X509 certificate\n");
01499 }
01500 #elif HAVE_OPENSSL_SSL_H
01501 struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
01502 SSL *ssl = (SSL *) sp->ssl;
01503 if (ssl)
01504 {
01505 X509 *server_cert = SSL_get_peer_certificate(ssl);
01506
01507 if (server_cert)
01508 {
01509 char *pem_buf;
01510 int pem_len;
01511 BIO *bio = BIO_new(BIO_s_mem());
01512
01513
01514 PEM_write_bio_X509(bio, server_cert);
01515 pem_len = BIO_get_mem_data(bio, &pem_buf);
01516 fwrite(pem_buf, pem_len, 1, stdout);
01517
01518
01519 X509_print_fp(stdout, server_cert);
01520 BIO_free(bio);
01521
01522 X509_free(server_cert);
01523 }
01524 }
01525 #endif
01526 }
01527
01528 void *cs_get_ssl(COMSTACK cs)
01529 {
01530 #if HAVE_OPENSSL_SSL_H
01531 struct tcpip_state *sp;
01532 if (!cs || cs->type != ssl_type)
01533 return 0;
01534 sp = (struct tcpip_state *) cs->cprivate;
01535 return sp->ssl;
01536 #else
01537 return 0;
01538 #endif
01539 }
01540
01541 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
01542 {
01543 #if ENABLE_SSL
01544 struct tcpip_state *sp;
01545 if (!cs || cs->type != ssl_type)
01546 return 0;
01547 sp = (struct tcpip_state *) cs->cprivate;
01548 #if HAVE_OPENSSL_SSL_H
01549 if (sp->ctx_alloc)
01550 return 0;
01551 sp->ctx = (SSL_CTX *) ctx;
01552 #endif
01553 return 1;
01554 #else
01555 return 0;
01556 #endif
01557 }
01558
01559 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
01560 {
01561 #if ENABLE_SSL
01562 struct tcpip_state *sp;
01563 if (!cs || cs->type != ssl_type)
01564 return 0;
01565 sp = (struct tcpip_state *) cs->cprivate;
01566 strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
01567 sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
01568 return 1;
01569 #else
01570 return 0;
01571 #endif
01572 }
01573
01574 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
01575 {
01576 #if HAVE_OPENSSL_SSL_H
01577 SSL *ssl = (SSL *) cs_get_ssl(cs);
01578 if (ssl)
01579 {
01580 X509 *server_cert = SSL_get_peer_certificate(ssl);
01581 if (server_cert)
01582 {
01583 BIO *bio = BIO_new(BIO_s_mem());
01584 char *pem_buf;
01585
01586 PEM_write_bio_X509(bio, server_cert);
01587 *len = BIO_get_mem_data(bio, &pem_buf);
01588 *buf = (char *) xmalloc(*len);
01589 memcpy(*buf, pem_buf, *len);
01590 BIO_free(bio);
01591 return 1;
01592 }
01593 }
01594 #endif
01595 return 0;
01596 }
01597
01598 static int tcpip_put_connect(COMSTACK h, char *buf, int size)
01599 {
01600 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
01601
01602 int r = tcpip_put(h, state->connect_request_buf,
01603 state->connect_request_len);
01604 if (r == 0)
01605 {
01606
01607 h->f_put = tcpip_put;
01608 r = tcpip_put(h, buf, size);
01609 }
01610 return r;
01611 }
01612
01613 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize)
01614 {
01615 struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
01616 int r;
01617
01618 r = tcpip_get(h, &state->connect_response_buf,
01619 &state->connect_response_len);
01620 if (r < 1)
01621 return r;
01622
01623 state->complete = cs_complete_auto;
01624 h->f_get = tcpip_get;
01625 return tcpip_get(h, buf, bufsize);
01626 }
01627
01628
01629
01630
01631
01632
01633
01634
01635
01636
01637