]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Move read/write_all_to_socket into lib/net.
authorNick Mathewson <nickm@torproject.org>
Wed, 27 Jun 2018 14:50:24 +0000 (10:50 -0400)
committerNick Mathewson <nickm@torproject.org>
Wed, 27 Jun 2018 16:01:11 +0000 (12:01 -0400)
src/common/util.c
src/common/util.h
src/lib/net/.may_include
src/lib/net/socket.c
src/lib/net/socket.h

index 2d426bb136bf9699fca8c4824044af81b74d5052..940f25e2753df93313efec3f547f8efdf201c3cb 100644 (file)
@@ -1094,24 +1094,6 @@ write_all_to_fd(int fd, const char *buf, size_t count)
   return (ssize_t)count;
 }
 
-/** Write <b>count</b> bytes from <b>buf</b> to <b>sock</b>. Return the number
- * of bytes written, or -1 on error.  Only use if fd is a blocking fd.  */
-ssize_t
-write_all_to_socket(tor_socket_t fd, const char *buf, size_t count)
-{
-  size_t written = 0;
-  ssize_t result;
-  raw_assert(count < SSIZE_MAX);
-
-  while (written != count) {
-    result = tor_socket_send(fd, buf+written, count-written, 0);
-    if (result<0)
-      return -1;
-    written += result;
-  }
-  return (ssize_t)count;
-}
-
 /** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes or
  * reach the end of the file.  Return the number of bytes read, or -1 on
  * error. Only use if fd is a blocking fd. */
@@ -1137,31 +1119,6 @@ read_all_from_fd(int fd, char *buf, size_t count)
   return (ssize_t)numread;
 }
 
-/** Read from <b>sock</b> to <b>buf</b>, until we get <b>count</b> bytes or
- * reach the end of the file.  Return the number of bytes read, or -1 on
- * error. Only use if fd is a blocking fd. */
-ssize_t
-read_all_from_socket(tor_socket_t sock, char *buf, size_t count)
-{
-  size_t numread = 0;
-  ssize_t result;
-
-  if (count > SIZE_T_CEILING || count > SSIZE_MAX) {
-    errno = EINVAL;
-    return -1;
-  }
-
-  while (numread < count) {
-    result = tor_socket_recv(sock, buf+numread, count-numread, 0);
-    if (result<0)
-      return -1;
-    else if (result == 0)
-      break;
-    numread += result;
-  }
-  return (ssize_t)numread;
-}
-
 /*
  *    Filesystem operations.
  */
index 1cb3e73b32f944f12b970d3d12e52f9c1dc17940..b7ac2a176118f2a2dd307d050e7d1248f6eba9b9 100644 (file)
@@ -118,9 +118,7 @@ int format_time_interval(char *out, size_t out_len, long interval);
 
 /* File helpers */
 ssize_t write_all_to_fd(int fd, const char *buf, size_t count);
-ssize_t write_all_to_socket(tor_socket_t fd, const char *buf, size_t count);
 ssize_t read_all_from_fd(int fd, char *buf, size_t count);
-ssize_t read_all_from_socket(tor_socket_t fd, char *buf, size_t count);
 
 #define write_all(fd, buf, count, isSock) \
   ((isSock) ? write_all_to_socket((fd), (buf), (count)) \
index 479f852939e75118422358fbbe6c4a415addaac5..1458dad99081021305bc70dd1bde33bf3d2e0f8c 100644 (file)
@@ -5,6 +5,7 @@ ht.h
 lib/cc/*.h
 lib/container/*.h
 lib/ctime/*.h
+lib/err/*.h
 lib/lock/*.h
 lib/log/*.h
 lib/net/*.h
index 4c14a42896f9c0d2e8d37d418634d31f9460ca65..cd18d7083992d286ac0bc28df0364b6303cb73c3 100644 (file)
@@ -7,6 +7,7 @@
 #include "lib/net/socket.h"
 #include "lib/net/address.h"
 #include "lib/cc/compat_compiler.h"
+#include "lib/err/torerr.h"
 #include "lib/lock/compat_mutex.h"
 #include "lib/log/torlog.h"
 #include "lib/log/util_bug.h"
@@ -647,3 +648,46 @@ set_socket_nonblocking(tor_socket_t sock)
 
   return 0;
 }
+
+/** Read from <b>sock</b> to <b>buf</b>, until we get <b>count</b> bytes or
+ * reach the end of the file.  Return the number of bytes read, or -1 on
+ * error. Only use if fd is a blocking fd. */
+ssize_t
+read_all_from_socket(tor_socket_t sock, char *buf, size_t count)
+{
+  size_t numread = 0;
+  ssize_t result;
+
+  if (count > SIZE_T_CEILING || count > SSIZE_MAX) {
+    errno = EINVAL;
+    return -1;
+  }
+
+  while (numread < count) {
+    result = tor_socket_recv(sock, buf+numread, count-numread, 0);
+    if (result<0)
+      return -1;
+    else if (result == 0)
+      break;
+    numread += result;
+  }
+  return (ssize_t)numread;
+}
+
+/** Write <b>count</b> bytes from <b>buf</b> to <b>sock</b>. Return the number
+ * of bytes written, or -1 on error.  Only use if fd is a blocking fd.  */
+ssize_t
+write_all_to_socket(tor_socket_t fd, const char *buf, size_t count)
+{
+  size_t written = 0;
+  ssize_t result;
+  raw_assert(count < SSIZE_MAX);
+
+  while (written != count) {
+    result = tor_socket_send(fd, buf+written, count-written, 0);
+    if (result<0)
+      return -1;
+    written += result;
+  }
+  return (ssize_t)count;
+}
index 93af92983a03d19a35a4f011aa2eac0e1ffaa315..99b0de4ff9382c06bbae25ae56355fe64c54c320 100644 (file)
@@ -52,6 +52,9 @@ int network_init(void);
 int get_max_sockets(void);
 void set_max_sockets(int);
 
+ssize_t write_all_to_socket(tor_socket_t fd, const char *buf, size_t count);
+ssize_t read_all_from_socket(tor_socket_t fd, char *buf, size_t count);
+
 /* For stupid historical reasons, windows sockets have an independent
  * set of errnos, and an independent way to get them.  Also, you can't
  * always believe WSAEWOULDBLOCK.  Use the macros below to compare