* use strbuffer as wprintf backend
[citadel.git] / webcit / tcp_sockets.c
index 9501a79fb9b7ff88e45e064eb98e314fa9c109f0..fa5429d32b8099f4381af5d51e4863fe9b5ece04 100644 (file)
@@ -1,49 +1,36 @@
 /*
- * tcp_sockets.c
- * 
- * TCP socket module for WebCit
- *
  * $Id$
  */
+/** 
+ * \defgroup TcpSockets TCP client socket module for WebCit
+ * \ingroup CitadelCommunitacion
+ */
+/*@{*/
 
+/*
+ * 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"
 
-#ifndef INADDR_NONE
-#define INADDR_NONE 0xffffffff
-#endif
+#include "webcit.h"
+#include "webserver.h"
 
+/**
+ * \brief register the timeout
+ * \param signum signalhandler number
+ * \return signals
+ */
 RETSIGTYPE timeout(int signum)
 {
-       fprintf(stderr, "Connection timed out.\n");
+       lprintf(1, "Connection timed out.\n");
        exit(3);
 }
 
 
-
-/*
- * Connect a unix domain socket
+/**
+ * \brief Connect a unix domain socket
+ * \param sockpath where to open a unix domain socket
  */
 int uds_connectsock(char *sockpath)
 {
@@ -56,14 +43,15 @@ int uds_connectsock(char *sockpath)
 
        s = socket(AF_UNIX, SOCK_STREAM, 0);
        if (s < 0) {
-               fprintf(stderr, "Can't create socket: %s\n",
+               lprintf(1, "Can't create socket: %s\n",
                        strerror(errno));
                return(-1);
        }
 
        if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-               fprintf(stderr, "can't connect: %s\n",
+               lprintf(1, "Can't connect: %s\n",
                        strerror(errno));
+               close(s);
                return(-1);
        }
 
@@ -71,8 +59,10 @@ int uds_connectsock(char *sockpath)
 }
 
 
-/*
- * Connect a TCP/IP socket
+/**
+ * \brief Connect a TCP/IP socket
+ * \param host the host to connect to
+ * \param service the service on the host to call
  */
 int tcp_connectsock(char *host, char *service)
 {
@@ -89,34 +79,35 @@ int tcp_connectsock(char *host, char *service)
        if (pse) {
                sin.sin_port = pse->s_port;
        } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
-               fprintf(stderr, "Can't get %s service entry\n", service);
+               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) {
-               fprintf(stderr, "Can't get %s host entry: %s\n",
+               lprintf(1, "Can't get %s host entry: %s\n",
                        host, strerror(errno));
                return (-1);
        }
        if ((ppe = getprotobyname("tcp")) == 0) {
-               fprintf(stderr, "Can't get TCP protocol entry: %s\n",
+               lprintf(1, "Can't get TCP protocol entry: %s\n",
                        strerror(errno));
                return (-1);
        }
 
        s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
        if (s < 0) {
-               fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
+               lprintf(1, "Can't create socket: %s\n", strerror(errno));
                return (-1);
        }
        signal(SIGALRM, timeout);
        alarm(30);
 
        if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
-               fprintf(stderr, "can't connect to %s.%s: %s\n",
+               lprintf(1, "Can't connect to %s.%s: %s\n",
                        host, service, strerror(errno));
+               close(s);
                return (-1);
        }
        alarm(0);
@@ -128,51 +119,104 @@ int tcp_connectsock(char *host, char *service)
 
 
 
-/*
- * Input binary data from socket
+/**
+ * \brief Input binary data from socket
+ * \param buf the buffer to get the input to
+ * \param bytes the maximal number of bytes to read
  */
-void serv_read(char *buf, int bytes)
+inline void _serv_read(char *buf, int bytes, struct 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) {
-                       fprintf(stderr, "Server connection broken: %s\n",
+                       lprintf(1, "Server connection broken: %s\n",
                                strerror(errno));
-                       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;
                }
                len = len + rlen;
        }
 }
 
+void serv_read(char *buf, int bytes)
+{
+       struct wcsession *WCC = WC;
+       _serv_read(buf, bytes, WCC);
+}
 
-/*
- * input string from pipe
+/**
+ * \brief input string from pipe
  */
-void serv_gets(char *strbuf)
+int serv_getln(char *strbuf, int bufsize)
 {
+       struct 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 != 13) && (ch != 0) && (len < 255));
-       strbuf[len - 1] = 0;
-       /* fprintf(stderr, ">%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
+/**
+ * \brief send binary to server
+ * \param buf the buffer to write to citadel server
+ * \param nbytes how many bytes to send to citadel server
  */
 void serv_write(char *buf, int nbytes)
 {
@@ -182,8 +226,10 @@ void serv_write(char *buf, int nbytes)
                retval = write(WC->serv_sock, &buf[bytes_written],
                               nbytes - bytes_written);
                if (retval < 1) {
-                       fprintf(stderr, "Server connection broken: %s\n",
+                       lprintf(1, "Server connection broken: %s\n",
                                strerror(errno));
+                       close(WC->serv_sock);
+                       WC->serv_sock = (-1);
                        WC->connected = 0;
                        WC->logged_in = 0;
                        return;
@@ -193,31 +239,43 @@ void serv_write(char *buf, int nbytes)
 }
 
 
-/*
- * send line to server
+/**
+ * \brief send line to server
+ * \param string the line to send to the citadel server
  */
 void serv_puts(char *string)
 {
-       char buf[256];
-
-       sprintf(buf, "%s\n", string);
-       serv_write(buf, strlen(buf));
+#ifdef SERV_TRACE
+       lprintf(9, "%3d<%s\n", WC->serv_sock, string);
+#endif
+       serv_write(string, strlen(string));
+       serv_write("\n", 1);
 }
 
 
-/*
- * convenience function to send stuff to the server
+/**
+ * \brief convenience function to send stuff to the server
+ * \param format the formatstring
+ * \param ... the entities to insert into format 
  */
 void serv_printf(const char *format,...)
 {
        va_list arg_ptr;
-       char buf[256];
+       char buf[SIZ];
+       size_t len;
 
        va_start(arg_ptr, format);
-       vsprintf(buf, format, arg_ptr);
+       vsnprintf(buf, sizeof buf, format, arg_ptr);
        va_end(arg_ptr);
 
-       strcat(buf, "\n");
-       serv_write(buf, strlen(buf));
-       /* fprintf(stderr, "<%s", buf); */
+       len = strlen(buf);
+       buf[len++] = '\n';
+       buf[len] = '\0';
+       serv_write(buf, len);
+#ifdef SERV_TRACE
+       lprintf(9, "<%s", buf);
+#endif
 }
+
+
+/*@}*/