common.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. #include <list>
  54. using namespace std;
  55. typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64
  56. typedef long long i64_t;
  57. typedef unsigned int u32_t;
  58. typedef int i32_t;
  59. typedef unsigned short u16_t;
  60. typedef short i16_t;
  61. typedef u32_t id_t;
  62. typedef u64_t iv_t;
  63. typedef u64_t padding_t;
  64. typedef u64_t anti_replay_seq_t;
  65. typedef u64_t my_time_t;
  66. const int max_addr_len=100;
  67. extern int force_socket_buf;
  68. /*
  69. struct ip_port_t
  70. {
  71. u32_t ip;
  72. int port;
  73. void from_u64(u64_t u64);
  74. u64_t to_u64();
  75. char * to_s();
  76. };*/
  77. typedef u64_t fd64_t;
  78. u32_t djb2(unsigned char *str,int len);
  79. u32_t sdbm(unsigned char *str,int len);
  80. struct address_t //TODO scope id
  81. {
  82. struct hash_function
  83. {
  84. u32_t operator()(const address_t &key) const
  85. {
  86. return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
  87. }
  88. };
  89. union storage_t //sockaddr_storage is too huge, we dont use it.
  90. {
  91. sockaddr_in ipv4;
  92. sockaddr_in6 ipv6;
  93. };
  94. storage_t inner;
  95. address_t()
  96. {
  97. clear();
  98. }
  99. void clear()
  100. {
  101. memset(&inner,0,sizeof(inner));
  102. }
  103. int from_ip_port(u32_t ip, int port)
  104. {
  105. clear();
  106. inner.ipv4.sin_family=AF_INET;
  107. inner.ipv4.sin_port=htons(port);
  108. inner.ipv4.sin_addr.s_addr=ip;
  109. return 0;
  110. }
  111. int from_str(char * str);
  112. int from_sockaddr(sockaddr *,socklen_t);
  113. char* get_str();
  114. void to_str(char *);
  115. inline u32_t get_type()
  116. {
  117. return ((sockaddr*)&inner)->sa_family;
  118. }
  119. inline u32_t get_len()
  120. {
  121. u32_t type=get_type();
  122. switch(type)
  123. {
  124. case AF_INET:
  125. return sizeof(sockaddr_in);
  126. case AF_INET6:
  127. return sizeof(sockaddr_in6);
  128. default:
  129. assert(0==1);
  130. }
  131. return -1;
  132. }
  133. inline u32_t get_port()
  134. {
  135. u32_t type=get_type();
  136. switch(type)
  137. {
  138. case AF_INET:
  139. return ntohs(inner.ipv4.sin_port);
  140. case AF_INET6:
  141. return ntohs(inner.ipv6.sin6_port);
  142. default:
  143. assert(0==1);
  144. }
  145. return -1;
  146. }
  147. inline void set_port(int port)
  148. {
  149. u32_t type=get_type();
  150. switch(type)
  151. {
  152. case AF_INET:
  153. inner.ipv4.sin_port=htons(port);
  154. break;
  155. case AF_INET6:
  156. inner.ipv6.sin6_port=htons(port);
  157. break;
  158. default:
  159. assert(0==1);
  160. }
  161. return ;
  162. }
  163. bool operator == (const address_t &b) const
  164. {
  165. //return this->data==b.data;
  166. return memcmp(&this->inner,&b.inner,sizeof(this->inner))==0;
  167. }
  168. int new_connected_udp_fd();
  169. char* get_ip();
  170. };
  171. namespace std {
  172. template <>
  173. struct hash<address_t>
  174. {
  175. std::size_t operator()(const address_t& key) const
  176. {
  177. //return address_t::hash_function(k);
  178. return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
  179. }
  180. };
  181. }
  182. struct not_copy_able_t
  183. {
  184. not_copy_able_t()
  185. {
  186. }
  187. not_copy_able_t(const not_copy_able_t &other)
  188. {
  189. assert(0==1);
  190. }
  191. const not_copy_able_t & operator=(const not_copy_able_t &other)
  192. {
  193. assert(0==1);
  194. return other;
  195. }
  196. };
  197. const int max_data_len=1800;
  198. const int buf_len=max_data_len+400;
  199. const int max_address_len=512;
  200. u64_t get_current_time();
  201. u64_t pack_u64(u32_t a,u32_t b);
  202. u32_t get_u64_h(u64_t a);
  203. u32_t get_u64_l(u64_t a);
  204. char * my_ntoa(u32_t ip);
  205. void init_random_number_fd();
  206. u64_t get_true_random_number_64();
  207. u32_t get_true_random_number();
  208. u32_t get_true_random_number_nz();
  209. u64_t ntoh64(u64_t a);
  210. u64_t hton64(u64_t a);
  211. void write_u16(char *,u16_t a);// network order
  212. u16_t read_u16(char *);
  213. void write_u32(char *,u32_t a);// network order
  214. u32_t read_u32(char *);
  215. void write_u64(char *,u64_t a);
  216. u64_t read_uu64(char *);
  217. bool larger_than_u16(uint16_t a,uint16_t b);
  218. bool larger_than_u32(u32_t a,u32_t b);
  219. void setnonblocking(int sock);
  220. int set_buf_size(int fd,int socket_buf_size);
  221. void myexit(int a);
  222. unsigned short csum(const unsigned short *ptr,int nbytes);
  223. unsigned short csum_with_header(char* header,int hlen,const unsigned short *ptr,int nbytes);
  224. int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len);
  225. int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3);
  226. const int show_none=0;
  227. const int show_command=0x1;
  228. const int show_log=0x2;
  229. const int show_all=show_command|show_log;
  230. int run_command(string command,char * &output,int flag=show_all);
  231. //int run_command_no_log(string command,char * &output);
  232. int read_file(const char * file,string &output);
  233. vector<string> string_to_vec(const char * s,const char * sp);
  234. vector< vector <string> > string_to_vec2(const char * s);
  235. string trim(const string& str, char c);
  236. string trim_conf_line(const string& str);
  237. vector<string> parse_conf_line(const string& s);
  238. int hex_to_u32_with_endian(const string & a,u32_t &output);
  239. int hex_to_u32(const string & a,u32_t &output);
  240. //extern string iptables_pattern;
  241. int create_fifo(char * file);
  242. void print_binary_chars(const char * a,int len);
  243. template <class key_t>
  244. struct lru_collector_t:not_copy_able_t
  245. {
  246. //typedef void* key_t;
  247. //#define key_t void*
  248. struct lru_pair_t
  249. {
  250. key_t key;
  251. my_time_t ts;
  252. };
  253. unordered_map<key_t,typename list<lru_pair_t>::iterator> mp;
  254. list<lru_pair_t> q;
  255. int update(key_t key)
  256. {
  257. assert(mp.find(key)!=mp.end());
  258. auto it=mp[key];
  259. q.erase(it);
  260. my_time_t value=get_current_time();
  261. if(!q.empty())
  262. {
  263. assert(value >=q.front().ts);
  264. }
  265. lru_pair_t tmp; tmp.key=key; tmp.ts=value;
  266. q.push_front( tmp);
  267. mp[key]=q.begin();
  268. return 0;
  269. }
  270. int new_key(key_t key)
  271. {
  272. assert(mp.find(key)==mp.end());
  273. my_time_t value=get_current_time();
  274. if(!q.empty())
  275. {
  276. assert(value >=q.front().ts);
  277. }
  278. lru_pair_t tmp; tmp.key=key; tmp.ts=value;
  279. q.push_front( tmp);
  280. mp[key]=q.begin();
  281. return 0;
  282. }
  283. int size()
  284. {
  285. return q.size();
  286. }
  287. int empty()
  288. {
  289. return q.empty();
  290. }
  291. void clear()
  292. {
  293. mp.clear(); q.clear();
  294. }
  295. my_time_t ts_of(key_t key)
  296. {
  297. assert(mp.find(key)!=mp.end());
  298. return mp[key]->ts;
  299. }
  300. my_time_t peek_back(key_t &key)
  301. {
  302. assert(!q.empty());
  303. auto it=q.end(); it--;
  304. key=it->key;
  305. return it->ts;
  306. }
  307. void erase(key_t key)
  308. {
  309. assert(mp.find(key)!=mp.end());
  310. q.erase(mp[key]);
  311. mp.erase(key);
  312. }
  313. /*
  314. void erase_back()
  315. {
  316. assert(!q.empty());
  317. auto it=q.end(); it--;
  318. key_t key=it->key;
  319. erase(key);
  320. }*/
  321. };
  322. #endif /* COMMON_H_ */