From c6cd29dd6fe8341ecb221a0d864cf104b34d3f0a Mon Sep 17 00:00:00 2001 From: Kenny Lam Date: Tue, 24 Oct 2017 22:58:58 +0800 Subject: [PATCH 1/8] Trial on using remote_host instead of ip --- README.md | 2 +- common.h | 1 + doc/README.zh-cn.md | 2 +- main.cpp | 11 +++++++++-- misc.cpp | 16 ++++++++-------- misc.h | 4 ++-- 6 files changed, 22 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index e21826a..7b5d9b4 100755 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ repository: https://github.com/wangyu-/udp2raw-tunnel usage: run as client : ./this_program -c -l local_listen_ip:local_port -r server_ip:server_port [options] - run as server : ./this_program -s -l server_listen_ip:server_port -r remote_ip:remote_port [options] + run as server : ./this_program -s -l server_listen_ip:server_port -r remote_host:remote_port [options] common options,these options must be same on both side: --raw-mode avaliable values:faketcp(default),udp,icmp diff --git a/common.h b/common.h index 3955a59..1bf48ef 100644 --- a/common.h +++ b/common.h @@ -24,6 +24,7 @@ #include #include //for exit(0); #include //For errno - the error number +#include // for gethostbyname() #include //Provides declarations for tcp header #include #include //Provides declarations for ip header diff --git a/doc/README.zh-cn.md b/doc/README.zh-cn.md index 04d53fd..c0213b1 100644 --- a/doc/README.zh-cn.md +++ b/doc/README.zh-cn.md @@ -103,7 +103,7 @@ repository: https://github.com/wangyu-/udp2raw-tunnel usage: run as client : ./this_program -c -l local_listen_ip:local_port -r server_ip:server_port [options] - run as server : ./this_program -s -l server_listen_ip:server_port -r remote_ip:remote_port [options] + run as server : ./this_program -s -l server_listen_ip:server_port -r remote_host:remote_port [options] common options,these options must be same on both side: --raw-mode avaliable values:faketcp(default),udp,icmp diff --git a/main.cpp b/main.cpp index bc0dae1..a2e8336 100755 --- a/main.cpp +++ b/main.cpp @@ -1023,7 +1023,7 @@ int client_event_loop() string hw_string; if(find_lower_level_info(remote_ip_uint32,dest_ip,if_name_string,hw_string)!=0) { - mylog(log_fatal,"auto detect lower-level info failed for %s,specific it manually\n",remote_ip); + mylog(log_fatal,"auto detect lower-level info failed for %s,specific it manually\n",remote_host); myexit(-1); } mylog(log_info,"we are running at lower-level (auto) mode,%s %s %s\n",my_ntoa(dest_ip),if_name_string.c_str(),hw_string.c_str()); @@ -1611,8 +1611,15 @@ int main(int argc, char *argv[]) mylog(log_error,"root check failed,make sure you run this program with root,we can try to continue,but it will likely fail\n"); } + + struct hostent *he; + if ( (he = gethostbyname(remote_host) ) == NULL ) { + mylog(log_error,"Unable to resolve hostname: %s\n",remote_host); + exit(1); /* error */ + } + remote_ip_uint32=inet_addr(he->h_addr_list[0]); + local_ip_uint32=inet_addr(local_ip); - remote_ip_uint32=inet_addr(remote_ip); source_ip_uint32=inet_addr(source_ip); diff --git a/misc.cpp b/misc.cpp index 983a647..2252dda 100644 --- a/misc.cpp +++ b/misc.cpp @@ -12,9 +12,9 @@ #include "connection.h" -char local_ip[100]="0.0.0.0", remote_ip[100]="255.255.255.255",source_ip[100]="0.0.0.0";//local_ip is for -l option,remote_ip for -r option,source for --source-ip +char local_ip[100]="0.0.0.0", remote_host[100]="255.255.255.255",source_ip[100]="0.0.0.0";//local_ip is for -l option,remote_host for -r option,source for --source-ip u32_t local_ip_uint32,remote_ip_uint32,source_ip_uint32;//convert from last line. -int local_port = -1, remote_port=-1,source_port=0;//similiar to local_ip remote_ip,buf for port.source_port=0 indicates --source-port is not enabled +int local_port = -1, remote_port=-1,source_port=0;//similiar to local_ip remote_host,buf for port.source_port=0 indicates --source-port is not enabled int force_source_ip=0; //if --source-ip is enabled @@ -100,7 +100,7 @@ void print_help() printf("\n"); printf("usage:\n"); printf(" run as client : ./this_program -c -l local_listen_ip:local_port -r server_ip:server_port [options]\n"); - printf(" run as server : ./this_program -s -l server_listen_ip:server_port -r remote_ip:remote_port [options]\n"); + printf(" run as server : ./this_program -s -l server_listen_ip:server_port -r remote_host:remote_port [options]\n"); printf("\n"); printf("common options,these options must be same on both side:\n"); printf(" --raw-mode avaliable values:faketcp(default),udp,icmp\n"); @@ -363,7 +363,7 @@ void process_arg(int argc, char *argv[]) //process all options case 'r': no_r = 0; if (strchr(optarg, ':') != 0) { - sscanf(optarg, "%[^:]:%d", remote_ip, &remote_port); + sscanf(optarg, "%[^:]:%d", remote_host, &remote_port); if(remote_port==22) { mylog(log_fatal,"port 22 not allowed\n"); @@ -623,7 +623,7 @@ void process_arg(int argc, char *argv[]) //process all options log_bare(log_info,"local_ip=%s ",local_ip); log_bare(log_info,"local_port=%d ",local_port); - log_bare(log_info,"remote_ip=%s ",remote_ip); + log_bare(log_info,"remote_host=%s ",remote_host); log_bare(log_info,"remote_port=%d ",remote_port); log_bare(log_info,"source_ip=%s ",source_ip); log_bare(log_info,"source_port=%d ",source_port); @@ -766,15 +766,15 @@ void iptables_rule() // handles -a -g --gen-add --keep-rule { if(raw_mode==mode_faketcp) { - sprintf(tmp_pattern,"-s %s/32 -p tcp -m tcp --sport %d",remote_ip,remote_port); + sprintf(tmp_pattern,"-s %s/32 -p tcp -m tcp --sport %d",remote_host,remote_port); } if(raw_mode==mode_udp) { - sprintf(tmp_pattern,"-s %s/32 -p udp -m udp --sport %d",remote_ip,remote_port); + sprintf(tmp_pattern,"-s %s/32 -p udp -m udp --sport %d",remote_host,remote_port); } if(raw_mode==mode_icmp) { - sprintf(tmp_pattern,"-s %s/32 -p icmp",remote_ip); + sprintf(tmp_pattern,"-s %s/32 -p icmp",remote_host); } pattern=tmp_pattern; } diff --git a/misc.h b/misc.h index b14c876..e976735 100644 --- a/misc.h +++ b/misc.h @@ -63,9 +63,9 @@ union current_state_t client_current_state_t client_current_state; }; -extern char local_ip[100], remote_ip[100],source_ip[100];//local_ip is for -l option,remote_ip for -r option,source for --source-ip +extern char local_ip[100], remote_host[100],source_ip[100];//local_ip is for -l option,remote_host for -r option,source for --source-ip extern u32_t local_ip_uint32,remote_ip_uint32,source_ip_uint32;//convert from last line. -extern int local_port , remote_port,source_port;//similiar to local_ip remote_ip,buf for port.source_port=0 indicates --source-port is not enabled +extern int local_port , remote_port,source_port;//similiar to local_ip remote_host,buf for port.source_port=0 indicates --source-port is not enabled extern int force_source_ip; //if --source-ip is enabled From 6f3eba419eb7d597033a46a1806442b81865c1d9 Mon Sep 17 00:00:00 2001 From: Kenny Lam Date: Wed, 25 Oct 2017 00:30:23 +0800 Subject: [PATCH 2/8] Fixed resolve type-conversion issue --- main.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index a2e8336..304ab85 100755 --- a/main.cpp +++ b/main.cpp @@ -795,6 +795,17 @@ int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,cha tmp_conv_id); return 0; } + struct hostent *he; + if ( (he = gethostbyname(remote_host) ) == NULL ) { + mylog(log_warn,"Unable to resolve hostname: %s, remote ip wasn't updated.\n",remote_host); + } else { + struct in_addr **addr_list = (struct in_addr **)he->h_addr_list; + if ((*addr_list[0]).s_addr!=remote_ip_uint32) { + remote_ip_uint32=(*addr_list[0]).s_addr; + mylog(log_info,"Updated remote ip '%s' as resolved result of '%s'\n",my_ntoa(remote_ip_uint32),remote_host); + } + } + struct sockaddr_in remote_addr_in={0}; socklen_t slen = sizeof(sockaddr_in); @@ -1617,7 +1628,8 @@ int main(int argc, char *argv[]) mylog(log_error,"Unable to resolve hostname: %s\n",remote_host); exit(1); /* error */ } - remote_ip_uint32=inet_addr(he->h_addr_list[0]); + struct in_addr **addr_list = (struct in_addr **)he->h_addr_list; + remote_ip_uint32=(*addr_list[0]).s_addr; local_ip_uint32=inet_addr(local_ip); source_ip_uint32=inet_addr(source_ip); From 08b14cc9eabd7be45f028b3af42788bc5d26a4bf Mon Sep 17 00:00:00 2001 From: Kenny Lam Date: Wed, 25 Oct 2017 00:35:20 +0800 Subject: [PATCH 3/8] Updated argument name --- misc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc.cpp b/misc.cpp index 2252dda..30a2cd0 100644 --- a/misc.cpp +++ b/misc.cpp @@ -99,7 +99,7 @@ void print_help() printf("repository: https://github.com/wangyu-/udp2raw-tunnel\n"); printf("\n"); printf("usage:\n"); - printf(" run as client : ./this_program -c -l local_listen_ip:local_port -r server_ip:server_port [options]\n"); + printf(" run as client : ./this_program -c -l local_listen_ip:local_port -r server_host:server_port [options]\n"); printf(" run as server : ./this_program -s -l server_listen_ip:server_port -r remote_host:remote_port [options]\n"); printf("\n"); printf("common options,these options must be same on both side:\n"); From c2aaf9b544be58567ab62d7d0de9f18c4a93d290 Mon Sep 17 00:00:00 2001 From: Kenny Lam Date: Wed, 25 Oct 2017 01:03:38 +0800 Subject: [PATCH 4/8] Added hostname resolving in client mode --- main.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 304ab85..c8991b7 100755 --- a/main.cpp +++ b/main.cpp @@ -37,6 +37,17 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is myexit(-1); } + struct hostent *he; + if ( (he = gethostbyname(remote_host) ) == NULL ) { + mylog(log_warn,"Unable to resolve hostname: %s, server ip wasn't updated.\n",remote_host); + } else { + struct in_addr **addr_list = (struct in_addr **)he->h_addr_list; + if ((*addr_list[0]).s_addr!=remote_ip_uint32) { + remote_ip_uint32=(*addr_list[0]).s_addr; + mylog(log_info,"Updated server ip '%s' as resolved result of '%s'\n",my_ntoa(remote_ip_uint32),remote_host); + } + } + conn_info.blob->anti_replay.re_init(); conn_info.my_id = get_true_random_number_nz(); ///todo no need to do this everytime @@ -1065,7 +1076,7 @@ int client_event_loop() } } - //printf("?????\n"); + if(source_ip_uint32==0) { mylog(log_info,"get_src_adress called\n"); @@ -1630,6 +1641,7 @@ int main(int argc, char *argv[]) } struct in_addr **addr_list = (struct in_addr **)he->h_addr_list; remote_ip_uint32=(*addr_list[0]).s_addr; + mylog(log_info,"%s ip = %s\n", program_mode==client_mode?"server":"remote", my_ntoa(remote_ip_uint32)); local_ip_uint32=inet_addr(local_ip); source_ip_uint32=inet_addr(source_ip); From eedffd90a097249e792c8db9a457c4226f4fb018 Mon Sep 17 00:00:00 2001 From: Kenny Lam Date: Wed, 25 Oct 2017 01:06:33 +0800 Subject: [PATCH 5/8] Updated argument name --- README.md | 2 +- doc/README.zh-cn.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7b5d9b4..547a67b 100755 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ git version:6e1df4b39f build date:Oct 24 2017 09:21:15 repository: https://github.com/wangyu-/udp2raw-tunnel usage: - run as client : ./this_program -c -l local_listen_ip:local_port -r server_ip:server_port [options] + run as client : ./this_program -c -l local_listen_ip:local_port -r remote_host:server_port [options] run as server : ./this_program -s -l server_listen_ip:server_port -r remote_host:remote_port [options] common options,these options must be same on both side: diff --git a/doc/README.zh-cn.md b/doc/README.zh-cn.md index c0213b1..e72e6d8 100644 --- a/doc/README.zh-cn.md +++ b/doc/README.zh-cn.md @@ -102,7 +102,7 @@ git version:adbe7d110f build date:Sep 6 2017 05:37:45 repository: https://github.com/wangyu-/udp2raw-tunnel usage: - run as client : ./this_program -c -l local_listen_ip:local_port -r server_ip:server_port [options] + run as client : ./this_program -c -l local_listen_ip:local_port -r remote_host:server_port [options] run as server : ./this_program -s -l server_listen_ip:server_port -r remote_host:remote_port [options] common options,these options must be same on both side: From 6ea083a49b77ffd1d2c756fd2339911903088177 Mon Sep 17 00:00:00 2001 From: Kenny Lam Date: Wed, 25 Apr 2018 23:23:17 +0800 Subject: [PATCH 6/8] Cleaning up merge tags --- main.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/main.cpp b/main.cpp index 3895011..a5da7b2 100755 --- a/main.cpp +++ b/main.cpp @@ -1092,13 +1092,9 @@ int client_event_loop() if(retry_on_error==0) { -<<<<<<< HEAD - mylog(log_fatal,"auto detect lower-level info failed for %s,specific it manually\n",remote_host); - myexit(-1); -======= if(find_lower_level_info(remote_ip_uint32,dest_ip,if_name_string,hw_string)!=0) { - mylog(log_fatal,"auto detect lower-level info failed for %s,specific it manually\n",remote_ip); + mylog(log_fatal,"auto detect lower-level info failed for %s,specific it manually\n",remote_host); myexit(-1); } } @@ -1109,7 +1105,7 @@ int client_event_loop() { if(find_lower_level_info(remote_ip_uint32,dest_ip,if_name_string,hw_string)!=0) { - mylog(log_warn,"auto detect lower-level info failed for %s,retry in %d seconds\n",remote_ip,retry_on_error_interval); + mylog(log_warn,"auto detect lower-level info failed for %s,retry in %d seconds\n",remote_host,retry_on_error_interval); sleep(retry_on_error_interval); } else @@ -1118,7 +1114,6 @@ int client_event_loop() } } ->>>>>>> upstream/master } mylog(log_info,"we are running at lower-level (auto) mode,%s %s %s\n",my_ntoa(dest_ip),if_name_string.c_str(),hw_string.c_str()); From f7383575b17ee1ff210bec24f960e54b85707702 Mon Sep 17 00:00:00 2001 From: Kenny Lam Date: Wed, 25 Apr 2018 23:32:47 +0800 Subject: [PATCH 7/8] Removed on-fly dns resolving as it is a blocking method --- main.cpp | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/main.cpp b/main.cpp index a5da7b2..98c93b1 100755 --- a/main.cpp +++ b/main.cpp @@ -50,17 +50,6 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is myexit(-1); } - struct hostent *he; - if ( (he = gethostbyname(remote_host) ) == NULL ) { - mylog(log_warn,"Unable to resolve hostname: %s, server ip wasn't updated.\n",remote_host); - } else { - struct in_addr **addr_list = (struct in_addr **)he->h_addr_list; - if ((*addr_list[0]).s_addr!=remote_ip_uint32) { - remote_ip_uint32=(*addr_list[0]).s_addr; - mylog(log_info,"Updated server ip '%s' as resolved result of '%s'\n",my_ntoa(remote_ip_uint32),remote_host); - } - } - conn_info.blob->anti_replay.re_init(); conn_info.my_id = get_true_random_number_nz(); ///todo no need to do this everytime @@ -837,16 +826,6 @@ int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,cha tmp_conv_id); return 0; } - struct hostent *he; - if ( (he = gethostbyname(remote_host) ) == NULL ) { - mylog(log_warn,"Unable to resolve hostname: %s, remote ip wasn't updated.\n",remote_host); - } else { - struct in_addr **addr_list = (struct in_addr **)he->h_addr_list; - if ((*addr_list[0]).s_addr!=remote_ip_uint32) { - remote_ip_uint32=(*addr_list[0]).s_addr; - mylog(log_info,"Updated remote ip '%s' as resolved result of '%s'\n",my_ntoa(remote_ip_uint32),remote_host); - } - } struct sockaddr_in remote_addr_in={0}; From 675ccdf650bcabd1fac2969eaf7c9878cb25bbb4 Mon Sep 17 00:00:00 2001 From: Kenny Lam Date: Thu, 26 Apr 2018 01:25:42 +0800 Subject: [PATCH 8/8] Avoided to add iptables rules with hostname --- misc.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/misc.cpp b/misc.cpp index 3bdcc57..74590de 100644 --- a/misc.cpp +++ b/misc.cpp @@ -848,15 +848,15 @@ void iptables_rule() // handles -a -g --gen-add --keep-rule --clear --wait-loc { if(raw_mode==mode_faketcp) { - sprintf(tmp_pattern,"-s %s/32 -p tcp -m tcp --sport %d",remote_host,remote_port); + sprintf(tmp_pattern,"-s %s/32 -p tcp -m tcp --sport %d",my_ntoa(remote_ip_uint32),remote_port); } if(raw_mode==mode_udp) { - sprintf(tmp_pattern,"-s %s/32 -p udp -m udp --sport %d",remote_host,remote_port); + sprintf(tmp_pattern,"-s %s/32 -p udp -m udp --sport %d",my_ntoa(remote_ip_uint32),remote_port); } if(raw_mode==mode_icmp) { - sprintf(tmp_pattern,"-s %s/32 -p icmp",remote_host); + sprintf(tmp_pattern,"-s %s/32 -p icmp",my_ntoa(remote_ip_uint32)); } pattern=tmp_pattern; }