Skip to content

Commit 5b7270d

Browse files
author
Adam Hamsik
committed
Add new thread-safe getsrvbyname for MacOS X
1 parent e6b5f24 commit 5b7270d

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ ALL_LIBS = $(SHARED_LIBS)
3737
PXCHAINS = proxychains4
3838
ALL_TOOLS = $(PXCHAINS)
3939

40-
4140
CCFLAGS+=$(USER_CFLAGS) $(MAC_CFLAGS)
4241
LDFLAGS+=$(USER_LDFLAGS) $(MAC_LDFLAGS)
4342
CXXFLAGS+=$(CCFLAGS) $(USER_CFLAGS) $(MAC_CFLAGS)
44-
CFLAGS_MAIN=-DLIB_DIR=\"$(libdir)\" -DINSTALL_PREFIX=\"$(prefix)\" -DDLL_NAME=\"$(LDSO_PATHNAME)\"
43+
CFLAGS_MAIN=-DLIB_DIR=\"$(libdir)\" -DINSTALL_PREFIX=\"$(prefix)\" -DDLL_NAME=\"$(LDSO_PATHNAME)\" -DSYSCONFDIR=\"$(confdir)\"
4544

4645
all: $(ALL_LIBS) $(ALL_TOOLS)
4746

src/core.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,31 @@ void proxy_freeaddrinfo(struct addrinfo *res) {
819819
free(res);
820820
}
821821

822+
void proxy_getserverbyname(const char * service, struct servent *se_buf,
823+
char * buf, size_t buf_len, struct servent **se_result)
824+
{
825+
826+
#ifdef __linux__
827+
getservbyname_r(service, NULL, se_buf, buf, buf_len, se_result);
828+
#endif
829+
830+
#ifdef __APPLE__
831+
struct servent *se;
832+
#ifdef THREAD_SAFE
833+
MUTEX_LOCK(&internal_getsrvbyname_lock);
834+
#endif
835+
if(service)
836+
se = getservbyname(service, NULL);
837+
if (!se)
838+
memcpy(se_buf, se, buf_len);
839+
*se_result = se_buf;
840+
#ifdef THREAD_SAFE
841+
MUTEX_UNLOCK(&internal_getsrvbyname_lock);
842+
#endif
843+
844+
#endif
845+
}
846+
822847

823848
int proxy_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) {
824849
struct gethostbyname_data ghdata;
@@ -833,7 +858,7 @@ int proxy_getaddrinfo(const char *node, const char *service, const struct addrin
833858
// printf("proxy_getaddrinfo node %s service %s\n",node,service);
834859
space = calloc(1, sizeof(struct addrinfo_data));
835860
if(!space) goto err1;
836-
861+
837862
if(node && !inet_aton(node, &((struct sockaddr_in *) &space->sockaddr_space)->sin_addr)) {
838863
hp = proxy_gethostbyname(node, &ghdata);
839864
if(hp)
@@ -842,14 +867,15 @@ int proxy_getaddrinfo(const char *node, const char *service, const struct addrin
842867
else
843868
goto err2;
844869
}
845-
if(service) getservbyname_r(service, NULL, &se_buf, buf, sizeof(buf), &se);
870+
if(service)
871+
proxy_getserverbyname(service, &se_buf, buf, sizeof(buf), &se);
846872

847873
port = se ? se->s_port : htons(atoi(service ? service : "0"));
848874
((struct sockaddr_in *) &space->sockaddr_space)->sin_port = port;
849875

850876
*res = p = &space->addrinfo_space;
851877
assert((size_t)p == (size_t) space);
852-
878+
853879
p->ai_addr = &space->sockaddr_space;
854880
if(node)
855881
strncpy(space->addr_name, node, sizeof(space->addr_name));
@@ -865,7 +891,7 @@ int proxy_getaddrinfo(const char *node, const char *service, const struct addrin
865891
} else {
866892
p->ai_flags = (AI_V4MAPPED | AI_ADDRCONFIG);
867893
}
868-
894+
869895
goto out;
870896
err2:
871897
free(space);

src/core.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ extern internal_ip_lookup_table internal_ips;
4242
#ifdef THREAD_SAFE
4343
#include <pthread.h>
4444
pthread_mutex_t internal_ips_lock;
45+
46+
#ifdef __APPLE__
47+
pthread_mutex_t internal_getsrvbyname_lock;
48+
#endif
49+
4550
# define MUTEX_LOCK(x) pthread_mutex_lock(x)
4651
# define MUTEX_UNLOCK(x) pthread_mutex_unlock(x)
4752
# define MUTEX_INIT(x,y) pthread_mutex_init(x, y)

src/libproxychains.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ static void* load_sym(char* symname, void* proxyfunc) {
9696

9797
static void do_init(void) {
9898
MUTEX_INIT(&internal_ips_lock, NULL);
99+
100+
#ifdef __APPLE__
101+
MUTEX_INIT(&internal_getsrvbyname_lock, NULL);
102+
#endif
103+
99104
/* read the config file */
100105
get_chain_data(proxychains_pd, &proxychains_proxy_count, &proxychains_ct);
101106

0 commit comments

Comments
 (0)