* typedef wcsession, so we don't always need to say gcc again its a struct.
[citadel.git] / webcit / tcp_sockets.c
index 42df6b96f90799888e48a1d05036b5cea458b60f..8c603ee1e720fa300961485c54341faf33426c1b 100644 (file)
@@ -1,41 +1,21 @@
 /*
- * tcp_sockets.c
- * 
- * TCP socket module for WebCit
- *
  * $Id$
  */
 
+/*
+ * Uncomment this to log all communications with the Citadel server
+#define SERV_TRACE 1
+ */
+
 
-#include <ctype.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/socket.h>
-#include <sys/time.h>
-#include <limits.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <sys/un.h>
-#include <netdb.h>
-#include <string.h>
-#include <pwd.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <pthread.h>
-#include <signal.h>
 #include "webcit.h"
 #include "webserver.h"
 
-
-#ifndef INADDR_NONE
-#define INADDR_NONE 0xffffffff
-#endif
-
+/*
+ *  register the timeout
+ *  signum signalhandler number
+ * \return signals
+ */
 RETSIGTYPE timeout(int signum)
 {
        lprintf(1, "Connection timed out.\n");
@@ -43,9 +23,9 @@ RETSIGTYPE timeout(int signum)
 }
 
 
-
 /*
- * Connect a unix domain socket
+ *  Connect a unix domain socket
+ *  sockpath where to open a unix domain socket
  */
 int uds_connectsock(char *sockpath)
 {
@@ -75,7 +55,9 @@ int uds_connectsock(char *sockpath)
 
 
 /*
- * Connect a TCP/IP socket
+ *  Connect a TCP/IP socket
+ *  host the host to connect to
+ *  service the service on the host to call
  */
 int tcp_connectsock(char *host, char *service)
 {
@@ -133,22 +115,25 @@ int tcp_connectsock(char *host, char *service)
 
 
 /*
- * Input binary data from socket
+ *  Input binary data from socket
+ *  buf the buffer to get the input to
+ *  bytes the maximal number of bytes to read
  */
-void serv_read(char *buf, int bytes)
+inline void _serv_read(char *buf, int bytes, wcsession *WCC)
 {
        int len, rlen;
 
        len = 0;
        while (len < bytes) {
-               rlen = read(WC->serv_sock, &buf[len], bytes - len);
+               rlen = read(WCC->serv_sock, &buf[len], bytes - len);
                if (rlen < 1) {
                        lprintf(1, "Server connection broken: %s\n",
                                strerror(errno));
-                       close(WC->serv_sock);
-                       WC->serv_sock = (-1);
-                       WC->connected = 0;
-                       WC->logged_in = 0;
+                       wc_backtrace();
+                       close(WCC->serv_sock);
+                       WCC->serv_sock = (-1);
+                       WCC->connected = 0;
+                       WCC->logged_in = 0;
                        memset(buf, 0, bytes);
                        return;
                }
@@ -156,33 +141,79 @@ void serv_read(char *buf, int bytes)
        }
 }
 
+void serv_read(char *buf, int bytes)
+{
+       wcsession *WCC = WC;
+       _serv_read(buf, bytes, WCC);
+}
 
 /*
- * input string from pipe
+ *  input string from pipe
  */
-void serv_gets(char *strbuf)
+int serv_getln(char *strbuf, int bufsize)
 {
+       wcsession *WCC = WC;
        int ch, len;
        char buf[2];
 
        len = 0;
-       strcpy(strbuf, "");
+       strbuf[0] = 0;
        do {
-               serv_read(&buf[0], 1);
+               _serv_read(&buf[0], 1, WCC);
                ch = buf[0];
-               strbuf[len++] = ch;
-       } while ((ch != 10) && (ch != 0) && (len < (SIZ-1)));
-       if (strbuf[len-1] == 10) strbuf[--len] = 0;
-       if (strbuf[len-1] == 13) strbuf[--len] = 0;
-       /* lprintf(9, ">%s\n", strbuf); */
+               if ((ch != 13) && (ch != 10)) {
+                       strbuf[len++] = ch;
+               }
+       } while ((ch != 10) && (ch != 0) && (len < (bufsize-1)));
+       strbuf[len] = 0;
+#ifdef SERV_TRACE
+       lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
+#endif
+       return len;
 }
 
+int StrBuf_ServGetln(StrBuf *buf)
+{
+       const char *ErrStr;
+       int rc;
+
+       rc = StrBufTCP_read_line(buf, &WC->serv_sock, 0, &ErrStr);
+       if (rc < 0)
+       {
+               lprintf(1, "Server connection broken: %s\n",
+                       ErrStr);
+               wc_backtrace();
+               WC->serv_sock = (-1);
+               WC->connected = 0;
+               WC->logged_in = 0;
+       }
+       return rc;
+}
 
+int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
+{
+       const char *Err;
+       int rc;
+       
+       rc = StrBufReadBLOB(buf, &WC->serv_sock, 1, BlobSize, &Err);
+       if (rc < 0)
+       {
+               lprintf(1, "Server connection broken: %s\n",
+                       Err);
+               wc_backtrace();
+               WC->serv_sock = (-1);
+               WC->connected = 0;
+               WC->logged_in = 0;
+       }
+       return rc;
+}
 
 /*
- * send binary to server
+ *  send binary to server
+ *  buf the buffer to write to citadel server
+ *  nbytes how many bytes to send to citadel server
  */
-void serv_write(char *buf, int nbytes)
+void serv_write(const char *buf, int nbytes)
 {
        int bytes_written = 0;
        int retval;
@@ -204,30 +235,53 @@ void serv_write(char *buf, int nbytes)
 
 
 /*
- * send line to server
+ *  send line to server
+ *  string the line to send to the citadel server
  */
-void serv_puts(char *string)
+void serv_puts(const char *string)
 {
-       char buf[SIZ];
+#ifdef SERV_TRACE
+       lprintf(9, "%3d<%s\n", WC->serv_sock, string);
+#endif
+       serv_write(string, strlen(string));
+       serv_write("\n", 1);
+}
 
-       sprintf(buf, "%s\n", string);
-       serv_write(buf, strlen(buf));
+/*
+ *  send line to server
+ *  string the line to send to the citadel server
+ */
+void serv_putbuf(const StrBuf *string)
+{
+#ifdef SERV_TRACE
+       lprintf(9, "%3d<%s\n", WC->serv_sock, ChrPtr(string));
+#endif
+       serv_write(ChrPtr(string), StrLength(string));
+       serv_write("\n", 1);
 }
 
 
 /*
- * convenience function to send stuff to the server
+ *  convenience function to send stuff to the server
+ *  format the formatstring
+ *  ... the entities to insert into format 
  */
 void serv_printf(const char *format,...)
 {
        va_list arg_ptr;
        char buf[SIZ];
+       size_t len;
 
        va_start(arg_ptr, format);
        vsnprintf(buf, sizeof buf, format, arg_ptr);
        va_end(arg_ptr);
 
-       strcat(buf, "\n");
-       serv_write(buf, strlen(buf));
-       /* lprintf(9, "<%s", buf); */
+       len = strlen(buf);
+       buf[len++] = '\n';
+       buf[len] = '\0';
+       serv_write(buf, len);
+#ifdef SERV_TRACE
+       lprintf(9, "<%s", buf);
+#endif
 }
+