3
3
#include " log.h"
4
4
#include " lib/md5.h"
5
5
6
- char local_address[100 ]=" 0.0.0.0" , remote_address[100 ]=" 255.255.255.255" ,source_address[100 ]=" 0.0.0.0" ;
7
- u32_t local_address_uint32,remote_address_uint32,source_address_uint32;
6
+ char local_ip[100 ]=" 0.0.0.0" , remote_ip[100 ]=" 255.255.255.255" ,source_ip[100 ]=" 0.0.0.0" ;
7
+ u32_t local_ip_uint32,remote_ip_uint32,source_ip_uint32;
8
+
9
+ int force_source_ip=0 ;
8
10
int source_port=0 ,local_port = -1 , remote_port = -1 ;
9
11
10
12
id_t const_id=0 ;
@@ -867,7 +869,7 @@ int try_to_list_and_bind(int port)
867
869
868
870
temp_bind_addr.sin_family = AF_INET;
869
871
temp_bind_addr.sin_port = htons (port);
870
- temp_bind_addr.sin_addr .s_addr = local_address_uint32 ;
872
+ temp_bind_addr.sin_addr .s_addr = local_ip_uint32 ;
871
873
872
874
if (bind (bind_fd, (struct sockaddr *)&temp_bind_addr, sizeof (temp_bind_addr)) !=0 )
873
875
{
@@ -987,11 +989,13 @@ int client_on_timer(conn_info_t &conn_info) //for client
987
989
conn_info.my_id = get_true_random_number_nz (); // /todo no need to do this everytime
988
990
989
991
u32_t new_ip=0 ;
990
- if (get_src_adress (new_ip)==0 )
992
+ if (!force_source_ip&& get_src_adress (new_ip)==0 )
991
993
{
992
- if (new_ip!=source_address_uint32 )
994
+ if (new_ip!=source_ip_uint32 )
993
995
{
994
- source_address_uint32=new_ip;
996
+ mylog (log_info," source ip changed from %s to" ,my_ntoa (source_ip_uint32));
997
+ log_bare (log_info," %s\n " ,my_ntoa (new_ip));
998
+ source_ip_uint32=new_ip;
995
999
send_info.src_ip =new_ip;
996
1000
}
997
1001
}
@@ -1685,7 +1689,7 @@ int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,cha
1685
1689
// memset(&remote_addr_in, 0, sizeof(remote_addr_in));
1686
1690
remote_addr_in.sin_family = AF_INET;
1687
1691
remote_addr_in.sin_port = htons (remote_port);
1688
- remote_addr_in.sin_addr .s_addr = remote_address_uint32 ;
1692
+ remote_addr_in.sin_addr .s_addr = remote_ip_uint32 ;
1689
1693
1690
1694
int new_udp_fd = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1691
1695
if (new_udp_fd < 0 ) {
@@ -1879,7 +1883,7 @@ int get_src_adress(u32_t &ip)
1879
1883
// memset(&remote_addr_in, 0, sizeof(remote_addr_in));
1880
1884
remote_addr_in.sin_family = AF_INET;
1881
1885
remote_addr_in.sin_port = htons (remote_port);
1882
- remote_addr_in.sin_addr .s_addr = remote_address_uint32 ;
1886
+ remote_addr_in.sin_addr .s_addr = remote_ip_uint32 ;
1883
1887
1884
1888
1885
1889
int new_udp_fd=socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
@@ -1888,7 +1892,7 @@ int get_src_adress(u32_t &ip)
1888
1892
mylog (log_warn," create udp_fd error\n " );
1889
1893
return -1 ;
1890
1894
}
1891
- set_buf_size (new_udp_fd);
1895
+ // set_buf_size(new_udp_fd);
1892
1896
1893
1897
mylog (log_debug," created new udp_fd %d\n " ,new_udp_fd);
1894
1898
int ret = connect (new_udp_fd, (struct sockaddr *) &remote_addr_in, slen);
@@ -1923,17 +1927,17 @@ int client_event_loop()
1923
1927
packet_info_t &recv_info=conn_info.raw_info .recv_info ;
1924
1928
1925
1929
// printf("?????\n");
1926
- if (source_address_uint32 ==0 )
1930
+ if (source_ip_uint32 ==0 )
1927
1931
{
1928
1932
mylog (log_info," get_src_adress called\n " );
1929
- if (get_src_adress (source_address_uint32 )!=0 )
1933
+ if (get_src_adress (source_ip_uint32 )!=0 )
1930
1934
{
1931
1935
mylog (log_fatal," the trick to auto get source ip failed,you should specific an ip by --source-ip\n " );
1932
1936
myexit (-1 );
1933
1937
}
1934
1938
}
1935
1939
in_addr tmp;
1936
- tmp.s_addr =source_address_uint32 ;
1940
+ tmp.s_addr =source_ip_uint32 ;
1937
1941
mylog (log_info," source ip = %s\n " ,inet_ntoa (tmp));
1938
1942
// printf("done\n");
1939
1943
@@ -1944,13 +1948,13 @@ int client_event_loop()
1944
1948
myexit (-1 );
1945
1949
}
1946
1950
send_info.src_port =source_port;
1947
- send_info.src_ip = source_address_uint32 ;
1951
+ send_info.src_ip = source_ip_uint32 ;
1948
1952
1949
1953
int i, j, k;int ret;
1950
1954
init_raw_socket ();
1951
1955
1952
1956
// init_filter(source_port);
1953
- send_info.dst_ip =remote_address_uint32 ;
1957
+ send_info.dst_ip =remote_ip_uint32 ;
1954
1958
send_info.dst_port =remote_port;
1955
1959
1956
1960
// g_packet_info.src_ip=source_address_uint32;
@@ -1968,7 +1972,7 @@ int client_event_loop()
1968
1972
// memset(&local_me, 0, sizeof(local_me));
1969
1973
local_me.sin_family = AF_INET;
1970
1974
local_me.sin_port = htons (local_port);
1971
- local_me.sin_addr .s_addr = local_address_uint32 ;
1975
+ local_me.sin_addr .s_addr = local_ip_uint32 ;
1972
1976
1973
1977
1974
1978
if (bind (udp_fd, (struct sockaddr *) &local_me, slen) == -1 ) {
@@ -2137,7 +2141,7 @@ int server_event_loop()
2137
2141
2138
2142
int i, j, k;int ret;
2139
2143
2140
- bind_address_uint32=local_address_uint32 ;// only server has bind adress,client sets it to zero
2144
+ bind_address_uint32=local_ip_uint32 ;// only server has bind adress,client sets it to zero
2141
2145
2142
2146
2143
2147
if (raw_mode==mode_faketcp)
@@ -2154,7 +2158,7 @@ int server_event_loop()
2154
2158
2155
2159
temp_bind_addr.sin_family = AF_INET;
2156
2160
temp_bind_addr.sin_port = htons (local_port);
2157
- temp_bind_addr.sin_addr .s_addr = local_address_uint32 ;
2161
+ temp_bind_addr.sin_addr .s_addr = local_ip_uint32 ;
2158
2162
2159
2163
if (bind (bind_fd, (struct sockaddr *)&temp_bind_addr, sizeof (temp_bind_addr)) !=0 )
2160
2164
{
@@ -2370,7 +2374,7 @@ void process_lower_level()
2370
2374
{
2371
2375
if (strchr (optarg , ' #' ) == 0 ) {
2372
2376
mylog (log_fatal,
2373
- " lower-level parameter invaild,should be if_name#mac_adress ,ie eth0#00:23:45:67:89:b9 \n " );
2377
+ " lower-level parameter invaild,check help page for format \n " );
2374
2378
myexit (-1 );
2375
2379
}
2376
2380
lower_level = 1 ;
@@ -2380,10 +2384,10 @@ void process_lower_level()
2380
2384
&hw[3 ], &hw[4 ], &hw[5 ]);
2381
2385
2382
2386
mylog (log_warn,
2383
- " make sure this is correct: ifname =<%s> gateway_hw_hd =<%x:%x:%x:%x:%x:%x > \n " ,
2387
+ " make sure this is correct: if_name =<%s> dest_mac_adress =<%02x:%02x:%02x:%02x:%02x:%02x > \n " ,
2384
2388
if_name, hw[0 ], hw[1 ], hw[2 ], hw[3 ], hw[4 ], hw[5 ]);
2385
2389
for (int i = 0 ; i < 6 ; i++) {
2386
- oppsite_hw_addr [i] = uint8_t (hw[i]);
2390
+ dest_hw_addr [i] = uint8_t (hw[i]);
2387
2391
}
2388
2392
}
2389
2393
void print_help ()
@@ -2426,7 +2430,7 @@ void print_help()
2426
2430
printf (" 1:increase every packet\n " );
2427
2431
printf (" 2:increase randomly, about every 3 packets (default)\n " );
2428
2432
// printf("\n");
2429
- printf (" --lower-level <string> send packet at OSI level 2, format:'if_name#gateway_mac_adress '\n " );
2433
+ printf (" --lower-level <string> send packet at OSI level 2, format:'if_name#dest_mac_adress '\n " );
2430
2434
printf (" ie:'eth0#00:23:45:67:89:b9'.Beta.\n " );
2431
2435
printf (" -h,--help print this help message\n " );
2432
2436
@@ -2512,7 +2516,7 @@ void process_arg(int argc, char *argv[])
2512
2516
case ' l' :
2513
2517
no_l = 0 ;
2514
2518
if (strchr (optarg , ' :' ) != 0 ) {
2515
- sscanf (optarg , " %[^:]:%d" , local_address , &local_port);
2519
+ sscanf (optarg , " %[^:]:%d" , local_ip , &local_port);
2516
2520
if (local_port==22 )
2517
2521
{
2518
2522
mylog (log_fatal," port 22 not allowed\n " );
@@ -2527,7 +2531,7 @@ void process_arg(int argc, char *argv[])
2527
2531
case ' r' :
2528
2532
no_r = 0 ;
2529
2533
if (strchr (optarg , ' :' ) != 0 ) {
2530
- sscanf (optarg , " %[^:]:%d" , remote_address , &remote_port);
2534
+ sscanf (optarg , " %[^:]:%d" , remote_ip , &remote_port);
2531
2535
if (remote_port==22 )
2532
2536
{
2533
2537
mylog (log_fatal," port 22 not allowed\n " );
@@ -2588,8 +2592,9 @@ void process_arg(int argc, char *argv[])
2588
2592
else if (strcmp (long_options[option_index].name ," source-ip" )==0 )
2589
2593
{
2590
2594
mylog (log_debug," parsing long option :source-ip\n " );
2591
- sscanf (optarg , " %s" , source_address);
2592
- mylog (log_debug," source: %s\n " ,source_address);
2595
+ sscanf (optarg , " %s" , source_ip);
2596
+ mylog (log_debug," source: %s\n " ,source_ip);
2597
+ force_source_ip=1 ;
2593
2598
}
2594
2599
else if (strcmp (long_options[option_index].name ," source-port" )==0 )
2595
2600
{
@@ -2738,11 +2743,11 @@ void process_arg(int argc, char *argv[])
2738
2743
2739
2744
log_bare (log_info," key=%s " ,key_string);
2740
2745
2741
- log_bare (log_info," local_ip=%s " ,local_address );
2746
+ log_bare (log_info," local_ip=%s " ,local_ip );
2742
2747
log_bare (log_info," local_port=%d " ,local_port);
2743
- log_bare (log_info," remote_ip=%s " ,remote_address );
2748
+ log_bare (log_info," remote_ip=%s " ,remote_ip );
2744
2749
log_bare (log_info," remote_port=%d " ,remote_port);
2745
- log_bare (log_info," source_ip=%s " ,source_address );
2750
+ log_bare (log_info," source_ip=%s " ,source_ip );
2746
2751
log_bare (log_info," source_port=%d " ,source_port);
2747
2752
2748
2753
log_bare (log_info," socket_buf_size=%d " ,socket_buf_size);
@@ -2756,17 +2761,17 @@ void iptables_rule()
2756
2761
{
2757
2762
if (raw_mode==mode_faketcp)
2758
2763
{
2759
- sprintf (rule," INPUT -s %s/32 -p tcp -m tcp --sport %d -j DROP" ,remote_address ,remote_port);
2764
+ sprintf (rule," INPUT -s %s/32 -p tcp -m tcp --sport %d -j DROP" ,remote_ip ,remote_port);
2760
2765
// mylog(log_warn,"make sure you have run once: iptables -A INPUT -s %s/32 -p tcp -m tcp --sport %d -j DROP\n",remote_address,remote_port);
2761
2766
}
2762
2767
if (raw_mode==mode_udp)
2763
2768
{
2764
- sprintf (rule," INPUT -s %s/32 -p udp -m udp --sport %d -j DROP" ,remote_address ,remote_port);
2769
+ sprintf (rule," INPUT -s %s/32 -p udp -m udp --sport %d -j DROP" ,remote_ip ,remote_port);
2765
2770
// mylog(log_warn,"make sure you have run once: iptables -A INPUT -s %s/32 -p udp -m udp --sport %d -j DROP\n",remote_address,remote_port);
2766
2771
}
2767
2772
if (raw_mode==mode_icmp)
2768
2773
{
2769
- sprintf (rule," INPUT -s %s/32 -p icmp -j DROP" ,remote_address );
2774
+ sprintf (rule," INPUT -s %s/32 -p icmp -j DROP" ,remote_ip );
2770
2775
// mylog(log_warn,"make sure you have run once: iptables -A INPUT -s %s/32 -p icmp -j DROP\n",remote_address);
2771
2776
}
2772
2777
}
@@ -2785,14 +2790,14 @@ void iptables_rule()
2785
2790
}
2786
2791
if (raw_mode==mode_icmp)
2787
2792
{
2788
- if (local_address_uint32 ==0 )
2793
+ if (local_ip_uint32 ==0 )
2789
2794
{
2790
2795
sprintf (rule," INPUT -p icmp -j DROP" );
2791
2796
// mylog(log_warn,"make sure you have run once: iptables -A INPUT -p icmp -j DROP\n");
2792
2797
}
2793
2798
else
2794
2799
{
2795
- sprintf (rule," INPUT -d %s/32 -p icmp -j DROP" ,local_address );
2800
+ sprintf (rule," INPUT -d %s/32 -p icmp -j DROP" ,local_ip );
2796
2801
// mylog(log_warn,"make sure you have run once: iptables -A INPUT -d %s/32 -p icmp -j DROP\n",local_address);
2797
2802
}
2798
2803
}
@@ -2849,9 +2854,9 @@ int main(int argc, char *argv[])
2849
2854
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 " );
2850
2855
}
2851
2856
2852
- local_address_uint32 =inet_addr (local_address );
2853
- remote_address_uint32 =inet_addr (remote_address );
2854
- source_address_uint32 =inet_addr (source_address );
2857
+ local_ip_uint32 =inet_addr (local_ip );
2858
+ remote_ip_uint32 =inet_addr (remote_ip );
2859
+ source_ip_uint32 =inet_addr (source_ip );
2855
2860
2856
2861
2857
2862
// current_time_rough=get_current_time();
0 commit comments