* Rewrote webcit_tcp_server() to work with both IPv4 and IPv6 on host systems with...
[citadel.git] / webcit / tcp_sockets.c
index 9c2393b9f0d3c5acb32bb4215687828a8b28a4a7..6d558d13ab3db805aa95268f09bfa9d00fb16f3b 100644 (file)
@@ -1,5 +1,21 @@
 /*
  * $Id$
+ *
+ * Copyright (c) 1987-2010 by the citadel.org team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 /*
@@ -12,6 +28,7 @@
 #include "webserver.h"
 
 extern int DisableGzip;
+long MaxRead = -1; /* should we do READ scattered or all at once? */
 
 /*
  * register the timeout
@@ -24,8 +41,7 @@ RETSIGTYPE timeout(int signum)
 
 
 /*
- *  Connect a unix domain socket
- *  sockpath where to open a unix domain socket
+ * Client side - connect to a unix domain socket
  */
 int uds_connectsock(char *sockpath)
 {
@@ -38,16 +54,12 @@ int uds_connectsock(char *sockpath)
 
        s = socket(AF_UNIX, SOCK_STREAM, 0);
        if (s < 0) {
-               lprintf(1, "Can't create socket[%s]: %s\n",
-                       sockpath,
-                       strerror(errno));
+               lprintf(1, "Can't create socket[%s]: %s\n", sockpath, strerror(errno));
                return(-1);
        }
 
        if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-               lprintf(1, "Can't connect [%s]: %s\n",
-                       sockpath,
-                       strerror(errno));
+               lprintf(1, "Can't connect [%s]: %s\n", sockpath, strerror(errno));
                close(s);
                return(-1);
        }
@@ -57,80 +69,37 @@ int uds_connectsock(char *sockpath)
 
 
 /*
- *  Connect a TCP/IP socket
- *  host the host to connect to
- *  service the service on the host to call
+ * TCP client - connect to a host/port (FIXME this needs to be IPv6 enabled)
  */
-int tcp_connectsock(char *host, char *service)
+int tcp_connectsock(char *host, int port)
 {
-        int fdflags;
-       struct hostent *phe;
-       struct servent *pse;
-       struct protoent *ppe;
-       struct sockaddr_in sin;
-       int s;
-
-       memset(&sin, 0, sizeof(sin));
-       sin.sin_family = AF_INET;
-
-       pse = getservbyname(service, "tcp");
-       if (pse) {
-               sin.sin_port = pse->s_port;
-       } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
-               lprintf(1, "Can't get %s service entry\n", service);
-               return (-1);
-       }
-       phe = gethostbyname(host);
-       if (phe) {
-               memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
-       } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
-               lprintf(1, "Can't get %s host entry: %s\n",
-                       host, strerror(errno));
-               return (-1);
-       }
-       if ((ppe = getprotobyname("tcp")) == 0) {
-               lprintf(1, "Can't get TCP protocol entry: %s\n",
-                       strerror(errno));
-               return (-1);
-       }
+       struct sockaddr_in stSockAddr;
+       int rv;
+       int sock;
 
-       s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
-       if (s < 0) {
+       sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+       if (sock < 0) {
                lprintf(1, "Can't create socket: %s\n", strerror(errno));
                return (-1);
        }
 
-       fdflags = fcntl(s, F_GETFL);
-       if (fdflags < 0)
-               lprintf(1, "unable to get socket flags!  %s.%s: %s \n",
-                       host, service, strerror(errno));
-       fdflags = fdflags | O_NONBLOCK;
-       if (fcntl(s, F_SETFD, fdflags) < 0)
-               lprintf(1, "unable to set socket nonblocking flags!  %s.%s: %s \n",
-                       host, service, strerror(errno));
+       memset(&stSockAddr, 0, sizeof(struct sockaddr_in));
+       stSockAddr.sin_family = AF_INET;
+       stSockAddr.sin_port = htons(port);
+       rv = inet_pton(AF_INET, host, &stSockAddr.sin_addr);
 
-       signal(SIGALRM, timeout);
-       alarm(30);
-
-       if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
-               lprintf(1, "Can't connect to %s.%s: %s\n",
-                       host, service, strerror(errno));
-               close(s);
+       if (rv <= 0) {
+               lprintf(1, "Can't grok %s: %s\n", host, strerror(errno));
                return (-1);
        }
-       alarm(0);
-       signal(SIGALRM, SIG_IGN);
-       if (!is_https) {
-               fdflags = fcntl(s, F_GETFL);
-               if (fdflags < 0)
-                       lprintf(1, "unable to get socket flags!  %s.%s: %s \n",
-                               host, service, strerror(errno));
-               fdflags = fdflags | O_NONBLOCK;
-               if (fcntl(s, F_SETFD, fdflags) < 0)
-                       lprintf(1, "unable to set socket nonblocking flags!  %s.%s: %s \n",
-                               host, service, strerror(errno));
+
+       if (connect(sock, (const struct sockaddr *)&stSockAddr, sizeof(struct sockaddr_in)) != 0) {
+               lprintf(1, "Can't connect to %s.%d: %s\n", host, port, strerror(errno));
+               close(sock);
+               return (-1);
        }
-       return (s);
+
+       return (sock);
 }
 
 
@@ -152,7 +121,7 @@ int serv_getln(char *strbuf, int bufsize)
        FlushStrBuf(WCC->MigrateReadLineBuf);
        strbuf[len] = '\0';
 #ifdef SERV_TRACE
-       lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
+       lprintf(9, "%3d<<<%s\n", WC->serv_sock, strbuf);
 #endif
        return len;
 }
@@ -163,7 +132,8 @@ int StrBuf_ServGetln(StrBuf *buf)
        wcsession *WCC = WC;
        const char *ErrStr = NULL;
        int rc;
-
+       
+       FlushStrBuf(buf);
        rc = StrBufTCP_read_buffered_line_fast(buf, 
                                               WCC->ReadBuf, 
                                               &WCC->ReadPos, 
@@ -179,6 +149,15 @@ int StrBuf_ServGetln(StrBuf *buf)
                WCC->connected = 0;
                WCC->logged_in = 0;
        }
+#ifdef SERV_TRACE
+       else 
+       {
+               long pos=0;
+               if (WCC->ReadPos != NULL)
+                       pos = WCC->ReadPos - ChrPtr(buf);
+               lprintf(9, "%3d<<<[%ld]%s\n", WC->serv_sock, pos, ChrPtr(buf));
+       }
+#endif
        return rc;
 }
 
@@ -205,6 +184,11 @@ int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
                WCC->connected = 0;
                WCC->logged_in = 0;
        }
+#ifdef SERV_TRACE
+        else
+                lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
+#endif
+
        return rc;
 }
 
@@ -225,6 +209,11 @@ int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
                WCC->connected = 0;
                WCC->logged_in = 0;
        }
+#ifdef SERV_TRACE
+        else
+                lprintf(9, "%3d<<<BLOB: %ld bytes\n", WC->serv_sock, StrLength(buf));
+#endif
+
        return rc;
 }
 
@@ -267,7 +256,7 @@ void serv_puts(const char *string)
 {
        wcsession *WCC = WC;
 #ifdef SERV_TRACE
-       lprintf(9, "%3d<%s\n", WC->serv_sock, string);
+       lprintf(9, "%3d>>>%s\n", WC->serv_sock, string);
 #endif
        FlushStrBuf(WCC->ReadBuf);
        WCC->ReadPos = NULL;
@@ -284,7 +273,7 @@ void serv_putbuf(const StrBuf *string)
 {
        wcsession *WCC = WC;
 #ifdef SERV_TRACE
-       lprintf(9, "%3d<%s\n", WC->serv_sock, ChrPtr(string));
+       lprintf(9, "%3d>>>%s\n", WC->serv_sock, ChrPtr(string));
 #endif
        FlushStrBuf(WCC->ReadBuf);
        WCC->ReadPos = NULL;
@@ -318,19 +307,132 @@ void serv_printf(const char *format,...)
        buf[len] = '\0';
        serv_write(buf, len);
 #ifdef SERV_TRACE
-       lprintf(9, "<%s", buf);
+       lprintf(9, ">>>%s", buf);
 #endif
 }
 
 
 
+/**
+ * Read binary data from server into memory using a series of
+ * server READ commands.
+ * \return the read content as StrBuf
+ */
+int serv_read_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf) 
+{
+       wcsession *WCC = WC;
+       size_t bytes = 0;
+       size_t thisblock = 0;
+       
+       if (Ret == NULL)
+           return -1;
+
+       if (MaxRead == -1)
+       {
+               serv_printf("READ %d|"SIZE_T_FMT, 0, total_len);
+               if (StrBuf_ServGetln(Buf) > 0)
+               {
+                       long YetRead;
+                       const char *ErrStr;
+                       const char *pch;
+                       int rc;
+
+                       if (GetServerStatus(Buf, NULL) == 6)
+                       {
+                           StrBufCutLeft(Buf, 4);
+                           thisblock = StrTol(Buf);
+                           if (WCC->serv_sock==-1) {
+                                   FlushStrBuf(Ret); 
+                                   return -1; 
+                           }
+
+                           if (WCC->ReadPos != NULL) {
+                                   pch = ChrPtr(WCC->ReadBuf);
+
+                                   YetRead = WCC->ReadPos - pch;
+                                   if (YetRead > 0)
+                                   {
+                                           long StillThere;
+                                           
+                                           StillThere = StrLength(WCC->ReadBuf) - 
+                                                   YetRead;
+                                           
+                                           StrBufPlain(Ret, 
+                                                       WCC->ReadPos,
+                                                       StillThere);
+                                           total_len -= StillThere;
+                                   }
+                                   FlushStrBuf(WCC->ReadBuf);
+                                   WCC->ReadPos = NULL;
+                           } 
+                           if (total_len > 0)
+                           {
+                                   rc = StrBufReadBLOB(Ret, 
+                                                       &WCC->serv_sock, 
+                                                       1, 
+                                                       total_len,
+                                                       &ErrStr);
+                                   if (rc < 0)
+                                   {
+                                           lprintf(1, "Server connection broken: %s\n",
+                                                   (ErrStr)?ErrStr:"");
+                                           wc_backtrace();
+                                           WCC->serv_sock = (-1);
+                                           WCC->connected = 0;
+                                           WCC->logged_in = 0;
+                                           return rc;
+                                   }
+                                   else
+                                           return StrLength(Ret);
+                           }
+                           else 
+                                   return StrLength(Ret);
+                       }
+               }
+               else
+                       return -1;
+       }
+       else while ((WCC->serv_sock!=-1) &&
+              (bytes < total_len)) {
+               thisblock = MaxRead;
+               if ((total_len - bytes) < thisblock) {
+                       thisblock = total_len - bytes;
+                       if (thisblock == 0) {
+                               FlushStrBuf(Ret); 
+                               return -1; 
+                       }
+               }
+               serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
+               if (StrBuf_ServGetln(Buf) > 0)
+               {
+                       if (GetServerStatus(Buf, NULL) == 6)
+                       {
+                           StrBufCutLeft(Buf, 4);
+                           thisblock = StrTol(Buf);
+                           if (WCC->serv_sock==-1) {
+                                   FlushStrBuf(Ret); 
+                                   return -1; 
+                           }
+                           StrBuf_ServGetBLOBBuffered(Ret, thisblock);
+                           bytes += thisblock;
+                   }
+                   else {
+                           lprintf(3, "Error: %s\n", ChrPtr(Buf) + 4);
+                           return -1;
+                   }
+               }
+       }
+       return StrLength(Ret);
+}
+
 
 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
 {
-       const char *Error, *pch, *pchs;
+       const char *Error;
+#ifdef HAVE_OPENSSL
+       const char *pch, *pchs;
        int rlen, len, retval = 0;
 
-#ifdef HAVE_OPENSSL
        if (is_https) {
                int ntries = 0;
                if (StrLength(Hdr->ReadBuf) > 0) {
@@ -388,60 +490,88 @@ int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
                                                         &Error);
 }
 
+
 /* 
  * This is a generic function to set up a master socket for listening on
- * a TCP port.  The server shuts down if the bind fails.
+ * a TCP port.  The server shuts down if the bind fails.  (IPv4/IPv6 version)
  *
  * ip_addr     IP address to bind
  * port_number port number to bind
  * queue_len   number of incoming connections to allow in the queue
  */
-int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
+int webcit_tcp_server(char *ip_addr, int port_number, int queue_len)
 {
        struct protoent *p;
-       struct sockaddr_in sin;
-       int s, i;
-
-       memset(&sin, 0, sizeof(sin));
-       sin.sin_family = AF_INET;
-       if (ip_addr == NULL) {
-               sin.sin_addr.s_addr = INADDR_ANY;
-       } else {
-               sin.sin_addr.s_addr = inet_addr(ip_addr);
+       struct sockaddr_in6 sin6;
+       struct sockaddr_in sin4;
+       int s, i, b;
+       int ip_version = 6;
+
+       memset(&sin6, 0, sizeof(sin6));
+       memset(&sin4, 0, sizeof(sin4));
+       sin6.sin6_family = AF_INET6;
+       sin4.sin_family = AF_INET;
+
+       if (    (ip_addr == NULL)                                                       /* any IPv6 address */
+               || (IsEmptyStr(ip_addr))
+               || (!strcmp(ip_addr, "*"))
+       ) {
+               ip_version = 6;
+               sin6.sin6_addr = in6addr_any;
        }
-
-       if (sin.sin_addr.s_addr == INADDR_NONE) {
-               sin.sin_addr.s_addr = INADDR_ANY;
+       else if (!strcmp(ip_addr, "0.0.0.0"))                                           /* any IPv4 address */
+       {
+               ip_version = 4;
+               sin4.sin_addr.s_addr = INADDR_ANY;
+       }
+       else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':')))                     /* specific IPv4 */
+       {
+               ip_version = 4;
+               if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
+                       lprintf(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
+                       return (-WC_EXIT_BIND);
+               }
+       }
+       else                                                                            /* specific IPv6 */
+       {
+               ip_version = 6;
+               if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
+                       lprintf(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
+                       return (-WC_EXIT_BIND);
+               }
        }
 
        if (port_number == 0) {
                lprintf(1, "Cannot start: no port number specified.\n");
                return (-WC_EXIT_BIND);
        }
-       sin.sin_port = htons((u_short) port_number);
+       sin6.sin6_port = htons((u_short) port_number);
+       sin4.sin_port = htons((u_short) port_number);
 
        p = getprotobyname("tcp");
 
-       s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
+       s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
        if (s < 0) {
-               lprintf(1, "Can't create a socket: %s\n", strerror(errno));
+               lprintf(1, "Can't create a listening socket: %s\n", strerror(errno));
                return (-WC_EXIT_BIND);
        }
        /* Set some socket options that make sense. */
        i = 1;
        setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
 
-       #ifndef __APPLE__
-       fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
-                                         there should be a preceding F_GETFL
-                                         and a bitwise OR with the previous
-                                         fd flags */
-       #endif
-       
-       if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
+       if (ip_version == 6) {
+               b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
+       }
+       else {
+               b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
+       }
+
+       if (b < 0) {
                lprintf(1, "Can't bind: %s\n", strerror(errno));
                return (-WC_EXIT_BIND);
        }
+
+
        if (listen(s, queue_len) < 0) {
                lprintf(1, "Can't listen: %s\n", strerror(errno));
                return (-WC_EXIT_BIND);
@@ -450,13 +580,12 @@ int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
 }
 
 
-
 /*
  * Create a Unix domain socket and listen on it
  * sockpath - file name of the unix domain socket
  * queue_len - Number of incoming connections to allow in the queue
  */
-int ig_uds_server(char *sockpath, int queue_len)
+int webcit_uds_server(char *sockpath, int queue_len)
 {
        struct sockaddr_un addr;
        int s;
@@ -479,8 +608,7 @@ int ig_uds_server(char *sockpath, int queue_len)
 
        s = socket(AF_UNIX, SOCK_STREAM, 0);
        if (s < 0) {
-               lprintf(1, "webcit: Can't create a socket: %s\n",
-                       strerror(errno));
+               lprintf(1, "webcit: Can't create a unix domain socket: %s\n", strerror(errno));
                return (-WC_EXIT_BIND);
        }
 
@@ -572,6 +700,7 @@ int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
        if (retval < 0) {
                lprintf(2, "client_read() failed: %s\n",
                        Error);
+               wc_backtrace();
                return retval;
        }
 
@@ -617,6 +746,11 @@ long end_burst(void)
                }
        }
 
+       if (WCC->WFBuf != NULL) {
+               WildFireSerializePayload(WCC->WFBuf, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
+               FreeStrBuf(&WCC->WFBuf);
+       }
+
        if (WCC->Hdr->HR.prohibit_caching)
                hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
        hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));