Skip to content

Commit 8356b45

Browse files
committed
trival
1 parent e502076 commit 8356b45

File tree

3 files changed

+44
-39
lines changed

3 files changed

+44
-39
lines changed

main.cpp

+41-36
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
#include "log.h"
44
#include "lib/md5.h"
55

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;
810
int source_port=0,local_port = -1, remote_port = -1;
911

1012
id_t const_id=0;
@@ -867,7 +869,7 @@ int try_to_list_and_bind(int port)
867869

868870
temp_bind_addr.sin_family = AF_INET;
869871
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;
871873

872874
if (bind(bind_fd, (struct sockaddr*)&temp_bind_addr, sizeof(temp_bind_addr)) !=0)
873875
{
@@ -987,11 +989,13 @@ int client_on_timer(conn_info_t &conn_info) //for client
987989
conn_info.my_id = get_true_random_number_nz(); ///todo no need to do this everytime
988990

989991
u32_t new_ip=0;
990-
if(get_src_adress(new_ip)==0)
992+
if(!force_source_ip&&get_src_adress(new_ip)==0)
991993
{
992-
if(new_ip!=source_address_uint32)
994+
if(new_ip!=source_ip_uint32)
993995
{
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;
995999
send_info.src_ip=new_ip;
9961000
}
9971001
}
@@ -1685,7 +1689,7 @@ int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,cha
16851689
//memset(&remote_addr_in, 0, sizeof(remote_addr_in));
16861690
remote_addr_in.sin_family = AF_INET;
16871691
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;
16891693

16901694
int new_udp_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
16911695
if (new_udp_fd < 0) {
@@ -1879,7 +1883,7 @@ int get_src_adress(u32_t &ip)
18791883
//memset(&remote_addr_in, 0, sizeof(remote_addr_in));
18801884
remote_addr_in.sin_family = AF_INET;
18811885
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;
18831887

18841888

18851889
int new_udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
@@ -1888,7 +1892,7 @@ int get_src_adress(u32_t &ip)
18881892
mylog(log_warn,"create udp_fd error\n");
18891893
return -1;
18901894
}
1891-
set_buf_size(new_udp_fd);
1895+
//set_buf_size(new_udp_fd);
18921896

18931897
mylog(log_debug,"created new udp_fd %d\n",new_udp_fd);
18941898
int ret = connect(new_udp_fd, (struct sockaddr *) &remote_addr_in, slen);
@@ -1923,17 +1927,17 @@ int client_event_loop()
19231927
packet_info_t &recv_info=conn_info.raw_info.recv_info;
19241928

19251929
//printf("?????\n");
1926-
if(source_address_uint32==0)
1930+
if(source_ip_uint32==0)
19271931
{
19281932
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)
19301934
{
19311935
mylog(log_fatal,"the trick to auto get source ip failed,you should specific an ip by --source-ip\n");
19321936
myexit(-1);
19331937
}
19341938
}
19351939
in_addr tmp;
1936-
tmp.s_addr=source_address_uint32;
1940+
tmp.s_addr=source_ip_uint32;
19371941
mylog(log_info,"source ip = %s\n",inet_ntoa(tmp));
19381942
//printf("done\n");
19391943

@@ -1944,13 +1948,13 @@ int client_event_loop()
19441948
myexit(-1);
19451949
}
19461950
send_info.src_port=source_port;
1947-
send_info.src_ip = source_address_uint32;
1951+
send_info.src_ip = source_ip_uint32;
19481952

19491953
int i, j, k;int ret;
19501954
init_raw_socket();
19511955

19521956
//init_filter(source_port);
1953-
send_info.dst_ip=remote_address_uint32;
1957+
send_info.dst_ip=remote_ip_uint32;
19541958
send_info.dst_port=remote_port;
19551959

19561960
//g_packet_info.src_ip=source_address_uint32;
@@ -1968,7 +1972,7 @@ int client_event_loop()
19681972
//memset(&local_me, 0, sizeof(local_me));
19691973
local_me.sin_family = AF_INET;
19701974
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;
19721976

19731977

19741978
if (bind(udp_fd, (struct sockaddr*) &local_me, slen) == -1) {
@@ -2137,7 +2141,7 @@ int server_event_loop()
21372141

21382142
int i, j, k;int ret;
21392143

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
21412145

21422146

21432147
if(raw_mode==mode_faketcp)
@@ -2154,7 +2158,7 @@ int server_event_loop()
21542158

21552159
temp_bind_addr.sin_family = AF_INET;
21562160
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;
21582162

21592163
if (bind(bind_fd, (struct sockaddr*)&temp_bind_addr, sizeof(temp_bind_addr)) !=0)
21602164
{
@@ -2370,7 +2374,7 @@ void process_lower_level()
23702374
{
23712375
if (strchr(optarg, '#') == 0) {
23722376
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");
23742378
myexit(-1);
23752379
}
23762380
lower_level = 1;
@@ -2380,10 +2384,10 @@ void process_lower_level()
23802384
&hw[3], &hw[4], &hw[5]);
23812385

23822386
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",
23842388
if_name, hw[0], hw[1], hw[2], hw[3], hw[4], hw[5]);
23852389
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]);
23872391
}
23882392
}
23892393
void print_help()
@@ -2426,7 +2430,7 @@ void print_help()
24262430
printf(" 1:increase every packet\n");
24272431
printf(" 2:increase randomly, about every 3 packets (default)\n");
24282432
// 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");
24302434
printf(" ie:'eth0#00:23:45:67:89:b9'.Beta.\n");
24312435
printf(" -h,--help print this help message\n");
24322436

@@ -2512,7 +2516,7 @@ void process_arg(int argc, char *argv[])
25122516
case 'l':
25132517
no_l = 0;
25142518
if (strchr(optarg, ':') != 0) {
2515-
sscanf(optarg, "%[^:]:%d", local_address, &local_port);
2519+
sscanf(optarg, "%[^:]:%d", local_ip, &local_port);
25162520
if(local_port==22)
25172521
{
25182522
mylog(log_fatal,"port 22 not allowed\n");
@@ -2527,7 +2531,7 @@ void process_arg(int argc, char *argv[])
25272531
case 'r':
25282532
no_r = 0;
25292533
if (strchr(optarg, ':') != 0) {
2530-
sscanf(optarg, "%[^:]:%d", remote_address, &remote_port);
2534+
sscanf(optarg, "%[^:]:%d", remote_ip, &remote_port);
25312535
if(remote_port==22)
25322536
{
25332537
mylog(log_fatal,"port 22 not allowed\n");
@@ -2588,8 +2592,9 @@ void process_arg(int argc, char *argv[])
25882592
else if(strcmp(long_options[option_index].name,"source-ip")==0)
25892593
{
25902594
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;
25932598
}
25942599
else if(strcmp(long_options[option_index].name,"source-port")==0)
25952600
{
@@ -2738,11 +2743,11 @@ void process_arg(int argc, char *argv[])
27382743

27392744
log_bare(log_info,"key=%s ",key_string);
27402745

2741-
log_bare(log_info,"local_ip=%s ",local_address);
2746+
log_bare(log_info,"local_ip=%s ",local_ip);
27422747
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);
27442749
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);
27462751
log_bare(log_info,"source_port=%d ",source_port);
27472752

27482753
log_bare(log_info,"socket_buf_size=%d ",socket_buf_size);
@@ -2756,17 +2761,17 @@ void iptables_rule()
27562761
{
27572762
if(raw_mode==mode_faketcp)
27582763
{
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);
27602765
//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);
27612766
}
27622767
if(raw_mode==mode_udp)
27632768
{
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);
27652770
//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);
27662771
}
27672772
if(raw_mode==mode_icmp)
27682773
{
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);
27702775
//mylog(log_warn,"make sure you have run once: iptables -A INPUT -s %s/32 -p icmp -j DROP\n",remote_address);
27712776
}
27722777
}
@@ -2785,14 +2790,14 @@ void iptables_rule()
27852790
}
27862791
if(raw_mode==mode_icmp)
27872792
{
2788-
if(local_address_uint32==0)
2793+
if(local_ip_uint32==0)
27892794
{
27902795
sprintf(rule,"INPUT -p icmp -j DROP");
27912796
//mylog(log_warn,"make sure you have run once: iptables -A INPUT -p icmp -j DROP\n");
27922797
}
27932798
else
27942799
{
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);
27962801
//mylog(log_warn,"make sure you have run once: iptables -A INPUT -d %s/32 -p icmp -j DROP\n",local_address);
27972802
}
27982803
}
@@ -2849,9 +2854,9 @@ int main(int argc, char *argv[])
28492854
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");
28502855
}
28512856

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);
28552860

28562861

28572862
//current_time_rough=get_current_time();

network.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ char if_name[100]="";
2626

2727
unsigned short g_ip_id_counter=0;
2828

29-
unsigned char oppsite_hw_addr[6]=
29+
unsigned char dest_hw_addr[6]=
3030
{0xff,0xff,0xff,0xff,0xff,0xff};
3131
//{0x00,0x23,0x45,0x67,0x89,0xb9};
3232

@@ -371,7 +371,7 @@ int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
371371
addr.sll_ifindex=ifindex;
372372
addr.sll_halen=ETHER_ADDR_LEN;
373373
addr.sll_protocol=htons(ETH_P_IP);
374-
memcpy(addr.sll_addr,oppsite_hw_addr,ETHER_ADDR_LEN);
374+
memcpy(addr.sll_addr,dest_hw_addr,ETHER_ADDR_LEN);
375375
ret = sendto(raw_send_fd, send_raw_ip_buf, ip_tot_len , 0, (struct sockaddr *) &addr, sizeof (addr));
376376
}
377377
if(ret==-1)

network.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extern int disable_bpf_filter;
1717

1818
extern int lower_level;
1919
extern char if_name[100];
20-
extern unsigned char oppsite_hw_addr[];
20+
extern unsigned char dest_hw_addr[];
2121

2222
struct icmphdr
2323
{

0 commit comments

Comments
 (0)