connection.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * connection.h
  3. *
  4. * Created on: Sep 23, 2017
  5. * Author: root
  6. */
  7. #ifndef CONNECTION_H_
  8. #define CONNECTION_H_
  9. extern int disable_anti_replay;
  10. #include "connection.h"
  11. #include "common.h"
  12. #include "log.h"
  13. #include "network.h"
  14. struct anti_replay_t //its for anti replay attack,similar to openvpn/ipsec 's anti replay window
  15. {
  16. u64_t max_packet_received;
  17. char window[anti_replay_window_size];
  18. anti_replay_seq_t anti_replay_seq;
  19. anti_replay_seq_t get_new_seq_for_send();
  20. anti_replay_t();
  21. void re_init();
  22. int is_vaild(u64_t seq);
  23. };//anti_replay;
  24. struct conv_manager_t // manage the udp connections
  25. {
  26. //typedef hash_map map;
  27. unordered_map<u64_t,u32_t> u64_to_conv; //conv and u64 are both supposed to be uniq
  28. unordered_map<u32_t,u64_t> conv_to_u64;
  29. unordered_map<u32_t,u64_t> conv_last_active_time;
  30. unordered_map<u32_t,u64_t>::iterator clear_it;
  31. unordered_map<u32_t,u64_t>::iterator it;
  32. unordered_map<u32_t,u64_t>::iterator old_it;
  33. //void (*clear_function)(uint64_t u64) ;
  34. long long last_clear_time;
  35. conv_manager_t();
  36. ~conv_manager_t();
  37. int get_size();
  38. void reserve();
  39. void clear();
  40. u32_t get_new_conv();
  41. int is_conv_used(u32_t conv);
  42. int is_u64_used(u64_t u64);
  43. u32_t find_conv_by_u64(u64_t u64);
  44. u64_t find_u64_by_conv(u32_t conv);
  45. int update_active_time(u32_t conv);
  46. int insert_conv(u32_t conv,u64_t u64);
  47. int erase_conv(u32_t conv);
  48. int clear_inactive(char * ip_port=0);
  49. int clear_inactive0(char * ip_port);
  50. };//g_conv_manager;
  51. struct blob_t //used in conn_info_t. conv_manager_t and anti_replay_t are costly data structures ,we dont allocate them until its necessary
  52. {
  53. conv_manager_t conv_manager;
  54. anti_replay_t anti_replay;
  55. };
  56. struct conn_info_t //stores info for a raw connection.for client ,there is only one connection,for server there can be thousand of connection since server can
  57. //handle multiple clients
  58. {
  59. current_state_t state;
  60. raw_info_t raw_info;
  61. u64_t last_state_time;
  62. u64_t last_hb_sent_time; //client re-use this for retry
  63. u64_t last_hb_recv_time;
  64. //long long last_resent_time;
  65. id_t my_id;
  66. id_t oppsite_id;
  67. int timer_fd;
  68. id_t oppsite_const_id;
  69. blob_t *blob;
  70. uint8_t my_roller;
  71. uint8_t oppsite_roller;
  72. u64_t last_oppsite_roller_time;
  73. /*
  74. const uint32_t &ip=raw_info.recv_info.src_ip;
  75. const uint16_t &port=raw_info.recv_info.src_port;
  76. */
  77. void recover(const conn_info_t &conn_info);
  78. void re_init();
  79. conn_info_t();
  80. void prepare();
  81. conn_info_t(const conn_info_t&b);
  82. conn_info_t& operator=(const conn_info_t& b);
  83. ~conn_info_t();
  84. };//g_conn_info;
  85. struct conn_manager_t //manager for connections. for client,we dont need conn_manager since there is only one connection.for server we use one conn_manager for all connections
  86. {
  87. u32_t ready_num;
  88. unordered_map<int,conn_info_t *> udp_fd_mp; //a bit dirty to used pointer,but can void unordered_map search
  89. unordered_map<int,conn_info_t *> timer_fd_mp;//we can use pointer here since unordered_map.rehash() uses shallow copy
  90. unordered_map<id_t,conn_info_t *> const_id_mp;
  91. unordered_map<u64_t,conn_info_t*> mp; //put it at end so that it de-consturcts first
  92. unordered_map<u64_t,conn_info_t*>::iterator clear_it;
  93. long long last_clear_time;
  94. conn_manager_t();
  95. int exist(u32_t ip,uint16_t port);
  96. /*
  97. int insert(uint32_t ip,uint16_t port)
  98. {
  99. uint64_t u64=0;
  100. u64=ip;
  101. u64<<=32u;
  102. u64|=port;
  103. mp[u64];
  104. return 0;
  105. }*/
  106. conn_info_t *& find_insert_p(u32_t ip,uint16_t port); //be aware,the adress may change after rehash
  107. conn_info_t & find_insert(u32_t ip,uint16_t port) ; //be aware,the adress may change after rehash
  108. int erase(unordered_map<u64_t,conn_info_t*>::iterator erase_it);
  109. int clear_inactive();
  110. int clear_inactive0();
  111. };
  112. #endif /* CONNECTION_H_ */