From 391dcd73543e36abe3328fa115ac3f21411e34a5 Mon Sep 17 00:00:00 2001 From: rofl0r Date: Tue, 12 Sep 2017 16:29:01 +0100 Subject: [PATCH] force server's ip address for outgoing connection when not bound to 0.0.0.0 if the user specifies a specific ip address for the server to listen on, then it is assumed he wants to use this ip for outgoing connections. closes #5 --- server.c | 12 ++++++++++++ server.h | 3 +++ sockssrv.c | 4 ++++ 3 files changed, 19 insertions(+) diff --git a/server.c b/server.c index a71de3d..5bab56b 100644 --- a/server.c +++ b/server.c @@ -1,5 +1,6 @@ #include "server.h" #include +#include #include int resolve(const char *host, unsigned short port, struct addrinfo** addr) { @@ -13,6 +14,12 @@ int resolve(const char *host, unsigned short port, struct addrinfo** addr) { return getaddrinfo(host, port_buf, &hints, addr); } +int server_bindtoip(const struct server *server, int fd) { + if(server->bindaddr.v4.sin_family != AF_UNSPEC) + return bind(fd, (struct sockaddr*) &server->bindaddr, server->bindaddrsz); + return 0; +} + int server_waitclient(struct server *server, struct client* client) { socklen_t clen = sizeof client->addr; return ((client->fd = accept(server->fd, (void*)&client->addr, &clen)) == -1)*-1; @@ -42,5 +49,10 @@ int server_setup(struct server *server, const char* listenip, unsigned short por return -3; } server->fd = listenfd; + if(strcmp(listenip, "0.0.0.0") && !resolve(listenip, 0, &ainfo)) { + server->bindaddrsz = ainfo->ai_addrlen; + memcpy(&server->bindaddr, ainfo->ai_addr, ainfo->ai_addrlen); + freeaddrinfo(ainfo); + } else server->bindaddr.v4.sin_family = AF_UNSPEC; return 0; } diff --git a/server.h b/server.h index 8f2f1a2..46d0a88 100644 --- a/server.h +++ b/server.h @@ -20,10 +20,13 @@ struct client { }; struct server { + union sockaddr_union bindaddr; int fd; + socklen_t bindaddrsz; }; int resolve(const char *host, unsigned short port, struct addrinfo** addr); +int server_bindtoip(const struct server *server, int fd); int server_waitclient(struct server *server, struct client* client); int server_setup(struct server *server, const char* listenip, unsigned short port); diff --git a/sockssrv.c b/sockssrv.c index e26c04d..42ab94c 100644 --- a/sockssrv.c +++ b/sockssrv.c @@ -38,6 +38,7 @@ static const char* auth_user; static const char* auth_pass; static sblist* auth_ips; static pthread_mutex_t auth_ips_mutex = PTHREAD_MUTEX_INITIALIZER; +static const struct server* server; enum socksstate { SS_1_CONNECTED, @@ -139,6 +140,8 @@ static int connect_socks_target(unsigned char *buf, size_t n, struct client *cli return -EC_GENERAL_FAILURE; } } + if(server_bindtoip(server, fd) == -1) + goto eval_errno; if(connect(fd, remote->ai_addr, remote->ai_addrlen) == -1) goto eval_errno; @@ -393,6 +396,7 @@ int main(int argc, char** argv) { perror("server_setup"); return 1; } + server = &s; while(1) { collect(threads); struct client c; -- 2.11.4.GIT