From e40c049db949b3cbff0fd201fb4e15e1faeb59c8 Mon Sep 17 00:00:00 2001 From: Taha Jahangir Date: Wed, 19 Aug 2020 08:10:39 +0430 Subject: [PATCH] Allow binding to another (non-default) interface --- README.md | 2 +- sockssrv.c | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e8c0af6..a276e47 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ libc is not even 50 KB. that's easily usable even on the cheapest routers. command line options ------------------------ - microsocks -1 -i listenip -p port -u user -P password -b bindaddr + microsocks -1 -i listenip -p port -u user -P password -b bindaddr -B bindiface all arguments are optional. by default listenip is 0.0.0.0 and port 1080. diff --git a/sockssrv.c b/sockssrv.c index afe7073..a8d2e21 100644 --- a/sockssrv.c +++ b/sockssrv.c @@ -60,6 +60,7 @@ static sblist* auth_ips; static pthread_mutex_t auth_ips_mutex = PTHREAD_MUTEX_INITIALIZER; static const struct server* server; static union sockaddr_union bind_addr = {.v4.sin_family = AF_UNSPEC}; +static const char* bind_interface; enum socksstate { SS_1_CONNECTED, @@ -165,6 +166,8 @@ static int connect_socks_target(unsigned char *buf, size_t n, struct client *cli return -EC_GENERAL_FAILURE; } } + if(bind_interface && setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_interface, strlen(bind_interface)) == -1) + goto eval_errno; if(SOCKADDR_UNION_AF(&bind_addr) != AF_UNSPEC && bindtoip(fd, &bind_addr) == -1) goto eval_errno; if(connect(fd, remote->ai_addr, remote->ai_addrlen) == -1) @@ -384,7 +387,7 @@ int main(int argc, char** argv) { int ch; const char *listenip = "0.0.0.0"; unsigned port = 1080; - while((ch = getopt(argc, argv, ":1b:i:p:u:P:")) != -1) { + while((ch = getopt(argc, argv, ":1b:B:i:p:u:P:")) != -1) { switch(ch) { case '1': auth_ips = sblist_new(sizeof(union sockaddr_union), 8); @@ -392,6 +395,10 @@ int main(int argc, char** argv) { case 'b': resolve_sa(optarg, 0, &bind_addr); break; + case 'B': + bind_interface = strdup(optarg); + zero_arg(optarg); + break; case 'u': auth_user = strdup(optarg); zero_arg(optarg);