mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-27 22:03:31 +01:00
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
This commit is contained in:
parent
036384fd8e
commit
cf17d0d29c
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
|
@ -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[]);
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user