[or-cvs] move network_init from or/main to common/compat

Roger Dingledine arma at seul.org
Wed Dec 22 05:28:17 UTC 2004


Update of /home2/or/cvsroot/tor/src/common
In directory moria.mit.edu:/home2/arma/work/onion/cvs/tor/src/common

Modified Files:
	compat.c compat.h util.c util.h 
Log Message:
move network_init from or/main to common/compat
call network_init in tor-resolve.c too
move tor_lookup_hostname from common/util to common/compat


Index: compat.c
===================================================================
RCS file: /home2/or/cvsroot/tor/src/common/compat.c,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -d -r1.24 -r1.25
--- compat.c	22 Dec 2004 02:46:28 -0000	1.24
+++ compat.c	22 Dec 2004 05:28:15 -0000	1.25
@@ -55,6 +55,9 @@
 #ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 #endif
+#ifdef HAVE_NETDB_H
+#include <netdb.h>
+#endif
 #ifdef HAVE_SYS_PARAM_H
 #include <sys/param.h> /* FreeBSD needs this to know what version it is */
 #endif
@@ -75,6 +78,11 @@
 #include "strlcat.c"
 #endif
 
+/* used by inet_addr, not defined on solaris anywhere!? */
+#ifndef INADDR_NONE
+#define INADDR_NONE ((unsigned long) -1)
+#endif
+
 /** Replacement for snprintf.  Differs from platform snprintf in two
  * ways: First, always NUL-terminates its output.  Second, always
  * returns -1 if the result is truncated.  (Note that this return
@@ -473,6 +481,45 @@
 #endif
 }
 
+/** Similar behavior to Unix gethostbyname: resolve <b>name</b>, and set
+ * *addr to the proper IP address, in network byte order.  Returns 0
+ * on success, -1 on failure; 1 on transient failure.
+ *
+ * (This function exists because standard windows gethostbyname
+ * doesn't treat raw IP addresses properly.)
+ */
+int tor_lookup_hostname(const char *name, uint32_t *addr)
+{
+  /* Perhaps eventually this should be replaced by a tor_getaddrinfo or
+   * something.
+   */
+  struct in_addr iaddr;
+  struct hostent *ent;
+  tor_assert(addr);
+  if (!*name) {
+    /* Empty address is an error. */
+    return -1;
+  } else if (tor_inet_aton(name, &iaddr)) {
+    /* It's an IP. */
+    memcpy(addr, &iaddr.s_addr, 4);
+    return 0;
+  } else {
+    ent = gethostbyname(name);
+    if (ent) {
+      /* break to remind us if we move away from IPv4 */
+      tor_assert(ent->h_length == 4);
+      memcpy(addr, ent->h_addr, 4);
+      return 0;
+    }
+    memset(addr, 0, 4);
+#ifdef MS_WINDOWS
+    return (WSAGetLastError() == WSATRY_AGAIN) ? 1 : -1;
+#else
+    return (h_errno == TRY_AGAIN) ? 1 : -1;
+#endif
+  }
+}
+
 /* Hold the result of our call to <b>uname</b>. */
 static char uname_result[256];
 /* True iff uname_result is set. */
@@ -719,3 +766,22 @@
   return strerror(e);
 }
 #endif
+
+/** Called before we make any calls to network-related functions.
+ * (Some operating systems require their network libraries to be
+ * initialized.) */
+int network_init(void)
+{
+#ifdef MS_WINDOWS
+  /* This silly exercise is necessary before windows will allow gethostbyname to work.
+   */
+  WSADATA WSAData;
+  int r;
+  r = WSAStartup(0x101,&WSAData);
+  if (r) {
+    log_fn(LOG_WARN,"Error initializing windows network layer: code was %d",r);
+    return -1;
+  }
+#endif
+  return 0;
+}

Index: compat.h
===================================================================
RCS file: /home2/or/cvsroot/tor/src/common/compat.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- compat.h	22 Dec 2004 02:32:26 -0000	1.14
+++ compat.h	22 Dec 2004 05:28:15 -0000	1.15
@@ -117,8 +117,10 @@
 
 struct in_addr;
 int tor_inet_aton(const char *cp, struct in_addr *addr);
+int tor_lookup_hostname(const char *name, uint32_t *addr);
 void set_socket_nonblocking(int socket);
 int tor_socketpair(int family, int type, int protocol, int fd[2]);
+int network_init(void);
 /* 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

Index: util.c
===================================================================
RCS file: /home2/or/cvsroot/tor/src/common/util.c,v
retrieving revision 1.195
retrieving revision 1.196
diff -u -d -r1.195 -r1.196
--- util.c	8 Dec 2004 00:40:01 -0000	1.195
+++ util.c	22 Dec 2004 05:28:15 -0000	1.196
@@ -62,9 +62,6 @@
 #ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 #endif
-#ifdef HAVE_NETDB_H
-#include <netdb.h>
-#endif
 #ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
 #endif
@@ -81,11 +78,6 @@
 #include <fcntl.h>
 #endif
 
-/* used by inet_addr, not defined on solaris anywhere!? */
-#ifndef INADDR_NONE
-#define INADDR_NONE ((unsigned long) -1)
-#endif
-
 #ifndef O_BINARY
 #define O_BINARY 0
 #endif
@@ -698,8 +690,7 @@
 }
 
 /** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes
- * or reach the end of the file.
- * isSocket must be 1 if fd
+ * or reach the end of the file. <b>isSocket</b> must be 1 if fd
  * was returned by socket() or accept(), and 0 if fd was returned by
  * open().  Return the number of bytes read, or -1 on error. Only use
  * if fd is a blocking fd. */
@@ -1069,45 +1060,6 @@
   return is_internal_IP(ip);
 }
 
-/** Similar behavior to Unix gethostbyname: resolve <b>name</b>, and set
- * *addr to the proper IP address, in network byte order.  Returns 0
- * on success, -1 on failure; 1 on transient failure.
- *
- * (This function exists because standard windows gethostbyname
- * doesn't treat raw IP addresses properly.)
- */
-int tor_lookup_hostname(const char *name, uint32_t *addr)
-{
-  /* Perhaps eventually this should be replaced by a tor_getaddrinfo or
-   * something.
-   */
-  struct in_addr iaddr;
-  struct hostent *ent;
-  tor_assert(addr);
-  if (!*name) {
-    /* Empty address is an error. */
-    return -1;
-  } else if (tor_inet_aton(name, &iaddr)) {
-    /* It's an IP. */
-    memcpy(addr, &iaddr.s_addr, 4);
-    return 0;
-  } else {
-    ent = gethostbyname(name);
-    if (ent) {
-      /* break to remind us if we move away from IPv4 */
-      tor_assert(ent->h_length == 4);
-      memcpy(addr, ent->h_addr, 4);
-      return 0;
-    }
-    memset(addr, 0, 4);
-#ifdef MS_WINDOWS
-    return (WSAGetLastError() == WSATRY_AGAIN) ? 1 : -1;
-#else
-    return (h_errno == TRY_AGAIN) ? 1 : -1;
-#endif
-  }
-}
-
 /** Parse a string of the form "host[:port]" from <b>addrport</b>.  If
  * <b>address</b> is provided, set *<b>address</b> to a copy of the
  * host portion of the string.  If <b>addr</b> is provided, try to

Index: util.h
===================================================================
RCS file: /home2/or/cvsroot/tor/src/common/util.h,v
retrieving revision 1.126
retrieving revision 1.127
diff -u -d -r1.126 -r1.127
--- util.h	22 Dec 2004 02:46:28 -0000	1.126
+++ util.h	22 Dec 2004 05:28:15 -0000	1.127
@@ -115,7 +115,6 @@
 /* Net helpers */
 int is_internal_IP(uint32_t ip);
 int is_local_IP(uint32_t ip);
-int tor_lookup_hostname(const char *name, uint32_t *addr);
 int parse_addr_port(const char *addrport, char **address, uint32_t *addr,
                     uint16_t *port);
 int parse_addr_and_port_range(const char *s, uint32_t *addr_out,



More information about the tor-commits mailing list