common.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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<unordered_map>
  45. #include<vector>
  46. #include<string>
  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. const int max_data_len=1600;
  53. const int buf_len=max_data_len+400;
  54. const u32_t max_handshake_conn_num=10000;
  55. const u32_t max_ready_conn_num=1000;
  56. const u32_t anti_replay_window_size=1000;
  57. const int max_conv_num=10000;
  58. const u32_t client_handshake_timeout=5000;
  59. const u32_t client_retry_interval=1000;
  60. const u32_t server_handshake_timeout=10000;// this should be much longer than clients. client retry initially ,server retry passtively
  61. const int conv_clear_ratio=10; //conv grabage collecter check 1/10 of all conv one time
  62. const int conn_clear_ratio=30;
  63. const int conv_clear_min=5;
  64. const int conn_clear_min=1;
  65. const u32_t conv_clear_interval=1000;
  66. const u32_t conn_clear_interval=1000;
  67. const i32_t max_fail_time=0;//disable
  68. const u32_t heartbeat_interval=1000;
  69. const u32_t timer_interval=400;//this should be smaller than heartbeat_interval and retry interval;
  70. //const uint32_t conv_timeout=120000; //120 second
  71. const u32_t conv_timeout=30000; //for test
  72. const u32_t client_conn_timeout=10000;
  73. const u32_t client_conn_uplink_timeout=client_conn_timeout+2000;
  74. //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
  75. const u32_t server_conn_timeout=conv_timeout+10000;//for test
  76. extern int about_to_exit;
  77. enum raw_mode_t{mode_faketcp=0,mode_udp,mode_icmp,mode_end};
  78. extern raw_mode_t raw_mode;
  79. enum program_mode_t {unset_mode=0,client_mode,server_mode};
  80. extern program_mode_t program_mode;
  81. extern unordered_map<int, const char*> raw_mode_tostring ;
  82. extern int socket_buf_size;
  83. typedef u32_t id_t;
  84. typedef u64_t iv_t;
  85. typedef u64_t padding_t;
  86. typedef u64_t anti_replay_seq_t;
  87. u64_t get_current_time();
  88. u64_t pack_u64(u32_t a,u32_t b);
  89. u32_t get_u64_h(u64_t a);
  90. u32_t get_u64_l(u64_t a);
  91. char * my_ntoa(u32_t ip);
  92. void myexit(int a);
  93. void init_random_number_fd();
  94. u64_t get_true_random_number_64();
  95. u32_t get_true_random_number();
  96. u32_t get_true_random_number_nz();
  97. u64_t ntoh64(u64_t a);
  98. u64_t hton64(u64_t a);
  99. bool larger_than_u16(uint16_t a,uint16_t b);
  100. bool larger_than_u32(u32_t a,u32_t b);
  101. void setnonblocking(int sock);
  102. int set_buf_size(int fd);
  103. unsigned short csum(const unsigned short *ptr,int nbytes);
  104. void signal_handler(int sig);
  105. int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len);
  106. int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3);
  107. void myexit(int a);
  108. int add_iptables_rule(char *);
  109. int clear_iptables_rule();
  110. int run_command(const char * command,char * &output);
  111. int read_file(const char * file,char * &output);
  112. vector<string> string_to_vec(const char * s,const char * sp);
  113. vector< vector <string> > string_to_vec2(const char * s);
  114. #endif /* COMMON_H_ */