common.h 3.6 KB

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