common.h 5.7 KB

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