From cdec339f112979fef20b288d87e3b071380cf857 Mon Sep 17 00:00:00 2001 From: Adam Hamsik Date: Thu, 26 Jan 2012 15:41:48 +0100 Subject: [PATCH] Remove THREAD_SAFE ifdefs and use macros for it. Is there any reason to not require threads by default ? --- src/core.c | 32 ++++++++++---------------------- src/core.h | 17 +++++++++++++---- src/libproxychains.c | 13 +++++++------ 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/core.c b/src/core.c index ab5a78b..dafb817 100644 --- a/src/core.c +++ b/src/core.c @@ -33,10 +33,6 @@ #include #include #include -#ifdef THREAD_SAFE -#include -pthread_mutex_t internal_ips_lock; -#endif #include "core.h" #include "common.h" @@ -48,7 +44,6 @@ extern unsigned int remote_dns_subnet; internal_ip_lookup_table internal_ips = {0, 0, NULL}; - uint32_t dalias_hash(char* s0) { unsigned char* s = (void*) s0; uint_fast32_t h = 0; @@ -69,16 +64,16 @@ uint32_t index_from_internal_ip(ip_type internalip) { char* string_from_internal_ip(ip_type internalip) { char* res = NULL; -#ifdef THREAD_SAFE - pthread_mutex_lock(&internal_ips_lock); -#endif + + proxychains_mutex_lock(&internal_ips_lock); + uint32_t index = index_from_internal_ip(internalip); if(index < internal_ips.counter) res = internal_ips.list[index]->string; -#ifdef THREAD_SAFE - pthread_mutex_unlock(&internal_ips_lock); -#endif - return res; + + proxychains_mutex_unlock(&internal_ips_lock); + + return res; } in_addr_t make_internal_ip(uint32_t index) { @@ -788,9 +783,7 @@ struct hostent* proxy_gethostbyname(const char *name) hash = dalias_hash((char*) name); -#ifdef THREAD_SAFE - pthread_mutex_lock(&internal_ips_lock); -#endif + proxychains_mutex_lock(&internal_ips_lock); // see if we already have this dns entry saved. if(internal_ips.counter) { @@ -837,10 +830,7 @@ struct hostent* proxy_gethostbyname(const char *name) have_ip: -#ifdef THREAD_SAFE - pthread_mutex_unlock(&internal_ips_lock); -#endif - + proxychains_mutex_unlock(&internal_ips_lock); strncpy(addr_name, name, sizeof(addr_name)); @@ -849,9 +839,7 @@ struct hostent* proxy_gethostbyname(const char *name) return &hostent_space; err_plus_unlock: -#ifdef THREAD_SAFE - pthread_mutex_unlock(&internal_ips_lock); -#endif + proxychains_mutex_unlock(&internal_ips_lock); return NULL; } int proxy_getaddrinfo(const char *node, const char *service, diff --git a/src/core.h b/src/core.h index ae57dc5..98764ba 100644 --- a/src/core.h +++ b/src/core.h @@ -37,10 +37,6 @@ typedef struct { } internal_ip_lookup_table; extern internal_ip_lookup_table internal_ips; -#ifdef THREAD_SAFE -#include -extern pthread_mutex_t internal_ips_lock; -#endif /*error codes*/ typedef enum { @@ -147,4 +143,17 @@ struct hostent* proxy_gethostbyname(const char *name); # define PDEBUG(fmt, args...) #endif +#ifdef THREAD_SAFE +#include +pthread_mutex_t internal_ips_lock; + +# define proxychains_mutex_lock(mutex) pthread_mutex_lock(mutex) +# define proxychains_mutex_unlock(mutex) pthread_mutex_unlock(mutex) +# define proxychains_mutex_init(mutex) pthread_mutex_init(mutex, NULL) +#else +# define proxychains_mutex_lock() +# define proxychains_mutex_unlock() +# define proxychains_mutex_init() +#endif + #endif diff --git a/src/libproxychains.c b/src/libproxychains.c index 960c993..35aab02 100644 --- a/src/libproxychains.c +++ b/src/libproxychains.c @@ -53,13 +53,13 @@ static int init_l = 0; unsigned int proxychains_proxy_count = 0; unsigned int proxychains_max_chain = 1; +unsigned int remote_dns_subnet = 224; localaddr_arg localnet_addr[MAX_LOCALNET]; chain_type proxychains_ct; proxy_data proxychains_pd[MAX_CHAIN]; size_t num_localnet_addr = 0; -unsigned int remote_dns_subnet = 224; static inline void get_chain_data(proxy_data *pd, unsigned int *proxy_count, chain_type *ct); @@ -68,12 +68,13 @@ static void init_lib(void); /* * Initialize libproxychains. */ -static void init_lib(void) +static void +init_lib(void) { -#ifdef THREAD_SAFE - pthread_mutex_init(&internal_ips_lock, NULL); -#endif - /* read the config file */ + /* Initialize mutex for internal ip list */ + proxychains_mutex_init(&internal_ips_lock); + + /* Read the config file */ get_chain_data(proxychains_pd, &proxychains_proxy_count, &proxychains_ct); proxychains_write_log(LOG_PREFIX "DLL init\n");