misc.h 4.3 KB

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