diff --git a/Makefile b/Makefile index 45d072c..bdaab44 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ SRCS = $(sort $(wildcard src/*.c)) OBJS = $(SRCS:.c=.o) LOBJS = src/core.o src/common.o src/libproxychains.o src/shm.o src/allocator_thread.o src/ip_type.o -CFLAGS += -Wall -O0 -g -std=c99 -D_GNU_SOURCE -pipe -DTHREAD_SAFE +CFLAGS += -Wall -O0 -g -std=c99 -D_GNU_SOURCE -pipe LDFLAGS = -shared -fPIC -Wl,--no-as-needed -ldl -lpthread INC = PIC = -fPIC diff --git a/src/core.c b/src/core.c index c7ed7b2..0c5d159 100644 --- a/src/core.c +++ b/src/core.c @@ -34,10 +34,8 @@ #include #include #include -#ifdef THREAD_SAFE -#include +#include "mutex.h" pthread_mutex_t hostdb_lock; -#endif #include "core.h" #include "common.h" @@ -692,6 +690,14 @@ int connect_proxy_chain(int sock, ip_type target_ip, return -1; } +void core_initialize(void) { + MUTEX_INIT(&hostdb_lock); +} + +void core_unload(void) { + MUTEX_DESTROY(&hostdb_lock); +} + static void gethostbyname_data_setstring(struct gethostbyname_data* data, char* name) { snprintf(data->addr_name, sizeof(data->addr_name), "%s", name); data->hostent_space.h_name = data->addr_name; diff --git a/src/core.h b/src/core.h index d6c088d..9e895b6 100644 --- a/src/core.h +++ b/src/core.h @@ -28,11 +28,6 @@ #include "ip_type.h" -#ifdef THREAD_SAFE -#include "mutex.h" -extern pthread_mutex_t hostdb_lock; -#endif - /*error codes*/ typedef enum { SUCCESS=0, @@ -121,6 +116,9 @@ void proxy_freeaddrinfo(struct addrinfo *res); void pc_stringfromipv4(unsigned char *ip_buf_4_bytes, char *outbuf_16_bytes); +void core_initialize(void); +void core_unload(void); + #include "debug.h" #endif diff --git a/src/libproxychains.c b/src/libproxychains.c index 37c1f88..d293006 100644 --- a/src/libproxychains.c +++ b/src/libproxychains.c @@ -32,6 +32,7 @@ #include #include #include +#include #include "core.h" @@ -64,9 +65,8 @@ localaddr_arg localnet_addr[MAX_LOCALNET]; size_t num_localnet_addr = 0; unsigned int remote_dns_subnet = 224; -#ifdef THREAD_SAFE pthread_once_t init_once = PTHREAD_ONCE_INIT; -#endif + static int init_l = 0; static inline void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_type * ct); @@ -97,7 +97,7 @@ static void* load_sym(char* symname, void* proxyfunc) { static void do_init(void) { srand(time(NULL)); - MUTEX_INIT(&hostdb_lock); + core_initialize(); at_init(); /* read the config file */ @@ -115,18 +115,23 @@ static void do_init(void) { init_l = 1; } +#if 0 +/* FIXME this is currently unused. + * it is not strictly needed. + * maybe let it be called by a gcc destructor, if that doesnt + * have negative consequences (e.g. when a child calles exit) */ +static void unload(void) { + at_close(); + core_unload(); +} +#endif + static void init_lib_wrapper(const char* caller) { #ifndef DEBUG (void) caller; #endif -#ifndef THREAD_SAFE - if(init_l) return; - PDEBUG("%s called from %s\n", __FUNCTION__, caller); - do_init(); -#else if(!init_l) PDEBUG("%s called from %s\n", __FUNCTION__, caller); pthread_once(&init_once, do_init); -#endif } /* if we use gcc >= 3, we can instruct the dynamic loader