common.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <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<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. typedef u64_t my_time_t;
  55. const int max_data_len=2000;
  56. const int buf_len=max_data_len+200;
  57. const u32_t conv_clear_interval=200;
  58. //const u32_t timer_interval=400;
  59. ////const u32_t conv_timeout=180000;
  60. const u32_t conv_timeout=40000;//for test
  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=10; //conv grabage collecter check 1/10 of all conv one time
  71. const int conn_clear_ratio=10;
  72. const int conv_clear_min=5;
  73. const int conn_clear_min=1;
  74. const u32_t conn_clear_interval=1000;
  75. const i32_t max_fail_time=0;//disable
  76. const u32_t heartbeat_interval=1000;
  77. const u32_t timer_interval=500;//this should be smaller than heartbeat_interval and retry interval;
  78. //const uint32_t conv_timeout=120000; //120 second
  79. //const u32_t conv_timeout=120000; //for test
  80. const u32_t client_conn_timeout=10000;
  81. const u32_t client_conn_uplink_timeout=client_conn_timeout+2000;
  82. //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
  83. const u32_t server_conn_timeout=conv_timeout+20000;//for test
  84. extern int about_to_exit;
  85. enum raw_mode_t{mode_faketcp=0,mode_udp,mode_icmp,mode_end};
  86. extern raw_mode_t raw_mode;
  87. enum program_mode_t {unset_mode=0,client_mode,server_mode};
  88. extern program_mode_t program_mode;
  89. extern unordered_map<int, const char*> raw_mode_tostring ;
  90. extern int max_pending_packet;
  91. typedef u32_t id_t;
  92. typedef u64_t iv_t;
  93. typedef u64_t padding_t;
  94. typedef u64_t anti_replay_seq_t;
  95. typedef u64_t fd64_t;
  96. enum dest_type{none=0,type_ip_port,type_fd64,type_ip_port_conv,type_fd64_conv/*,type_fd*/};
  97. struct ip_port_t
  98. {
  99. u32_t ip;
  100. int port;
  101. void from_u64(u64_t u64);
  102. u64_t to_u64();
  103. char * to_s();
  104. };
  105. union inner_t
  106. {
  107. ip_port_t ip_port;
  108. //int fd;
  109. fd64_t fd64;
  110. };
  111. struct dest_t
  112. {
  113. dest_type type;
  114. inner_t inner;
  115. u32_t conv;
  116. int cook=0;
  117. };
  118. struct fd_info_t
  119. {
  120. ip_port_t ip_port;
  121. };
  122. u64_t get_current_time();
  123. u64_t get_current_time_us();
  124. u64_t pack_u64(u32_t a,u32_t b);
  125. u32_t get_u64_h(u64_t a);
  126. u32_t get_u64_l(u64_t a);
  127. void write_u16(char *,u16_t a);
  128. u16_t read_u16(char *);
  129. void write_u32(char *,u32_t a);
  130. u32_t read_u32(char *);
  131. void write_u64(char *,u64_t a);
  132. u64_t read_uu64(char *);
  133. char * my_ntoa(u32_t ip);
  134. void myexit(int a);
  135. void init_random_number_fd();
  136. u64_t get_true_random_number_64();
  137. u32_t get_true_random_number();
  138. u32_t get_true_random_number_nz();
  139. u64_t ntoh64(u64_t a);
  140. u64_t hton64(u64_t a);
  141. bool larger_than_u16(uint16_t a,uint16_t b);
  142. bool larger_than_u32(u32_t a,u32_t b);
  143. void setnonblocking(int sock);
  144. int set_buf_size(int fd,int socket_buf_size,int force_socket_buf=0);
  145. unsigned short csum(const unsigned short *ptr,int nbytes);
  146. void signal_handler(int sig);
  147. int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len);
  148. int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3);
  149. void myexit(int a);
  150. int add_iptables_rule(char *);
  151. int clear_iptables_rule();
  152. void get_true_random_chars(char * s,int len);
  153. int random_between(u32_t a,u32_t b);
  154. int set_timer_ms(int epollfd,int &timer_fd,u32_t timer_interval);
  155. int round_up_div(int a,int b);
  156. /*
  157. int create_new_udp(int &new_udp_fd,int remote_address_uint32,int remote_port);
  158. */
  159. #endif /* COMMON_H_ */