connection.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include "misc.h"
  15. struct anti_replay_t //its for anti replay attack,similar to openvpn/ipsec 's anti replay window
  16. {
  17. u64_t max_packet_received;
  18. char window[anti_replay_window_size];
  19. anti_replay_seq_t anti_replay_seq;
  20. anti_replay_seq_t get_new_seq_for_send();
  21. anti_replay_t();
  22. void re_init();
  23. int is_vaild(u64_t seq);
  24. };//anti_replay;
  25. struct conv_manager_t // manage the udp connections
  26. {
  27. //typedef hash_map map;
  28. unordered_map<u64_t,u32_t> u64_to_conv; //conv and u64 are both supposed to be uniq
  29. unordered_map<u32_t,u64_t> conv_to_u64;
  30. unordered_map<u32_t,u64_t> conv_last_active_time;
  31. unordered_map<u32_t,u64_t>::iterator clear_it;
  32. unordered_map<u32_t,u64_t>::iterator it;
  33. unordered_map<u32_t,u64_t>::iterator old_it;
  34. //void (*clear_function)(uint64_t u64) ;
  35. long long last_clear_time;
  36. conv_manager_t();
  37. ~conv_manager_t();
  38. int get_size();
  39. void reserve();
  40. void clear();
  41. u32_t get_new_conv();
  42. int is_conv_used(u32_t conv);
  43. int is_u64_used(u64_t u64);
  44. u32_t find_conv_by_u64(u64_t u64);
  45. u64_t find_u64_by_conv(u32_t conv);
  46. int update_active_time(u32_t conv);
  47. int insert_conv(u32_t conv,u64_t u64);
  48. int erase_conv(u32_t conv);
  49. int clear_inactive(char * ip_port=0);
  50. int clear_inactive0(char * ip_port);
  51. };//g_conv_manager;
  52. 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
  53. {
  54. conv_manager_t conv_manager;
  55. anti_replay_t anti_replay;
  56. };
  57. 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
  58. //handle multiple clients
  59. {
  60. current_state_t state;
  61. raw_info_t raw_info;
  62. u64_t last_state_time;
  63. u64_t last_hb_sent_time; //client re-use this for retry
  64. u64_t last_hb_recv_time;
  65. //long long last_resent_time;
  66. id_t my_id;
  67. id_t oppsite_id;
  68. fd64_t timer_fd64;
  69. id_t oppsite_const_id;
  70. blob_t *blob;
  71. uint8_t my_roller;
  72. uint8_t oppsite_roller;
  73. u64_t last_oppsite_roller_time;
  74. // ip_port_t ip_port;
  75. /*
  76. const uint32_t &ip=raw_info.recv_info.src_ip;
  77. const uint16_t &port=raw_info.recv_info.src_port;
  78. */
  79. void recover(const conn_info_t &conn_info);
  80. void re_init();
  81. conn_info_t();
  82. void prepare();
  83. conn_info_t(const conn_info_t&b);
  84. conn_info_t& operator=(const conn_info_t& b);
  85. ~conn_info_t();
  86. };//g_conn_info;
  87. 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
  88. {
  89. u32_t ready_num;
  90. //unordered_map<int,conn_info_t *> udp_fd_mp; //a bit dirty to used pointer,but can void unordered_map search
  91. //unordered_map<int,conn_info_t *> timer_fd_mp;//we can use pointer here since unordered_map.rehash() uses shallow copy
  92. unordered_map<id_t,conn_info_t *> const_id_mp;
  93. unordered_map<u64_t,conn_info_t*> mp; //put it at end so that it de-consturcts first
  94. unordered_map<u64_t,conn_info_t*>::iterator clear_it;
  95. long long last_clear_time;
  96. conn_manager_t();
  97. int exist(u32_t ip,uint16_t port);
  98. /*
  99. int insert(uint32_t ip,uint16_t port)
  100. {
  101. uint64_t u64=0;
  102. u64=ip;
  103. u64<<=32u;
  104. u64|=port;
  105. mp[u64];
  106. return 0;
  107. }*/
  108. conn_info_t *& find_insert_p(u32_t ip,uint16_t port); //be aware,the adress may change after rehash
  109. conn_info_t & find_insert(u32_t ip,uint16_t port) ; //be aware,the adress may change after rehash
  110. int erase(unordered_map<u64_t,conn_info_t*>::iterator erase_it);
  111. int clear_inactive();
  112. int clear_inactive0();
  113. };
  114. extern conn_manager_t conn_manager;
  115. void server_clear_function(u64_t u64);
  116. int send_bare(raw_info_t &raw_info,const char* data,int len);//send function with encryption but no anti replay,this is used when client and server verifys each other
  117. //you have to design the protocol carefully, so that you wont be affect by relay attack
  118. //int reserved_parse_bare(const char *input,int input_len,char* & data,int & len); // a sub function used in recv_bare
  119. int recv_bare(raw_info_t &raw_info,char* & data,int & len);//recv function with encryption but no anti replay,this is used when client and server verifys each other
  120. //you have to design the protocol carefully, so that you wont be affect by relay attack
  121. int send_handshake(raw_info_t &raw_info,id_t id1,id_t id2,id_t id3);// a warp for send_bare for sending handshake(this is not tcp handshake) easily
  122. int send_safer(conn_info_t &conn_info,char type,const char* data,int len); //safer transfer function with anti-replay,when mutually verification is done.
  123. int send_data_safer(conn_info_t &conn_info,const char* data,int len,u32_t conv_num);//a wrap for send_safer for transfer data.
  124. //int reserved_parse_safer(conn_info_t &conn_info,const char * input,int input_len,char &type,char* &data,int &len);//subfunction for recv_safer,allow overlap
  125. int recv_safer(conn_info_t &conn_info,char &type,char* &data,int &len);///safer transfer function with anti-replay,when mutually verification is done.
  126. #endif /* CONNECTION_H_ */