common.h 11 KB

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