GNU TLS operational. Issues with non-blocked I/O left.
[yaz-moved-to-github.git] / src / tcpip.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file tcpip.c
7  * \brief Implements TCP/IP + SSL COMSTACK.
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #if HAVE_SYS_TYPES_H
17 #include <sys/types.h>
18 #endif
19 #if HAVE_SYS_TIME_H
20 #include <sys/time.h>
21 #endif
22 #if HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25
26 #ifdef WIN32
27
28 /* VS 2003 or later has getaddrinfo; older versions do not */
29 #include <winsock2.h>
30 #if _MSC_VER >= 1300
31 #include <ws2tcpip.h>
32 #define HAVE_GETADDRINFO 1
33 #else
34 #define HAVE_GETADDRINFO 0
35 #endif
36
37 #else
38 #include <netinet/in.h>
39 #include <netdb.h>
40 #include <arpa/inet.h>
41 #include <netinet/tcp.h>
42 #endif
43
44 #if HAVE_SYS_SOCKET_H
45 #include <sys/socket.h>
46 #endif
47 #if HAVE_SYS_WAIT_H
48 #include <sys/wait.h>
49 #endif
50
51 #if HAVE_GNUTLS_H
52 #include <gnutls/openssl.h>
53 #define ENABLE_SSL 1
54 #endif
55
56 #if HAVE_OPENSSL_SSL_H
57 #include <openssl/ssl.h>
58 #include <openssl/err.h>
59 #define ENABLE_SSL 1
60 #endif
61
62 #include <yaz/comstack.h>
63 #include <yaz/tcpip.h>
64 #include <yaz/nmem.h>
65
66 static int tcpip_close(COMSTACK h);
67 static int tcpip_put(COMSTACK h, char *buf, int size);
68 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
69 static int tcpip_put_connect(COMSTACK h, char *buf, int size);
70 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize);
71 static int tcpip_connect(COMSTACK h, void *address);
72 static int tcpip_more(COMSTACK h);
73 static int tcpip_rcvconnect(COMSTACK h);
74 static int tcpip_bind(COMSTACK h, void *address, int mode);
75 static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
76                  int (*check_ip)(void *cd, const char *a, int len, int type),
77                  void *cd);
78 static int tcpip_set_blocking(COMSTACK p, int blocking);
79
80 #if ENABLE_SSL
81 static int ssl_get(COMSTACK h, char **buf, int *bufsize);
82 static int ssl_put(COMSTACK h, char *buf, int size);
83 #endif
84
85 static COMSTACK tcpip_accept(COMSTACK h);
86 static char *tcpip_addrstr(COMSTACK h);
87 static void *tcpip_straddr(COMSTACK h, const char *str);
88
89 #if 0
90 #define TRC(x) x
91 #else
92 #define TRC(X)
93 #endif
94
95 #ifndef YAZ_SOCKLEN_T
96 #define YAZ_SOCKLEN_T int
97 #endif
98
99 /* this state is used for both SSL and straight TCP/IP */
100 typedef struct tcpip_state
101 {
102     char *altbuf; /* alternate buffer for surplus data */
103     int altsize;  /* size as xmalloced */
104     int altlen;   /* length of data or 0 if none */
105
106     int written;  /* -1 if we aren't writing */
107     int towrite;  /* to verify against user input */
108     int (*complete)(const char *buf, int len); /* length/complete. */
109 #if HAVE_GETADDRINFO
110     struct addrinfo *ai;
111 #else
112     struct sockaddr_in addr;  /* returned by cs_straddr */
113 #endif
114     char buf[128]; /* returned by cs_addrstr */
115 #if ENABLE_SSL
116     SSL_CTX *ctx;       /* current CTX. */
117     SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
118     SSL *ssl;
119     char cert_fname[256];
120 #endif
121     char *connect_request_buf;
122     int connect_request_len;
123     char *connect_response_buf;
124     int connect_response_len;
125 } tcpip_state;
126
127 #ifdef WIN32
128 static int tcpip_init (void)
129 {
130     static int initialized = 0;
131     if (!initialized)
132     {
133         WORD requested;
134         WSADATA wd;
135
136         requested = MAKEWORD(1, 1);
137         if (WSAStartup(requested, &wd))
138             return 0;
139         initialized = 1;
140     }
141     return 1;
142 }
143 #else
144 static int tcpip_init (void)
145 {
146     return 1;
147 }
148 #endif
149
150 /*
151  * This function is always called through the cs_create() macro.
152  * s >= 0: socket has already been established for us.
153  */
154 COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
155 {
156     COMSTACK p;
157     tcpip_state *sp;
158
159     if (!tcpip_init ())
160         return 0;
161     if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
162         return 0;
163     if (!(sp = (struct tcpip_state *)(p->cprivate =
164                                          xmalloc(sizeof(tcpip_state)))))
165         return 0;
166
167     p->flags = flags;
168
169     p->io_pending = 0;
170     p->iofile = s;
171     p->type = tcpip_type;
172     p->protocol = (enum oid_proto) protocol;
173
174     p->f_connect = tcpip_connect;
175     p->f_rcvconnect = tcpip_rcvconnect;
176     p->f_get = tcpip_get;
177     p->f_put = tcpip_put;
178     p->f_close = tcpip_close;
179     p->f_more = tcpip_more;
180     p->f_bind = tcpip_bind;
181     p->f_listen = tcpip_listen;
182     p->f_accept = tcpip_accept;
183     p->f_addrstr = tcpip_addrstr;
184     p->f_straddr = tcpip_straddr;
185     p->f_set_blocking = tcpip_set_blocking;
186     p->max_recv_bytes = 5000000;
187
188     p->state = s < 0 ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
189     p->event = CS_NONE;
190     p->cerrno = 0;
191     p->stackerr = 0;
192     p->user = 0;
193
194 #if ENABLE_SSL
195     sp->ctx = sp->ctx_alloc = 0;
196     sp->ssl = 0;
197     strcpy(sp->cert_fname, "yaz.pem");
198 #endif
199
200 #if HAVE_GETADDRINFO
201     sp->ai = 0;
202 #endif
203     sp->altbuf = 0;
204     sp->altsize = sp->altlen = 0;
205     sp->towrite = sp->written = -1;
206     if (protocol == PROTO_WAIS)
207         sp->complete = completeWAIS;
208     else
209         sp->complete = cs_complete_auto;
210
211     sp->connect_request_buf = 0;
212     sp->connect_request_len = 0;
213     sp->connect_response_buf = 0;
214     sp->connect_response_len = 0;
215
216     p->timeout = COMSTACK_DEFAULT_TIMEOUT;
217     TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
218
219     return p;
220 }
221
222 COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
223                           const char *connect_host)
224 {
225     COMSTACK p = tcpip_type(s, flags, protocol, 0);
226     if (!p)
227         return 0;
228     if (connect_host)
229     {
230         tcpip_state *sp = (tcpip_state *) p->cprivate;
231         sp->connect_request_buf = (char *) xmalloc(strlen(connect_host) + 30);
232         sprintf(sp->connect_request_buf, "CONNECT %s HTTP/1.0\r\n\r\n",
233                 connect_host);
234         sp->connect_request_len = strlen(sp->connect_request_buf);
235         p->f_put = tcpip_put_connect;
236         p->f_get = tcpip_get_connect;
237         sp->complete = cs_complete_auto_head; /* only want HTTP header */
238     }
239     return p;
240 }
241
242
243 #if ENABLE_SSL
244
245 COMSTACK ssl_type(int s, int flags, int protocol, void *vp)
246 {
247     tcpip_state *sp;
248     COMSTACK p;
249
250     p = tcpip_type (s, flags, protocol, 0);
251     if (!p)
252         return 0;
253     p->f_get = ssl_get;
254     p->f_put = ssl_put;
255     p->type = ssl_type;
256     sp = (tcpip_state *) p->cprivate;
257
258     sp->ctx = (SSL_CTX *) vp;  /* may be NULL */
259
260     /* note: we don't handle already opened socket in SSL mode - yet */
261     return p;
262 }
263 #endif
264
265 #if HAVE_GETADDRINFO
266 /* resolve using getaddrinfo */
267 struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
268 {
269     struct addrinfo hints, *res;
270     int error;
271     char host[512], *p;
272
273     hints.ai_flags = 0;
274     hints.ai_family = AF_UNSPEC;
275     hints.ai_socktype = SOCK_STREAM;
276     hints.ai_protocol = 0;
277     hints.ai_addrlen        = 0;
278     hints.ai_addr           = NULL;
279     hints.ai_canonname      = NULL;
280     hints.ai_next           = NULL;
281
282     strncpy(host, str, sizeof(host)-1);
283     host[sizeof(host)-1] = 0;
284     if ((p = strchr(host, '/')))
285         *p = 0;
286     if ((p = strrchr(host, ':')))
287     {
288         *p = '\0';
289         port = p+1;
290     }
291
292     if (!strcmp("@", host))
293     {
294         hints.ai_flags = AI_PASSIVE;
295         error = getaddrinfo(0, port, &hints, &res);
296     }
297     else
298     {
299         error = getaddrinfo(host, port, &hints, &res);
300     }
301     if (error)
302         return 0;
303     return res;
304 }
305
306 #endif
307 /* gethostbyname .. old systems */
308 int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
309                        int default_port)
310 {
311     struct hostent *hp;
312     char *p, buf[512];
313     short int port = default_port;
314 #ifdef WIN32
315     unsigned long tmpadd;
316 #else
317     in_addr_t tmpadd;
318 #endif
319     TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
320     add->sin_family = AF_INET;
321     strncpy(buf, str, sizeof(buf)-1);
322     buf[sizeof(buf)-1] = 0;
323     if ((p = strchr(buf, '/')))
324         *p = 0;
325     if ((p = strrchr(buf, ':')))
326     {
327         *p = 0;
328         port = atoi(p + 1);
329     }
330     add->sin_port = htons(port);
331     if (!strcmp("@", buf))
332     {
333         add->sin_addr.s_addr = INADDR_ANY;
334     }
335     else if ((tmpadd = inet_addr(buf)) != -1)
336     {
337         memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
338     }
339     else if ((hp = gethostbyname(buf)))
340     {
341         memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
342                sizeof(struct in_addr));
343     }
344     else
345         return 0;
346     return 1;
347 }
348
349
350 #if HAVE_GETADDRINFO
351 void *tcpip_straddr(COMSTACK h, const char *str)
352 {
353     tcpip_state *sp = (tcpip_state *)h->cprivate;
354     const char *port = "210";
355     if (h->protocol == PROTO_HTTP)
356         port = "80";
357     if (!tcpip_init ())
358         return 0;
359
360     if (sp->ai)
361         freeaddrinfo(sp->ai);
362     sp->ai = tcpip_getaddrinfo(str, port);
363     if (sp->ai && h->state == CS_ST_UNBND)
364     {
365         int s = -1;
366         struct addrinfo *ai = sp->ai;
367         for (; ai; ai = ai->ai_next)
368         {
369             s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
370             if (s != -1)
371                 break;
372         }
373         if (s == -1)
374             return 0;
375         h->iofile = s;
376         
377         if (!tcpip_set_blocking(h, h->flags))
378             return 0;
379     }
380     return sp->ai;
381 }
382 #else
383 void *tcpip_straddr(COMSTACK h, const char *str)
384 {
385     tcpip_state *sp = (tcpip_state *)h->cprivate;
386     int port = 210;
387     if (h->protocol == PROTO_HTTP)
388         port = 80;
389
390     if (!tcpip_init ())
391         return 0;
392     if (!tcpip_strtoaddr_ex (str, &sp->addr, port))
393         return 0;
394     if (h->state == CS_ST_UNBND)
395     {
396         int s;
397         s = socket(AF_INET, SOCK_STREAM, 0);
398         if (s < 0)
399             return 0;
400         h->iofile = s;
401
402         if (!tcpip_set_blocking(h, h->flags))
403             return 0;
404     }
405     return &sp->addr;
406 }
407 #endif
408
409 int tcpip_more(COMSTACK h)
410 {
411     tcpip_state *sp = (tcpip_state *)h->cprivate;
412     
413     return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
414 }
415
416 /*
417  * connect(2) will block (sometimes) - nothing we can do short of doing
418  * weird things like spawning subprocesses or threading or some weird junk
419  * like that.
420  */
421 int tcpip_connect(COMSTACK h, void *address)
422 {
423 #if HAVE_GETADDRINFO
424     tcpip_state *sp = (tcpip_state *)h->cprivate;
425 #else
426     struct sockaddr_in *add = (struct sockaddr_in *) address;
427 #endif
428     int r;
429 #ifdef __sun__
430     int recbuflen;
431     YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
432 #endif
433     TRC(fprintf(stderr, "tcpip_connect\n"));
434     h->io_pending = 0;
435     if (h->state != CS_ST_UNBND)
436     {
437         h->cerrno = CSOUTSTATE;
438         return -1;
439     }
440 #if HAVE_GETADDRINFO
441     if (sp->ai != (struct addrinfo *) address)
442     {
443         h->cerrno = CSOUTSTATE;
444         return -1;
445     }
446 #endif
447 #ifdef __sun__
448     /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
449      * This gives the connect a chance to negotiate with the other side
450      * (see 'man tcp') 
451      */
452     if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
453     {
454         h->cerrno = CSYSERR;
455         return -1;
456     }
457     TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
458                  recbuflen ));
459     recbuflen *= 10; /* lets be optimistic */
460     if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
461     {
462         h->cerrno = CSYSERR;
463         return -1;
464     }
465     if ( getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
466     {
467         h->cerrno = CSYSERR;
468         return -1;
469     }
470     TRC(fprintf( stderr, "New Size of TCP Receive Buffer = %d\n",
471                  recbuflen ));
472 #endif
473
474 #if HAVE_GETADDRINFO
475     r = connect(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
476     freeaddrinfo(sp->ai);
477     sp->ai = 0;
478 #else
479     r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
480 #endif
481     if (r < 0)
482     {
483 #ifdef WIN32
484         if (WSAGetLastError() == WSAEWOULDBLOCK)
485         {
486             h->event = CS_CONNECT;
487             h->state = CS_ST_CONNECTING;
488             h->io_pending = CS_WANT_WRITE;
489             return 1;
490         }
491 #else
492         if (yaz_errno() == EINPROGRESS)
493         {
494             h->event = CS_CONNECT;
495             h->state = CS_ST_CONNECTING;
496             h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
497             return 1;
498         }
499 #endif
500         h->cerrno = CSYSERR;
501         return -1;
502     }
503     h->event = CS_CONNECT;
504     h->state = CS_ST_CONNECTING;
505
506     return tcpip_rcvconnect (h);
507 }
508
509 /*
510  * nop
511  */
512 int tcpip_rcvconnect(COMSTACK h)
513 {
514 #if ENABLE_SSL
515     tcpip_state *sp = (tcpip_state *)h->cprivate;
516 #endif
517     TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
518
519     if (h->state == CS_ST_DATAXFER)
520         return 0;
521     if (h->state != CS_ST_CONNECTING)
522     {
523         h->cerrno = CSOUTSTATE;
524         return -1;
525     }
526 #if ENABLE_SSL
527     if (h->type == ssl_type && !sp->ctx)
528     {
529         SSL_library_init();
530         SSL_load_error_strings();
531
532         sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_client_method());
533         if (!sp->ctx)
534         {
535             h->cerrno = CSERRORSSL;
536             return -1;
537         }
538     }
539     if (sp->ctx)
540     {
541         int res;
542
543         if (!sp->ssl)
544         {
545             sp->ssl = SSL_new (sp->ctx);
546             SSL_set_fd (sp->ssl, h->iofile);
547         }
548         res = SSL_connect(sp->ssl);
549 #if HAVE_OPENSSL_SSL_H
550         if (res <= 0)
551         {
552             int err = SSL_get_error(sp->ssl, res);
553             if (err == SSL_ERROR_WANT_READ)
554             {
555                 h->io_pending = CS_WANT_READ;
556                 return 1;
557             }
558             if (err == SSL_ERROR_WANT_WRITE)
559             {
560                 h->io_pending = CS_WANT_WRITE;
561                 return 1;
562             }
563             h->cerrno = CSERRORSSL;
564             return -1;
565         }
566 #else
567         TRC(fprintf(stderr, "SSL_connect res=%d last_error=%d\n",
568                     res, sp->ssl->last_error));
569         if (res == 0 && sp->ssl->last_error == GNUTLS_E_AGAIN)
570         {
571             h->io_pending = CS_WANT_READ;
572             return 1;
573         }
574         else if (res <= 0)
575         {
576             h->cerrno = CSERRORSSL;
577             return -1;
578         }
579 #endif
580     }
581 #endif
582     h->event = CS_DATA;
583     h->state = CS_ST_DATAXFER;
584     return 0;
585 }
586
587 #define CERTF "ztest.pem"
588 #define KEYF "ztest.pem"
589
590 static void tcpip_setsockopt (int fd)
591 {
592 #if 0
593     int len = 4096;
594     int set = 1;
595     
596     if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
597     {
598         yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
599     }
600     if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
601     {
602         yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
603     }
604     if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
605     {
606         yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
607     }
608 #endif
609 }
610
611 static int tcpip_bind(COMSTACK h, void *address, int mode)
612 {
613     int r;
614     tcpip_state *sp = (tcpip_state *)h->cprivate;
615 #if HAVE_GETADDRINFO
616 #else
617     struct sockaddr *addr = (struct sockaddr *)address;
618 #endif
619 #ifdef WIN32
620     BOOL one = 1;
621 #else
622     int one = 1;
623 #endif
624
625 #if HAVE_GETADDRINFO
626     if (sp->ai != (struct addrinfo *) address)
627     {
628         h->cerrno = CSOUTSTATE;
629         return -1;
630     }
631 #endif
632
633 #if ENABLE_SSL
634     if (h->type == ssl_type && !sp->ctx)
635     {
636         SSL_library_init();
637         SSL_load_error_strings();
638
639         sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_server_method());
640         if (!sp->ctx)
641         {
642             h->cerrno = CSERRORSSL;
643             return -1;
644         }
645     }
646     if (sp->ctx)
647     {
648         if (sp->ctx_alloc)
649         {
650             int res;
651             res = SSL_CTX_use_certificate_file(sp->ctx, sp->cert_fname,
652                                                SSL_FILETYPE_PEM);
653             if (res <= 0)
654             {
655 #if HAVE_OPENSSL_SSL_H
656                 ERR_print_errors_fp(stderr);
657 #else
658                 fprintf(stderr, " SSL_CTX_use_certificate_file %s failed\n",
659                         sp->cert_fname);
660 #endif
661                 exit (2);
662             }
663             res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
664                                                SSL_FILETYPE_PEM);
665             if (res <= 0)
666             {
667 #if HAVE_OPENSSL_SSL_H
668                 ERR_print_errors_fp(stderr);
669 #else
670                 fprintf(stderr, " SSL_CTX_use_certificate_file %s failed\n",
671                         sp->cert_fname);
672 #endif
673                 exit (3);
674             }
675 #if HAVE_OPENSSL_SSL_H
676             res = SSL_CTX_check_private_key (sp->ctx);
677             if (res <= 0)
678             {
679                 ERR_print_errors_fp(stderr);
680                 exit(5);
681             }
682 #endif
683         }
684         TRC (fprintf (stderr, "ssl_bind\n"));
685     }
686     else
687     {
688         TRC (fprintf (stderr, "tcpip_bind\n"));
689     }
690 #else
691     TRC (fprintf (stderr, "tcpip_bind\n"));
692 #endif
693 #ifndef WIN32
694     if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*) 
695         &one, sizeof(one)) < 0)
696     {
697         h->cerrno = CSYSERR;
698         return -1;
699     }
700 #endif
701     tcpip_setsockopt(h->iofile);
702 #if HAVE_GETADDRINFO
703     r = bind(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
704     freeaddrinfo(sp->ai);
705     sp->ai = 0;
706 #else
707     r = bind(h->iofile, addr, sizeof(struct sockaddr_in));
708 #endif
709     if (r)
710     {
711         h->cerrno = CSYSERR;
712         return -1;
713     }
714     /* Allow a maximum-sized backlog of waiting-to-connect clients */
715     if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0)
716     {
717         h->cerrno = CSYSERR;
718         return -1;
719     }
720     h->state = CS_ST_IDLE;
721     h->event = CS_LISTEN;
722     return 0;
723 }
724
725 int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
726                  int (*check_ip)(void *cd, const char *a, int len, int t),
727                  void *cd)
728 {
729     struct sockaddr_in addr;
730     YAZ_SOCKLEN_T len = sizeof(addr);
731
732     TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
733     if (h->state != CS_ST_IDLE)
734     {
735         h->cerrno = CSOUTSTATE;
736         return -1;
737     }
738     h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
739     if (h->newfd < 0)
740     {
741         if (
742 #ifdef WIN32
743             WSAGetLastError() == WSAEWOULDBLOCK
744 #else
745             yaz_errno() == EWOULDBLOCK 
746 #ifdef EAGAIN
747 #if EAGAIN != EWOULDBLOCK
748             || yaz_errno() == EAGAIN
749 #endif
750 #endif
751 #endif
752             )
753             h->cerrno = CSNODATA;
754         else
755         {
756 #ifdef WIN32
757             shutdown(h->iofile, SD_RECEIVE);
758 #else
759             shutdown(h->iofile, SHUT_RD);
760 #endif
761             listen(h->iofile, SOMAXCONN);
762             h->cerrno = CSYSERR;
763         }
764         return -1;
765     }
766     if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
767         memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
768     else if (addrlen)
769         *addrlen = 0;
770     if (check_ip && (*check_ip)(cd, (const char *) &addr,
771         sizeof(addr), AF_INET))
772     {
773         h->cerrno = CSDENY;
774 #ifdef WIN32
775         closesocket(h->newfd);
776 #else
777         close(h->newfd);
778 #endif
779         h->newfd = -1;
780         return -1;
781     }
782     h->state = CS_ST_INCON;
783     tcpip_setsockopt (h->newfd);
784     return 0;
785 }
786
787 COMSTACK tcpip_accept(COMSTACK h)
788 {
789     COMSTACK cnew;
790     tcpip_state *state, *st = (tcpip_state *)h->cprivate;
791 #ifdef WIN32
792     unsigned long tru = 1;
793 #endif
794
795     TRC(fprintf(stderr, "tcpip_accept\n"));
796     if (h->state == CS_ST_INCON)
797     {
798         if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
799         {
800             h->cerrno = CSYSERR;
801 #ifdef WIN32
802             closesocket(h->newfd);
803 #else
804             close(h->newfd);
805 #endif
806             h->newfd = -1;
807             return 0;
808         }
809         memcpy(cnew, h, sizeof(*h));
810         cnew->iofile = h->newfd;
811         cnew->io_pending = 0;
812         if (!(state = (tcpip_state *)
813               (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
814         {
815             h->cerrno = CSYSERR;
816             if (h->newfd != -1)
817             {
818 #ifdef WIN32
819                 closesocket(h->newfd);
820 #else
821                 close(h->newfd);
822 #endif
823                 h->newfd = -1;
824             }
825             return 0;
826         }
827         if (!tcpip_set_blocking(cnew, cnew->flags))
828         {
829             h->cerrno = CSYSERR;
830             if (h->newfd != -1)
831             {
832 #ifdef WIN32
833                 closesocket(h->newfd);
834 #else
835                 close(h->newfd);
836 #endif
837                 h->newfd = -1;
838             }
839             xfree (cnew);
840             xfree (state);
841             return 0;
842         }
843         h->newfd = -1;
844         state->altbuf = 0;
845         state->altsize = state->altlen = 0;
846         state->towrite = state->written = -1;
847         state->complete = st->complete;
848 #if HAVE_GETADDRINFO
849         state->ai = 0;
850 #endif
851         cnew->state = CS_ST_ACCEPT;
852         h->state = CS_ST_IDLE;
853         
854 #if ENABLE_SSL
855         state->ctx = st->ctx;
856         state->ctx_alloc = 0;
857         state->ssl = st->ssl;
858         if (state->ctx)
859         {
860             state->ssl = SSL_new (state->ctx);
861             SSL_set_fd (state->ssl, cnew->iofile);
862         }
863 #endif
864         state->connect_request_buf = 0;
865         state->connect_response_buf = 0;
866         h = cnew;
867     }
868     if (h->state == CS_ST_ACCEPT)
869     {
870 #if ENABLE_SSL
871         tcpip_state *state = (tcpip_state *)h->cprivate;
872         if (state->ctx)
873         {
874             int res;
875             TRC(fprintf(stderr, "SSL_accept\n"));
876             res = SSL_accept (state->ssl);
877 #if HAVE_OPENSSL_SSL_H
878             if (res <= 0)
879             {
880                 int err = SSL_get_error(state->ssl, res);
881                 if (err == SSL_ERROR_WANT_READ)
882                 {
883                     h->io_pending = CS_WANT_READ;
884                     return h;
885                 }
886                 if (err == SSL_ERROR_WANT_WRITE)
887                 {
888                     h->io_pending = CS_WANT_WRITE;
889                     return h;
890                 }
891                 cs_close (h);
892                 return 0;
893             }
894 #else
895             TRC(fprintf(stderr, "SSL_accept res=%d last_error=%d\n",
896                         res, state->ssl->last_error));
897             if (res == 0 && state->ssl->last_error == GNUTLS_E_AGAIN)
898             {
899                 h->io_pending = CS_WANT_READ;
900                 return h;
901             }
902             else if (res <= 0) /* assume real error */
903             {
904                 cs_close(h);
905                 return 0;
906             }
907 #endif
908         }
909 #endif
910     }
911     else
912     {
913         h->cerrno = CSOUTSTATE;
914         return 0;
915     }
916     h->io_pending = 0;
917     h->state = CS_ST_DATAXFER;
918     h->event = CS_DATA;
919     return h;
920 }
921
922 #define CS_TCPIP_BUFCHUNK 4096
923
924 /*
925  * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
926  * 0=connection closed.
927  */
928 int tcpip_get(COMSTACK h, char **buf, int *bufsize)
929 {
930     tcpip_state *sp = (tcpip_state *)h->cprivate;
931     char *tmpc;
932     int tmpi, berlen, rest, req, tomove;
933     int hasread = 0, res;
934
935     TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
936     if (sp->altlen) /* switch buffers */
937     {
938         TRC(fprintf(stderr, "  %d bytes in altbuf (0x%x)\n", sp->altlen,
939             (unsigned) sp->altbuf));
940         tmpc = *buf;
941         tmpi = *bufsize;
942         *buf = sp->altbuf;
943         *bufsize = sp->altsize;
944         hasread = sp->altlen;
945         sp->altlen = 0;
946         sp->altbuf = tmpc;
947         sp->altsize = tmpi;
948     }
949     h->io_pending = 0;
950     while (!(berlen = (*sp->complete)(*buf, hasread)))
951     {
952         if (!*bufsize)
953         {
954             if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
955             {
956                 h->cerrno = CSYSERR;
957                 return -1;
958             }
959         }
960         else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
961             if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
962             {
963                 h->cerrno = CSYSERR;
964                 return -1;
965             }
966 #ifdef __sun__
967         yaz_set_errno( 0 );
968         /* unfortunatly, sun sometimes forgets to set errno in recv
969            when EWOULDBLOCK etc. would be required (res = -1) */
970 #endif
971         res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
972         TRC(fprintf(stderr, "  recv res=%d, hasread=%d\n", res, hasread));
973         if (res < 0)
974         {
975             TRC(fprintf(stderr, "  recv errno=%d, (%s)\n", yaz_errno(), 
976                       strerror(yaz_errno())));
977 #ifdef WIN32
978             if (WSAGetLastError() == WSAEWOULDBLOCK)
979             {
980                 h->io_pending = CS_WANT_READ;
981                 break;
982             }
983             else
984             {
985                 h->cerrno = CSYSERR;
986                 return -1;
987             }
988 #else
989             if (yaz_errno() == EWOULDBLOCK 
990 #ifdef EAGAIN   
991 #if EAGAIN != EWOULDBLOCK
992                 || yaz_errno() == EAGAIN
993 #endif
994 #endif
995                 || yaz_errno() == EINPROGRESS
996 #ifdef __sun__
997                 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this */
998 #endif
999                 )
1000             {
1001                 h->io_pending = CS_WANT_READ;
1002                 break;
1003             }
1004             else if (yaz_errno() == 0)
1005                 continue;
1006             else
1007             {
1008                 h->cerrno = CSYSERR;
1009                 return -1;
1010             }
1011 #endif
1012         }
1013         else if (!res)
1014             return hasread;
1015         hasread += res;
1016         if (hasread > h->max_recv_bytes)
1017         {
1018             h->cerrno = CSBUFSIZE;
1019             return -1;
1020         }
1021     }
1022     TRC (fprintf (stderr, "  Out of read loop with hasread=%d, berlen=%d\n",
1023                   hasread, berlen));
1024     /* move surplus buffer (or everything if we didn't get a BER rec.) */
1025     if (hasread > berlen)
1026     {
1027         tomove = req = hasread - berlen;
1028         rest = tomove % CS_TCPIP_BUFCHUNK;
1029         if (rest)
1030             req += CS_TCPIP_BUFCHUNK - rest;
1031         if (!sp->altbuf)
1032         {
1033             if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1034             {
1035                 h->cerrno = CSYSERR;
1036                 return -1;
1037             }
1038         } else if (sp->altsize < req)
1039             if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1040             {
1041                 h->cerrno = CSYSERR;
1042                 return -1;
1043             }
1044         TRC(fprintf(stderr, "  Moving %d bytes to altbuf(0x%x)\n", tomove,
1045             (unsigned) sp->altbuf));
1046         memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1047     }
1048     if (berlen < CS_TCPIP_BUFCHUNK - 1)
1049         *(*buf + berlen) = '\0';
1050     return berlen ? berlen : 1;
1051 }
1052
1053
1054 #if ENABLE_SSL
1055 /*
1056  * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
1057  * 0=connection closed.
1058  */
1059 int ssl_get(COMSTACK h, char **buf, int *bufsize)
1060 {
1061     tcpip_state *sp = (tcpip_state *)h->cprivate;
1062     char *tmpc;
1063     int tmpi, berlen, rest, req, tomove;
1064     int hasread = 0, res;
1065
1066     TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
1067     if (sp->altlen) /* switch buffers */
1068     {
1069         TRC(fprintf(stderr, "  %d bytes in altbuf (0x%x)\n", sp->altlen,
1070             (unsigned) sp->altbuf));
1071         tmpc = *buf;
1072         tmpi = *bufsize;
1073         *buf = sp->altbuf;
1074         *bufsize = sp->altsize;
1075         hasread = sp->altlen;
1076         sp->altlen = 0;
1077         sp->altbuf = tmpc;
1078         sp->altsize = tmpi;
1079     }
1080     h->io_pending = 0;
1081     while (!(berlen = (*sp->complete)(*buf, hasread)))
1082     {
1083         if (!*bufsize)
1084         {
1085             if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
1086                 return -1;
1087         }
1088         else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
1089             if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
1090                 return -1;
1091         res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
1092         TRC(fprintf(stderr, "  SSL_read res=%d, hasread=%d\n", res, hasread));
1093         if (res <= 0)
1094         {
1095             int ssl_err = SSL_get_error(sp->ssl, res);
1096             if (ssl_err == SSL_ERROR_WANT_READ)
1097             {
1098                 h->io_pending = CS_WANT_READ;
1099                 break;
1100             }
1101             if (ssl_err == SSL_ERROR_WANT_WRITE)
1102             {
1103                 h->io_pending = CS_WANT_WRITE;
1104                 break;
1105             }
1106             if (res == 0)
1107                 return 0;
1108             h->cerrno = CSERRORSSL;
1109             return -1;
1110         }
1111         hasread += res;
1112     }
1113     TRC (fprintf (stderr, "  Out of read loop with hasread=%d, berlen=%d\n",
1114         hasread, berlen));
1115     /* move surplus buffer (or everything if we didn't get a BER rec.) */
1116     if (hasread > berlen)
1117     {
1118         tomove = req = hasread - berlen;
1119         rest = tomove % CS_TCPIP_BUFCHUNK;
1120         if (rest)
1121             req += CS_TCPIP_BUFCHUNK - rest;
1122         if (!sp->altbuf)
1123         {
1124             if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
1125                 return -1;
1126         } else if (sp->altsize < req)
1127             if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
1128                 return -1;
1129         TRC(fprintf(stderr, "  Moving %d bytes to altbuf(0x%x)\n", tomove,
1130             (unsigned) sp->altbuf));
1131         memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
1132     }
1133     if (berlen < CS_TCPIP_BUFCHUNK - 1)
1134         *(*buf + berlen) = '\0';
1135     return berlen ? berlen : 1;
1136 }
1137 #endif
1138
1139 /*
1140  * Returns 1, 0 or -1
1141  * In nonblocking mode, you must call again with same buffer while
1142  * return value is 1.
1143  */
1144 int tcpip_put(COMSTACK h, char *buf, int size)
1145 {
1146     int res;
1147     struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1148
1149     TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
1150     h->io_pending = 0;
1151     h->event = CS_DATA;
1152     if (state->towrite < 0)
1153     {
1154         state->towrite = size;
1155         state->written = 0;
1156     }
1157     else if (state->towrite != size)
1158     {
1159         h->cerrno = CSWRONGBUF;
1160         return -1;
1161     }
1162     while (state->towrite > state->written)
1163     {
1164         if ((res =
1165              send(h->iofile, buf + state->written, size -
1166                   state->written, 
1167 #ifdef MSG_NOSIGNAL
1168                   MSG_NOSIGNAL
1169 #else
1170                   0
1171 #endif
1172                  )) < 0)
1173         {
1174             if (
1175 #ifdef WIN32
1176                 WSAGetLastError() == WSAEWOULDBLOCK
1177 #else
1178                 yaz_errno() == EWOULDBLOCK 
1179 #ifdef EAGAIN
1180 #if EAGAIN != EWOULDBLOCK
1181              || yaz_errno() == EAGAIN
1182 #endif
1183 #endif
1184 #ifdef __sun__
1185                 || yaz_errno() == ENOENT /* Sun's sometimes set errno to this value! */
1186 #endif
1187                 || yaz_errno() == EINPROGRESS
1188 #endif
1189                 )
1190             {
1191                 TRC(fprintf(stderr, "  Flow control stop\n"));
1192                 h->io_pending = CS_WANT_WRITE;
1193                 return 1;
1194             }
1195             h->cerrno = CSYSERR;
1196             return -1;
1197         }
1198         state->written += res;
1199         TRC(fprintf(stderr, "  Wrote %d, written=%d, nbytes=%d\n",
1200                     res, state->written, size));
1201     }
1202     state->towrite = state->written = -1;
1203     TRC(fprintf(stderr, "  Ok\n"));
1204     return 0;
1205 }
1206
1207
1208 #if ENABLE_SSL
1209 /*
1210  * Returns 1, 0 or -1
1211  * In nonblocking mode, you must call again with same buffer while
1212  * return value is 1.
1213  */
1214 int ssl_put(COMSTACK h, char *buf, int size)
1215 {
1216     int res;
1217     struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1218
1219     TRC(fprintf(stderr, "ssl_put: size=%d\n", size));
1220     h->io_pending = 0;
1221     h->event = CS_DATA;
1222     if (state->towrite < 0)
1223     {
1224         state->towrite = size;
1225         state->written = 0;
1226     }
1227     else if (state->towrite != size)
1228     {
1229         h->cerrno = CSWRONGBUF;
1230         return -1;
1231     }
1232     while (state->towrite > state->written)
1233     {
1234         res = SSL_write (state->ssl, buf + state->written,
1235                          size - state->written);
1236         if (res <= 0)
1237         {
1238             int ssl_err = SSL_get_error(state->ssl, res);
1239             if (ssl_err == SSL_ERROR_WANT_READ)
1240             {
1241                 h->io_pending = CS_WANT_READ;
1242                 return 1;
1243             }
1244             if (ssl_err == SSL_ERROR_WANT_WRITE)
1245             {
1246                 h->io_pending = CS_WANT_WRITE;
1247                 return 1;
1248             }
1249             h->cerrno = CSERRORSSL;
1250             return -1;
1251         }
1252         state->written += res;
1253         TRC(fprintf(stderr, "  Wrote %d, written=%d, nbytes=%d\n",
1254                     res, state->written, size));
1255     }
1256     state->towrite = state->written = -1;
1257     TRC(fprintf(stderr, "  Ok\n"));
1258     return 0;
1259 }
1260 #endif
1261
1262 int tcpip_close(COMSTACK h)
1263 {
1264     tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1265
1266     TRC(fprintf(stderr, "tcpip_close\n"));
1267     if (h->iofile != -1)
1268     {
1269 #if ENABLE_SSL
1270         if (sp->ssl)
1271         {
1272             SSL_shutdown (sp->ssl);
1273         }
1274 #endif
1275 #ifdef WIN32
1276         closesocket(h->iofile);
1277 #else
1278         close(h->iofile);
1279 #endif
1280     }
1281     if (sp->altbuf)
1282         xfree(sp->altbuf);
1283 #if ENABLE_SSL
1284     if (sp->ssl)
1285     {
1286         TRC (fprintf(stderr, "SSL_free\n"));
1287         SSL_free (sp->ssl);
1288     }
1289     sp->ssl = 0;
1290     if (sp->ctx_alloc)
1291         SSL_CTX_free (sp->ctx_alloc);
1292 #endif
1293 #if HAVE_GETADDRINFO
1294     if (sp->ai)
1295         freeaddrinfo(sp->ai);
1296 #endif
1297     xfree(sp->connect_request_buf);
1298     xfree(sp->connect_response_buf);
1299     xfree(sp);
1300     xfree(h);
1301     return 0;
1302 }
1303
1304 char *tcpip_addrstr(COMSTACK h)
1305 {
1306     tcpip_state *sp = (struct tcpip_state *)h->cprivate;
1307     char *r = 0, *buf = sp->buf;
1308
1309 #if HAVE_GETADDRINFO
1310     char host[120];
1311     struct sockaddr_storage addr;
1312     YAZ_SOCKLEN_T len = sizeof(addr);
1313     
1314     if (getpeername(h->iofile, (struct sockaddr *)&addr, &len) < 0)
1315     {
1316         h->cerrno = CSYSERR;
1317         return 0;
1318     }
1319     if (getnameinfo((struct sockaddr *) &addr, len, host, sizeof(host)-1, 
1320                     0, 0, 
1321                     (h->flags & CS_FLAGS_NUMERICHOST) ? NI_NUMERICHOST : 0))
1322     {
1323         r = "unknown";
1324     }
1325     else
1326         r = host;
1327     
1328 #else
1329
1330     struct sockaddr_in addr;
1331     YAZ_SOCKLEN_T len = sizeof(addr);
1332     struct hostent *host;
1333     
1334     if (getpeername(h->iofile, (struct sockaddr*) &addr, &len) < 0)
1335     {
1336         h->cerrno = CSYSERR;
1337         return 0;
1338     }
1339     if (!(h->flags & CS_FLAGS_NUMERICHOST))
1340     {
1341         if ((host = gethostbyaddr((char*)&addr.sin_addr,
1342                                   sizeof(addr.sin_addr),
1343                                   AF_INET)))
1344             r = (char*) host->h_name;
1345     }
1346     if (!r)
1347         r = inet_ntoa(addr.sin_addr);        
1348 #endif
1349
1350     if (h->protocol == PROTO_HTTP)
1351         sprintf(buf, "http:%s", r);
1352     else
1353         sprintf(buf, "tcp:%s", r);
1354 #if ENABLE_SSL
1355     if (sp->ctx)
1356     {
1357         if (h->protocol == PROTO_HTTP)
1358             sprintf(buf, "https:%s", r);
1359         else
1360             sprintf(buf, "ssl:%s", r);
1361     }
1362 #endif
1363     return buf;
1364 }
1365
1366 int static tcpip_set_blocking(COMSTACK p, int flags)
1367 {
1368     unsigned long flag;
1369     
1370 #ifdef WIN32
1371     flag = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
1372     if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
1373         return 0;
1374 #else
1375     flag = fcntl(p->iofile, F_GETFL, 0);
1376     if (flags & CS_FLAGS_BLOCKING)
1377         flag = flag & ~O_NONBLOCK;  /* blocking */
1378     else
1379     {
1380         flag = flag | O_NONBLOCK;   /* non-blocking */
1381         signal(SIGPIPE, SIG_IGN);
1382     }
1383     if (fcntl(p->iofile, F_SETFL, flag) < 0)
1384         return 0;
1385 #endif
1386     p->flags = flags;
1387     return 1;
1388 }
1389
1390 #if ENABLE_SSL
1391 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1392 {
1393     struct tcpip_state *sp;
1394     if (!cs || cs->type != ssl_type)
1395         return 0;
1396     sp = (struct tcpip_state *) cs->cprivate;
1397     if (sp->ctx_alloc)
1398         return 0;
1399     sp->ctx = (SSL_CTX *) ctx;
1400     return 1;
1401 }
1402
1403 void *cs_get_ssl(COMSTACK cs)
1404 {
1405     struct tcpip_state *sp;
1406     if (!cs || cs->type != ssl_type)
1407         return 0;
1408     sp = (struct tcpip_state *) cs->cprivate;
1409     return sp->ssl;  
1410 }
1411
1412 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1413 {
1414     struct tcpip_state *sp;
1415     if (!cs || cs->type != ssl_type)
1416         return 0;
1417     sp = (struct tcpip_state *) cs->cprivate;
1418     strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
1419     sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
1420     return 1;
1421 }
1422
1423 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1424 {
1425     SSL *ssl = (SSL *) cs_get_ssl(cs);
1426     if (ssl)
1427     {
1428 #if HAVE_OPENSSL_SSL_H
1429         X509 *server_cert = SSL_get_peer_certificate (ssl);
1430         if (server_cert)
1431         {
1432             BIO *bio = BIO_new(BIO_s_mem());
1433             char *pem_buf;
1434             /* get PEM buffer in memory */
1435             PEM_write_bio_X509(bio, server_cert);
1436             *len = BIO_get_mem_data(bio, &pem_buf);
1437             *buf = (char *) xmalloc(*len);
1438             memcpy(*buf, pem_buf, *len);
1439             BIO_free(bio);
1440             return 1;
1441         }
1442 #endif
1443     }
1444     return 0;
1445 }
1446 #else
1447 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
1448 {
1449     return 0;
1450 }
1451
1452 void *cs_get_ssl(COMSTACK cs)
1453 {
1454     return 0;
1455 }
1456
1457 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
1458 {
1459     return 0;
1460 }
1461
1462 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
1463 {
1464     return 0;
1465 }
1466 #endif
1467
1468
1469 static int tcpip_put_connect(COMSTACK h, char *buf, int size)
1470 {
1471     struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1472
1473     int r = tcpip_put(h, state->connect_request_buf,
1474                       state->connect_request_len);
1475     if (r == 0)
1476     {
1477         /* it's sent */
1478         h->f_put = tcpip_put; /* switch to normal tcpip put */
1479         r = tcpip_put(h, buf, size);
1480     }
1481     return r;
1482 }
1483
1484 static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize)
1485 {
1486     struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
1487     int r;
1488
1489     r = tcpip_get(h, &state->connect_response_buf, 
1490                   &state->connect_response_len);
1491     if (r < 1)
1492         return r;
1493     /* got the connect response completely */
1494     state->complete = cs_complete_auto; /* switch to normal tcpip get */
1495     h->f_get = tcpip_get;
1496     return tcpip_get(h, buf, bufsize);
1497 }
1498
1499
1500 /*
1501  * Local variables:
1502  * c-basic-offset: 4
1503  * indent-tabs-mode: nil
1504  * End:
1505  * vim: shiftwidth=4 tabstop=8 expandtab
1506  */
1507