mirror of
https://github.com/wangyu-/udp2raw.git
synced 2025-01-19 14:29:34 +08:00
auto add iptables rule, fixed Wformat warnings
This commit is contained in:
parent
32fd9f77a5
commit
77eff2e6b1
67
common.cpp
67
common.cpp
@ -9,37 +9,39 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
raw_mode_t raw_mode=mode_faketcp;
|
raw_mode_t raw_mode=mode_faketcp;
|
||||||
unordered_map<int, const char*> raw_mode_tostring = {{mode_faketcp, "faketcp"}, {mode_udp, "udp"}, {mode_icmp, "icmp"}};
|
unordered_map<int, const char*> raw_mode_tostring = {{mode_faketcp, "faketcp"}, {mode_udp, "udp"}, {mode_icmp, "icmp"}};
|
||||||
int socket_buf_size=1024*1024;
|
int socket_buf_size=1024*1024;
|
||||||
static int random_number_fd=-1;
|
static int random_number_fd=-1;
|
||||||
char iptables_rule[200];
|
char iptables_rule[200]="";
|
||||||
program_mode_t program_mode=unset_mode;//0 unset; 1client 2server
|
program_mode_t program_mode=unset_mode;//0 unset; 1client 2server
|
||||||
|
|
||||||
uint64_t get_current_time()
|
u64_t get_current_time()
|
||||||
{
|
{
|
||||||
timespec tmp_time;
|
timespec tmp_time;
|
||||||
clock_gettime(CLOCK_MONOTONIC, &tmp_time);
|
clock_gettime(CLOCK_MONOTONIC, &tmp_time);
|
||||||
return tmp_time.tv_sec*1000+tmp_time.tv_nsec/(1000*1000l);
|
return tmp_time.tv_sec*1000+tmp_time.tv_nsec/(1000*1000l);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t pack_u64(uint32_t a,uint32_t b)
|
u64_t pack_u64(u32_t a,u32_t b)
|
||||||
{
|
{
|
||||||
uint64_t ret=a;
|
u64_t ret=a;
|
||||||
ret<<=32u;
|
ret<<=32u;
|
||||||
ret+=b;
|
ret+=b;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
uint32_t get_u64_h(uint64_t a)
|
u32_t get_u64_h(u64_t a)
|
||||||
{
|
{
|
||||||
return a>>32u;
|
return a>>32u;
|
||||||
}
|
}
|
||||||
uint32_t get_u64_l(uint64_t a)
|
u32_t get_u64_l(u64_t a)
|
||||||
{
|
{
|
||||||
return (a<<32u)>>32u;
|
return (a<<32u)>>32u;
|
||||||
}
|
}
|
||||||
|
|
||||||
char * my_ntoa(uint32_t ip)
|
char * my_ntoa(u32_t ip)
|
||||||
{
|
{
|
||||||
in_addr a;
|
in_addr a;
|
||||||
a.s_addr=ip;
|
a.s_addr=ip;
|
||||||
@ -47,13 +49,31 @@ char * my_ntoa(uint32_t ip)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int add_iptables_rule(char *)
|
int add_iptables_rule(char * s)
|
||||||
{
|
{
|
||||||
|
strcpy(iptables_rule,s);
|
||||||
|
char buf[300]="iptables -A ";
|
||||||
|
strcat(buf,s);
|
||||||
|
if(system(buf)==0)
|
||||||
|
{
|
||||||
|
mylog(log_warn,"auto added iptables rule by: %s\n",buf);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mylog(log_fatal,"auto added iptables failed by: %s\n",buf);
|
||||||
|
myexit(-1);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int remove_iptables_rule(char *)
|
int clear_iptables_rule()
|
||||||
{
|
{
|
||||||
|
if(iptables_rule[0]!=0)
|
||||||
|
{
|
||||||
|
char buf[300]="iptables -D ";
|
||||||
|
strcat(buf,iptables_rule);
|
||||||
|
system(buf);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,39 +90,39 @@ void init_random_number_fd()
|
|||||||
}
|
}
|
||||||
setnonblocking(random_number_fd);
|
setnonblocking(random_number_fd);
|
||||||
}
|
}
|
||||||
uint64_t get_true_random_number_64()
|
u64_t get_true_random_number_64()
|
||||||
{
|
{
|
||||||
uint64_t ret;
|
u64_t ret;
|
||||||
int size=read(random_number_fd,&ret,sizeof(ret));
|
int size=read(random_number_fd,&ret,sizeof(ret));
|
||||||
if(size!=sizeof(ret))
|
if(size!=sizeof(ret))
|
||||||
{
|
{
|
||||||
mylog(log_fatal,"get random number failed\n",size);
|
mylog(log_fatal,"get random number failed %d\n",size);
|
||||||
myexit(-1);
|
myexit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
uint32_t get_true_random_number()
|
u32_t get_true_random_number()
|
||||||
{
|
{
|
||||||
uint32_t ret;
|
u32_t ret;
|
||||||
int size=read(random_number_fd,&ret,sizeof(ret));
|
int size=read(random_number_fd,&ret,sizeof(ret));
|
||||||
if(size!=sizeof(ret))
|
if(size!=sizeof(ret))
|
||||||
{
|
{
|
||||||
mylog(log_fatal,"get random number failed\n",size);
|
mylog(log_fatal,"get random number failed %d\n",size);
|
||||||
myexit(-1);
|
myexit(-1);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
uint32_t get_true_random_number_nz() //nz for non-zero
|
u32_t get_true_random_number_nz() //nz for non-zero
|
||||||
{
|
{
|
||||||
uint32_t ret=0;
|
u32_t ret=0;
|
||||||
while(ret==0)
|
while(ret==0)
|
||||||
{
|
{
|
||||||
ret=get_true_random_number();
|
ret=get_true_random_number();
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
uint64_t ntoh64(uint64_t a)
|
u64_t ntoh64(u64_t a)
|
||||||
{
|
{
|
||||||
if(__BYTE_ORDER == __LITTLE_ENDIAN)
|
if(__BYTE_ORDER == __LITTLE_ENDIAN)
|
||||||
{
|
{
|
||||||
@ -111,7 +131,7 @@ uint64_t ntoh64(uint64_t a)
|
|||||||
else return a;
|
else return a;
|
||||||
|
|
||||||
}
|
}
|
||||||
uint64_t hton64(uint64_t a)
|
u64_t hton64(u64_t a)
|
||||||
{
|
{
|
||||||
if(__BYTE_ORDER == __LITTLE_ENDIAN)
|
if(__BYTE_ORDER == __LITTLE_ENDIAN)
|
||||||
{
|
{
|
||||||
@ -183,7 +203,8 @@ int set_buf_size(int fd)
|
|||||||
void myexit(int a)
|
void myexit(int a)
|
||||||
{
|
{
|
||||||
if(enable_log_color)
|
if(enable_log_color)
|
||||||
printf(RESET);
|
puts(RESET);
|
||||||
|
clear_iptables_rule();
|
||||||
exit(a);
|
exit(a);
|
||||||
}
|
}
|
||||||
void INThandler(int sig)
|
void INThandler(int sig)
|
||||||
@ -219,13 +240,13 @@ int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool larger_than_u32(uint32_t a,uint32_t b)
|
bool larger_than_u32(u32_t a,u32_t b)
|
||||||
{
|
{
|
||||||
|
|
||||||
uint32_t smaller,bigger;
|
u32_t smaller,bigger;
|
||||||
smaller=min(a,b);//smaller in normal sense
|
smaller=min(a,b);//smaller in normal sense
|
||||||
bigger=max(a,b);
|
bigger=max(a,b);
|
||||||
uint32_t distance=min(bigger-smaller,smaller+(0xffffffff-bigger+1));
|
u32_t distance=min(bigger-smaller,smaller+(0xffffffff-bigger+1));
|
||||||
if(distance==bigger-smaller)
|
if(distance==bigger-smaller)
|
||||||
{
|
{
|
||||||
if(bigger==a)
|
if(bigger==a)
|
||||||
|
73
common.h
73
common.h
@ -7,13 +7,14 @@
|
|||||||
|
|
||||||
#ifndef COMMON_H_
|
#ifndef COMMON_H_
|
||||||
#define COMMON_H_
|
#define COMMON_H_
|
||||||
|
#define __STDC_FORMAT_MACROS 1
|
||||||
//#define __STDC_FORMAT_MACROS 1
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
|
||||||
#include<stdio.h>
|
#include<stdio.h>
|
||||||
#include<string.h>
|
#include<string.h>
|
||||||
#include<stdlib.h>
|
#include<stdlib.h>
|
||||||
#include<getopt.h>
|
#include<getopt.h>
|
||||||
|
|
||||||
#include<unistd.h>
|
#include<unistd.h>
|
||||||
#include<errno.h>
|
#include<errno.h>
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
@ -48,41 +49,49 @@
|
|||||||
#include<unordered_map>
|
#include<unordered_map>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64
|
||||||
|
typedef long long i64_t;
|
||||||
|
|
||||||
|
typedef unsigned int u32_t;
|
||||||
|
typedef int i32_t;
|
||||||
|
|
||||||
|
|
||||||
const int max_data_len=1600;
|
const int max_data_len=1600;
|
||||||
const int buf_len=max_data_len+200;
|
const int buf_len=max_data_len+200;
|
||||||
const uint32_t max_handshake_conn_num=10000;
|
const u32_t max_handshake_conn_num=10000;
|
||||||
const uint32_t max_ready_conn_num=1000;
|
const u32_t max_ready_conn_num=1000;
|
||||||
const uint32_t anti_replay_window_size=1000;
|
const u32_t anti_replay_window_size=1000;
|
||||||
const int max_conv_num=10000;
|
const int max_conv_num=10000;
|
||||||
|
|
||||||
const uint32_t client_handshake_timeout=5000;
|
const u32_t client_handshake_timeout=5000;
|
||||||
const uint32_t client_retry_interval=1000;
|
const u32_t client_retry_interval=1000;
|
||||||
|
|
||||||
const uint32_t server_handshake_timeout=10000;// this should be much longer than clients. client retry initially ,server retry passtively
|
const u32_t server_handshake_timeout=10000;// this should be much longer than clients. client retry initially ,server retry passtively
|
||||||
|
|
||||||
const int conv_clear_ratio=10; //conv grabage collecter check 1/10 of all conv one time
|
const int conv_clear_ratio=10; //conv grabage collecter check 1/10 of all conv one time
|
||||||
const int conn_clear_ratio=10;
|
const int conn_clear_ratio=10;
|
||||||
const int conv_clear_min=5;
|
const int conv_clear_min=5;
|
||||||
const int conn_clear_min=1;
|
const int conn_clear_min=1;
|
||||||
|
|
||||||
const uint32_t conv_clear_interval=1000;
|
const u32_t conv_clear_interval=1000;
|
||||||
const uint32_t conn_clear_interval=1000;
|
const u32_t conn_clear_interval=1000;
|
||||||
|
|
||||||
|
|
||||||
const int max_fail_time=100000;
|
const int max_fail_time=100000;
|
||||||
|
|
||||||
|
|
||||||
const uint32_t heartbeat_interval=1000;
|
const u32_t heartbeat_interval=1000;
|
||||||
|
|
||||||
const uint32_t timer_interval=400;//this should be smaller than heartbeat_interval and retry interval;
|
const u32_t timer_interval=400;//this should be smaller than heartbeat_interval and retry interval;
|
||||||
|
|
||||||
//const uint32_t conv_timeout=120000; //120 second
|
//const uint32_t conv_timeout=120000; //120 second
|
||||||
const uint32_t conv_timeout=30000; //for test
|
const u32_t conv_timeout=30000; //for test
|
||||||
|
|
||||||
const uint32_t client_conn_timeout=10000;
|
const u32_t client_conn_timeout=10000;
|
||||||
|
|
||||||
//const uint32_t server_conn_timeout=conv_timeout+60000;//this should be 60s+ longer than conv_timeout,so that conv_manager can destruct convs gradually,to avoid latency glicth
|
//const uint32_t server_conn_timeout=conv_timeout+60000;//this should be 60s+ longer than conv_timeout,so that conv_manager can destruct convs gradually,to avoid latency glicth
|
||||||
const uint32_t server_conn_timeout=conv_timeout+10000;//for test
|
const u32_t server_conn_timeout=conv_timeout+10000;//for test
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -93,32 +102,32 @@ extern program_mode_t program_mode;
|
|||||||
extern unordered_map<int, const char*> raw_mode_tostring ;
|
extern unordered_map<int, const char*> raw_mode_tostring ;
|
||||||
extern int socket_buf_size;
|
extern int socket_buf_size;
|
||||||
|
|
||||||
typedef uint32_t id_t;
|
typedef u32_t id_t;
|
||||||
|
|
||||||
typedef uint64_t iv_t;
|
typedef u64_t iv_t;
|
||||||
|
|
||||||
typedef uint64_t padding_t;
|
typedef u64_t padding_t;
|
||||||
|
|
||||||
typedef uint64_t anti_replay_seq_t;
|
typedef u64_t anti_replay_seq_t;
|
||||||
|
|
||||||
uint64_t get_current_time();
|
u64_t get_current_time();
|
||||||
uint64_t pack_u64(uint32_t a,uint32_t b);
|
u64_t pack_u64(u32_t a,u32_t b);
|
||||||
|
|
||||||
uint32_t get_u64_h(uint64_t a);
|
u32_t get_u64_h(u64_t a);
|
||||||
|
|
||||||
uint32_t get_u64_l(uint64_t a);
|
u32_t get_u64_l(u64_t a);
|
||||||
|
|
||||||
char * my_ntoa(uint32_t ip);
|
char * my_ntoa(u32_t ip);
|
||||||
|
|
||||||
void myexit(int a);
|
void myexit(int a);
|
||||||
void init_random_number_fd();
|
void init_random_number_fd();
|
||||||
uint64_t get_true_random_number_64();
|
u64_t get_true_random_number_64();
|
||||||
uint32_t get_true_random_number();
|
u32_t get_true_random_number();
|
||||||
uint32_t get_true_random_number_nz();
|
u32_t get_true_random_number_nz();
|
||||||
uint64_t ntoh64(uint64_t a);
|
u64_t ntoh64(u64_t a);
|
||||||
uint64_t hton64(uint64_t a);
|
u64_t hton64(u64_t a);
|
||||||
bool larger_than_u16(uint16_t a,uint16_t b);
|
bool larger_than_u16(uint16_t a,uint16_t b);
|
||||||
bool larger_than_u32(uint32_t a,uint32_t b);
|
bool larger_than_u32(u32_t a,u32_t b);
|
||||||
void setnonblocking(int sock);
|
void setnonblocking(int sock);
|
||||||
int set_buf_size(int fd);
|
int set_buf_size(int fd);
|
||||||
|
|
||||||
@ -130,4 +139,8 @@ int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3);
|
|||||||
|
|
||||||
void myexit(int a);
|
void myexit(int a);
|
||||||
|
|
||||||
|
int add_iptables_rule(char *);
|
||||||
|
|
||||||
|
int clear_iptables_rule();
|
||||||
|
|
||||||
#endif /* COMMON_H_ */
|
#endif /* COMMON_H_ */
|
||||||
|
@ -213,7 +213,7 @@ int cipher_none_encrypt(const char *data,char *output,int &len,char * key)
|
|||||||
int cipher_aes128cbc_decrypt(const char *data,char *output,int &len,char * key)
|
int cipher_aes128cbc_decrypt(const char *data,char *output,int &len,char * key)
|
||||||
{
|
{
|
||||||
|
|
||||||
if(len%16 !=0) {mylog(log_debug,"len%16!=0\n");return -1;}
|
if(len%16 !=0) {mylog(log_debug,"len%%16!=0\n");return -1;}
|
||||||
//if(len<0) {mylog(log_debug,"len <0\n");return -1;}
|
//if(len<0) {mylog(log_debug,"len <0\n");return -1;}
|
||||||
AES_CBC_decrypt_buffer((unsigned char *)output,(unsigned char *)data,len,(unsigned char *)key,(unsigned char *)zero_iv);
|
AES_CBC_decrypt_buffer((unsigned char *)output,(unsigned char *)data,len,(unsigned char *)key,(unsigned char *)zero_iv);
|
||||||
if(de_padding(output,len,16)<0) return -1;
|
if(de_padding(output,len,16)<0) return -1;
|
||||||
|
8
log.cpp
8
log.cpp
@ -20,7 +20,7 @@ void log0(const char * file,const char * function,int line,int level,const char*
|
|||||||
tm_info = localtime(&timer);
|
tm_info = localtime(&timer);
|
||||||
|
|
||||||
if(enable_log_color)
|
if(enable_log_color)
|
||||||
printf(log_color[level]);
|
puts(log_color[level]);
|
||||||
|
|
||||||
strftime(buffer, 100, "%Y-%m-%d %H:%M:%S", tm_info);
|
strftime(buffer, 100, "%Y-%m-%d %H:%M:%S", tm_info);
|
||||||
printf("[%s][%s]",buffer,log_text[level]);
|
printf("[%s][%s]",buffer,log_text[level]);
|
||||||
@ -32,7 +32,7 @@ void log0(const char * file,const char * function,int line,int level,const char*
|
|||||||
vfprintf(stdout, str, vlist);
|
vfprintf(stdout, str, vlist);
|
||||||
va_end(vlist);
|
va_end(vlist);
|
||||||
if(enable_log_color)
|
if(enable_log_color)
|
||||||
printf(RESET);
|
puts(RESET);
|
||||||
|
|
||||||
//printf("\n");
|
//printf("\n");
|
||||||
//if(enable_log_color)
|
//if(enable_log_color)
|
||||||
@ -46,13 +46,13 @@ void log_bare(int level,const char* str, ...)
|
|||||||
if(level>log_level) return ;
|
if(level>log_level) return ;
|
||||||
if(level>log_trace||level<0) return ;
|
if(level>log_trace||level<0) return ;
|
||||||
if(enable_log_color)
|
if(enable_log_color)
|
||||||
printf(log_color[level]);
|
puts(log_color[level]);
|
||||||
va_list vlist;
|
va_list vlist;
|
||||||
va_start(vlist, str);
|
va_start(vlist, str);
|
||||||
vfprintf(stdout, str, vlist);
|
vfprintf(stdout, str, vlist);
|
||||||
va_end(vlist);
|
va_end(vlist);
|
||||||
if(enable_log_color)
|
if(enable_log_color)
|
||||||
printf(RESET);
|
puts(RESET);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
8
log.h
8
log.h
@ -83,9 +83,13 @@ extern int enable_log_position;
|
|||||||
extern int enable_log_color;
|
extern int enable_log_color;
|
||||||
|
|
||||||
|
|
||||||
#define mylog(...) log0(__FILE__,__FUNCTION__,__LINE__,__VA_ARGS__)
|
#ifdef MY_DEBUG
|
||||||
|
#define mylog(__first_argu__dummy_abcde__,...) printf(__VA_ARGS__)
|
||||||
|
|
||||||
|
#else
|
||||||
|
#define mylog(...) log0(__FILE__,__FUNCTION__,__LINE__,__VA_ARGS__)
|
||||||
|
#endif
|
||||||
|
|
||||||
//#define mylog(__first_argu__dummy_abcde__,...) printf(__VA_ARGS__)
|
|
||||||
|
|
||||||
//#define mylog(__first_argu__dummy_abcde__,...) {;}
|
//#define mylog(__first_argu__dummy_abcde__,...) {;}
|
||||||
|
|
||||||
|
239
main.cpp
239
main.cpp
@ -4,7 +4,7 @@
|
|||||||
#include "md5.h"
|
#include "md5.h"
|
||||||
|
|
||||||
char local_address[100]="0.0.0.0", remote_address[100]="255.255.255.255",source_address[100]="0.0.0.0";
|
char local_address[100]="0.0.0.0", remote_address[100]="255.255.255.255",source_address[100]="0.0.0.0";
|
||||||
uint32_t local_address_uint32,remote_address_uint32,source_address_uint32;
|
u32_t local_address_uint32,remote_address_uint32,source_address_uint32;
|
||||||
int source_port=0,local_port = -1, remote_port = -1;
|
int source_port=0,local_port = -1, remote_port = -1;
|
||||||
|
|
||||||
id_t const_id=0;
|
id_t const_id=0;
|
||||||
@ -30,8 +30,9 @@ int fail_time_counter=0;
|
|||||||
int epoll_trigger_counter=0;
|
int epoll_trigger_counter=0;
|
||||||
int debug_flag=0;
|
int debug_flag=0;
|
||||||
int auto_add_iptables_rule=0;
|
int auto_add_iptables_rule=0;
|
||||||
//int debug_resend=0;
|
|
||||||
|
|
||||||
|
int debug_resend=0;
|
||||||
|
int disable_anti_replay=0;
|
||||||
char key_string[1000]= "secret key";
|
char key_string[1000]= "secret key";
|
||||||
char key[16];//,key2[16];
|
char key[16];//,key2[16];
|
||||||
|
|
||||||
@ -43,7 +44,7 @@ int VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
|
|||||||
|
|
||||||
struct anti_replay_t
|
struct anti_replay_t
|
||||||
{
|
{
|
||||||
uint64_t max_packet_received;
|
u64_t max_packet_received;
|
||||||
char window[anti_replay_window_size];
|
char window[anti_replay_window_size];
|
||||||
char disabled;
|
char disabled;
|
||||||
anti_replay_seq_t anti_replay_seq;
|
anti_replay_seq_t anti_replay_seq;
|
||||||
@ -53,14 +54,14 @@ struct anti_replay_t
|
|||||||
}
|
}
|
||||||
anti_replay_t()
|
anti_replay_t()
|
||||||
{
|
{
|
||||||
disabled=0;
|
disabled=disable_anti_replay;
|
||||||
max_packet_received=0;
|
max_packet_received=0;
|
||||||
anti_replay_seq=get_true_random_number_64()/10;//random first seq
|
anti_replay_seq=get_true_random_number_64()/10;//random first seq
|
||||||
//memset(window,0,sizeof(window)); //not necessary
|
//memset(window,0,sizeof(window)); //not necessary
|
||||||
}
|
}
|
||||||
void re_init()
|
void re_init()
|
||||||
{
|
{
|
||||||
disabled=0;
|
disabled=disable_anti_replay;
|
||||||
max_packet_received=0;
|
max_packet_received=0;
|
||||||
//memset(window,0,sizeof(window));
|
//memset(window,0,sizeof(window));
|
||||||
}
|
}
|
||||||
@ -73,7 +74,7 @@ struct anti_replay_t
|
|||||||
disabled=0;
|
disabled=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_vaild(uint64_t seq)
|
int is_vaild(u64_t seq)
|
||||||
{
|
{
|
||||||
//if(disabled) return 0;
|
//if(disabled) return 0;
|
||||||
|
|
||||||
@ -87,7 +88,7 @@ struct anti_replay_t
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (uint64_t i=max_packet_received+1;i<seq;i++)
|
for (u64_t i=max_packet_received+1;i<seq;i++)
|
||||||
window[i%anti_replay_window_size]=0;
|
window[i%anti_replay_window_size]=0;
|
||||||
window[seq%anti_replay_window_size]=1;
|
window[seq%anti_replay_window_size]=1;
|
||||||
}
|
}
|
||||||
@ -113,19 +114,19 @@ struct anti_replay_t
|
|||||||
}
|
}
|
||||||
};//anti_replay;
|
};//anti_replay;
|
||||||
|
|
||||||
void server_clear_function(uint64_t u64);
|
void server_clear_function(u64_t u64);
|
||||||
struct conv_manager_t //TODO change map to unordered map
|
struct conv_manager_t //TODO change map to unordered map
|
||||||
{
|
{
|
||||||
//typedef hash_map map;
|
//typedef hash_map map;
|
||||||
unordered_map<uint64_t,uint32_t> u64_to_conv; //conv and u64 are both supposed to be uniq
|
unordered_map<u64_t,u32_t> u64_to_conv; //conv and u64 are both supposed to be uniq
|
||||||
unordered_map<uint32_t,uint64_t> conv_to_u64;
|
unordered_map<u32_t,u64_t> conv_to_u64;
|
||||||
|
|
||||||
unordered_map<uint32_t,uint64_t> conv_last_active_time;
|
unordered_map<u32_t,u64_t> conv_last_active_time;
|
||||||
|
|
||||||
unordered_map<uint32_t,uint64_t>::iterator clear_it;
|
unordered_map<u32_t,u64_t>::iterator clear_it;
|
||||||
|
|
||||||
unordered_map<uint32_t,uint64_t>::iterator it;
|
unordered_map<u32_t,u64_t>::iterator it;
|
||||||
unordered_map<uint32_t,uint64_t>::iterator old_it;
|
unordered_map<u32_t,u64_t>::iterator old_it;
|
||||||
|
|
||||||
//void (*clear_function)(uint64_t u64) ;
|
//void (*clear_function)(uint64_t u64) ;
|
||||||
|
|
||||||
@ -170,46 +171,46 @@ struct conv_manager_t //TODO change map to unordered map
|
|||||||
clear_it=conv_last_active_time.begin();
|
clear_it=conv_last_active_time.begin();
|
||||||
|
|
||||||
}
|
}
|
||||||
uint32_t get_new_conv()
|
u32_t get_new_conv()
|
||||||
{
|
{
|
||||||
uint32_t conv=get_true_random_number_nz();
|
u32_t conv=get_true_random_number_nz();
|
||||||
while(conv_to_u64.find(conv)!=conv_to_u64.end())
|
while(conv_to_u64.find(conv)!=conv_to_u64.end())
|
||||||
{
|
{
|
||||||
conv=get_true_random_number_nz();
|
conv=get_true_random_number_nz();
|
||||||
}
|
}
|
||||||
return conv;
|
return conv;
|
||||||
}
|
}
|
||||||
int is_conv_used(uint32_t conv)
|
int is_conv_used(u32_t conv)
|
||||||
{
|
{
|
||||||
return conv_to_u64.find(conv)!=conv_to_u64.end();
|
return conv_to_u64.find(conv)!=conv_to_u64.end();
|
||||||
}
|
}
|
||||||
int is_u64_used(uint64_t u64)
|
int is_u64_used(u64_t u64)
|
||||||
{
|
{
|
||||||
return u64_to_conv.find(u64)!=u64_to_conv.end();
|
return u64_to_conv.find(u64)!=u64_to_conv.end();
|
||||||
}
|
}
|
||||||
uint32_t find_conv_by_u64(uint64_t u64)
|
u32_t find_conv_by_u64(u64_t u64)
|
||||||
{
|
{
|
||||||
return u64_to_conv[u64];
|
return u64_to_conv[u64];
|
||||||
}
|
}
|
||||||
uint64_t find_u64_by_conv(uint32_t conv)
|
u64_t find_u64_by_conv(u32_t conv)
|
||||||
{
|
{
|
||||||
return conv_to_u64[conv];
|
return conv_to_u64[conv];
|
||||||
}
|
}
|
||||||
int update_active_time(uint32_t conv)
|
int update_active_time(u32_t conv)
|
||||||
{
|
{
|
||||||
return conv_last_active_time[conv]=get_current_time();
|
return conv_last_active_time[conv]=get_current_time();
|
||||||
}
|
}
|
||||||
int insert_conv(uint32_t conv,uint64_t u64)
|
int insert_conv(u32_t conv,u64_t u64)
|
||||||
{
|
{
|
||||||
u64_to_conv[u64]=conv;
|
u64_to_conv[u64]=conv;
|
||||||
conv_to_u64[conv]=u64;
|
conv_to_u64[conv]=u64;
|
||||||
conv_last_active_time[conv]=get_current_time();
|
conv_last_active_time[conv]=get_current_time();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int erase_conv(uint32_t conv)
|
int erase_conv(u32_t conv)
|
||||||
{
|
{
|
||||||
if(disable_conv_clear) return 0;
|
if(disable_conv_clear) return 0;
|
||||||
uint64_t u64=conv_to_u64[conv];
|
u64_t u64=conv_to_u64[conv];
|
||||||
if(program_mode==server_mode)
|
if(program_mode==server_mode)
|
||||||
{
|
{
|
||||||
server_clear_function(u64);
|
server_clear_function(u64);
|
||||||
@ -240,7 +241,7 @@ struct conv_manager_t //TODO change map to unordered map
|
|||||||
int size=conv_last_active_time.size();
|
int size=conv_last_active_time.size();
|
||||||
int num_to_clean=size/conv_clear_ratio+conv_clear_min; //clear 1/10 each time,to avoid latency glitch
|
int num_to_clean=size/conv_clear_ratio+conv_clear_min; //clear 1/10 each time,to avoid latency glitch
|
||||||
|
|
||||||
uint64_t current_time=get_current_time();
|
u64_t current_time=get_current_time();
|
||||||
for(;;)
|
for(;;)
|
||||||
{
|
{
|
||||||
if(cnt>=num_to_clean) break;
|
if(cnt>=num_to_clean) break;
|
||||||
@ -278,9 +279,9 @@ struct conn_info_t
|
|||||||
current_state_t state;
|
current_state_t state;
|
||||||
|
|
||||||
raw_info_t raw_info;
|
raw_info_t raw_info;
|
||||||
long long last_state_time;
|
u64_t last_state_time;
|
||||||
long long last_hb_sent_time; //client re-use this for retry
|
u64_t last_hb_sent_time; //client re-use this for retry
|
||||||
long long last_hb_recv_time;
|
u64_t last_hb_recv_time;
|
||||||
//long long last_resent_time;
|
//long long last_resent_time;
|
||||||
|
|
||||||
id_t my_id;
|
id_t my_id;
|
||||||
@ -344,16 +345,16 @@ struct conn_info_t
|
|||||||
struct conn_manager_t
|
struct conn_manager_t
|
||||||
{
|
{
|
||||||
|
|
||||||
uint32_t ready_num;
|
u32_t ready_num;
|
||||||
|
|
||||||
unordered_map<int,conn_info_t *> udp_fd_mp; //a bit dirty to used pointer,but can void unordered_map search
|
unordered_map<int,conn_info_t *> udp_fd_mp; //a bit dirty to used pointer,but can void unordered_map search
|
||||||
unordered_map<int,conn_info_t *> timer_fd_mp;//we can use pointer here since unordered_map.rehash() uses shallow copy
|
unordered_map<int,conn_info_t *> timer_fd_mp;//we can use pointer here since unordered_map.rehash() uses shallow copy
|
||||||
|
|
||||||
unordered_map<id_t,conn_info_t *> const_id_mp;
|
unordered_map<id_t,conn_info_t *> const_id_mp;
|
||||||
|
|
||||||
unordered_map<uint64_t,conn_info_t*> mp; //put it at end so that it de-consturcts first
|
unordered_map<u64_t,conn_info_t*> mp; //put it at end so that it de-consturcts first
|
||||||
|
|
||||||
unordered_map<uint64_t,conn_info_t*>::iterator clear_it;
|
unordered_map<u64_t,conn_info_t*>::iterator clear_it;
|
||||||
|
|
||||||
long long last_clear_time;
|
long long last_clear_time;
|
||||||
|
|
||||||
@ -369,9 +370,9 @@ struct conn_manager_t
|
|||||||
//current_ready_ip=0;
|
//current_ready_ip=0;
|
||||||
// current_ready_port=0;
|
// current_ready_port=0;
|
||||||
}
|
}
|
||||||
int exist(uint32_t ip,uint16_t port)
|
int exist(u32_t ip,uint16_t port)
|
||||||
{
|
{
|
||||||
uint64_t u64=0;
|
u64_t u64=0;
|
||||||
u64=ip;
|
u64=ip;
|
||||||
u64<<=32u;
|
u64<<=32u;
|
||||||
u64|=port;
|
u64|=port;
|
||||||
@ -391,38 +392,38 @@ struct conn_manager_t
|
|||||||
mp[u64];
|
mp[u64];
|
||||||
return 0;
|
return 0;
|
||||||
}*/
|
}*/
|
||||||
conn_info_t *& find_insert_p(uint32_t ip,uint16_t port) //be aware,the adress may change after rehash
|
conn_info_t *& find_insert_p(u32_t ip,uint16_t port) //be aware,the adress may change after rehash
|
||||||
{
|
{
|
||||||
uint64_t u64=0;
|
u64_t u64=0;
|
||||||
u64=ip;
|
u64=ip;
|
||||||
u64<<=32u;
|
u64<<=32u;
|
||||||
u64|=port;
|
u64|=port;
|
||||||
unordered_map<uint64_t,conn_info_t*>::iterator it=mp.find(u64);
|
unordered_map<u64_t,conn_info_t*>::iterator it=mp.find(u64);
|
||||||
if(it==mp.end())
|
if(it==mp.end())
|
||||||
{
|
{
|
||||||
mp[u64]=new conn_info_t;
|
mp[u64]=new conn_info_t;
|
||||||
}
|
}
|
||||||
return mp[u64];
|
return mp[u64];
|
||||||
}
|
}
|
||||||
conn_info_t & find_insert(uint32_t ip,uint16_t port) //be aware,the adress may change after rehash
|
conn_info_t & find_insert(u32_t ip,uint16_t port) //be aware,the adress may change after rehash
|
||||||
{
|
{
|
||||||
uint64_t u64=0;
|
u64_t u64=0;
|
||||||
u64=ip;
|
u64=ip;
|
||||||
u64<<=32u;
|
u64<<=32u;
|
||||||
u64|=port;
|
u64|=port;
|
||||||
unordered_map<uint64_t,conn_info_t*>::iterator it=mp.find(u64);
|
unordered_map<u64_t,conn_info_t*>::iterator it=mp.find(u64);
|
||||||
if(it==mp.end())
|
if(it==mp.end())
|
||||||
{
|
{
|
||||||
mp[u64]=new conn_info_t;
|
mp[u64]=new conn_info_t;
|
||||||
}
|
}
|
||||||
return *mp[u64];
|
return *mp[u64];
|
||||||
}
|
}
|
||||||
int erase(unordered_map<uint64_t,conn_info_t*>::iterator erase_it)
|
int erase(unordered_map<u64_t,conn_info_t*>::iterator erase_it)
|
||||||
{
|
{
|
||||||
if(erase_it->second->state.server_current_state==server_ready)
|
if(erase_it->second->state.server_current_state==server_ready)
|
||||||
{
|
{
|
||||||
ready_num--;
|
ready_num--;
|
||||||
assert(int32_t(ready_num)!=-1);
|
assert(i32_t(ready_num)!=-1);
|
||||||
assert(erase_it->second!=0);
|
assert(erase_it->second!=0);
|
||||||
assert(erase_it->second->timer_fd !=0);
|
assert(erase_it->second->timer_fd !=0);
|
||||||
assert(erase_it->second->oppsite_const_id!=0);
|
assert(erase_it->second->oppsite_const_id!=0);
|
||||||
@ -456,8 +457,8 @@ int clear_inactive()
|
|||||||
}
|
}
|
||||||
int clear_inactive0()
|
int clear_inactive0()
|
||||||
{
|
{
|
||||||
unordered_map<uint64_t,conn_info_t*>::iterator it;
|
unordered_map<u64_t,conn_info_t*>::iterator it;
|
||||||
unordered_map<uint64_t,conn_info_t*>::iterator old_it;
|
unordered_map<u64_t,conn_info_t*>::iterator old_it;
|
||||||
|
|
||||||
if(disable_conn_clear) return 0;
|
if(disable_conn_clear) return 0;
|
||||||
|
|
||||||
@ -467,10 +468,10 @@ int clear_inactive0()
|
|||||||
int size=mp.size();
|
int size=mp.size();
|
||||||
int num_to_clean=size/conn_clear_ratio+conn_clear_min; //clear 1/10 each time,to avoid latency glitch
|
int num_to_clean=size/conn_clear_ratio+conn_clear_min; //clear 1/10 each time,to avoid latency glitch
|
||||||
|
|
||||||
mylog(log_trace,"mp.size() %d\n",mp.size());
|
mylog(log_trace,"mp.size() %d\n", size);
|
||||||
|
|
||||||
num_to_clean=min(num_to_clean,(int)mp.size());
|
num_to_clean=min(num_to_clean,(int)mp.size());
|
||||||
uint64_t current_time=get_current_time();
|
u64_t current_time=get_current_time();
|
||||||
|
|
||||||
for(;;)
|
for(;;)
|
||||||
{
|
{
|
||||||
@ -537,7 +538,7 @@ int TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
|
|||||||
////////==========================type divider=======================================================
|
////////==========================type divider=======================================================
|
||||||
|
|
||||||
|
|
||||||
int server_on_raw_recv_pre_ready(conn_info_t &conn_info,uint32_t tmp_oppsite_const_id);
|
int server_on_raw_recv_pre_ready(conn_info_t &conn_info,u32_t tmp_oppsite_const_id);
|
||||||
int server_on_raw_recv_ready(conn_info_t &conn_info);
|
int server_on_raw_recv_ready(conn_info_t &conn_info);
|
||||||
int server_on_raw_recv_handshake1(conn_info_t &conn_info,id_t tmp_oppsite_id );
|
int server_on_raw_recv_handshake1(conn_info_t &conn_info,id_t tmp_oppsite_id );
|
||||||
int DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;
|
int DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;
|
||||||
@ -672,7 +673,7 @@ int pre_recv_deprecated(char * data, int &data_len)
|
|||||||
}*/
|
}*/
|
||||||
|
|
||||||
|
|
||||||
void server_clear_function(uint64_t u64)
|
void server_clear_function(u64_t u64)
|
||||||
{
|
{
|
||||||
int fd=int(u64);
|
int fd=int(u64);
|
||||||
int ret;
|
int ret;
|
||||||
@ -850,14 +851,14 @@ int send_safer(conn_info_t &conn_info,const char* data,int len)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int send_data_safer(conn_info_t &conn_info,const char* data,int len,uint32_t conv_num)
|
int send_data_safer(conn_info_t &conn_info,const char* data,int len,u32_t conv_num)
|
||||||
{
|
{
|
||||||
packet_info_t &send_info=conn_info.raw_info.send_info;
|
packet_info_t &send_info=conn_info.raw_info.send_info;
|
||||||
packet_info_t &recv_info=conn_info.raw_info.recv_info;
|
packet_info_t &recv_info=conn_info.raw_info.recv_info;
|
||||||
|
|
||||||
char send_data_buf[buf_len];
|
char send_data_buf[buf_len];
|
||||||
send_data_buf[0]='d';
|
send_data_buf[0]='d';
|
||||||
uint32_t n_conv_num=htonl(conv_num);
|
u32_t n_conv_num=htonl(conv_num);
|
||||||
memcpy(send_data_buf+1,&n_conv_num,sizeof(n_conv_num));
|
memcpy(send_data_buf+1,&n_conv_num,sizeof(n_conv_num));
|
||||||
|
|
||||||
memcpy(send_data_buf+1+sizeof(n_conv_num),data,len);
|
memcpy(send_data_buf+1+sizeof(n_conv_num),data,len);
|
||||||
@ -1235,7 +1236,8 @@ int client_on_timer(conn_info_t &conn_info) //for client
|
|||||||
else if(conn_info.state.client_current_state==client_ready)
|
else if(conn_info.state.client_current_state==client_ready)
|
||||||
{
|
{
|
||||||
fail_time_counter=0;
|
fail_time_counter=0;
|
||||||
mylog(log_trace,"time %lld %lld\n",get_current_time(),conn_info.last_state_time);
|
mylog(log_trace,"time %llu,%llu\n",get_current_time(),conn_info.last_state_time);
|
||||||
|
|
||||||
if(get_current_time()-conn_info.last_hb_recv_time>client_conn_timeout)
|
if(get_current_time()-conn_info.last_hb_recv_time>client_conn_timeout)
|
||||||
{
|
{
|
||||||
conn_info.state.client_current_state=client_idle;
|
conn_info.state.client_current_state=client_idle;
|
||||||
@ -1370,9 +1372,9 @@ int client_on_raw_recv(conn_info_t &conn_info)
|
|||||||
mylog(log_debug,"too short to be a handshake\n");
|
mylog(log_debug,"too short to be a handshake\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
id_t tmp_oppsite_id= ntohl(* ((uint32_t *)&data[0]));
|
id_t tmp_oppsite_id= ntohl(* ((u32_t *)&data[0]));
|
||||||
id_t tmp_my_id=ntohl(* ((uint32_t *)&data[sizeof(id_t)]));
|
id_t tmp_my_id=ntohl(* ((u32_t *)&data[sizeof(id_t)]));
|
||||||
id_t tmp_oppsite_const_id=ntohl(* ((uint32_t *)&data[sizeof(id_t)*2]));
|
id_t tmp_oppsite_const_id=ntohl(* ((u32_t *)&data[sizeof(id_t)*2]));
|
||||||
|
|
||||||
if(tmp_my_id!=conn_info.my_id)
|
if(tmp_my_id!=conn_info.my_id)
|
||||||
{
|
{
|
||||||
@ -1432,13 +1434,13 @@ int client_on_raw_recv(conn_info_t &conn_info)
|
|||||||
conn_info.last_hb_recv_time=get_current_time();
|
conn_info.last_hb_recv_time=get_current_time();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else if(data_len>= int( sizeof(uint32_t)+1 )&&data[0]=='d')
|
else if(data_len>= int( sizeof(u32_t)+1 )&&data[0]=='d')
|
||||||
{
|
{
|
||||||
mylog(log_trace,"received a data from fake tcp,len:%d\n",data_len);
|
mylog(log_trace,"received a data from fake tcp,len:%d\n",data_len);
|
||||||
|
|
||||||
conn_info.last_hb_recv_time=get_current_time();
|
conn_info.last_hb_recv_time=get_current_time();
|
||||||
|
|
||||||
uint32_t tmp_conv_id= ntohl(* ((uint32_t *)&data[1]));
|
u32_t tmp_conv_id= ntohl(* ((u32_t *)&data[1]));
|
||||||
|
|
||||||
if(!conn_info.blob->conv_manager.is_conv_used(tmp_conv_id))
|
if(!conn_info.blob->conv_manager.is_conv_used(tmp_conv_id))
|
||||||
{
|
{
|
||||||
@ -1448,7 +1450,7 @@ int client_on_raw_recv(conn_info_t &conn_info)
|
|||||||
|
|
||||||
conn_info.blob->conv_manager.update_active_time(tmp_conv_id);
|
conn_info.blob->conv_manager.update_active_time(tmp_conv_id);
|
||||||
|
|
||||||
uint64_t u64=conn_info.blob->conv_manager.find_u64_by_conv(tmp_conv_id);
|
u64_t u64=conn_info.blob->conv_manager.find_u64_by_conv(tmp_conv_id);
|
||||||
|
|
||||||
|
|
||||||
sockaddr_in tmp_sockaddr;
|
sockaddr_in tmp_sockaddr;
|
||||||
@ -1459,7 +1461,7 @@ int client_on_raw_recv(conn_info_t &conn_info)
|
|||||||
tmp_sockaddr.sin_port= htons(uint16_t((u64<<32u)>>32u));
|
tmp_sockaddr.sin_port= htons(uint16_t((u64<<32u)>>32u));
|
||||||
|
|
||||||
|
|
||||||
int ret=sendto(udp_fd,data+1+sizeof(uint32_t),data_len -(1+sizeof(uint32_t)),0,(struct sockaddr *)&tmp_sockaddr,sizeof(tmp_sockaddr));
|
int ret=sendto(udp_fd,data+1+sizeof(u32_t),data_len -(1+sizeof(u32_t)),0,(struct sockaddr *)&tmp_sockaddr,sizeof(tmp_sockaddr));
|
||||||
|
|
||||||
if(ret<0)
|
if(ret<0)
|
||||||
{
|
{
|
||||||
@ -1498,7 +1500,7 @@ int server_on_raw_recv_multi()
|
|||||||
mylog(log_trace,"peek_raw failed\n");
|
mylog(log_trace,"peek_raw failed\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
uint32_t ip=peek_info.src_ip;uint16_t port=peek_info.src_port;
|
u32_t ip=peek_info.src_ip;uint16_t port=peek_info.src_port;
|
||||||
mylog(log_trace,"peek_raw %s %d\n",my_ntoa(ip),port);
|
mylog(log_trace,"peek_raw %s %d\n",my_ntoa(ip),port);
|
||||||
char ip_port[40];
|
char ip_port[40];
|
||||||
sprintf(ip_port,"%s:%d",my_ntoa(ip),port);
|
sprintf(ip_port,"%s:%d",my_ntoa(ip),port);
|
||||||
@ -1564,7 +1566,7 @@ int server_on_raw_recv_multi()
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
id_t zero=ntohl(* ((uint32_t *)&data[sizeof(id_t)]));
|
id_t zero=ntohl(* ((u32_t *)&data[sizeof(id_t)]));
|
||||||
if(zero!=0)
|
if(zero!=0)
|
||||||
{
|
{
|
||||||
mylog(log_debug,"[%s]not a invalid initial handshake\n",ip_port);
|
mylog(log_debug,"[%s]not a invalid initial handshake\n",ip_port);
|
||||||
@ -1586,7 +1588,7 @@ int server_on_raw_recv_multi()
|
|||||||
send_info.dst_port = recv_info.src_port;
|
send_info.dst_port = recv_info.src_port;
|
||||||
send_info.dst_ip = recv_info.src_ip;
|
send_info.dst_ip = recv_info.src_ip;
|
||||||
|
|
||||||
id_t tmp_oppsite_id= ntohl(* ((uint32_t *)&data[0]));
|
id_t tmp_oppsite_id= ntohl(* ((u32_t *)&data[0]));
|
||||||
mylog(log_info,"handshake received %x\n",tmp_oppsite_id);
|
mylog(log_info,"handshake received %x\n",tmp_oppsite_id);
|
||||||
|
|
||||||
conn_info.my_id=get_true_random_number_nz();
|
conn_info.my_id=get_true_random_number_nz();
|
||||||
@ -1619,8 +1621,8 @@ int server_on_raw_recv_multi()
|
|||||||
mylog(log_debug,"[%s] data_len=%d too short to be a handshake\n",ip_port,data_len);
|
mylog(log_debug,"[%s] data_len=%d too short to be a handshake\n",ip_port,data_len);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
id_t tmp_oppsite_id= ntohl(* ((uint32_t *)&data[0]));
|
id_t tmp_oppsite_id= ntohl(* ((u32_t *)&data[0]));
|
||||||
id_t tmp_my_id=ntohl(* ((uint32_t *)&data[sizeof(id_t)]));
|
id_t tmp_my_id=ntohl(* ((u32_t *)&data[sizeof(id_t)]));
|
||||||
|
|
||||||
if(tmp_my_id==0) //received init handshake again
|
if(tmp_my_id==0) //received init handshake again
|
||||||
{
|
{
|
||||||
@ -1630,7 +1632,7 @@ int server_on_raw_recv_multi()
|
|||||||
else if(tmp_my_id==conn_info.my_id)
|
else if(tmp_my_id==conn_info.my_id)
|
||||||
{
|
{
|
||||||
conn_info.oppsite_id=tmp_oppsite_id;
|
conn_info.oppsite_id=tmp_oppsite_id;
|
||||||
id_t tmp_oppsite_const_id=ntohl(* ((uint32_t *)&data[sizeof(id_t)*2]));
|
id_t tmp_oppsite_const_id=ntohl(* ((u32_t *)&data[sizeof(id_t)*2]));
|
||||||
|
|
||||||
if(raw_mode==mode_faketcp)
|
if(raw_mode==mode_faketcp)
|
||||||
{
|
{
|
||||||
@ -1704,14 +1706,14 @@ int server_on_raw_recv_ready(conn_info_t &conn_info)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (data[0] == 'h' && data_len == 1) {
|
if (data[0] == 'h' && data_len == 1) {
|
||||||
uint32_t tmp = ntohl(*((uint32_t *) &data[1 + sizeof(uint32_t)]));
|
u32_t tmp = ntohl(*((u32_t *) &data[1 + sizeof(u32_t)]));
|
||||||
mylog(log_debug,"[%s][hb]received hb \n",ip_port);
|
mylog(log_debug,"[%s][hb]received hb \n",ip_port);
|
||||||
conn_info.last_hb_recv_time = get_current_time();
|
conn_info.last_hb_recv_time = get_current_time();
|
||||||
return 0;
|
return 0;
|
||||||
} else if (data[0] == 'd' && data_len >=int( sizeof(uint32_t) + 1))
|
} else if (data[0] == 'd' && data_len >=int( sizeof(u32_t) + 1))
|
||||||
{
|
{
|
||||||
|
|
||||||
uint32_t tmp_conv_id = ntohl(*((uint32_t *) &data[1]));
|
u32_t tmp_conv_id = ntohl(*((u32_t *) &data[1]));
|
||||||
|
|
||||||
conn_info.last_hb_recv_time = get_current_time();
|
conn_info.last_hb_recv_time = get_current_time();
|
||||||
|
|
||||||
@ -1749,8 +1751,8 @@ int server_on_raw_recv_ready(conn_info_t &conn_info)
|
|||||||
}
|
}
|
||||||
struct epoll_event ev;
|
struct epoll_event ev;
|
||||||
|
|
||||||
uint64_t u64 = (uint32_t(new_udp_fd))+(1llu<<32u);
|
u64_t u64 = (u32_t(new_udp_fd))+(1llu<<32u);
|
||||||
mylog(log_trace, "u64: %ld\n", u64);
|
mylog(log_trace, "u64: %lld\n", u64);
|
||||||
ev.events = EPOLLIN;
|
ev.events = EPOLLIN;
|
||||||
|
|
||||||
ev.data.u64 = u64;
|
ev.data.u64 = u64;
|
||||||
@ -1777,15 +1779,15 @@ int server_on_raw_recv_ready(conn_info_t &conn_info)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t u64 = conn_info.blob->conv_manager.find_u64_by_conv(tmp_conv_id);
|
u64_t u64 = conn_info.blob->conv_manager.find_u64_by_conv(tmp_conv_id);
|
||||||
|
|
||||||
conn_info.blob->conv_manager.update_active_time(tmp_conv_id);
|
conn_info.blob->conv_manager.update_active_time(tmp_conv_id);
|
||||||
|
|
||||||
int fd = int((u64 << 32u) >> 32u);
|
int fd = int((u64 << 32u) >> 32u);
|
||||||
|
|
||||||
mylog(log_trace, "received a data from fake tcp,len:%d\n", data_len);
|
mylog(log_trace, "received a data from fake tcp,len:%d\n", data_len);
|
||||||
int ret = send(fd, data + 1 + sizeof(uint32_t),
|
int ret = send(fd, data + 1 + sizeof(u32_t),
|
||||||
data_len - (1 + sizeof(uint32_t)), 0);
|
data_len - (1 + sizeof(u32_t)), 0);
|
||||||
|
|
||||||
mylog(log_trace, "%d byte sent ,fd :%d\n ", ret, fd);
|
mylog(log_trace, "%d byte sent ,fd :%d\n ", ret, fd);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
@ -1797,9 +1799,9 @@ int server_on_raw_recv_ready(conn_info_t &conn_info)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int server_on_raw_recv_pre_ready(conn_info_t &conn_info,uint32_t tmp_oppsite_const_id)
|
int server_on_raw_recv_pre_ready(conn_info_t &conn_info,u32_t tmp_oppsite_const_id)
|
||||||
{
|
{
|
||||||
uint32_t ip;uint16_t port;
|
u32_t ip;uint16_t port;
|
||||||
ip=conn_info.raw_info.recv_info.src_ip;
|
ip=conn_info.raw_info.recv_info.src_ip;
|
||||||
port=conn_info.raw_info.recv_info.src_port;
|
port=conn_info.raw_info.recv_info.src_port;
|
||||||
char ip_port[40];
|
char ip_port[40];
|
||||||
@ -1915,7 +1917,7 @@ int server_on_raw_recv_pre_ready(conn_info_t &conn_info,uint32_t tmp_oppsite_con
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_src_adress(uint32_t &ip)
|
int get_src_adress(u32_t &ip)
|
||||||
{
|
{
|
||||||
struct sockaddr_in remote_addr_in;
|
struct sockaddr_in remote_addr_in;
|
||||||
|
|
||||||
@ -2066,21 +2068,21 @@ int client_event_loop()
|
|||||||
}
|
}
|
||||||
int idx;
|
int idx;
|
||||||
for (idx = 0; idx < nfds; ++idx) {
|
for (idx = 0; idx < nfds; ++idx) {
|
||||||
if (events[idx].data.u64 == (uint64_t)raw_recv_fd)
|
if (events[idx].data.u64 == (u64_t)raw_recv_fd)
|
||||||
{
|
{
|
||||||
iphdr *iph;tcphdr *tcph;
|
iphdr *iph;tcphdr *tcph;
|
||||||
client_on_raw_recv(conn_info);
|
client_on_raw_recv(conn_info);
|
||||||
}
|
}
|
||||||
else if(events[idx].data.u64 ==(uint64_t)timer_fd)
|
else if(events[idx].data.u64 ==(u64_t)timer_fd)
|
||||||
{
|
{
|
||||||
uint64_t value;
|
u64_t value;
|
||||||
read(timer_fd, &value, 8);
|
read(timer_fd, &value, 8);
|
||||||
client_on_timer(conn_info);
|
client_on_timer(conn_info);
|
||||||
|
|
||||||
mylog(log_trace,"epoll_trigger_counter: %d \n",epoll_trigger_counter);
|
mylog(log_trace,"epoll_trigger_counter: %d \n",epoll_trigger_counter);
|
||||||
epoll_trigger_counter=0;
|
epoll_trigger_counter=0;
|
||||||
}
|
}
|
||||||
else if (events[idx].data.u64 == (uint64_t)udp_fd)
|
else if (events[idx].data.u64 == (u64_t)udp_fd)
|
||||||
{
|
{
|
||||||
|
|
||||||
int recv_len;
|
int recv_len;
|
||||||
@ -2116,8 +2118,8 @@ int client_event_loop()
|
|||||||
}*/
|
}*/
|
||||||
|
|
||||||
//last_udp_recv_time=get_current_time();
|
//last_udp_recv_time=get_current_time();
|
||||||
uint64_t u64=((uint64_t(udp_new_addr_in.sin_addr.s_addr))<<32u)+ntohs(udp_new_addr_in.sin_port);
|
u64_t u64=((u64_t(udp_new_addr_in.sin_addr.s_addr))<<32u)+ntohs(udp_new_addr_in.sin_port);
|
||||||
uint32_t conv;
|
u32_t conv;
|
||||||
|
|
||||||
if(!conn_info.blob->conv_manager.is_u64_used(u64))
|
if(!conn_info.blob->conv_manager.is_u64_used(u64))
|
||||||
{
|
{
|
||||||
@ -2227,8 +2229,8 @@ int server_event_loop()
|
|||||||
|
|
||||||
set_timer(epollfd,timer_fd);
|
set_timer(epollfd,timer_fd);
|
||||||
|
|
||||||
long int begin_time=0;
|
u64_t begin_time=0;
|
||||||
long int end_time=0;
|
u64_t end_time=0;
|
||||||
|
|
||||||
while(1)////////////////////////
|
while(1)////////////////////////
|
||||||
{
|
{
|
||||||
@ -2244,38 +2246,38 @@ int server_event_loop()
|
|||||||
//mylog(log_debug,"ndfs: %d \n",nfds);
|
//mylog(log_debug,"ndfs: %d \n",nfds);
|
||||||
epoll_trigger_counter++;
|
epoll_trigger_counter++;
|
||||||
//printf("%d %d %d %d\n",timer_fd,raw_recv_fd,raw_send_fd,n);
|
//printf("%d %d %d %d\n",timer_fd,raw_recv_fd,raw_send_fd,n);
|
||||||
if ((events[idx].data.u64 ) == (uint64_t)timer_fd)
|
if ((events[idx].data.u64 ) == (u64_t)timer_fd)
|
||||||
{
|
{
|
||||||
if(debug_flag)begin_time=get_current_time();
|
if(debug_flag)begin_time=get_current_time();
|
||||||
conn_manager.clear_inactive();
|
conn_manager.clear_inactive();
|
||||||
uint64_t dummy;
|
u64_t dummy;
|
||||||
read(timer_fd, &dummy, 8);
|
read(timer_fd, &dummy, 8);
|
||||||
//current_time_rough=get_current_time();
|
//current_time_rough=get_current_time();
|
||||||
if(debug_flag)
|
if(debug_flag)
|
||||||
{
|
{
|
||||||
end_time=get_current_time()-begin_time;
|
end_time=get_current_time()-begin_time;
|
||||||
mylog(log_debug,"conn_manager.clear_inactive(),%lld,%lld,%lld \n",begin_time,end_time,end_time-begin_time);
|
mylog(log_debug,"conn_manager.clear_inactive(),%llu,%llu,%llu\n",begin_time,end_time,end_time-begin_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
mylog(log_trace,"epoll_trigger_counter: %d \n",epoll_trigger_counter);
|
mylog(log_trace,"epoll_trigger_counter: %d \n",epoll_trigger_counter);
|
||||||
epoll_trigger_counter=0;
|
epoll_trigger_counter=0;
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (events[idx].data.u64 == (uint64_t)raw_recv_fd)
|
else if (events[idx].data.u64 == (u64_t)raw_recv_fd)
|
||||||
{
|
{
|
||||||
if(debug_flag)begin_time=get_current_time();
|
if(debug_flag)begin_time=get_current_time();
|
||||||
server_on_raw_recv_multi();
|
server_on_raw_recv_multi();
|
||||||
if(debug_flag)
|
if(debug_flag)
|
||||||
{
|
{
|
||||||
end_time=get_current_time()-begin_time;
|
end_time=get_current_time()-begin_time;
|
||||||
mylog(log_debug,"conn_manager.clear_inactive(),%lld,%lld,%lld \n",begin_time,end_time,end_time-begin_time);
|
mylog(log_debug,"conn_manager.clear_inactive(),%llu,%llu,%llu \n",begin_time,end_time,end_time-begin_time);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ((events[idx].data.u64 >>32u) == 2u)
|
else if ((events[idx].data.u64 >>32u) == 2u)
|
||||||
{
|
{
|
||||||
if(debug_flag)begin_time=get_current_time();
|
if(debug_flag)begin_time=get_current_time();
|
||||||
int fd=get_u64_l(events[idx].data.u64);
|
int fd=get_u64_l(events[idx].data.u64);
|
||||||
uint64_t dummy;
|
u64_t dummy;
|
||||||
read(fd, &dummy, 8);
|
read(fd, &dummy, 8);
|
||||||
|
|
||||||
if(conn_manager.timer_fd_mp.find(fd)==conn_manager.timer_fd_mp.end()) //this can happen,when fd is a just closed fd
|
if(conn_manager.timer_fd_mp.find(fd)==conn_manager.timer_fd_mp.end()) //this can happen,when fd is a just closed fd
|
||||||
@ -2284,8 +2286,8 @@ int server_event_loop()
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
conn_info_t* p_conn_info=conn_manager.timer_fd_mp[fd];
|
conn_info_t* p_conn_info=conn_manager.timer_fd_mp[fd];
|
||||||
uint32_t ip=p_conn_info->raw_info.recv_info.src_ip;
|
u32_t ip=p_conn_info->raw_info.recv_info.src_ip;
|
||||||
uint32_t port=p_conn_info->raw_info.recv_info.src_port;
|
u32_t port=p_conn_info->raw_info.recv_info.src_port;
|
||||||
if(!conn_manager.exist(ip,port))//TODO remove this for peformance
|
if(!conn_manager.exist(ip,port))//TODO remove this for peformance
|
||||||
{
|
{
|
||||||
mylog(log_fatal,"ip port no longer exits 1!!!this shouldnt happen\n");
|
mylog(log_fatal,"ip port no longer exits 1!!!this shouldnt happen\n");
|
||||||
@ -2302,7 +2304,7 @@ int server_event_loop()
|
|||||||
if(debug_flag)
|
if(debug_flag)
|
||||||
{
|
{
|
||||||
end_time=get_current_time()-begin_time;
|
end_time=get_current_time()-begin_time;
|
||||||
mylog(log_debug,"conn_manager.clear_inactive(),%lld,%lld,%lld \n",begin_time,end_time,end_time-begin_time);
|
mylog(log_debug,"conn_manager.clear_inactive(),%llu,%llu,%llu \n",begin_time,end_time,end_time-begin_time);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ((events[idx].data.u64 >>32u) == 1u)
|
else if ((events[idx].data.u64 >>32u) == 1u)
|
||||||
@ -2321,17 +2323,17 @@ int server_event_loop()
|
|||||||
}
|
}
|
||||||
conn_info_t* p_conn_info=conn_manager.udp_fd_mp[fd];
|
conn_info_t* p_conn_info=conn_manager.udp_fd_mp[fd];
|
||||||
|
|
||||||
uint32_t ip=p_conn_info->raw_info.recv_info.src_ip;
|
u32_t ip=p_conn_info->raw_info.recv_info.src_ip;
|
||||||
uint32_t port=p_conn_info->raw_info.recv_info.src_port;
|
u32_t port=p_conn_info->raw_info.recv_info.src_port;
|
||||||
if(!conn_manager.exist(ip,port))//TODO remove this for peformance
|
if(!conn_manager.exist(ip,port))//TODO remove this for peformance
|
||||||
{
|
{
|
||||||
mylog(log_fatal,"ip port no longer exits 2!!!this shouldnt happen\n", nfds);
|
mylog(log_fatal,"ip port no longer exits 2!!!this shouldnt happen\n");
|
||||||
myexit(-1);
|
myexit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(p_conn_info->state.server_current_state!=server_ready)//TODO remove this for peformance
|
if(p_conn_info->state.server_current_state!=server_ready)//TODO remove this for peformance
|
||||||
{
|
{
|
||||||
mylog(log_fatal,"p_conn_info->state.server_current_state!=server_ready!!!this shouldnt happen\n", nfds);
|
mylog(log_fatal,"p_conn_info->state.server_current_state!=server_ready!!!this shouldnt happen\n");
|
||||||
myexit(-1);
|
myexit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2344,7 +2346,7 @@ int server_event_loop()
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t conv_id=conn_info.blob->conv_manager.find_conv_by_u64(fd);
|
u32_t conv_id=conn_info.blob->conv_manager.find_conv_by_u64(fd);
|
||||||
|
|
||||||
int recv_len=recv(fd,buf,buf_len,0);
|
int recv_len=recv(fd,buf,buf_len,0);
|
||||||
|
|
||||||
@ -2397,8 +2399,8 @@ void print_help()
|
|||||||
printf(" --cipher-mode <string> avaliable values:md5,crc32,sum,none\n");
|
printf(" --cipher-mode <string> avaliable values:md5,crc32,sum,none\n");
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf("client options:\n");
|
printf("client options:\n");
|
||||||
printf(" --source-ip <ip> override source-ip for raw socket\n");
|
printf(" --source-ip <ip> force source-ip for raw socket\n");
|
||||||
printf(" --source-port <port> override source-port for tcp/udp \n");
|
printf(" --source-port <port> force source-port for raw socket,tcp/udp only\n");
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf("other options:\n");
|
printf("other options:\n");
|
||||||
printf(" --log-level <number> 0:never print log\n");
|
printf(" --log-level <number> 0:never print log\n");
|
||||||
@ -2409,16 +2411,16 @@ void print_help()
|
|||||||
printf(" 5:debug\n");
|
printf(" 5:debug\n");
|
||||||
printf(" 6:trace\n");
|
printf(" 6:trace\n");
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf(" --disable-color disable log color\n");
|
|
||||||
printf(" --log-position enable file name,function name,line number in log\n");
|
printf(" --log-position enable file name,function name,line number in log\n");
|
||||||
|
printf(" --disable-color disable log color\n");
|
||||||
printf(" --disable-bpf disable the kernel space filter,most time its not necessary\n");
|
printf(" --disable-bpf disable the kernel space filter,most time its not necessary\n");
|
||||||
printf(" unless you suspect there is a bug\n");
|
printf(" unless you suspect there is a bug\n");
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf(" --sock-buf <number> buf size for socket,>=1 and <=10240,unit:kbyte\n");
|
printf(" --sock-buf <number> buf size for socket,>=10 and <=10240,unit:kbyte\n");
|
||||||
printf(" --seqmode <number> seq increase mode for faketcp:\n");
|
printf(" --seqmode <number> seq increase mode for faketcp:\n");
|
||||||
printf(" 0:dont increase\n");
|
printf(" 0:dont increase\n");
|
||||||
printf(" 1:increase every packet\n");
|
printf(" 1:increase every packet\n");
|
||||||
printf(" 2:increase randomly, about every 5 packets (default)\n");
|
printf(" 2:increase randomly, about every 3 packets (default)\n");
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf(" -h,--help print this help message\n");
|
printf(" -h,--help print this help message\n");
|
||||||
|
|
||||||
@ -2440,8 +2442,8 @@ void process_arg(int argc, char *argv[])
|
|||||||
{"disable-color", no_argument, 0, 1},
|
{"disable-color", no_argument, 0, 1},
|
||||||
{"log-position", no_argument, 0, 1},
|
{"log-position", no_argument, 0, 1},
|
||||||
{"disable-bpf", no_argument, 0, 1},
|
{"disable-bpf", no_argument, 0, 1},
|
||||||
|
{"disable-anti-replay", no_argument, 0, 1},
|
||||||
{"debug", no_argument, 0, 1},
|
{"debug", no_argument, 0, 1},
|
||||||
{"debug-resend", no_argument, 0, 1},
|
|
||||||
{"sock-buf", required_argument, 0, 1},
|
{"sock-buf", required_argument, 0, 1},
|
||||||
{"seq-mode", required_argument, 0, 1},
|
{"seq-mode", required_argument, 0, 1},
|
||||||
{NULL, 0, 0, 0}
|
{NULL, 0, 0, 0}
|
||||||
@ -2540,7 +2542,7 @@ void process_arg(int argc, char *argv[])
|
|||||||
case 'h':
|
case 'h':
|
||||||
break;
|
break;
|
||||||
case 'a':
|
case 'a':
|
||||||
//auto_add_iptables_rule=1;
|
auto_add_iptables_rule=1;
|
||||||
break;
|
break;
|
||||||
case 'k':
|
case 'k':
|
||||||
mylog(log_debug,"parsing key option\n");
|
mylog(log_debug,"parsing key option\n");
|
||||||
@ -2558,7 +2560,7 @@ void process_arg(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
mylog(log_debug,"parsing long option :source-port\n");
|
mylog(log_debug,"parsing long option :source-port\n");
|
||||||
sscanf(optarg, "%d", &source_port);
|
sscanf(optarg, "%d", &source_port);
|
||||||
mylog(log_info,"source: %d\n",&source_port);
|
mylog(log_info,"source: %d\n",source_port);
|
||||||
}
|
}
|
||||||
else if(strcmp(long_options[option_index].name,"raw-mode")==0)
|
else if(strcmp(long_options[option_index].name,"raw-mode")==0)
|
||||||
{
|
{
|
||||||
@ -2635,11 +2637,15 @@ void process_arg(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
disable_bpf_filter=1;
|
disable_bpf_filter=1;
|
||||||
}
|
}
|
||||||
|
else if(strcmp(long_options[option_index].name,"disable-anti-replay")==0)
|
||||||
|
{
|
||||||
|
disable_anti_replay=1;
|
||||||
|
}
|
||||||
else if(strcmp(long_options[option_index].name,"sock-buf")==0)
|
else if(strcmp(long_options[option_index].name,"sock-buf")==0)
|
||||||
{
|
{
|
||||||
int tmp=-1;
|
int tmp=-1;
|
||||||
sscanf(optarg,"%d",&tmp);
|
sscanf(optarg,"%d",&tmp);
|
||||||
if(1<=tmp&&tmp<=10*1024)
|
if(10<=tmp&&tmp<=10*1024)
|
||||||
{
|
{
|
||||||
socket_buf_size=tmp*1024;
|
socket_buf_size=tmp*1024;
|
||||||
}
|
}
|
||||||
@ -2684,7 +2690,7 @@ void process_arg(int argc, char *argv[])
|
|||||||
myexit(-1);
|
myexit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
mylog(log_info,"important variables: ", argc);
|
mylog(log_info,"important variables: ");
|
||||||
|
|
||||||
log_bare(log_info,"log_level=%d:%s ",log_level,log_text[log_level]);
|
log_bare(log_info,"log_level=%d:%s ",log_level,log_text[log_level]);
|
||||||
log_bare(log_info,"raw_mode=%s ",raw_mode_tostring[raw_mode]);
|
log_bare(log_info,"raw_mode=%s ",raw_mode_tostring[raw_mode]);
|
||||||
@ -2706,22 +2712,22 @@ void process_arg(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
void iptables_warn()
|
void iptables_warn()
|
||||||
{
|
{
|
||||||
char iptables[200];
|
char rule[200];
|
||||||
if(program_mode==client_mode)
|
if(program_mode==client_mode)
|
||||||
{
|
{
|
||||||
if(raw_mode==mode_faketcp)
|
if(raw_mode==mode_faketcp)
|
||||||
{
|
{
|
||||||
sprintf(iptables,"INPUT -s %s/32 -p tcp -m tcp --sport %d -j DROP\n",remote_address,remote_port);
|
sprintf(rule,"INPUT -s %s/32 -p tcp -m tcp --sport %d -j DROP",remote_address,remote_port);
|
||||||
//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);
|
//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);
|
||||||
}
|
}
|
||||||
if(raw_mode==mode_udp)
|
if(raw_mode==mode_udp)
|
||||||
{
|
{
|
||||||
sprintf(iptables,"INPUT -s %s/32 -p udp -m udp --sport %d -j DROP\n",remote_address,remote_port);
|
sprintf(rule,"INPUT -s %s/32 -p udp -m udp --sport %d -j DROP",remote_address,remote_port);
|
||||||
//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);
|
//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);
|
||||||
}
|
}
|
||||||
if(raw_mode==mode_icmp)
|
if(raw_mode==mode_icmp)
|
||||||
{
|
{
|
||||||
sprintf(iptables,"INPUT -s %s/32 -p icmp -j DROP\n",remote_address);
|
sprintf(rule,"INPUT -s %s/32 -p icmp -j DROP",remote_address);
|
||||||
//mylog(log_warn,"make sure you have run once: iptables -A INPUT -s %s/32 -p icmp -j DROP\n",remote_address);
|
//mylog(log_warn,"make sure you have run once: iptables -A INPUT -s %s/32 -p icmp -j DROP\n",remote_address);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2730,35 +2736,36 @@ void iptables_warn()
|
|||||||
|
|
||||||
if(raw_mode==mode_faketcp)
|
if(raw_mode==mode_faketcp)
|
||||||
{
|
{
|
||||||
sprintf(iptables,"INPUT -p tcp -m tcp --dport %d -j DROP\n",local_port);
|
sprintf(rule,"INPUT -p tcp -m tcp --dport %d -j DROP",local_port);
|
||||||
//mylog(log_warn,"make sure you have run once: iptables -A INPUT -p tcp -m tcp --dport %d -j DROP\n",local_port);
|
//mylog(log_warn,"make sure you have run once: iptables -A INPUT -p tcp -m tcp --dport %d -j DROP\n",local_port);
|
||||||
}
|
}
|
||||||
if(raw_mode==mode_udp)
|
if(raw_mode==mode_udp)
|
||||||
{
|
{
|
||||||
sprintf(iptables,"INPUT -p udp -m udp --udp %d -j DROP\n",local_port);
|
sprintf(rule,"INPUT -p udp -m udp --udp %d -j DROP",local_port);
|
||||||
//mylog(log_warn,"make sure you have run once: iptables -A INPUT -p udp -m udp --udp %d -j DROP\n",local_port);
|
//mylog(log_warn,"make sure you have run once: iptables -A INPUT -p udp -m udp --udp %d -j DROP\n",local_port);
|
||||||
}
|
}
|
||||||
if(raw_mode==mode_icmp)
|
if(raw_mode==mode_icmp)
|
||||||
{
|
{
|
||||||
if(local_address_uint32==0)
|
if(local_address_uint32==0)
|
||||||
{
|
{
|
||||||
sprintf(iptables,"INPUT -p icmp -j DROP\n");
|
sprintf(rule,"INPUT -p icmp -j DROP");
|
||||||
//mylog(log_warn,"make sure you have run once: iptables -A INPUT -p icmp -j DROP\n");
|
//mylog(log_warn,"make sure you have run once: iptables -A INPUT -p icmp -j DROP\n");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
sprintf(iptables,"INPUT -d %s/32 -p icmp -j DROP\n",local_address);
|
sprintf(rule,"INPUT -d %s/32 -p icmp -j DROP",local_address);
|
||||||
//mylog(log_warn,"make sure you have run once: iptables -A INPUT -d %s/32 -p icmp -j DROP\n",local_address);
|
//mylog(log_warn,"make sure you have run once: iptables -A INPUT -d %s/32 -p icmp -j DROP\n",local_address);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(auto_add_iptables_rule)
|
if(auto_add_iptables_rule)
|
||||||
{
|
{
|
||||||
//not implemented
|
strcat(rule," -m comment --comment udp2raw_added ");
|
||||||
|
add_iptables_rule(rule);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mylog(log_warn,"make sure you have run once: iptables -A %s\n",iptables);
|
mylog(log_warn,"make sure you have run once: iptables -A %s\n",rule);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
10
makefile
10
makefile
@ -1,9 +1,13 @@
|
|||||||
ccmips=mips-openwrt-linux-g++
|
ccmips=mips-openwrt-linux-g++
|
||||||
FLAGS=-Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -O3
|
FLAGS=-Wall -Wextra -Wno-unused-variable -Wno-unused-parameter
|
||||||
|
FLAGS2= -O3
|
||||||
all:
|
all:
|
||||||
sudo killall udp2raw||true
|
sudo killall udp2raw||true
|
||||||
sleep 0.2
|
sleep 0.2
|
||||||
g++ main.cpp -o udp2raw -static -ggdb -I. aes.c md5.c encrypt.cpp log.cpp network.cpp common.cpp -lrt -std=c++11 ${FLAGS}
|
g++ main.cpp -o udp2raw -static -ggdb -I. aes.c md5.c encrypt.cpp log.cpp network.cpp common.cpp -lrt -std=c++11 ${FLAGS} ${FLAGS2}
|
||||||
${ccmips} main.cpp -o udp2raw_mips -lrt -I. aes.c md5.c encrypt.cpp log.cpp network.cpp common.cpp -std=c++11 ${FLAGS}
|
${ccmips} main.cpp -o udp2raw_mips -lrt -I. aes.c md5.c encrypt.cpp log.cpp network.cpp common.cpp -std=c++11 ${FLAGS} ${FLAGS2}
|
||||||
|
|
||||||
|
|
||||||
|
debug:
|
||||||
|
g++ main.cpp -o udp2raw -static -ggdb -I. aes.c md5.c encrypt.cpp log.cpp network.cpp common.cpp -lrt -std=c++11 ${FLAGS} -Wformat-nonliteral -D MY_DEBUG
|
||||||
|
${ccmips} main.cpp -o udp2raw -ggdb -I. aes.c md5.c encrypt.cpp log.cpp network.cpp common.cpp -lrt -std=c++11 ${FLAGS} -Wformat-nonliteral -D MY_DEBUG
|
||||||
|
32
network.cpp
32
network.cpp
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
int raw_recv_fd=-1;
|
int raw_recv_fd=-1;
|
||||||
int raw_send_fd=-1;
|
int raw_send_fd=-1;
|
||||||
uint32_t link_level_header_len=0;//set it to 14 if SOCK_RAW is used in socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
|
u32_t link_level_header_len=0;//set it to 14 if SOCK_RAW is used in socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
|
||||||
|
|
||||||
int seq_mode=2;
|
int seq_mode=2;
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ int filter_port=-1;
|
|||||||
|
|
||||||
int disable_bpf_filter=0; //for test only,most time no need to disable this
|
int disable_bpf_filter=0; //for test only,most time no need to disable this
|
||||||
|
|
||||||
uint32_t bind_address_uint32=0;
|
u32_t bind_address_uint32=0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -422,7 +422,7 @@ int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
|
|||||||
|
|
||||||
unsigned short iphdrlen =iph->ihl*4;
|
unsigned short iphdrlen =iph->ihl*4;
|
||||||
|
|
||||||
uint32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
|
u32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
|
||||||
|
|
||||||
if(ip_chk!=0)
|
if(ip_chk!=0)
|
||||||
{
|
{
|
||||||
@ -576,14 +576,14 @@ int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) {
|
|||||||
send_raw_tcp_buf[i++] = 0x08; //ts i=6
|
send_raw_tcp_buf[i++] = 0x08; //ts i=6
|
||||||
send_raw_tcp_buf[i++] = 0x0a; //i=7
|
send_raw_tcp_buf[i++] = 0x0a; //i=7
|
||||||
|
|
||||||
*(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(
|
*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(
|
||||||
(uint32_t) get_current_time());
|
(u32_t) get_current_time());
|
||||||
|
|
||||||
i += 4;
|
i += 4;
|
||||||
|
|
||||||
//mylog(log_info,"[syn]<send_info.ts_ack= %u>\n",send_info.ts_ack);
|
//mylog(log_info,"[syn]<send_info.ts_ack= %u>\n",send_info.ts_ack);
|
||||||
|
|
||||||
*(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
|
*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
|
||||||
i += 4;
|
i += 4;
|
||||||
|
|
||||||
send_raw_tcp_buf[i++] = 0x01;
|
send_raw_tcp_buf[i++] = 0x01;
|
||||||
@ -600,14 +600,14 @@ int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) {
|
|||||||
send_raw_tcp_buf[i++] = 0x08; //ts //i=2
|
send_raw_tcp_buf[i++] = 0x08; //ts //i=2
|
||||||
send_raw_tcp_buf[i++] = 0x0a; //i=3;
|
send_raw_tcp_buf[i++] = 0x0a; //i=3;
|
||||||
|
|
||||||
*(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(
|
*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(
|
||||||
(uint32_t) get_current_time());
|
(u32_t) get_current_time());
|
||||||
|
|
||||||
i += 4;
|
i += 4;
|
||||||
|
|
||||||
//mylog(log_info,"<send_info.ts_ack= %u>\n",send_info.ts_ack);
|
//mylog(log_info,"<send_info.ts_ack= %u>\n",send_info.ts_ack);
|
||||||
|
|
||||||
*(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
|
*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
|
||||||
i += 4;
|
i += 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1026,13 +1026,13 @@ int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen)
|
|||||||
if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
|
if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
|
||||||
{
|
{
|
||||||
recv_info.has_ts=1;
|
recv_info.has_ts=1;
|
||||||
recv_info.ts=ntohl(*(uint32_t*)(&tcp_option[8]));
|
recv_info.ts=ntohl(*(u32_t*)(&tcp_option[8]));
|
||||||
recv_info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[12]));
|
recv_info.ts_ack=ntohl(*(u32_t*)(&tcp_option[12]));
|
||||||
//g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
|
//g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mylog(log_info,"???\n",tcph->doff);
|
// mylog(log_info,"\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(tcph->doff==8)
|
else if(tcph->doff==8)
|
||||||
@ -1040,13 +1040,13 @@ int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen)
|
|||||||
if(tcp_option[2]==0x08 &&tcp_option[3]==0x0a)
|
if(tcp_option[2]==0x08 &&tcp_option[3]==0x0a)
|
||||||
{
|
{
|
||||||
recv_info.has_ts=1;
|
recv_info.has_ts=1;
|
||||||
recv_info.ts=ntohl(*(uint32_t*)(&tcp_option[4]));
|
recv_info.ts=ntohl(*(u32_t*)(&tcp_option[4]));
|
||||||
recv_info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[8]));
|
recv_info.ts_ack=ntohl(*(u32_t*)(&tcp_option[8]));
|
||||||
//g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
|
//g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mylog(log_info,"!!!\n",tcph->doff);
|
//mylog(log_info,"!!!\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -1055,7 +1055,7 @@ int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen)
|
|||||||
}
|
}
|
||||||
if(tcph->rst==1)
|
if(tcph->rst==1)
|
||||||
{
|
{
|
||||||
mylog(log_warn,"%%%%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
|
mylog(log_error,"rst==1\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
recv_info.ack=tcph->ack;
|
recv_info.ack=tcph->ack;
|
||||||
|
12
network.h
12
network.h
@ -12,7 +12,7 @@ extern int raw_recv_fd;
|
|||||||
extern int raw_send_fd;
|
extern int raw_send_fd;
|
||||||
extern int seq_mode;
|
extern int seq_mode;
|
||||||
extern int filter_port;
|
extern int filter_port;
|
||||||
extern uint32_t bind_address_uint32;
|
extern u32_t bind_address_uint32;
|
||||||
extern int disable_bpf_filter;
|
extern int disable_bpf_filter;
|
||||||
|
|
||||||
struct icmphdr
|
struct icmphdr
|
||||||
@ -36,19 +36,19 @@ struct packet_info_t //todo change this to union
|
|||||||
{
|
{
|
||||||
uint8_t protocol;
|
uint8_t protocol;
|
||||||
//ip_part:
|
//ip_part:
|
||||||
uint32_t src_ip;
|
u32_t src_ip;
|
||||||
uint16_t src_port;
|
uint16_t src_port;
|
||||||
|
|
||||||
uint32_t dst_ip;
|
u32_t dst_ip;
|
||||||
uint16_t dst_port;
|
uint16_t dst_port;
|
||||||
|
|
||||||
//tcp_part:
|
//tcp_part:
|
||||||
bool syn,ack,psh,rst;
|
bool syn,ack,psh,rst;
|
||||||
|
|
||||||
uint32_t seq,ack_seq;
|
u32_t seq,ack_seq;
|
||||||
|
|
||||||
|
|
||||||
uint32_t ts,ts_ack;
|
u32_t ts,ts_ack;
|
||||||
|
|
||||||
|
|
||||||
uint16_t icmp_seq;
|
uint16_t icmp_seq;
|
||||||
@ -65,7 +65,7 @@ struct raw_info_t
|
|||||||
int last_send_len;
|
int last_send_len;
|
||||||
int last_recv_len;
|
int last_recv_len;
|
||||||
|
|
||||||
uint32_t reserved_seq;
|
u32_t reserved_seq;
|
||||||
//uint32_t first_seq,first_ack_seq;
|
//uint32_t first_seq,first_ack_seq;
|
||||||
|
|
||||||
};//g_raw_info;
|
};//g_raw_info;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user