* Replaced serv_gets() with serv_getln() - which now requires the caller
[citadel.git] / webcit / tcp_sockets.c
index bb20bbd2ff1d91622e2b1b88cf2f5a1971bb8596..6880cd28b4c3fe98887d07921eb70a9f3a4c08ef 100644 (file)
@@ -1,11 +1,14 @@
 /*
- * tcp_sockets.c
+ * $Id$
  * 
- * TCP socket module for WebCit
+ * TCP client 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>
@@ -54,7 +57,7 @@ int uds_connectsock(char *sockpath)
 
        memset(&addr, 0, sizeof(addr));
        addr.sun_family = AF_UNIX;
-       safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
+       strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
 
        s = socket(AF_UNIX, SOCK_STREAM, 0);
        if (s < 0) {
@@ -66,6 +69,7 @@ int uds_connectsock(char *sockpath)
        if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
                lprintf(1, "Can't connect: %s\n",
                        strerror(errno));
+               close(s);
                return(-1);
        }
 
@@ -119,6 +123,7 @@ int tcp_connectsock(char *host, char *service)
        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);
                return (-1);
        }
        alarm(0);
@@ -143,8 +148,11 @@ void serv_read(char *buf, int bytes)
                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;
+                       memset(buf, 0, bytes);
                        return;
                }
                len = len + rlen;
@@ -155,7 +163,7 @@ void serv_read(char *buf, int bytes)
 /*
  * input string from pipe
  */
-void serv_gets(char *strbuf)
+void serv_getln(char *strbuf, int bufsize)
 {
        int ch, len;
        char buf[2];
@@ -166,9 +174,12 @@ void serv_gets(char *strbuf)
                serv_read(&buf[0], 1);
                ch = buf[0];
                strbuf[len++] = ch;
-       } while ((ch != 10) && (ch != 13) && (ch != 0) && (len < 255));
-       strbuf[len - 1] = 0;
-       /* lprintf(9, ">%s\n", strbuf); */
+       } while ((ch != 10) && (ch != 0) && (len < (bufsize-1)));
+       if (strbuf[len-1] == 10) strbuf[--len] = 0;
+       if (strbuf[len-1] == 13) strbuf[--len] = 0;
+#ifdef SERV_TRACE
+       lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
+#endif
 }
 
 
@@ -186,6 +197,8 @@ void serv_write(char *buf, int nbytes)
                if (retval < 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;
                        return;
@@ -202,6 +215,9 @@ void serv_puts(char *string)
 {
        char buf[SIZ];
 
+#ifdef SERV_TRACE
+       lprintf(9, "%3d<%s\n", WC->serv_sock, string);
+#endif
        sprintf(buf, "%s\n", string);
        serv_write(buf, strlen(buf));
 }
@@ -216,10 +232,12 @@ 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
 }