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