common.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * common.h
  3. *
  4. * Created on: Jul 29, 2017
  5. * Author: wangyu
  6. */
  7. #ifndef UDP2RAW_COMMON_H_
  8. #define UDP2RAW_COMMON_H_
  9. #define __STDC_FORMAT_MACROS 1
  10. #include <inttypes.h>
  11. #include<stdio.h>
  12. #include<string.h>
  13. #include<stdlib.h>
  14. #include<getopt.h>
  15. #include<unistd.h>
  16. #include<errno.h>
  17. #include <sys/epoll.h>
  18. #include <sys/wait.h>
  19. #include <sys/socket.h> //for socket ofcourse
  20. #include <sys/types.h>
  21. #include <stdlib.h> //for exit(0);
  22. #include <errno.h> //For errno - the error number
  23. #include <netinet/tcp.h> //Provides declarations for tcp header
  24. #include <netinet/udp.h>
  25. #include <netinet/ip.h> //Provides declarations for ip header
  26. #include <netinet/if_ether.h>
  27. #include <arpa/inet.h>
  28. #include <fcntl.h>
  29. #include <byteswap.h>
  30. #include <arpa/inet.h>
  31. #include <linux/if_ether.h>
  32. #include <linux/filter.h>
  33. #include <sys/time.h>
  34. #include <time.h>
  35. #include <sys/timerfd.h>
  36. #include <sys/ioctl.h>
  37. #include <netinet/in.h>
  38. #include <net/if.h>
  39. #include <arpa/inet.h>
  40. #include <stdarg.h>
  41. #include <assert.h>
  42. #include <linux/if_packet.h>
  43. #include <byteswap.h>
  44. #include <pthread.h>
  45. #include<unordered_map>
  46. #include<vector>
  47. #include<string>
  48. using namespace std;
  49. typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64
  50. typedef long long i64_t;
  51. typedef unsigned int u32_t;
  52. typedef int i32_t;
  53. const int max_data_len=1600;
  54. const int buf_len=max_data_len+400;
  55. const u32_t max_handshake_conn_num=10000;
  56. const u32_t max_ready_conn_num=1000;
  57. const u32_t anti_replay_window_size=4000;
  58. const int max_conv_num=10000;
  59. const u32_t client_handshake_timeout=5000;//unit ms
  60. const u32_t client_retry_interval=1000;//ms
  61. const u32_t server_handshake_timeout=client_handshake_timeout+5000;// this should be longer than clients. client retry initially ,server retry passtively
  62. const int conv_clear_ratio=10; //conv grabage collecter check 1/10 of all conv one time
  63. const int conn_clear_ratio=30;
  64. const int conv_clear_min=1;
  65. const int conn_clear_min=1;
  66. const u32_t conv_clear_interval=3000;//ms
  67. const u32_t conn_clear_interval=3000;//ms
  68. const i32_t max_fail_time=0;//disable
  69. const u32_t heartbeat_interval=1000;//ms
  70. const u32_t timer_interval=400;//ms. this should be smaller than heartbeat_interval and retry interval;
  71. const uint32_t conv_timeout=120000; //ms. 120 second
  72. //const u32_t conv_timeout=30000; //for test
  73. const u32_t client_conn_timeout=15000;//ms.
  74. const u32_t client_conn_uplink_timeout=client_conn_timeout+2000;//ms
  75. const uint32_t server_conn_timeout=conv_timeout+60000;//ms. this should be 60s+ longer than conv_timeout,so that conv_manager can destruct convs gradually,to avoid latency glicth
  76. //const u32_t server_conn_timeout=conv_timeout+10000;//for test
  77. const u32_t iptables_rule_keep_interval=15;//unit: second;
  78. extern int about_to_exit;
  79. extern pthread_t keep_thread;
  80. extern int keep_thread_running;
  81. enum raw_mode_t{mode_faketcp=0,mode_udp,mode_icmp,mode_end};
  82. extern raw_mode_t raw_mode;
  83. enum program_mode_t {unset_mode=0,client_mode,server_mode};
  84. extern program_mode_t program_mode;
  85. extern unordered_map<int, const char*> raw_mode_tostring ;
  86. extern int socket_buf_size;
  87. extern int force_socket_buf;
  88. enum server_current_state_t {server_idle=0,server_handshake1,server_ready}; //server state machine
  89. enum client_current_state_t {client_idle=0,client_tcp_handshake,client_handshake1,client_handshake2,client_ready};//client state machine
  90. union current_state_t
  91. {
  92. server_current_state_t server_current_state;
  93. client_current_state_t client_current_state;
  94. };
  95. typedef u32_t id_t;
  96. typedef u64_t iv_t;
  97. typedef u64_t padding_t;
  98. typedef u64_t anti_replay_seq_t;
  99. u64_t get_current_time();
  100. u64_t pack_u64(u32_t a,u32_t b);
  101. u32_t get_u64_h(u64_t a);
  102. u32_t get_u64_l(u64_t a);
  103. char * my_ntoa(u32_t ip);
  104. void myexit(int a);
  105. void init_random_number_fd();
  106. u64_t get_true_random_number_64();
  107. u32_t get_true_random_number();
  108. u32_t get_true_random_number_nz();
  109. u64_t ntoh64(u64_t a);
  110. u64_t hton64(u64_t a);
  111. bool larger_than_u16(uint16_t a,uint16_t b);
  112. bool larger_than_u32(u32_t a,u32_t b);
  113. void setnonblocking(int sock);
  114. int set_buf_size(int fd);
  115. unsigned short csum(const unsigned short *ptr,int nbytes);
  116. void signal_handler(int sig);
  117. int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len);
  118. int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3);
  119. void myexit(int a);
  120. int add_iptables_rule(const char *);
  121. int clear_iptables_rule();
  122. int iptables_gen_add(const char * s,u32_t const_id);
  123. int iptables_rule_init(const char * s,u32_t const_id,int keep);
  124. int keep_iptables_rule();
  125. const int show_none=0;
  126. const int show_command=0x1;
  127. const int show_log=0x2;
  128. const int show_all=show_command|show_log;
  129. int run_command(string command,char * &output,int flag=show_all);
  130. //int run_command_no_log(string command,char * &output);
  131. int read_file(const char * file,string &output);
  132. vector<string> string_to_vec(const char * s,const char * sp);
  133. vector< vector <string> > string_to_vec2(const char * s);
  134. string trim(const string& str, char c);
  135. string trim_conf_line(const string& str);
  136. vector<string> parse_conf_line(const string& s);
  137. int hex_to_u32_with_endian(const string & a,u32_t &output);
  138. int hex_to_u32(const string & a,u32_t &output);
  139. //extern string iptables_pattern;
  140. #endif /* COMMON_H_ */