2017-07-29 20:32:26 +08:00
/*
* common . h
*
* Created on : Jul 29 , 2017
* Author : wangyu
*/
# ifndef COMMON_H_
# define COMMON_H_
2017-08-04 17:12:23 +08:00
# define __STDC_FORMAT_MACROS 1
2017-08-04 03:29:53 +08:00
# include <inttypes.h>
2017-08-04 17:12:23 +08:00
2017-07-29 20:32:26 +08:00
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <getopt.h>
2017-08-04 17:12:23 +08:00
2017-07-30 16:57:24 +08:00
# include <unistd.h>
2017-07-29 20:32:26 +08:00
# include <errno.h>
# include <sys/epoll.h>
# include <sys/wait.h>
# include <sys/socket.h> //for socket ofcourse
# include <sys/types.h>
# include <stdlib.h> //for exit(0);
# include <errno.h> //For errno - the error number
# include <netinet/tcp.h> //Provides declarations for tcp header
# include <netinet/udp.h>
# include <netinet/ip.h> //Provides declarations for ip header
# include <netinet/if_ether.h>
# include <arpa/inet.h>
# include <fcntl.h>
# include <byteswap.h>
# include <arpa/inet.h>
# include <linux/if_ether.h>
# include <linux/filter.h>
# include <sys/time.h>
# include <time.h>
# include <sys/timerfd.h>
# include <sys/ioctl.h>
# include <netinet/in.h>
# include <net/if.h>
# include <arpa/inet.h>
# include <stdarg.h>
2017-07-30 16:37:49 +08:00
# include <assert.h>
2017-08-07 13:58:42 +08:00
# include <linux/if_packet.h>
2017-08-16 19:44:19 +08:00
# include <byteswap.h>
2017-08-20 16:28:23 +08:00
# include <pthread.h>
2017-07-30 16:57:24 +08:00
# include <unordered_map>
2017-08-17 23:40:17 +08:00
# include <vector>
# include <string>
2017-07-29 20:32:26 +08:00
using namespace std ;
2017-08-04 17:12:23 +08:00
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 ;
2017-07-31 13:26:07 +08:00
const int max_data_len = 1600 ;
2017-08-12 19:21:24 +08:00
const int buf_len = max_data_len + 400 ;
2017-08-04 17:12:23 +08:00
const u32_t max_handshake_conn_num = 10000 ;
const u32_t max_ready_conn_num = 1000 ;
const u32_t anti_replay_window_size = 1000 ;
2017-07-30 05:53:30 +08:00
const int max_conv_num = 10000 ;
2017-07-29 20:32:26 +08:00
2017-08-04 17:12:23 +08:00
const u32_t client_handshake_timeout = 5000 ;
const u32_t client_retry_interval = 1000 ;
2017-07-30 18:09:51 +08:00
2017-08-04 17:12:23 +08:00
const u32_t server_handshake_timeout = 10000 ; // this should be much longer than clients. client retry initially ,server retry passtively
2017-07-30 05:53:30 +08:00
const int conv_clear_ratio = 10 ; //conv grabage collecter check 1/10 of all conv one time
2017-08-11 16:17:26 +08:00
const int conn_clear_ratio = 30 ;
2017-07-30 13:39:18 +08:00
const int conv_clear_min = 5 ;
const int conn_clear_min = 1 ;
2017-07-29 20:32:26 +08:00
2017-08-04 17:12:23 +08:00
const u32_t conv_clear_interval = 1000 ;
const u32_t conn_clear_interval = 1000 ;
2017-08-02 18:57:27 +08:00
2017-08-05 10:03:40 +08:00
const i32_t max_fail_time = 0 ; //disable
2017-07-29 20:32:26 +08:00
2017-08-04 17:12:23 +08:00
const u32_t heartbeat_interval = 1000 ;
2017-07-29 20:32:26 +08:00
2017-08-04 17:12:23 +08:00
const u32_t timer_interval = 400 ; //this should be smaller than heartbeat_interval and retry interval;
2017-07-29 20:32:26 +08:00
2017-07-30 16:37:49 +08:00
//const uint32_t conv_timeout=120000; //120 second
2017-08-04 17:12:23 +08:00
const u32_t conv_timeout = 30000 ; //for test
2017-07-29 20:32:26 +08:00
2017-08-04 17:12:23 +08:00
const u32_t client_conn_timeout = 10000 ;
2017-08-08 15:49:11 +08:00
const u32_t client_conn_uplink_timeout = client_conn_timeout + 2000 ;
2017-07-29 20:32:26 +08:00
2017-07-30 16:37:49 +08:00
//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
2017-08-04 17:12:23 +08:00
const u32_t server_conn_timeout = conv_timeout + 10000 ; //for test
2017-07-29 20:32:26 +08:00
2017-08-20 16:28:23 +08:00
//const u32_t iptables_rule_keep_interval=4000;
2017-08-04 23:08:45 +08:00
extern int about_to_exit ;
2017-08-20 16:28:23 +08:00
extern pthread_t keep_thread ;
2017-08-21 20:26:55 +08:00
extern int keep_thread_running ;
2017-07-29 20:32:26 +08:00
2017-07-30 17:07:17 +08:00
enum raw_mode_t { mode_faketcp = 0 , mode_udp , mode_icmp , mode_end } ;
2017-07-29 20:32:26 +08:00
extern raw_mode_t raw_mode ;
enum program_mode_t { unset_mode = 0 , client_mode , server_mode } ;
extern program_mode_t program_mode ;
2017-07-30 16:57:24 +08:00
extern unordered_map < int , const char * > raw_mode_tostring ;
2017-07-29 20:32:26 +08:00
extern int socket_buf_size ;
2017-08-04 17:12:23 +08:00
typedef u32_t id_t ;
2017-07-29 20:32:26 +08:00
2017-08-04 17:12:23 +08:00
typedef u64_t iv_t ;
2017-07-29 20:32:26 +08:00
2017-08-04 17:12:23 +08:00
typedef u64_t padding_t ;
2017-08-01 20:03:29 +08:00
2017-08-04 17:12:23 +08:00
typedef u64_t anti_replay_seq_t ;
2017-07-29 20:32:26 +08:00
2017-08-04 17:12:23 +08:00
u64_t get_current_time ( ) ;
u64_t pack_u64 ( u32_t a , u32_t b ) ;
2017-07-29 20:32:26 +08:00
2017-08-04 17:12:23 +08:00
u32_t get_u64_h ( u64_t a ) ;
2017-07-29 20:32:26 +08:00
2017-08-04 17:12:23 +08:00
u32_t get_u64_l ( u64_t a ) ;
2017-07-29 20:32:26 +08:00
2017-08-04 17:12:23 +08:00
char * my_ntoa ( u32_t ip ) ;
2017-07-29 20:32:26 +08:00
void myexit ( int a ) ;
void init_random_number_fd ( ) ;
2017-08-04 17:12:23 +08:00
u64_t get_true_random_number_64 ( ) ;
u32_t get_true_random_number ( ) ;
u32_t get_true_random_number_nz ( ) ;
u64_t ntoh64 ( u64_t a ) ;
u64_t hton64 ( u64_t a ) ;
2017-08-04 12:46:46 +08:00
bool larger_than_u16 ( uint16_t a , uint16_t b ) ;
2017-08-04 17:12:23 +08:00
bool larger_than_u32 ( u32_t a , u32_t b ) ;
2017-07-29 20:32:26 +08:00
void setnonblocking ( int sock ) ;
int set_buf_size ( int fd ) ;
unsigned short csum ( const unsigned short * ptr , int nbytes ) ;
2017-08-04 18:35:51 +08:00
void signal_handler ( int sig ) ;
2017-07-29 20:32:26 +08:00
int numbers_to_char ( id_t id1 , id_t id2 , id_t id3 , char * & data , int & len ) ;
int char_to_numbers ( const char * data , int len , id_t & id1 , id_t & id2 , id_t & id3 ) ;
2017-08-04 11:51:39 +08:00
void myexit ( int a ) ;
2017-07-29 20:32:26 +08:00
2017-08-20 16:28:23 +08:00
int add_iptables_rule ( const char * ) ;
2017-08-04 17:12:23 +08:00
int clear_iptables_rule ( ) ;
2017-08-20 16:28:23 +08:00
int iptables_gen_add ( const char * s , u32_t const_id ) ;
int iptables_rule_init ( const char * s , u32_t const_id , int keep ) ;
int keep_iptables_rule ( ) ;
const int show_none = 0 ;
const int show_command = 0x1 ;
const int show_log = 0x2 ;
const int show_all = show_command | show_log ;
int run_command ( string command , char * & output , int flag = show_all ) ;
//int run_command_no_log(string command,char * &output);
2017-08-26 05:38:33 -05:00
int read_file ( const char * file , string & output ) ;
2017-08-17 23:40:17 +08:00
vector < string > string_to_vec ( const char * s , const char * sp ) ;
vector < vector < string > > string_to_vec2 ( const char * s ) ;
2017-08-23 10:38:55 +08:00
string trim ( const string & str , char c ) ;
2017-08-23 07:01:21 -05:00
string trim_conf_line ( const string & str ) ;
vector < string > parse_conf_line ( const string & s ) ;
2017-08-26 05:38:33 -05:00
int hex_to_u32_with_endian ( const string & a , u32_t & output ) ;
int hex_to_u32 ( const string & a , u32_t & output ) ;
2017-08-20 16:28:23 +08:00
//extern string iptables_pattern;
2017-07-29 20:32:26 +08:00
# endif /* COMMON_H_ */