common.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <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=1000;
  58. const int max_conv_num=10000;
  59. const u32_t client_handshake_timeout=5000;
  60. const u32_t client_retry_interval=1000;
  61. const u32_t server_handshake_timeout=10000;// this should be much 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=5;
  65. const int conn_clear_min=1;
  66. const u32_t conv_clear_interval=1000;
  67. const u32_t conn_clear_interval=1000;
  68. const i32_t max_fail_time=0;//disable
  69. const u32_t heartbeat_interval=1000;
  70. const u32_t timer_interval=400;//this should be smaller than heartbeat_interval and retry interval;
  71. //const uint32_t conv_timeout=120000; //120 second
  72. const u32_t conv_timeout=30000; //for test
  73. const u32_t client_conn_timeout=10000;
  74. const u32_t client_conn_uplink_timeout=client_conn_timeout+2000;
  75. //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
  76. const u32_t server_conn_timeout=conv_timeout+10000;//for test
  77. //const u32_t iptables_rule_keep_interval=4000;
  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. typedef u32_t id_t;
  88. typedef u64_t iv_t;
  89. typedef u64_t padding_t;
  90. typedef u64_t anti_replay_seq_t;
  91. u64_t get_current_time();
  92. u64_t pack_u64(u32_t a,u32_t b);
  93. u32_t get_u64_h(u64_t a);
  94. u32_t get_u64_l(u64_t a);
  95. char * my_ntoa(u32_t ip);
  96. void myexit(int a);
  97. void init_random_number_fd();
  98. u64_t get_true_random_number_64();
  99. u32_t get_true_random_number();
  100. u32_t get_true_random_number_nz();
  101. u64_t ntoh64(u64_t a);
  102. u64_t hton64(u64_t a);
  103. bool larger_than_u16(uint16_t a,uint16_t b);
  104. bool larger_than_u32(u32_t a,u32_t b);
  105. void setnonblocking(int sock);
  106. int set_buf_size(int fd);
  107. unsigned short csum(const unsigned short *ptr,int nbytes);
  108. void signal_handler(int sig);
  109. int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len);
  110. int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3);
  111. void myexit(int a);
  112. int add_iptables_rule(const char *);
  113. int clear_iptables_rule();
  114. int iptables_gen_add(const char * s,u32_t const_id);
  115. int iptables_rule_init(const char * s,u32_t const_id,int keep);
  116. int keep_iptables_rule();
  117. const int show_none=0;
  118. const int show_command=0x1;
  119. const int show_log=0x2;
  120. const int show_all=show_command|show_log;
  121. int run_command(string command,char * &output,int flag=show_all);
  122. //int run_command_no_log(string command,char * &output);
  123. int read_file(const char * file,string &output);
  124. vector<string> string_to_vec(const char * s,const char * sp);
  125. vector< vector <string> > string_to_vec2(const char * s);
  126. string trim(const string& str, char c);
  127. string trim_conf_line(const string& str);
  128. vector<string> parse_conf_line(const string& s);
  129. int hex_to_u32_with_endian(const string & a,u32_t &output);
  130. int hex_to_u32(const string & a,u32_t &output);
  131. //extern string iptables_pattern;
  132. #endif /* COMMON_H_ */