• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef LOOKUP_H
2 #define LOOKUP_H
3 
4 #include <stdint.h>
5 #include <stddef.h>
6 #include <features.h>
7 #include <netinet/in.h>
8 #include <netdb.h>
9 #ifndef __LITEOS__
10 #include <musl_log.h>
11 #endif
12 
13 #if OHOS_DNS_PROXY_BY_NETSYS
14 
15 #include <stdio.h>
16 
17 #if DNS_CONFIG_DEBUG
18 #ifndef DNS_CONFIG_PRINT
19 #define DNS_CONFIG_PRINT(fmt, ...) printf("DNS " fmt "\n", ##__VA_ARGS__)
20 #endif
21 #else
22 #define DNS_CONFIG_PRINT(fmt, ...)
23 #endif
24 #endif
25 
26 struct aibuf {
27 	struct addrinfo ai;
28 	union sa {
29 		struct sockaddr_in sin;
30 		struct sockaddr_in6 sin6;
31 	} sa;
32 	volatile int lock[1];
33 	short slot, ref;
34 };
35 
36 struct address {
37 	int family;
38 	unsigned scopeid;
39 	uint8_t addr[16];
40 	int sortkey;
41 };
42 
43 struct service {
44 	uint16_t port;
45 	unsigned char proto, socktype;
46 };
47 
48 #define MAXNS 5
49 
50 struct resolvconf {
51 	struct address ns[MAXNS];
52 	unsigned nns, attempts, ndots;
53 #if OHOS_DNS_PROXY_BY_NETSYS
54     unsigned non_public;
55 #endif
56 	unsigned timeout;
57 };
58 
59 /* The limit of 48 results is a non-sharp bound on the number of addresses
60  * that can fit in one 512-byte DNS packet full of v4 results and a second
61  * packet full of v6 results. Due to headers, the actual limit is lower. */
62 #define MAXADDRS 48
63 #define MAXSERVS 2
64 
65 hidden int __lookup_serv(struct service buf[static MAXSERVS], const char *name, int proto, int socktype, int flags);
66 hidden int lookup_name_ext(struct address buf[static MAXADDRS], char canon[static 256], const char *name,
67 							 int family, int flags, int netid);
68 hidden int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags);
69 hidden int __lookup_ipliteral(struct address buf[static 1], const char *name, int family);
70 
71 hidden int __get_resolv_conf(struct resolvconf *, char *, size_t);
72 hidden int get_resolv_conf_ext(struct resolvconf *, char *, size_t, int netid);
73 hidden int __res_msend_rc(int, const unsigned char *const *, const int *, unsigned char *const *, int *, int, const struct resolvconf *);
74 hidden int res_msend_rc_ext(int, int, const unsigned char *const *, const int *, unsigned char *const *,
75 							int *, int, const struct resolvconf *, int *);
76 
77 hidden int __dns_parse(const unsigned char *, int, int (*)(void *, int, const void *, int, const void *, int), void *);
78 hidden int predefined_host_name_from_hosts(struct address buf[static MAXADDRS],
79 	char canon[static 256], const char *name, int family);
80 hidden int predefined_host_is_contain_host(const char *host);
81 hidden int predefined_host_lookup_ip(const char* host, const char* serv,
82     const struct addrinfo* hint, struct addrinfo** res);
83 hidden int res_bind_socket(int, int);
84 hidden int revert_dns_fail_cause(int cause);
85 
86 #if OHOS_DNS_PROXY_BY_NETSYS
87 #define DNS_SO_PATH "libnetsys_client.z.so"
88 #define MAX_SERVER_NUM 5
89 #define MAX_SERVER_LENGTH 50
90 #define OHOS_GET_CONFIG_FUNC_NAME "NetSysGetResolvConf"
91 #define OHOS_GET_CACHE_FUNC_NAME "NetSysGetResolvCache"
92 #define OHOS_SET_CACHE_FUNC_NAME "NetSysSetResolvCache"
93 #define OHOS_JUDGE_IPV6_FUNC_NAME "NetSysIsIpv6Enable"
94 #define OHOS_POST_DNS_RESULT_FUNC_NAME "NetSysPostDnsResult"
95 #define OHOS_GET_DEFAULT_NET_FUNC_NAME "NetSysGetDefaultNetwork"
96 #define MAX_RESULTS 32
97 #define MAX_CANON_NAME 256
98 #define MACRO_MIN(a, b) ((a) < (b) ? (a) : (b))
99 
100 struct resolv_config {
101 	int32_t error;
102 	int32_t timeout_ms;
103 	uint32_t retry_count;
104     uint32_t non_public_num;
105 	char nameservers[MAX_SERVER_NUM][MAX_SERVER_LENGTH + 1];
106 };
107 
108 typedef union {
109 	struct sockaddr sa;
110 	struct sockaddr_in6 sin6;
111 	struct sockaddr_in sin;
112 } aligned_sockAddr;
113 
114 struct addr_info_wrapper {
115 	uint32_t ai_flags;
116 	uint32_t ai_family;
117 	uint32_t ai_sockType;
118 	uint32_t ai_protocol;
119 	uint32_t ai_addrLen;
120 	aligned_sockAddr ai_addr;
121 	char ai_canonName[MAX_CANON_NAME + 1];
122 };
123 
124 struct param_wrapper {
125 	char *host;
126 	char *serv;
127 	struct addrinfo *hint;
128 };
129 
130 typedef int32_t (*GetConfig)(uint16_t netId, struct resolv_config *config);
131 
132 typedef int32_t (*GetCache)(uint16_t netId, struct param_wrapper param,
133 							struct addr_info_wrapper addr_info[static MAX_RESULTS],
134 							uint32_t *num);
135 
136 typedef int32_t (*SetCache)(uint16_t netId, struct param_wrapper param, struct addrinfo *res);
137 
138 typedef int (*JudgeIpv6)(uint16_t netId);
139 
140 typedef int (*PostDnsResult)(int netid, char* name, int usedtime, int queryfail,
141 							 struct addrinfo *res, struct queryparam *param);
142 
143 typedef int (*GetDefaultNet)(uint16_t netId, int32_t *currentnetid);
144 
145 /* If the memory holder points to stores NULL value, try to load symbol from the
146  * dns lib into holder; otherwise, it does nothing. */
147 hidden void resolve_dns_sym(void **holder, const char *symbol);
148 
149 void
150 dns_set_addr_info_to_netsys_cache(const char *__restrict host, const char *__restrict serv,
151 								  const struct addrinfo *__restrict
152 								  hint, struct addrinfo *res);
153 
154 void dns_set_addr_info_to_netsys_cache2(const int netid, const char *__restrict host, const char *__restrict serv,
155 										const struct addrinfo *__restrict hint, struct addrinfo *res);
156 
157 int dns_get_addr_info_from_netsys_cache(const char *__restrict host, const char *__restrict serv,
158 										const struct addrinfo *__restrict hint, struct addrinfo **__restrict res);
159 
160 int dns_get_addr_info_from_netsys_cache2(const int netid, const char *__restrict host, const char *__restrict serv,
161 										 const struct addrinfo *__restrict hint, struct addrinfo **__restrict res);
162 
163 int dns_post_result_to_netsys_cache(int netid, char* name, int usedtime, int querypass,
164 									struct addrinfo *res, struct queryparam *param);
165 
166 int dns_get_default_network(int *currentnetid);
167 #endif
168 
169 #if OHOS_FWMARK_CLIENT_BY_NETSYS
170 #define FWMARKCLIENT_SO_PATH "libfwmark_client.z.so"
171 #define OHOS_BIND_SOCKET_FUNC_NAME "BindSocket"
172 
173 typedef int32_t (*BindSocket)(int32_t fd, uint32_t netId);
174 
175 #define OHOS_NETSYS_BIND_SOCKET_FUNC_NAME "NetSysBindSocket"
176 
177 typedef int32_t (*BindSocket_Ext)(int32_t fd, uint32_t netId);
178 
179 #endif
180 
181 enum DNS_FAIL_REASON {
182 	// -2
183 	DNS_FAIL_REASON_PARAM_INVALID = -1101,
184 	DNS_FAIL_REASON_HOST_NAME_ILLEGAL = -1102,
185 	DNS_FAIL_REASON_GET_RESOLV_CONF_FAILED = -1103,
186 	DNS_FAIL_REASON_FAIL_TO_PARSE_DNS = -1104,
187 	DNS_FAIL_REASON_SERVER_NO_SUCH_NAME = -1105,
188 
189 	// -3
190 	DNS_FAIL_REASON_ROUTE_CONFIG_ERR = -1201,
191 	DNS_FAIL_REASON_FIREWALL_INTERCEPTION = -1202,
192 	DNS_FAIL_REASON_SERVER_NO_RESULT = -1203,
193 	DNS_FAIL_REASON_TCP_QUERY_FAILED = -1204,
194 	DNS_FAIL_REASON_CORE_ERRNO_BASE = -1205,
195 
196 	// -11
197 	DNS_FAIL_REASON_LACK_V6_SUPPORT = -1501,
198 	DNS_FAIL_REASON_CREATE_UDP_SOCKET_FAILED = -1502,
199 };
200 
201 #define FALLBACK_TCP_QUERY 200
202 
203 #endif
204