common.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <stdlib.h> //for exit(0);
  22. #include <errno.h> //For errno - the error number
  23. #include <netinet/tcp.h> //Provides declarations for tcp header
  24. #include <netinet/udp.h>
  25. #include <netinet/ip.h> //Provides declarations for ip header
  26. #include <netinet/if_ether.h>
  27. #include <arpa/inet.h>
  28. #include <fcntl.h>
  29. #include <byteswap.h>
  30. #include <arpa/inet.h>
  31. #include <linux/if_ether.h>
  32. #include <linux/filter.h>
  33. #include <sys/time.h>
  34. #include <time.h>
  35. #include <sys/timerfd.h>
  36. #include <sys/ioctl.h>
  37. #include <netinet/in.h>
  38. #include <net/if.h>
  39. #include <arpa/inet.h>
  40. #include <stdarg.h>
  41. #include <assert.h>
  42. #include <linux/if_packet.h>
  43. #include <byteswap.h>
  44. #include <pthread.h>
  45. #include<unordered_map>
  46. #include <fstream>
  47. #include <string>
  48. #include <vector>
  49. #include <map>
  50. #include <set>
  51. using namespace std;
  52. typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64
  53. typedef long long i64_t;
  54. typedef unsigned int u32_t;
  55. typedef int i32_t;
  56. typedef u32_t id_t;
  57. typedef u64_t iv_t;
  58. typedef u64_t padding_t;
  59. typedef u64_t anti_replay_seq_t;
  60. const int max_data_len=1600;
  61. const int buf_len=max_data_len+400;
  62. u64_t get_current_time();
  63. u64_t pack_u64(u32_t a,u32_t b);
  64. u32_t get_u64_h(u64_t a);
  65. u32_t get_u64_l(u64_t a);
  66. char * my_ntoa(u32_t ip);
  67. void init_random_number_fd();
  68. u64_t get_true_random_number_64();
  69. u32_t get_true_random_number();
  70. u32_t get_true_random_number_nz();
  71. u64_t ntoh64(u64_t a);
  72. u64_t hton64(u64_t a);
  73. bool larger_than_u16(uint16_t a,uint16_t b);
  74. bool larger_than_u32(u32_t a,u32_t b);
  75. void setnonblocking(int sock);
  76. int set_buf_size(int fd,int socket_buf_size,int force_socket_buf);
  77. void myexit(int a);
  78. unsigned short csum(const unsigned short *ptr,int nbytes);
  79. int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len);
  80. int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3);
  81. const int show_none=0;
  82. const int show_command=0x1;
  83. const int show_log=0x2;
  84. const int show_all=show_command|show_log;
  85. int run_command(string command,char * &output,int flag=show_all);
  86. //int run_command_no_log(string command,char * &output);
  87. int read_file(const char * file,string &output);
  88. vector<string> string_to_vec(const char * s,const char * sp);
  89. vector< vector <string> > string_to_vec2(const char * s);
  90. string trim(const string& str, char c);
  91. string trim_conf_line(const string& str);
  92. vector<string> parse_conf_line(const string& s);
  93. int hex_to_u32_with_endian(const string & a,u32_t &output);
  94. int hex_to_u32(const string & a,u32_t &output);
  95. //extern string iptables_pattern;
  96. #endif /* COMMON_H_ */