common.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. {
  114. return closesocket(fd);
  115. }
  116. #else
  117. typedef int my_fd_t;
  118. inline int sock_close(my_fd_t fd)
  119. {
  120. return close(fd);
  121. }
  122. #endif
  123. typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64
  124. typedef long long i64_t;
  125. typedef unsigned int u32_t;
  126. typedef int i32_t;
  127. typedef unsigned short u16_t;
  128. typedef short i16_t;
  129. typedef u32_t my_id_t;
  130. typedef u64_t iv_t;
  131. typedef u64_t padding_t;
  132. typedef u64_t anti_replay_seq_t;
  133. typedef u64_t my_time_t;
  134. const int max_addr_len=100;
  135. extern int force_socket_buf;
  136. extern int g_fix_gro;
  137. /*
  138. struct ip_port_t
  139. {
  140. u32_t ip;
  141. int port;
  142. void from_u64(u64_t u64);
  143. u64_t to_u64();
  144. char * to_s();
  145. };*/
  146. typedef u64_t fd64_t;
  147. u32_t djb2(unsigned char *str,int len);
  148. u32_t sdbm(unsigned char *str,int len);
  149. struct address_t //TODO scope id
  150. {
  151. struct hash_function
  152. {
  153. u32_t operator()(const address_t &key) const
  154. {
  155. return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
  156. }
  157. };
  158. union storage_t //sockaddr_storage is too huge, we dont use it.
  159. {
  160. sockaddr_in ipv4;
  161. sockaddr_in6 ipv6;
  162. };
  163. storage_t inner;
  164. address_t()
  165. {
  166. clear();
  167. }
  168. void clear()
  169. {
  170. memset(&inner,0,sizeof(inner));
  171. }
  172. int from_ip_port(u32_t ip, int port)
  173. {
  174. clear();
  175. inner.ipv4.sin_family=AF_INET;
  176. inner.ipv4.sin_port=htons(port);
  177. inner.ipv4.sin_addr.s_addr=ip;
  178. return 0;
  179. }
  180. int from_ip_port_new(int type, void * ip, int port)
  181. {
  182. clear();
  183. if(type==AF_INET)
  184. {
  185. inner.ipv4.sin_family=AF_INET;
  186. inner.ipv4.sin_port=htons(port);
  187. inner.ipv4.sin_addr.s_addr=*((u32_t *)ip);
  188. }
  189. else if(type==AF_INET6)
  190. {
  191. inner.ipv6.sin6_family=AF_INET6;
  192. inner.ipv6.sin6_port=htons(port);
  193. inner.ipv6.sin6_addr=*((in6_addr*)ip);
  194. }
  195. return 0;
  196. }
  197. int from_str(char * str);
  198. int from_str_ip_only(char * str);
  199. int from_sockaddr(sockaddr *,socklen_t);
  200. char* get_str();
  201. void to_str(char *);
  202. inline u32_t get_type()
  203. {
  204. u32_t ret=((sockaddr*)&inner)->sa_family;
  205. assert(ret==AF_INET||ret==AF_INET6);
  206. return ret;
  207. }
  208. inline u32_t get_len()
  209. {
  210. u32_t type=get_type();
  211. switch(type)
  212. {
  213. case AF_INET:
  214. return sizeof(sockaddr_in);
  215. case AF_INET6:
  216. return sizeof(sockaddr_in6);
  217. default:
  218. assert(0==1);
  219. }
  220. return -1;
  221. }
  222. inline u32_t get_port()
  223. {
  224. u32_t type=get_type();
  225. switch(type)
  226. {
  227. case AF_INET:
  228. return ntohs(inner.ipv4.sin_port);
  229. case AF_INET6:
  230. return ntohs(inner.ipv6.sin6_port);
  231. default:
  232. assert(0==1);
  233. }
  234. return -1;
  235. }
  236. inline void set_port(int port)
  237. {
  238. u32_t type=get_type();
  239. switch(type)
  240. {
  241. case AF_INET:
  242. inner.ipv4.sin_port=htons(port);
  243. break;
  244. case AF_INET6:
  245. inner.ipv6.sin6_port=htons(port);
  246. break;
  247. default:
  248. assert(0==1);
  249. }
  250. return ;
  251. }
  252. bool operator == (const address_t &b) const
  253. {
  254. //return this->data==b.data;
  255. return memcmp(&this->inner,&b.inner,sizeof(this->inner))==0;
  256. }
  257. int new_connected_udp_fd();
  258. char* get_ip();
  259. };
  260. namespace std {
  261. template <>
  262. struct hash<address_t>
  263. {
  264. std::size_t operator()(const address_t& key) const
  265. {
  266. //return address_t::hash_function(k);
  267. return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
  268. }
  269. };
  270. }
  271. union my_ip_t //just a simple version of address_t,stores ip only
  272. {
  273. u32_t v4;
  274. in6_addr v6;
  275. bool equal (const my_ip_t &b) const;
  276. //int from_str(char * str);
  277. char * get_str1() const;
  278. char * get_str2() const;
  279. int from_address_t(address_t a);
  280. };
  281. struct not_copy_able_t
  282. {
  283. not_copy_able_t()
  284. {
  285. }
  286. not_copy_able_t(const not_copy_able_t &other)
  287. {
  288. assert(0==1);
  289. }
  290. const not_copy_able_t & operator=(const not_copy_able_t &other)
  291. {
  292. assert(0==1);
  293. return other;
  294. }
  295. };
  296. const int huge_data_len=65535+100; //a packet with link level header might be larger than 65535
  297. const int huge_buf_len=huge_data_len+100;
  298. const int max_data_len=1800;
  299. const int buf_len=max_data_len+400;
  300. //const int max_address_len=512;
  301. #ifdef UDP2RAW_MP
  302. const int queue_len=200;
  303. struct queue_t
  304. {
  305. char data[queue_len][huge_buf_len];
  306. int data_len[queue_len];
  307. int head=0;
  308. int tail=0;
  309. void clear()
  310. {
  311. head=tail=0;
  312. }
  313. int empty()
  314. {
  315. if(head==tail) return 1;
  316. else return 0;
  317. }
  318. int full()
  319. {
  320. if( (tail+1)%queue_len==head ) return 1;
  321. else return 0;
  322. }
  323. void peek_front(char * & p,int &len)
  324. {
  325. assert(!empty());
  326. p=data[head];
  327. len=data_len[head];
  328. }
  329. void pop_front()
  330. {
  331. assert(!empty());
  332. head++;head%=queue_len;
  333. }
  334. void push_back(char * p,int len)
  335. {
  336. assert(!full());
  337. memcpy(data[tail],p,len);
  338. data_len[tail]=len;
  339. tail++;tail%=queue_len;
  340. }
  341. };
  342. int init_ws();
  343. #endif
  344. u64_t get_current_time();
  345. u64_t pack_u64(u32_t a,u32_t b);
  346. u32_t get_u64_h(u64_t a);
  347. u32_t get_u64_l(u64_t a);
  348. char * my_ntoa(u32_t ip);
  349. void init_random_number_fd();
  350. u64_t get_true_random_number_64();
  351. u32_t get_true_random_number();
  352. u32_t get_true_random_number_nz();
  353. u64_t ntoh64(u64_t a);
  354. u64_t hton64(u64_t a);
  355. void write_u16(char *,u16_t a);// network order
  356. u16_t read_u16(char *);
  357. void write_u32(char *,u32_t a);// network order
  358. u32_t read_u32(char *);
  359. void write_u64(char *,u64_t a);
  360. u64_t read_u64(char *);
  361. bool larger_than_u16(uint16_t a,uint16_t b);
  362. bool larger_than_u32(u32_t a,u32_t b);
  363. void setnonblocking(int sock);
  364. int set_buf_size(int fd,int socket_buf_size);
  365. void myexit(int a);
  366. unsigned short csum(const unsigned short *ptr,int nbytes);
  367. unsigned short csum_with_header(char* header,int hlen,const unsigned short *ptr,int nbytes);
  368. int numbers_to_char(my_id_t id1,my_id_t id2,my_id_t id3,char * &data,int &len);
  369. int char_to_numbers(const char * data,int len,my_id_t &id1,my_id_t &id2,my_id_t &id3);
  370. const int show_none=0;
  371. const int show_command=0x1;
  372. const int show_log=0x2;
  373. const int show_all=show_command|show_log;
  374. int run_command(string command,char * &output,int flag=show_all);
  375. //int run_command_no_log(string command,char * &output);
  376. int read_file(const char * file,string &output);
  377. vector<string> string_to_vec(const char * s,const char * sp);
  378. vector< vector <string> > string_to_vec2(const char * s);
  379. string trim(const string& str, char c);
  380. string trim_conf_line(const string& str);
  381. vector<string> parse_conf_line(const string& s);
  382. int hex_to_u32_with_endian(const string & a,u32_t &output);
  383. int hex_to_u32(const string & a,u32_t &output);
  384. //extern string iptables_pattern;
  385. int create_fifo(char * file);
  386. void print_binary_chars(const char * a,int len);
  387. template <class key_t>
  388. struct lru_collector_t:not_copy_able_t
  389. {
  390. //typedef void* key_t;
  391. //#define key_t void*
  392. struct lru_pair_t
  393. {
  394. key_t key;
  395. my_time_t ts;
  396. };
  397. unordered_map<key_t,typename list<lru_pair_t>::iterator> mp;
  398. list<lru_pair_t> q;
  399. int update(key_t key)
  400. {
  401. assert(mp.find(key)!=mp.end());
  402. auto it=mp[key];
  403. q.erase(it);
  404. my_time_t value=get_current_time();
  405. if(!q.empty())
  406. {
  407. assert(value >=q.front().ts);
  408. }
  409. lru_pair_t tmp; tmp.key=key; tmp.ts=value;
  410. q.push_front( tmp);
  411. mp[key]=q.begin();
  412. return 0;
  413. }
  414. int new_key(key_t key)
  415. {
  416. assert(mp.find(key)==mp.end());
  417. my_time_t value=get_current_time();
  418. if(!q.empty())
  419. {
  420. assert(value >=q.front().ts);
  421. }
  422. lru_pair_t tmp; tmp.key=key; tmp.ts=value;
  423. q.push_front( tmp);
  424. mp[key]=q.begin();
  425. return 0;
  426. }
  427. int size()
  428. {
  429. return q.size();
  430. }
  431. int empty()
  432. {
  433. return q.empty();
  434. }
  435. void clear()
  436. {
  437. mp.clear(); q.clear();
  438. }
  439. my_time_t ts_of(key_t key)
  440. {
  441. assert(mp.find(key)!=mp.end());
  442. return mp[key]->ts;
  443. }
  444. my_time_t peek_back(key_t &key)
  445. {
  446. assert(!q.empty());
  447. auto it=q.end(); it--;
  448. key=it->key;
  449. return it->ts;
  450. }
  451. void erase(key_t key)
  452. {
  453. assert(mp.find(key)!=mp.end());
  454. q.erase(mp[key]);
  455. mp.erase(key);
  456. }
  457. /*
  458. void erase_back()
  459. {
  460. assert(!q.empty());
  461. auto it=q.end(); it--;
  462. key_t key=it->key;
  463. erase(key);
  464. }*/
  465. };
  466. #endif /* COMMON_H_ */