]> andersk Git - moira.git/blobdiff - update/sendrecv.c
Command line printer manipulation client, and build goo.
[moira.git] / update / sendrecv.c
index 7321319efd0625b3560a92181cee5d536605c652..f82dc90f48aa9d86e35d9ff9c61f2e66fbaed590 100644 (file)
@@ -10,6 +10,7 @@
 #include <mit-copyright.h>
 #include <moira.h>
 
+#include <sys/types.h>
 #include <sys/uio.h>
 
 #include <errno.h>
@@ -18,8 +19,8 @@
 
 RCSID("$Header$");
 
-#define putlong(cp, l) { cp[0] = l >> 24; cp[1] = l >> 16; cp[2] = l >> 8; cp[3] = l; }
-#define getlong(cp, l) l = ((cp[0] * 256 + cp[1]) * 256 + cp[2]) * 256 + cp[3]
+#define putlong(cp, l) { ((unsigned char *)cp)[0] = l >> 24; ((unsigned char *)cp)[1] = l >> 16; ((unsigned char *)cp)[2] = l >> 8; ((unsigned char *)cp)[3] = l; }
+#define getlong(cp, l) l = ((((unsigned char *)cp)[0] * 256 + ((unsigned char *)cp)[1]) * 256 + ((unsigned char *)cp)[2]) * 256 + ((unsigned char *)cp)[3]
 
 extern void fail(int conn, int err, char *msg);
 
@@ -41,12 +42,19 @@ int send_int(int conn, long data)
 int recv_int(int conn, long *data)
 {
   char buf[8];
+  int len;
 
-  if (read(conn, buf, 8) != 8)
+  len = read(conn, buf, 8);
+  if (len == -1)
     {
       fail(conn, errno, "reading integer");
       return errno;
     }
+  else if (len < 8)
+    {
+      fail(conn, 0, "remote connection closed while reading integer");
+      return EIO;
+    }
   getlong((buf + 4), *data);
   return 0;
 }
@@ -78,17 +86,31 @@ int send_string(int conn, char *buf, size_t len)
 int recv_string(int conn, char **buf, size_t *len)
 {
   char tmp[4];
+  int size, more;
 
-  if (read(conn, tmp, 4) != 4)
+  size = read(conn, tmp, 4);
+  if (size == -1)
     {
       fail(conn, errno, "reading string");
       return errno;
     }
-  if (read(conn, tmp, 4) != 4)
+  else if (size < 4)
+    {
+      fail(conn, 0, "remote connection closed while reading string");
+      return EIO;
+    }
+
+  size = read(conn, tmp, 4);
+  if (size == -1)
     {
       fail(conn, errno, "reading string");
       return errno;
     }
+  else if (size < 4)
+    {
+      fail(conn, 0, "remote connection closed while reading string");
+      return EIO;
+    }
   getlong(tmp, *len);
 
   *buf = malloc(*len);
@@ -97,7 +119,14 @@ int recv_string(int conn, char **buf, size_t *len)
       fail(conn, ENOMEM, "reading string");
       return ENOMEM;
     }
-  if (read(conn, *buf, *len) != *len)
+  for (size = 0; size < *len; size += more)
+    {
+      more = read(conn, *buf + size, *len - size);
+      if (!more)
+       break;
+    }
+  
+  if (size != *len)
     {
       free(buf);
       fail(conn, errno, "reading string");
This page took 0.179793 seconds and 4 git commands to generate.