]> code.citadel.org Git - citadel.git/blobdiff - webcit/tcp_sockets.c
Performed a bunch of markup fixes suggested by validator.w3.org
[citadel.git] / webcit / tcp_sockets.c
index fa37c18d3a7ff42f0264cb8f4e02427480f54144..440178396e7eb67dabb0ebdb19f44d4fdc205928 100644 (file)
@@ -1,9 +1,7 @@
 /*
- * $Id$
+ * Copyright (c) 1987-2011 by the citadel.org team
  *
- * Copyright (c) 1987-2010 by the citadel.org team
- *
- * This program is free software; you can redistribute it and/or modify
+ * This program is open source 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.
@@ -112,6 +110,7 @@ int tcp_connectsock(char *host, char *service)
        rc = getaddrinfo(host, service, &hints, &res);
        if (rc != 0) {
                lprintf(1, "%s: %s\n", host, gai_strerror(rc));
+               freeaddrinfo(res);
                return(-1);
        }
 
@@ -127,10 +126,12 @@ int tcp_connectsock(char *host, char *service)
                s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
                if (s < 0) {
                        lprintf(1, "socket() failed: %s\n", strerror(errno));
+                       freeaddrinfo(res);
                        return(-1);
                }
-               rc = connect(s, res->ai_addr, res->ai_addrlen);
+               rc = connect(s, ai->ai_addr, ai->ai_addrlen);
                if (rc >= 0) {
+                       freeaddrinfo(res);
                        return(s);
                }
                else {
@@ -138,7 +139,7 @@ int tcp_connectsock(char *host, char *service)
                        close(s);
                }
        }
-
+        freeaddrinfo(res);
        return(-1);
 }
 
@@ -191,9 +192,9 @@ int StrBuf_ServGetln(StrBuf *buf)
 #ifdef SERV_TRACE
        else 
        {
-               long pos=0;
+               long pos = 0;
                if (WCC->ReadPos != NULL)
-                       pos = WCC->ReadPos - ChrPtr(buf);
+                       pos = WCC->ReadPos - ChrPtr(WCC->ReadBuf);
                lprintf(9, "%3d<<<[%ld]%s\n", WC->serv_sock, pos, ChrPtr(buf));
        }
 #endif
@@ -256,6 +257,42 @@ int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
        return rc;
 }
 
+
+void FlushReadBuf (void)
+{
+       long len;
+       const char *pch;
+       const char *pche;
+       wcsession *WCC = WC;
+
+       len = StrLength(WCC->ReadBuf);
+       if ((len > 0) &&
+           (WCC->ReadPos != NULL) && 
+           (WCC->ReadPos != StrBufNOTNULL))
+               
+       {
+               pch = ChrPtr(WCC->ReadBuf);
+               pche = pch + len;
+               if (WCC->ReadPos != pche)
+               {
+                       lprintf(1, "ERROR: somebody didn't eat his soup! Remaing Chars: %d [%s]\n", 
+                               pche - WCC->ReadPos, pche);
+                       lprintf(1, 
+                               "--------------------------------------------------------------------------------\n"
+                               "Whole buf: [%s]\n"
+                               "--------------------------------------------------------------------------------\n", 
+                               pch);
+                       AppendImportantMessage(HKEY("Suppenkasper alert! watch your webcit logfile and get connected to your favourite opensource Crew."));
+               }
+       }
+
+       FlushStrBuf(WCC->ReadBuf);
+       WCC->ReadPos = NULL;
+
+
+}
+
+
 /*
  *  send binary to server
  *  buf the buffer to write to citadel server
@@ -267,8 +304,7 @@ void serv_write(const char *buf, int nbytes)
        int bytes_written = 0;
        int retval;
 
-       FlushStrBuf(WCC->ReadBuf);
-       WCC->ReadPos = NULL;
+       FlushReadBuf();
        while (bytes_written < nbytes) {
                retval = write(WCC->serv_sock, &buf[bytes_written],
                               nbytes - bytes_written);
@@ -293,12 +329,10 @@ void serv_write(const char *buf, int nbytes)
  */
 void serv_puts(const char *string)
 {
-       wcsession *WCC = WC;
 #ifdef SERV_TRACE
        lprintf(9, "%3d>>>%s\n", WC->serv_sock, string);
 #endif
-       FlushStrBuf(WCC->ReadBuf);
-       WCC->ReadPos = NULL;
+       FlushReadBuf();
 
        serv_write(string, strlen(string));
        serv_write("\n", 1);
@@ -310,12 +344,10 @@ void serv_puts(const char *string)
  */
 void serv_putbuf(const StrBuf *string)
 {
-       wcsession *WCC = WC;
 #ifdef SERV_TRACE
        lprintf(9, "%3d>>>%s\n", WC->serv_sock, ChrPtr(string));
 #endif
-       FlushStrBuf(WCC->ReadBuf);
-       WCC->ReadPos = NULL;
+       FlushReadBuf();
 
        serv_write(ChrPtr(string), StrLength(string));
        serv_write("\n", 1);
@@ -329,13 +361,11 @@ void serv_putbuf(const StrBuf *string)
  */
 void serv_printf(const char *format,...)
 {
-       wcsession *WCC = WC;
        va_list arg_ptr;
        char buf[SIZ];
        size_t len;
 
-       FlushStrBuf(WCC->ReadBuf);
-       WCC->ReadPos = NULL;
+       FlushReadBuf();
 
        va_start(arg_ptr, format);
        vsnprintf(buf, sizeof buf, format, arg_ptr);
@@ -351,116 +381,45 @@ void serv_printf(const char *format,...)
 }
 
 
-
-/**
- * Read binary data from server into memory using a series of
- * server READ commands.
- * \return the read content as StrBuf
+/*
+ * Read binary data from server into memory using a series of server READ commands.
+ * returns 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;
+       size_t bytes_read = 0;
+       size_t this_block = 0;
+       int rc;
 
-       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;
+       if (Ret == NULL) {
+               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; 
-                       }
+
+       while (bytes_read < total_len) {
+
+               if (WCC->serv_sock==-1) {
+                       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;
-                   }
+
+               serv_printf("READ %d|%d", bytes_read, total_len-bytes_read);
+               if ( (StrBuf_ServGetln(Buf) > 0) && (GetServerStatus(Buf, NULL) == 6) ) {
+                       StrBufCutLeft(Buf, 4);
+                       this_block = StrTol(Buf);
+                       rc = StrBuf_ServGetBLOBBuffered(Ret, this_block);
+                       if (rc < 0) {
+                               lprintf(1, "Server connection broken during download\n");
+                               wc_backtrace();
+                               WCC->serv_sock = (-1);
+                               WCC->connected = 0;
+                               WCC->logged_in = 0;
+                               return rc;
+                       }
+                       bytes_read += rc;
                }
        }
+
        return StrLength(Ret);
 }
 
@@ -779,7 +738,7 @@ long end_burst(void)
                if (CompressBuffer(WCC->WBuf) > 0)
                        hprintf("Content-encoding: gzip\r\n");
                else {
-                       lprintf(CTDL_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
+                       syslog(LOG_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
                        wc_backtrace();
                }
        }