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