]> code.citadel.org Git - citadel.git/blobdiff - webcit/tcp_sockets.c
Remove the intermediate buffer in serv_puts()
[citadel.git] / webcit / tcp_sockets.c
index b3c853d31f9d2b0b8c709ce302bb64f9c5853ba8..ed5ab54af16bd3dc8a0a3742154a19ad554807a3 100644 (file)
@@ -1,41 +1,25 @@
 /*
- * 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"
 #include "webserver.h"
 
-
-#ifndef INADDR_NONE
-#define INADDR_NONE 0xffffffff
-#endif
-
+/**
+ * \brief register the timeout
+ * \param signum signalhandler number
+ * \return signals
+ */
 RETSIGTYPE timeout(int signum)
 {
        lprintf(1, "Connection timed out.\n");
@@ -43,9 +27,9 @@ RETSIGTYPE timeout(int signum)
 }
 
 
-
-/*
- * 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)
 {
@@ -74,8 +58,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)
 {
@@ -132,8 +118,10 @@ 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)
 {
@@ -149,6 +137,7 @@ void serv_read(char *buf, int bytes)
                        WC->serv_sock = (-1);
                        WC->connected = 0;
                        WC->logged_in = 0;
+                       memset(buf, 0, bytes);
                        return;
                }
                len = len + rlen;
@@ -156,30 +145,35 @@ void serv_read(char *buf, int bytes)
 }
 
 
-/*
- * input string from pipe
+/**
+ * \brief input string from pipe
  */
-void serv_gets(char *strbuf)
+void serv_getln(char *strbuf, int bufsize)
 {
        int ch, len;
        char buf[2];
 
        len = 0;
-       strcpy(strbuf, "");
+       strbuf[0] = 0;
        do {
                serv_read(&buf[0], 1);
                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
 }
 
 
 
-/*
- * 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)
 {
@@ -202,20 +196,24 @@ 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[SIZ];
-
-       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,...)
 {
@@ -223,10 +221,15 @@ void serv_printf(const char *format,...)
        char buf[SIZ];
 
        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));
-       /* lprintf(9, "<%s", buf); */
+#ifdef SERV_TRACE
+       lprintf(9, "<%s", buf);
+#endif
 }
+
+
+/*@}*/