client.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. #include "common.h"
  2. #include "network.h"
  3. #include "connection.h"
  4. #include "misc.h"
  5. #include "log.h"
  6. #include "lib/md5.h"
  7. #include "encrypt.h"
  8. #include "fd_manager.h"
  9. #ifdef UDP2RAW_MP
  10. u32_t detect_interval=1500;
  11. u64_t laste_detect_time=0;
  12. int use_udp_for_detection=0;
  13. int use_tcp_for_detection=1;
  14. extern pcap_t *pcap_handle;
  15. extern int pcap_captured_full_len;
  16. #endif
  17. int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is ready in epoll
  18. {
  19. packet_info_t &send_info=conn_info.raw_info.send_info;
  20. packet_info_t &recv_info=conn_info.raw_info.recv_info;
  21. raw_info_t &raw_info=conn_info.raw_info;
  22. conn_info.blob->conv_manager.c.clear_inactive();
  23. mylog(log_trace,"timer!\n");
  24. mylog(log_trace,"roller my %d,oppsite %d,%lld\n",int(conn_info.my_roller),int(conn_info.oppsite_roller),conn_info.last_oppsite_roller_time);
  25. mylog(log_trace,"<client_on_timer,send_info.ts_ack= %u>\n",send_info.ts_ack);
  26. #ifdef UDP2RAW_MP
  27. //mylog(log_debug,"pcap cnt :%d\n",pcap_cnt);
  28. if(send_with_pcap&&!pcap_header_captured)
  29. {
  30. if(get_current_time()-laste_detect_time>detect_interval)
  31. {
  32. laste_detect_time=get_current_time();
  33. }
  34. else
  35. {
  36. return 0;
  37. }
  38. /*
  39. struct sockaddr_in remote_addr_in={0};
  40. socklen_t slen = sizeof(sockaddr_in);
  41. int port=get_true_random_number()%65534+1;
  42. remote_addr_in.sin_family = AF_INET;
  43. remote_addr_in.sin_port = htons(port);
  44. remote_addr_in.sin_addr.s_addr = remote_ip_uint32;*/
  45. int port=get_true_random_number()%65534+1;
  46. address_t tmp_addr=remote_addr;
  47. tmp_addr.set_port(port);
  48. if(use_udp_for_detection)
  49. {
  50. int new_udp_fd=socket(tmp_addr.get_type(), SOCK_DGRAM, IPPROTO_UDP);
  51. if(new_udp_fd<0)
  52. {
  53. mylog(log_warn,"create new_udp_fd error\n");
  54. return -1;
  55. }
  56. setnonblocking(new_udp_fd);
  57. u64_t tmp=get_true_random_number();
  58. int ret=sendto(new_udp_fd,(char*)(&tmp),sizeof(tmp),0,(struct sockaddr *)&tmp_addr.inner,tmp_addr.get_len());
  59. if(ret==-1)
  60. {
  61. mylog(log_warn,"sendto() failed\n");
  62. }
  63. sock_close(new_udp_fd);
  64. }
  65. if(use_tcp_for_detection)
  66. {
  67. static int last_tcp_fd=-1;
  68. int new_tcp_fd=socket(tmp_addr.get_type(), SOCK_STREAM, IPPROTO_TCP);
  69. if(new_tcp_fd<0)
  70. {
  71. mylog(log_warn,"create new_tcp_fd error\n");
  72. return -1;
  73. }
  74. setnonblocking(new_tcp_fd);
  75. connect(new_tcp_fd,(struct sockaddr *)&tmp_addr.inner,tmp_addr.get_len());
  76. if(last_tcp_fd!=-1)
  77. sock_close(last_tcp_fd);
  78. last_tcp_fd=new_tcp_fd;
  79. //close(new_tcp_fd);
  80. }
  81. mylog(log_info,"waiting for a use-able packet to be captured\n");
  82. return 0;
  83. }
  84. #endif
  85. if(raw_info.disabled)
  86. {
  87. conn_info.state.client_current_state=client_idle;
  88. conn_info.my_id=get_true_random_number_nz();
  89. mylog(log_info,"state back to client_idle\n");
  90. }
  91. if(conn_info.state.client_current_state==client_idle)
  92. {
  93. raw_info.rst_received=0;
  94. raw_info.disabled=0;
  95. fail_time_counter++;
  96. if(max_fail_time>0&&fail_time_counter>max_fail_time)
  97. {
  98. mylog(log_fatal,"max_fail_time exceed\n");
  99. myexit(-1);
  100. }
  101. conn_info.blob->anti_replay.re_init();
  102. conn_info.my_id = get_true_random_number_nz(); ///todo no need to do this everytime
  103. address_t tmp_addr;
  104. //u32_t new_ip=0;
  105. if(!force_source_ip)
  106. {
  107. if(get_src_adress2(tmp_addr,remote_addr)!=0)
  108. {
  109. mylog(log_warn,"get_src_adress() failed\n");
  110. return -1;
  111. }
  112. //source_addr=new_addr;
  113. //source_addr.set_port(0);
  114. mylog(log_info,"source_addr is now %s\n",tmp_addr.get_ip());
  115. /*
  116. if(new_ip!=source_ip_uint32)
  117. {
  118. mylog(log_info,"source ip changed from %s to ",my_ntoa(source_ip_uint32));
  119. log_bare(log_info,"%s\n",my_ntoa(new_ip));
  120. source_ip_uint32=new_ip;
  121. send_info.src_ip=new_ip;
  122. }*/
  123. }
  124. else
  125. {
  126. tmp_addr=source_addr;
  127. }
  128. send_info.new_src_ip.from_address_t(tmp_addr);
  129. if (force_source_port == 0)
  130. {
  131. send_info.src_port = client_bind_to_a_new_port2(bind_fd,tmp_addr);
  132. }
  133. else
  134. {
  135. send_info.src_port = source_port;
  136. }
  137. if (raw_mode == mode_icmp)
  138. {
  139. send_info.dst_port = send_info.src_port;
  140. }
  141. mylog(log_info, "using port %d\n", send_info.src_port);
  142. init_filter(send_info.src_port);
  143. if(raw_mode==mode_icmp||raw_mode==mode_udp)
  144. {
  145. conn_info.state.client_current_state=client_handshake1;
  146. mylog(log_info,"state changed from client_idle to client_pre_handshake\n");
  147. }
  148. if(raw_mode==mode_faketcp)
  149. {
  150. if(use_tcp_dummy_socket)
  151. {
  152. setnonblocking(bind_fd);
  153. int ret=connect(bind_fd,(struct sockaddr *)&remote_addr.inner,remote_addr.get_len());
  154. mylog(log_debug,"ret=%d,errno=%s, %d %s\n",ret,get_sock_error(),bind_fd,remote_addr.get_str());
  155. //mylog(log_info,"ret=%d,errno=,%d %s\n",ret,bind_fd,remote_addr.get_str());
  156. conn_info.state.client_current_state=client_tcp_handshake_dummy;
  157. mylog(log_info,"state changed from client_idle to client_tcp_handshake_dummy\n");
  158. }
  159. else
  160. {
  161. conn_info.state.client_current_state=client_tcp_handshake;
  162. mylog(log_info,"state changed from client_idle to client_tcp_handshake\n");
  163. }
  164. }
  165. conn_info.last_state_time=get_current_time();
  166. conn_info.last_hb_sent_time=0;
  167. //dont return;
  168. }
  169. if(conn_info.state.client_current_state==client_tcp_handshake) //send and resend syn
  170. {
  171. assert(raw_mode==mode_faketcp);
  172. if (get_current_time() - conn_info.last_state_time > client_handshake_timeout)
  173. {
  174. conn_info.state.client_current_state = client_idle;
  175. mylog(log_info, "state back to client_idle from client_tcp_handshake\n");
  176. return 0;
  177. }
  178. else if (get_current_time() - conn_info.last_hb_sent_time > client_retry_interval)
  179. {
  180. if (raw_mode == mode_faketcp)
  181. {
  182. if (conn_info.last_hb_sent_time == 0)
  183. {
  184. send_info.psh = 0;
  185. send_info.syn = 1;
  186. send_info.ack = 0;
  187. send_info.ts_ack =0;
  188. send_info.seq=get_true_random_number();
  189. send_info.ack_seq=get_true_random_number();
  190. }
  191. }
  192. send_raw0(raw_info, 0, 0);
  193. conn_info.last_hb_sent_time = get_current_time();
  194. mylog(log_info, "(re)sent tcp syn\n");
  195. return 0;
  196. }
  197. else
  198. {
  199. return 0;
  200. }
  201. return 0;
  202. }
  203. else if(conn_info.state.client_current_state==client_tcp_handshake_dummy)
  204. {
  205. assert(raw_mode==mode_faketcp);
  206. if (get_current_time() - conn_info.last_state_time > client_handshake_timeout)
  207. {
  208. conn_info.state.client_current_state = client_idle;
  209. mylog(log_info, "state back to client_idle from client_tcp_handshake_dummy\n");
  210. return 0;
  211. }
  212. }
  213. else if(conn_info.state.client_current_state==client_handshake1)//send and resend handshake1
  214. {
  215. if(get_current_time()-conn_info.last_state_time>client_handshake_timeout)
  216. {
  217. conn_info.state.client_current_state=client_idle;
  218. mylog(log_info,"state back to client_idle from client_handshake1\n");
  219. return 0;
  220. }
  221. else if(get_current_time()-conn_info.last_hb_sent_time>client_retry_interval)
  222. {
  223. if(raw_mode==mode_faketcp)
  224. {
  225. if(conn_info.last_hb_sent_time==0)
  226. {
  227. send_info.seq++;
  228. send_info.ack_seq=recv_info.seq+1;
  229. send_info.ts_ack=recv_info.ts;
  230. raw_info.reserved_send_seq=send_info.seq;
  231. }
  232. send_info.seq=raw_info.reserved_send_seq;
  233. send_info.psh = 0;
  234. send_info.syn = 0;
  235. send_info.ack = 1;
  236. if(!use_tcp_dummy_socket)
  237. send_raw0(raw_info, 0, 0);
  238. send_handshake(raw_info,conn_info.my_id,0,const_id);
  239. send_info.seq+=raw_info.send_info.data_len;
  240. }
  241. else
  242. {
  243. send_handshake(raw_info,conn_info.my_id,0,const_id);
  244. if(raw_mode==mode_icmp)
  245. send_info.my_icmp_seq++;
  246. }
  247. conn_info.last_hb_sent_time=get_current_time();
  248. mylog(log_info,"(re)sent handshake1\n");
  249. return 0;
  250. }
  251. else
  252. {
  253. return 0;
  254. }
  255. return 0;
  256. }
  257. else if(conn_info.state.client_current_state==client_handshake2)
  258. {
  259. if(get_current_time()-conn_info.last_state_time>client_handshake_timeout)
  260. {
  261. conn_info.state.client_current_state=client_idle;
  262. mylog(log_info,"state back to client_idle from client_handshake2\n");
  263. return 0;
  264. }
  265. else if(get_current_time()-conn_info.last_hb_sent_time>client_retry_interval)
  266. {
  267. if(raw_mode==mode_faketcp)
  268. {
  269. if(conn_info.last_hb_sent_time==0)
  270. {
  271. send_info.ack_seq=recv_info.seq+raw_info.recv_info.data_len;
  272. send_info.ts_ack=recv_info.ts;
  273. raw_info.reserved_send_seq=send_info.seq;
  274. }
  275. send_info.seq=raw_info.reserved_send_seq;
  276. send_handshake(raw_info,conn_info.my_id,conn_info.oppsite_id,const_id);
  277. send_info.seq+=raw_info.send_info.data_len;
  278. }
  279. else
  280. {
  281. send_handshake(raw_info,conn_info.my_id,conn_info.oppsite_id,const_id);
  282. if(raw_mode==mode_icmp)
  283. send_info.my_icmp_seq++;
  284. }
  285. conn_info.last_hb_sent_time=get_current_time();
  286. mylog(log_info,"(re)sent handshake2\n");
  287. return 0;
  288. }
  289. else
  290. {
  291. return 0;
  292. }
  293. return 0;
  294. }
  295. else if(conn_info.state.client_current_state==client_ready)
  296. {
  297. fail_time_counter=0;
  298. mylog(log_trace,"time %llu,%llu\n",get_current_time(),conn_info.last_state_time);
  299. if(get_current_time()-conn_info.last_hb_recv_time>client_conn_timeout)
  300. {
  301. conn_info.state.client_current_state=client_idle;
  302. conn_info.my_id=get_true_random_number_nz();
  303. mylog(log_info,"state back to client_idle from client_ready bc of server-->client direction timeout\n");
  304. return 0;
  305. }
  306. if(get_current_time()- conn_info.last_oppsite_roller_time>client_conn_uplink_timeout)
  307. {
  308. conn_info.state.client_current_state=client_idle;
  309. conn_info.my_id=get_true_random_number_nz();
  310. mylog(log_info,"state back to client_idle from client_ready bc of client-->server direction timeout\n");
  311. }
  312. if(get_current_time()-conn_info.last_hb_sent_time<heartbeat_interval)
  313. {
  314. return 0;
  315. }
  316. mylog(log_debug,"heartbeat sent <%x,%x>\n",conn_info.oppsite_id,conn_info.my_id);
  317. if(hb_mode==0)
  318. send_safer(conn_info,'h',hb_buf,0);/////////////send
  319. else
  320. send_safer(conn_info,'h',hb_buf,hb_len);
  321. conn_info.last_hb_sent_time=get_current_time();
  322. return 0;
  323. }
  324. else
  325. {
  326. mylog(log_fatal,"unknown state,this shouldnt happen.\n");
  327. myexit(-1);
  328. }
  329. return 0;
  330. }
  331. int client_on_raw_recv_hs2_or_ready(conn_info_t &conn_info,char type,char *data,int data_len)
  332. {
  333. packet_info_t &send_info=conn_info.raw_info.send_info;
  334. packet_info_t &recv_info=conn_info.raw_info.recv_info;
  335. if(!recv_info.new_src_ip.equal(send_info.new_dst_ip)||recv_info.src_port!=send_info.dst_port)
  336. {
  337. mylog(log_warn,"unexpected adress %s %s %d %d,this shouldnt happen.\n",recv_info.new_src_ip.get_str1(),send_info.new_dst_ip.get_str2(),recv_info.src_port,send_info.dst_port);
  338. return -1;
  339. }
  340. if(conn_info.state.client_current_state==client_handshake2)
  341. {
  342. mylog(log_info,"changed state from to client_handshake2 to client_ready\n");
  343. conn_info.state.client_current_state=client_ready;
  344. conn_info.last_hb_sent_time=0;
  345. conn_info.last_hb_recv_time=get_current_time();
  346. conn_info.last_oppsite_roller_time=conn_info.last_hb_recv_time;
  347. client_on_timer(conn_info);
  348. }
  349. if(data_len>=0&&type=='h')
  350. {
  351. mylog(log_debug,"[hb]heart beat received,oppsite_roller=%d\n",int(conn_info.oppsite_roller));
  352. conn_info.last_hb_recv_time=get_current_time();
  353. return 0;
  354. }
  355. else if(data_len>= int( sizeof(u32_t))&&type=='d')
  356. {
  357. mylog(log_trace,"received a data from fake tcp,len:%d\n",data_len);
  358. if(hb_mode==0)
  359. conn_info.last_hb_recv_time=get_current_time();
  360. u32_t tmp_conv_id;
  361. memcpy(&tmp_conv_id,&data[0],sizeof(tmp_conv_id));
  362. tmp_conv_id=ntohl(tmp_conv_id);
  363. if(!conn_info.blob->conv_manager.c.is_conv_used(tmp_conv_id))
  364. {
  365. mylog(log_info,"unknow conv %d,ignore\n",tmp_conv_id);
  366. return 0;
  367. }
  368. conn_info.blob->conv_manager.c.update_active_time(tmp_conv_id);
  369. //u64_t u64=conn_info.blob->conv_manager.c.find_data_by_conv(tmp_conv_id);
  370. address_t tmp_addr=conn_info.blob->conv_manager.c.find_data_by_conv(tmp_conv_id);
  371. //sockaddr_in tmp_sockaddr={0};
  372. //tmp_sockaddr.sin_family = AF_INET;
  373. //tmp_sockaddr.sin_addr.s_addr=(u64>>32u);
  374. //tmp_sockaddr.sin_port= htons(uint16_t((u64<<32u)>>32u));
  375. int ret=sendto(udp_fd,data+sizeof(u32_t),data_len -(sizeof(u32_t)),0,(struct sockaddr *)&tmp_addr.inner,tmp_addr.get_len());
  376. if(ret<0)
  377. {
  378. mylog(log_warn,"sento returned %d,%s,%02x,%s\n",ret,get_sock_error(),int(tmp_addr.get_type()),tmp_addr.get_str());
  379. //perror("ret<0");
  380. }
  381. }
  382. else
  383. {
  384. mylog(log_warn,"unknown packet,this shouldnt happen.\n");
  385. return -1;
  386. }
  387. return 0;
  388. }
  389. int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a packet.
  390. {
  391. char* data;int data_len;
  392. packet_info_t &send_info=conn_info.raw_info.send_info;
  393. packet_info_t &recv_info=conn_info.raw_info.recv_info;
  394. raw_info_t &raw_info=conn_info.raw_info;
  395. mylog(log_trace,"<client_on_raw_recv,send_info.ts_ack= %u>\n",send_info.ts_ack);
  396. #ifdef UDP2RAW_LINUX
  397. if(pre_recv_raw_packet()<0) return -1;
  398. #endif
  399. if(conn_info.state.client_current_state==client_idle )
  400. {
  401. discard_raw_packet();
  402. //recv(raw_recv_fd, 0,0, 0 );
  403. }
  404. else if(conn_info.state.client_current_state==client_tcp_handshake||conn_info.state.client_current_state==client_tcp_handshake_dummy)//received syn ack
  405. {
  406. assert(raw_mode==mode_faketcp);
  407. if(recv_raw0(raw_info,data,data_len)<0)
  408. {
  409. return -1;
  410. }
  411. if(data_len>=max_data_len+1)
  412. {
  413. mylog(log_debug,"data_len=%d >= max_data_len+1,ignored",data_len);
  414. return -1;
  415. }
  416. if(!recv_info.new_src_ip.equal(send_info.new_dst_ip)||recv_info.src_port!=send_info.dst_port)
  417. {
  418. mylog(log_debug,"unexpected adress %s %s %d %d\n",recv_info.new_src_ip.get_str1(),send_info.new_dst_ip.get_str2(),recv_info.src_port,send_info.dst_port);
  419. return -1;
  420. }
  421. if(data_len==0&&raw_info.recv_info.syn==1&&raw_info.recv_info.ack==1)
  422. {
  423. if(conn_info.state.client_current_state==client_tcp_handshake)
  424. {
  425. if(recv_info.ack_seq!=send_info.seq+1)
  426. {
  427. mylog(log_debug,"seq ack_seq mis match\n");
  428. return -1;
  429. }
  430. mylog(log_info,"state changed from client_tcp_handshake to client_handshake1\n");
  431. }
  432. else
  433. {
  434. send_info.seq=recv_info.ack_seq-1;
  435. mylog(log_info,"state changed from client_tcp_dummy to client_handshake1\n");
  436. //send_info.ack_seq=recv_info.seq+1;
  437. }
  438. conn_info.state.client_current_state = client_handshake1;
  439. conn_info.last_state_time = get_current_time();
  440. conn_info.last_hb_sent_time=0;
  441. client_on_timer(conn_info);
  442. return 0;
  443. }
  444. else
  445. {
  446. mylog(log_debug,"unexpected packet type,expected:syn ack\n");
  447. return -1;
  448. }
  449. }
  450. else if(conn_info.state.client_current_state==client_handshake1)//recevied respond of handshake1
  451. {
  452. if(recv_bare(raw_info,data,data_len)!=0)
  453. {
  454. mylog(log_debug,"recv_bare failed!\n");
  455. return -1;
  456. }
  457. if(!recv_info.new_src_ip.equal(send_info.new_dst_ip)||recv_info.src_port!=send_info.dst_port)
  458. {
  459. mylog(log_debug,"unexpected adress %s %s %d %d\n",recv_info.new_src_ip.get_str1(),send_info.new_dst_ip.get_str2(),recv_info.src_port,send_info.dst_port);
  460. return -1;
  461. }
  462. if(data_len<int( 3*sizeof(my_id_t)))
  463. {
  464. mylog(log_debug,"too short to be a handshake\n");
  465. return -1;
  466. }
  467. my_id_t tmp_oppsite_id;
  468. memcpy(&tmp_oppsite_id,&data[0],sizeof(tmp_oppsite_id));
  469. tmp_oppsite_id=ntohl(tmp_oppsite_id);
  470. my_id_t tmp_my_id;
  471. memcpy(&tmp_my_id,&data[sizeof(my_id_t)],sizeof(tmp_my_id));
  472. tmp_my_id=ntohl(tmp_my_id);
  473. my_id_t tmp_oppsite_const_id;
  474. memcpy(&tmp_oppsite_const_id,&data[sizeof(my_id_t)*2],sizeof(tmp_oppsite_const_id));
  475. tmp_oppsite_const_id=ntohl(tmp_oppsite_const_id);
  476. if(tmp_my_id!=conn_info.my_id)
  477. {
  478. mylog(log_debug,"tmp_my_id doesnt match\n");
  479. return -1;
  480. }
  481. if(raw_mode==mode_faketcp)
  482. {
  483. if(recv_info.ack_seq!=send_info.seq)
  484. {
  485. mylog(log_debug,"seq ack_seq mis match\n");
  486. return -1;
  487. }
  488. if(recv_info.seq!=send_info.ack_seq)
  489. {
  490. mylog(log_debug,"seq ack_seq mis match\n");
  491. return -1;
  492. }
  493. }
  494. conn_info.oppsite_id=tmp_oppsite_id;
  495. mylog(log_info,"changed state from to client_handshake1 to client_handshake2,my_id is %x,oppsite id is %x\n",conn_info.my_id,conn_info.oppsite_id);
  496. conn_info.state.client_current_state = client_handshake2;
  497. conn_info.last_state_time = get_current_time();
  498. conn_info.last_hb_sent_time=0;
  499. client_on_timer(conn_info);
  500. return 0;
  501. }
  502. else if(conn_info.state.client_current_state==client_handshake2||conn_info.state.client_current_state==client_ready)//received heartbeat or data
  503. {
  504. vector<char> type_vec;
  505. vector<string> data_vec;
  506. recv_safer_multi(conn_info,type_vec,data_vec);
  507. if(data_vec.empty())
  508. {
  509. mylog(log_debug,"recv_safer failed!\n");
  510. return -1;
  511. }
  512. for(int i=0;i<(int)type_vec.size();i++)
  513. {
  514. char type=type_vec[i];
  515. char *data=(char *)data_vec[i].c_str(); //be careful, do not append data to it
  516. int data_len=data_vec[i].length();
  517. client_on_raw_recv_hs2_or_ready(conn_info, type, data,data_len);
  518. }
  519. return 0;
  520. }
  521. else
  522. {
  523. mylog(log_fatal,"unknown state,this shouldnt happen.\n");
  524. myexit(-1);
  525. }
  526. return 0;
  527. }
  528. int client_on_udp_recv(conn_info_t &conn_info)
  529. {
  530. int recv_len;
  531. char buf[buf_len];
  532. address_t::storage_t udp_new_addr_in={{0}};
  533. socklen_t udp_new_addr_len = sizeof(address_t::storage_t);
  534. if ((recv_len = recvfrom(udp_fd, buf, max_data_len+1, 0,
  535. (struct sockaddr *) &udp_new_addr_in, &udp_new_addr_len)) == -1) {
  536. mylog(log_debug,"recv_from error,%s\n",get_sock_error());
  537. return -1;
  538. //myexit(1);
  539. };
  540. if(recv_len==max_data_len+1)
  541. {
  542. mylog(log_warn,"huge packet, data_len > %d,dropped\n",max_data_len);
  543. return -1;
  544. }
  545. if(recv_len>=mtu_warn)
  546. {
  547. mylog(log_warn,"huge packet,data len=%d (>=%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ",recv_len,mtu_warn);
  548. }
  549. address_t tmp_addr;
  550. tmp_addr.from_sockaddr((sockaddr *)&udp_new_addr_in,udp_new_addr_len);
  551. u32_t conv;
  552. if(!conn_info.blob->conv_manager.c.is_data_used(tmp_addr))
  553. {
  554. if(conn_info.blob->conv_manager.c.get_size() >=max_conv_num)
  555. {
  556. mylog(log_warn,"ignored new udp connect bc max_conv_num exceed\n");
  557. return -1;
  558. }
  559. conv=conn_info.blob->conv_manager.c.get_new_conv();
  560. conn_info.blob->conv_manager.c.insert_conv(conv,tmp_addr);
  561. mylog(log_info,"new packet from %s,conv_id=%x\n",tmp_addr.get_str(),conv);
  562. }
  563. else
  564. {
  565. conv=conn_info.blob->conv_manager.c.find_conv_by_data(tmp_addr);
  566. }
  567. conn_info.blob->conv_manager.c.update_active_time(conv);
  568. if(conn_info.state.client_current_state==client_ready)
  569. {
  570. send_data_safer(conn_info,buf,recv_len,conv);
  571. }
  572. return 0;
  573. }
  574. void udp_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
  575. {
  576. conn_info_t & conn_info= *((conn_info_t*)watcher->data);
  577. client_on_udp_recv(conn_info);
  578. }
  579. void raw_recv_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
  580. {
  581. if(is_udp2raw_mp)assert(0==1);
  582. conn_info_t & conn_info= *((conn_info_t*)watcher->data);
  583. client_on_raw_recv(conn_info);
  584. }
  585. #ifdef UDP2RAW_MP
  586. void async_cb(struct ev_loop *loop, struct ev_async *watcher, int revents)
  587. {
  588. conn_info_t & conn_info= *((conn_info_t*)watcher->data);
  589. if(send_with_pcap&&!pcap_header_captured)
  590. {
  591. int empty=0;char *p;int len;
  592. pthread_mutex_lock(&queue_mutex);
  593. empty=my_queue.empty();
  594. if(!empty)
  595. {
  596. my_queue.peek_front(p,len);
  597. my_queue.pop_front();
  598. }
  599. pthread_mutex_unlock(&queue_mutex);
  600. if(empty) return;
  601. pcap_header_captured=1;
  602. assert(pcap_link_header_len!=-1);
  603. memcpy(pcap_header_buf,p,max_data_len);
  604. log_bare(log_info,"link level header captured:\n");
  605. unsigned char *tmp=(unsigned char*)pcap_header_buf;
  606. pcap_captured_full_len=len;
  607. for(int i=0;i<pcap_link_header_len;i++)
  608. log_bare(log_info,"<%x>",(u32_t)tmp[i]);
  609. log_bare(log_info,"\n");
  610. return ;
  611. }
  612. //mylog(log_info,"async_cb called\n");
  613. while(1)
  614. {
  615. int empty=0;char *p;int len;
  616. pthread_mutex_lock(&queue_mutex);
  617. empty=my_queue.empty();
  618. if(!empty)
  619. {
  620. my_queue.peek_front(p,len);
  621. my_queue.pop_front();
  622. }
  623. pthread_mutex_unlock(&queue_mutex);
  624. if(empty) break;
  625. if(g_fix_gro==0&&len>max_data_len)
  626. {
  627. mylog(log_warn,"huge packet %d > %d, dropped\n",len,max_data_len);
  628. break;
  629. }
  630. int new_len=len-pcap_link_header_len;
  631. memcpy(g_packet_buf,p+pcap_link_header_len,new_len);
  632. g_packet_buf_len=new_len;
  633. assert(g_packet_buf_cnt==0);
  634. g_packet_buf_cnt++;
  635. client_on_raw_recv(conn_info);
  636. }
  637. }
  638. #endif
  639. void clear_timer_cb(struct ev_loop *loop, struct ev_timer *watcher, int revents)
  640. {
  641. conn_info_t & conn_info= *((conn_info_t*)watcher->data);
  642. client_on_timer(conn_info);
  643. }
  644. void fifo_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
  645. {
  646. conn_info_t & conn_info= *((conn_info_t*)watcher->data);
  647. char buf[buf_len];
  648. int fifo_fd=watcher->fd;
  649. int len=read (fifo_fd, buf, sizeof (buf));
  650. if(len<0)
  651. {
  652. mylog(log_warn,"fifo read failed len=%d,errno=%s\n",len,get_sock_error());
  653. return;
  654. }
  655. buf[len]=0;
  656. while(len>=1&&buf[len-1]=='\n')
  657. buf[len-1]=0;
  658. mylog(log_info,"got data from fifo,len=%d,s=[%s]\n",len,buf);
  659. if(strcmp(buf,"reconnect")==0)
  660. {
  661. mylog(log_info,"received command: reconnect\n");
  662. conn_info.state.client_current_state=client_idle;
  663. conn_info.my_id=get_true_random_number_nz();
  664. }
  665. else
  666. {
  667. mylog(log_info,"unknown command\n");
  668. }
  669. }
  670. int client_event_loop()
  671. {
  672. char buf[buf_len];
  673. conn_info_t conn_info;
  674. conn_info.my_id=get_true_random_number_nz();
  675. conn_info.prepare();
  676. packet_info_t &send_info=conn_info.raw_info.send_info;
  677. packet_info_t &recv_info=conn_info.raw_info.recv_info;
  678. #ifdef UDP2RAW_LINUX
  679. if(lower_level)
  680. {
  681. if(lower_level_manual)
  682. {
  683. int index;
  684. init_ifindex(if_name,raw_send_fd,index);
  685. //init_ifindex(if_name);
  686. memset(&send_info.addr_ll, 0, sizeof(send_info.addr_ll));
  687. send_info.addr_ll.sll_family = AF_PACKET;
  688. send_info.addr_ll.sll_ifindex =index;
  689. send_info.addr_ll.sll_halen = ETHER_ADDR_LEN;
  690. send_info.addr_ll.sll_protocol = htons(ETH_P_IP);
  691. memcpy(&send_info.addr_ll.sll_addr, dest_hw_addr, ETHER_ADDR_LEN);
  692. mylog(log_info,"we are running at lower-level (manual) mode\n");
  693. }
  694. else
  695. {
  696. u32_t dest_ip;
  697. string if_name_string;
  698. string hw_string;
  699. assert(remote_addr.get_type()==AF_INET);
  700. if(retry_on_error==0)
  701. {
  702. if(find_lower_level_info(remote_addr.inner.ipv4.sin_addr.s_addr,dest_ip,if_name_string,hw_string)!=0)
  703. {
  704. mylog(log_fatal,"auto detect lower-level info failed for %s,specific it manually\n",remote_addr.get_ip());
  705. myexit(-1);
  706. }
  707. }
  708. else
  709. {
  710. int ok=0;
  711. while(!ok)
  712. {
  713. if(find_lower_level_info(remote_addr.inner.ipv4.sin_addr.s_addr,dest_ip,if_name_string,hw_string)!=0)
  714. {
  715. mylog(log_warn,"auto detect lower-level info failed for %s,retry in %d seconds\n",remote_addr.get_ip(),retry_on_error_interval);
  716. sleep(retry_on_error_interval);
  717. }
  718. else
  719. {
  720. ok=1;
  721. }
  722. }
  723. }
  724. mylog(log_info,"we are running at lower-level (auto) mode,%s %s %s\n",my_ntoa(dest_ip),if_name_string.c_str(),hw_string.c_str());
  725. u32_t hw[6];
  726. memset(hw, 0, sizeof(hw));
  727. sscanf(hw_string.c_str(), "%x:%x:%x:%x:%x:%x",&hw[0], &hw[1], &hw[2],
  728. &hw[3], &hw[4], &hw[5]);
  729. mylog(log_warn,
  730. "make sure this is correct: if_name=<%s> dest_mac_adress=<%02x:%02x:%02x:%02x:%02x:%02x> \n",
  731. if_name_string.c_str(), hw[0], hw[1], hw[2], hw[3], hw[4], hw[5]);
  732. for (int i = 0; i < 6; i++) {
  733. dest_hw_addr[i] = uint8_t(hw[i]);
  734. }
  735. //mylog(log_fatal,"--lower-level auto for client hasnt been implemented\n");
  736. int index;
  737. init_ifindex(if_name_string.c_str(),raw_send_fd,index);
  738. memset(&send_info.addr_ll, 0, sizeof(send_info.addr_ll));
  739. send_info.addr_ll.sll_family = AF_PACKET;
  740. send_info.addr_ll.sll_ifindex = index;
  741. send_info.addr_ll.sll_halen = ETHER_ADDR_LEN;
  742. send_info.addr_ll.sll_protocol = htons(ETH_P_IP);
  743. memcpy(&send_info.addr_ll.sll_addr, dest_hw_addr, ETHER_ADDR_LEN);
  744. //mylog(log_info,"we are running at lower-level (manual) mode\n");
  745. }
  746. }
  747. #endif
  748. #ifdef UDP2RAW_MP
  749. address_t tmp_addr;
  750. if(get_src_adress2(tmp_addr,remote_addr)!=0)
  751. {
  752. mylog(log_error,"get_src_adress() failed\n");
  753. myexit(-1);
  754. }
  755. if(strcmp(dev,"")==0)
  756. {
  757. mylog(log_info,"--dev have not been set, trying to detect automatically, available devices:\n");
  758. mylog(log_info,"available device(device name: ip address ; description):\n");
  759. char errbuf[PCAP_ERRBUF_SIZE];
  760. int found=0;
  761. pcap_if_t *interfaces,*d;
  762. if(pcap_findalldevs(&interfaces,errbuf)==-1)
  763. {
  764. mylog(log_fatal,"error in pcap_findalldevs(),%s\n",errbuf);
  765. myexit(-1);
  766. }
  767. for(pcap_if_t *d=interfaces; d!=NULL; d=d->next) {
  768. log_bare(log_warn,"%s:", d->name);
  769. int cnt=0;
  770. for(pcap_addr_t *a=d->addresses; a!=NULL; a=a->next) {
  771. if(a->addr==NULL)
  772. {
  773. log_bare(log_debug," [a->addr==NULL]");
  774. continue;
  775. }
  776. if(a->addr->sa_family == AF_INET||a->addr->sa_family == AF_INET6)
  777. {
  778. cnt++;
  779. if(a->addr->sa_family ==AF_INET)
  780. {
  781. char s[max_addr_len];
  782. inet_ntop(AF_INET, &((struct sockaddr_in*)a->addr)->sin_addr, s,max_addr_len);
  783. log_bare(log_warn," [%s]", s);
  784. if(a->addr->sa_family==raw_ip_version)
  785. {
  786. if(((struct sockaddr_in*)a->addr)->sin_addr.s_addr ==tmp_addr.inner.ipv4.sin_addr.s_addr)
  787. {
  788. found++;
  789. strcpy(dev,d->name);
  790. }
  791. }
  792. }
  793. else
  794. {
  795. assert(a->addr->sa_family ==AF_INET6);
  796. char s[max_addr_len];
  797. inet_ntop(AF_INET6, &((struct sockaddr_in6*)a->addr)->sin6_addr, s,max_addr_len);
  798. log_bare(log_warn," [%s]", s);
  799. if(a->addr->sa_family==raw_ip_version)
  800. {
  801. if( memcmp( &((struct sockaddr_in6*)a->addr)->sin6_addr,&tmp_addr.inner.ipv6.sin6_addr,sizeof(struct in6_addr))==0 )
  802. {
  803. found++;
  804. strcpy(dev,d->name);
  805. }
  806. }
  807. }
  808. }
  809. else
  810. {
  811. log_bare(log_debug," [unknow:%d]",int(a->addr->sa_family));
  812. }
  813. }
  814. if(cnt==0) log_bare(log_warn," [no ip found]");
  815. if(d->description==0)
  816. {
  817. log_bare(log_warn,"; (no description available)");
  818. }
  819. else
  820. {
  821. log_bare(log_warn,"; %s", d->description);
  822. }
  823. log_bare(log_warn,"\n");
  824. }
  825. if(found==0)
  826. {
  827. mylog(log_fatal,"no matched device found for ip: [%s]\n",tmp_addr.get_ip());
  828. myexit(-1);
  829. }
  830. else if(found==1)
  831. {
  832. mylog(log_info,"using device:[%s], ip: [%s]\n",dev,tmp_addr.get_ip());
  833. }
  834. else
  835. {
  836. mylog(log_fatal,"more than one devices found for ip: [%s] , you need to use --dev manually\n",tmp_addr.get_ip());
  837. myexit(-1);
  838. }
  839. }
  840. else
  841. {
  842. mylog(log_info,"--dev has been manually set, using device:[%s]\n",dev);
  843. }
  844. #endif
  845. send_info.src_port=0;
  846. memset(&send_info.new_src_ip,0,sizeof(send_info.new_src_ip));
  847. int i, j, k;int ret;
  848. send_info.new_dst_ip.from_address_t(remote_addr);
  849. send_info.dst_port=remote_addr.get_port();
  850. udp_fd=socket(local_addr.get_type(), SOCK_DGRAM, IPPROTO_UDP);
  851. set_buf_size(udp_fd,socket_buf_size);
  852. if (::bind(udp_fd, (struct sockaddr*) &local_addr.inner, local_addr.get_len()) == -1) {
  853. mylog(log_fatal,"socket bind error\n");
  854. //perror("socket bind error");
  855. myexit(1);
  856. }
  857. setnonblocking(udp_fd);
  858. //epollfd = epoll_create1(0);
  859. //const int max_events = 4096;
  860. //struct epoll_event ev, events[max_events];
  861. //if (epollfd < 0) {
  862. // mylog(log_fatal,"epoll return %d\n", epollfd);
  863. // myexit(-1);
  864. //}
  865. struct ev_loop * loop= ev_default_loop(0);
  866. assert(loop != NULL);
  867. //ev.events = EPOLLIN;
  868. //ev.data.u64 = udp_fd;
  869. //ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, udp_fd, &ev);
  870. //if (ret!=0) {
  871. // mylog(log_fatal,"add udp_listen_fd error\n");
  872. // myexit(-1);
  873. //}
  874. struct ev_io udp_accept_watcher;
  875. udp_accept_watcher.data=&conn_info;
  876. ev_io_init(&udp_accept_watcher, udp_accept_cb, udp_fd, EV_READ);
  877. ev_io_start(loop, &udp_accept_watcher);
  878. //ev.events = EPOLLIN;
  879. //ev.data.u64 = raw_recv_fd;
  880. //ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, raw_recv_fd, &ev);
  881. //if (ret!= 0) {
  882. // mylog(log_fatal,"add raw_fd error\n");
  883. // myexit(-1);
  884. //}
  885. #ifdef UDP2RAW_LINUX
  886. struct ev_io raw_recv_watcher;
  887. raw_recv_watcher.data=&conn_info;
  888. ev_io_init(&raw_recv_watcher, raw_recv_cb, raw_recv_fd, EV_READ);
  889. ev_io_start(loop, &raw_recv_watcher);
  890. #endif
  891. #ifdef UDP2RAW_MP
  892. g_default_loop=loop;
  893. async_watcher.data=&conn_info;
  894. ev_async_init(&async_watcher,async_cb);
  895. ev_async_start(loop,&async_watcher);
  896. init_raw_socket();//must be put after dev detection
  897. #endif
  898. //set_timer(epollfd,timer_fd);
  899. struct ev_timer clear_timer;
  900. clear_timer.data=&conn_info;
  901. ev_timer_init(&clear_timer, clear_timer_cb, 0, timer_interval/1000.0);
  902. ev_timer_start(loop, &clear_timer);
  903. mylog(log_debug,"send_raw : from %s %d to %s %d\n",send_info.new_src_ip.get_str1(),send_info.src_port,send_info.new_dst_ip.get_str2(),send_info.dst_port);
  904. int fifo_fd=-1;
  905. struct ev_io fifo_watcher;
  906. fifo_watcher.data=&conn_info;
  907. if(fifo_file[0]!=0)
  908. {
  909. fifo_fd=create_fifo(fifo_file);
  910. ev_io_init(&fifo_watcher, fifo_cb, fifo_fd, EV_READ);
  911. ev_io_start(loop, &fifo_watcher);
  912. mylog(log_info,"fifo_file=%s\n",fifo_file);
  913. }
  914. ev_run(loop, 0);
  915. return 0;
  916. }