util.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2024 Ruilin Peng (Nick) <[email protected]>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef SMART_DNS_UTIL_H
  19. #define SMART_DNS_UTIL_H
  20. #include "stringutil.h"
  21. #include <netdb.h>
  22. #include <time.h>
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif /*__cplusplus */
  26. #ifndef TCP_FASTOPEN
  27. #define TCP_FASTOPEN 23
  28. #endif
  29. #ifndef TCP_FASTOPEN_CONNECT
  30. #define TCP_FASTOPEN_CONNECT 30
  31. #endif
  32. #ifndef TCP_THIN_LINEAR_TIMEOUTS
  33. #define TCP_THIN_LINEAR_TIMEOUTS 16
  34. #endif
  35. #ifndef TCP_THIN_DUPACK
  36. #define TCP_THIN_DUPACK 17
  37. #endif
  38. #define PORT_NOT_DEFINED -1
  39. #define MAX_IP_LEN 64
  40. #ifndef BASE_FILE_NAME
  41. #define BASE_FILE_NAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
  42. #endif
  43. #define BUG(format, ...) bug_ext(BASE_FILE_NAME, __LINE__, __func__, format, ##__VA_ARGS__)
  44. void bug_ext(const char *file, int line, const char *func, const char *errfmt, ...)
  45. __attribute__((format(printf, 4, 5))) __attribute__((nonnull(4)));
  46. unsigned long get_tick_count(void);
  47. char *dir_name(char *path);
  48. char *get_host_by_addr(char *host, int maxsize, struct sockaddr *addr);
  49. int generate_random_addr(unsigned char *addr, int addr_len, int mask);
  50. int generate_addr_map(const unsigned char *addr_from, const unsigned char *addr_to, unsigned char *addr_out,
  51. int addr_len, int mask);
  52. int is_private_addr(const unsigned char *addr, int addr_len);
  53. int getaddr_by_host(const char *host, struct sockaddr *addr, socklen_t *addr_len);
  54. int get_raw_addr_by_ip(const char *ip, unsigned char *raw_addr, int *raw_addr_len);
  55. int getsocket_inet(int fd, struct sockaddr *addr, socklen_t *addr_len);
  56. int fill_sockaddr_by_ip(unsigned char *ip, int ip_len, int port, struct sockaddr *addr, socklen_t *addr_len);
  57. int parse_ip(const char *value, char *ip, int *port);
  58. int check_is_ipaddr(const char *ip);
  59. int check_is_ipv4(const char *ip);
  60. int check_is_ipv6(const char *ip);
  61. int parser_mac_address(const char *in_mac, uint8_t mac[6]);
  62. int parse_uri(const char *value, char *scheme, char *host, int *port, char *path);
  63. int parse_uri_ext(const char *value, char *scheme, char *user, char *password, char *host, int *port, char *path);
  64. void urldecode(char *dst, const char *src);
  65. int set_fd_nonblock(int fd, int nonblock);
  66. char *reverse_string(char *output, const char *input, int len, int to_lower_case);
  67. char *to_lower_case(char *output, const char *input, int len);
  68. void print_stack(void);
  69. int ipset_add(const char *ipset_name, const unsigned char addr[], int addr_len, unsigned long timeout);
  70. int ipset_del(const char *ipset_name, const unsigned char addr[], int addr_len);
  71. int netlink_get_neighbors(int family,
  72. int (*callback)(const uint8_t *net_addr, int net_addr_len, const uint8_t mac[6], void *arg),
  73. void *arg);
  74. void SSL_CRYPTO_thread_setup(void);
  75. void SSL_CRYPTO_thread_cleanup(void);
  76. unsigned char *SSL_SHA256(const unsigned char *d, size_t n, unsigned char *md);
  77. int SSL_base64_decode(const char *in, unsigned char *out, int max_outlen);
  78. int SSL_base64_encode(const void *in, int in_len, char *out);
  79. int generate_cert_key(const char *key_path, const char *cert_path, const char *san, int days);
  80. int create_pid_file(const char *pid_file);
  81. int full_path(char *normalized_path, int normalized_path_len, const char *path);
  82. /* Parse a TLS packet for the Server Name Indication extension in the client
  83. * hello handshake, returning the first server name found (pointer to static
  84. * array)
  85. *
  86. * Returns:
  87. * >=0 - length of the hostname and updates *hostname
  88. * caller is responsible for freeing *hostname
  89. * -1 - Incomplete request
  90. * -2 - No Host header included in this request
  91. * -3 - Invalid hostname pointer
  92. * -4 - malloc failure
  93. * < -4 - Invalid TLS client hello
  94. */
  95. int parse_tls_header(const char *data, size_t data_len, char *hostname, const char **hostname_ptr);
  96. void get_compiled_time(struct tm *tm);
  97. unsigned long get_system_mem_size(void);
  98. int is_numeric(const char *str);
  99. int has_network_raw_cap(void);
  100. int has_unprivileged_ping(void);
  101. int set_sock_keepalive(int fd, int keepidle, int keepinterval, int keepcnt);
  102. int set_sock_lingertime(int fd, int time);
  103. uint64_t get_free_space(const char *path);
  104. void print_stack(void);
  105. void close_all_fd(int keepfd);
  106. typedef enum daemon_ret {
  107. DAEMON_RET_OK = 0,
  108. DAEMON_RET_ERR = -1,
  109. DAEMON_RET_CHILD_OK = -2,
  110. DAEMON_RET_PARENT_OK = -3,
  111. } daemon_ret;
  112. daemon_ret daemon_run(int *wstatus);
  113. int daemon_kickoff(int status, int no_close);
  114. int daemon_keepalive(void);
  115. void daemon_close_stdfds(void);
  116. int write_file(const char *filename, void *data, int data_len);
  117. int dns_packet_save(const char *dir, const char *type, const char *from, const void *packet, int packet_len);
  118. int dns_packet_debug(const char *packet_file);
  119. #ifdef __cplusplus
  120. }
  121. #endif /*__cplusplus */
  122. #endif