common.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. * comm.cpp
  3. *
  4. * Created on: Jul 29, 2017
  5. * Author: wangyu
  6. */
  7. #include "common.h"
  8. #include "log.h"
  9. int about_to_exit=0;
  10. raw_mode_t raw_mode=mode_faketcp;
  11. unordered_map<int, const char*> raw_mode_tostring = {{mode_faketcp, "faketcp"}, {mode_udp, "udp"}, {mode_icmp, "icmp"}};
  12. int socket_buf_size=1024*1024;
  13. static int random_number_fd=-1;
  14. string iptables_pattern="";
  15. int iptables_rule_added=0;
  16. int iptables_rule_keeped=0;
  17. int iptables_rule_keep_index=0;
  18. //int iptables_rule_no_clear=0;
  19. program_mode_t program_mode=unset_mode;//0 unset; 1client 2server
  20. u64_t get_current_time()
  21. {
  22. timespec tmp_time;
  23. clock_gettime(CLOCK_MONOTONIC, &tmp_time);
  24. return tmp_time.tv_sec*1000+tmp_time.tv_nsec/(1000*1000l);
  25. }
  26. u64_t pack_u64(u32_t a,u32_t b)
  27. {
  28. u64_t ret=a;
  29. ret<<=32u;
  30. ret+=b;
  31. return ret;
  32. }
  33. u32_t get_u64_h(u64_t a)
  34. {
  35. return a>>32u;
  36. }
  37. u32_t get_u64_l(u64_t a)
  38. {
  39. return (a<<32u)>>32u;
  40. }
  41. char * my_ntoa(u32_t ip)
  42. {
  43. in_addr a;
  44. a.s_addr=ip;
  45. return inet_ntoa(a);
  46. }
  47. /*
  48. int add_iptables_rule(const char * s)
  49. {
  50. iptables_pattern=s;
  51. string rule="iptables -I INPUT ";
  52. rule+=iptables_pattern;
  53. rule+=" -j DROP";
  54. char *output;
  55. if(run_command(rule.c_str(),output)==0)
  56. {
  57. mylog(log_warn,"auto added iptables rule by: %s\n",rule.c_str());
  58. }
  59. else
  60. {
  61. mylog(log_fatal,"auto added iptables failed by: %s\n",rule.c_str());
  62. //mylog(log_fatal,"reason : %s\n",strerror(errno));
  63. myexit(-1);
  64. }
  65. iptables_rule_added=1;
  66. return 0;
  67. }*/
  68. string chain[2];
  69. string rule_keep[2];
  70. string rule_keep_add[2];
  71. string rule_keep_del[2];
  72. u64_t keep_rule_last_time=0;
  73. pthread_t keep_thread;
  74. int keep_thread_running=0;
  75. int iptables_gen_add(const char * s,u32_t const_id)
  76. {
  77. string dummy="";
  78. iptables_pattern=s;
  79. chain[0] =dummy+ "udp2rawDwrW_C";
  80. rule_keep[0]=dummy+ iptables_pattern+" -j " +chain[0];
  81. rule_keep_add[0]=dummy+"iptables -I INPUT "+rule_keep[0];
  82. char *output;
  83. run_command(dummy+"iptables -N "+chain[0],output,show_none);
  84. run_command(dummy+"iptables -F "+chain[0],output);
  85. run_command(dummy+"iptables -I "+chain[0] + " -j DROP",output);
  86. rule_keep_del[0]=dummy+"iptables -D INPUT "+rule_keep[0];
  87. run_command(rule_keep_del[0],output,show_none);
  88. run_command(rule_keep_del[0],output,show_none);
  89. if(run_command(rule_keep_add[0],output)!=0)
  90. {
  91. mylog(log_fatal,"auto added iptables failed by: %s\n",rule_keep_add[0].c_str());
  92. myexit(-1);
  93. }
  94. return 0;
  95. }
  96. int iptables_rule_init(const char * s,u32_t const_id,int keep)
  97. {
  98. iptables_pattern=s;
  99. iptables_rule_added=1;
  100. iptables_rule_keeped=keep;
  101. string dummy="";
  102. char const_id_str[100];
  103. sprintf(const_id_str, "%x", const_id);
  104. chain[0] =dummy+ "udp2rawDwrW_"+const_id_str+"_C0";
  105. chain[1] =dummy+ "udp2rawDwrW_"+const_id_str+"_C1";
  106. rule_keep[0]=dummy+ iptables_pattern+" -j " +chain[0];
  107. rule_keep[1]=dummy+ iptables_pattern+" -j " +chain[1];
  108. rule_keep_add[0]=dummy+"iptables -I INPUT "+rule_keep[0];
  109. rule_keep_add[1]=dummy+"iptables -I INPUT "+rule_keep[1];
  110. rule_keep_del[0]=dummy+"iptables -D INPUT "+rule_keep[0];
  111. rule_keep_del[1]=dummy+"iptables -D INPUT "+rule_keep[1];
  112. keep_rule_last_time=get_current_time();
  113. char *output;
  114. for(int i=0;i<=iptables_rule_keeped;i++)
  115. {
  116. run_command(dummy+"iptables -N "+chain[i],output);
  117. run_command(dummy+"iptables -F "+chain[i],output);
  118. run_command(dummy+"iptables -I "+chain[i] + " -j DROP",output);
  119. if(run_command(rule_keep_add[i],output)!=0)
  120. {
  121. mylog(log_fatal,"auto added iptables failed by: %s\n",rule_keep_add[i].c_str());
  122. myexit(-1);
  123. }
  124. }
  125. mylog(log_warn,"auto added iptables rules\n");
  126. return 0;
  127. }
  128. int keep_iptables_rule() //magic to work on a machine without grep/iptables --check/-m commment
  129. {
  130. /*
  131. if(iptables_rule_keeped==0) return 0;
  132. uint64_t tmp_current_time=get_current_time();
  133. if(tmp_current_time-keep_rule_last_time<=iptables_rule_keep_interval)
  134. {
  135. return 0;
  136. }
  137. else
  138. {
  139. keep_rule_last_time=tmp_current_time;
  140. }*/
  141. mylog(log_debug,"keep_iptables_rule begin %llu\n",get_current_time());
  142. iptables_rule_keep_index+=1;
  143. iptables_rule_keep_index%=2;
  144. string dummy="";
  145. char *output;
  146. int i=iptables_rule_keep_index;
  147. run_command(dummy + "iptables -N " + chain[i], output,show_none);
  148. if (run_command(dummy + "iptables -F " + chain[i], output,show_none) != 0)
  149. mylog(log_warn, "iptables -F failed %d\n",i);
  150. if (run_command(dummy + "iptables -I " + chain[i] + " -j DROP",output,show_none) != 0)
  151. mylog(log_warn, "iptables -I failed %d\n",i);
  152. if (run_command(rule_keep_del[i], output,show_none) != 0)
  153. mylog(log_warn, "rule_keep_del failed %d\n",i);
  154. run_command(rule_keep_del[i], output,show_none); //do it twice,incase it fails for unknown random reason
  155. if(run_command(rule_keep_add[i], output,show_log)!=0)
  156. mylog(log_warn, "rule_keep_del failed %d\n",i);
  157. mylog(log_debug,"keep_iptables_rule end %llu\n",get_current_time());
  158. return 0;
  159. }
  160. int clear_iptables_rule()
  161. {
  162. char *output;
  163. string dummy="";
  164. if(!iptables_rule_added) return 0;
  165. for(int i=0;i<=iptables_rule_keeped;i++ )
  166. {
  167. run_command(rule_keep_del[i],output);
  168. run_command(dummy+"iptables -F "+chain[i],output);
  169. run_command(dummy+"iptables -X "+chain[i],output);
  170. }
  171. return 0;
  172. }
  173. void init_random_number_fd()
  174. {
  175. random_number_fd=open("/dev/urandom",O_RDONLY);
  176. if(random_number_fd==-1)
  177. {
  178. mylog(log_fatal,"error open /dev/urandom\n");
  179. myexit(-1);
  180. }
  181. setnonblocking(random_number_fd);
  182. }
  183. u64_t get_true_random_number_64()
  184. {
  185. u64_t ret;
  186. int size=read(random_number_fd,&ret,sizeof(ret));
  187. if(size!=sizeof(ret))
  188. {
  189. mylog(log_fatal,"get random number failed %d\n",size);
  190. myexit(-1);
  191. }
  192. return ret;
  193. }
  194. u32_t get_true_random_number()
  195. {
  196. u32_t ret;
  197. int size=read(random_number_fd,&ret,sizeof(ret));
  198. if(size!=sizeof(ret))
  199. {
  200. mylog(log_fatal,"get random number failed %d\n",size);
  201. myexit(-1);
  202. }
  203. return ret;
  204. }
  205. u32_t get_true_random_number_nz() //nz for non-zero
  206. {
  207. u32_t ret=0;
  208. while(ret==0)
  209. {
  210. ret=get_true_random_number();
  211. }
  212. return ret;
  213. }
  214. u64_t ntoh64(u64_t a)
  215. {
  216. if(__BYTE_ORDER == __LITTLE_ENDIAN)
  217. {
  218. return bswap_64( a);
  219. }
  220. else return a;
  221. }
  222. u64_t hton64(u64_t a)
  223. {
  224. if(__BYTE_ORDER == __LITTLE_ENDIAN)
  225. {
  226. return bswap_64( a);
  227. }
  228. else return a;
  229. }
  230. void setnonblocking(int sock) {
  231. int opts;
  232. opts = fcntl(sock, F_GETFL);
  233. if (opts < 0) {
  234. mylog(log_fatal,"fcntl(sock,GETFL)\n");
  235. //perror("fcntl(sock,GETFL)");
  236. myexit(1);
  237. }
  238. opts = opts | O_NONBLOCK;
  239. if (fcntl(sock, F_SETFL, opts) < 0) {
  240. mylog(log_fatal,"fcntl(sock,SETFL,opts)\n");
  241. //perror("fcntl(sock,SETFL,opts)");
  242. myexit(1);
  243. }
  244. }
  245. /*
  246. Generic checksum calculation function
  247. */
  248. unsigned short csum(const unsigned short *ptr,int nbytes) {
  249. register long sum;
  250. unsigned short oddbyte;
  251. register short answer;
  252. sum=0;
  253. while(nbytes>1) {
  254. sum+=*ptr++;
  255. nbytes-=2;
  256. }
  257. if(nbytes==1) {
  258. oddbyte=0;
  259. *((u_char*)&oddbyte)=*(u_char*)ptr;
  260. sum+=oddbyte;
  261. }
  262. sum = (sum>>16)+(sum & 0xffff);
  263. sum = sum + (sum>>16);
  264. answer=(short)~sum;
  265. return(answer);
  266. }
  267. int set_buf_size(int fd)
  268. {
  269. if(setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  270. {
  271. mylog(log_fatal,"SO_SNDBUFFORCE fail,fd %d\n",fd);
  272. myexit(1);
  273. }
  274. if(setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  275. {
  276. mylog(log_fatal,"SO_RCVBUFFORCE fail,fd %d\n",fd);
  277. myexit(1);
  278. }
  279. return 0;
  280. }
  281. void myexit(int a)
  282. {
  283. if(enable_log_color)
  284. printf("%s\n",RESET);
  285. if(keep_thread_running)
  286. {
  287. if(pthread_cancel(keep_thread))
  288. {
  289. mylog(log_warn,"pthread_cancel failed\n");
  290. }
  291. else
  292. {
  293. mylog(log_info,"pthread_cancel success\n");
  294. }
  295. }
  296. clear_iptables_rule();
  297. exit(a);
  298. }
  299. void signal_handler(int sig)
  300. {
  301. about_to_exit=1;
  302. // myexit(0);
  303. }
  304. int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len)
  305. {
  306. static char buf[buf_len];
  307. data=buf;
  308. id_t tmp=htonl(id1);
  309. memcpy(buf,&tmp,sizeof(tmp));
  310. tmp=htonl(id2);
  311. memcpy(buf+sizeof(tmp),&tmp,sizeof(tmp));
  312. tmp=htonl(id3);
  313. memcpy(buf+sizeof(tmp)*2,&tmp,sizeof(tmp));
  314. len=sizeof(id_t)*3;
  315. return 0;
  316. }
  317. int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3)
  318. {
  319. if(len<int(sizeof(id_t)*3)) return -1;
  320. id1=ntohl( *((id_t*)(data+0)) );
  321. id2=ntohl( *((id_t*)(data+sizeof(id_t))) );
  322. id3=ntohl( *((id_t*)(data+sizeof(id_t)*2)) );
  323. return 0;
  324. }
  325. int hex_to_u32(const string & a,u32_t &output)
  326. {
  327. //string b="0x";
  328. //b+=a;
  329. if(sscanf(a.c_str(),"%x",&output)==1)
  330. {
  331. //printf("%s %x\n",a.c_str(),output);
  332. return 0;
  333. }
  334. mylog(log_error,"<%s> doesnt contain a hex\n",a.c_str());
  335. return -1;
  336. }
  337. int hex_to_u32_with_endian(const string & a,u32_t &output)
  338. {
  339. //string b="0x";
  340. //b+=a;
  341. if(sscanf(a.c_str(),"%x",&output)==1)
  342. {
  343. output=htonl(output);
  344. //printf("%s %x\n",a.c_str(),output);
  345. return 0;
  346. }
  347. mylog(log_error,"<%s> doesnt contain a hex\n",a.c_str());
  348. return -1;
  349. }
  350. bool larger_than_u32(u32_t a,u32_t b)
  351. {
  352. u32_t smaller,bigger;
  353. smaller=min(a,b);//smaller in normal sense
  354. bigger=max(a,b);
  355. u32_t distance=min(bigger-smaller,smaller+(0xffffffff-bigger+1));
  356. if(distance==bigger-smaller)
  357. {
  358. if(bigger==a)
  359. {
  360. return 1;
  361. }
  362. else
  363. {
  364. return 0;
  365. }
  366. }
  367. else
  368. {
  369. if(smaller==b)
  370. {
  371. return 0;
  372. }
  373. else
  374. {
  375. return 1;
  376. }
  377. }
  378. }
  379. bool larger_than_u16(uint16_t a,uint16_t b)
  380. {
  381. uint16_t smaller,bigger;
  382. smaller=min(a,b);//smaller in normal sense
  383. bigger=max(a,b);
  384. uint16_t distance=min(bigger-smaller,smaller+(0xffff-bigger+1));
  385. if(distance==bigger-smaller)
  386. {
  387. if(bigger==a)
  388. {
  389. return 1;
  390. }
  391. else
  392. {
  393. return 0;
  394. }
  395. }
  396. else
  397. {
  398. if(smaller==b)
  399. {
  400. return 0;
  401. }
  402. else
  403. {
  404. return 1;
  405. }
  406. }
  407. }
  408. vector<string> string_to_vec(const char * s,const char * sp) {
  409. vector<string> res;
  410. string str=s;
  411. char *p = strtok ((char *)str.c_str(),sp);
  412. while (p != NULL)
  413. {
  414. res.push_back(p);
  415. //printf ("%s\n",p);
  416. p = strtok(NULL, sp);
  417. }
  418. /* for(int i=0;i<(int)res.size();i++)
  419. {
  420. printf("<<%s>>\n",res[i].c_str());
  421. }*/
  422. return res;
  423. }
  424. vector< vector <string> > string_to_vec2(const char * s)
  425. {
  426. vector< vector <string> > res;
  427. vector<string> lines=string_to_vec(s,"\n");
  428. for(int i=0;i<int(lines.size());i++)
  429. {
  430. vector<string> tmp;
  431. tmp=string_to_vec(lines[i].c_str(),"\t ");
  432. res.push_back(tmp);
  433. }
  434. return res;
  435. }
  436. int read_file(const char * file,string &output)
  437. {
  438. const int max_len=3*1024*1024;
  439. // static char buf[max_len+100];
  440. string buf0;
  441. buf0.reserve(max_len+200);
  442. char * buf=(char *)buf0.c_str();
  443. buf[max_len]=0;
  444. //buf[sizeof(buf)-1]=0;
  445. int fd=open(file,O_RDONLY);
  446. if(fd==-1)
  447. {
  448. mylog(log_error,"read_file %s fail\n",file);
  449. return -1;
  450. }
  451. int len=read(fd,buf,max_len);
  452. if(len==max_len)
  453. {
  454. buf[0]=0;
  455. mylog(log_error,"%s too long,buf not large enough\n",file);
  456. return -2;
  457. }
  458. else if(len<0)
  459. {
  460. buf[0]=0;
  461. mylog(log_error,"%s read fail %d\n",file,len);
  462. return -3;
  463. }
  464. else
  465. {
  466. buf[len]=0;
  467. output=buf;
  468. }
  469. return 0;
  470. }
  471. int run_command(string command0,char * &output,int flag) {
  472. FILE *in;
  473. if((flag&show_log)==0) command0+=" 2>&1 ";
  474. const char * command=command0.c_str();
  475. int level= (flag&show_log)?log_warn:log_debug;
  476. if(flag&show_command)
  477. {
  478. mylog(log_info,"run_command %s\n",command);
  479. }
  480. else
  481. {
  482. mylog(log_debug,"run_command %s\n",command);
  483. }
  484. static __thread char buf[1024*1024+100];
  485. buf[sizeof(buf)-1]=0;
  486. if(!(in = popen(command, "r"))){
  487. mylog(level,"command %s popen failed,errno %s\n",command,strerror(errno));
  488. return -1;
  489. }
  490. int len =fread(buf, 1024*1024, 1, in);
  491. if(len==1024*1024)
  492. {
  493. buf[0]=0;
  494. mylog(level,"too long,buf not larger enough\n");
  495. return -2;
  496. }
  497. else
  498. {
  499. buf[len]=0;
  500. }
  501. int ret;
  502. if(( ret=ferror(in) ))
  503. {
  504. mylog(level,"command %s fread failed,ferror return value %d \n",command,ret);
  505. return -3;
  506. }
  507. //if(output!=0)
  508. output=buf;
  509. ret= pclose(in);
  510. int ret2=WEXITSTATUS(ret);
  511. if(ret!=0||ret2!=0)
  512. {
  513. mylog(level,"commnad %s ,pclose returned %d ,WEXITSTATUS %d,errnor :%s \n",command,ret,ret2,strerror(errno));
  514. return -4;
  515. }
  516. return 0;
  517. }
  518. /*
  519. int run_command_no_log(string command0,char * &output) {
  520. FILE *in;
  521. command0+=" 2>&1 ";
  522. const char * command=command0.c_str();
  523. mylog(log_debug,"run_command_no_log %s\n",command);
  524. static char buf[1024*1024+100];
  525. buf[sizeof(buf)-1]=0;
  526. if(!(in = popen(command, "r"))){
  527. mylog(log_debug,"command %s popen failed,errno %s\n",command,strerror(errno));
  528. return -1;
  529. }
  530. int len =fread(buf, 1024*1024, 1, in);
  531. if(len==1024*1024)
  532. {
  533. buf[0]=0;
  534. mylog(log_debug,"too long,buf not larger enough\n");
  535. return -2;
  536. }
  537. else
  538. {
  539. buf[len]=0;
  540. }
  541. int ret;
  542. if(( ret=ferror(in) ))
  543. {
  544. mylog(log_debug,"command %s fread failed,ferror return value %d \n",command,ret);
  545. return -3;
  546. }
  547. //if(output!=0)
  548. output=buf;
  549. ret= pclose(in);
  550. int ret2=WEXITSTATUS(ret);
  551. if(ret!=0||ret2!=0)
  552. {
  553. mylog(log_debug,"commnad %s ,pclose returned %d ,WEXITSTATUS %d,errnor :%s \n",command,ret,ret2,strerror(errno));
  554. return -4;
  555. }
  556. return 0;
  557. }*/
  558. // Remove preceding and trailing characters
  559. string trim(const string& str, char c) {
  560. size_t first = str.find_first_not_of(c);
  561. if(string::npos==first)
  562. {
  563. return "";
  564. }
  565. size_t last = str.find_last_not_of(c);
  566. return str.substr(first,(last-first+1));
  567. }
  568. vector<string> parse_conf_line(const string& s0)
  569. {
  570. string s=s0;
  571. s.reserve(s.length()+200);
  572. char *buf=(char *)s.c_str();
  573. //char buf[s.length()+200];
  574. char *p=buf;
  575. int i=int(s.length())-1;
  576. int j;
  577. vector<string>res;
  578. strcpy(buf,(char *)s.c_str());
  579. while(i>=0)
  580. {
  581. if(buf[i]==' ' || buf[i]== '\t')
  582. buf[i]=0;
  583. else break;
  584. i--;
  585. }
  586. while(*p!=0)
  587. {
  588. if(*p==' ' || *p== '\t')
  589. {
  590. p++;
  591. }
  592. else break;
  593. }
  594. int new_len=strlen(p);
  595. if(new_len==0)return res;
  596. if(p[0]=='#') return res;
  597. if(p[0]!='-')
  598. {
  599. mylog(log_fatal,"line :<%s> not begin with '-' ",s.c_str());
  600. myexit(-1);
  601. }
  602. for(i=0;i<new_len;i++)
  603. {
  604. if(p[i]==' '||p[i]=='\t')
  605. {
  606. break;
  607. }
  608. }
  609. if(i==new_len)
  610. {
  611. res.push_back(p);
  612. return res;
  613. }
  614. j=i;
  615. while(p[j]==' '||p[j]=='\t')
  616. j++;
  617. p[i]=0;
  618. res.push_back(p);
  619. res.push_back(p+j);
  620. return res;
  621. }