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