common.h 12 KB

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