From b17b36cbcecb5f1407ba31ec2cb93cb4f25871b7 Mon Sep 17 00:00:00 2001 From: z3APA3A <3APA3A@3proxy.ru> Date: Mon, 14 Aug 2017 18:22:15 +0300 Subject: [PATCH] Add support to bind to interface (-Di / -Do) via SO_BINDTODEVICE --- src/common.c | 4 +++- src/proxymain.c | 19 ++++++++++++++++++- src/structures.h | 4 ++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/common.c b/src/common.c index dfc3dd8..96e4e81 100644 --- a/src/common.c +++ b/src/common.c @@ -714,7 +714,9 @@ int doconnect(struct clientparam * param){ #endif } #endif - +#ifdef SO_BINDTODEVICE + if(param->srv->obindtodevice) so._setsockopt(param->remsock, SOL_SOCKET, SO_BINDTODEVICE, param->srv->obindtodevice, strlen(param->srv->obindtodevice) + 1); +#endif if(SAISNULL(¶m->sinsl)){ #ifndef NOIPV6 if(*SAFAMILY(¶m->sinsr) == AF_INET6) param->sinsl = param->srv->extsa6; diff --git a/src/proxymain.c b/src/proxymain.c index 6e8f9f4..69db547 100644 --- a/src/proxymain.c +++ b/src/proxymain.c @@ -1,6 +1,6 @@ /* 3APA3A simpliest proxy server - (c) 2002-2016 by Vladimir Dubrovin <3proxy@3proxy.ru> + (c) 2002-2017 by Vladimir Dubrovin <3proxy@3proxy.ru> please read License Agreement @@ -176,6 +176,10 @@ int MODULEMAINFUNC (int argc, char** argv){ " -u never ask for username\n" " -u2 always ask for username\n" #endif +#ifdef SO_BINDTODEVICE + " -Di(DEVICENAME) bind to incoming device, e.g. eth1\n" + " -Do(DEVICENAME) bind to outgoing device, e.g. eth1\n" +#endif #ifdef WITHSLICE " -s Use slice() - faster proxing, but no filtering for data\n" #endif @@ -302,6 +306,12 @@ int MODULEMAINFUNC (int argc, char** argv){ if(!conf.demon)daemonize(); conf.demon = 1; break; +#ifdef SO_BINDTODEVICE + case 'D': + if(argv[i][2] == 'i') srv.ibindtodevice = mystrdup(argv[i] + 3); + else if(argv[i][2] == 'o') srv.obindtodevice = mystrdup(argv[i] + 3); + break; +#endif case 'l': srv.logfunc = logstdout; if(srv.logtarget) myfree(srv.logtarget); @@ -565,6 +575,9 @@ int MODULEMAINFUNC (int argc, char** argv){ #ifdef SO_REUSEPORT opt = 1; so._setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (char *)&opt, sizeof(int)); +#endif +#ifdef SO_BINDTODEVICE + if(srv.ibindtodevice) so._setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, srv.ibindtodevice, strlen(srv.ibindtodevice) + 1); #endif } size = sizeof(srv.intsa); @@ -916,6 +929,10 @@ void srvfree(struct srvparam * srv){ if(srv->logtarget) myfree(srv->logtarget); if(srv->logformat) myfree(srv->logformat); if(srv->nonprintable) myfree(srv->nonprintable); +#ifdef SO_BINDTODEVICE + if(srv->ibindtodevice) myfree(srv->ibindtodevice); + if(srv->obindtodevice) myfree(srv->obindtodevice); +#endif } diff --git a/src/structures.h b/src/structures.h index b6f89fc..89b83cc 100644 --- a/src/structures.h +++ b/src/structures.h @@ -417,6 +417,10 @@ struct srvparam { struct pollfd fds; FILE *stdlog; unsigned char * target; +#ifdef SO_BINDTODEVICE + char * ibindtodevice; + char * obindtodevice; +#endif struct auth *authenticate; struct pollfd * srvfds; struct ace *acl;