common.h 9.3 KB

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