From 1305e5ddd4634f5ebd6dc3577de59320650ea7cd Mon Sep 17 00:00:00 2001 From: picronsin Date: Mon, 21 Nov 2022 17:45:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E7=BD=91=E5=8D=A1?= =?UTF-8?q?=E9=80=89=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ auth.c | 17 +++++++++++++++++ configparse.c | 1 + configparse.h | 1 + main.c | 10 +++++++++- 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0984eba..1aff73e 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Options: --conf , -c import configuration file --bindip , -b bind your ip address(default is 0.0.0.0) --log , -l specify log file + --interface , -i bind interface --802.1x, -x enable 802.1x --daemon, -d set daemon flag --eternal, -e set eternal flag @@ -30,6 +31,7 @@ $ dogcom -m pppoe -c dogcom.conf -x # (PS: only on Linux build) $ dogcom -m pppoe -c dogcom.conf -e # eternal dogcoming (default times is 5) $ dogcom -m pppoe -c dogcom.conf -v $ dogcom -m dhcp -c dogcom.conf -b 10.2.3.12 -v +$ dogcom -m dhcp -c dogcom.conf -i eth0.2 # (PS: only on Linux build) ``` #### To build: diff --git a/auth.c b/auth.c index 57d1883..61f808a 100644 --- a/auth.c +++ b/auth.c @@ -12,6 +12,7 @@ typedef int socklen_t; #include #include #include +#include #endif #include "auth.h" @@ -568,6 +569,7 @@ int dogcom(int try_times) { #endif return 1; } + // bind socket if (bind(sockfd, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) { #ifdef WIN32 @@ -595,6 +597,21 @@ int dogcom(int try_times) { return 1; } + // bind interface +#ifdef linux + if (strlen(bind_ifr_name) > 0) + { + struct ifreq ifr; + memset(&ifr, 0, sizeof(ifr)); + strcpy(ifr.ifr_name, bind_ifr_name); + if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0) + { + perror("Failed to bind ifr"); + return 1; + } + } +#endif + // start dogcoming if (strcmp(mode, "dhcp") == 0) { int login_failed_attempts = 0; diff --git a/configparse.c b/configparse.c index 5b4d4d0..e1db35c 100644 --- a/configparse.c +++ b/configparse.c @@ -11,6 +11,7 @@ int eternal_flag = 0; char *log_path; char mode[10]; char bind_ip[20]; +char bind_ifr_name[20]; struct config drcom_config; static int read_d_config(char *buf, int size); diff --git a/configparse.h b/configparse.h index 1bd5648..f8e4392 100644 --- a/configparse.h +++ b/configparse.h @@ -30,6 +30,7 @@ extern int eternal_flag; extern char *log_path; extern char mode[10]; extern char bind_ip[20]; +extern char bind_ifr_name[20]; int config_parse(char *filepath); diff --git a/main.c b/main.c index 19b2440..391fbcd 100644 --- a/main.c +++ b/main.c @@ -33,6 +33,7 @@ int main(int argc, char *argv[]) { {"bindip", required_argument, 0, 'b'}, {"log", required_argument, 0, 'l'}, #ifdef linux + {"interface", required_argument,0, 'i'}, {"daemon", no_argument, 0, 'd'}, {"802.1x", no_argument, 0, 'x'}, #endif @@ -44,7 +45,7 @@ int main(int argc, char *argv[]) { int c; int option_index = 0; #ifdef linux - c = getopt_long(argc, argv, "m:c:b:l:dxevh", long_options, &option_index); + c = getopt_long(argc, argv, "m:c:b:l:i:dxevh", long_options, &option_index); #else c = getopt_long(argc, argv, "m:c:b:l:evh", long_options, &option_index); #endif @@ -98,6 +99,9 @@ int main(int argc, char *argv[]) { #endif break; #ifdef linux + case 'i': + strcpy(bind_ifr_name, optarg); + break; case 'd': daemon_flag = 1; break; @@ -151,6 +155,9 @@ int main(int argc, char *argv[]) { if (strlen(bind_ip) == 0) { memcpy(bind_ip, default_bind_ip, sizeof(default_bind_ip)); } + if (strlen(bind_ifr_name) == 0) { + memset(bind_ifr_name, 0, 20); + } dogcom(5); } else { return 1; @@ -177,6 +184,7 @@ void print_help(int exval) { printf("\t--bindip , -b bind your ip address(default is 0.0.0.0)\n"); printf("\t--log , -l specify log file\n"); #ifdef linux + printf("\t--interface, -i bind interface\n"); printf("\t--daemon, -d set daemon flag\n"); printf("\t--802.1x, -x enable 802.1x\n"); #endif