diff options
author | Roger Dingledine <arma@torproject.org> | 2004-12-22 05:29:06 +0000 |
---|---|---|
committer | Roger Dingledine <arma@torproject.org> | 2004-12-22 05:29:06 +0000 |
commit | cf17d0d29c9526a0e9b6631693eb2bf0479e655c (patch) | |
tree | f2da64c5808b3120b5422d6e4948f73775976f92 /src | |
parent | 036384fd8eaf83354ed59abec1c9816abd82528c (diff) | |
download | tor-cf17d0d29c9526a0e9b6631693eb2bf0479e655c.tar.gz tor-cf17d0d29c9526a0e9b6631693eb2bf0479e655c.zip |
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
svn:r3203
Diffstat (limited to 'src')
-rw-r--r-- | src/common/compat.c | 66 | ||||
-rw-r--r-- | src/common/compat.h | 2 | ||||
-rw-r--r-- | src/common/util.c | 50 | ||||
-rw-r--r-- | src/common/util.h | 1 | ||||
-rw-r--r-- | src/or/main.c | 19 | ||||
-rw-r--r-- | src/or/or.h | 1 | ||||
-rw-r--r-- | src/tools/tor-resolve.c | 5 |
7 files changed, 74 insertions, 70 deletions
diff --git a/src/common/compat.c b/src/common/compat.c index bad94c3eea..b2be40312f 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -55,6 +55,9 @@ const char compat_c_id[] = "$Id$"; #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 @@ const char compat_c_id[] = "$Id$"; #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 @@ int tor_inet_aton(const char *c, struct in_addr* addr) #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 @@ const char *tor_socket_strerror(int e) 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; +} diff --git a/src/common/compat.h b/src/common/compat.h index 48163a95fc..dafd1339a5 100644 --- a/src/common/compat.h +++ b/src/common/compat.h @@ -117,8 +117,10 @@ int replace_file(const char *from, const char *to); 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 diff --git a/src/common/util.c b/src/common/util.c index 162fae680f..e4a2889d76 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -62,9 +62,6 @@ const char util_c_id[] = "$Id$"; #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 @@ const char util_c_id[] = "$Id$"; #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 @@ int write_all(int fd, const char *buf, size_t count, int isSocket) { } /** 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 @@ int is_local_IP(uint32_t ip) { 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 diff --git a/src/common/util.h b/src/common/util.h index a37018bf3d..691c05e40f 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -115,7 +115,6 @@ char *expand_filename(const char *filename); /* 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, diff --git a/src/or/main.c b/src/or/main.c index 1c2779066f..1b072cbe12 100644 --- a/src/or/main.c +++ b/src/or/main.c @@ -1026,25 +1026,6 @@ static void dumpstats(int severity) { rend_service_dump_stats(severity); } -/** 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; -} - /** Called by exit() as we shut down the process. */ static void exit_function(void) diff --git a/src/or/or.h b/src/or/or.h index 54f0d012ab..59bf131ce4 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -1390,7 +1390,6 @@ int proxy_mode(or_options_t *options); void handle_signals(int is_parent); void tor_cleanup(void); -int network_init(void); int tor_main(int argc, char *argv[]); diff --git a/src/tools/tor-resolve.c b/src/tools/tor-resolve.c index a6e4402ce7..f269ee63bb 100644 --- a/src/tools/tor-resolve.c +++ b/src/tools/tor-resolve.c @@ -211,6 +211,11 @@ main(int argc, char **argv) return 1; } + if (network_init()<0) { + log_fn(LOG_ERR,"Error initializing network; exiting."); + return -1; + } + if (do_resolve(arg[0], sockshost, socksport, &result)) return 1; |