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 3 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(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags); 61 hidden int __lookup_ipliteral(struct address buf[static 1], const char *name, int family); 62 63 hidden int __get_resolv_conf(struct resolvconf *, char *, size_t); 64 hidden int __res_msend_rc(int, const unsigned char *const *, const int *, unsigned char *const *, int *, int, const struct resolvconf *); 65 66 hidden int __dns_parse(const unsigned char *, int, int (*)(void *, int, const void *, int, const void *), void *); 67 68 #if OHOS_DNS_PROXY_BY_NETSYS 69 #define DNS_SO_PATH "libnetsys_client.z.so" 70 #define MAX_SERVER_NUM 4 71 #define MAX_SERVER_LENGTH 50 72 #define OHOS_GET_CONFIG_FUNC_NAME "NetSysGetResolvConf" 73 #define OHOS_GET_CACHE_FUNC_NAME "NetSysGetResolvCache" 74 #define OHOS_SET_CACHE_FUNC_NAME "NetSysSetResolvCache" 75 #define MAX_RESULTS 32 76 #define MAX_CANON_NAME 256 77 #define MACRO_MIN(a, b) ((a) < (b) ? (a) : (b)) 78 79 struct resolv_config { 80 int32_t error; 81 int32_t timeout_ms; 82 uint32_t retry_count; 83 char nameservers[MAX_SERVER_NUM][MAX_SERVER_LENGTH + 1]; 84 }; 85 86 typedef union { 87 struct sockaddr sa; 88 struct sockaddr_in6 sin6; 89 struct sockaddr_in sin; 90 } aligned_sockAddr; 91 92 struct addr_info_wrapper { 93 uint32_t ai_flags; 94 uint32_t ai_family; 95 uint32_t ai_sockType; 96 uint32_t ai_protocol; 97 uint32_t ai_addrLen; 98 aligned_sockAddr ai_addr; 99 char ai_canonName[MAX_CANON_NAME + 1]; 100 }; 101 102 struct param_wrapper { 103 char *host; 104 char *serv; 105 struct addrinfo *hint; 106 }; 107 108 typedef int32_t (*GetConfig)(uint16_t netId, struct resolv_config *config); 109 110 typedef int32_t (*GetCache)(uint16_t netId, struct param_wrapper param, 111 struct addr_info_wrapper addr_info[static MAX_RESULTS], 112 uint32_t *num); 113 114 typedef int32_t (*SetCache)(uint16_t netId, struct param_wrapper param, struct addrinfo *res); 115 116 void 117 dns_set_addr_info_to_netsys_cache(const char *__restrict host, const char *__restrict serv, 118 const struct addrinfo *__restrict 119 hint, struct addrinfo *res); 120 121 int dns_get_addr_info_from_netsys_cache(const char *__restrict host, const char *__restrict serv, 122 const struct addrinfo *__restrict hint, struct addrinfo **__restrict res); 123 124 #endif 125 126 #endif 127