common.h 10 KB

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