main.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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. char hb_buf[buf_len];
  10. int on_epoll_recv_event=0; //TODO, just a flag to help detect epoll infinite shoot
  11. u32_t detect_interval=1500;
  12. u64_t laste_detect_time=0;
  13. int use_udp_for_detection=0;
  14. int use_tcp_for_detection=1;
  15. int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is ready in epoll
  16. {
  17. packet_info_t &send_info=conn_info.raw_info.send_info;
  18. packet_info_t &recv_info=conn_info.raw_info.recv_info;
  19. raw_info_t &raw_info=conn_info.raw_info;
  20. conn_info.blob->conv_manager.clear_inactive();
  21. mylog(log_trace,"timer!\n");
  22. 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);
  23. mylog(log_trace,"<client_on_timer,send_info.ts_ack= %u>\n",send_info.ts_ack);
  24. //mylog(log_debug,"pcap cnt :%d\n",pcap_cnt);
  25. if(send_with_pcap&&!pcap_header_captured)
  26. {
  27. if(get_current_time()-laste_detect_time>detect_interval)
  28. {
  29. laste_detect_time=get_current_time();
  30. }
  31. else
  32. {
  33. return 0;
  34. }
  35. struct sockaddr_in remote_addr_in={0};
  36. socklen_t slen = sizeof(sockaddr_in);
  37. //memset(&remote_addr_in, 0, sizeof(remote_addr_in));
  38. int port=get_true_random_number()%65534+1;
  39. remote_addr_in.sin_family = AF_INET;
  40. remote_addr_in.sin_port = htons(port);
  41. remote_addr_in.sin_addr.s_addr = remote_ip_uint32;
  42. if(use_udp_for_detection)
  43. {
  44. int new_udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  45. if(new_udp_fd<0)
  46. {
  47. mylog(log_warn,"create new_udp_fd error\n");
  48. return -1;
  49. }
  50. setnonblocking(new_udp_fd);
  51. u64_t tmp=get_true_random_number();
  52. int ret=sendto(new_udp_fd,(char*)(&tmp),sizeof(tmp),0,(struct sockaddr *)&remote_addr_in,sizeof(remote_addr_in));
  53. if(ret==-1)
  54. {
  55. mylog(log_warn,"sendto() failed\n");
  56. }
  57. close(new_udp_fd);
  58. }
  59. if(use_tcp_for_detection)
  60. {
  61. static int last_tcp_fd=-1;
  62. int new_tcp_fd=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  63. if(new_tcp_fd<0)
  64. {
  65. mylog(log_warn,"create new_tcp_fd error\n");
  66. return -1;
  67. }
  68. setnonblocking(new_tcp_fd);
  69. connect(new_tcp_fd,(struct sockaddr *)&remote_addr_in,sizeof(remote_addr_in));
  70. if(last_tcp_fd!=-1)
  71. close(last_tcp_fd);
  72. last_tcp_fd=new_tcp_fd;
  73. //close(new_tcp_fd);
  74. }
  75. mylog(log_info,"waiting for a use-able packet to be captured\n");
  76. return 0;
  77. }
  78. if(raw_info.disabled)
  79. {
  80. conn_info.state.client_current_state=client_idle;
  81. conn_info.my_id=get_true_random_number_nz();
  82. mylog(log_info,"state back to client_idle\n");
  83. }
  84. if(conn_info.state.client_current_state==client_idle)
  85. {
  86. raw_info.rst_received=0;
  87. raw_info.disabled=0;
  88. fail_time_counter++;
  89. if(max_fail_time>0&&fail_time_counter>max_fail_time)
  90. {
  91. mylog(log_fatal,"max_fail_time exceed\n");
  92. myexit(-1);
  93. }
  94. conn_info.blob->anti_replay.re_init();
  95. conn_info.my_id = get_true_random_number_nz(); ///todo no need to do this everytime
  96. u32_t new_ip=0;
  97. if(!force_source_ip&&get_src_adress(new_ip,remote_ip_uint32,remote_port)==0)
  98. {
  99. if(new_ip!=source_ip_uint32)
  100. {
  101. mylog(log_info,"source ip changed from %s to ",my_ntoa(source_ip_uint32));
  102. log_bare(log_info,"%s\n",my_ntoa(new_ip));
  103. source_ip_uint32=new_ip;
  104. send_info.src_ip=new_ip;
  105. }
  106. }
  107. if (source_port == 0)
  108. {
  109. send_info.src_port = client_bind_to_a_new_port(bind_fd,0);
  110. }
  111. else
  112. {
  113. send_info.src_port = source_port;
  114. assert(try_to_list_and_bind(bind_fd,0,source_port)==0);
  115. }
  116. if (raw_mode == mode_icmp)
  117. {
  118. send_info.dst_port = send_info.src_port;
  119. }
  120. mylog(log_info, "using port %d\n", send_info.src_port);
  121. init_filter(send_info.src_port);
  122. if(raw_mode==mode_icmp||raw_mode==mode_udp)
  123. {
  124. conn_info.state.client_current_state=client_handshake1;
  125. mylog(log_info,"state changed from client_idle to client_pre_handshake\n");
  126. }
  127. if(raw_mode==mode_faketcp)
  128. {
  129. if(use_tcp_dummy_socket)
  130. {
  131. struct sockaddr_in remote_addr_in={0};
  132. socklen_t slen = sizeof(sockaddr_in);
  133. //memset(&remote_addr_in, 0, sizeof(remote_addr_in));
  134. remote_addr_in.sin_family = AF_INET;
  135. remote_addr_in.sin_port = htons(remote_port);
  136. remote_addr_in.sin_addr.s_addr = remote_ip_uint32;
  137. //int new_tcp_fd=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  138. setnonblocking(bind_fd);
  139. int ret=connect(bind_fd,(struct sockaddr *)&remote_addr_in,sizeof(remote_addr_in));
  140. mylog(log_info,"ret=%d,errno=%s,%d %d\n",ret,strerror(errno),bind_fd,remote_port);
  141. conn_info.state.client_current_state=client_tcp_handshake_dummy;
  142. mylog(log_info,"state changed from client_idle to client_tcp_handshake_dummy\n");
  143. }
  144. else
  145. {
  146. conn_info.state.client_current_state=client_tcp_handshake;
  147. mylog(log_info,"state changed from client_idle to client_tcp_handshake\n");
  148. }
  149. }
  150. conn_info.last_state_time=get_current_time();
  151. conn_info.last_hb_sent_time=0;
  152. //dont return;
  153. }
  154. if(conn_info.state.client_current_state==client_tcp_handshake) //send and resend syn
  155. {
  156. assert(raw_mode==mode_faketcp);
  157. if (get_current_time() - conn_info.last_state_time > client_handshake_timeout)
  158. {
  159. conn_info.state.client_current_state = client_idle;
  160. mylog(log_info, "state back to client_idle from client_tcp_handshake\n");
  161. return 0;
  162. }
  163. else if (get_current_time() - conn_info.last_hb_sent_time > client_retry_interval)
  164. {
  165. if (raw_mode == mode_faketcp)
  166. {
  167. if (conn_info.last_hb_sent_time == 0)
  168. {
  169. send_info.psh = 0;
  170. send_info.syn = 1;
  171. send_info.ack = 0;
  172. send_info.ts_ack =0;
  173. send_info.seq=get_true_random_number();
  174. send_info.ack_seq=get_true_random_number();
  175. }
  176. }
  177. send_raw0(raw_info, 0, 0);
  178. conn_info.last_hb_sent_time = get_current_time();
  179. mylog(log_info, "(re)sent tcp syn\n");
  180. return 0;
  181. }
  182. else
  183. {
  184. return 0;
  185. }
  186. return 0;
  187. }
  188. else if(conn_info.state.client_current_state==client_tcp_handshake_dummy)
  189. {
  190. assert(raw_mode==mode_faketcp);
  191. if (get_current_time() - conn_info.last_state_time > client_handshake_timeout)
  192. {
  193. conn_info.state.client_current_state = client_idle;
  194. mylog(log_info, "state back to client_idle from client_tcp_handshake_dummy\n");
  195. return 0;
  196. }
  197. }
  198. else if(conn_info.state.client_current_state==client_handshake1)//send and resend handshake1
  199. {
  200. if(get_current_time()-conn_info.last_state_time>client_handshake_timeout)
  201. {
  202. conn_info.state.client_current_state=client_idle;
  203. mylog(log_info,"state back to client_idle from client_handshake1\n");
  204. return 0;
  205. }
  206. else if(get_current_time()-conn_info.last_hb_sent_time>client_retry_interval)
  207. {
  208. if(raw_mode==mode_faketcp)
  209. {
  210. if(conn_info.last_hb_sent_time==0)
  211. {
  212. send_info.seq++;
  213. send_info.ack_seq=recv_info.seq+1;
  214. send_info.ts_ack=recv_info.ts;
  215. raw_info.reserved_send_seq=send_info.seq;
  216. }
  217. send_info.seq=raw_info.reserved_send_seq;
  218. send_info.psh = 0;
  219. send_info.syn = 0;
  220. send_info.ack = 1;
  221. if(!use_tcp_dummy_socket)
  222. send_raw0(raw_info, 0, 0);
  223. send_handshake(raw_info,conn_info.my_id,0,const_id);
  224. send_info.seq+=raw_info.send_info.data_len;
  225. }
  226. else
  227. {
  228. send_handshake(raw_info,conn_info.my_id,0,const_id);
  229. if(raw_mode==mode_icmp)
  230. send_info.my_icmp_seq++;
  231. }
  232. conn_info.last_hb_sent_time=get_current_time();
  233. mylog(log_info,"(re)sent handshake1\n");
  234. return 0;
  235. }
  236. else
  237. {
  238. return 0;
  239. }
  240. return 0;
  241. }
  242. else if(conn_info.state.client_current_state==client_handshake2)
  243. {
  244. if(get_current_time()-conn_info.last_state_time>client_handshake_timeout)
  245. {
  246. conn_info.state.client_current_state=client_idle;
  247. mylog(log_info,"state back to client_idle from client_handshake2\n");
  248. return 0;
  249. }
  250. else if(get_current_time()-conn_info.last_hb_sent_time>client_retry_interval)
  251. {
  252. if(raw_mode==mode_faketcp)
  253. {
  254. if(conn_info.last_hb_sent_time==0)
  255. {
  256. send_info.ack_seq=recv_info.seq+raw_info.recv_info.data_len;
  257. send_info.ts_ack=recv_info.ts;
  258. raw_info.reserved_send_seq=send_info.seq;
  259. }
  260. send_info.seq=raw_info.reserved_send_seq;
  261. send_handshake(raw_info,conn_info.my_id,conn_info.oppsite_id,const_id);
  262. send_info.seq+=raw_info.send_info.data_len;
  263. }
  264. else
  265. {
  266. send_handshake(raw_info,conn_info.my_id,conn_info.oppsite_id,const_id);
  267. if(raw_mode==mode_icmp)
  268. send_info.my_icmp_seq++;
  269. }
  270. conn_info.last_hb_sent_time=get_current_time();
  271. mylog(log_info,"(re)sent handshake2\n");
  272. return 0;
  273. }
  274. else
  275. {
  276. return 0;
  277. }
  278. return 0;
  279. }
  280. else if(conn_info.state.client_current_state==client_ready)
  281. {
  282. fail_time_counter=0;
  283. mylog(log_trace,"time %llu,%llu\n",get_current_time(),conn_info.last_state_time);
  284. if(get_current_time()-conn_info.last_hb_recv_time>client_conn_timeout)
  285. {
  286. conn_info.state.client_current_state=client_idle;
  287. conn_info.my_id=get_true_random_number_nz();
  288. mylog(log_info,"state back to client_idle from client_ready bc of server-->client direction timeout\n");
  289. return 0;
  290. }
  291. if(get_current_time()- conn_info.last_oppsite_roller_time>client_conn_uplink_timeout)
  292. {
  293. conn_info.state.client_current_state=client_idle;
  294. conn_info.my_id=get_true_random_number_nz();
  295. mylog(log_info,"state back to client_idle from client_ready bc of client-->server direction timeout\n");
  296. }
  297. if(get_current_time()-conn_info.last_hb_sent_time<heartbeat_interval)
  298. {
  299. return 0;
  300. }
  301. mylog(log_debug,"heartbeat sent <%x,%x>\n",conn_info.oppsite_id,conn_info.my_id);
  302. if(hb_mode==0)
  303. send_safer(conn_info,'h',hb_buf,0);/////////////send
  304. else
  305. send_safer(conn_info,'h',hb_buf,hb_len);
  306. conn_info.last_hb_sent_time=get_current_time();
  307. return 0;
  308. }
  309. else
  310. {
  311. mylog(log_fatal,"unknown state,this shouldnt happen.\n");
  312. myexit(-1);
  313. }
  314. return 0;
  315. }
  316. int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a packet.
  317. {
  318. char* data;int data_len;
  319. packet_info_t &send_info=conn_info.raw_info.send_info;
  320. packet_info_t &recv_info=conn_info.raw_info.recv_info;
  321. raw_info_t &raw_info=conn_info.raw_info;
  322. mylog(log_trace,"<client_on_raw_recv,send_info.ts_ack= %u>\n",send_info.ts_ack);
  323. if(conn_info.state.client_current_state==client_idle )
  324. {
  325. g_packet_buf_cnt--;
  326. //recv(raw_recv_fd, 0,0, 0 );
  327. //pthread_mutex_lock(&queue_mutex);
  328. //my_queue.pop_front();
  329. //pthread_mutex_unlock(&queue_mutex);
  330. }
  331. else if(conn_info.state.client_current_state==client_tcp_handshake||conn_info.state.client_current_state==client_tcp_handshake_dummy)//received syn ack
  332. {
  333. assert(raw_mode==mode_faketcp);
  334. if(recv_raw0(raw_info,data,data_len)<0)
  335. {
  336. return -1;
  337. }
  338. if(recv_info.src_ip!=send_info.dst_ip||recv_info.src_port!=send_info.dst_port)
  339. {
  340. mylog(log_debug,"unexpected adress %x %x %d %d\n",recv_info.src_ip,send_info.dst_ip,recv_info.src_port,send_info.dst_port);
  341. return -1;
  342. }
  343. if(data_len==0&&raw_info.recv_info.syn==1&&raw_info.recv_info.ack==1)
  344. {
  345. if(conn_info.state.client_current_state==client_tcp_handshake)
  346. {
  347. if(recv_info.ack_seq!=send_info.seq+1)
  348. {
  349. mylog(log_debug,"seq ack_seq mis match\n");
  350. return -1;
  351. }
  352. mylog(log_info,"state changed from client_tcp_handshake to client_handshake1\n");
  353. }
  354. else
  355. {
  356. send_info.seq=recv_info.ack_seq-1;
  357. mylog(log_info,"state changed from client_tcp_dummy to client_handshake1\n");
  358. //send_info.ack_seq=recv_info.seq+1;
  359. }
  360. conn_info.state.client_current_state = client_handshake1;
  361. conn_info.last_state_time = get_current_time();
  362. conn_info.last_hb_sent_time=0;
  363. client_on_timer(conn_info);
  364. return 0;
  365. }
  366. else
  367. {
  368. mylog(log_debug,"unexpected packet type,expected:syn ack\n");
  369. return -1;
  370. }
  371. }
  372. else if(conn_info.state.client_current_state==client_handshake1)//recevied respond of handshake1
  373. {
  374. if(recv_bare(raw_info,data,data_len)!=0)
  375. {
  376. mylog(log_debug,"recv_bare failed!\n");
  377. return -1;
  378. }
  379. if(recv_info.src_ip!=send_info.dst_ip||recv_info.src_port!=send_info.dst_port)
  380. {
  381. mylog(log_debug,"unexpected adress %x %x %d %d\n",recv_info.src_ip,send_info.dst_ip,recv_info.src_port,send_info.dst_port);
  382. return -1;
  383. }
  384. if(data_len<int( 3*sizeof(my_id_t)))
  385. {
  386. mylog(log_debug,"too short to be a handshake\n");
  387. return -1;
  388. }
  389. my_id_t tmp_oppsite_id;
  390. memcpy(&tmp_oppsite_id,&data[0],sizeof(tmp_oppsite_id));
  391. tmp_oppsite_id=ntohl(tmp_oppsite_id);
  392. my_id_t tmp_my_id;
  393. memcpy(&tmp_my_id,&data[sizeof(my_id_t)],sizeof(tmp_my_id));
  394. tmp_my_id=ntohl(tmp_my_id);
  395. my_id_t tmp_oppsite_const_id;
  396. memcpy(&tmp_oppsite_const_id,&data[sizeof(my_id_t)*2],sizeof(tmp_oppsite_const_id));
  397. tmp_oppsite_const_id=ntohl(tmp_oppsite_const_id);
  398. if(tmp_my_id!=conn_info.my_id)
  399. {
  400. mylog(log_debug,"tmp_my_id doesnt match\n");
  401. return -1;
  402. }
  403. if(raw_mode==mode_faketcp)
  404. {
  405. if(recv_info.ack_seq!=send_info.seq)
  406. {
  407. mylog(log_debug,"seq ack_seq mis match\n");
  408. return -1;
  409. }
  410. if(recv_info.seq!=send_info.ack_seq)
  411. {
  412. mylog(log_debug,"seq ack_seq mis match\n");
  413. return -1;
  414. }
  415. }
  416. conn_info.oppsite_id=tmp_oppsite_id;
  417. 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);
  418. conn_info.state.client_current_state = client_handshake2;
  419. conn_info.last_state_time = get_current_time();
  420. conn_info.last_hb_sent_time=0;
  421. client_on_timer(conn_info);
  422. return 0;
  423. }
  424. else if(conn_info.state.client_current_state==client_handshake2||conn_info.state.client_current_state==client_ready)//received heartbeat or data
  425. {
  426. char type;
  427. if(recv_safer(conn_info,type,data,data_len)!=0)
  428. {
  429. mylog(log_debug,"recv_safer failed!\n");
  430. return -1;
  431. }
  432. if(recv_info.src_ip!=send_info.dst_ip||recv_info.src_port!=send_info.dst_port)
  433. {
  434. mylog(log_warn,"unexpected adress %x %x %d %d,this shouldnt happen.\n",recv_info.src_ip,send_info.dst_ip,recv_info.src_port,send_info.dst_port);
  435. return -1;
  436. }
  437. if(conn_info.state.client_current_state==client_handshake2)
  438. {
  439. mylog(log_info,"changed state from to client_handshake2 to client_ready\n");
  440. conn_info.state.client_current_state=client_ready;
  441. conn_info.last_hb_sent_time=0;
  442. conn_info.last_hb_recv_time=get_current_time();
  443. conn_info.last_oppsite_roller_time=conn_info.last_hb_recv_time;
  444. client_on_timer(conn_info);
  445. }
  446. if(data_len>=0&&type=='h')
  447. {
  448. mylog(log_debug,"[hb]heart beat received,oppsite_roller=%d\n",int(conn_info.oppsite_roller));
  449. conn_info.last_hb_recv_time=get_current_time();
  450. return 0;
  451. }
  452. else if(data_len>= int( sizeof(u32_t))&&type=='d')
  453. {
  454. mylog(log_trace,"received a data from fake tcp,len:%d\n",data_len);
  455. if(hb_mode==0)
  456. conn_info.last_hb_recv_time=get_current_time();
  457. u32_t tmp_conv_id;
  458. memcpy(&tmp_conv_id,&data[0],sizeof(tmp_conv_id));
  459. tmp_conv_id=ntohl(tmp_conv_id);
  460. if(!conn_info.blob->conv_manager.is_conv_used(tmp_conv_id))
  461. {
  462. mylog(log_info,"unknow conv %d,ignore\n",tmp_conv_id);
  463. return 0;
  464. }
  465. conn_info.blob->conv_manager.update_active_time(tmp_conv_id);
  466. u64_t u64=conn_info.blob->conv_manager.find_u64_by_conv(tmp_conv_id);
  467. sockaddr_in tmp_sockaddr={0};
  468. tmp_sockaddr.sin_family = AF_INET;
  469. tmp_sockaddr.sin_addr.s_addr=(u64>>32u);
  470. tmp_sockaddr.sin_port= htons(uint16_t((u64<<32u)>>32u));
  471. int ret=sendto(udp_fd,data+sizeof(u32_t),data_len -(sizeof(u32_t)),0,(struct sockaddr *)&tmp_sockaddr,sizeof(tmp_sockaddr));
  472. if(ret<0)
  473. {
  474. mylog(log_warn,"sento returned %d\n",ret);
  475. }
  476. mylog(log_trace,"%s :%d\n",inet_ntoa(tmp_sockaddr.sin_addr),ntohs(tmp_sockaddr.sin_port));
  477. mylog(log_trace,"%d byte sent\n",ret);
  478. }
  479. else
  480. {
  481. mylog(log_warn,"unknown packet,this shouldnt happen.\n");
  482. return -1;
  483. }
  484. return 0;
  485. }
  486. else
  487. {
  488. mylog(log_fatal,"unknown state,this shouldnt happen.\n");
  489. myexit(-1);
  490. }
  491. return 0;
  492. }
  493. void udp_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
  494. {
  495. char buf[buf_len];
  496. conn_info_t & conn_info= *((conn_info_t*)watcher->data);;
  497. int recv_len;
  498. struct sockaddr_in udp_new_addr_in={0};
  499. socklen_t udp_new_addr_len = sizeof(sockaddr_in);
  500. if ((recv_len = recvfrom(udp_fd, buf, max_data_len+1, 0,
  501. (struct sockaddr *) &udp_new_addr_in, &udp_new_addr_len)) == -1) {
  502. mylog(log_error,"recv_from error,this shouldnt happen at client\n");
  503. myexit(1);
  504. };
  505. if(recv_len==max_data_len+1)
  506. {
  507. mylog(log_warn,"huge packet, data_len > %d,dropped\n",max_data_len);
  508. return;
  509. }
  510. if(recv_len>=mtu_warn)
  511. {
  512. 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);
  513. }
  514. mylog(log_trace,"Received packet from %s:%d,len: %d\n", inet_ntoa(udp_new_addr_in.sin_addr),
  515. ntohs(udp_new_addr_in.sin_port),recv_len);
  516. u64_t u64=((u64_t(udp_new_addr_in.sin_addr.s_addr))<<32u)+ntohs(udp_new_addr_in.sin_port);
  517. u32_t conv;
  518. if(!conn_info.blob->conv_manager.is_u64_used(u64))
  519. {
  520. if(conn_info.blob->conv_manager.get_size() >=max_conv_num)
  521. {
  522. mylog(log_warn,"ignored new udp connect bc max_conv_num exceed\n");
  523. return;
  524. }
  525. conv=conn_info.blob->conv_manager.get_new_conv();
  526. conn_info.blob->conv_manager.insert_conv(conv,u64);
  527. mylog(log_info,"new packet from %s:%d,conv_id=%x\n",inet_ntoa(udp_new_addr_in.sin_addr),ntohs(udp_new_addr_in.sin_port),conv);
  528. }
  529. else
  530. {
  531. conv=conn_info.blob->conv_manager.find_conv_by_u64(u64);
  532. }
  533. conn_info.blob->conv_manager.update_active_time(conv);
  534. if(conn_info.state.client_current_state==client_ready)
  535. {
  536. send_data_safer(conn_info,buf,recv_len,conv);
  537. }
  538. }
  539. void raw_recv_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
  540. {
  541. assert(0==1);
  542. conn_info_t & conn_info= *((conn_info_t*)watcher->data);
  543. client_on_raw_recv(conn_info);
  544. }
  545. void async_cb(struct ev_loop *loop, struct ev_async *watcher, int revents)
  546. {
  547. conn_info_t & conn_info= *((conn_info_t*)watcher->data);
  548. if(send_with_pcap&&!pcap_header_captured)
  549. {
  550. int empty=0;char *p;int len;
  551. pthread_mutex_lock(&queue_mutex);
  552. empty=my_queue.empty();
  553. if(!empty)
  554. {
  555. my_queue.peek_front(p,len);
  556. my_queue.pop_front();
  557. }
  558. pthread_mutex_unlock(&queue_mutex);
  559. if(empty) return;
  560. pcap_header_captured=1;
  561. assert(pcap_link_header_len!=-1);
  562. memcpy(pcap_header_buf,p,pcap_link_header_len);
  563. log_bare(log_info,"link level header captured:\n");
  564. for(int i=0;i<pcap_link_header_len;i++)
  565. log_bare(log_info,"<%x>",(u32_t)(unsigned char)pcap_header_buf[i]);
  566. log_bare(log_info,"\n");
  567. return ;
  568. }
  569. //mylog(log_info,"async_cb called\n");
  570. while(1)
  571. {
  572. int empty=0;char *p;int len;
  573. pthread_mutex_lock(&queue_mutex);
  574. empty=my_queue.empty();
  575. if(!empty)
  576. {
  577. my_queue.peek_front(p,len);
  578. my_queue.pop_front();
  579. }
  580. pthread_mutex_unlock(&queue_mutex);
  581. if(empty) break;
  582. int new_len=len-pcap_link_header_len;
  583. memcpy(g_packet_buf,p+pcap_link_header_len,new_len);
  584. g_packet_buf_len=new_len;
  585. assert(g_packet_buf_cnt==0);
  586. g_packet_buf_cnt++;
  587. client_on_raw_recv(conn_info);
  588. }
  589. }
  590. void clear_timer_cb(struct ev_loop *loop, struct ev_timer *watcher, int revents)
  591. {
  592. conn_info_t & conn_info= *((conn_info_t*)watcher->data);
  593. //u64_t value;
  594. //read(timer_fd, &value, 8);
  595. client_on_timer(conn_info);
  596. mylog(log_trace,"epoll_trigger_counter: %d \n",epoll_trigger_counter);
  597. epoll_trigger_counter=0;
  598. }
  599. void fifo_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
  600. {
  601. conn_info_t & conn_info= *((conn_info_t*)watcher->data);
  602. char buf[buf_len];
  603. int fifo_fd=watcher->fd;
  604. int len=read (fifo_fd, buf, sizeof (buf));
  605. if(len<0)
  606. {
  607. mylog(log_warn,"fifo read failed len=%d,errno=%s\n",len,strerror(errno));
  608. return;
  609. }
  610. buf[len]=0;
  611. while(len>=1&&buf[len-1]=='\n')
  612. buf[len-1]=0;
  613. mylog(log_info,"got data from fifo,len=%d,s=[%s]\n",len,buf);
  614. if(strcmp(buf,"reconnect")==0)
  615. {
  616. mylog(log_info,"received command: reconnect\n");
  617. conn_info.state.client_current_state=client_idle;
  618. conn_info.my_id=get_true_random_number_nz();
  619. }
  620. else
  621. {
  622. mylog(log_info,"unknown command\n");
  623. }
  624. }
  625. int client_event_loop()
  626. {
  627. char buf[buf_len];
  628. conn_info_t conn_info;
  629. conn_info.my_id=get_true_random_number_nz();
  630. conn_info.prepare();
  631. packet_info_t &send_info=conn_info.raw_info.send_info;
  632. packet_info_t &recv_info=conn_info.raw_info.recv_info;
  633. if(source_ip_uint32==0)
  634. {
  635. mylog(log_info,"get_src_adress called\n");
  636. if(retry_on_error==0)
  637. {
  638. if(get_src_adress(source_ip_uint32,remote_ip_uint32,remote_port)!=0)
  639. {
  640. mylog(log_fatal,"the trick to auto get source ip failed, maybe you dont have internet access\n");
  641. myexit(-1);
  642. }
  643. }
  644. else
  645. {
  646. int ok=0;
  647. while(!ok)
  648. {
  649. if(get_src_adress(source_ip_uint32,remote_ip_uint32,remote_port)!=0)
  650. {
  651. mylog(log_warn,"the trick to auto get source ip failed, maybe you dont have internet access, retry in %d seconds\n",retry_on_error_interval);
  652. sleep(retry_on_error_interval);
  653. }
  654. else
  655. {
  656. ok=1;
  657. }
  658. }
  659. }
  660. }
  661. in_addr tmp;
  662. tmp.s_addr=source_ip_uint32;
  663. mylog(log_info,"source ip = %s\n",inet_ntoa(tmp));
  664. //printf("done\n");
  665. if(try_to_list_and_bind(bind_fd,local_ip_uint32,source_port)!=0)
  666. {
  667. mylog(log_fatal,"bind to source_port:%d fail\n ",source_port);
  668. myexit(-1);
  669. }
  670. send_info.src_port=source_port;
  671. send_info.src_ip = source_ip_uint32;
  672. int i, j, k;int ret;
  673. send_info.dst_ip=remote_ip_uint32;
  674. send_info.dst_port=remote_port;
  675. udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  676. set_buf_size(udp_fd,socket_buf_size,force_socket_buf);
  677. int yes = 1;
  678. struct sockaddr_in local_me={0};
  679. socklen_t slen = sizeof(sockaddr_in);
  680. local_me.sin_family = AF_INET;
  681. local_me.sin_port = htons(local_port);
  682. local_me.sin_addr.s_addr = local_ip_uint32;
  683. if (::bind(udp_fd, (struct sockaddr*) &local_me, slen) == -1) {
  684. mylog(log_fatal,"socket bind error\n");
  685. myexit(1);
  686. }
  687. setnonblocking(udp_fd);
  688. //epollfd = epoll_create1(0);
  689. //const int max_events = 4096;
  690. //struct epoll_event ev, events[max_events];
  691. //if (epollfd < 0) {
  692. // mylog(log_fatal,"epoll return %d\n", epollfd);
  693. // myexit(-1);
  694. //}
  695. struct ev_loop * loop= ev_default_loop(0);
  696. assert(loop != NULL);
  697. //ev.events = EPOLLIN;
  698. //ev.data.u64 = udp_fd;
  699. //ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, udp_fd, &ev);
  700. //if (ret!=0) {
  701. // mylog(log_fatal,"add udp_listen_fd error\n");
  702. // myexit(-1);
  703. //}
  704. struct ev_io udp_accept_watcher;
  705. udp_accept_watcher.data=&conn_info;
  706. ev_io_init(&udp_accept_watcher, udp_accept_cb, udp_fd, EV_READ);
  707. ev_io_start(loop, &udp_accept_watcher);
  708. //ev.events = EPOLLIN;
  709. //ev.data.u64 = raw_recv_fd;
  710. //ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, raw_recv_fd, &ev);
  711. //if (ret!= 0) {
  712. // mylog(log_fatal,"add raw_fd error\n");
  713. // myexit(-1);
  714. //}
  715. //struct ev_io raw_watcher;
  716. //raw_watcher.data=&conn_info;
  717. // ev_io_init(&raw_watcher, raw_recv_cb, raw_recv_fd, EV_READ);
  718. //ev_io_start(loop, &raw_watcher);
  719. g_default_loop=loop;
  720. async_watcher.data=&conn_info;
  721. ev_async_init(&async_watcher,async_cb);
  722. ev_async_start(loop,&async_watcher);
  723. init_raw_socket();
  724. int unbind=1;
  725. //set_timer(epollfd,timer_fd);
  726. struct ev_timer clear_timer;
  727. clear_timer.data=&conn_info;
  728. ev_timer_init(&clear_timer, clear_timer_cb, 0, timer_interval/1000.0);
  729. ev_timer_start(loop, &clear_timer);
  730. mylog(log_debug,"send_raw : from %x %d to %x %d\n",send_info.src_ip,send_info.src_port,send_info.dst_ip,send_info.dst_port);
  731. int fifo_fd=-1;
  732. struct ev_io fifo_watcher;
  733. fifo_watcher.data=&conn_info;
  734. if(fifo_file[0]!=0)
  735. {
  736. fifo_fd=create_fifo(fifo_file);
  737. //ev.events = EPOLLIN;
  738. //ev.data.u64 = fifo_fd;
  739. //ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, fifo_fd, &ev);
  740. //if (ret!= 0) {
  741. // mylog(log_fatal,"add fifo_fd to epoll error %s\n",strerror(errno));
  742. // myexit(-1);
  743. //}
  744. ev_io_init(&fifo_watcher, fifo_cb, fifo_fd, EV_READ);
  745. ev_io_start(loop, &fifo_watcher);
  746. mylog(log_info,"fifo_file=%s\n",fifo_file);
  747. }
  748. ev_run(loop, 0);
  749. /*
  750. while(1)////////////////////////
  751. {
  752. if(about_to_exit) myexit(0);
  753. epoll_trigger_counter++;
  754. int nfds = epoll_wait(epollfd, events, max_events, 180 * 1000);
  755. if (nfds < 0) { //allow zero
  756. if(errno==EINTR )
  757. {
  758. mylog(log_info,"epoll interrupted by signal,continue\n");
  759. }
  760. else
  761. {
  762. mylog(log_fatal,"epoll_wait return %d,%s\n", nfds,strerror(errno));
  763. myexit(-1);
  764. }
  765. }
  766. int idx;
  767. for (idx = 0; idx < nfds; ++idx) {
  768. if (events[idx].data.u64 == (u64_t)raw_recv_fd)
  769. {
  770. iphdr *iph;tcphdr *tcph;
  771. }
  772. else if(events[idx].data.u64 ==(u64_t)timer_fd)
  773. {
  774. }
  775. else if (events[idx].data.u64 == (u64_t)fifo_fd)
  776. {
  777. }
  778. else if (events[idx].data.u64 == (u64_t)udp_fd)
  779. {
  780. }
  781. else
  782. {
  783. mylog(log_fatal,"unknown fd,this should never happen\n");
  784. myexit(-1);
  785. }
  786. }
  787. }*/
  788. return 0;
  789. }
  790. void sigpipe_cb(struct ev_loop *l, ev_signal *w, int revents)
  791. {
  792. mylog(log_info, "got sigpipe, ignored");
  793. }
  794. void sigterm_cb(struct ev_loop *l, ev_signal *w, int revents)
  795. {
  796. mylog(log_info, "got sigterm, exit");
  797. myexit(0);
  798. }
  799. void sigint_cb(struct ev_loop *l, ev_signal *w, int revents)
  800. {
  801. mylog(log_info, "got sigint, exit");
  802. myexit(0);
  803. }
  804. int main(int argc, char *argv[])
  805. {
  806. init_pcap();
  807. int i=0;
  808. char errbuf[PCAP_ERRBUF_SIZE];
  809. pcap_if_t *interfaces,*d;
  810. if(pcap_findalldevs(&interfaces,errbuf)==-1)
  811. {
  812. printf("\nerror in pcap findall devs");
  813. return -1;
  814. }
  815. for(pcap_if_t *d=interfaces; d!=NULL; d=d->next) {
  816. printf("%s:", d->name);
  817. for(pcap_addr_t *a=d->addresses; a!=NULL; a=a->next) {
  818. if(a->addr->sa_family == AF_INET)
  819. printf(" %s", inet_ntoa(((struct sockaddr_in*)a->addr)->sin_addr));
  820. }
  821. printf("\n");
  822. }
  823. //libnet_t *l; /* the libnet context */
  824. //char errbuf[LIBNET_ERRBUF_SIZE];
  825. //l = libnet_init(LIBNET_RAW4, NULL, errbuf);
  826. dup2(1, 2);//redirect stderr to stdout
  827. //signal(SIGINT, signal_handler);
  828. //signal(SIGHUP, signal_handler);
  829. //signal(SIGKILL, signal_handler);
  830. //signal(SIGTERM, signal_handler);
  831. //signal(SIGQUIT, signal_handler);
  832. struct ev_loop* loop=ev_default_loop(0);
  833. ev_signal signal_watcher_sigpipe;
  834. ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE);
  835. ev_signal_start(loop, &signal_watcher_sigpipe);
  836. ev_signal signal_watcher_sigterm;
  837. ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM);
  838. ev_signal_start(loop, &signal_watcher_sigterm);
  839. ev_signal signal_watcher_sigint;
  840. ev_signal_init(&signal_watcher_sigint, sigint_cb, SIGINT);
  841. ev_signal_start(loop, &signal_watcher_sigint);
  842. pre_process_arg(argc,argv);
  843. if(geteuid() != 0)
  844. {
  845. mylog(log_warn,"root check failed, it seems like you are using a non-root account. we can try to continue, but it may fail. If you want to run udp2raw as non-root, you have to add iptables rule manually, and grant udp2raw CAP_NET_RAW capability, check README.md in repo for more info.\n");
  846. }
  847. else
  848. {
  849. mylog(log_warn,"you can run udp2raw with non-root account for better security. check README.md in repo for more info.\n");
  850. }
  851. local_ip_uint32=inet_addr(local_ip);
  852. source_ip_uint32=inet_addr(source_ip);
  853. strcpy(remote_ip,remote_address);
  854. mylog(log_info,"remote_ip=[%s], make sure this is a vaild IP address\n",remote_ip);
  855. remote_ip_uint32=inet_addr(remote_ip);
  856. init_random_number_fd();
  857. srand(get_true_random_number_nz());
  858. const_id=get_true_random_number_nz();
  859. mylog(log_info,"const_id:%x\n",const_id);
  860. char tmp[1000]="";
  861. strcat(tmp,key_string);
  862. strcat(tmp,"key1");
  863. md5((uint8_t*)tmp,strlen(tmp),(uint8_t*)key);
  864. iptables_rule();
  865. if(program_mode==client_mode)
  866. {
  867. client_event_loop();
  868. }
  869. else
  870. {
  871. mylog(log_fatal,"server mode not supported in portable version\n");
  872. myexit(-1);
  873. //server_event_loop();
  874. }
  875. return 0;
  876. }