common.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * common.h
  3. *
  4. * Created on: Jul 29, 2017
  5. * Author: wangyu
  6. */
  7. #ifndef UDP2RAW_COMMON_H_
  8. #define UDP2RAW_COMMON_H_
  9. #define __STDC_FORMAT_MACROS 1
  10. #include <inttypes.h>
  11. #include<stdio.h>
  12. #include<string.h>
  13. #include<stdlib.h>
  14. #include<getopt.h>
  15. #include<unistd.h>
  16. #include<errno.h>
  17. #include <sys/epoll.h>
  18. #include <sys/wait.h>
  19. #include <sys/socket.h> //for socket ofcourse
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <stdlib.h> //for exit(0);
  23. #include <errno.h> //For errno - the error number
  24. #include <netdb.h> // for gethostbyname()
  25. #include <netinet/tcp.h> //Provides declarations for tcp header
  26. #include <netinet/udp.h>
  27. #include <netinet/ip.h> //Provides declarations for ip header
  28. #include <netinet/if_ether.h>
  29. #include <arpa/inet.h>
  30. #include <fcntl.h>
  31. #include <byteswap.h>
  32. #include <arpa/inet.h>
  33. #include <linux/if_ether.h>
  34. #include <linux/filter.h>
  35. #include <sys/time.h>
  36. #include <time.h>
  37. #include <sys/timerfd.h>
  38. #include <sys/ioctl.h>
  39. #include <netinet/in.h>
  40. #include <net/if.h>
  41. #include <arpa/inet.h>
  42. #include <stdarg.h>
  43. #include <assert.h>
  44. #include <linux/if_packet.h>
  45. #include <byteswap.h>
  46. #include <pthread.h>
  47. #include<unordered_map>
  48. #include <fstream>
  49. #include <string>
  50. #include <vector>
  51. #include <map>
  52. #include <set>
  53. using namespace std;
  54. typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64
  55. typedef long long i64_t;
  56. typedef unsigned int u32_t;
  57. typedef int i32_t;
  58. typedef u32_t id_t;
  59. typedef u64_t iv_t;
  60. typedef u64_t padding_t;
  61. typedef u64_t anti_replay_seq_t;
  62. struct ip_port_t
  63. {
  64. u32_t ip;
  65. int port;
  66. void from_u64(u64_t u64);
  67. u64_t to_u64();
  68. char * to_s();
  69. };
  70. typedef u64_t fd64_t;
  71. const int max_data_len=1800;
  72. const int buf_len=max_data_len+400;
  73. u64_t get_current_time();
  74. u64_t pack_u64(u32_t a,u32_t b);
  75. u32_t get_u64_h(u64_t a);
  76. u32_t get_u64_l(u64_t a);
  77. char * my_ntoa(u32_t ip);
  78. void init_random_number_fd();
  79. u64_t get_true_random_number_64();
  80. u32_t get_true_random_number();
  81. u32_t get_true_random_number_nz();
  82. u64_t ntoh64(u64_t a);
  83. u64_t hton64(u64_t a);
  84. bool larger_than_u16(uint16_t a,uint16_t b);
  85. bool larger_than_u32(u32_t a,u32_t b);
  86. void setnonblocking(int sock);
  87. int set_buf_size(int fd,int socket_buf_size,int force_socket_buf);
  88. void myexit(int a);
  89. unsigned short csum(const unsigned short *ptr,int nbytes);
  90. int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len);
  91. int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3);
  92. const int show_none=0;
  93. const int show_command=0x1;
  94. const int show_log=0x2;
  95. const int show_all=show_command|show_log;
  96. int run_command(string command,char * &output,int flag=show_all);
  97. //int run_command_no_log(string command,char * &output);
  98. int read_file(const char * file,string &output);
  99. vector<string> string_to_vec(const char * s,const char * sp);
  100. vector< vector <string> > string_to_vec2(const char * s);
  101. string trim(const string& str, char c);
  102. string trim_conf_line(const string& str);
  103. vector<string> parse_conf_line(const string& s);
  104. int hex_to_u32_with_endian(const string & a,u32_t &output);
  105. int hex_to_u32(const string & a,u32_t &output);
  106. //extern string iptables_pattern;
  107. int create_fifo(char * file);
  108. #endif /* COMMON_H_ */