client.cpp 34 KB

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