connection.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*
  2. * connection.cpp
  3. *
  4. * Created on: Sep 23, 2017
  5. * Author: root
  6. */
  7. #include "connection.h"
  8. #include "encrypt.h"
  9. #include "fd_manager.h"
  10. int disable_anti_replay=0;//if anti_replay windows is diabled
  11. const int disable_conn_clear=0;//a raw connection is called conn.
  12. conn_manager_t conn_manager;
  13. anti_replay_seq_t anti_replay_t::get_new_seq_for_send()
  14. {
  15. return anti_replay_seq++;
  16. }
  17. anti_replay_t::anti_replay_t()
  18. {
  19. max_packet_received=0;
  20. anti_replay_seq=get_true_random_number_64()/10;//random first seq
  21. //memset(window,0,sizeof(window)); //not necessary
  22. }
  23. void anti_replay_t::re_init()
  24. {
  25. max_packet_received=0;
  26. //memset(window,0,sizeof(window));
  27. }
  28. int anti_replay_t::is_vaild(u64_t seq)
  29. {
  30. if(disable_anti_replay) return 1;
  31. //if(disabled) return 0;
  32. if(seq==max_packet_received) return 0;
  33. else if(seq>max_packet_received)
  34. {
  35. if(seq-max_packet_received>=anti_replay_window_size)
  36. {
  37. memset(window,0,sizeof(window));
  38. window[seq%anti_replay_window_size]=1;
  39. }
  40. else
  41. {
  42. for (u64_t i=max_packet_received+1;i<seq;i++)
  43. window[i%anti_replay_window_size]=0;
  44. window[seq%anti_replay_window_size]=1;
  45. }
  46. max_packet_received=seq;
  47. return 1;
  48. }
  49. else if(seq<max_packet_received)
  50. {
  51. if(max_packet_received-seq>=anti_replay_window_size) return 0;
  52. else
  53. {
  54. if (window[seq%anti_replay_window_size]==1) return 0;
  55. else
  56. {
  57. window[seq%anti_replay_window_size]=1;
  58. return 1;
  59. }
  60. }
  61. }
  62. return 0; //for complier check
  63. }
  64. void conn_info_t::recover(const conn_info_t &conn_info)
  65. {
  66. raw_info=conn_info.raw_info;
  67. raw_info.rst_received=0;
  68. raw_info.disabled=0;
  69. last_state_time=conn_info.last_state_time;
  70. last_hb_recv_time=conn_info.last_hb_recv_time;
  71. last_hb_sent_time=conn_info.last_hb_sent_time;
  72. my_id=conn_info.my_id;
  73. oppsite_id=conn_info.oppsite_id;
  74. blob->anti_replay.re_init();
  75. my_roller=0;//no need to set,but for easier debug,set it to zero
  76. oppsite_roller=0;//same as above
  77. last_oppsite_roller_time=0;
  78. }
  79. void conn_info_t::re_init()
  80. {
  81. //send_packet_info.protocol=g_packet_info_send.protocol;
  82. if(program_mode==server_mode)
  83. state.server_current_state=server_idle;
  84. else
  85. state.client_current_state=client_idle;
  86. last_state_time=0;
  87. oppsite_const_id=0;
  88. timer_fd64=0;
  89. my_roller=0;
  90. oppsite_roller=0;
  91. last_oppsite_roller_time=0;
  92. }
  93. conn_info_t::conn_info_t()
  94. {
  95. blob=0;
  96. re_init();
  97. }
  98. void conn_info_t::prepare()
  99. {
  100. assert(blob==0);
  101. blob=new blob_t;
  102. blob->conv_manager.s.additional_clear_function=server_clear_function;
  103. }
  104. conn_info_t::conn_info_t(const conn_info_t&b)
  105. {
  106. assert(0==1);
  107. //mylog(log_error,"called!!!!!!!!!!!!!\n");
  108. *this=b;
  109. if(blob!=0)
  110. {
  111. blob=new blob_t(*b.blob);
  112. }
  113. }
  114. conn_info_t& conn_info_t::operator=(const conn_info_t& b)
  115. {
  116. mylog(log_fatal,"not allowed\n");
  117. myexit(-1);
  118. return *this;
  119. }
  120. conn_info_t::~conn_info_t()
  121. {
  122. if(program_mode==server_mode)
  123. {
  124. if(state.server_current_state==server_ready)
  125. {
  126. assert(blob!=0);
  127. assert(oppsite_const_id!=0);
  128. //assert(conn_manager.const_id_mp.find(oppsite_const_id)!=conn_manager.const_id_mp.end()); // conn_manager 's deconstuction function erases it
  129. }
  130. else
  131. {
  132. assert(blob==0);
  133. assert(oppsite_const_id==0);
  134. }
  135. }
  136. assert(timer_fd64==0);
  137. //if(oppsite_const_id!=0) //do this at conn_manager 's deconstuction function
  138. //conn_manager.const_id_mp.erase(oppsite_const_id);
  139. if(blob!=0)
  140. delete blob;
  141. //send_packet_info.protocol=g_packet_info_send.protocol;
  142. }
  143. conn_manager_t::conn_manager_t()
  144. {
  145. ready_num=0;
  146. mp.reserve(10007);
  147. //clear_it=mp.begin();
  148. // timer_fd_mp.reserve(10007);
  149. const_id_mp.reserve(10007);
  150. // udp_fd_mp.reserve(100007);
  151. last_clear_time=0;
  152. //current_ready_ip=0;
  153. // current_ready_port=0;
  154. }
  155. int conn_manager_t::exist(address_t addr)
  156. {
  157. //u64_t u64=0;
  158. //u64=ip;
  159. //u64<<=32u;
  160. //u64|=port;
  161. if(mp.find(addr)!=mp.end())
  162. {
  163. return 1;
  164. }
  165. return 0;
  166. }
  167. /*
  168. int insert(uint32_t ip,uint16_t port)
  169. {
  170. uint64_t u64=0;
  171. u64=ip;
  172. u64<<=32u;
  173. u64|=port;
  174. mp[u64];
  175. return 0;
  176. }*/
  177. conn_info_t *& conn_manager_t::find_insert_p(address_t addr) //be aware,the adress may change after rehash
  178. {
  179. // u64_t u64=0;
  180. //u64=ip;
  181. //u64<<=32u;
  182. //u64|=port;
  183. unordered_map<address_t,conn_info_t*>::iterator it=mp.find(addr);
  184. if(it==mp.end())
  185. {
  186. mp[addr]=new conn_info_t;
  187. }
  188. return mp[addr];
  189. }
  190. conn_info_t & conn_manager_t::find_insert(address_t addr) //be aware,the adress may change after rehash
  191. {
  192. //u64_t u64=0;
  193. //u64=ip;
  194. //u64<<=32u;
  195. //u64|=port;
  196. unordered_map<address_t,conn_info_t*>::iterator it=mp.find(addr);
  197. if(it==mp.end())
  198. {
  199. mp[addr]=new conn_info_t;
  200. }
  201. return *mp[addr];
  202. }
  203. int conn_manager_t::erase(unordered_map<address_t,conn_info_t*>::iterator erase_it)
  204. {
  205. if(erase_it->second->state.server_current_state==server_ready)
  206. {
  207. ready_num--;
  208. assert(i32_t(ready_num)!=-1);
  209. assert(erase_it->second!=0);
  210. assert(erase_it->second->timer_fd64 !=0);
  211. assert(fd_manager.exist(erase_it->second->timer_fd64));
  212. assert(erase_it->second->oppsite_const_id!=0);
  213. assert(const_id_mp.find(erase_it->second->oppsite_const_id)!=const_id_mp.end());
  214. //assert(timer_fd_mp.find(erase_it->second->timer_fd)!=timer_fd_mp.end());
  215. const_id_mp.erase(erase_it->second->oppsite_const_id);
  216. fd_manager.fd64_close(erase_it->second->timer_fd64);
  217. erase_it->second->timer_fd64=0;
  218. //timer_fd_mp.erase(erase_it->second->timer_fd);
  219. //close(erase_it->second->timer_fd);// close will auto delte it from epoll
  220. delete(erase_it->second);
  221. mp.erase(erase_it->first);
  222. }
  223. else
  224. {
  225. assert(erase_it->second->blob==0);
  226. assert(erase_it->second->timer_fd64 ==0);
  227. assert(erase_it->second->oppsite_const_id==0);
  228. delete(erase_it->second);
  229. mp.erase(erase_it->first);
  230. }
  231. return 0;
  232. }
  233. int conn_manager_t::clear_inactive()
  234. {
  235. if(get_current_time()-last_clear_time>conn_clear_interval)
  236. {
  237. last_clear_time=get_current_time();
  238. return clear_inactive0();
  239. }
  240. return 0;
  241. }
  242. int conn_manager_t::clear_inactive0()
  243. {
  244. //unordered_map<u64_t,conn_info_t*>::iterator it;
  245. // unordered_map<u64_t,conn_info_t*>::iterator old_it;
  246. if(disable_conn_clear) return 0;
  247. //map<uint32_t,uint64_t>::iterator it;
  248. int cnt=0;
  249. //it=clear_it;
  250. int size=mp.size();
  251. int num_to_clean=size/conn_clear_ratio+conn_clear_min; //clear 1/10 each time,to avoid latency glitch
  252. mylog(log_trace,"mp.size() %d\n", size);
  253. num_to_clean=min(num_to_clean,(int)mp.size());
  254. u64_t current_time=get_current_time();
  255. for(;;)
  256. {
  257. if(cnt>=num_to_clean) break;
  258. if(lru.empty()) break;
  259. address_t key;
  260. my_time_t ts=lru.peek_back(key);
  261. //if(mp.begin()==mp.end()) break;
  262. //if(it==mp.end())
  263. //{
  264. // it=mp.begin();
  265. //}
  266. auto it=mp.find(key);
  267. if(it->second->state.server_current_state==server_ready &&current_time - it->second->last_hb_recv_time <=server_conn_timeout)
  268. {
  269. //it++;
  270. }
  271. else if(it->second->state.server_current_state!=server_ready&& current_time - it->second->last_state_time <=server_handshake_timeout )
  272. {
  273. //it++;
  274. }
  275. else if(it->second->blob!=0&&it->second->blob->conv_manager.s.get_size() >0)
  276. {
  277. assert(it->second->state.server_current_state==server_ready);
  278. //it++;
  279. }
  280. else
  281. {
  282. mylog(log_info,"[%s:%d]inactive conn cleared \n",my_ntoa(it->second->raw_info.recv_info.src_ip),it->second->raw_info.recv_info.src_port);
  283. //old_it=it;
  284. //it++;
  285. erase(it);
  286. }
  287. cnt++;
  288. }
  289. //clear_it=it;
  290. return 0;
  291. }
  292. 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
  293. //you have to design the protocol carefully, so that you wont be affect by relay attack
  294. {
  295. if(len<0)
  296. {
  297. mylog(log_debug,"input_len <0\n");
  298. return -1;
  299. }
  300. packet_info_t &send_info=raw_info.send_info;
  301. packet_info_t &recv_info=raw_info.recv_info;
  302. char send_data_buf[buf_len]; //buf for send data and send hb
  303. char send_data_buf2[buf_len];
  304. //static send_bare[buf_len];
  305. iv_t iv=get_true_random_number_64();
  306. padding_t padding=get_true_random_number_64();
  307. memcpy(send_data_buf,&iv,sizeof(iv));
  308. memcpy(send_data_buf+sizeof(iv),&padding,sizeof(padding));
  309. send_data_buf[sizeof(iv)+sizeof(padding)]='b';
  310. memcpy(send_data_buf+sizeof(iv)+sizeof(padding)+1,data,len);
  311. int new_len=len+sizeof(iv)+sizeof(padding)+1;
  312. if(my_encrypt(send_data_buf,send_data_buf2,new_len)!=0)
  313. {
  314. return -1;
  315. }
  316. send_raw0(raw_info,send_data_buf2,new_len);
  317. return 0;
  318. }
  319. int reserved_parse_bare(const char *input,int input_len,char* & data,int & len) // a sub function used in recv_bare
  320. {
  321. static char recv_data_buf[buf_len];
  322. if(input_len<0)
  323. {
  324. mylog(log_debug,"input_len <0\n");
  325. return -1;
  326. }
  327. if(my_decrypt(input,recv_data_buf,input_len)!=0)
  328. {
  329. mylog(log_debug,"decrypt_fail in recv bare\n");
  330. return -1;
  331. }
  332. if(recv_data_buf[sizeof(iv_t)+sizeof(padding_t)]!='b')
  333. {
  334. mylog(log_debug,"not a bare packet\n");
  335. return -1;
  336. }
  337. len=input_len;
  338. data=recv_data_buf+sizeof(iv_t)+sizeof(padding_t)+1;
  339. len-=sizeof(iv_t)+sizeof(padding_t)+1;
  340. if(len<0)
  341. {
  342. mylog(log_debug,"len <0\n");
  343. return -1;
  344. }
  345. return 0;
  346. }
  347. 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
  348. //you have to design the protocol carefully, so that you wont be affect by relay attack
  349. {
  350. packet_info_t &send_info=raw_info.send_info;
  351. packet_info_t &recv_info=raw_info.recv_info;
  352. if(recv_raw0(raw_info,data,len)<0)
  353. {
  354. //printf("recv_raw_fail in recv bare\n");
  355. return -1;
  356. }
  357. if ((raw_mode == mode_faketcp && (recv_info.syn == 1 || recv_info.ack != 1)))
  358. {
  359. mylog(log_debug,"unexpect packet type recv_info.syn=%d recv_info.ack=%d \n",recv_info.syn,recv_info.ack);
  360. return -1;
  361. }
  362. return reserved_parse_bare(data,len,data,len);
  363. }
  364. 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
  365. {
  366. packet_info_t &send_info=raw_info.send_info;
  367. packet_info_t &recv_info=raw_info.recv_info;
  368. char * data;int len;
  369. //len=sizeof(id_t)*3;
  370. if(numbers_to_char(id1,id2,id3,data,len)!=0) return -1;
  371. if(send_bare(raw_info,data,len)!=0) {mylog(log_warn,"send bare fail\n");return -1;}
  372. return 0;
  373. }
  374. /*
  375. int recv_handshake(packet_info_t &info,id_t &id1,id_t &id2,id_t &id3)
  376. {
  377. char * data;int len;
  378. if(recv_bare(info,data,len)!=0) return -1;
  379. if(char_to_numbers(data,len,id1,id2,id3)!=0) return -1;
  380. return 0;
  381. }*/
  382. 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.
  383. {
  384. packet_info_t &send_info=conn_info.raw_info.send_info;
  385. packet_info_t &recv_info=conn_info.raw_info.recv_info;
  386. if(type!='h'&&type!='d')
  387. {
  388. mylog(log_warn,"first byte is not h or d ,%x\n",type);
  389. return -1;
  390. }
  391. char send_data_buf[buf_len]; //buf for send data and send hb
  392. char send_data_buf2[buf_len];
  393. id_t n_tmp_id=htonl(conn_info.my_id);
  394. memcpy(send_data_buf,&n_tmp_id,sizeof(n_tmp_id));
  395. n_tmp_id=htonl(conn_info.oppsite_id);
  396. memcpy(send_data_buf+sizeof(n_tmp_id),&n_tmp_id,sizeof(n_tmp_id));
  397. anti_replay_seq_t n_seq=hton64(conn_info.blob->anti_replay.get_new_seq_for_send());
  398. memcpy(send_data_buf+sizeof(n_tmp_id)*2,&n_seq,sizeof(n_seq));
  399. send_data_buf[sizeof(n_tmp_id)*2+sizeof(n_seq)]=type;
  400. send_data_buf[sizeof(n_tmp_id)*2+sizeof(n_seq)+1]=conn_info.my_roller;
  401. memcpy(send_data_buf+2+sizeof(n_tmp_id)*2+sizeof(n_seq),data,len);//data;
  402. int new_len=len+sizeof(n_seq)+sizeof(n_tmp_id)*2+2;
  403. if(my_encrypt(send_data_buf,send_data_buf2,new_len)!=0)
  404. {
  405. return -1;
  406. }
  407. if(send_raw0(conn_info.raw_info,send_data_buf2,new_len)!=0) return -1;
  408. if(after_send_raw0(conn_info.raw_info)!=0) return -1;
  409. return 0;
  410. }
  411. 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.
  412. {
  413. packet_info_t &send_info=conn_info.raw_info.send_info;
  414. packet_info_t &recv_info=conn_info.raw_info.recv_info;
  415. char send_data_buf[buf_len];
  416. //send_data_buf[0]='d';
  417. u32_t n_conv_num=htonl(conv_num);
  418. memcpy(send_data_buf,&n_conv_num,sizeof(n_conv_num));
  419. memcpy(send_data_buf+sizeof(n_conv_num),data,len);
  420. int new_len=len+sizeof(n_conv_num);
  421. send_safer(conn_info,'d',send_data_buf,new_len);
  422. return 0;
  423. }
  424. 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
  425. {
  426. static char recv_data_buf[buf_len];
  427. // char *recv_data_buf=recv_data_buf0; //fix strict alias warning
  428. if(my_decrypt(input,recv_data_buf,input_len)!=0)
  429. {
  430. //printf("decrypt fail\n");
  431. return -1;
  432. }
  433. //char *a=recv_data_buf;
  434. //id_t h_oppiste_id= ntohl ( *((id_t * )(recv_data_buf)) );
  435. id_t h_oppsite_id;
  436. memcpy(&h_oppsite_id,recv_data_buf,sizeof(h_oppsite_id));
  437. h_oppsite_id=ntohl(h_oppsite_id);
  438. //id_t h_my_id= ntohl ( *((id_t * )(recv_data_buf+sizeof(id_t))) );
  439. id_t h_my_id;
  440. memcpy(&h_my_id,recv_data_buf+sizeof(id_t),sizeof(h_my_id));
  441. h_my_id=ntohl(h_my_id);
  442. //anti_replay_seq_t h_seq= ntoh64 ( *((anti_replay_seq_t * )(recv_data_buf +sizeof(id_t) *2 )) );
  443. anti_replay_seq_t h_seq;
  444. memcpy(&h_seq,recv_data_buf +sizeof(id_t) *2 ,sizeof(h_seq));
  445. h_seq=ntoh64(h_seq);
  446. if(h_oppsite_id!=conn_info.oppsite_id||h_my_id!=conn_info.my_id)
  447. {
  448. mylog(log_debug,"id and oppsite_id verification failed %x %x %x %x \n",h_oppsite_id,conn_info.oppsite_id,h_my_id,conn_info.my_id);
  449. return -1;
  450. }
  451. if (conn_info.blob->anti_replay.is_vaild(h_seq) != 1) {
  452. mylog(log_debug,"dropped replay packet\n");
  453. return -1;
  454. }
  455. //printf("recv _len %d\n ",recv_len);
  456. data=recv_data_buf+sizeof(anti_replay_seq_t)+sizeof(id_t)*2;
  457. len=input_len-(sizeof(anti_replay_seq_t)+sizeof(id_t)*2 );
  458. if(data[0]!='h'&&data[0]!='d')
  459. {
  460. mylog(log_debug,"first byte is not h or d ,%x\n",data[0]);
  461. return -1;
  462. }
  463. uint8_t roller=data[1];
  464. type=data[0];
  465. data+=2;
  466. len-=2;
  467. if(len<0)
  468. {
  469. mylog(log_debug,"len <0 ,%d\n",len);
  470. return -1;
  471. }
  472. if(roller!=conn_info.oppsite_roller)
  473. {
  474. conn_info.oppsite_roller=roller;
  475. conn_info.last_oppsite_roller_time=get_current_time();
  476. }
  477. if(hb_mode==0)
  478. conn_info.my_roller++;//increase on a successful recv
  479. else if(hb_mode==1)
  480. {
  481. if(type=='h')
  482. conn_info.my_roller++;
  483. }
  484. else
  485. {
  486. assert(0==1);
  487. }
  488. if(after_recv_raw0(conn_info.raw_info)!=0) return -1;
  489. return 0;
  490. }
  491. 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.
  492. {
  493. packet_info_t &send_info=conn_info.raw_info.send_info;
  494. packet_info_t &recv_info=conn_info.raw_info.recv_info;
  495. char * recv_data;int recv_len;
  496. static char recv_data_buf[buf_len];
  497. if(recv_raw0(conn_info.raw_info,recv_data,recv_len)!=0) return -1;
  498. return reserved_parse_safer(conn_info,recv_data,recv_len,type,data,len);
  499. }
  500. void server_clear_function(u64_t u64)//used in conv_manager in server mode.for server we have to use one udp fd for one conv(udp connection),
  501. //so we have to close the fd when conv expires
  502. {
  503. //int fd=int(u64);
  504. // int ret;
  505. //assert(fd!=0);
  506. /*
  507. epoll_event ev;
  508. ev.events = EPOLLIN;
  509. ev.data.u64 = u64;
  510. ret = epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &ev);
  511. if (ret!=0)
  512. {
  513. mylog(log_fatal,"fd:%d epoll delete failed!!!!\n",fd);
  514. myexit(-1); //this shouldnt happen
  515. }*/ //no need
  516. /*ret= close(fd); //closed fd should be auto removed from epoll
  517. if (ret!=0)
  518. {
  519. mylog(log_fatal,"close fd %d failed !!!!\n",fd);
  520. myexit(-1); //this shouldnt happen
  521. }*/
  522. //mylog(log_fatal,"size:%d !!!!\n",conn_manager.udp_fd_mp.size());
  523. fd64_t fd64=u64;
  524. assert(fd_manager.exist(fd64));
  525. fd_manager.fd64_close(fd64);
  526. //assert(conn_manager.udp_fd_mp.find(fd)!=conn_manager.udp_fd_mp.end());
  527. //conn_manager.udp_fd_mp.erase(fd);
  528. }