]> code.citadel.org Git - citadel.git/blobdiff - citadel/clientsocket.c
* Renamed "dynloader" to "serv_extensions" globally. We don't want people
[citadel.git] / citadel / clientsocket.c
index 286659aef946e8e4787c3253972ae740d196a51c..24aa21e6b974ef520a9cb3ffe17222017d62f652 100644 (file)
@@ -18,6 +18,7 @@
 #include <stdio.h>
 #include <signal.h>
 #include <sys/types.h>
+#include <sys/time.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <stdarg.h>
 #include "citadel.h"
 #include "server.h"
-#include "dynloader.h"
+#include "serv_extensions.h"
 #ifndef HAVE_SNPRINTF
 #include "snprintf.h"
 #endif
 #include "sysdep_decls.h"
+#include <clientsocket.h>
 
 #ifndef INADDR_NONE
 #define INADDR_NONE 0xffffffff
@@ -83,7 +85,7 @@ int sock_connect(char *host, char *service, char *protocol)
        }
 
        if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
-               lprintf(3, "can't connect to %s.%s: %s\n",
+               lprintf(3, "can't connect to %s:%s: %s\n",
                        host, service, strerror(errno));
                return(-1);
        }
@@ -91,23 +93,52 @@ int sock_connect(char *host, char *service, char *protocol)
        return (s);
 }
 
+
+
 /*
- * sock_read() - input binary data from socket.
+ * sock_read_to() - input binary data from socket, with a settable timeout.
  * Returns the number of bytes read, or -1 for error.
  */
-int sock_read(int sock, char *buf, int bytes)
+int sock_read_to(int sock, char *buf, int bytes, int timeout)
 {
-       int len, rlen;
+       int len,rlen;
+       fd_set rfds;
+       struct timeval tv;
+       int retval;
 
        len = 0;
-       while (len < bytes) {
-               rlen = read(sock, &buf[len], bytes - len);
-               if (rlen < 1) {
-                       return (-1);
+       while(len<bytes) {
+               FD_ZERO(&rfds);
+               FD_SET(sock, &rfds);
+               tv.tv_sec = timeout;
+               tv.tv_usec = 0;
+
+               retval = select(sock+1, &rfds, NULL, NULL, &tv);
+
+               if (FD_ISSET(sock, &rfds) == 0) {       /* timed out */
+                       lprintf(9, "sock_read() timed out.\n");
+                       return(-1);
+               }
+
+               rlen = read(sock, &buf[len], bytes-len);
+               if (rlen<1) {
+                       lprintf(2, "sock_read() failed: %s\n",
+                               strerror(errno));
+                       return(-1);
                }
                len = len + rlen;
        }
-       return (len);
+       return(bytes);
+}
+
+
+/*
+ * sock_read() - input binary data from socket.
+ * Returns the number of bytes read, or -1 for error.
+ */
+inline int sock_read(int sock, char *buf, int bytes)
+{
+       return sock_read_to(sock, buf, bytes, CLIENT_TIMEOUT);
 }
 
 
@@ -201,19 +232,3 @@ int sock_puts(int sock, char *buf)
        if (j<0) return(j);
        return(i+j);
 }
-
-
-/*
- * sock_puts_crlf() - same as sock_puts() but ends line with CRLF, not LF
- * Returns the number of bytes written, or -1 for error.
- */
-int sock_puts_crlf(int sock, char *buf)
-{
-       int i, j;
-
-       i = sock_write(sock, buf, strlen(buf));
-       if (i<0) return(i);
-       j = sock_write(sock, "\r\n", 2);
-       if (j<0) return(j);
-       return(i+j);
-}