misc.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * misc.h
  3. *
  4. * Created on: Sep 23, 2017
  5. * Author: root
  6. */
  7. #ifndef MISC_H_
  8. #define MISC_H_
  9. #include "common.h"
  10. #include "log.h"
  11. #include "network.h"
  12. extern int hb_mode;
  13. extern int hb_len;
  14. extern int mtu_warn;
  15. extern int max_rst_allowed;
  16. extern int max_rst_to_show;
  17. const u32_t max_handshake_conn_num=10000;
  18. const u32_t max_ready_conn_num=1000;
  19. const u32_t anti_replay_window_size=4000;
  20. const int max_conv_num=10000;
  21. const u32_t client_handshake_timeout=5000;//unit ms
  22. const u32_t client_retry_interval=1000;//ms
  23. const u32_t server_handshake_timeout=client_handshake_timeout+5000;// this should be longer than clients. client retry initially ,server retry passtively
  24. const int conv_clear_ratio=30; //conv grabage collecter check 1/30 of all conv one time
  25. const int conn_clear_ratio=50;
  26. const int conv_clear_min=1;
  27. const int conn_clear_min=1;
  28. const u32_t conv_clear_interval=1000;//ms
  29. const u32_t conn_clear_interval=1000;//ms
  30. const i32_t max_fail_time=0;//disable
  31. const u32_t heartbeat_interval=600;//ms
  32. const u32_t timer_interval=400;//ms. this should be smaller than heartbeat_interval and retry interval;
  33. const uint32_t conv_timeout=180000; //ms. 120 second
  34. //const u32_t conv_timeout=30000; //for test
  35. const u32_t client_conn_timeout=10000;//ms.
  36. const u32_t client_conn_uplink_timeout=client_conn_timeout+2000;//ms
  37. const uint32_t server_conn_timeout=conv_timeout+60000;//ms. this should be 60s+ longer than conv_timeout,so that conv_manager can destruct convs gradually,to avoid latency glicth
  38. //const u32_t server_conn_timeout=conv_timeout+10000;//for test
  39. const u32_t iptables_rule_keep_interval=20;//unit: second;
  40. enum server_current_state_t {server_idle=0,server_handshake1,server_ready}; //server state machine
  41. enum client_current_state_t {client_idle=0,client_tcp_handshake,client_handshake1,client_handshake2,client_ready};//client state machine
  42. enum raw_mode_t{mode_faketcp=0,mode_udp,mode_icmp,mode_end};
  43. enum program_mode_t {unset_mode=0,client_mode,server_mode};
  44. union current_state_t
  45. {
  46. server_current_state_t server_current_state;
  47. client_current_state_t client_current_state;
  48. };
  49. extern char local_ip[100], remote_host[100],source_ip[100];//local_ip is for -l option,remote_host for -r option,source for --source-ip
  50. extern u32_t local_ip_uint32,remote_ip_uint32,source_ip_uint32;//convert from last line.
  51. extern int local_port , remote_port,source_port;//similiar to local_ip remote_host,buf for port.source_port=0 indicates --source-port is not enabled
  52. extern int force_source_ip; //if --source-ip is enabled
  53. extern id_t const_id;//an id used for connection recovery,its generated randomly,it never change since its generated
  54. extern int udp_fd; //for client only. client use this fd to listen and handle udp connection
  55. extern int bind_fd; //bind only,never send or recv. its just a dummy fd for bind,so that other program wont occupy the same port
  56. extern int epollfd; //fd for epoll
  57. extern int timer_fd; //the general timer fd for client and server.for server this is not the only timer find,every connection has a timer fd.
  58. extern int fail_time_counter;//determine if the max_fail_time is reached
  59. extern int epoll_trigger_counter;//for debug only
  60. extern int debug_flag;//for debug only
  61. extern int simple_rule; //deprecated.
  62. extern int keep_rule; //whether to monitor the iptables rule periodly,re-add if losted
  63. extern int auto_add_iptables_rule;//if -a is set
  64. extern int generate_iptables_rule;//if -g is set
  65. extern int generate_iptables_rule_add;// if --gen-add is set
  66. extern int retry_on_error;
  67. const int retry_on_error_interval=10;
  68. extern int debug_resend; // debug only
  69. extern char key_string[1000];// -k option
  70. extern char fifo_file[1000];
  71. extern raw_mode_t raw_mode;
  72. extern program_mode_t program_mode;
  73. extern unordered_map<int, const char*> raw_mode_tostring ;
  74. extern int about_to_exit;
  75. extern int socket_buf_size;
  76. extern int force_socket_buf;
  77. extern pthread_t keep_thread;
  78. extern int keep_thread_running;
  79. int process_lower_level_arg();
  80. void print_help();
  81. void iptables_rule();
  82. void pre_process_arg(int argc, char *argv[]);//mainly for load conf file;
  83. int unit_test();
  84. int set_timer(int epollfd,int &timer_fd);
  85. int set_timer_server(int epollfd,int &timer_fd,fd64_t &fd64);
  86. int handle_lower_level(raw_info_t &raw_info);
  87. int add_iptables_rule(const char *);
  88. int clear_iptables_rule();
  89. int iptables_gen_add(const char * s,u32_t const_id);
  90. int iptables_rule_init(const char * s,u32_t const_id,int keep);
  91. int keep_iptables_rule();
  92. void signal_handler(int sig);
  93. #endif /* MISC_H_ */