Ver código fonte

code refactor for dns resolve; disabled it by default

wangyu- 7 anos atrás
pai
commit
b0976dbee6
7 arquivos alterados com 51 adições e 23 exclusões
  1. 2 2
      README.md
  2. 2 0
      common.h
  3. 2 2
      doc/README.zh-cn.md
  4. 21 7
      main.cpp
  5. 1 1
      makefile
  6. 18 9
      misc.cpp
  7. 5 2
      misc.h

+ 2 - 2
README.md

@@ -91,8 +91,8 @@ 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 remote_host:server_port  [options]
-    run as server : ./this_program -s -l server_listen_ip:server_port -r remote_host:remote_port  [options]
+    run as client : ./this_program -c -l local_listen_ip:local_port -r remote_address:server_port  [options]
+    run as server : ./this_program -s -l server_listen_ip:server_port -r remote_address:remote_port  [options]
 
 common options,these options must be same on both side:
     --raw-mode            <string>        avaliable values:faketcp(default),udp,icmp

+ 2 - 0
common.h

@@ -89,6 +89,8 @@ typedef u64_t fd64_t;
 const int max_data_len=1800;
 const int buf_len=max_data_len+400;
 
+const int max_address_len=512;
+
 u64_t get_current_time();
 u64_t pack_u64(u32_t a,u32_t b);
 

+ 2 - 2
doc/README.zh-cn.md

@@ -119,8 +119,8 @@ 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 remote_host:server_port  [options]
-    run as server : ./this_program -s -l server_listen_ip:server_port -r remote_host:remote_port  [options]
+    run as client : ./this_program -c -l local_listen_ip:local_port -r remote_address:server_port  [options]
+    run as server : ./this_program -s -l server_listen_ip:server_port -r remote_address:remote_port  [options]
 
 common options,these options must be same on both side:
     --raw-mode            <string>        avaliable values:faketcp(default),udp,icmp

+ 21 - 7
main.cpp

@@ -1073,7 +1073,7 @@ int client_event_loop()
 			{
 				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_host);
+					mylog(log_fatal,"auto detect lower-level info failed for %s,specific it manually\n",remote_ip);
 					myexit(-1);
 				}
 			}
@@ -1084,7 +1084,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_host,retry_on_error_interval);
+						mylog(log_warn,"auto detect lower-level info failed for %s,retry in %d seconds\n",remote_ip,retry_on_error_interval);
 						sleep(retry_on_error_interval);
 					}
 					else
@@ -1758,19 +1758,33 @@ int main(int argc, char *argv[])
 		mylog(log_warn,"you can run udp2raw with non-root account for better security. check README.md in repo for more info.\n");
 	}
 
-
+	local_ip_uint32=inet_addr(local_ip);
+	source_ip_uint32=inet_addr(source_ip);
+	
+//	if(enable_dns_resolve)
+//	{
+#if ENABLE_DNS_RESOLVE
 	struct hostent        *he;
-	if ( (he = gethostbyname(remote_host) ) == NULL ) {
-		mylog(log_error,"Unable to resolve hostname: %s\n",remote_host);
+	if ( (he = gethostbyname(remote_address) ) == NULL ) {
+		mylog(log_error,"Unable to resolve hostname: %s\n",remote_address);
 		exit(1); /* error */
 	}
 	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);
 
+	strcpy(remote_ip,my_ntoa(remote_ip_uint32));
+#else
+
+//	}
+//	else
+	{
+		//strncpy(remote_ip,remote_address,sizeof(remote_ip)-1);
+		strcpy(remote_ip,remote_address);
+		remote_ip_uint32=inet_addr(remote_ip);
+	}
+#endif
 
 	//current_time_rough=get_current_time();
 

+ 1 - 1
makefile

@@ -8,7 +8,7 @@ cc_mips24kc_le=/toolchains/lede-sdk-17.01.2-ramips-mt7621_gcc-5.4.0_musl-1.1.16.
 cc_arm= /toolchains/arm-2014.05/bin/arm-none-linux-gnueabi-g++
 #cc_arm=/toolchains/lede-sdk-17.01.2-brcm2708-bcm2708_gcc-5.4.0_musl-1.1.16_eabi.Linux-x86_64/staging_dir/toolchain-arm_arm1176jzf-s+vfp_gcc-5.4.0_musl-1.1.16_eabi/bin/arm-openwrt-linux-muslgnueabi-g++
 #cc_bcm2708=/home/wangyu/raspberry/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++ 
-FLAGS= -std=c++11 -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-missing-field-initializers
+FLAGS= -std=c++11 -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-missing-field-initializers ${OPT}
 
 COMMON=main.cpp lib/md5.c encrypt.cpp log.cpp network.cpp common.cpp  connection.cpp misc.cpp fd_manager.cpp -lpthread
 SOURCES= $(COMMON) lib/aes_faster_c/aes.c lib/aes_faster_c/wrapper.c

+ 18 - 9
misc.cpp

@@ -22,12 +22,15 @@ int max_rst_to_show=15;
 
 int max_rst_allowed=-1;
 
+int enable_dns_resolve=0;
+
 
 fd_manager_t fd_manager;
 
-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
+char remote_address[max_address_len]="";
+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
 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_host,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_ip,buf for port.source_port=0 indicates --source-port is not enabled
 
 int force_source_ip=0; //if --source-ip is enabled
 
@@ -118,8 +121,8 @@ 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_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("    run as client : ./this_program -c -l local_listen_ip:local_port -r server_address:server_port  [options]\n");
+	printf("    run as server : ./this_program -s -l server_listen_ip:server_port -r remote_address:remote_port  [options]\n");
 	printf("\n");
 	printf("common options,these options must be same on both side:\n");
 	printf("    --raw-mode            <string>        avaliable values:faketcp(default),udp,icmp\n");
@@ -274,6 +277,7 @@ void process_arg(int argc, char *argv[])  //process all options
 		{"mtu-warn", required_argument,    0, 1},
 		{"max-rst-to-show", required_argument,    0, 1},
 		{"max-rst-allowed", required_argument,    0, 1},
+		{"dns-resolve", no_argument,    0, 1},
 		{NULL, 0, 0, 0}
 	  };
 
@@ -393,7 +397,7 @@ void process_arg(int argc, char *argv[])  //process all options
 		case 'r':
 			no_r = 0;
 			if (strchr(optarg, ':') != 0) {
-				sscanf(optarg, "%[^:]:%d", remote_host, &remote_port);
+				sscanf(optarg, "%[^:]:%d", remote_address, &remote_port);
 				if(remote_port==22)
 				{
 					mylog(log_fatal,"port 22 not allowed\n");
@@ -646,6 +650,11 @@ void process_arg(int argc, char *argv[])  //process all options
 				assert(max_rst_allowed>=-1);
 				mylog(log_info,"max_rst_allowed=%d \n",max_rst_allowed);
 			}
+			else if(strcmp(long_options[option_index].name,"dns-resolve")==0)
+			{
+				enable_dns_resolve=1;
+				mylog(log_info,"dns-resolve enabled \n",max_rst_allowed);
+			}
 			else
 			{
 				mylog(log_warn,"ignored unknown long option ,option_index:%d code:<%x>\n",option_index, optopt);
@@ -683,7 +692,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_host=%s ",remote_host);
+	 log_bare(log_info,"remote_address=%s ",remote_address);
 	 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);
@@ -848,15 +857,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",my_ntoa(remote_ip_uint32),remote_port);
+			sprintf(tmp_pattern,"-s %s/32 -p tcp -m tcp --sport %d",remote_ip,remote_port);
 		}
 		if(raw_mode==mode_udp)
 		{
-			sprintf(tmp_pattern,"-s %s/32 -p udp -m udp --sport %d",my_ntoa(remote_ip_uint32),remote_port);
+			sprintf(tmp_pattern,"-s %s/32 -p udp -m udp --sport %d",remote_ip,remote_port);
 		}
 		if(raw_mode==mode_icmp)
 		{
-			sprintf(tmp_pattern,"-s %s/32 -p icmp",my_ntoa(remote_ip_uint32));
+			sprintf(tmp_pattern,"-s %s/32 -p icmp",remote_ip);
 		}
 		pattern=tmp_pattern;
 	}

+ 5 - 2
misc.h

@@ -20,6 +20,8 @@ extern int mtu_warn;
 extern int max_rst_allowed;
 extern int max_rst_to_show;
 
+extern int enable_dns_resolve;
+
 
 const u32_t max_handshake_conn_num=10000;
 const u32_t max_ready_conn_num=1000;
@@ -69,9 +71,10 @@ union current_state_t
 	client_current_state_t client_current_state;
 };
 
-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 char remote_address[max_address_len];
+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 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_host,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_ip,buf for port.source_port=0 indicates --source-port is not enabled
 
 extern int force_source_ip; //if --source-ip is enabled