common.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. /*
  127. struct ip_port_t
  128. {
  129. u32_t ip;
  130. int port;
  131. void from_u64(u64_t u64);
  132. u64_t to_u64();
  133. char * to_s();
  134. };*/
  135. typedef u64_t fd64_t;
  136. u32_t djb2(unsigned char *str,int len);
  137. u32_t sdbm(unsigned char *str,int len);
  138. struct address_t //TODO scope id
  139. {
  140. struct hash_function
  141. {
  142. u32_t operator()(const address_t &key) const
  143. {
  144. return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
  145. }
  146. };
  147. union storage_t //sockaddr_storage is too huge, we dont use it.
  148. {
  149. sockaddr_in ipv4;
  150. sockaddr_in6 ipv6;
  151. };
  152. storage_t inner;
  153. address_t()
  154. {
  155. clear();
  156. }
  157. void clear()
  158. {
  159. memset(&inner,0,sizeof(inner));
  160. }
  161. int from_ip_port(u32_t ip, int port)
  162. {
  163. clear();
  164. inner.ipv4.sin_family=AF_INET;
  165. inner.ipv4.sin_port=htons(port);
  166. inner.ipv4.sin_addr.s_addr=ip;
  167. return 0;
  168. }
  169. int from_ip_port_new(int type, void * ip, int port)
  170. {
  171. clear();
  172. if(type==AF_INET)
  173. {
  174. inner.ipv4.sin_family=AF_INET;
  175. inner.ipv4.sin_port=htons(port);
  176. inner.ipv4.sin_addr.s_addr=*((u32_t *)ip);
  177. }
  178. else if(type==AF_INET6)
  179. {
  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. {
  193. u32_t ret=((sockaddr*)&inner)->sa_family;
  194. assert(ret==AF_INET||ret==AF_INET6);
  195. return ret;
  196. }
  197. inline u32_t get_len()
  198. {
  199. u32_t type=get_type();
  200. switch(type)
  201. {
  202. case AF_INET:
  203. return sizeof(sockaddr_in);
  204. case AF_INET6:
  205. return sizeof(sockaddr_in6);
  206. default:
  207. assert(0==1);
  208. }
  209. return -1;
  210. }
  211. inline u32_t get_port()
  212. {
  213. u32_t type=get_type();
  214. switch(type)
  215. {
  216. case AF_INET:
  217. return ntohs(inner.ipv4.sin_port);
  218. case AF_INET6:
  219. return ntohs(inner.ipv6.sin6_port);
  220. default:
  221. assert(0==1);
  222. }
  223. return -1;
  224. }
  225. inline void set_port(int port)
  226. {
  227. u32_t type=get_type();
  228. switch(type)
  229. {
  230. case AF_INET:
  231. inner.ipv4.sin_port=htons(port);
  232. break;
  233. case AF_INET6:
  234. inner.ipv6.sin6_port=htons(port);
  235. break;
  236. default:
  237. assert(0==1);
  238. }
  239. return ;
  240. }
  241. bool operator == (const address_t &b) const
  242. {
  243. //return this->data==b.data;
  244. return memcmp(&this->inner,&b.inner,sizeof(this->inner))==0;
  245. }
  246. int new_connected_udp_fd();
  247. char* get_ip();
  248. };
  249. namespace std {
  250. template <>
  251. struct hash<address_t>
  252. {
  253. std::size_t operator()(const address_t& key) const
  254. {
  255. //return address_t::hash_function(k);
  256. return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
  257. }
  258. };
  259. }
  260. union my_ip_t //just a simple version of address_t,stores ip only
  261. {
  262. u32_t v4;
  263. in6_addr v6;
  264. bool equal (const my_ip_t &b) const;
  265. //int from_str(char * str);
  266. char * get_str1() const;
  267. char * get_str2() const;
  268. int from_address_t(address_t a);
  269. };
  270. struct not_copy_able_t
  271. {
  272. not_copy_able_t()
  273. {
  274. }
  275. not_copy_able_t(const not_copy_able_t &other)
  276. {
  277. assert(0==1);
  278. }
  279. const not_copy_able_t & operator=(const not_copy_able_t &other)
  280. {
  281. assert(0==1);
  282. return other;
  283. }
  284. };
  285. const int max_data_len=1800;
  286. const int buf_len=max_data_len+400;
  287. //const int max_address_len=512;
  288. u64_t get_current_time();
  289. u64_t pack_u64(u32_t a,u32_t b);
  290. u32_t get_u64_h(u64_t a);
  291. u32_t get_u64_l(u64_t a);
  292. char * my_ntoa(u32_t ip);
  293. void init_random_number_fd();
  294. u64_t get_true_random_number_64();
  295. u32_t get_true_random_number();
  296. u32_t get_true_random_number_nz();
  297. u64_t ntoh64(u64_t a);
  298. u64_t hton64(u64_t a);
  299. void write_u16(char *,u16_t a);// network order
  300. u16_t read_u16(char *);
  301. void write_u32(char *,u32_t a);// network order
  302. u32_t read_u32(char *);
  303. void write_u64(char *,u64_t a);
  304. u64_t read_u64(char *);
  305. bool larger_than_u16(uint16_t a,uint16_t b);
  306. bool larger_than_u32(u32_t a,u32_t b);
  307. void setnonblocking(int sock);
  308. int set_buf_size(int fd,int socket_buf_size);
  309. void myexit(int a);
  310. unsigned short csum(const unsigned short *ptr,int nbytes);
  311. unsigned short csum_with_header(char* header,int hlen,const unsigned short *ptr,int nbytes);
  312. int numbers_to_char(my_id_t id1,my_id_t id2,my_id_t id3,char * &data,int &len);
  313. int char_to_numbers(const char * data,int len,my_id_t &id1,my_id_t &id2,my_id_t &id3);
  314. const int show_none=0;
  315. const int show_command=0x1;
  316. const int show_log=0x2;
  317. const int show_all=show_command|show_log;
  318. int run_command(string command,char * &output,int flag=show_all);
  319. //int run_command_no_log(string command,char * &output);
  320. int read_file(const char * file,string &output);
  321. vector<string> string_to_vec(const char * s,const char * sp);
  322. vector< vector <string> > string_to_vec2(const char * s);
  323. string trim(const string& str, char c);
  324. string trim_conf_line(const string& str);
  325. vector<string> parse_conf_line(const string& s);
  326. int hex_to_u32_with_endian(const string & a,u32_t &output);
  327. int hex_to_u32(const string & a,u32_t &output);
  328. //extern string iptables_pattern;
  329. int create_fifo(char * file);
  330. void print_binary_chars(const char * a,int len);
  331. template <class key_t>
  332. struct lru_collector_t:not_copy_able_t
  333. {
  334. //typedef void* key_t;
  335. //#define key_t void*
  336. struct lru_pair_t
  337. {
  338. key_t key;
  339. my_time_t ts;
  340. };
  341. unordered_map<key_t,typename list<lru_pair_t>::iterator> mp;
  342. list<lru_pair_t> q;
  343. int update(key_t key)
  344. {
  345. assert(mp.find(key)!=mp.end());
  346. auto it=mp[key];
  347. q.erase(it);
  348. my_time_t value=get_current_time();
  349. if(!q.empty())
  350. {
  351. assert(value >=q.front().ts);
  352. }
  353. lru_pair_t tmp; tmp.key=key; tmp.ts=value;
  354. q.push_front( tmp);
  355. mp[key]=q.begin();
  356. return 0;
  357. }
  358. int new_key(key_t key)
  359. {
  360. assert(mp.find(key)==mp.end());
  361. my_time_t value=get_current_time();
  362. if(!q.empty())
  363. {
  364. assert(value >=q.front().ts);
  365. }
  366. lru_pair_t tmp; tmp.key=key; tmp.ts=value;
  367. q.push_front( tmp);
  368. mp[key]=q.begin();
  369. return 0;
  370. }
  371. int size()
  372. {
  373. return q.size();
  374. }
  375. int empty()
  376. {
  377. return q.empty();
  378. }
  379. void clear()
  380. {
  381. mp.clear(); q.clear();
  382. }
  383. my_time_t ts_of(key_t key)
  384. {
  385. assert(mp.find(key)!=mp.end());
  386. return mp[key]->ts;
  387. }
  388. my_time_t peek_back(key_t &key)
  389. {
  390. assert(!q.empty());
  391. auto it=q.end(); it--;
  392. key=it->key;
  393. return it->ts;
  394. }
  395. void erase(key_t key)
  396. {
  397. assert(mp.find(key)!=mp.end());
  398. q.erase(mp[key]);
  399. mp.erase(key);
  400. }
  401. /*
  402. void erase_back()
  403. {
  404. assert(!q.empty());
  405. auto it=q.end(); it--;
  406. key_t key=it->key;
  407. erase(key);
  408. }*/
  409. };
  410. #endif /* COMMON_H_ */