structanti_replay_t//its for anti replay attack,similar to openvpn/ipsec 's anti replay window
{
u64_tmax_packet_received;
charwindow[anti_replay_window_size];
anti_replay_seq_tanti_replay_seq;
anti_replay_seq_tget_new_seq_for_send();
anti_replay_t();
voidre_init();
intis_vaild(u64_tseq);
};//anti_replay;
structconv_manager_t// manage the udp connections
{
//typedef hash_map map;
unordered_map<u64_t,u32_t>u64_to_conv;//conv and u64 are both supposed to be uniq
unordered_map<u32_t,u64_t>conv_to_u64;
unordered_map<u32_t,u64_t>conv_last_active_time;
unordered_map<u32_t,u64_t>::iteratorclear_it;
unordered_map<u32_t,u64_t>::iteratorit;
unordered_map<u32_t,u64_t>::iteratorold_it;
//void (*clear_function)(uint64_t u64) ;
longlonglast_clear_time;
conv_manager_t();
~conv_manager_t();
intget_size();
voidreserve();
voidclear();
u32_tget_new_conv();
intis_conv_used(u32_tconv);
intis_u64_used(u64_tu64);
u32_tfind_conv_by_u64(u64_tu64);
u64_tfind_u64_by_conv(u32_tconv);
intupdate_active_time(u32_tconv);
intinsert_conv(u32_tconv,u64_tu64);
interase_conv(u32_tconv);
intclear_inactive(char*ip_port=0);
intclear_inactive0(char*ip_port);
};//g_conv_manager;
structblob_t//used in conn_info_t. conv_manager_t and anti_replay_t are costly data structures ,we dont allocate them until its necessary
{
conv_manager_tconv_manager;
anti_replay_tanti_replay;
};
structconn_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
//handle multiple clients
{
current_state_tstate;
raw_info_traw_info;
u64_tlast_state_time;
u64_tlast_hb_sent_time;//client re-use this for retry
structconn_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
intsend_bare(raw_info_t&raw_info,constchar*data,intlen);//send function with encryption but no anti replay,this is used when client and server verifys each other
//you have to design the protocol carefully, so that you wont be affect by relay attack
intrecv_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
//you have to design the protocol carefully, so that you wont be affect by relay attack
intsend_handshake(raw_info_t&raw_info,id_tid1,id_tid2,id_tid3);// a warp for send_bare for sending handshake(this is not tcp handshake) easily
intsend_safer(conn_info_t&conn_info,chartype,constchar*data,intlen);//safer transfer function with anti-replay,when mutually verification is done.
intsend_data_safer(conn_info_t&conn_info,constchar*data,intlen,u32_tconv_num);//a wrap for send_safer for transfer data.