common.h 6.1 KB

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