common.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * common.h
  3. *
  4. * Created on: Jul 29, 2017
  5. * Author: wangyu
  6. */
  7. #ifndef COMMON_H_
  8. #define 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/wait.h>
  18. #include <sys/socket.h> //for socket ofcourse
  19. #include <sys/types.h>
  20. #include <sys/stat.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 <arpa/inet.h>
  30. #include <sys/time.h>
  31. #include <time.h>
  32. #include <sys/ioctl.h>
  33. #include <netinet/in.h>
  34. #include <net/if.h>
  35. #include <arpa/inet.h>
  36. #include <stdarg.h>
  37. #include <assert.h>
  38. #include <my_ev.h>
  39. #include<unordered_map>
  40. #include<unordered_set>
  41. #include<map>
  42. #include<list>
  43. using namespace std;
  44. typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64
  45. typedef long long i64_t;
  46. typedef unsigned int u32_t;
  47. typedef int i32_t;
  48. typedef unsigned short u16_t;
  49. typedef short i16_t;
  50. struct my_itimerspec {
  51. struct timespec it_interval; /* Timer interval */
  52. struct timespec it_value; /* Initial expiration */
  53. };
  54. typedef u64_t my_time_t;
  55. const int max_data_len=3600;
  56. const int buf_len=max_data_len+200;
  57. //const u32_t timer_interval=400;
  58. ////const u32_t conv_timeout=180000;
  59. //const u32_t conv_timeout=40000;//for test
  60. const u32_t conv_timeout=180000;
  61. const int max_conv_num=10000;
  62. const int max_conn_num=200;
  63. /*
  64. const u32_t max_handshake_conn_num=10000;
  65. const u32_t max_ready_conn_num=1000;
  66. //const u32_t anti_replay_window_size=1000;
  67. const u32_t client_handshake_timeout=5000;
  68. const u32_t client_retry_interval=1000;
  69. const u32_t server_handshake_timeout=10000;// this should be much longer than clients. client retry initially ,server retry passtively*/
  70. const int conv_clear_ratio=30; //conv grabage collecter check 1/30 of all conv one time
  71. const int conn_clear_ratio=50;
  72. const int conv_clear_min=1;
  73. const int conn_clear_min=1;
  74. const u32_t conv_clear_interval=1000;
  75. const u32_t conn_clear_interval=1000;
  76. const i32_t max_fail_time=0;//disable
  77. const u32_t heartbeat_interval=1000;
  78. const u32_t timer_interval=400;//this should be smaller than heartbeat_interval and retry interval;
  79. //const uint32_t conv_timeout=120000; //120 second
  80. //const u32_t conv_timeout=120000; //for test
  81. const u32_t client_conn_timeout=10000;
  82. const u32_t client_conn_uplink_timeout=client_conn_timeout+2000;
  83. //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
  84. const u32_t server_conn_timeout=conv_timeout+20000;//for test
  85. extern int about_to_exit;
  86. enum raw_mode_t{mode_faketcp=0,mode_udp,mode_icmp,mode_end};
  87. extern raw_mode_t raw_mode;
  88. enum program_mode_t {unset_mode=0,client_mode,server_mode};
  89. extern program_mode_t client_or_server;
  90. extern unordered_map<int, const char*> raw_mode_tostring ;
  91. enum working_mode_t {unset_working_mode=0,tunnel_mode,tun_dev_mode};
  92. extern working_mode_t working_mode;
  93. extern int socket_buf_size;
  94. typedef u32_t id_t;
  95. typedef u64_t iv_t;
  96. typedef u64_t padding_t;
  97. typedef u64_t anti_replay_seq_t;
  98. typedef u64_t fd64_t;
  99. //enum dest_type{none=0,type_fd64_ip_port,type_fd64,type_fd64_ip_port_conv,type_fd64_conv/*,type_fd*/};
  100. enum dest_type{none=0,type_fd64_ip_port,type_fd64,type_fd,type_write_fd,type_fd_ip_port/*,type_fd*/};
  101. struct ip_port_t
  102. {
  103. u32_t ip;
  104. int port;
  105. void from_u64(u64_t u64);
  106. u64_t to_u64();
  107. char * to_s();
  108. };
  109. struct fd64_ip_port_t
  110. {
  111. fd64_t fd64;
  112. ip_port_t ip_port;
  113. };
  114. struct fd_ip_port_t
  115. {
  116. int fd;
  117. ip_port_t ip_port;
  118. };
  119. union inner_t
  120. {
  121. fd64_t fd64;
  122. int fd;
  123. fd64_ip_port_t fd64_ip_port;
  124. fd_ip_port_t fd_ip_port;
  125. };
  126. struct dest_t
  127. {
  128. dest_type type;
  129. inner_t inner;
  130. u32_t conv;
  131. int cook=0;
  132. };
  133. struct fd_info_t
  134. {
  135. ip_port_t ip_port;
  136. ev_io io_watcher;
  137. };
  138. struct pseudo_header {
  139. u_int32_t source_address;
  140. u_int32_t dest_address;
  141. u_int8_t placeholder;
  142. u_int8_t protocol;
  143. u_int16_t tcp_length;
  144. };
  145. u64_t get_current_time();
  146. u64_t get_current_time_us();
  147. u64_t pack_u64(u32_t a,u32_t b);
  148. u32_t get_u64_h(u64_t a);
  149. u32_t get_u64_l(u64_t a);
  150. void write_u16(char *,u16_t a);
  151. u16_t read_u16(char *);
  152. void write_u32(char *,u32_t a);
  153. u32_t read_u32(char *);
  154. void write_u64(char *,u64_t a);
  155. u64_t read_uu64(char *);
  156. char * my_ntoa(u32_t ip);
  157. void myexit(int a);
  158. void init_random_number_fd();
  159. u64_t get_fake_random_number_64();
  160. u32_t get_fake_random_number();
  161. u32_t get_fake_random_number_nz();
  162. u64_t ntoh64(u64_t a);
  163. u64_t hton64(u64_t a);
  164. bool larger_than_u16(uint16_t a,uint16_t b);
  165. bool larger_than_u32(u32_t a,u32_t b);
  166. void setnonblocking(int sock);
  167. int set_buf_size(int fd,int socket_buf_size,int force_socket_buf=0);
  168. unsigned short csum(const unsigned short *ptr,int nbytes);
  169. unsigned short tcp_csum(const pseudo_header & ph,const unsigned short *ptr,int nbytes);
  170. void signal_handler(int sig);
  171. int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len);
  172. int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3);
  173. void myexit(int a);
  174. int add_iptables_rule(char *);
  175. int clear_iptables_rule();
  176. void get_fake_random_chars(char * s,int len);
  177. int random_between(u32_t a,u32_t b);
  178. int set_timer_ms(int epollfd,int &timer_fd,u32_t timer_interval);
  179. int round_up_div(int a,int b);
  180. int create_fifo(char * file);
  181. /*
  182. int create_new_udp(int &new_udp_fd,int remote_address_uint32,int remote_port);
  183. */
  184. int new_listen_socket(int &fd,u32_t ip,int port);
  185. int new_connected_socket(int &fd,u32_t ip,int port);
  186. #endif /* COMMON_H_ */