common.h 6.8 KB

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