common.cpp 11 KB

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