1
0

common.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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) {//works both for big and little endian
  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. //TODO
  352. //looks like this can simply be done by return ((i32_t)(a-b) >0)
  353. {
  354. u32_t smaller,bigger;
  355. smaller=min(a,b);//smaller in normal sense
  356. bigger=max(a,b);
  357. u32_t distance=min(bigger-smaller,smaller+(0xffffffff-bigger+1));
  358. if(distance==bigger-smaller)
  359. {
  360. if(bigger==a)
  361. {
  362. return 1;
  363. }
  364. else
  365. {
  366. return 0;
  367. }
  368. }
  369. else
  370. {
  371. if(smaller==b)
  372. {
  373. return 0;
  374. }
  375. else
  376. {
  377. return 1;
  378. }
  379. }
  380. }
  381. bool larger_than_u16(uint16_t a,uint16_t b)
  382. {
  383. uint16_t smaller,bigger;
  384. smaller=min(a,b);//smaller in normal sense
  385. bigger=max(a,b);
  386. uint16_t distance=min(bigger-smaller,smaller+(0xffff-bigger+1));
  387. if(distance==bigger-smaller)
  388. {
  389. if(bigger==a)
  390. {
  391. return 1;
  392. }
  393. else
  394. {
  395. return 0;
  396. }
  397. }
  398. else
  399. {
  400. if(smaller==b)
  401. {
  402. return 0;
  403. }
  404. else
  405. {
  406. return 1;
  407. }
  408. }
  409. }
  410. vector<string> string_to_vec(const char * s,const char * sp) {
  411. vector<string> res;
  412. string str=s;
  413. char *p = strtok ((char *)str.c_str(),sp);
  414. while (p != NULL)
  415. {
  416. res.push_back(p);
  417. //printf ("%s\n",p);
  418. p = strtok(NULL, sp);
  419. }
  420. /* for(int i=0;i<(int)res.size();i++)
  421. {
  422. printf("<<%s>>\n",res[i].c_str());
  423. }*/
  424. return res;
  425. }
  426. vector< vector <string> > string_to_vec2(const char * s)
  427. {
  428. vector< vector <string> > res;
  429. vector<string> lines=string_to_vec(s,"\n");
  430. for(int i=0;i<int(lines.size());i++)
  431. {
  432. vector<string> tmp;
  433. tmp=string_to_vec(lines[i].c_str(),"\t ");
  434. res.push_back(tmp);
  435. }
  436. return res;
  437. }
  438. int read_file(const char * file,string &output)
  439. {
  440. const int max_len=3*1024*1024;
  441. // static char buf[max_len+100];
  442. string buf0;
  443. buf0.reserve(max_len+200);
  444. char * buf=(char *)buf0.c_str();
  445. buf[max_len]=0;
  446. //buf[sizeof(buf)-1]=0;
  447. int fd=open(file,O_RDONLY);
  448. if(fd==-1)
  449. {
  450. mylog(log_error,"read_file %s fail\n",file);
  451. return -1;
  452. }
  453. int len=read(fd,buf,max_len);
  454. if(len==max_len)
  455. {
  456. buf[0]=0;
  457. mylog(log_error,"%s too long,buf not large enough\n",file);
  458. return -2;
  459. }
  460. else if(len<0)
  461. {
  462. buf[0]=0;
  463. mylog(log_error,"%s read fail %d\n",file,len);
  464. return -3;
  465. }
  466. else
  467. {
  468. buf[len]=0;
  469. output=buf;
  470. }
  471. return 0;
  472. }
  473. int run_command(string command0,char * &output,int flag) {
  474. FILE *in;
  475. if((flag&show_log)==0) command0+=" 2>&1 ";
  476. const char * command=command0.c_str();
  477. int level= (flag&show_log)?log_warn:log_debug;
  478. if(flag&show_command)
  479. {
  480. mylog(log_info,"run_command %s\n",command);
  481. }
  482. else
  483. {
  484. mylog(log_debug,"run_command %s\n",command);
  485. }
  486. static __thread char buf[1024*1024+100];
  487. buf[sizeof(buf)-1]=0;
  488. if(!(in = popen(command, "r"))){
  489. mylog(level,"command %s popen failed,errno %s\n",command,strerror(errno));
  490. return -1;
  491. }
  492. int len =fread(buf, 1024*1024, 1, in);
  493. if(len==1024*1024)
  494. {
  495. buf[0]=0;
  496. mylog(level,"too long,buf not larger enough\n");
  497. return -2;
  498. }
  499. else
  500. {
  501. buf[len]=0;
  502. }
  503. int ret;
  504. if(( ret=ferror(in) ))
  505. {
  506. mylog(level,"command %s fread failed,ferror return value %d \n",command,ret);
  507. return -3;
  508. }
  509. //if(output!=0)
  510. output=buf;
  511. ret= pclose(in);
  512. int ret2=WEXITSTATUS(ret);
  513. if(ret!=0||ret2!=0)
  514. {
  515. mylog(level,"commnad %s ,pclose returned %d ,WEXITSTATUS %d,errnor :%s \n",command,ret,ret2,strerror(errno));
  516. return -4;
  517. }
  518. return 0;
  519. }
  520. /*
  521. int run_command_no_log(string command0,char * &output) {
  522. FILE *in;
  523. command0+=" 2>&1 ";
  524. const char * command=command0.c_str();
  525. mylog(log_debug,"run_command_no_log %s\n",command);
  526. static char buf[1024*1024+100];
  527. buf[sizeof(buf)-1]=0;
  528. if(!(in = popen(command, "r"))){
  529. mylog(log_debug,"command %s popen failed,errno %s\n",command,strerror(errno));
  530. return -1;
  531. }
  532. int len =fread(buf, 1024*1024, 1, in);
  533. if(len==1024*1024)
  534. {
  535. buf[0]=0;
  536. mylog(log_debug,"too long,buf not larger enough\n");
  537. return -2;
  538. }
  539. else
  540. {
  541. buf[len]=0;
  542. }
  543. int ret;
  544. if(( ret=ferror(in) ))
  545. {
  546. mylog(log_debug,"command %s fread failed,ferror return value %d \n",command,ret);
  547. return -3;
  548. }
  549. //if(output!=0)
  550. output=buf;
  551. ret= pclose(in);
  552. int ret2=WEXITSTATUS(ret);
  553. if(ret!=0||ret2!=0)
  554. {
  555. mylog(log_debug,"commnad %s ,pclose returned %d ,WEXITSTATUS %d,errnor :%s \n",command,ret,ret2,strerror(errno));
  556. return -4;
  557. }
  558. return 0;
  559. }*/
  560. // Remove preceding and trailing characters
  561. string trim(const string& str, char c) {
  562. size_t first = str.find_first_not_of(c);
  563. if(string::npos==first)
  564. {
  565. return "";
  566. }
  567. size_t last = str.find_last_not_of(c);
  568. return str.substr(first,(last-first+1));
  569. }
  570. vector<string> parse_conf_line(const string& s0)
  571. {
  572. string s=s0;
  573. s.reserve(s.length()+200);
  574. char *buf=(char *)s.c_str();
  575. //char buf[s.length()+200];
  576. char *p=buf;
  577. int i=int(s.length())-1;
  578. int j;
  579. vector<string>res;
  580. strcpy(buf,(char *)s.c_str());
  581. while(i>=0)
  582. {
  583. if(buf[i]==' ' || buf[i]== '\t')
  584. buf[i]=0;
  585. else break;
  586. i--;
  587. }
  588. while(*p!=0)
  589. {
  590. if(*p==' ' || *p== '\t')
  591. {
  592. p++;
  593. }
  594. else break;
  595. }
  596. int new_len=strlen(p);
  597. if(new_len==0)return res;
  598. if(p[0]=='#') return res;
  599. if(p[0]!='-')
  600. {
  601. mylog(log_fatal,"line :<%s> not begin with '-' ",s.c_str());
  602. myexit(-1);
  603. }
  604. for(i=0;i<new_len;i++)
  605. {
  606. if(p[i]==' '||p[i]=='\t')
  607. {
  608. break;
  609. }
  610. }
  611. if(i==new_len)
  612. {
  613. res.push_back(p);
  614. return res;
  615. }
  616. j=i;
  617. while(p[j]==' '||p[j]=='\t')
  618. j++;
  619. p[i]=0;
  620. res.push_back(p);
  621. res.push_back(p+j);
  622. return res;
  623. }