common.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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/stat.h>
  18. #include <stdlib.h> //for exit(0);
  19. #include <errno.h> //For errno - the error number
  20. #include <fcntl.h>
  21. #include <sys/time.h>
  22. #include <time.h>
  23. #include <stdarg.h>
  24. #include <assert.h>
  25. #include <pthread.h>
  26. #if defined(UDP2RAW_MP)
  27. #if !defined(__CYGWIN__) && !defined(__MINGW32__)
  28. #include <pcap.h>
  29. #else
  30. #include <pcap_wrapper.h>
  31. #define NO_LIBNET
  32. #endif
  33. #ifndef NO_LIBNET
  34. #include <libnet.h>
  35. #endif
  36. #else
  37. //#include <linux/if_ether.h>
  38. #include <linux/filter.h>
  39. #include <linux/if_packet.h>
  40. #include <sys/epoll.h>
  41. //#include <sys/wait.h> //signal
  42. #include <netinet/if_ether.h>
  43. #include <net/if.h>
  44. #include <sys/timerfd.h>
  45. #endif
  46. #include <my_ev.h>
  47. #if defined(__MINGW32__)
  48. #include <winsock2.h>
  49. #include <ws2ipdef.h>
  50. typedef unsigned char u_int8_t;
  51. typedef unsigned short u_int16_t;
  52. typedef unsigned int u_int32_t;
  53. typedef int socklen_t;
  54. #else
  55. #include <sys/socket.h>
  56. #include <sys/types.h>
  57. #include <sys/ioctl.h>
  58. #include <arpa/inet.h>
  59. #include <netinet/in.h>
  60. #endif
  61. #include<unordered_map>
  62. #include <fstream>
  63. #include <string>
  64. #include <vector>
  65. #include <map>
  66. #include <set>
  67. #include <list>
  68. using namespace std;
  69. #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
  70. defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || \
  71. defined(__BIG_ENDIAN__) || \
  72. defined(__ARMEB__) || \
  73. defined(__THUMBEB__) || \
  74. defined(__AARCH64EB__) || \
  75. defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__)
  76. #define UDP2RAW_BIG_ENDIAN 1
  77. #endif
  78. #if defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
  79. defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || \
  80. defined(__LITTLE_ENDIAN__) || \
  81. defined(__ARMEL__) || \
  82. defined(__THUMBEL__) || \
  83. defined(__AARCH64EL__) || \
  84. defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
  85. #define UDP2RAW_LITTLE_ENDIAN 1
  86. #endif
  87. #if defined(UDP2RAW_BIG_ENDIAN) &&defined(UDP2RAW_LITTLE_ENDIAN)
  88. #error "endian detection conflicts"
  89. #endif
  90. #if !defined(UDP2RAW_BIG_ENDIAN) && !defined(UDP2RAW_LITTLE_ENDIAN)
  91. #error "endian detection failed"
  92. #endif
  93. #if defined(__MINGW32__)
  94. int inet_pton(int af, const char *src, void *dst);
  95. const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
  96. #define setsockopt(a,b,c,d,e) setsockopt(a,b,c,(const char *)(d),e)
  97. #endif
  98. char *get_sock_error();
  99. int get_sock_errno();
  100. #if defined(__MINGW32__)
  101. typedef SOCKET my_fd_t;
  102. inline int sock_close(my_fd_t fd)
  103. {
  104. return closesocket(fd);
  105. }
  106. #else
  107. typedef int my_fd_t;
  108. inline int sock_close(my_fd_t fd)
  109. {
  110. return close(fd);
  111. }
  112. #endif
  113. typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64
  114. typedef long long i64_t;
  115. typedef unsigned int u32_t;
  116. typedef int i32_t;
  117. typedef unsigned short u16_t;
  118. typedef short i16_t;
  119. typedef u32_t my_id_t;
  120. typedef u64_t iv_t;
  121. typedef u64_t padding_t;
  122. typedef u64_t anti_replay_seq_t;
  123. typedef u64_t my_time_t;
  124. const int max_addr_len=100;
  125. extern int force_socket_buf;
  126. extern int g_fix_gro;
  127. /*
  128. struct ip_port_t
  129. {
  130. u32_t ip;
  131. int port;
  132. void from_u64(u64_t u64);
  133. u64_t to_u64();
  134. char * to_s();
  135. };*/
  136. typedef u64_t fd64_t;
  137. u32_t djb2(unsigned char *str,int len);
  138. u32_t sdbm(unsigned char *str,int len);
  139. struct address_t //TODO scope id
  140. {
  141. struct hash_function
  142. {
  143. u32_t operator()(const address_t &key) const
  144. {
  145. return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
  146. }
  147. };
  148. union storage_t //sockaddr_storage is too huge, we dont use it.
  149. {
  150. sockaddr_in ipv4;
  151. sockaddr_in6 ipv6;
  152. };
  153. storage_t inner;
  154. address_t()
  155. {
  156. clear();
  157. }
  158. void clear()
  159. {
  160. memset(&inner,0,sizeof(inner));
  161. }
  162. int from_ip_port(u32_t ip, int port)
  163. {
  164. clear();
  165. inner.ipv4.sin_family=AF_INET;
  166. inner.ipv4.sin_port=htons(port);
  167. inner.ipv4.sin_addr.s_addr=ip;
  168. return 0;
  169. }
  170. int from_ip_port_new(int type, void * ip, int port)
  171. {
  172. clear();
  173. if(type==AF_INET)
  174. {
  175. inner.ipv4.sin_family=AF_INET;
  176. inner.ipv4.sin_port=htons(port);
  177. inner.ipv4.sin_addr.s_addr=*((u32_t *)ip);
  178. }
  179. else if(type==AF_INET6)
  180. {
  181. inner.ipv6.sin6_family=AF_INET6;
  182. inner.ipv6.sin6_port=htons(port);
  183. inner.ipv6.sin6_addr=*((in6_addr*)ip);
  184. }
  185. return 0;
  186. }
  187. int from_str(char * str);
  188. int from_str_ip_only(char * str);
  189. int from_sockaddr(sockaddr *,socklen_t);
  190. char* get_str();
  191. void to_str(char *);
  192. inline u32_t get_type()
  193. {
  194. u32_t ret=((sockaddr*)&inner)->sa_family;
  195. assert(ret==AF_INET||ret==AF_INET6);
  196. return ret;
  197. }
  198. inline u32_t get_len()
  199. {
  200. u32_t type=get_type();
  201. switch(type)
  202. {
  203. case AF_INET:
  204. return sizeof(sockaddr_in);
  205. case AF_INET6:
  206. return sizeof(sockaddr_in6);
  207. default:
  208. assert(0==1);
  209. }
  210. return -1;
  211. }
  212. inline u32_t get_port()
  213. {
  214. u32_t type=get_type();
  215. switch(type)
  216. {
  217. case AF_INET:
  218. return ntohs(inner.ipv4.sin_port);
  219. case AF_INET6:
  220. return ntohs(inner.ipv6.sin6_port);
  221. default:
  222. assert(0==1);
  223. }
  224. return -1;
  225. }
  226. inline void set_port(int port)
  227. {
  228. u32_t type=get_type();
  229. switch(type)
  230. {
  231. case AF_INET:
  232. inner.ipv4.sin_port=htons(port);
  233. break;
  234. case AF_INET6:
  235. inner.ipv6.sin6_port=htons(port);
  236. break;
  237. default:
  238. assert(0==1);
  239. }
  240. return ;
  241. }
  242. bool operator == (const address_t &b) const
  243. {
  244. //return this->data==b.data;
  245. return memcmp(&this->inner,&b.inner,sizeof(this->inner))==0;
  246. }
  247. int new_connected_udp_fd();
  248. char* get_ip();
  249. };
  250. namespace std {
  251. template <>
  252. struct hash<address_t>
  253. {
  254. std::size_t operator()(const address_t& key) const
  255. {
  256. //return address_t::hash_function(k);
  257. return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
  258. }
  259. };
  260. }
  261. union my_ip_t //just a simple version of address_t,stores ip only
  262. {
  263. u32_t v4;
  264. in6_addr v6;
  265. bool equal (const my_ip_t &b) const;
  266. //int from_str(char * str);
  267. char * get_str1() const;
  268. char * get_str2() const;
  269. int from_address_t(address_t a);
  270. };
  271. struct not_copy_able_t
  272. {
  273. not_copy_able_t()
  274. {
  275. }
  276. not_copy_able_t(const not_copy_able_t &other)
  277. {
  278. assert(0==1);
  279. }
  280. const not_copy_able_t & operator=(const not_copy_able_t &other)
  281. {
  282. assert(0==1);
  283. return other;
  284. }
  285. };
  286. const int single_max_data_len=1800;
  287. const int max_data_len=single_max_data_len*10;
  288. const int buf_len=max_data_len+800;
  289. //const int max_data_len_gro=max_data_len*10;
  290. //const int buf_len_gro=max_data_len_gro+400;
  291. //const int max_address_len=512;
  292. u64_t get_current_time();
  293. u64_t pack_u64(u32_t a,u32_t b);
  294. u32_t get_u64_h(u64_t a);
  295. u32_t get_u64_l(u64_t a);
  296. char * my_ntoa(u32_t ip);
  297. void init_random_number_fd();
  298. u64_t get_true_random_number_64();
  299. u32_t get_true_random_number();
  300. u32_t get_true_random_number_nz();
  301. u64_t ntoh64(u64_t a);
  302. u64_t hton64(u64_t a);
  303. void write_u16(char *,u16_t a);// network order
  304. u16_t read_u16(char *);
  305. void write_u32(char *,u32_t a);// network order
  306. u32_t read_u32(char *);
  307. void write_u64(char *,u64_t a);
  308. u64_t read_u64(char *);
  309. bool larger_than_u16(uint16_t a,uint16_t b);
  310. bool larger_than_u32(u32_t a,u32_t b);
  311. void setnonblocking(int sock);
  312. int set_buf_size(int fd,int socket_buf_size);
  313. void myexit(int a);
  314. unsigned short csum(const unsigned short *ptr,int nbytes);
  315. unsigned short csum_with_header(char* header,int hlen,const unsigned short *ptr,int nbytes);
  316. int numbers_to_char(my_id_t id1,my_id_t id2,my_id_t id3,char * &data,int &len);
  317. int char_to_numbers(const char * data,int len,my_id_t &id1,my_id_t &id2,my_id_t &id3);
  318. const int show_none=0;
  319. const int show_command=0x1;
  320. const int show_log=0x2;
  321. const int show_all=show_command|show_log;
  322. int run_command(string command,char * &output,int flag=show_all);
  323. //int run_command_no_log(string command,char * &output);
  324. int read_file(const char * file,string &output);
  325. vector<string> string_to_vec(const char * s,const char * sp);
  326. vector< vector <string> > string_to_vec2(const char * s);
  327. string trim(const string& str, char c);
  328. string trim_conf_line(const string& str);
  329. vector<string> parse_conf_line(const string& s);
  330. int hex_to_u32_with_endian(const string & a,u32_t &output);
  331. int hex_to_u32(const string & a,u32_t &output);
  332. //extern string iptables_pattern;
  333. int create_fifo(char * file);
  334. void print_binary_chars(const char * a,int len);
  335. template <class key_t>
  336. struct lru_collector_t:not_copy_able_t
  337. {
  338. //typedef void* key_t;
  339. //#define key_t void*
  340. struct lru_pair_t
  341. {
  342. key_t key;
  343. my_time_t ts;
  344. };
  345. unordered_map<key_t,typename list<lru_pair_t>::iterator> mp;
  346. list<lru_pair_t> q;
  347. int update(key_t key)
  348. {
  349. assert(mp.find(key)!=mp.end());
  350. auto it=mp[key];
  351. q.erase(it);
  352. my_time_t value=get_current_time();
  353. if(!q.empty())
  354. {
  355. assert(value >=q.front().ts);
  356. }
  357. lru_pair_t tmp; tmp.key=key; tmp.ts=value;
  358. q.push_front( tmp);
  359. mp[key]=q.begin();
  360. return 0;
  361. }
  362. int new_key(key_t key)
  363. {
  364. assert(mp.find(key)==mp.end());
  365. my_time_t value=get_current_time();
  366. if(!q.empty())
  367. {
  368. assert(value >=q.front().ts);
  369. }
  370. lru_pair_t tmp; tmp.key=key; tmp.ts=value;
  371. q.push_front( tmp);
  372. mp[key]=q.begin();
  373. return 0;
  374. }
  375. int size()
  376. {
  377. return q.size();
  378. }
  379. int empty()
  380. {
  381. return q.empty();
  382. }
  383. void clear()
  384. {
  385. mp.clear(); q.clear();
  386. }
  387. my_time_t ts_of(key_t key)
  388. {
  389. assert(mp.find(key)!=mp.end());
  390. return mp[key]->ts;
  391. }
  392. my_time_t peek_back(key_t &key)
  393. {
  394. assert(!q.empty());
  395. auto it=q.end(); it--;
  396. key=it->key;
  397. return it->ts;
  398. }
  399. void erase(key_t key)
  400. {
  401. assert(mp.find(key)!=mp.end());
  402. q.erase(mp[key]);
  403. mp.erase(key);
  404. }
  405. /*
  406. void erase_back()
  407. {
  408. assert(!q.empty());
  409. auto it=q.end(); it--;
  410. key_t key=it->key;
  411. erase(key);
  412. }*/
  413. };
  414. #endif /* COMMON_H_ */