common.cpp 11 KB

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