common.h 12 KB

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