common.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * common.h
  3. *
  4. * Created on: Jul 29, 2017
  5. * Author: wangyu
  6. */
  7. #ifndef COMMON_H_
  8. #define 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/types.h>
  18. #include <sys/stat.h>
  19. #include <stdlib.h> //for exit(0);
  20. #include <errno.h> //For errno - the error number
  21. //#include <netinet/tcp.h> //Provides declarations for tcp header
  22. //#include <netinet/udp.h>
  23. //#include <netinet/ip.h> //Provides declarations for ip header
  24. //#include <netinet/if_ether.h>
  25. #include <fcntl.h>
  26. #include <sys/time.h>
  27. #include <time.h>
  28. //#include <netinet/in.h>
  29. //#include <net/if.h>
  30. #include <stdarg.h>
  31. #include <assert.h>
  32. #if !defined(NO_LIBEV_EMBED)
  33. #include <my_ev.h>
  34. #else
  35. #include "ev.h"
  36. #endif
  37. #if defined(__MINGW32__)
  38. #include <winsock2.h>
  39. #include <ws2tcpip.h>
  40. typedef int socklen_t;
  41. #else
  42. #include <sys/socket.h>
  43. #include <sys/types.h>
  44. #include <sys/ioctl.h>
  45. #include <arpa/inet.h>
  46. #include <netinet/in.h>
  47. #endif
  48. #include<unordered_map>
  49. #include<unordered_set>
  50. #include<map>
  51. #include<list>
  52. #include<string>
  53. #include<vector>
  54. using namespace std;
  55. typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64
  56. typedef long long i64_t;
  57. typedef unsigned int u32_t;
  58. typedef int i32_t;
  59. typedef unsigned short u16_t;
  60. typedef short i16_t;
  61. #if defined(__MINGW32__)
  62. int inet_pton(int af, const char *src, void *dst);
  63. const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
  64. #define setsockopt(a,b,c,d,e) setsockopt(a,b,c,(const char *)(d),e)
  65. #endif
  66. char *get_sock_error();
  67. int get_sock_errno();
  68. int init_ws();
  69. #if defined(__MINGW32__)
  70. typedef SOCKET my_fd_t;
  71. inline int sock_close(my_fd_t fd)
  72. {
  73. return closesocket(fd);
  74. }
  75. #else
  76. typedef int my_fd_t;
  77. inline int sock_close(my_fd_t fd)
  78. {
  79. return close(fd);
  80. }
  81. #endif
  82. struct my_itimerspec {
  83. struct timespec it_interval; /* Timer interval */
  84. struct timespec it_value; /* Initial expiration */
  85. };
  86. typedef u64_t my_time_t;
  87. const int max_addr_len=100;
  88. const int max_data_len=3600;
  89. const int buf_len=max_data_len+200;
  90. const int default_mtu=1250;
  91. //const u32_t timer_interval=400;
  92. ////const u32_t conv_timeout=180000;
  93. //const u32_t conv_timeout=40000;//for test
  94. const u32_t conv_timeout=180000;
  95. const int max_conv_num=10000;
  96. const int max_conn_num=200;
  97. /*
  98. const u32_t max_handshake_conn_num=10000;
  99. const u32_t max_ready_conn_num=1000;
  100. //const u32_t anti_replay_window_size=1000;
  101. const u32_t client_handshake_timeout=5000;
  102. const u32_t client_retry_interval=1000;
  103. const u32_t server_handshake_timeout=10000;// this should be much longer than clients. client retry initially ,server retry passtively*/
  104. const int conv_clear_ratio=30; //conv grabage collecter check 1/30 of all conv one time
  105. const int conn_clear_ratio=50;
  106. const int conv_clear_min=1;
  107. const int conn_clear_min=1;
  108. const u32_t conv_clear_interval=1000;
  109. const u32_t conn_clear_interval=1000;
  110. const i32_t max_fail_time=0;//disable
  111. const u32_t heartbeat_interval=1000;
  112. const u32_t timer_interval=400;//this should be smaller than heartbeat_interval and retry interval;
  113. //const uint32_t conv_timeout=120000; //120 second
  114. //const u32_t conv_timeout=120000; //for test
  115. const u32_t client_conn_timeout=10000;
  116. const u32_t client_conn_uplink_timeout=client_conn_timeout+2000;
  117. //const uint32_t server_conn_timeout=conv_timeout+60000;//this should be 60s+ longer than conv_timeout,so that conv_manager can destruct convs gradually,to avoid latency glicth
  118. const u32_t server_conn_timeout=conv_timeout+20000;//for test
  119. extern int about_to_exit;
  120. enum raw_mode_t{mode_faketcp=0,mode_udp,mode_icmp,mode_end};
  121. extern raw_mode_t raw_mode;
  122. enum program_mode_t {unset_mode=0,client_mode,server_mode};
  123. extern program_mode_t program_mode;
  124. extern unordered_map<int, const char*> raw_mode_tostring ;
  125. enum working_mode_t {unset_working_mode=0,tunnel_mode,tun_dev_mode};
  126. extern working_mode_t working_mode;
  127. extern int socket_buf_size;
  128. //typedef u32_t id_t;
  129. typedef u64_t iv_t;
  130. typedef u64_t padding_t;
  131. typedef u64_t anti_replay_seq_t;
  132. typedef u64_t fd64_t;
  133. //enum dest_type{none=0,type_fd64_ip_port,type_fd64,type_fd64_ip_port_conv,type_fd64_conv/*,type_fd*/};
  134. enum dest_type{none=0,type_fd64_addr,type_fd64,type_fd,type_write_fd,type_fd_addr/*,type_fd*/};
  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. struct fd64_ip_port_t
  145. {
  146. fd64_t fd64;
  147. ip_port_t ip_port;
  148. };
  149. struct fd_ip_port_t
  150. {
  151. int fd;
  152. ip_port_t ip_port;
  153. };*/
  154. struct pseudo_header {
  155. u32_t source_address;
  156. u32_t dest_address;
  157. unsigned char placeholder;
  158. unsigned char protocol;
  159. unsigned short tcp_length;
  160. };
  161. u32_t djb2(unsigned char *str,int len);
  162. u32_t sdbm(unsigned char *str,int len);
  163. struct address_t //TODO scope id
  164. {
  165. struct hash_function
  166. {
  167. u32_t operator()(const address_t &key) const
  168. {
  169. return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
  170. }
  171. };
  172. union storage_t //sockaddr_storage is too huge, we dont use it.
  173. {
  174. sockaddr_in ipv4;
  175. sockaddr_in6 ipv6;
  176. };
  177. storage_t inner;
  178. /*address_t()
  179. {
  180. clear();
  181. }*/
  182. void clear()
  183. {
  184. memset(&inner,0,sizeof(inner));
  185. }
  186. /*
  187. int from_ip_port(u32_t ip, int port)
  188. {
  189. clear();
  190. inner.ipv4.sin_family=AF_INET;
  191. inner.ipv4.sin_port=htons(port);
  192. inner.ipv4.sin_addr.s_addr=ip;
  193. return 0;
  194. }*/
  195. int from_ip_port_new(int type, void * ip, int port)
  196. {
  197. clear();
  198. if(type==AF_INET)
  199. {
  200. inner.ipv4.sin_family=AF_INET;
  201. inner.ipv4.sin_port=htons(port);
  202. inner.ipv4.sin_addr.s_addr=*((u32_t *)ip);
  203. }
  204. else if(type==AF_INET6)
  205. {
  206. inner.ipv6.sin6_family=AF_INET6;
  207. inner.ipv6.sin6_port=htons(port);
  208. inner.ipv6.sin6_addr=*((in6_addr*)ip);
  209. }
  210. return 0;
  211. }
  212. int from_str(char * str);
  213. int from_str_ip_only(char * str);
  214. int from_sockaddr(sockaddr *,socklen_t);
  215. char* get_str();
  216. void to_str(char *);
  217. inline int is_vaild()
  218. {
  219. u32_t ret=((sockaddr*)&inner)->sa_family;
  220. return (ret==AF_INET||ret==AF_INET6);
  221. }
  222. inline u32_t get_type()
  223. {
  224. assert(is_vaild());
  225. u32_t ret=((sockaddr*)&inner)->sa_family;
  226. return ret;
  227. }
  228. inline u32_t get_len()
  229. {
  230. u32_t type=get_type();
  231. switch(type)
  232. {
  233. case AF_INET:
  234. return sizeof(sockaddr_in);
  235. case AF_INET6:
  236. return sizeof(sockaddr_in6);
  237. default:
  238. assert(0==1);
  239. }
  240. return -1;
  241. }
  242. inline u32_t get_port()
  243. {
  244. u32_t type=get_type();
  245. switch(type)
  246. {
  247. case AF_INET:
  248. return ntohs(inner.ipv4.sin_port);
  249. case AF_INET6:
  250. return ntohs(inner.ipv6.sin6_port);
  251. default:
  252. assert(0==1);
  253. }
  254. return -1;
  255. }
  256. inline void set_port(int port)
  257. {
  258. u32_t type=get_type();
  259. switch(type)
  260. {
  261. case AF_INET:
  262. inner.ipv4.sin_port=htons(port);
  263. break;
  264. case AF_INET6:
  265. inner.ipv6.sin6_port=htons(port);
  266. break;
  267. default:
  268. assert(0==1);
  269. }
  270. return ;
  271. }
  272. bool operator == (const address_t &b) const
  273. {
  274. //return this->data==b.data;
  275. return memcmp(&this->inner,&b.inner,sizeof(this->inner))==0;
  276. }
  277. int new_connected_udp_fd();
  278. char* get_ip();
  279. };
  280. namespace std {
  281. template <>
  282. struct hash<address_t>
  283. {
  284. std::size_t operator()(const address_t& key) const
  285. {
  286. //return address_t::hash_function(k);
  287. return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
  288. }
  289. };
  290. }
  291. struct fd64_addr_t
  292. {
  293. fd64_t fd64;
  294. address_t addr;
  295. };
  296. struct fd_addr_t
  297. {
  298. int fd;
  299. address_t addr;
  300. };
  301. union inner_t
  302. {
  303. fd64_t fd64;
  304. int fd;
  305. fd64_addr_t fd64_addr;
  306. fd_addr_t fd_addr;
  307. };
  308. struct dest_t
  309. {
  310. dest_type type;
  311. inner_t inner;
  312. u32_t conv;
  313. int cook=0;
  314. };
  315. struct fd_info_t
  316. {
  317. address_t addr;
  318. ev_io io_watcher;
  319. };
  320. u64_t get_current_time();
  321. //u64_t get_current_time_rough();
  322. u64_t get_current_time_us();
  323. u64_t pack_u64(u32_t a,u32_t b);
  324. u32_t get_u64_h(u64_t a);
  325. u32_t get_u64_l(u64_t a);
  326. void write_u16(char *,u16_t a);
  327. u16_t read_u16(char *);
  328. void write_u32(char *,u32_t a);
  329. u32_t read_u32(char *);
  330. void write_u64(char *,u64_t a);
  331. u64_t read_uu64(char *);
  332. char * my_ntoa(u32_t ip);
  333. void myexit(int a);
  334. void init_random_number_fd();
  335. u64_t get_fake_random_number_64();
  336. u32_t get_fake_random_number();
  337. u32_t get_fake_random_number_nz();
  338. u64_t ntoh64(u64_t a);
  339. u64_t hton64(u64_t a);
  340. bool larger_than_u16(uint16_t a,uint16_t b);
  341. bool larger_than_u32(u32_t a,u32_t b);
  342. void setnonblocking(int sock);
  343. int set_buf_size(int fd,int socket_buf_size);
  344. unsigned short csum(const unsigned short *ptr,int nbytes);
  345. unsigned short tcp_csum(const pseudo_header & ph,const unsigned short *ptr,int nbytes);
  346. void signal_handler(int sig);
  347. //int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len);
  348. //int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3);
  349. void myexit(int a);
  350. int add_iptables_rule(char *);
  351. int clear_iptables_rule();
  352. void get_fake_random_chars(char * s,int len);
  353. int random_between(u32_t a,u32_t b);
  354. int set_timer_ms(int epollfd,int &timer_fd,u32_t timer_interval);
  355. int round_up_div(int a,int b);
  356. int create_fifo(char * file);
  357. /*
  358. int create_new_udp(int &new_udp_fd,int remote_address_uint32,int remote_port);
  359. */
  360. int new_listen_socket(int &fd,u32_t ip,int port);
  361. int new_connected_socket(int &fd,u32_t ip,int port);
  362. int new_listen_socket2(int &fd,address_t &addr);
  363. int new_connected_socket2(int &fd,address_t &addr,bool bind_enabled,address_t &bind_addr,char interface_string[]);
  364. struct not_copy_able_t
  365. {
  366. not_copy_able_t()
  367. {
  368. }
  369. not_copy_able_t(const not_copy_able_t &other)
  370. {
  371. assert(0==1);
  372. }
  373. const not_copy_able_t & operator=(const not_copy_able_t &other)
  374. {
  375. assert(0==1);
  376. return other;
  377. }
  378. };
  379. template <class key_t>
  380. struct lru_collector_t:not_copy_able_t
  381. {
  382. //typedef void* key_t;
  383. //#define key_t void*
  384. struct lru_pair_t
  385. {
  386. key_t key;
  387. my_time_t ts;
  388. };
  389. unordered_map<key_t,typename list<lru_pair_t>::iterator> mp;
  390. list<lru_pair_t> q;
  391. int update(key_t key)
  392. {
  393. assert(mp.find(key)!=mp.end());
  394. auto it=mp[key];
  395. q.erase(it);
  396. my_time_t value=get_current_time();
  397. if(!q.empty())
  398. {
  399. assert(value >=q.front().ts);
  400. }
  401. lru_pair_t tmp; tmp.key=key; tmp.ts=value;
  402. q.push_front( tmp);
  403. mp[key]=q.begin();
  404. return 0;
  405. }
  406. int new_key(key_t key)
  407. {
  408. assert(mp.find(key)==mp.end());
  409. my_time_t value=get_current_time();
  410. if(!q.empty())
  411. {
  412. assert(value >=q.front().ts);
  413. }
  414. lru_pair_t tmp; tmp.key=key; tmp.ts=value;
  415. q.push_front( tmp);
  416. mp[key]=q.begin();
  417. return 0;
  418. }
  419. int size()
  420. {
  421. return q.size();
  422. }
  423. int empty()
  424. {
  425. return q.empty();
  426. }
  427. void clear()
  428. {
  429. mp.clear(); q.clear();
  430. }
  431. my_time_t ts_of(key_t key)
  432. {
  433. assert(mp.find(key)!=mp.end());
  434. return mp[key]->ts;
  435. }
  436. my_time_t peek_back(key_t &key)
  437. {
  438. assert(!q.empty());
  439. auto it=q.end(); it--;
  440. key=it->key;
  441. return it->ts;
  442. }
  443. void erase(key_t key)
  444. {
  445. assert(mp.find(key)!=mp.end());
  446. q.erase(mp[key]);
  447. mp.erase(key);
  448. }
  449. /*
  450. void erase_back()
  451. {
  452. assert(!q.empty());
  453. auto it=q.end(); it--;
  454. key_t key=it->key;
  455. erase(key);
  456. }*/
  457. };
  458. vector<string> string_to_vec(const char * s,const char * sp) ;
  459. #endif /* COMMON_H_ */