common.h 10 KB

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