Re-indent resolver code with clang-format
clang-format --style file -i *.{c,h}
Test: code does not compile yet
Change-Id: I8c509de4f8558b2dd74668cf40bb73c4316dc5ad
diff --git a/resolv/getaddrinfo.c b/resolv/getaddrinfo.c
index f1488a9..97b5b6f 100644
--- a/resolv/getaddrinfo.c
+++ b/resolv/getaddrinfo.c
@@ -77,58 +77,54 @@
* friends.
*/
-#include <fcntl.h>
-#include <sys/cdefs.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/param.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <net/if.h>
-#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
+#include <fcntl.h>
+#include <net/if.h>
#include <netdb.h>
-#include "NetdClientDispatch.h"
-#include "resolv_cache.h"
-#include "resolv_netid.h"
-#include "resolv_private.h"
+#include <netinet/in.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
+#include <sys/cdefs.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/un.h>
#include <unistd.h>
+#include "NetdClientDispatch.h"
+#include "resolv_cache.h"
+#include "resolv_netid.h"
+#include "resolv_private.h"
-#include <syslog.h>
#include <stdarg.h>
+#include <syslog.h>
#include "nsswitch.h"
#include "private/bionic_defs.h"
typedef union sockaddr_union {
- struct sockaddr generic;
- struct sockaddr_in in;
+ struct sockaddr generic;
+ struct sockaddr_in in;
struct sockaddr_in6 in6;
} sockaddr_union;
#define SUCCESS 0
#define ANY 0
#define YES 1
-#define NO 0
+#define NO 0
-static const char in_addrany[] = { 0, 0, 0, 0 };
-static const char in_loopback[] = { 127, 0, 0, 1 };
+static const char in_addrany[] = {0, 0, 0, 0};
+static const char in_loopback[] = {127, 0, 0, 1};
#ifdef INET6
-static const char in6_addrany[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
-};
-static const char in6_loopback[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
-};
+static const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+static const char in6_loopback[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
#endif
#if defined(__ANDROID__)
@@ -137,36 +133,32 @@
#endif
static const struct afd {
- int a_af;
- int a_addrlen;
- int a_socklen;
- int a_off;
- const char *a_addrany;
- const char *a_loopback;
- int a_scoped;
-} afdl [] = {
+ int a_af;
+ int a_addrlen;
+ int a_socklen;
+ int a_off;
+ const char* a_addrany;
+ const char* a_loopback;
+ int a_scoped;
+} afdl[] = {
#ifdef INET6
- {PF_INET6, sizeof(struct in6_addr),
- sizeof(struct sockaddr_in6),
- offsetof(struct sockaddr_in6, sin6_addr),
- in6_addrany, in6_loopback, 1},
+ {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
+ offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
#endif
- {PF_INET, sizeof(struct in_addr),
- sizeof(struct sockaddr_in),
- offsetof(struct sockaddr_in, sin_addr),
- in_addrany, in_loopback, 0},
- {0, 0, 0, 0, NULL, NULL, 0},
+ {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
+ offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
+ {0, 0, 0, 0, NULL, NULL, 0},
};
struct explore {
- int e_af;
- int e_socktype;
- int e_protocol;
- const char *e_protostr;
- int e_wild;
-#define WILD_AF(ex) ((ex)->e_wild & 0x01)
-#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
-#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
+ int e_af;
+ int e_socktype;
+ int e_protocol;
+ const char* e_protostr;
+ int e_wild;
+#define WILD_AF(ex) ((ex)->e_wild & 0x01)
+#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
+#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
};
static const struct explore explore[] = {
@@ -174,191 +166,169 @@
{ PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
#endif
#ifdef INET6
- { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
- { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
- { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
+ {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
+ {PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
+ {PF_INET6, SOCK_RAW, ANY, NULL, 0x05},
#endif
- { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
- { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
- { PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
- { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
- { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
- { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 },
- { -1, 0, 0, NULL, 0 },
+ {PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
+ {PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
+ {PF_INET, SOCK_RAW, ANY, NULL, 0x05},
+ {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
+ {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
+ {PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05},
+ {-1, 0, 0, NULL, 0},
};
#ifdef INET6
-#define PTON_MAX 16
+#define PTON_MAX 16
#else
-#define PTON_MAX 4
+#define PTON_MAX 4
#endif
static const ns_src default_dns_files[] = {
- { NSSRC_FILES, NS_SUCCESS },
- { NSSRC_DNS, NS_SUCCESS },
- { 0, 0 }
-};
+ {NSSRC_FILES, NS_SUCCESS}, {NSSRC_DNS, NS_SUCCESS}, {0, 0}};
-#define MAXPACKET (8*1024)
+#define MAXPACKET (8 * 1024)
typedef union {
- HEADER hdr;
- u_char buf[MAXPACKET];
+ HEADER hdr;
+ u_char buf[MAXPACKET];
} querybuf;
struct res_target {
- struct res_target *next;
- const char *name; /* domain name */
- int qclass, qtype; /* class and type of query */
- u_char *answer; /* buffer to put answer */
- int anslen; /* size of answer buffer */
- int n; /* result length */
+ struct res_target* next;
+ const char* name; /* domain name */
+ int qclass, qtype; /* class and type of query */
+ u_char* answer; /* buffer to put answer */
+ int anslen; /* size of answer buffer */
+ int n; /* result length */
};
-static int str2number(const char *);
-static int explore_fqdn(const struct addrinfo *, const char *,
- const char *, struct addrinfo **, const struct android_net_context *);
-static int explore_null(const struct addrinfo *,
- const char *, struct addrinfo **);
-static int explore_numeric(const struct addrinfo *, const char *,
- const char *, struct addrinfo **, const char *);
-static int explore_numeric_scope(const struct addrinfo *, const char *,
- const char *, struct addrinfo **);
-static int get_canonname(const struct addrinfo *,
- struct addrinfo *, const char *);
-static struct addrinfo *get_ai(const struct addrinfo *,
- const struct afd *, const char *);
-static int get_portmatch(const struct addrinfo *, const char *);
-static int get_port(const struct addrinfo *, const char *, int);
-static const struct afd *find_afd(int);
+static int str2number(const char*);
+static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
+ const struct android_net_context*);
+static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
+static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
+ const char*);
+static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
+ struct addrinfo**);
+static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
+static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
+static int get_portmatch(const struct addrinfo*, const char*);
+static int get_port(const struct addrinfo*, const char*, int);
+static const struct afd* find_afd(int);
#ifdef INET6
-static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);
+static int ip6_str2scopeid(char*, struct sockaddr_in6*, u_int32_t*);
#endif
-static struct addrinfo *getanswer(const querybuf *, int, const char *, int,
- const struct addrinfo *);
-static int _dns_getaddrinfo(void *, void *, va_list);
-static void _sethtent(FILE **);
-static void _endhtent(FILE **);
-static struct addrinfo *_gethtent(FILE **, const char *,
- const struct addrinfo *);
-static int _files_getaddrinfo(void *, void *, va_list);
-static int _find_src_addr(const struct sockaddr *, struct sockaddr *, unsigned , uid_t);
+static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*);
+static int _dns_getaddrinfo(void*, void*, va_list);
+static void _sethtent(FILE**);
+static void _endhtent(FILE**);
+static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
+static int _files_getaddrinfo(void*, void*, va_list);
+static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
-static int res_queryN(const char *, struct res_target *, res_state);
-static int res_searchN(const char *, struct res_target *, res_state);
-static int res_querydomainN(const char *, const char *,
- struct res_target *, res_state);
+static int res_queryN(const char*, struct res_target*, res_state);
+static int res_searchN(const char*, struct res_target*, res_state);
+static int res_querydomainN(const char*, const char*, struct res_target*, res_state);
-static const char * const ai_errlist[] = {
- "Success",
- "Address family for hostname not supported", /* EAI_ADDRFAMILY */
- "Temporary failure in name resolution", /* EAI_AGAIN */
- "Invalid value for ai_flags", /* EAI_BADFLAGS */
- "Non-recoverable failure in name resolution", /* EAI_FAIL */
- "ai_family not supported", /* EAI_FAMILY */
- "Memory allocation failure", /* EAI_MEMORY */
- "No address associated with hostname", /* EAI_NODATA */
- "hostname nor servname provided, or not known", /* EAI_NONAME */
- "servname not supported for ai_socktype", /* EAI_SERVICE */
- "ai_socktype not supported", /* EAI_SOCKTYPE */
- "System error returned in errno", /* EAI_SYSTEM */
- "Invalid value for hints", /* EAI_BADHINTS */
- "Resolved protocol is unknown", /* EAI_PROTOCOL */
- "Argument buffer overflow", /* EAI_OVERFLOW */
- "Unknown error", /* EAI_MAX */
+static const char* const ai_errlist[] = {
+ "Success",
+ "Address family for hostname not supported", /* EAI_ADDRFAMILY */
+ "Temporary failure in name resolution", /* EAI_AGAIN */
+ "Invalid value for ai_flags", /* EAI_BADFLAGS */
+ "Non-recoverable failure in name resolution", /* EAI_FAIL */
+ "ai_family not supported", /* EAI_FAMILY */
+ "Memory allocation failure", /* EAI_MEMORY */
+ "No address associated with hostname", /* EAI_NODATA */
+ "hostname nor servname provided, or not known", /* EAI_NONAME */
+ "servname not supported for ai_socktype", /* EAI_SERVICE */
+ "ai_socktype not supported", /* EAI_SOCKTYPE */
+ "System error returned in errno", /* EAI_SYSTEM */
+ "Invalid value for hints", /* EAI_BADHINTS */
+ "Resolved protocol is unknown", /* EAI_PROTOCOL */
+ "Argument buffer overflow", /* EAI_OVERFLOW */
+ "Unknown error", /* EAI_MAX */
};
/* XXX macros that make external reference is BAD. */
-#define GET_AI(ai, afd, addr) \
-do { \
- /* external reference: pai, error, and label free */ \
- (ai) = get_ai(pai, (afd), (addr)); \
- if ((ai) == NULL) { \
- error = EAI_MEMORY; \
- goto free; \
- } \
-} while (/*CONSTCOND*/0)
+#define GET_AI(ai, afd, addr) \
+ do { \
+ /* external reference: pai, error, and label free */ \
+ (ai) = get_ai(pai, (afd), (addr)); \
+ if ((ai) == NULL) { \
+ error = EAI_MEMORY; \
+ goto free; \
+ } \
+ } while (/*CONSTCOND*/ 0)
-#define GET_PORT(ai, serv) \
-do { \
- /* external reference: error and label free */ \
- error = get_port((ai), (serv), 0); \
- if (error != 0) \
- goto free; \
-} while (/*CONSTCOND*/0)
+#define GET_PORT(ai, serv) \
+ do { \
+ /* external reference: error and label free */ \
+ error = get_port((ai), (serv), 0); \
+ if (error != 0) goto free; \
+ } while (/*CONSTCOND*/ 0)
-#define GET_CANONNAME(ai, str) \
-do { \
- /* external reference: pai, error and label free */ \
- error = get_canonname(pai, (ai), (str)); \
- if (error != 0) \
- goto free; \
-} while (/*CONSTCOND*/0)
+#define GET_CANONNAME(ai, str) \
+ do { \
+ /* external reference: pai, error and label free */ \
+ error = get_canonname(pai, (ai), (str)); \
+ if (error != 0) goto free; \
+ } while (/*CONSTCOND*/ 0)
-#define ERR(err) \
-do { \
- /* external reference: error, and label bad */ \
- error = (err); \
- goto bad; \
- /*NOTREACHED*/ \
-} while (/*CONSTCOND*/0)
+#define ERR(err) \
+ do { \
+ /* external reference: error, and label bad */ \
+ error = (err); \
+ goto bad; \
+ /*NOTREACHED*/ \
+ } while (/*CONSTCOND*/ 0)
-#define MATCH_FAMILY(x, y, w) \
- ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || \
- (y) == PF_UNSPEC)))
-#define MATCH(x, y, w) \
- ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
+#define MATCH_FAMILY(x, y, w) \
+ ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
+#define MATCH(x, y, w) ((x) == (y) || (/*CONSTCOND*/ (w) && ((x) == ANY || (y) == ANY)))
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
-const char *
-gai_strerror(int ecode)
-{
- if (ecode < 0 || ecode > EAI_MAX)
- ecode = EAI_MAX;
- return ai_errlist[ecode];
+const char* gai_strerror(int ecode) {
+ if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
+ return ai_errlist[ecode];
}
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
-void
-freeaddrinfo(struct addrinfo *ai)
-{
- struct addrinfo *next;
+void freeaddrinfo(struct addrinfo* ai) {
+ struct addrinfo* next;
#if defined(__BIONIC__)
- if (ai == NULL) return;
+ if (ai == NULL) return;
#else
- _DIAGASSERT(ai != NULL);
+ _DIAGASSERT(ai != NULL);
#endif
- do {
- next = ai->ai_next;
- if (ai->ai_canonname)
- free(ai->ai_canonname);
- /* no need to free(ai->ai_addr) */
- free(ai);
- ai = next;
- } while (ai);
+ do {
+ next = ai->ai_next;
+ if (ai->ai_canonname) free(ai->ai_canonname);
+ /* no need to free(ai->ai_addr) */
+ free(ai);
+ ai = next;
+ } while (ai);
}
-static int
-str2number(const char *p)
-{
- char *ep;
- unsigned long v;
+static int str2number(const char* p) {
+ char* ep;
+ unsigned long v;
- assert(p != NULL);
+ assert(p != NULL);
- if (*p == '\0')
- return -1;
- ep = NULL;
- errno = 0;
- v = strtoul(p, &ep, 10);
- if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
- return v;
- else
- return -1;
+ if (*p == '\0') return -1;
+ ep = NULL;
+ errno = 0;
+ v = strtoul(p, &ep, 10);
+ if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
+ return v;
+ else
+ return -1;
}
/*
@@ -370,488 +340,430 @@
* on the local system". However, bionic doesn't currently support getifaddrs,
* so checking for connectivity is the next best thing.
*/
-static int
-_have_ipv6(unsigned mark, uid_t uid) {
- static const struct sockaddr_in6 sin6_test = {
- .sin6_family = AF_INET6,
- .sin6_addr.s6_addr = { // 2000::
- 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
- };
- sockaddr_union addr = { .in6 = sin6_test };
- return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
+static int _have_ipv6(unsigned mark, uid_t uid) {
+ static const struct sockaddr_in6 sin6_test = {
+ .sin6_family = AF_INET6,
+ .sin6_addr.s6_addr = {// 2000::
+ 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
+ sockaddr_union addr = {.in6 = sin6_test};
+ return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
}
-static int
-_have_ipv4(unsigned mark, uid_t uid) {
- static const struct sockaddr_in sin_test = {
- .sin_family = AF_INET,
- .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
- };
- sockaddr_union addr = { .in = sin_test };
- return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
+static int _have_ipv4(unsigned mark, uid_t uid) {
+ static const struct sockaddr_in sin_test = {
+ .sin_family = AF_INET,
+ .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
+ };
+ sockaddr_union addr = {.in = sin_test};
+ return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
}
bool readBE32(FILE* fp, int32_t* result) {
- int32_t tmp;
- if (fread(&tmp, sizeof(tmp), 1, fp) != 1) {
- return false;
- }
- *result = ntohl(tmp);
- return true;
+ int32_t tmp;
+ if (fread(&tmp, sizeof(tmp), 1, fp) != 1) {
+ return false;
+ }
+ *result = ntohl(tmp);
+ return true;
}
#if defined(__ANDROID__)
// Returns 0 on success, else returns on error.
-static int
-android_getaddrinfo_proxy(
- const char *hostname, const char *servname,
- const struct addrinfo *hints, struct addrinfo **res, unsigned netid)
-{
- int success = 0;
+static int android_getaddrinfo_proxy(const char* hostname, const char* servname,
+ const struct addrinfo* hints, struct addrinfo** res,
+ unsigned netid) {
+ int success = 0;
- // Clear this at start, as we use its non-NULLness later (in the
- // error path) to decide if we have to free up any memory we
- // allocated in the process (before failing).
- *res = NULL;
+ // Clear this at start, as we use its non-NULLness later (in the
+ // error path) to decide if we have to free up any memory we
+ // allocated in the process (before failing).
+ *res = NULL;
- // Bogus things we can't serialize. Don't use the proxy. These will fail - let them.
- if ((hostname != NULL &&
- strcspn(hostname, " \n\r\t^'\"") != strlen(hostname)) ||
- (servname != NULL &&
- strcspn(servname, " \n\r\t^'\"") != strlen(servname))) {
- return EAI_NODATA;
- }
+ // Bogus things we can't serialize. Don't use the proxy. These will fail - let them.
+ if ((hostname != NULL && strcspn(hostname, " \n\r\t^'\"") != strlen(hostname)) ||
+ (servname != NULL && strcspn(servname, " \n\r\t^'\"") != strlen(servname))) {
+ return EAI_NODATA;
+ }
- FILE* proxy = android_open_proxy();
- if (proxy == NULL) {
- return EAI_SYSTEM;
- }
+ FILE* proxy = android_open_proxy();
+ if (proxy == NULL) {
+ return EAI_SYSTEM;
+ }
- netid = __netdClientDispatch.netIdForResolv(netid);
+ netid = __netdClientDispatch.netIdForResolv(netid);
- // Send the request.
- if (fprintf(proxy, "getaddrinfo %s %s %d %d %d %d %u",
- hostname == NULL ? "^" : hostname,
- servname == NULL ? "^" : servname,
- hints == NULL ? -1 : hints->ai_flags,
- hints == NULL ? -1 : hints->ai_family,
- hints == NULL ? -1 : hints->ai_socktype,
- hints == NULL ? -1 : hints->ai_protocol,
- netid) < 0) {
- goto exit;
- }
- // literal NULL byte at end, required by FrameworkListener
- if (fputc(0, proxy) == EOF ||
- fflush(proxy) != 0) {
- goto exit;
- }
+ // Send the request.
+ if (fprintf(proxy, "getaddrinfo %s %s %d %d %d %d %u", hostname == NULL ? "^" : hostname,
+ servname == NULL ? "^" : servname, hints == NULL ? -1 : hints->ai_flags,
+ hints == NULL ? -1 : hints->ai_family, hints == NULL ? -1 : hints->ai_socktype,
+ hints == NULL ? -1 : hints->ai_protocol, netid) < 0) {
+ goto exit;
+ }
+ // literal NULL byte at end, required by FrameworkListener
+ if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
+ goto exit;
+ }
- char buf[4];
- // read result code for gethostbyaddr
- if (fread(buf, 1, sizeof(buf), proxy) != sizeof(buf)) {
- goto exit;
- }
+ char buf[4];
+ // read result code for gethostbyaddr
+ if (fread(buf, 1, sizeof(buf), proxy) != sizeof(buf)) {
+ goto exit;
+ }
- int result_code = (int)strtol(buf, NULL, 10);
- // verify the code itself
- if (result_code != DnsProxyQueryResult) {
- fread(buf, 1, sizeof(buf), proxy);
- goto exit;
- }
+ int result_code = (int) strtol(buf, NULL, 10);
+ // verify the code itself
+ if (result_code != DnsProxyQueryResult) {
+ fread(buf, 1, sizeof(buf), proxy);
+ goto exit;
+ }
- struct addrinfo* ai = NULL;
- struct addrinfo** nextres = res;
- while (1) {
- int32_t have_more;
- if (!readBE32(proxy, &have_more)) {
- break;
- }
- if (have_more == 0) {
- success = 1;
- break;
- }
+ struct addrinfo* ai = NULL;
+ struct addrinfo** nextres = res;
+ while (1) {
+ int32_t have_more;
+ if (!readBE32(proxy, &have_more)) {
+ break;
+ }
+ if (have_more == 0) {
+ success = 1;
+ break;
+ }
- struct addrinfo* ai = calloc(1, sizeof(struct addrinfo) + sizeof(struct sockaddr_storage));
- if (ai == NULL) {
- break;
- }
- ai->ai_addr = (struct sockaddr*)(ai + 1);
+ struct addrinfo* ai = calloc(1, sizeof(struct addrinfo) + sizeof(struct sockaddr_storage));
+ if (ai == NULL) {
+ break;
+ }
+ ai->ai_addr = (struct sockaddr*) (ai + 1);
- // struct addrinfo {
- // int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
- // int ai_family; /* PF_xxx */
- // int ai_socktype; /* SOCK_xxx */
- // int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
- // socklen_t ai_addrlen; /* length of ai_addr */
- // char *ai_canonname; /* canonical name for hostname */
- // struct sockaddr *ai_addr; /* binary address */
- // struct addrinfo *ai_next; /* next structure in linked list */
- // };
+ // struct addrinfo {
+ // int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
+ // int ai_family; /* PF_xxx */
+ // int ai_socktype; /* SOCK_xxx */
+ // int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
+ // socklen_t ai_addrlen; /* length of ai_addr */
+ // char *ai_canonname; /* canonical name for hostname */
+ // struct sockaddr *ai_addr; /* binary address */
+ // struct addrinfo *ai_next; /* next structure in linked list */
+ // };
- // Read the struct piece by piece because we might be a 32-bit process
- // talking to a 64-bit netd.
- int32_t addr_len;
- bool success =
- readBE32(proxy, &ai->ai_flags) &&
- readBE32(proxy, &ai->ai_family) &&
- readBE32(proxy, &ai->ai_socktype) &&
- readBE32(proxy, &ai->ai_protocol) &&
- readBE32(proxy, &addr_len);
- if (!success) {
- break;
- }
+ // Read the struct piece by piece because we might be a 32-bit process
+ // talking to a 64-bit netd.
+ int32_t addr_len;
+ bool success = readBE32(proxy, &ai->ai_flags) && readBE32(proxy, &ai->ai_family) &&
+ readBE32(proxy, &ai->ai_socktype) && readBE32(proxy, &ai->ai_protocol) &&
+ readBE32(proxy, &addr_len);
+ if (!success) {
+ break;
+ }
- // Set ai_addrlen and read the ai_addr data.
- ai->ai_addrlen = addr_len;
- if (addr_len != 0) {
- if ((size_t) addr_len > sizeof(struct sockaddr_storage)) {
- // Bogus; too big.
- break;
- }
- if (fread(ai->ai_addr, addr_len, 1, proxy) != 1) {
- break;
- }
- }
+ // Set ai_addrlen and read the ai_addr data.
+ ai->ai_addrlen = addr_len;
+ if (addr_len != 0) {
+ if ((size_t) addr_len > sizeof(struct sockaddr_storage)) {
+ // Bogus; too big.
+ break;
+ }
+ if (fread(ai->ai_addr, addr_len, 1, proxy) != 1) {
+ break;
+ }
+ }
- // The string for ai_cannonname.
- int32_t name_len;
- if (!readBE32(proxy, &name_len)) {
- break;
- }
- if (name_len != 0) {
- ai->ai_canonname = (char*) malloc(name_len);
- if (fread(ai->ai_canonname, name_len, 1, proxy) != 1) {
- break;
- }
- if (ai->ai_canonname[name_len - 1] != '\0') {
- // The proxy should be returning this
- // NULL-terminated.
- break;
- }
- }
+ // The string for ai_cannonname.
+ int32_t name_len;
+ if (!readBE32(proxy, &name_len)) {
+ break;
+ }
+ if (name_len != 0) {
+ ai->ai_canonname = (char*) malloc(name_len);
+ if (fread(ai->ai_canonname, name_len, 1, proxy) != 1) {
+ break;
+ }
+ if (ai->ai_canonname[name_len - 1] != '\0') {
+ // The proxy should be returning this
+ // NULL-terminated.
+ break;
+ }
+ }
- *nextres = ai;
- nextres = &ai->ai_next;
- ai = NULL;
- }
+ *nextres = ai;
+ nextres = &ai->ai_next;
+ ai = NULL;
+ }
- if (ai != NULL) {
- // Clean up partially-built addrinfo that we never ended up
- // attaching to the response.
- freeaddrinfo(ai);
- }
+ if (ai != NULL) {
+ // Clean up partially-built addrinfo that we never ended up
+ // attaching to the response.
+ freeaddrinfo(ai);
+ }
exit:
- if (proxy != NULL) {
- fclose(proxy);
- }
+ if (proxy != NULL) {
+ fclose(proxy);
+ }
- if (success) {
- return 0;
- }
+ if (success) {
+ return 0;
+ }
- // Proxy failed;
- // clean up memory we might've allocated.
- if (*res) {
- freeaddrinfo(*res);
- *res = NULL;
- }
- return EAI_NODATA;
+ // Proxy failed;
+ // clean up memory we might've allocated.
+ if (*res) {
+ freeaddrinfo(*res);
+ *res = NULL;
+ }
+ return EAI_NODATA;
}
#endif
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
-int
-getaddrinfo(const char *hostname, const char *servname,
- const struct addrinfo *hints, struct addrinfo **res)
-{
- return android_getaddrinfofornet(hostname, servname, hints, NETID_UNSET, MARK_UNSET, res);
+int getaddrinfo(const char* hostname, const char* servname, const struct addrinfo* hints,
+ struct addrinfo** res) {
+ return android_getaddrinfofornet(hostname, servname, hints, NETID_UNSET, MARK_UNSET, res);
}
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
-int
-android_getaddrinfofornet(const char *hostname, const char *servname,
- const struct addrinfo *hints, unsigned netid, unsigned mark, struct addrinfo **res)
-{
- struct android_net_context netcontext = {
- .app_netid = netid,
- .app_mark = mark,
- .dns_netid = netid,
- .dns_mark = mark,
- .uid = NET_CONTEXT_INVALID_UID,
- };
- return android_getaddrinfofornetcontext(hostname, servname, hints, &netcontext, res);
+int android_getaddrinfofornet(const char* hostname, const char* servname,
+ const struct addrinfo* hints, unsigned netid, unsigned mark,
+ struct addrinfo** res) {
+ struct android_net_context netcontext = {
+ .app_netid = netid,
+ .app_mark = mark,
+ .dns_netid = netid,
+ .dns_mark = mark,
+ .uid = NET_CONTEXT_INVALID_UID,
+ };
+ return android_getaddrinfofornetcontext(hostname, servname, hints, &netcontext, res);
}
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
-int
-android_getaddrinfofornetcontext(const char *hostname, const char *servname,
- const struct addrinfo *hints, const struct android_net_context *netcontext,
- struct addrinfo **res)
-{
- struct addrinfo sentinel;
- struct addrinfo *cur;
- int error = 0;
- struct addrinfo ai;
- struct addrinfo ai0;
- struct addrinfo *pai;
- const struct explore *ex;
+int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
+ const struct addrinfo* hints,
+ const struct android_net_context* netcontext,
+ struct addrinfo** res) {
+ struct addrinfo sentinel;
+ struct addrinfo* cur;
+ int error = 0;
+ struct addrinfo ai;
+ struct addrinfo ai0;
+ struct addrinfo* pai;
+ const struct explore* ex;
- /* hostname is allowed to be NULL */
- /* servname is allowed to be NULL */
- /* hints is allowed to be NULL */
- assert(res != NULL);
- assert(netcontext != NULL);
- memset(&sentinel, 0, sizeof(sentinel));
- cur = &sentinel;
- pai = &ai;
- pai->ai_flags = 0;
- pai->ai_family = PF_UNSPEC;
- pai->ai_socktype = ANY;
- pai->ai_protocol = ANY;
- pai->ai_addrlen = 0;
- pai->ai_canonname = NULL;
- pai->ai_addr = NULL;
- pai->ai_next = NULL;
+ /* hostname is allowed to be NULL */
+ /* servname is allowed to be NULL */
+ /* hints is allowed to be NULL */
+ assert(res != NULL);
+ assert(netcontext != NULL);
+ memset(&sentinel, 0, sizeof(sentinel));
+ cur = &sentinel;
+ pai = &ai;
+ pai->ai_flags = 0;
+ pai->ai_family = PF_UNSPEC;
+ pai->ai_socktype = ANY;
+ pai->ai_protocol = ANY;
+ pai->ai_addrlen = 0;
+ pai->ai_canonname = NULL;
+ pai->ai_addr = NULL;
+ pai->ai_next = NULL;
- if (hostname == NULL && servname == NULL)
- return EAI_NONAME;
- if (hints) {
- /* error check for hints */
- if (hints->ai_addrlen || hints->ai_canonname ||
- hints->ai_addr || hints->ai_next)
- ERR(EAI_BADHINTS); /* xxx */
- if (hints->ai_flags & ~AI_MASK)
- ERR(EAI_BADFLAGS);
- switch (hints->ai_family) {
- case PF_UNSPEC:
- case PF_INET:
+ if (hostname == NULL && servname == NULL) return EAI_NONAME;
+ if (hints) {
+ /* error check for hints */
+ if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next)
+ ERR(EAI_BADHINTS); /* xxx */
+ if (hints->ai_flags & ~AI_MASK) ERR(EAI_BADFLAGS);
+ switch (hints->ai_family) {
+ case PF_UNSPEC:
+ case PF_INET:
#ifdef INET6
- case PF_INET6:
+ case PF_INET6:
#endif
- break;
- default:
- ERR(EAI_FAMILY);
- }
- memcpy(pai, hints, sizeof(*pai));
+ break;
+ default:
+ ERR(EAI_FAMILY);
+ }
+ memcpy(pai, hints, sizeof(*pai));
- /*
- * if both socktype/protocol are specified, check if they
- * are meaningful combination.
- */
- if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
- for (ex = explore; ex->e_af >= 0; ex++) {
- if (pai->ai_family != ex->e_af)
- continue;
- if (ex->e_socktype == ANY)
- continue;
- if (ex->e_protocol == ANY)
- continue;
- if (pai->ai_socktype == ex->e_socktype
- && pai->ai_protocol != ex->e_protocol) {
- ERR(EAI_BADHINTS);
- }
- }
- }
- }
+ /*
+ * if both socktype/protocol are specified, check if they
+ * are meaningful combination.
+ */
+ if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
+ for (ex = explore; ex->e_af >= 0; ex++) {
+ if (pai->ai_family != ex->e_af) continue;
+ if (ex->e_socktype == ANY) continue;
+ if (ex->e_protocol == ANY) continue;
+ if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) {
+ ERR(EAI_BADHINTS);
+ }
+ }
+ }
+ }
- /*
- * check for special cases. (1) numeric servname is disallowed if
- * socktype/protocol are left unspecified. (2) servname is disallowed
- * for raw and other inet{,6} sockets.
- */
- if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
+ /*
+ * check for special cases. (1) numeric servname is disallowed if
+ * socktype/protocol are left unspecified. (2) servname is disallowed
+ * for raw and other inet{,6} sockets.
+ */
+ if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
#ifdef PF_INET6
- || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
+ || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
#endif
- ) {
- ai0 = *pai; /* backup *pai */
+ ) {
+ ai0 = *pai; /* backup *pai */
- if (pai->ai_family == PF_UNSPEC) {
+ if (pai->ai_family == PF_UNSPEC) {
#ifdef PF_INET6
- pai->ai_family = PF_INET6;
+ pai->ai_family = PF_INET6;
#else
- pai->ai_family = PF_INET;
+ pai->ai_family = PF_INET;
#endif
- }
- error = get_portmatch(pai, servname);
- if (error)
- ERR(error);
+ }
+ error = get_portmatch(pai, servname);
+ if (error) ERR(error);
- *pai = ai0;
- }
+ *pai = ai0;
+ }
- ai0 = *pai;
+ ai0 = *pai;
- /* NULL hostname, or numeric hostname */
- for (ex = explore; ex->e_af >= 0; ex++) {
- *pai = ai0;
+ /* NULL hostname, or numeric hostname */
+ for (ex = explore; ex->e_af >= 0; ex++) {
+ *pai = ai0;
- /* PF_UNSPEC entries are prepared for DNS queries only */
- if (ex->e_af == PF_UNSPEC)
- continue;
+ /* PF_UNSPEC entries are prepared for DNS queries only */
+ if (ex->e_af == PF_UNSPEC) continue;
- if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
- continue;
- if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
- continue;
- if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
- continue;
+ if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue;
+ if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
+ if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
- if (pai->ai_family == PF_UNSPEC)
- pai->ai_family = ex->e_af;
- if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
- pai->ai_socktype = ex->e_socktype;
- if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
- pai->ai_protocol = ex->e_protocol;
+ if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af;
+ if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
+ if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
- if (hostname == NULL)
- error = explore_null(pai, servname, &cur->ai_next);
- else
- error = explore_numeric_scope(pai, hostname, servname,
- &cur->ai_next);
+ if (hostname == NULL)
+ error = explore_null(pai, servname, &cur->ai_next);
+ else
+ error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
- if (error)
- goto free;
+ if (error) goto free;
- while (cur->ai_next)
- cur = cur->ai_next;
- }
+ while (cur->ai_next) cur = cur->ai_next;
+ }
- /*
- * XXX
- * If numeric representation of AF1 can be interpreted as FQDN
- * representation of AF2, we need to think again about the code below.
- */
- if (sentinel.ai_next)
- goto good;
+ /*
+ * XXX
+ * If numeric representation of AF1 can be interpreted as FQDN
+ * representation of AF2, we need to think again about the code below.
+ */
+ if (sentinel.ai_next) goto good;
- if (hostname == NULL)
- ERR(EAI_NODATA);
- if (pai->ai_flags & AI_NUMERICHOST)
- ERR(EAI_NONAME);
+ if (hostname == NULL) ERR(EAI_NODATA);
+ if (pai->ai_flags & AI_NUMERICHOST) ERR(EAI_NONAME);
#if defined(__ANDROID__)
- int gai_error = android_getaddrinfo_proxy(
- hostname, servname, hints, res, netcontext->app_netid);
- if (gai_error != EAI_SYSTEM) {
- return gai_error;
- }
+ int gai_error =
+ android_getaddrinfo_proxy(hostname, servname, hints, res, netcontext->app_netid);
+ if (gai_error != EAI_SYSTEM) {
+ return gai_error;
+ }
#endif
- /*
- * hostname as alphabetical name.
- * we would like to prefer AF_INET6 than AF_INET, so we'll make a
- * outer loop by AFs.
- */
- for (ex = explore; ex->e_af >= 0; ex++) {
- *pai = ai0;
+ /*
+ * hostname as alphabetical name.
+ * we would like to prefer AF_INET6 than AF_INET, so we'll make a
+ * outer loop by AFs.
+ */
+ for (ex = explore; ex->e_af >= 0; ex++) {
+ *pai = ai0;
- /* require exact match for family field */
- if (pai->ai_family != ex->e_af)
- continue;
+ /* require exact match for family field */
+ if (pai->ai_family != ex->e_af) continue;
- if (!MATCH(pai->ai_socktype, ex->e_socktype,
- WILD_SOCKTYPE(ex))) {
- continue;
- }
- if (!MATCH(pai->ai_protocol, ex->e_protocol,
- WILD_PROTOCOL(ex))) {
- continue;
- }
+ if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
+ continue;
+ }
+ if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
+ continue;
+ }
- if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
- pai->ai_socktype = ex->e_socktype;
- if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
- pai->ai_protocol = ex->e_protocol;
+ if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
+ if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
- error = explore_fqdn(
- pai, hostname, servname, &cur->ai_next, netcontext);
+ error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext);
- while (cur && cur->ai_next)
- cur = cur->ai_next;
- }
+ while (cur && cur->ai_next) cur = cur->ai_next;
+ }
- /* XXX */
- if (sentinel.ai_next)
- error = 0;
+ /* XXX */
+ if (sentinel.ai_next) error = 0;
- if (error)
- goto free;
- if (error == 0) {
- if (sentinel.ai_next) {
- good:
- *res = sentinel.ai_next;
- return SUCCESS;
- } else
- error = EAI_FAIL;
- }
- free:
- bad:
- if (sentinel.ai_next)
- freeaddrinfo(sentinel.ai_next);
- *res = NULL;
- return error;
+ if (error) goto free;
+ if (error == 0) {
+ if (sentinel.ai_next) {
+ good:
+ *res = sentinel.ai_next;
+ return SUCCESS;
+ } else
+ error = EAI_FAIL;
+ }
+free:
+bad:
+ if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
+ *res = NULL;
+ return error;
}
/*
* FQDN hostname, DNS lookup
*/
-static int
-explore_fqdn(const struct addrinfo *pai, const char *hostname,
- const char *servname, struct addrinfo **res,
- const struct android_net_context *netcontext)
-{
- struct addrinfo *result;
- struct addrinfo *cur;
- int error = 0;
- static const ns_dtab dtab[] = {
- NS_FILES_CB(_files_getaddrinfo, NULL)
- { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */
- NS_NIS_CB(_yp_getaddrinfo, NULL)
- { 0, 0, 0 }
- };
+static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
+ struct addrinfo** res, const struct android_net_context* netcontext) {
+ struct addrinfo* result;
+ struct addrinfo* cur;
+ int error = 0;
+ static const ns_dtab dtab[] = {NS_FILES_CB(_files_getaddrinfo, NULL){
+ NSSRC_DNS, _dns_getaddrinfo, NULL}, /* force -DHESIOD */
+ NS_NIS_CB(_yp_getaddrinfo, NULL){0, 0, 0}};
- assert(pai != NULL);
- /* hostname may be NULL */
- /* servname may be NULL */
- assert(res != NULL);
+ assert(pai != NULL);
+ /* hostname may be NULL */
+ /* servname may be NULL */
+ assert(res != NULL);
- result = NULL;
+ result = NULL;
- /*
- * if the servname does not match socktype/protocol, ignore it.
- */
- if (get_portmatch(pai, servname) != 0)
- return 0;
+ /*
+ * if the servname does not match socktype/protocol, ignore it.
+ */
+ if (get_portmatch(pai, servname) != 0) return 0;
- switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
- default_dns_files, hostname, pai, netcontext)) {
- case NS_TRYAGAIN:
- error = EAI_AGAIN;
- goto free;
- case NS_UNAVAIL:
- error = EAI_FAIL;
- goto free;
- case NS_NOTFOUND:
- error = EAI_NODATA;
- goto free;
- case NS_SUCCESS:
- error = 0;
- for (cur = result; cur; cur = cur->ai_next) {
- GET_PORT(cur, servname);
- /* canonname should be filled already */
- }
- break;
- }
+ switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", default_dns_files, hostname, pai,
+ netcontext)) {
+ case NS_TRYAGAIN:
+ error = EAI_AGAIN;
+ goto free;
+ case NS_UNAVAIL:
+ error = EAI_FAIL;
+ goto free;
+ case NS_NOTFOUND:
+ error = EAI_NODATA;
+ goto free;
+ case NS_SUCCESS:
+ error = 0;
+ for (cur = result; cur; cur = cur->ai_next) {
+ GET_PORT(cur, servname);
+ /* canonname should be filled already */
+ }
+ break;
+ }
- *res = result;
+ *res = result;
- return 0;
+ return 0;
free:
- if (result)
- freeaddrinfo(result);
- return error;
+ if (result) freeaddrinfo(result);
+ return error;
}
/*
@@ -859,102 +771,91 @@
* passive socket -> anyaddr (0.0.0.0 or ::)
* non-passive socket -> localhost (127.0.0.1 or ::1)
*/
-static int
-explore_null(const struct addrinfo *pai, const char *servname,
- struct addrinfo **res)
-{
- int s;
- const struct afd *afd;
- struct addrinfo *cur;
- struct addrinfo sentinel;
- int error;
+static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
+ int s;
+ const struct afd* afd;
+ struct addrinfo* cur;
+ struct addrinfo sentinel;
+ int error;
- assert(pai != NULL);
- /* servname may be NULL */
- assert(res != NULL);
+ assert(pai != NULL);
+ /* servname may be NULL */
+ assert(res != NULL);
- *res = NULL;
- sentinel.ai_next = NULL;
- cur = &sentinel;
+ *res = NULL;
+ sentinel.ai_next = NULL;
+ cur = &sentinel;
- /*
- * filter out AFs that are not supported by the kernel
- * XXX errno?
- */
- s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
- if (s < 0) {
- if (errno != EMFILE)
- return 0;
- } else
- close(s);
+ /*
+ * filter out AFs that are not supported by the kernel
+ * XXX errno?
+ */
+ s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+ if (s < 0) {
+ if (errno != EMFILE) return 0;
+ } else
+ close(s);
- /*
- * if the servname does not match socktype/protocol, ignore it.
- */
- if (get_portmatch(pai, servname) != 0)
- return 0;
+ /*
+ * if the servname does not match socktype/protocol, ignore it.
+ */
+ if (get_portmatch(pai, servname) != 0) return 0;
- afd = find_afd(pai->ai_family);
- if (afd == NULL)
- return 0;
+ afd = find_afd(pai->ai_family);
+ if (afd == NULL) return 0;
- if (pai->ai_flags & AI_PASSIVE) {
- GET_AI(cur->ai_next, afd, afd->a_addrany);
- /* xxx meaningless?
- * GET_CANONNAME(cur->ai_next, "anyaddr");
- */
- GET_PORT(cur->ai_next, servname);
- } else {
- GET_AI(cur->ai_next, afd, afd->a_loopback);
- /* xxx meaningless?
- * GET_CANONNAME(cur->ai_next, "localhost");
- */
- GET_PORT(cur->ai_next, servname);
- }
- cur = cur->ai_next;
+ if (pai->ai_flags & AI_PASSIVE) {
+ GET_AI(cur->ai_next, afd, afd->a_addrany);
+ /* xxx meaningless?
+ * GET_CANONNAME(cur->ai_next, "anyaddr");
+ */
+ GET_PORT(cur->ai_next, servname);
+ } else {
+ GET_AI(cur->ai_next, afd, afd->a_loopback);
+ /* xxx meaningless?
+ * GET_CANONNAME(cur->ai_next, "localhost");
+ */
+ GET_PORT(cur->ai_next, servname);
+ }
+ cur = cur->ai_next;
- *res = sentinel.ai_next;
- return 0;
+ *res = sentinel.ai_next;
+ return 0;
free:
- if (sentinel.ai_next)
- freeaddrinfo(sentinel.ai_next);
- return error;
+ if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
+ return error;
}
/*
* numeric hostname
*/
-static int
-explore_numeric(const struct addrinfo *pai, const char *hostname,
- const char *servname, struct addrinfo **res, const char *canonname)
-{
- const struct afd *afd;
- struct addrinfo *cur;
- struct addrinfo sentinel;
- int error;
- char pton[PTON_MAX];
+static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
+ struct addrinfo** res, const char* canonname) {
+ const struct afd* afd;
+ struct addrinfo* cur;
+ struct addrinfo sentinel;
+ int error;
+ char pton[PTON_MAX];
- assert(pai != NULL);
- /* hostname may be NULL */
- /* servname may be NULL */
- assert(res != NULL);
+ assert(pai != NULL);
+ /* hostname may be NULL */
+ /* servname may be NULL */
+ assert(res != NULL);
- *res = NULL;
- sentinel.ai_next = NULL;
- cur = &sentinel;
+ *res = NULL;
+ sentinel.ai_next = NULL;
+ cur = &sentinel;
- /*
- * if the servname does not match socktype/protocol, ignore it.
- */
- if (get_portmatch(pai, servname) != 0)
- return 0;
+ /*
+ * if the servname does not match socktype/protocol, ignore it.
+ */
+ if (get_portmatch(pai, servname) != 0) return 0;
- afd = find_afd(pai->ai_family);
- if (afd == NULL)
- return 0;
+ afd = find_afd(pai->ai_family);
+ if (afd == NULL) return 0;
- switch (afd->a_af) {
+ switch (afd->a_af) {
#if 0 /*X/Open spec*/
case AF_INET:
if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
@@ -977,593 +878,544 @@
}
break;
#endif
- default:
- if (inet_pton(afd->a_af, hostname, pton) == 1) {
- if (pai->ai_family == afd->a_af ||
- pai->ai_family == PF_UNSPEC /*?*/) {
- GET_AI(cur->ai_next, afd, pton);
- GET_PORT(cur->ai_next, servname);
- if ((pai->ai_flags & AI_CANONNAME)) {
- /*
- * Set the numeric address itself as
- * the canonical name, based on a
- * clarification in rfc2553bis-03.
- */
- GET_CANONNAME(cur->ai_next, canonname);
- }
- while (cur->ai_next)
- cur = cur->ai_next;
- } else
- ERR(EAI_FAMILY); /*xxx*/
- }
- break;
- }
+ default:
+ if (inet_pton(afd->a_af, hostname, pton) == 1) {
+ if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
+ GET_AI(cur->ai_next, afd, pton);
+ GET_PORT(cur->ai_next, servname);
+ if ((pai->ai_flags & AI_CANONNAME)) {
+ /*
+ * Set the numeric address itself as
+ * the canonical name, based on a
+ * clarification in rfc2553bis-03.
+ */
+ GET_CANONNAME(cur->ai_next, canonname);
+ }
+ while (cur->ai_next) cur = cur->ai_next;
+ } else
+ ERR(EAI_FAMILY); /*xxx*/
+ }
+ break;
+ }
- *res = sentinel.ai_next;
- return 0;
+ *res = sentinel.ai_next;
+ return 0;
free:
bad:
- if (sentinel.ai_next)
- freeaddrinfo(sentinel.ai_next);
- return error;
+ if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next);
+ return error;
}
/*
* numeric hostname with scope
*/
-static int
-explore_numeric_scope(const struct addrinfo *pai, const char *hostname,
- const char *servname, struct addrinfo **res)
-{
+static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
+ const char* servname, struct addrinfo** res) {
#if !defined(SCOPE_DELIMITER) || !defined(INET6)
- return explore_numeric(pai, hostname, servname, res, hostname);
+ return explore_numeric(pai, hostname, servname, res, hostname);
#else
- const struct afd *afd;
- struct addrinfo *cur;
- int error;
- char *cp, *hostname2 = NULL, *scope, *addr;
- struct sockaddr_in6 *sin6;
+ const struct afd* afd;
+ struct addrinfo* cur;
+ int error;
+ char *cp, *hostname2 = NULL, *scope, *addr;
+ struct sockaddr_in6* sin6;
- assert(pai != NULL);
- /* hostname may be NULL */
- /* servname may be NULL */
- assert(res != NULL);
+ assert(pai != NULL);
+ /* hostname may be NULL */
+ /* servname may be NULL */
+ assert(res != NULL);
- /*
- * if the servname does not match socktype/protocol, ignore it.
- */
- if (get_portmatch(pai, servname) != 0)
- return 0;
+ /*
+ * if the servname does not match socktype/protocol, ignore it.
+ */
+ if (get_portmatch(pai, servname) != 0) return 0;
- afd = find_afd(pai->ai_family);
- if (afd == NULL)
- return 0;
+ afd = find_afd(pai->ai_family);
+ if (afd == NULL) return 0;
- if (!afd->a_scoped)
- return explore_numeric(pai, hostname, servname, res, hostname);
+ if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
- cp = strchr(hostname, SCOPE_DELIMITER);
- if (cp == NULL)
- return explore_numeric(pai, hostname, servname, res, hostname);
+ cp = strchr(hostname, SCOPE_DELIMITER);
+ if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
- /*
- * Handle special case of <scoped_address><delimiter><scope id>
- */
- hostname2 = strdup(hostname);
- if (hostname2 == NULL)
- return EAI_MEMORY;
- /* terminate at the delimiter */
- hostname2[cp - hostname] = '\0';
- addr = hostname2;
- scope = cp + 1;
+ /*
+ * Handle special case of <scoped_address><delimiter><scope id>
+ */
+ hostname2 = strdup(hostname);
+ if (hostname2 == NULL) return EAI_MEMORY;
+ /* terminate at the delimiter */
+ hostname2[cp - hostname] = '\0';
+ addr = hostname2;
+ scope = cp + 1;
- error = explore_numeric(pai, addr, servname, res, hostname);
- if (error == 0) {
- u_int32_t scopeid;
+ error = explore_numeric(pai, addr, servname, res, hostname);
+ if (error == 0) {
+ u_int32_t scopeid;
- for (cur = *res; cur; cur = cur->ai_next) {
- if (cur->ai_family != AF_INET6)
- continue;
- sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
- if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
- free(hostname2);
- return(EAI_NODATA); /* XXX: is return OK? */
- }
- sin6->sin6_scope_id = scopeid;
- }
- }
+ for (cur = *res; cur; cur = cur->ai_next) {
+ if (cur->ai_family != AF_INET6) continue;
+ sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
+ if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
+ free(hostname2);
+ return (EAI_NODATA); /* XXX: is return OK? */
+ }
+ sin6->sin6_scope_id = scopeid;
+ }
+ }
- free(hostname2);
+ free(hostname2);
- return error;
+ return error;
#endif
}
-static int
-get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str)
-{
+static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
+ assert(pai != NULL);
+ assert(ai != NULL);
+ assert(str != NULL);
- assert(pai != NULL);
- assert(ai != NULL);
- assert(str != NULL);
-
- if ((pai->ai_flags & AI_CANONNAME) != 0) {
- ai->ai_canonname = strdup(str);
- if (ai->ai_canonname == NULL)
- return EAI_MEMORY;
- }
- return 0;
+ if ((pai->ai_flags & AI_CANONNAME) != 0) {
+ ai->ai_canonname = strdup(str);
+ if (ai->ai_canonname == NULL) return EAI_MEMORY;
+ }
+ return 0;
}
-static struct addrinfo *
-get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
-{
- char *p;
- struct addrinfo *ai;
+static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
+ const char* addr) {
+ char* p;
+ struct addrinfo* ai;
- assert(pai != NULL);
- assert(afd != NULL);
- assert(addr != NULL);
+ assert(pai != NULL);
+ assert(afd != NULL);
+ assert(addr != NULL);
- ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
- + (afd->a_socklen));
- if (ai == NULL)
- return NULL;
+ ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + (afd->a_socklen));
+ if (ai == NULL) return NULL;
- memcpy(ai, pai, sizeof(struct addrinfo));
- ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
- memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
+ memcpy(ai, pai, sizeof(struct addrinfo));
+ ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
+ memset(ai->ai_addr, 0, (size_t) afd->a_socklen);
#ifdef HAVE_SA_LEN
- ai->ai_addr->sa_len = afd->a_socklen;
+ ai->ai_addr->sa_len = afd->a_socklen;
#endif
- ai->ai_addrlen = afd->a_socklen;
-#if defined (__alpha__) || (defined(__i386__) && defined(_LP64)) || defined(__sparc64__)
- ai->__ai_pad0 = 0;
+ ai->ai_addrlen = afd->a_socklen;
+#if defined(__alpha__) || (defined(__i386__) && defined(_LP64)) || defined(__sparc64__)
+ ai->__ai_pad0 = 0;
#endif
- ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
- p = (char *)(void *)(ai->ai_addr);
- memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
- return ai;
+ ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
+ p = (char*) (void*) (ai->ai_addr);
+ memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
+ return ai;
}
-static int
-get_portmatch(const struct addrinfo *ai, const char *servname)
-{
+static int get_portmatch(const struct addrinfo* ai, const char* servname) {
+ assert(ai != NULL);
+ /* servname may be NULL */
- assert(ai != NULL);
- /* servname may be NULL */
-
- return get_port(ai, servname, 1);
+ return get_port(ai, servname, 1);
}
-static int
-get_port(const struct addrinfo *ai, const char *servname, int matchonly)
-{
- const char *proto;
- struct servent *sp;
- int port;
- int allownumeric;
+static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
+ const char* proto;
+ struct servent* sp;
+ int port;
+ int allownumeric;
- assert(ai != NULL);
- /* servname may be NULL */
+ assert(ai != NULL);
+ /* servname may be NULL */
- if (servname == NULL)
- return 0;
- switch (ai->ai_family) {
- case AF_INET:
+ if (servname == NULL) return 0;
+ switch (ai->ai_family) {
+ case AF_INET:
#ifdef AF_INET6
- case AF_INET6:
+ case AF_INET6:
#endif
- break;
- default:
- return 0;
- }
+ break;
+ default:
+ return 0;
+ }
- switch (ai->ai_socktype) {
- case SOCK_RAW:
- return EAI_SERVICE;
- case SOCK_DGRAM:
- case SOCK_STREAM:
- allownumeric = 1;
- break;
- case ANY:
-#if 1 /* ANDROID-SPECIFIC CHANGE TO MATCH GLIBC */
- allownumeric = 1;
+ switch (ai->ai_socktype) {
+ case SOCK_RAW:
+ return EAI_SERVICE;
+ case SOCK_DGRAM:
+ case SOCK_STREAM:
+ allownumeric = 1;
+ break;
+ case ANY:
+#if 1 /* ANDROID-SPECIFIC CHANGE TO MATCH GLIBC */
+ allownumeric = 1;
#else
- allownumeric = 0;
+ allownumeric = 0;
#endif
- break;
- default:
- return EAI_SOCKTYPE;
- }
+ break;
+ default:
+ return EAI_SOCKTYPE;
+ }
- port = str2number(servname);
- if (port >= 0) {
- if (!allownumeric)
- return EAI_SERVICE;
- if (port < 0 || port > 65535)
- return EAI_SERVICE;
- port = htons(port);
- } else {
- if (ai->ai_flags & AI_NUMERICSERV)
- return EAI_NONAME;
+ port = str2number(servname);
+ if (port >= 0) {
+ if (!allownumeric) return EAI_SERVICE;
+ if (port < 0 || port > 65535) return EAI_SERVICE;
+ port = htons(port);
+ } else {
+ if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
- switch (ai->ai_socktype) {
- case SOCK_DGRAM:
- proto = "udp";
- break;
- case SOCK_STREAM:
- proto = "tcp";
- break;
- default:
- proto = NULL;
- break;
- }
+ switch (ai->ai_socktype) {
+ case SOCK_DGRAM:
+ proto = "udp";
+ break;
+ case SOCK_STREAM:
+ proto = "tcp";
+ break;
+ default:
+ proto = NULL;
+ break;
+ }
- if ((sp = getservbyname(servname, proto)) == NULL)
- return EAI_SERVICE;
- port = sp->s_port;
- }
+ if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
+ port = sp->s_port;
+ }
- if (!matchonly) {
- switch (ai->ai_family) {
- case AF_INET:
- ((struct sockaddr_in *)(void *)
- ai->ai_addr)->sin_port = port;
- break;
+ if (!matchonly) {
+ switch (ai->ai_family) {
+ case AF_INET:
+ ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
+ break;
#ifdef INET6
- case AF_INET6:
- ((struct sockaddr_in6 *)(void *)
- ai->ai_addr)->sin6_port = port;
- break;
+ case AF_INET6:
+ ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
+ break;
#endif
- }
- }
+ }
+ }
- return 0;
+ return 0;
}
-static const struct afd *
-find_afd(int af)
-{
- const struct afd *afd;
+static const struct afd* find_afd(int af) {
+ const struct afd* afd;
- if (af == PF_UNSPEC)
- return NULL;
- for (afd = afdl; afd->a_af; afd++) {
- if (afd->a_af == af)
- return afd;
- }
- return NULL;
+ if (af == PF_UNSPEC) return NULL;
+ for (afd = afdl; afd->a_af; afd++) {
+ if (afd->a_af == af) return afd;
+ }
+ return NULL;
}
#ifdef INET6
/* convert a string to a scope identifier. XXX: IPv6 specific */
-static int
-ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)
-{
- u_long lscopeid;
- struct in6_addr *a6;
- char *ep;
+static int ip6_str2scopeid(char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
+ u_long lscopeid;
+ struct in6_addr* a6;
+ char* ep;
- assert(scope != NULL);
- assert(sin6 != NULL);
- assert(scopeid != NULL);
+ assert(scope != NULL);
+ assert(sin6 != NULL);
+ assert(scopeid != NULL);
- a6 = &sin6->sin6_addr;
+ a6 = &sin6->sin6_addr;
- /* empty scopeid portion is invalid */
- if (*scope == '\0')
- return -1;
+ /* empty scopeid portion is invalid */
+ if (*scope == '\0') return -1;
- if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
- /*
- * We currently assume a one-to-one mapping between links
- * and interfaces, so we simply use interface indices for
- * like-local scopes.
- */
- *scopeid = if_nametoindex(scope);
- if (*scopeid == 0)
- goto trynumeric;
- return 0;
- }
+ if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
+ /*
+ * We currently assume a one-to-one mapping between links
+ * and interfaces, so we simply use interface indices for
+ * like-local scopes.
+ */
+ *scopeid = if_nametoindex(scope);
+ if (*scopeid == 0) goto trynumeric;
+ return 0;
+ }
- /* still unclear about literal, allow numeric only - placeholder */
- if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
- goto trynumeric;
- if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
- goto trynumeric;
- else
- goto trynumeric; /* global */
+ /* still unclear about literal, allow numeric only - placeholder */
+ if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
+ if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
+ goto trynumeric;
+ else
+ goto trynumeric; /* global */
- /* try to convert to a numeric id as a last resort */
- trynumeric:
- errno = 0;
- lscopeid = strtoul(scope, &ep, 10);
- *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
- if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
- return 0;
- else
- return -1;
+ /* try to convert to a numeric id as a last resort */
+trynumeric:
+ errno = 0;
+ lscopeid = strtoul(scope, &ep, 10);
+ *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
+ if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
+ return 0;
+ else
+ return -1;
}
#endif
/* code duplicate with gethnamaddr.c */
-static const char AskedForGot[] =
- "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
+static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
-#define BOUNDED_INCR(x) \
- do { \
- BOUNDS_CHECK(cp, x); \
- cp += (x); \
- } while (/*CONSTCOND*/0)
+#define BOUNDED_INCR(x) \
+ do { \
+ BOUNDS_CHECK(cp, x); \
+ cp += (x); \
+ } while (/*CONSTCOND*/ 0)
-#define BOUNDS_CHECK(ptr, count) \
- do { \
- if (eom - (ptr) < (count)) { h_errno = NO_RECOVERY; return NULL; } \
- } while (/*CONSTCOND*/0)
+#define BOUNDS_CHECK(ptr, count) \
+ do { \
+ if (eom - (ptr) < (count)) { \
+ h_errno = NO_RECOVERY; \
+ return NULL; \
+ } \
+ } while (/*CONSTCOND*/ 0)
-static struct addrinfo *
-getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
- const struct addrinfo *pai)
-{
- struct addrinfo sentinel, *cur;
- struct addrinfo ai;
- const struct afd *afd;
- char *canonname;
- const HEADER *hp;
- const u_char *cp;
- int n;
- const u_char *eom;
- char *bp, *ep;
- int type, class, ancount, qdcount;
- int haveanswer, had_error;
- char tbuf[MAXDNAME];
- int (*name_ok) (const char *);
- char hostbuf[8*1024];
+static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
+ const struct addrinfo* pai) {
+ struct addrinfo sentinel, *cur;
+ struct addrinfo ai;
+ const struct afd* afd;
+ char* canonname;
+ const HEADER* hp;
+ const u_char* cp;
+ int n;
+ const u_char* eom;
+ char *bp, *ep;
+ int type, class, ancount, qdcount;
+ int haveanswer, had_error;
+ char tbuf[MAXDNAME];
+ int (*name_ok)(const char*);
+ char hostbuf[8 * 1024];
- assert(answer != NULL);
- assert(qname != NULL);
- assert(pai != NULL);
+ assert(answer != NULL);
+ assert(qname != NULL);
+ assert(pai != NULL);
- memset(&sentinel, 0, sizeof(sentinel));
- cur = &sentinel;
+ memset(&sentinel, 0, sizeof(sentinel));
+ cur = &sentinel;
- canonname = NULL;
- eom = answer->buf + anslen;
- switch (qtype) {
- case T_A:
- case T_AAAA:
- case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
- name_ok = res_hnok;
- break;
- default:
- return NULL; /* XXX should be abort(); */
- }
- /*
- * find first satisfactory answer
- */
- hp = &answer->hdr;
- ancount = ntohs(hp->ancount);
- qdcount = ntohs(hp->qdcount);
- bp = hostbuf;
- ep = hostbuf + sizeof hostbuf;
- cp = answer->buf;
- BOUNDED_INCR(HFIXEDSZ);
- if (qdcount != 1) {
- h_errno = NO_RECOVERY;
- return (NULL);
- }
- n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
- if ((n < 0) || !(*name_ok)(bp)) {
- h_errno = NO_RECOVERY;
- return (NULL);
- }
- BOUNDED_INCR(n + QFIXEDSZ);
- if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
- /* res_send() has already verified that the query name is the
- * same as the one we sent; this just gets the expanded name
- * (i.e., with the succeeding search-domain tacked on).
- */
- n = strlen(bp) + 1; /* for the \0 */
- if (n >= MAXHOSTNAMELEN) {
- h_errno = NO_RECOVERY;
- return (NULL);
- }
- canonname = bp;
- bp += n;
- /* The qname can be abbreviated, but h_name is now absolute. */
- qname = canonname;
- }
- haveanswer = 0;
- had_error = 0;
- while (ancount-- > 0 && cp < eom && !had_error) {
- n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
- if ((n < 0) || !(*name_ok)(bp)) {
- had_error++;
- continue;
- }
- cp += n; /* name */
- BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
- type = _getshort(cp);
- cp += INT16SZ; /* type */
- class = _getshort(cp);
- cp += INT16SZ + INT32SZ; /* class, TTL */
- n = _getshort(cp);
- cp += INT16SZ; /* len */
- BOUNDS_CHECK(cp, n);
- if (class != C_IN) {
- /* XXX - debug? syslog? */
- cp += n;
- continue; /* XXX - had_error++ ? */
- }
- if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
- type == T_CNAME) {
- n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
- if ((n < 0) || !(*name_ok)(tbuf)) {
- had_error++;
- continue;
- }
- cp += n;
- /* Get canonical name. */
- n = strlen(tbuf) + 1; /* for the \0 */
- if (n > ep - bp || n >= MAXHOSTNAMELEN) {
- had_error++;
- continue;
- }
- strlcpy(bp, tbuf, (size_t)(ep - bp));
- canonname = bp;
- bp += n;
- continue;
- }
- if (qtype == T_ANY) {
- if (!(type == T_A || type == T_AAAA)) {
- cp += n;
- continue;
- }
- } else if (type != qtype) {
- if (type != T_KEY && type != T_SIG)
- syslog(LOG_NOTICE|LOG_AUTH,
- "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
- qname, p_class(C_IN), p_type(qtype),
- p_type(type));
- cp += n;
- continue; /* XXX - had_error++ ? */
- }
- switch (type) {
- case T_A:
- case T_AAAA:
- if (strcasecmp(canonname, bp) != 0) {
- syslog(LOG_NOTICE|LOG_AUTH,
- AskedForGot, canonname, bp);
- cp += n;
- continue; /* XXX - had_error++ ? */
- }
- if (type == T_A && n != INADDRSZ) {
- cp += n;
- continue;
- }
- if (type == T_AAAA && n != IN6ADDRSZ) {
- cp += n;
- continue;
- }
- if (type == T_AAAA) {
- struct in6_addr in6;
- memcpy(&in6, cp, IN6ADDRSZ);
- if (IN6_IS_ADDR_V4MAPPED(&in6)) {
- cp += n;
- continue;
- }
- }
- if (!haveanswer) {
- int nn;
+ canonname = NULL;
+ eom = answer->buf + anslen;
+ switch (qtype) {
+ case T_A:
+ case T_AAAA:
+ case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
+ name_ok = res_hnok;
+ break;
+ default:
+ return NULL; /* XXX should be abort(); */
+ }
+ /*
+ * find first satisfactory answer
+ */
+ hp = &answer->hdr;
+ ancount = ntohs(hp->ancount);
+ qdcount = ntohs(hp->qdcount);
+ bp = hostbuf;
+ ep = hostbuf + sizeof hostbuf;
+ cp = answer->buf;
+ BOUNDED_INCR(HFIXEDSZ);
+ if (qdcount != 1) {
+ h_errno = NO_RECOVERY;
+ return (NULL);
+ }
+ n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
+ if ((n < 0) || !(*name_ok)(bp)) {
+ h_errno = NO_RECOVERY;
+ return (NULL);
+ }
+ BOUNDED_INCR(n + QFIXEDSZ);
+ if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
+ /* res_send() has already verified that the query name is the
+ * same as the one we sent; this just gets the expanded name
+ * (i.e., with the succeeding search-domain tacked on).
+ */
+ n = strlen(bp) + 1; /* for the \0 */
+ if (n >= MAXHOSTNAMELEN) {
+ h_errno = NO_RECOVERY;
+ return (NULL);
+ }
+ canonname = bp;
+ bp += n;
+ /* The qname can be abbreviated, but h_name is now absolute. */
+ qname = canonname;
+ }
+ haveanswer = 0;
+ had_error = 0;
+ while (ancount-- > 0 && cp < eom && !had_error) {
+ n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
+ if ((n < 0) || !(*name_ok)(bp)) {
+ had_error++;
+ continue;
+ }
+ cp += n; /* name */
+ BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
+ type = _getshort(cp);
+ cp += INT16SZ; /* type */
+ class = _getshort(cp);
+ cp += INT16SZ + INT32SZ; /* class, TTL */
+ n = _getshort(cp);
+ cp += INT16SZ; /* len */
+ BOUNDS_CHECK(cp, n);
+ if (class != C_IN) {
+ /* XXX - debug? syslog? */
+ cp += n;
+ continue; /* XXX - had_error++ ? */
+ }
+ if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
+ n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
+ if ((n < 0) || !(*name_ok)(tbuf)) {
+ had_error++;
+ continue;
+ }
+ cp += n;
+ /* Get canonical name. */
+ n = strlen(tbuf) + 1; /* for the \0 */
+ if (n > ep - bp || n >= MAXHOSTNAMELEN) {
+ had_error++;
+ continue;
+ }
+ strlcpy(bp, tbuf, (size_t)(ep - bp));
+ canonname = bp;
+ bp += n;
+ continue;
+ }
+ if (qtype == T_ANY) {
+ if (!(type == T_A || type == T_AAAA)) {
+ cp += n;
+ continue;
+ }
+ } else if (type != qtype) {
+ if (type != T_KEY && type != T_SIG)
+ syslog(LOG_NOTICE | LOG_AUTH,
+ "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
+ p_class(C_IN), p_type(qtype), p_type(type));
+ cp += n;
+ continue; /* XXX - had_error++ ? */
+ }
+ switch (type) {
+ case T_A:
+ case T_AAAA:
+ if (strcasecmp(canonname, bp) != 0) {
+ syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
+ cp += n;
+ continue; /* XXX - had_error++ ? */
+ }
+ if (type == T_A && n != INADDRSZ) {
+ cp += n;
+ continue;
+ }
+ if (type == T_AAAA && n != IN6ADDRSZ) {
+ cp += n;
+ continue;
+ }
+ if (type == T_AAAA) {
+ struct in6_addr in6;
+ memcpy(&in6, cp, IN6ADDRSZ);
+ if (IN6_IS_ADDR_V4MAPPED(&in6)) {
+ cp += n;
+ continue;
+ }
+ }
+ if (!haveanswer) {
+ int nn;
- canonname = bp;
- nn = strlen(bp) + 1; /* for the \0 */
- bp += nn;
- }
+ canonname = bp;
+ nn = strlen(bp) + 1; /* for the \0 */
+ bp += nn;
+ }
- /* don't overwrite pai */
- ai = *pai;
- ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
- afd = find_afd(ai.ai_family);
- if (afd == NULL) {
- cp += n;
- continue;
- }
- cur->ai_next = get_ai(&ai, afd, (const char *)cp);
- if (cur->ai_next == NULL)
- had_error++;
- while (cur && cur->ai_next)
- cur = cur->ai_next;
- cp += n;
- break;
- default:
- abort();
- }
- if (!had_error)
- haveanswer++;
- }
- if (haveanswer) {
- if (!canonname)
- (void)get_canonname(pai, sentinel.ai_next, qname);
- else
- (void)get_canonname(pai, sentinel.ai_next, canonname);
- h_errno = NETDB_SUCCESS;
- return sentinel.ai_next;
- }
+ /* don't overwrite pai */
+ ai = *pai;
+ ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
+ afd = find_afd(ai.ai_family);
+ if (afd == NULL) {
+ cp += n;
+ continue;
+ }
+ cur->ai_next = get_ai(&ai, afd, (const char*) cp);
+ if (cur->ai_next == NULL) had_error++;
+ while (cur && cur->ai_next) cur = cur->ai_next;
+ cp += n;
+ break;
+ default:
+ abort();
+ }
+ if (!had_error) haveanswer++;
+ }
+ if (haveanswer) {
+ if (!canonname)
+ (void) get_canonname(pai, sentinel.ai_next, qname);
+ else
+ (void) get_canonname(pai, sentinel.ai_next, canonname);
+ h_errno = NETDB_SUCCESS;
+ return sentinel.ai_next;
+ }
- h_errno = NO_RECOVERY;
- return NULL;
+ h_errno = NO_RECOVERY;
+ return NULL;
}
struct addrinfo_sort_elem {
- struct addrinfo *ai;
- int has_src_addr;
- sockaddr_union src_addr;
- int original_order;
+ struct addrinfo* ai;
+ int has_src_addr;
+ sockaddr_union src_addr;
+ int original_order;
};
/*ARGSUSED*/
-static int
-_get_scope(const struct sockaddr *addr)
-{
- if (addr->sa_family == AF_INET6) {
- const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
- if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
- return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
- } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
- IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
- /*
- * RFC 4291 section 2.5.3 says loopback is to be treated as having
- * link-local scope.
- */
- return IPV6_ADDR_SCOPE_LINKLOCAL;
- } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
- return IPV6_ADDR_SCOPE_SITELOCAL;
- } else {
- return IPV6_ADDR_SCOPE_GLOBAL;
- }
- } else if (addr->sa_family == AF_INET) {
- const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
- unsigned long int na = ntohl(addr4->sin_addr.s_addr);
+static int _get_scope(const struct sockaddr* addr) {
+ if (addr->sa_family == AF_INET6) {
+ const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
+ if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
+ return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
+ } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
+ IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
+ /*
+ * RFC 4291 section 2.5.3 says loopback is to be treated as having
+ * link-local scope.
+ */
+ return IPV6_ADDR_SCOPE_LINKLOCAL;
+ } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
+ return IPV6_ADDR_SCOPE_SITELOCAL;
+ } else {
+ return IPV6_ADDR_SCOPE_GLOBAL;
+ }
+ } else if (addr->sa_family == AF_INET) {
+ const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
+ unsigned long int na = ntohl(addr4->sin_addr.s_addr);
- if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
- (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
- return IPV6_ADDR_SCOPE_LINKLOCAL;
- } else {
- /*
- * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
- * and shared addresses (100.64.0.0/10), are assigned global scope.
- */
- return IPV6_ADDR_SCOPE_GLOBAL;
- }
- } else {
- /*
- * This should never happen.
- * Return a scope with low priority as a last resort.
- */
- return IPV6_ADDR_SCOPE_NODELOCAL;
- }
+ if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
+ (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
+ return IPV6_ADDR_SCOPE_LINKLOCAL;
+ } else {
+ /*
+ * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
+ * and shared addresses (100.64.0.0/10), are assigned global scope.
+ */
+ return IPV6_ADDR_SCOPE_GLOBAL;
+ }
+ } else {
+ /*
+ * This should never happen.
+ * Return a scope with low priority as a last resort.
+ */
+ return IPV6_ADDR_SCOPE_NODELOCAL;
+ }
}
/* These macros are modelled after the ones in <netinet/in6.h>. */
/* RFC 4380, section 2.6 */
-#define IN6_IS_ADDR_TEREDO(a) \
- ((*(const uint32_t *)(const void *)(&(a)->s6_addr[0]) == ntohl(0x20010000)))
+#define IN6_IS_ADDR_TEREDO(a) \
+ ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
/* RFC 3056, section 2. */
-#define IN6_IS_ADDR_6TO4(a) \
- (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
+#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
-#define IN6_IS_ADDR_6BONE(a) \
- (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
+#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
/*
* Get the label for a given IPv4/IPv6 address.
@@ -1571,40 +1423,38 @@
*/
/*ARGSUSED*/
-static int
-_get_label(const struct sockaddr *addr)
-{
- if (addr->sa_family == AF_INET) {
- return 4;
- } else if (addr->sa_family == AF_INET6) {
- const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
- if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
- return 0;
- } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
- return 4;
- } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
- return 2;
- } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
- return 5;
- } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
- return 13;
- } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
- return 3;
- } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
- return 11;
- } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
- return 12;
- } else {
- /* All other IPv6 addresses, including global unicast addresses. */
- return 1;
- }
- } else {
- /*
- * This should never happen.
- * Return a semi-random label as a last resort.
- */
- return 1;
- }
+static int _get_label(const struct sockaddr* addr) {
+ if (addr->sa_family == AF_INET) {
+ return 4;
+ } else if (addr->sa_family == AF_INET6) {
+ const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
+ if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
+ return 0;
+ } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
+ return 4;
+ } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
+ return 2;
+ } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
+ return 5;
+ } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
+ return 13;
+ } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
+ return 3;
+ } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
+ return 11;
+ } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
+ return 12;
+ } else {
+ /* All other IPv6 addresses, including global unicast addresses. */
+ return 1;
+ }
+ } else {
+ /*
+ * This should never happen.
+ * Return a semi-random label as a last resort.
+ */
+ return 1;
+ }
}
/*
@@ -1613,34 +1463,32 @@
*/
/*ARGSUSED*/
-static int
-_get_precedence(const struct sockaddr *addr)
-{
- if (addr->sa_family == AF_INET) {
- return 35;
- } else if (addr->sa_family == AF_INET6) {
- const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
- if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
- return 50;
- } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
- return 35;
- } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
- return 30;
- } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
- return 5;
- } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
- return 3;
- } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
- IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
- IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
- return 1;
- } else {
- /* All other IPv6 addresses, including global unicast addresses. */
- return 40;
- }
- } else {
- return 1;
- }
+static int _get_precedence(const struct sockaddr* addr) {
+ if (addr->sa_family == AF_INET) {
+ return 35;
+ } else if (addr->sa_family == AF_INET6) {
+ const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
+ if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
+ return 50;
+ } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
+ return 35;
+ } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
+ return 30;
+ } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
+ return 5;
+ } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
+ return 3;
+ } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
+ IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
+ IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
+ return 1;
+ } else {
+ /* All other IPv6 addresses, including global unicast addresses. */
+ return 40;
+ }
+ } else {
+ return 1;
+ }
}
/*
@@ -1648,28 +1496,26 @@
*/
/*ARGSUSED*/
-static int
-_common_prefix_len(const struct in6_addr *a1, const struct in6_addr *a2)
-{
- const char *p1 = (const char *)a1;
- const char *p2 = (const char *)a2;
- unsigned i;
+static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
+ const char* p1 = (const char*) a1;
+ const char* p2 = (const char*) a2;
+ unsigned i;
- for (i = 0; i < sizeof(*a1); ++i) {
- int x, j;
+ for (i = 0; i < sizeof(*a1); ++i) {
+ int x, j;
- if (p1[i] == p2[i]) {
- continue;
- }
- x = p1[i] ^ p2[i];
- for (j = 0; j < CHAR_BIT; ++j) {
- if (x & (1 << (CHAR_BIT - 1))) {
- return i * CHAR_BIT + j;
- }
- x <<= 1;
- }
- }
- return sizeof(*a1) * CHAR_BIT;
+ if (p1[i] == p2[i]) {
+ continue;
+ }
+ x = p1[i] ^ p2[i];
+ for (j = 0; j < CHAR_BIT; ++j) {
+ if (x & (1 << (CHAR_BIT - 1))) {
+ return i * CHAR_BIT + j;
+ }
+ x <<= 1;
+ }
+ }
+ return sizeof(*a1) * CHAR_BIT;
}
/*
@@ -1678,100 +1524,98 @@
*/
/*ARGSUSED*/
-static int
-_rfc6724_compare(const void *ptr1, const void* ptr2)
-{
- const struct addrinfo_sort_elem *a1 = (const struct addrinfo_sort_elem *)ptr1;
- const struct addrinfo_sort_elem *a2 = (const struct addrinfo_sort_elem *)ptr2;
- int scope_src1, scope_dst1, scope_match1;
- int scope_src2, scope_dst2, scope_match2;
- int label_src1, label_dst1, label_match1;
- int label_src2, label_dst2, label_match2;
- int precedence1, precedence2;
- int prefixlen1, prefixlen2;
+static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
+ const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
+ const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
+ int scope_src1, scope_dst1, scope_match1;
+ int scope_src2, scope_dst2, scope_match2;
+ int label_src1, label_dst1, label_match1;
+ int label_src2, label_dst2, label_match2;
+ int precedence1, precedence2;
+ int prefixlen1, prefixlen2;
- /* Rule 1: Avoid unusable destinations. */
- if (a1->has_src_addr != a2->has_src_addr) {
- return a2->has_src_addr - a1->has_src_addr;
- }
+ /* Rule 1: Avoid unusable destinations. */
+ if (a1->has_src_addr != a2->has_src_addr) {
+ return a2->has_src_addr - a1->has_src_addr;
+ }
- /* Rule 2: Prefer matching scope. */
- scope_src1 = _get_scope(&a1->src_addr.generic);
- scope_dst1 = _get_scope(a1->ai->ai_addr);
- scope_match1 = (scope_src1 == scope_dst1);
+ /* Rule 2: Prefer matching scope. */
+ scope_src1 = _get_scope(&a1->src_addr.generic);
+ scope_dst1 = _get_scope(a1->ai->ai_addr);
+ scope_match1 = (scope_src1 == scope_dst1);
- scope_src2 = _get_scope(&a2->src_addr.generic);
- scope_dst2 = _get_scope(a2->ai->ai_addr);
- scope_match2 = (scope_src2 == scope_dst2);
+ scope_src2 = _get_scope(&a2->src_addr.generic);
+ scope_dst2 = _get_scope(a2->ai->ai_addr);
+ scope_match2 = (scope_src2 == scope_dst2);
- if (scope_match1 != scope_match2) {
- return scope_match2 - scope_match1;
- }
+ if (scope_match1 != scope_match2) {
+ return scope_match2 - scope_match1;
+ }
- /*
- * Rule 3: Avoid deprecated addresses.
- * TODO(sesse): We don't currently have a good way of finding this.
- */
+ /*
+ * Rule 3: Avoid deprecated addresses.
+ * TODO(sesse): We don't currently have a good way of finding this.
+ */
- /*
- * Rule 4: Prefer home addresses.
- * TODO(sesse): We don't currently have a good way of finding this.
- */
+ /*
+ * Rule 4: Prefer home addresses.
+ * TODO(sesse): We don't currently have a good way of finding this.
+ */
- /* Rule 5: Prefer matching label. */
- label_src1 = _get_label(&a1->src_addr.generic);
- label_dst1 = _get_label(a1->ai->ai_addr);
- label_match1 = (label_src1 == label_dst1);
+ /* Rule 5: Prefer matching label. */
+ label_src1 = _get_label(&a1->src_addr.generic);
+ label_dst1 = _get_label(a1->ai->ai_addr);
+ label_match1 = (label_src1 == label_dst1);
- label_src2 = _get_label(&a2->src_addr.generic);
- label_dst2 = _get_label(a2->ai->ai_addr);
- label_match2 = (label_src2 == label_dst2);
+ label_src2 = _get_label(&a2->src_addr.generic);
+ label_dst2 = _get_label(a2->ai->ai_addr);
+ label_match2 = (label_src2 == label_dst2);
- if (label_match1 != label_match2) {
- return label_match2 - label_match1;
- }
+ if (label_match1 != label_match2) {
+ return label_match2 - label_match1;
+ }
- /* Rule 6: Prefer higher precedence. */
- precedence1 = _get_precedence(a1->ai->ai_addr);
- precedence2 = _get_precedence(a2->ai->ai_addr);
- if (precedence1 != precedence2) {
- return precedence2 - precedence1;
- }
+ /* Rule 6: Prefer higher precedence. */
+ precedence1 = _get_precedence(a1->ai->ai_addr);
+ precedence2 = _get_precedence(a2->ai->ai_addr);
+ if (precedence1 != precedence2) {
+ return precedence2 - precedence1;
+ }
- /*
- * Rule 7: Prefer native transport.
- * TODO(sesse): We don't currently have a good way of finding this.
- */
+ /*
+ * Rule 7: Prefer native transport.
+ * TODO(sesse): We don't currently have a good way of finding this.
+ */
- /* Rule 8: Prefer smaller scope. */
- if (scope_dst1 != scope_dst2) {
- return scope_dst1 - scope_dst2;
- }
+ /* Rule 8: Prefer smaller scope. */
+ if (scope_dst1 != scope_dst2) {
+ return scope_dst1 - scope_dst2;
+ }
- /*
- * Rule 9: Use longest matching prefix.
- * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
- * to work very well directly applied to IPv4. (glibc uses information from
- * the routing table for a custom IPv4 implementation here.)
- */
- if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 &&
- a2->has_src_addr && a2->ai->ai_addr->sa_family == AF_INET6) {
- const struct sockaddr_in6 *a1_src = &a1->src_addr.in6;
- const struct sockaddr_in6 *a1_dst = (const struct sockaddr_in6 *)a1->ai->ai_addr;
- const struct sockaddr_in6 *a2_src = &a2->src_addr.in6;
- const struct sockaddr_in6 *a2_dst = (const struct sockaddr_in6 *)a2->ai->ai_addr;
- prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
- prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
- if (prefixlen1 != prefixlen2) {
- return prefixlen2 - prefixlen1;
- }
- }
+ /*
+ * Rule 9: Use longest matching prefix.
+ * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
+ * to work very well directly applied to IPv4. (glibc uses information from
+ * the routing table for a custom IPv4 implementation here.)
+ */
+ if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
+ a2->ai->ai_addr->sa_family == AF_INET6) {
+ const struct sockaddr_in6* a1_src = &a1->src_addr.in6;
+ const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
+ const struct sockaddr_in6* a2_src = &a2->src_addr.in6;
+ const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
+ prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
+ prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
+ if (prefixlen1 != prefixlen2) {
+ return prefixlen2 - prefixlen1;
+ }
+ }
- /*
- * Rule 10: Leave the order unchanged.
- * We need this since qsort() is not necessarily stable.
- */
- return a1->original_order - a2->original_order;
+ /*
+ * Rule 10: Leave the order unchanged.
+ * We need this since qsort() is not necessarily stable.
+ */
+ return a1->original_order - a2->original_order;
}
/*
@@ -1784,56 +1628,55 @@
*/
/*ARGSUSED*/
-static int
-_find_src_addr(const struct sockaddr *addr, struct sockaddr *src_addr, unsigned mark, uid_t uid)
-{
- int sock;
- int ret;
- socklen_t len;
+static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
+ uid_t uid) {
+ int sock;
+ int ret;
+ socklen_t len;
- switch (addr->sa_family) {
- case AF_INET:
- len = sizeof(struct sockaddr_in);
- break;
- case AF_INET6:
- len = sizeof(struct sockaddr_in6);
- break;
- default:
- /* No known usable source address for non-INET families. */
- return 0;
- }
+ switch (addr->sa_family) {
+ case AF_INET:
+ len = sizeof(struct sockaddr_in);
+ break;
+ case AF_INET6:
+ len = sizeof(struct sockaddr_in6);
+ break;
+ default:
+ /* No known usable source address for non-INET families. */
+ return 0;
+ }
- sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
- if (sock == -1) {
- if (errno == EAFNOSUPPORT) {
- return 0;
- } else {
- return -1;
- }
- }
- if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
- close(sock);
- return 0;
- }
- if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t)-1) < 0) {
- close(sock);
- return 0;
- }
- do {
- ret = __connect(sock, addr, len);
- } while (ret == -1 && errno == EINTR);
+ sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
+ if (sock == -1) {
+ if (errno == EAFNOSUPPORT) {
+ return 0;
+ } else {
+ return -1;
+ }
+ }
+ if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
+ close(sock);
+ return 0;
+ }
+ if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
+ close(sock);
+ return 0;
+ }
+ do {
+ ret = __connect(sock, addr, len);
+ } while (ret == -1 && errno == EINTR);
- if (ret == -1) {
- close(sock);
- return 0;
- }
+ if (ret == -1) {
+ close(sock);
+ return 0;
+ }
- if (src_addr && getsockname(sock, src_addr, &len) == -1) {
- close(sock);
- return -1;
- }
- close(sock);
- return 1;
+ if (src_addr && getsockname(sock, src_addr, &len) == -1) {
+ close(sock);
+ return -1;
+ }
+ close(sock);
+ return 1;
}
/*
@@ -1842,306 +1685,279 @@
*/
/*ARGSUSED*/
-static void
-_rfc6724_sort(struct addrinfo *list_sentinel, unsigned mark, uid_t uid)
-{
- struct addrinfo *cur;
- int nelem = 0, i;
- struct addrinfo_sort_elem *elems;
+static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
+ struct addrinfo* cur;
+ int nelem = 0, i;
+ struct addrinfo_sort_elem* elems;
- cur = list_sentinel->ai_next;
- while (cur) {
- ++nelem;
- cur = cur->ai_next;
- }
+ cur = list_sentinel->ai_next;
+ while (cur) {
+ ++nelem;
+ cur = cur->ai_next;
+ }
- elems = (struct addrinfo_sort_elem *)malloc(nelem * sizeof(struct addrinfo_sort_elem));
- if (elems == NULL) {
- goto error;
- }
+ elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
+ if (elems == NULL) {
+ goto error;
+ }
- /*
- * Convert the linked list to an array that also contains the candidate
- * source address for each destination address.
- */
- for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
- int has_src_addr;
- assert(cur != NULL);
- elems[i].ai = cur;
- elems[i].original_order = i;
+ /*
+ * Convert the linked list to an array that also contains the candidate
+ * source address for each destination address.
+ */
+ for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
+ int has_src_addr;
+ assert(cur != NULL);
+ elems[i].ai = cur;
+ elems[i].original_order = i;
- has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.generic, mark, uid);
- if (has_src_addr == -1) {
- goto error;
- }
- elems[i].has_src_addr = has_src_addr;
- }
+ has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.generic, mark, uid);
+ if (has_src_addr == -1) {
+ goto error;
+ }
+ elems[i].has_src_addr = has_src_addr;
+ }
- /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
- qsort((void *)elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
+ /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
+ qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
- list_sentinel->ai_next = elems[0].ai;
- for (i = 0; i < nelem - 1; ++i) {
- elems[i].ai->ai_next = elems[i + 1].ai;
- }
- elems[nelem - 1].ai->ai_next = NULL;
+ list_sentinel->ai_next = elems[0].ai;
+ for (i = 0; i < nelem - 1; ++i) {
+ elems[i].ai->ai_next = elems[i + 1].ai;
+ }
+ elems[nelem - 1].ai->ai_next = NULL;
error:
- free(elems);
+ free(elems);
}
/*ARGSUSED*/
-static int
-_dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
-{
- struct addrinfo *ai;
- querybuf *buf, *buf2;
- const char *name;
- const struct addrinfo *pai;
- struct addrinfo sentinel, *cur;
- struct res_target q, q2;
- res_state res;
- const struct android_net_context *netcontext;
+static int _dns_getaddrinfo(void* rv, void* cb_data, va_list ap) {
+ struct addrinfo* ai;
+ querybuf *buf, *buf2;
+ const char* name;
+ const struct addrinfo* pai;
+ struct addrinfo sentinel, *cur;
+ struct res_target q, q2;
+ res_state res;
+ const struct android_net_context* netcontext;
- name = va_arg(ap, char *);
- pai = va_arg(ap, const struct addrinfo *);
- netcontext = va_arg(ap, const struct android_net_context *);
- //fprintf(stderr, "_dns_getaddrinfo() name = '%s'\n", name);
+ name = va_arg(ap, char*);
+ pai = va_arg(ap, const struct addrinfo*);
+ netcontext = va_arg(ap, const struct android_net_context*);
+ // fprintf(stderr, "_dns_getaddrinfo() name = '%s'\n", name);
- memset(&q, 0, sizeof(q));
- memset(&q2, 0, sizeof(q2));
- memset(&sentinel, 0, sizeof(sentinel));
- cur = &sentinel;
+ memset(&q, 0, sizeof(q));
+ memset(&q2, 0, sizeof(q2));
+ memset(&sentinel, 0, sizeof(sentinel));
+ cur = &sentinel;
- buf = malloc(sizeof(*buf));
- if (buf == NULL) {
- h_errno = NETDB_INTERNAL;
- return NS_NOTFOUND;
- }
- buf2 = malloc(sizeof(*buf2));
- if (buf2 == NULL) {
- free(buf);
- h_errno = NETDB_INTERNAL;
- return NS_NOTFOUND;
- }
+ buf = malloc(sizeof(*buf));
+ if (buf == NULL) {
+ h_errno = NETDB_INTERNAL;
+ return NS_NOTFOUND;
+ }
+ buf2 = malloc(sizeof(*buf2));
+ if (buf2 == NULL) {
+ free(buf);
+ h_errno = NETDB_INTERNAL;
+ return NS_NOTFOUND;
+ }
- switch (pai->ai_family) {
- case AF_UNSPEC:
- /* prefer IPv6 */
- q.name = name;
- q.qclass = C_IN;
- q.answer = buf->buf;
- q.anslen = sizeof(buf->buf);
- int query_ipv6 = 1, query_ipv4 = 1;
- if (pai->ai_flags & AI_ADDRCONFIG) {
- query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
- query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
- }
- if (query_ipv6) {
- q.qtype = T_AAAA;
- if (query_ipv4) {
- q.next = &q2;
- q2.name = name;
- q2.qclass = C_IN;
- q2.qtype = T_A;
- q2.answer = buf2->buf;
- q2.anslen = sizeof(buf2->buf);
- }
- } else if (query_ipv4) {
- q.qtype = T_A;
- } else {
- free(buf);
- free(buf2);
- return NS_NOTFOUND;
- }
- break;
- case AF_INET:
- q.name = name;
- q.qclass = C_IN;
- q.qtype = T_A;
- q.answer = buf->buf;
- q.anslen = sizeof(buf->buf);
- break;
- case AF_INET6:
- q.name = name;
- q.qclass = C_IN;
- q.qtype = T_AAAA;
- q.answer = buf->buf;
- q.anslen = sizeof(buf->buf);
- break;
- default:
- free(buf);
- free(buf2);
- return NS_UNAVAIL;
- }
+ switch (pai->ai_family) {
+ case AF_UNSPEC:
+ /* prefer IPv6 */
+ q.name = name;
+ q.qclass = C_IN;
+ q.answer = buf->buf;
+ q.anslen = sizeof(buf->buf);
+ int query_ipv6 = 1, query_ipv4 = 1;
+ if (pai->ai_flags & AI_ADDRCONFIG) {
+ query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
+ query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
+ }
+ if (query_ipv6) {
+ q.qtype = T_AAAA;
+ if (query_ipv4) {
+ q.next = &q2;
+ q2.name = name;
+ q2.qclass = C_IN;
+ q2.qtype = T_A;
+ q2.answer = buf2->buf;
+ q2.anslen = sizeof(buf2->buf);
+ }
+ } else if (query_ipv4) {
+ q.qtype = T_A;
+ } else {
+ free(buf);
+ free(buf2);
+ return NS_NOTFOUND;
+ }
+ break;
+ case AF_INET:
+ q.name = name;
+ q.qclass = C_IN;
+ q.qtype = T_A;
+ q.answer = buf->buf;
+ q.anslen = sizeof(buf->buf);
+ break;
+ case AF_INET6:
+ q.name = name;
+ q.qclass = C_IN;
+ q.qtype = T_AAAA;
+ q.answer = buf->buf;
+ q.anslen = sizeof(buf->buf);
+ break;
+ default:
+ free(buf);
+ free(buf2);
+ return NS_UNAVAIL;
+ }
- res = __res_get_state();
- if (res == NULL) {
- free(buf);
- free(buf2);
- return NS_NOTFOUND;
- }
+ res = __res_get_state();
+ if (res == NULL) {
+ free(buf);
+ free(buf2);
+ return NS_NOTFOUND;
+ }
- /* this just sets our netid val in the thread private data so we don't have to
- * modify the api's all the way down to res_send.c's res_nsend. We could
- * fully populate the thread private data here, but if we get down there
- * and have a cache hit that would be wasted, so we do the rest there on miss
- */
- res_setnetcontext(res, netcontext);
- if (res_searchN(name, &q, res) < 0) {
- __res_put_state(res);
- free(buf);
- free(buf2);
- return NS_NOTFOUND;
- }
- ai = getanswer(buf, q.n, q.name, q.qtype, pai);
- if (ai) {
- cur->ai_next = ai;
- while (cur && cur->ai_next)
- cur = cur->ai_next;
- }
- if (q.next) {
- ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
- if (ai)
- cur->ai_next = ai;
- }
- free(buf);
- free(buf2);
- if (sentinel.ai_next == NULL) {
- __res_put_state(res);
- switch (h_errno) {
- case HOST_NOT_FOUND:
- return NS_NOTFOUND;
- case TRY_AGAIN:
- return NS_TRYAGAIN;
- default:
- return NS_UNAVAIL;
- }
- }
+ /* this just sets our netid val in the thread private data so we don't have to
+ * modify the api's all the way down to res_send.c's res_nsend. We could
+ * fully populate the thread private data here, but if we get down there
+ * and have a cache hit that would be wasted, so we do the rest there on miss
+ */
+ res_setnetcontext(res, netcontext);
+ if (res_searchN(name, &q, res) < 0) {
+ __res_put_state(res);
+ free(buf);
+ free(buf2);
+ return NS_NOTFOUND;
+ }
+ ai = getanswer(buf, q.n, q.name, q.qtype, pai);
+ if (ai) {
+ cur->ai_next = ai;
+ while (cur && cur->ai_next) cur = cur->ai_next;
+ }
+ if (q.next) {
+ ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
+ if (ai) cur->ai_next = ai;
+ }
+ free(buf);
+ free(buf2);
+ if (sentinel.ai_next == NULL) {
+ __res_put_state(res);
+ switch (h_errno) {
+ case HOST_NOT_FOUND:
+ return NS_NOTFOUND;
+ case TRY_AGAIN:
+ return NS_TRYAGAIN;
+ default:
+ return NS_UNAVAIL;
+ }
+ }
- _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
+ _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
- __res_put_state(res);
+ __res_put_state(res);
- *((struct addrinfo **)rv) = sentinel.ai_next;
- return NS_SUCCESS;
+ *((struct addrinfo**) rv) = sentinel.ai_next;
+ return NS_SUCCESS;
}
-static void
-_sethtent(FILE **hostf)
-{
-
- if (!*hostf)
- *hostf = fopen(_PATH_HOSTS, "re");
- else
- rewind(*hostf);
+static void _sethtent(FILE** hostf) {
+ if (!*hostf)
+ *hostf = fopen(_PATH_HOSTS, "re");
+ else
+ rewind(*hostf);
}
-static void
-_endhtent(FILE **hostf)
-{
-
- if (*hostf) {
- (void) fclose(*hostf);
- *hostf = NULL;
- }
+static void _endhtent(FILE** hostf) {
+ if (*hostf) {
+ (void) fclose(*hostf);
+ *hostf = NULL;
+ }
}
-static struct addrinfo *
-_gethtent(FILE **hostf, const char *name, const struct addrinfo *pai)
-{
- char *p;
- char *cp, *tname, *cname;
- struct addrinfo hints, *res0, *res;
- int error;
- const char *addr;
- char hostbuf[8*1024];
+static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
+ char* p;
+ char *cp, *tname, *cname;
+ struct addrinfo hints, *res0, *res;
+ int error;
+ const char* addr;
+ char hostbuf[8 * 1024];
-// fprintf(stderr, "_gethtent() name = '%s'\n", name);
- assert(name != NULL);
- assert(pai != NULL);
+ // fprintf(stderr, "_gethtent() name = '%s'\n", name);
+ assert(name != NULL);
+ assert(pai != NULL);
- if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re")))
- return (NULL);
- again:
- if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf)))
- return (NULL);
- if (*p == '#')
- goto again;
- if (!(cp = strpbrk(p, "#\n")))
- goto again;
- *cp = '\0';
- if (!(cp = strpbrk(p, " \t")))
- goto again;
- *cp++ = '\0';
- addr = p;
- /* if this is not something we're looking for, skip it. */
- cname = NULL;
- while (cp && *cp) {
- if (*cp == ' ' || *cp == '\t') {
- cp++;
- continue;
- }
- if (!cname)
- cname = cp;
- tname = cp;
- if ((cp = strpbrk(cp, " \t")) != NULL)
- *cp++ = '\0';
-// fprintf(stderr, "\ttname = '%s'", tname);
- if (strcasecmp(name, tname) == 0)
- goto found;
- }
- goto again;
+ if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
+again:
+ if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
+ if (*p == '#') goto again;
+ if (!(cp = strpbrk(p, "#\n"))) goto again;
+ *cp = '\0';
+ if (!(cp = strpbrk(p, " \t"))) goto again;
+ *cp++ = '\0';
+ addr = p;
+ /* if this is not something we're looking for, skip it. */
+ cname = NULL;
+ while (cp && *cp) {
+ if (*cp == ' ' || *cp == '\t') {
+ cp++;
+ continue;
+ }
+ if (!cname) cname = cp;
+ tname = cp;
+ if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
+ // fprintf(stderr, "\ttname = '%s'", tname);
+ if (strcasecmp(name, tname) == 0) goto found;
+ }
+ goto again;
found:
- hints = *pai;
- hints.ai_flags = AI_NUMERICHOST;
- error = getaddrinfo(addr, NULL, &hints, &res0);
- if (error)
- goto again;
- for (res = res0; res; res = res->ai_next) {
- /* cover it up */
- res->ai_flags = pai->ai_flags;
+ hints = *pai;
+ hints.ai_flags = AI_NUMERICHOST;
+ error = getaddrinfo(addr, NULL, &hints, &res0);
+ if (error) goto again;
+ for (res = res0; res; res = res->ai_next) {
+ /* cover it up */
+ res->ai_flags = pai->ai_flags;
- if (pai->ai_flags & AI_CANONNAME) {
- if (get_canonname(pai, res, cname) != 0) {
- freeaddrinfo(res0);
- goto again;
- }
- }
- }
- return res0;
+ if (pai->ai_flags & AI_CANONNAME) {
+ if (get_canonname(pai, res, cname) != 0) {
+ freeaddrinfo(res0);
+ goto again;
+ }
+ }
+ }
+ return res0;
}
/*ARGSUSED*/
-static int
-_files_getaddrinfo(void *rv, void *cb_data, va_list ap)
-{
- const char *name;
- const struct addrinfo *pai;
- struct addrinfo sentinel, *cur;
- struct addrinfo *p;
- FILE *hostf = NULL;
+static int _files_getaddrinfo(void* rv, void* cb_data, va_list ap) {
+ const char* name;
+ const struct addrinfo* pai;
+ struct addrinfo sentinel, *cur;
+ struct addrinfo* p;
+ FILE* hostf = NULL;
- name = va_arg(ap, char *);
- pai = va_arg(ap, struct addrinfo *);
+ name = va_arg(ap, char*);
+ pai = va_arg(ap, struct addrinfo*);
-// fprintf(stderr, "_files_getaddrinfo() name = '%s'\n", name);
- memset(&sentinel, 0, sizeof(sentinel));
- cur = &sentinel;
+ // fprintf(stderr, "_files_getaddrinfo() name = '%s'\n", name);
+ memset(&sentinel, 0, sizeof(sentinel));
+ cur = &sentinel;
- _sethtent(&hostf);
- while ((p = _gethtent(&hostf, name, pai)) != NULL) {
- cur->ai_next = p;
- while (cur && cur->ai_next)
- cur = cur->ai_next;
- }
- _endhtent(&hostf);
+ _sethtent(&hostf);
+ while ((p = _gethtent(&hostf, name, pai)) != NULL) {
+ cur->ai_next = p;
+ while (cur && cur->ai_next) cur = cur->ai_next;
+ }
+ _endhtent(&hostf);
- *((struct addrinfo **)rv) = sentinel.ai_next;
- if (sentinel.ai_next == NULL)
- return NS_NOTFOUND;
- return NS_SUCCESS;
+ *((struct addrinfo**) rv) = sentinel.ai_next;
+ if (sentinel.ai_next == NULL) return NS_NOTFOUND;
+ return NS_SUCCESS;
}
/* resolver logic */
@@ -2156,61 +1972,56 @@
*
* Caller must parse answer and determine whether it answers the question.
*/
-static int
-res_queryN(const char *name, /* domain name */ struct res_target *target,
- res_state res)
-{
- u_char buf[MAXPACKET];
- HEADER *hp;
- int n;
- struct res_target *t;
- int rcode;
- int ancount;
+static int res_queryN(const char* name, /* domain name */ struct res_target* target,
+ res_state res) {
+ u_char buf[MAXPACKET];
+ HEADER* hp;
+ int n;
+ struct res_target* t;
+ int rcode;
+ int ancount;
- assert(name != NULL);
- /* XXX: target may be NULL??? */
+ assert(name != NULL);
+ /* XXX: target may be NULL??? */
- rcode = NOERROR;
- ancount = 0;
+ rcode = NOERROR;
+ ancount = 0;
- for (t = target; t; t = t->next) {
- int class, type;
- u_char *answer;
- int anslen;
- u_int oflags;
+ for (t = target; t; t = t->next) {
+ int class, type;
+ u_char* answer;
+ int anslen;
+ u_int oflags;
- hp = (HEADER *)(void *)t->answer;
- oflags = res->_flags;
+ hp = (HEADER*) (void*) t->answer;
+ oflags = res->_flags;
-again:
- hp->rcode = NOERROR; /* default */
+ again:
+ hp->rcode = NOERROR; /* default */
- /* make it easier... */
- class = t->qclass;
- type = t->qtype;
- answer = t->answer;
- anslen = t->anslen;
+ /* make it easier... */
+ class = t->qclass;
+ type = t->qtype;
+ answer = t->answer;
+ anslen = t->anslen;
#ifdef DEBUG
- if (res->options & RES_DEBUG)
- printf(";; res_nquery(%s, %d, %d)\n", name, class, type);
+ if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, class, type);
#endif
- n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL,
- buf, sizeof(buf));
+ n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL, buf, sizeof(buf));
#ifdef RES_USE_EDNS0
- if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
- (res->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0)
- n = res_nopt(res, n, buf, sizeof(buf), anslen);
+ if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
+ (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
+ n = res_nopt(res, n, buf, sizeof(buf), anslen);
#endif
- if (n <= 0) {
+ if (n <= 0) {
#ifdef DEBUG
- if (res->options & RES_DEBUG)
- printf(";; res_nquery: mkquery failed\n");
+ if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
#endif
- h_errno = NO_RECOVERY;
- return n;
- }
- n = res_nsend(res, buf, n, answer, anslen);
+ h_errno = NO_RECOVERY;
+ return n;
+ }
+ n = res_nsend(res, buf, n, answer, anslen);
#if 0
if (n < 0) {
#ifdef DEBUG
@@ -2222,54 +2033,52 @@
}
#endif
- if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
- rcode = hp->rcode; /* record most recent error */
+ if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
+ rcode = hp->rcode; /* record most recent error */
#ifdef RES_USE_EDNS0
- /* if the query choked with EDNS0, retry without EDNS0 */
- if ((res->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0 &&
- ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
- res->_flags |= RES_F_EDNS0ERR;
+ /* if the query choked with EDNS0, retry without EDNS0 */
+ if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
+ ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
+ res->_flags |= RES_F_EDNS0ERR;
#ifdef DEBUG
- if (res->options & RES_DEBUG)
- printf(";; res_nquery: retry without EDNS0\n");
+ if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
#endif
- goto again;
- }
+ goto again;
+ }
#endif
#ifdef DEBUG
- if (res->options & RES_DEBUG)
- printf(";; rcode = %u, ancount=%u\n", hp->rcode,
- ntohs(hp->ancount));
+ if (res->options & RES_DEBUG)
+ printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
#endif
- continue;
- }
+ continue;
+ }
- ancount += ntohs(hp->ancount);
+ ancount += ntohs(hp->ancount);
- t->n = n;
- }
+ t->n = n;
+ }
- if (ancount == 0) {
- switch (rcode) {
- case NXDOMAIN:
- h_errno = HOST_NOT_FOUND;
- break;
- case SERVFAIL:
- h_errno = TRY_AGAIN;
- break;
- case NOERROR:
- h_errno = NO_DATA;
- break;
- case FORMERR:
- case NOTIMP:
- case REFUSED:
- default:
- h_errno = NO_RECOVERY;
- break;
- }
- return -1;
- }
- return ancount;
+ if (ancount == 0) {
+ switch (rcode) {
+ case NXDOMAIN:
+ h_errno = HOST_NOT_FOUND;
+ break;
+ case SERVFAIL:
+ h_errno = TRY_AGAIN;
+ break;
+ case NOERROR:
+ h_errno = NO_DATA;
+ break;
+ case FORMERR:
+ case NOTIMP:
+ case REFUSED:
+ default:
+ h_errno = NO_RECOVERY;
+ break;
+ }
+ return -1;
+ }
+ return ancount;
}
/*
@@ -2278,193 +2087,178 @@
* If enabled, implement search rules until answer or unrecoverable failure
* is detected. Error code, if any, is left in h_errno.
*/
-static int
-res_searchN(const char *name, struct res_target *target, res_state res)
-{
- const char *cp, * const *domain;
- HEADER *hp;
- u_int dots;
- int trailing_dot, ret, saved_herrno;
- int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
+static int res_searchN(const char* name, struct res_target* target, res_state res) {
+ const char *cp, *const *domain;
+ HEADER* hp;
+ u_int dots;
+ int trailing_dot, ret, saved_herrno;
+ int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
- assert(name != NULL);
- assert(target != NULL);
+ assert(name != NULL);
+ assert(target != NULL);
- hp = (HEADER *)(void *)target->answer; /*XXX*/
+ hp = (HEADER*) (void*) target->answer; /*XXX*/
- errno = 0;
- h_errno = HOST_NOT_FOUND; /* default, if we never query */
- dots = 0;
- for (cp = name; *cp; cp++)
- dots += (*cp == '.');
- trailing_dot = 0;
- if (cp > name && *--cp == '.')
- trailing_dot++;
+ errno = 0;
+ h_errno = HOST_NOT_FOUND; /* default, if we never query */
+ dots = 0;
+ for (cp = name; *cp; cp++) dots += (*cp == '.');
+ trailing_dot = 0;
+ if (cp > name && *--cp == '.') trailing_dot++;
+ // fprintf(stderr, "res_searchN() name = '%s'\n", name);
- //fprintf(stderr, "res_searchN() name = '%s'\n", name);
+ /*
+ * if there aren't any dots, it could be a user-level alias
+ */
+ if (!dots && (cp = __hostalias(name)) != NULL) {
+ ret = res_queryN(cp, target, res);
+ return ret;
+ }
- /*
- * if there aren't any dots, it could be a user-level alias
- */
- if (!dots && (cp = __hostalias(name)) != NULL) {
- ret = res_queryN(cp, target, res);
- return ret;
- }
+ /*
+ * If there are dots in the name already, let's just give it a try
+ * 'as is'. The threshold can be set with the "ndots" option.
+ */
+ saved_herrno = -1;
+ if (dots >= res->ndots) {
+ ret = res_querydomainN(name, NULL, target, res);
+ if (ret > 0) return (ret);
+ saved_herrno = h_errno;
+ tried_as_is++;
+ }
- /*
- * If there are dots in the name already, let's just give it a try
- * 'as is'. The threshold can be set with the "ndots" option.
- */
- saved_herrno = -1;
- if (dots >= res->ndots) {
- ret = res_querydomainN(name, NULL, target, res);
- if (ret > 0)
- return (ret);
- saved_herrno = h_errno;
- tried_as_is++;
- }
+ /*
+ * We do at least one level of search if
+ * - there is no dot and RES_DEFNAME is set, or
+ * - there is at least one dot, there is no trailing dot,
+ * and RES_DNSRCH is set.
+ */
+ if ((!dots && (res->options & RES_DEFNAMES)) ||
+ (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
+ int done = 0;
- /*
- * We do at least one level of search if
- * - there is no dot and RES_DEFNAME is set, or
- * - there is at least one dot, there is no trailing dot,
- * and RES_DNSRCH is set.
- */
- if ((!dots && (res->options & RES_DEFNAMES)) ||
- (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
- int done = 0;
+ /* Unfortunately we need to set stuff up before
+ * the domain stuff is tried. Will have a better
+ * fix after thread pools are used.
+ */
+ _resolv_populate_res_for_net(res);
- /* Unfortunately we need to set stuff up before
- * the domain stuff is tried. Will have a better
- * fix after thread pools are used.
- */
- _resolv_populate_res_for_net(res);
+ for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
+ ret = res_querydomainN(name, *domain, target, res);
+ if (ret > 0) return ret;
- for (domain = (const char * const *)res->dnsrch;
- *domain && !done;
- domain++) {
+ /*
+ * If no server present, give up.
+ * If name isn't found in this domain,
+ * keep trying higher domains in the search list
+ * (if that's enabled).
+ * On a NO_DATA error, keep trying, otherwise
+ * a wildcard entry of another type could keep us
+ * from finding this entry higher in the domain.
+ * If we get some other error (negative answer or
+ * server failure), then stop searching up,
+ * but try the input name below in case it's
+ * fully-qualified.
+ */
+ if (errno == ECONNREFUSED) {
+ h_errno = TRY_AGAIN;
+ return -1;
+ }
- ret = res_querydomainN(name, *domain, target, res);
- if (ret > 0)
- return ret;
+ switch (h_errno) {
+ case NO_DATA:
+ got_nodata++;
+ /* FALLTHROUGH */
+ case HOST_NOT_FOUND:
+ /* keep trying */
+ break;
+ case TRY_AGAIN:
+ if (hp->rcode == SERVFAIL) {
+ /* try next search element, if any */
+ got_servfail++;
+ break;
+ }
+ /* FALLTHROUGH */
+ default:
+ /* anything else implies that we're done */
+ done++;
+ }
+ /*
+ * if we got here for some reason other than DNSRCH,
+ * we only wanted one iteration of the loop, so stop.
+ */
+ if (!(res->options & RES_DNSRCH)) done++;
+ }
+ }
- /*
- * If no server present, give up.
- * If name isn't found in this domain,
- * keep trying higher domains in the search list
- * (if that's enabled).
- * On a NO_DATA error, keep trying, otherwise
- * a wildcard entry of another type could keep us
- * from finding this entry higher in the domain.
- * If we get some other error (negative answer or
- * server failure), then stop searching up,
- * but try the input name below in case it's
- * fully-qualified.
- */
- if (errno == ECONNREFUSED) {
- h_errno = TRY_AGAIN;
- return -1;
- }
+ /*
+ * if we have not already tried the name "as is", do that now.
+ * note that we do this regardless of how many dots were in the
+ * name or whether it ends with a dot.
+ */
+ if (!tried_as_is) {
+ ret = res_querydomainN(name, NULL, target, res);
+ if (ret > 0) return ret;
+ }
- switch (h_errno) {
- case NO_DATA:
- got_nodata++;
- /* FALLTHROUGH */
- case HOST_NOT_FOUND:
- /* keep trying */
- break;
- case TRY_AGAIN:
- if (hp->rcode == SERVFAIL) {
- /* try next search element, if any */
- got_servfail++;
- break;
- }
- /* FALLTHROUGH */
- default:
- /* anything else implies that we're done */
- done++;
- }
- /*
- * if we got here for some reason other than DNSRCH,
- * we only wanted one iteration of the loop, so stop.
- */
- if (!(res->options & RES_DNSRCH))
- done++;
- }
- }
-
- /*
- * if we have not already tried the name "as is", do that now.
- * note that we do this regardless of how many dots were in the
- * name or whether it ends with a dot.
- */
- if (!tried_as_is) {
- ret = res_querydomainN(name, NULL, target, res);
- if (ret > 0)
- return ret;
- }
-
- /*
- * if we got here, we didn't satisfy the search.
- * if we did an initial full query, return that query's h_errno
- * (note that we wouldn't be here if that query had succeeded).
- * else if we ever got a nodata, send that back as the reason.
- * else send back meaningless h_errno, that being the one from
- * the last DNSRCH we did.
- */
- if (saved_herrno != -1)
- h_errno = saved_herrno;
- else if (got_nodata)
- h_errno = NO_DATA;
- else if (got_servfail)
- h_errno = TRY_AGAIN;
- return -1;
+ /*
+ * if we got here, we didn't satisfy the search.
+ * if we did an initial full query, return that query's h_errno
+ * (note that we wouldn't be here if that query had succeeded).
+ * else if we ever got a nodata, send that back as the reason.
+ * else send back meaningless h_errno, that being the one from
+ * the last DNSRCH we did.
+ */
+ if (saved_herrno != -1)
+ h_errno = saved_herrno;
+ else if (got_nodata)
+ h_errno = NO_DATA;
+ else if (got_servfail)
+ h_errno = TRY_AGAIN;
+ return -1;
}
/*
* Perform a call on res_query on the concatenation of name and domain,
* removing a trailing dot from name if domain is NULL.
*/
-static int
-res_querydomainN(const char *name, const char *domain,
- struct res_target *target, res_state res)
-{
- char nbuf[MAXDNAME];
- const char *longname = nbuf;
- size_t n, d;
+static int res_querydomainN(const char* name, const char* domain, struct res_target* target,
+ res_state res) {
+ char nbuf[MAXDNAME];
+ const char* longname = nbuf;
+ size_t n, d;
- assert(name != NULL);
- /* XXX: target may be NULL??? */
+ assert(name != NULL);
+ /* XXX: target may be NULL??? */
#ifdef DEBUG
- if (res->options & RES_DEBUG)
- printf(";; res_querydomain(%s, %s)\n",
- name, domain?domain:"<Nil>");
+ if (res->options & RES_DEBUG)
+ printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
#endif
- if (domain == NULL) {
- /*
- * Check for trailing '.';
- * copy without '.' if present.
- */
- n = strlen(name);
- if (n + 1 > sizeof(nbuf)) {
- h_errno = NO_RECOVERY;
- return -1;
- }
- if (n > 0 && name[--n] == '.') {
- strncpy(nbuf, name, n);
- nbuf[n] = '\0';
- } else
- longname = name;
- } else {
- n = strlen(name);
- d = strlen(domain);
- if (n + 1 + d + 1 > sizeof(nbuf)) {
- h_errno = NO_RECOVERY;
- return -1;
- }
- snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
- }
- return res_queryN(longname, target, res);
+ if (domain == NULL) {
+ /*
+ * Check for trailing '.';
+ * copy without '.' if present.
+ */
+ n = strlen(name);
+ if (n + 1 > sizeof(nbuf)) {
+ h_errno = NO_RECOVERY;
+ return -1;
+ }
+ if (n > 0 && name[--n] == '.') {
+ strncpy(nbuf, name, n);
+ nbuf[n] = '\0';
+ } else
+ longname = name;
+ } else {
+ n = strlen(name);
+ d = strlen(domain);
+ if (n + 1 + d + 1 > sizeof(nbuf)) {
+ h_errno = NO_RECOVERY;
+ return -1;
+ }
+ snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
+ }
+ return res_queryN(longname, target, res);
}
diff --git a/resolv/gethnamaddr.c b/resolv/gethnamaddr.c
index 7589380..5924540 100644
--- a/resolv/gethnamaddr.c
+++ b/resolv/gethnamaddr.c
@@ -54,80 +54,76 @@
#include <sys/cdefs.h>
#include <sys/types.h>
-#include <sys/param.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
-#include "NetdClientDispatch.h"
-#include "resolv_netid.h"
-#include "resolv_private.h"
-#include "resolv_cache.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
+#include <netinet/in.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <strings.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/un.h>
#include <syslog.h>
#include <unistd.h>
+#include "NetdClientDispatch.h"
+#include "resolv_cache.h"
+#include "resolv_netid.h"
+#include "resolv_private.h"
#define ALIGNBYTES (sizeof(uintptr_t) - 1)
-#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES)
+#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
#ifndef LOG_AUTH
-# define LOG_AUTH 0
+#define LOG_AUTH 0
#endif
-#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
+#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
-#include "nsswitch.h"
#include <stdlib.h>
#include <string.h>
+#include "nsswitch.h"
#include "hostent.h"
-#define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || \
- (ok)(nm) != 0)
+#define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || (ok)(nm) != 0)
#define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
#define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
-#define addalias(d, s, arr, siz) do { \
- if (d >= &arr[siz]) { \
- char **xptr = realloc(arr, (siz + 10) * sizeof(*arr)); \
- if (xptr == NULL) \
- goto nospc; \
- d = xptr + (d - arr); \
- arr = xptr; \
- siz += 10; \
- } \
- *d++ = s; \
-} while (/*CONSTCOND*/0)
+#define addalias(d, s, arr, siz) \
+ do { \
+ if (d >= &arr[siz]) { \
+ char** xptr = realloc(arr, (siz + 10) * sizeof(*arr)); \
+ if (xptr == NULL) goto nospc; \
+ d = xptr + (d - arr); \
+ arr = xptr; \
+ siz += 10; \
+ } \
+ *d++ = s; \
+ } while (/*CONSTCOND*/ 0)
-#define setup(arr, siz) do { \
- arr = malloc((siz = 10) * sizeof(*arr)); \
- if (arr == NULL) \
- goto nospc; \
-} while (/*CONSTCOND*/0)
+#define setup(arr, siz) \
+ do { \
+ arr = malloc((siz = 10) * sizeof(*arr)); \
+ if (arr == NULL) goto nospc; \
+ } while (/*CONSTCOND*/ 0)
// This should be synchronized to ResponseCode.h
static const int DnsProxyQueryResult = 222;
-static const char AskedForGot[] =
- "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
+static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
-static const struct android_net_context NETCONTEXT_UNSET = {
- .app_mark = MARK_UNSET,
- .app_netid = NETID_UNSET,
- .dns_mark = MARK_UNSET,
- .dns_netid = NETID_UNSET,
- .uid = NET_CONTEXT_INVALID_UID
-};
+static const struct android_net_context NETCONTEXT_UNSET = {.app_mark = MARK_UNSET,
+ .app_netid = NETID_UNSET,
+ .dns_mark = MARK_UNSET,
+ .dns_netid = NETID_UNSET,
+ .uid = NET_CONTEXT_INVALID_UID};
-#define MAXPACKET (8*1024)
+#define MAXPACKET (8 * 1024)
typedef union {
HEADER hdr;
@@ -140,1423 +136,1313 @@
} align;
#ifdef DEBUG
-static void debugprintf(const char *, res_state, ...)
- __attribute__((__format__(__printf__, 1, 3)));
+static void debugprintf(const char*, res_state, ...) __attribute__((__format__(__printf__, 1, 3)));
#endif
-static struct hostent *getanswer(const querybuf *, int, const char *, int,
- res_state, struct hostent *, char *, size_t, int *);
-static void map_v4v6_address(const char *, char *);
-static void map_v4v6_hostent(struct hostent *, char **, char *);
-static void addrsort(char **, int, res_state);
+static struct hostent* getanswer(const querybuf*, int, const char*, int, res_state, struct hostent*,
+ char*, size_t, int*);
+static void map_v4v6_address(const char*, char*);
+static void map_v4v6_hostent(struct hostent*, char**, char*);
+static void addrsort(char**, int, res_state);
void ht_sethostent(int);
void ht_endhostent(void);
-struct hostent *ht_gethostbyname(char *);
-struct hostent *ht_gethostbyaddr(const char *, int, int);
+struct hostent* ht_gethostbyname(char*);
+struct hostent* ht_gethostbyaddr(const char*, int, int);
void dns_service(void);
#undef dn_skipname
-int dn_skipname(const u_char *, const u_char *);
-static int _dns_gethtbyaddr(void *, void *, va_list);
-static int _dns_gethtbyname(void *, void *, va_list);
+int dn_skipname(const u_char*, const u_char*);
+static int _dns_gethtbyaddr(void*, void*, va_list);
+static int _dns_gethtbyname(void*, void*, va_list);
-static struct hostent *gethostbyname_internal(const char *, int, res_state,
- struct hostent *, char *, size_t, int *, const struct android_net_context *);
-static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(const void*, socklen_t,
- int, struct hostent *, char *, size_t, int *, const struct android_net_context *);
+static struct hostent* gethostbyname_internal(const char*, int, res_state, struct hostent*, char*,
+ size_t, int*, const struct android_net_context*);
+static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
+ const void*, socklen_t, int, struct hostent*, char*, size_t, int*,
+ const struct android_net_context*);
static const ns_src default_dns_files[] = {
- { NSSRC_FILES, NS_SUCCESS },
- { NSSRC_DNS, NS_SUCCESS },
- { 0, 0 }
-};
+ {NSSRC_FILES, NS_SUCCESS}, {NSSRC_DNS, NS_SUCCESS}, {0, 0}};
static int h_errno_to_result(int* herrno_p) {
- // glibc considers ERANGE a special case (and BSD uses ENOSPC instead).
- if (*herrno_p == NETDB_INTERNAL && errno == ENOSPC) {
- errno = ERANGE;
- return errno;
- }
- // glibc considers HOST_NOT_FOUND not an error for the _r functions' return value.
- return (*herrno_p != HOST_NOT_FOUND) ? *herrno_p : 0;
+ // glibc considers ERANGE a special case (and BSD uses ENOSPC instead).
+ if (*herrno_p == NETDB_INTERNAL && errno == ENOSPC) {
+ errno = ERANGE;
+ return errno;
+ }
+ // glibc considers HOST_NOT_FOUND not an error for the _r functions' return value.
+ return (*herrno_p != HOST_NOT_FOUND) ? *herrno_p : 0;
}
#ifdef DEBUG
-static void
-debugprintf(const char *msg, res_state res, ...)
-{
- _DIAGASSERT(msg != NULL);
+static void debugprintf(const char* msg, res_state res, ...) {
+ _DIAGASSERT(msg != NULL);
- if (res->options & RES_DEBUG) {
- int save = errno;
- va_list ap;
+ if (res->options & RES_DEBUG) {
+ int save = errno;
+ va_list ap;
- va_start (ap, res);
- vprintf(msg, ap);
- va_end (ap);
+ va_start(ap, res);
+ vprintf(msg, ap);
+ va_end(ap);
- errno = save;
- }
+ errno = save;
+ }
}
#else
-# define debugprintf(msg, res, num) /*nada*/
+#define debugprintf(msg, res, num) /*nada*/
#endif
-#define BOUNDED_INCR(x) \
- do { \
- BOUNDS_CHECK(cp, x); \
- cp += (x); \
- } while (/*CONSTCOND*/0)
+#define BOUNDED_INCR(x) \
+ do { \
+ BOUNDS_CHECK(cp, x); \
+ cp += (x); \
+ } while (/*CONSTCOND*/ 0)
-#define BOUNDS_CHECK(ptr, count) \
- do { \
- if (eom - (ptr) < (count)) \
- goto no_recovery; \
- } while (/*CONSTCOND*/0)
+#define BOUNDS_CHECK(ptr, count) \
+ do { \
+ if (eom - (ptr) < (count)) goto no_recovery; \
+ } while (/*CONSTCOND*/ 0)
-static struct hostent *
-getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
- res_state res, struct hostent *hent, char *buf, size_t buflen, int *he)
-{
- const HEADER *hp;
- const u_char *cp;
- int n;
- size_t qlen;
- const u_char *eom, *erdata;
- char *bp, **ap, **hap, *ep;
- int type, class, ancount, qdcount;
- int haveanswer, had_error;
- int toobig = 0;
- char tbuf[MAXDNAME];
- char **aliases;
- size_t maxaliases;
- char *addr_ptrs[MAXADDRS];
- const char *tname;
- int (*name_ok)(const char *);
+static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
+ res_state res, struct hostent* hent, char* buf, size_t buflen,
+ int* he) {
+ const HEADER* hp;
+ const u_char* cp;
+ int n;
+ size_t qlen;
+ const u_char *eom, *erdata;
+ char *bp, **ap, **hap, *ep;
+ int type, class, ancount, qdcount;
+ int haveanswer, had_error;
+ int toobig = 0;
+ char tbuf[MAXDNAME];
+ char** aliases;
+ size_t maxaliases;
+ char* addr_ptrs[MAXADDRS];
+ const char* tname;
+ int (*name_ok)(const char*);
- _DIAGASSERT(answer != NULL);
- _DIAGASSERT(qname != NULL);
+ _DIAGASSERT(answer != NULL);
+ _DIAGASSERT(qname != NULL);
- tname = qname;
- hent->h_name = NULL;
- eom = answer->buf + anslen;
- switch (qtype) {
- case T_A:
- case T_AAAA:
- name_ok = res_hnok;
- break;
- case T_PTR:
- name_ok = res_dnok;
- break;
- default:
- *he = NO_RECOVERY;
- return NULL; /* XXX should be abort(); */
- }
+ tname = qname;
+ hent->h_name = NULL;
+ eom = answer->buf + anslen;
+ switch (qtype) {
+ case T_A:
+ case T_AAAA:
+ name_ok = res_hnok;
+ break;
+ case T_PTR:
+ name_ok = res_dnok;
+ break;
+ default:
+ *he = NO_RECOVERY;
+ return NULL; /* XXX should be abort(); */
+ }
- setup(aliases, maxaliases);
- /*
- * find first satisfactory answer
- */
- hp = &answer->hdr;
- ancount = ntohs(hp->ancount);
- qdcount = ntohs(hp->qdcount);
- bp = buf;
- ep = buf + buflen;
- cp = answer->buf;
- BOUNDED_INCR(HFIXEDSZ);
- if (qdcount != 1)
- goto no_recovery;
+ setup(aliases, maxaliases);
+ /*
+ * find first satisfactory answer
+ */
+ hp = &answer->hdr;
+ ancount = ntohs(hp->ancount);
+ qdcount = ntohs(hp->qdcount);
+ bp = buf;
+ ep = buf + buflen;
+ cp = answer->buf;
+ BOUNDED_INCR(HFIXEDSZ);
+ if (qdcount != 1) goto no_recovery;
- n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
- if ((n < 0) || !maybe_ok(res, bp, name_ok))
- goto no_recovery;
+ n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
+ if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery;
- BOUNDED_INCR(n + QFIXEDSZ);
- if (qtype == T_A || qtype == T_AAAA) {
- /* res_send() has already verified that the query name is the
- * same as the one we sent; this just gets the expanded name
- * (i.e., with the succeeding search-domain tacked on).
- */
- n = (int)strlen(bp) + 1; /* for the \0 */
- if (n >= MAXHOSTNAMELEN)
- goto no_recovery;
- hent->h_name = bp;
- bp += n;
- /* The qname can be abbreviated, but h_name is now absolute. */
- qname = hent->h_name;
- }
- hent->h_aliases = ap = aliases;
- hent->h_addr_list = hap = addr_ptrs;
- *ap = NULL;
- *hap = NULL;
- haveanswer = 0;
- had_error = 0;
- while (ancount-- > 0 && cp < eom && !had_error) {
- n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
- if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
- had_error++;
- continue;
- }
- cp += n; /* name */
- BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
- type = _getshort(cp);
- cp += INT16SZ; /* type */
- class = _getshort(cp);
- cp += INT16SZ + INT32SZ; /* class, TTL */
- n = _getshort(cp);
- cp += INT16SZ; /* len */
- BOUNDS_CHECK(cp, n);
- erdata = cp + n;
- if (class != C_IN) {
- /* XXX - debug? syslog? */
- cp += n;
- continue; /* XXX - had_error++ ? */
- }
- if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
- n = dn_expand(answer->buf, eom, cp, tbuf,
- (int)sizeof tbuf);
- if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
- had_error++;
- continue;
- }
- cp += n;
- if (cp != erdata)
- goto no_recovery;
- /* Store alias. */
- addalias(ap, bp, aliases, maxaliases);
- n = (int)strlen(bp) + 1; /* for the \0 */
- if (n >= MAXHOSTNAMELEN) {
- had_error++;
- continue;
- }
- bp += n;
- /* Get canonical name. */
- n = (int)strlen(tbuf) + 1; /* for the \0 */
- if (n > ep - bp || n >= MAXHOSTNAMELEN) {
- had_error++;
- continue;
- }
- strlcpy(bp, tbuf, (size_t)(ep - bp));
- hent->h_name = bp;
- bp += n;
- continue;
- }
- if (qtype == T_PTR && type == T_CNAME) {
- n = dn_expand(answer->buf, eom, cp, tbuf,
- (int)sizeof tbuf);
- if (n < 0 || !maybe_dnok(res, tbuf)) {
- had_error++;
- continue;
- }
- cp += n;
- if (cp != erdata)
- goto no_recovery;
- /* Get canonical name. */
- n = (int)strlen(tbuf) + 1; /* for the \0 */
- if (n > ep - bp || n >= MAXHOSTNAMELEN) {
- had_error++;
- continue;
- }
- strlcpy(bp, tbuf, (size_t)(ep - bp));
- tname = bp;
- bp += n;
- continue;
- }
- if (type != qtype) {
- if (type != T_KEY && type != T_SIG)
- syslog(LOG_NOTICE|LOG_AUTH,
- "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
- qname, p_class(C_IN), p_type(qtype),
- p_type(type));
- cp += n;
- continue; /* XXX - had_error++ ? */
- }
- switch (type) {
- case T_PTR:
- if (strcasecmp(tname, bp) != 0) {
- syslog(LOG_NOTICE|LOG_AUTH,
- AskedForGot, qname, bp);
- cp += n;
- continue; /* XXX - had_error++ ? */
- }
- n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
- if ((n < 0) || !maybe_hnok(res, bp)) {
- had_error++;
- break;
- }
+ BOUNDED_INCR(n + QFIXEDSZ);
+ if (qtype == T_A || qtype == T_AAAA) {
+ /* res_send() has already verified that the query name is the
+ * same as the one we sent; this just gets the expanded name
+ * (i.e., with the succeeding search-domain tacked on).
+ */
+ n = (int) strlen(bp) + 1; /* for the \0 */
+ if (n >= MAXHOSTNAMELEN) goto no_recovery;
+ hent->h_name = bp;
+ bp += n;
+ /* The qname can be abbreviated, but h_name is now absolute. */
+ qname = hent->h_name;
+ }
+ hent->h_aliases = ap = aliases;
+ hent->h_addr_list = hap = addr_ptrs;
+ *ap = NULL;
+ *hap = NULL;
+ haveanswer = 0;
+ had_error = 0;
+ while (ancount-- > 0 && cp < eom && !had_error) {
+ n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
+ if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
+ had_error++;
+ continue;
+ }
+ cp += n; /* name */
+ BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
+ type = _getshort(cp);
+ cp += INT16SZ; /* type */
+ class = _getshort(cp);
+ cp += INT16SZ + INT32SZ; /* class, TTL */
+ n = _getshort(cp);
+ cp += INT16SZ; /* len */
+ BOUNDS_CHECK(cp, n);
+ erdata = cp + n;
+ if (class != C_IN) {
+ /* XXX - debug? syslog? */
+ cp += n;
+ continue; /* XXX - had_error++ ? */
+ }
+ if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
+ n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
+ if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
+ had_error++;
+ continue;
+ }
+ cp += n;
+ if (cp != erdata) goto no_recovery;
+ /* Store alias. */
+ addalias(ap, bp, aliases, maxaliases);
+ n = (int) strlen(bp) + 1; /* for the \0 */
+ if (n >= MAXHOSTNAMELEN) {
+ had_error++;
+ continue;
+ }
+ bp += n;
+ /* Get canonical name. */
+ n = (int) strlen(tbuf) + 1; /* for the \0 */
+ if (n > ep - bp || n >= MAXHOSTNAMELEN) {
+ had_error++;
+ continue;
+ }
+ strlcpy(bp, tbuf, (size_t)(ep - bp));
+ hent->h_name = bp;
+ bp += n;
+ continue;
+ }
+ if (qtype == T_PTR && type == T_CNAME) {
+ n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
+ if (n < 0 || !maybe_dnok(res, tbuf)) {
+ had_error++;
+ continue;
+ }
+ cp += n;
+ if (cp != erdata) goto no_recovery;
+ /* Get canonical name. */
+ n = (int) strlen(tbuf) + 1; /* for the \0 */
+ if (n > ep - bp || n >= MAXHOSTNAMELEN) {
+ had_error++;
+ continue;
+ }
+ strlcpy(bp, tbuf, (size_t)(ep - bp));
+ tname = bp;
+ bp += n;
+ continue;
+ }
+ if (type != qtype) {
+ if (type != T_KEY && type != T_SIG)
+ syslog(LOG_NOTICE | LOG_AUTH,
+ "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
+ p_class(C_IN), p_type(qtype), p_type(type));
+ cp += n;
+ continue; /* XXX - had_error++ ? */
+ }
+ switch (type) {
+ case T_PTR:
+ if (strcasecmp(tname, bp) != 0) {
+ syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
+ cp += n;
+ continue; /* XXX - had_error++ ? */
+ }
+ n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
+ if ((n < 0) || !maybe_hnok(res, bp)) {
+ had_error++;
+ break;
+ }
#if MULTI_PTRS_ARE_ALIASES
- cp += n;
- if (cp != erdata)
- goto no_recovery;
- if (!haveanswer)
- hent->h_name = bp;
- else
- addalias(ap, bp, aliases, maxaliases);
- if (n != -1) {
- n = (int)strlen(bp) + 1; /* for the \0 */
- if (n >= MAXHOSTNAMELEN) {
- had_error++;
- break;
- }
- bp += n;
- }
- break;
+ cp += n;
+ if (cp != erdata) goto no_recovery;
+ if (!haveanswer)
+ hent->h_name = bp;
+ else
+ addalias(ap, bp, aliases, maxaliases);
+ if (n != -1) {
+ n = (int) strlen(bp) + 1; /* for the \0 */
+ if (n >= MAXHOSTNAMELEN) {
+ had_error++;
+ break;
+ }
+ bp += n;
+ }
+ break;
#else
- hent->h_name = bp;
- if (res->options & RES_USE_INET6) {
- n = strlen(bp) + 1; /* for the \0 */
- if (n >= MAXHOSTNAMELEN) {
- had_error++;
- break;
- }
- bp += n;
- map_v4v6_hostent(hent, &bp, ep);
- }
- goto success;
+ hent->h_name = bp;
+ if (res->options & RES_USE_INET6) {
+ n = strlen(bp) + 1; /* for the \0 */
+ if (n >= MAXHOSTNAMELEN) {
+ had_error++;
+ break;
+ }
+ bp += n;
+ map_v4v6_hostent(hent, &bp, ep);
+ }
+ goto success;
#endif
- case T_A:
- case T_AAAA:
- if (strcasecmp(hent->h_name, bp) != 0) {
- syslog(LOG_NOTICE|LOG_AUTH,
- AskedForGot, hent->h_name, bp);
- cp += n;
- continue; /* XXX - had_error++ ? */
- }
- if (n != hent->h_length) {
- cp += n;
- continue;
- }
- if (type == T_AAAA) {
- struct in6_addr in6;
- memcpy(&in6, cp, NS_IN6ADDRSZ);
- if (IN6_IS_ADDR_V4MAPPED(&in6)) {
- cp += n;
- continue;
- }
- }
- if (!haveanswer) {
- int nn;
+ case T_A:
+ case T_AAAA:
+ if (strcasecmp(hent->h_name, bp) != 0) {
+ syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, hent->h_name, bp);
+ cp += n;
+ continue; /* XXX - had_error++ ? */
+ }
+ if (n != hent->h_length) {
+ cp += n;
+ continue;
+ }
+ if (type == T_AAAA) {
+ struct in6_addr in6;
+ memcpy(&in6, cp, NS_IN6ADDRSZ);
+ if (IN6_IS_ADDR_V4MAPPED(&in6)) {
+ cp += n;
+ continue;
+ }
+ }
+ if (!haveanswer) {
+ int nn;
- hent->h_name = bp;
- nn = (int)strlen(bp) + 1; /* for the \0 */
- bp += nn;
- }
+ hent->h_name = bp;
+ nn = (int) strlen(bp) + 1; /* for the \0 */
+ bp += nn;
+ }
- bp += sizeof(align) -
- (size_t)((u_long)bp % sizeof(align));
+ bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align));
- if (bp + n >= ep) {
- debugprintf("size (%d) too big\n", res, n);
- had_error++;
- continue;
- }
- if (hap >= &addr_ptrs[MAXADDRS - 1]) {
- if (!toobig++) {
- debugprintf("Too many addresses (%d)\n",
- res, MAXADDRS);
- }
- cp += n;
- continue;
- }
- (void)memcpy(*hap++ = bp, cp, (size_t)n);
- bp += n;
- cp += n;
- if (cp != erdata)
- goto no_recovery;
- break;
- default:
- abort();
- }
- if (!had_error)
- haveanswer++;
- }
- if (haveanswer) {
- *ap = NULL;
- *hap = NULL;
- /*
- * Note: we sort even if host can take only one address
- * in its return structures - should give it the "best"
- * address in that case, not some random one
- */
- if (res->nsort && haveanswer > 1 && qtype == T_A)
- addrsort(addr_ptrs, haveanswer, res);
- if (!hent->h_name) {
- n = (int)strlen(qname) + 1; /* for the \0 */
- if (n > ep - bp || n >= MAXHOSTNAMELEN)
- goto no_recovery;
- strlcpy(bp, qname, (size_t)(ep - bp));
- hent->h_name = bp;
- bp += n;
- }
- if (res->options & RES_USE_INET6)
- map_v4v6_hostent(hent, &bp, ep);
- goto success;
- }
+ if (bp + n >= ep) {
+ debugprintf("size (%d) too big\n", res, n);
+ had_error++;
+ continue;
+ }
+ if (hap >= &addr_ptrs[MAXADDRS - 1]) {
+ if (!toobig++) {
+ debugprintf("Too many addresses (%d)\n", res, MAXADDRS);
+ }
+ cp += n;
+ continue;
+ }
+ (void) memcpy(*hap++ = bp, cp, (size_t) n);
+ bp += n;
+ cp += n;
+ if (cp != erdata) goto no_recovery;
+ break;
+ default:
+ abort();
+ }
+ if (!had_error) haveanswer++;
+ }
+ if (haveanswer) {
+ *ap = NULL;
+ *hap = NULL;
+ /*
+ * Note: we sort even if host can take only one address
+ * in its return structures - should give it the "best"
+ * address in that case, not some random one
+ */
+ if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res);
+ if (!hent->h_name) {
+ n = (int) strlen(qname) + 1; /* for the \0 */
+ if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
+ strlcpy(bp, qname, (size_t)(ep - bp));
+ hent->h_name = bp;
+ bp += n;
+ }
+ if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep);
+ goto success;
+ }
no_recovery:
- free(aliases);
- *he = NO_RECOVERY;
- return NULL;
+ free(aliases);
+ *he = NO_RECOVERY;
+ return NULL;
success:
- bp = (char *)ALIGN(bp);
- n = (int)(ap - aliases);
- qlen = (n + 1) * sizeof(*hent->h_aliases);
- if ((size_t)(ep - bp) < qlen)
- goto nospc;
- hent->h_aliases = (void *)bp;
- memcpy(bp, aliases, qlen);
- free(aliases);
- aliases = NULL;
+ bp = (char*) ALIGN(bp);
+ n = (int) (ap - aliases);
+ qlen = (n + 1) * sizeof(*hent->h_aliases);
+ if ((size_t)(ep - bp) < qlen) goto nospc;
+ hent->h_aliases = (void*) bp;
+ memcpy(bp, aliases, qlen);
+ free(aliases);
+ aliases = NULL;
- bp += qlen;
- n = (int)(hap - addr_ptrs);
- qlen = (n + 1) * sizeof(*hent->h_addr_list);
- if ((size_t)(ep - bp) < qlen)
- goto nospc;
- hent->h_addr_list = (void *)bp;
- memcpy(bp, addr_ptrs, qlen);
- *he = NETDB_SUCCESS;
- return hent;
+ bp += qlen;
+ n = (int) (hap - addr_ptrs);
+ qlen = (n + 1) * sizeof(*hent->h_addr_list);
+ if ((size_t)(ep - bp) < qlen) goto nospc;
+ hent->h_addr_list = (void*) bp;
+ memcpy(bp, addr_ptrs, qlen);
+ *he = NETDB_SUCCESS;
+ return hent;
nospc:
- free(aliases);
- errno = ENOSPC;
- *he = NETDB_INTERNAL;
- return NULL;
+ free(aliases);
+ errno = ENOSPC;
+ *he = NETDB_INTERNAL;
+ return NULL;
}
/* The prototype of gethostbyname_r is from glibc, not that in netbsd. */
-int
-gethostbyname_r(const char *name, struct hostent *hp, char *buf, size_t buflen,
- struct hostent **result, int *errorp)
-{
- res_state res = __res_get_state();
- if (res == NULL) {
- *result = NULL;
- *errorp = NETDB_INTERNAL;
- return -1;
- }
+int gethostbyname_r(const char* name, struct hostent* hp, char* buf, size_t buflen,
+ struct hostent** result, int* errorp) {
+ res_state res = __res_get_state();
+ if (res == NULL) {
+ *result = NULL;
+ *errorp = NETDB_INTERNAL;
+ return -1;
+ }
- _DIAGASSERT(name != NULL);
+ _DIAGASSERT(name != NULL);
- if (res->options & RES_USE_INET6) {
- *result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp,
- &NETCONTEXT_UNSET);
- if (*result) {
- __res_put_state(res);
- return 0;
- }
- }
- *result = gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp,
- &NETCONTEXT_UNSET);
- return h_errno_to_result(errorp);
+ if (res->options & RES_USE_INET6) {
+ *result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp,
+ &NETCONTEXT_UNSET);
+ if (*result) {
+ __res_put_state(res);
+ return 0;
+ }
+ }
+ *result =
+ gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET);
+ return h_errno_to_result(errorp);
}
/* The prototype of gethostbyname2_r is from glibc, not that in netbsd. */
-int
-gethostbyname2_r(const char *name, int af, struct hostent *hp, char *buf,
- size_t buflen, struct hostent **result, int *errorp)
-{
- res_state res = __res_get_state();
- if (res == NULL) {
- *result = NULL;
- *errorp = NETDB_INTERNAL;
- return -1;
- }
- *result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp,
- &NETCONTEXT_UNSET);
- return h_errno_to_result(errorp);
+int gethostbyname2_r(const char* name, int af, struct hostent* hp, char* buf, size_t buflen,
+ struct hostent** result, int* errorp) {
+ res_state res = __res_get_state();
+ if (res == NULL) {
+ *result = NULL;
+ *errorp = NETDB_INTERNAL;
+ return -1;
+ }
+ *result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET);
+ return h_errno_to_result(errorp);
}
__LIBC_HIDDEN__ FILE* android_open_proxy() {
- const char* cache_mode = getenv("ANDROID_DNS_MODE");
- bool use_proxy = (cache_mode == NULL || strcmp(cache_mode, "local") != 0);
- if (!use_proxy) {
- return NULL;
- }
+ const char* cache_mode = getenv("ANDROID_DNS_MODE");
+ bool use_proxy = (cache_mode == NULL || strcmp(cache_mode, "local") != 0);
+ if (!use_proxy) {
+ return NULL;
+ }
- int s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
- if (s == -1) {
- return NULL;
- }
+ int s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ if (s == -1) {
+ return NULL;
+ }
- const int one = 1;
- setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
+ const int one = 1;
+ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
- struct sockaddr_un proxy_addr;
- memset(&proxy_addr, 0, sizeof(proxy_addr));
- proxy_addr.sun_family = AF_UNIX;
- strlcpy(proxy_addr.sun_path, "/dev/socket/dnsproxyd", sizeof(proxy_addr.sun_path));
+ struct sockaddr_un proxy_addr;
+ memset(&proxy_addr, 0, sizeof(proxy_addr));
+ proxy_addr.sun_family = AF_UNIX;
+ strlcpy(proxy_addr.sun_path, "/dev/socket/dnsproxyd", sizeof(proxy_addr.sun_path));
- if (TEMP_FAILURE_RETRY(connect(s, (const struct sockaddr*) &proxy_addr, sizeof(proxy_addr))) != 0) {
- close(s);
- return NULL;
- }
+ if (TEMP_FAILURE_RETRY(connect(s, (const struct sockaddr*) &proxy_addr, sizeof(proxy_addr))) !=
+ 0) {
+ close(s);
+ return NULL;
+ }
- return fdopen(s, "r+");
+ return fdopen(s, "r+");
}
-static struct hostent *
-android_read_hostent(FILE* proxy, struct hostent* hp, char* hbuf, size_t hbuflen, int *he)
-{
- uint32_t size;
- char buf[4];
- if (fread(buf, 1, sizeof(buf), proxy) != sizeof(buf)) return NULL;
+static struct hostent* android_read_hostent(FILE* proxy, struct hostent* hp, char* hbuf,
+ size_t hbuflen, int* he) {
+ uint32_t size;
+ char buf[4];
+ if (fread(buf, 1, sizeof(buf), proxy) != sizeof(buf)) return NULL;
- // This is reading serialized data from system/netd/server/DnsProxyListener.cpp
- // and changes here need to be matched there.
- int result_code = strtol(buf, NULL, 10);
- if (result_code != DnsProxyQueryResult) {
- fread(&size, 1, sizeof(size), proxy);
- *he = HOST_NOT_FOUND;
- return NULL;
- }
+ // This is reading serialized data from system/netd/server/DnsProxyListener.cpp
+ // and changes here need to be matched there.
+ int result_code = strtol(buf, NULL, 10);
+ if (result_code != DnsProxyQueryResult) {
+ fread(&size, 1, sizeof(size), proxy);
+ *he = HOST_NOT_FOUND;
+ return NULL;
+ }
- if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
- size = ntohl(size);
+ if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
+ size = ntohl(size);
- memset(hp, 0, sizeof(*hp));
- char *ptr = hbuf;
- char *hbuf_end = hbuf + hbuflen;
+ memset(hp, 0, sizeof(*hp));
+ char* ptr = hbuf;
+ char* hbuf_end = hbuf + hbuflen;
- if (ptr + size > hbuf_end) {
- goto nospc;
- }
- if (fread(ptr, 1, size, proxy) != size) return NULL;
- hp->h_name = ptr;
- ptr += size;
+ if (ptr + size > hbuf_end) {
+ goto nospc;
+ }
+ if (fread(ptr, 1, size, proxy) != size) return NULL;
+ hp->h_name = ptr;
+ ptr += size;
- char *aliases_ptrs[MAXALIASES];
- char **aliases = &aliases_ptrs[0];
+ char* aliases_ptrs[MAXALIASES];
+ char** aliases = &aliases_ptrs[0];
- while (1) {
- if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
- size = ntohl(size);
+ while (1) {
+ if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
+ size = ntohl(size);
- if (size == 0) {
- *aliases = NULL;
- break;
- }
- if (ptr + size > hbuf_end) {
- goto nospc;
- }
- if (fread(ptr, 1, size, proxy) != size) return NULL;
- if (aliases < &aliases_ptrs[MAXALIASES - 1]) {
- *aliases++ = ptr;
- }
- ptr += size;
- }
+ if (size == 0) {
+ *aliases = NULL;
+ break;
+ }
+ if (ptr + size > hbuf_end) {
+ goto nospc;
+ }
+ if (fread(ptr, 1, size, proxy) != size) return NULL;
+ if (aliases < &aliases_ptrs[MAXALIASES - 1]) {
+ *aliases++ = ptr;
+ }
+ ptr += size;
+ }
- // Fix alignment after variable-length data.
- ptr = (char*)ALIGN(ptr);
+ // Fix alignment after variable-length data.
+ ptr = (char*) ALIGN(ptr);
- int aliases_len = ((int)(aliases - aliases_ptrs) + 1) * sizeof(*hp->h_aliases);
- if (ptr + aliases_len > hbuf_end) {
- goto nospc;
- }
- hp->h_aliases = (void*)ptr;
- memcpy(ptr, aliases_ptrs, aliases_len);
- ptr += aliases_len;
+ int aliases_len = ((int) (aliases - aliases_ptrs) + 1) * sizeof(*hp->h_aliases);
+ if (ptr + aliases_len > hbuf_end) {
+ goto nospc;
+ }
+ hp->h_aliases = (void*) ptr;
+ memcpy(ptr, aliases_ptrs, aliases_len);
+ ptr += aliases_len;
- if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
- hp->h_addrtype = ntohl(size);
+ if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
+ hp->h_addrtype = ntohl(size);
- if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
- hp->h_length = ntohl(size);
+ if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
+ hp->h_length = ntohl(size);
- char *addr_ptrs[MAXADDRS];
- char **addr_p = &addr_ptrs[0];
+ char* addr_ptrs[MAXADDRS];
+ char** addr_p = &addr_ptrs[0];
- while (1) {
- if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
- size = ntohl(size);
- if (size == 0) {
- *addr_p = NULL;
- break;
- }
- if (ptr + size > hbuf_end) {
- goto nospc;
- }
- if (fread(ptr, 1, size, proxy) != size) return NULL;
- if (addr_p < &addr_ptrs[MAXADDRS - 1]) {
- *addr_p++ = ptr;
- }
- ptr += size;
- }
+ while (1) {
+ if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
+ size = ntohl(size);
+ if (size == 0) {
+ *addr_p = NULL;
+ break;
+ }
+ if (ptr + size > hbuf_end) {
+ goto nospc;
+ }
+ if (fread(ptr, 1, size, proxy) != size) return NULL;
+ if (addr_p < &addr_ptrs[MAXADDRS - 1]) {
+ *addr_p++ = ptr;
+ }
+ ptr += size;
+ }
- // Fix alignment after variable-length data.
- ptr = (char*)ALIGN(ptr);
+ // Fix alignment after variable-length data.
+ ptr = (char*) ALIGN(ptr);
- int addrs_len = ((int)(addr_p - addr_ptrs) + 1) * sizeof(*hp->h_addr_list);
- if (ptr + addrs_len > hbuf_end) {
- goto nospc;
- }
- hp->h_addr_list = (void*)ptr;
- memcpy(ptr, addr_ptrs, addrs_len);
- *he = NETDB_SUCCESS;
- return hp;
+ int addrs_len = ((int) (addr_p - addr_ptrs) + 1) * sizeof(*hp->h_addr_list);
+ if (ptr + addrs_len > hbuf_end) {
+ goto nospc;
+ }
+ hp->h_addr_list = (void*) ptr;
+ memcpy(ptr, addr_ptrs, addrs_len);
+ *he = NETDB_SUCCESS;
+ return hp;
nospc:
- *he = NETDB_INTERNAL;
- errno = ENOSPC;
- return NULL;
+ *he = NETDB_INTERNAL;
+ errno = ENOSPC;
+ return NULL;
}
-static struct hostent *
-gethostbyname_internal_real(const char *name, int af, res_state res, struct hostent *hp, char *buf,
- size_t buflen, int *he)
-{
- const char *cp;
- struct getnamaddr info;
- char hbuf[MAXHOSTNAMELEN];
- size_t size;
- static const ns_dtab dtab[] = {
- NS_FILES_CB(_hf_gethtbyname, NULL)
- { NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */
- NS_NIS_CB(_yp_gethtbyname, NULL)
- NS_NULL_CB
- };
+static struct hostent* gethostbyname_internal_real(const char* name, int af, res_state res,
+ struct hostent* hp, char* buf, size_t buflen,
+ int* he) {
+ const char* cp;
+ struct getnamaddr info;
+ char hbuf[MAXHOSTNAMELEN];
+ size_t size;
+ static const ns_dtab dtab[] = {NS_FILES_CB(_hf_gethtbyname, NULL){NSSRC_DNS, _dns_gethtbyname,
+ NULL}, /* force -DHESIOD */
+ NS_NIS_CB(_yp_gethtbyname, NULL) NS_NULL_CB};
- _DIAGASSERT(name != NULL);
+ _DIAGASSERT(name != NULL);
- switch (af) {
- case AF_INET:
- size = NS_INADDRSZ;
- break;
- case AF_INET6:
- size = NS_IN6ADDRSZ;
- break;
- default:
- *he = NETDB_INTERNAL;
- errno = EAFNOSUPPORT;
- return NULL;
- }
- if (buflen < size)
- goto nospc;
+ switch (af) {
+ case AF_INET:
+ size = NS_INADDRSZ;
+ break;
+ case AF_INET6:
+ size = NS_IN6ADDRSZ;
+ break;
+ default:
+ *he = NETDB_INTERNAL;
+ errno = EAFNOSUPPORT;
+ return NULL;
+ }
+ if (buflen < size) goto nospc;
- hp->h_addrtype = af;
- hp->h_length = (int)size;
+ hp->h_addrtype = af;
+ hp->h_length = (int) size;
- /*
- * if there aren't any dots, it could be a user-level alias.
- * this is also done in res_nquery() since we are not the only
- * function that looks up host names.
- */
- if (!strchr(name, '.') && (cp = res_hostalias(res, name,
- hbuf, sizeof(hbuf))))
- name = cp;
+ /*
+ * if there aren't any dots, it could be a user-level alias.
+ * this is also done in res_nquery() since we are not the only
+ * function that looks up host names.
+ */
+ if (!strchr(name, '.') && (cp = res_hostalias(res, name, hbuf, sizeof(hbuf)))) name = cp;
- /*
- * disallow names consisting only of digits/dots, unless
- * they end in a dot.
- */
- if (isdigit((u_char) name[0]))
- for (cp = name;; ++cp) {
- if (!*cp) {
- if (*--cp == '.')
- break;
- /*
- * All-numeric, no dot at the end.
- * Fake up a hostent as if we'd actually
- * done a lookup.
- */
- goto fake;
- }
- if (!isdigit((u_char) *cp) && *cp != '.')
- break;
- }
- if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) ||
- name[0] == ':')
- for (cp = name;; ++cp) {
- if (!*cp) {
- if (*--cp == '.')
- break;
- /*
- * All-IPv6-legal, no dot at the end.
- * Fake up a hostent as if we'd actually
- * done a lookup.
- */
- goto fake;
- }
- if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.')
- break;
- }
+ /*
+ * disallow names consisting only of digits/dots, unless
+ * they end in a dot.
+ */
+ if (isdigit((u_char) name[0]))
+ for (cp = name;; ++cp) {
+ if (!*cp) {
+ if (*--cp == '.') break;
+ /*
+ * All-numeric, no dot at the end.
+ * Fake up a hostent as if we'd actually
+ * done a lookup.
+ */
+ goto fake;
+ }
+ if (!isdigit((u_char) *cp) && *cp != '.') break;
+ }
+ if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':')
+ for (cp = name;; ++cp) {
+ if (!*cp) {
+ if (*--cp == '.') break;
+ /*
+ * All-IPv6-legal, no dot at the end.
+ * Fake up a hostent as if we'd actually
+ * done a lookup.
+ */
+ goto fake;
+ }
+ if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break;
+ }
- *he = NETDB_INTERNAL;
- info.hp = hp;
- info.buf = buf;
- info.buflen = buflen;
- info.he = he;
- if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyname",
- default_dns_files, name, strlen(name), af) != NS_SUCCESS)
- return NULL;
- *he = NETDB_SUCCESS;
- return hp;
+ *he = NETDB_INTERNAL;
+ info.hp = hp;
+ info.buf = buf;
+ info.buflen = buflen;
+ info.he = he;
+ if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyname", default_dns_files, name, strlen(name),
+ af) != NS_SUCCESS)
+ return NULL;
+ *he = NETDB_SUCCESS;
+ return hp;
nospc:
- *he = NETDB_INTERNAL;
- errno = ENOSPC;
- return NULL;
+ *he = NETDB_INTERNAL;
+ errno = ENOSPC;
+ return NULL;
fake:
- HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
- HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
+ HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
+ HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
- hp->h_aliases[0] = NULL;
- if (size > buflen)
- goto nospc;
+ hp->h_aliases[0] = NULL;
+ if (size > buflen) goto nospc;
- if (inet_pton(af, name, buf) <= 0) {
- *he = HOST_NOT_FOUND;
- return NULL;
- }
- hp->h_addr_list[0] = buf;
- hp->h_addr_list[1] = NULL;
- buf += size;
- buflen -= size;
- HENT_SCOPY(hp->h_name, name, buf, buflen);
- if (res->options & RES_USE_INET6)
- map_v4v6_hostent(hp, &buf, buf + buflen);
- *he = NETDB_SUCCESS;
- return hp;
+ if (inet_pton(af, name, buf) <= 0) {
+ *he = HOST_NOT_FOUND;
+ return NULL;
+ }
+ hp->h_addr_list[0] = buf;
+ hp->h_addr_list[1] = NULL;
+ buf += size;
+ buflen -= size;
+ HENT_SCOPY(hp->h_name, name, buf, buflen);
+ if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen);
+ *he = NETDB_SUCCESS;
+ return hp;
}
// very similar in proxy-ness to android_getaddrinfo_proxy
-static struct hostent *
-gethostbyname_internal(const char *name, int af, res_state res, struct hostent *hp, char *hbuf,
- size_t hbuflen, int *errorp, const struct android_net_context *netcontext)
-{
- FILE* proxy = android_open_proxy();
- if (proxy == NULL) {
- // Either we're not supposed to be using the proxy or the proxy is unavailable.
- res_setnetcontext(res, netcontext);
- return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
- }
+static struct hostent* gethostbyname_internal(const char* name, int af, res_state res,
+ struct hostent* hp, char* hbuf, size_t hbuflen,
+ int* errorp,
+ const struct android_net_context* netcontext) {
+ FILE* proxy = android_open_proxy();
+ if (proxy == NULL) {
+ // Either we're not supposed to be using the proxy or the proxy is unavailable.
+ res_setnetcontext(res, netcontext);
+ return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
+ }
- unsigned netid = __netdClientDispatch.netIdForResolv(netcontext->app_netid);
+ unsigned netid = __netdClientDispatch.netIdForResolv(netcontext->app_netid);
- // This is writing to system/netd/server/DnsProxyListener.cpp and changes
- // here need to be matched there.
- if (fprintf(proxy, "gethostbyname %u %s %d",
- netid,
- name == NULL ? "^" : name,
- af) < 0) {
- fclose(proxy);
- return NULL;
- }
+ // This is writing to system/netd/server/DnsProxyListener.cpp and changes
+ // here need to be matched there.
+ if (fprintf(proxy, "gethostbyname %u %s %d", netid, name == NULL ? "^" : name, af) < 0) {
+ fclose(proxy);
+ return NULL;
+ }
- if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
- fclose(proxy);
- return NULL;
- }
+ if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
+ fclose(proxy);
+ return NULL;
+ }
- struct hostent* result = android_read_hostent(proxy, hp, hbuf, hbuflen, errorp);
- fclose(proxy);
- return result;
+ struct hostent* result = android_read_hostent(proxy, hp, hbuf, hbuflen, errorp);
+ fclose(proxy);
+ return result;
}
/* The prototype of gethostbyaddr_r is from glibc, not that in netbsd. */
-int gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *hp, char *buf,
- size_t buflen, struct hostent **result, int *h_errnop)
-{
- *result = android_gethostbyaddrfornetcontext_proxy_internal(
- addr, len, af, hp, buf, buflen, h_errnop, &NETCONTEXT_UNSET);
- return h_errno_to_result(h_errnop);
+int gethostbyaddr_r(const void* addr, socklen_t len, int af, struct hostent* hp, char* buf,
+ size_t buflen, struct hostent** result, int* h_errnop) {
+ *result = android_gethostbyaddrfornetcontext_proxy_internal(addr, len, af, hp, buf, buflen,
+ h_errnop, &NETCONTEXT_UNSET);
+ return h_errno_to_result(h_errnop);
}
-static struct hostent *
-android_gethostbyaddrfornetcontext_real(const void *addr, socklen_t len, int af, struct hostent *hp,
- char *buf, size_t buflen, int *he,
- const struct android_net_context *netcontext)
-{
- const u_char *uaddr = (const u_char *)addr;
- socklen_t size;
- struct getnamaddr info;
- static const ns_dtab dtab[] = {
- NS_FILES_CB(_hf_gethtbyaddr, NULL)
- { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */
- NS_NIS_CB(_yp_gethtbyaddr, NULL)
- NS_NULL_CB
- };
+static struct hostent* android_gethostbyaddrfornetcontext_real(
+ const void* addr, socklen_t len, int af, struct hostent* hp, char* buf, size_t buflen,
+ int* he, const struct android_net_context* netcontext) {
+ const u_char* uaddr = (const u_char*) addr;
+ socklen_t size;
+ struct getnamaddr info;
+ static const ns_dtab dtab[] = {NS_FILES_CB(_hf_gethtbyaddr, NULL){NSSRC_DNS, _dns_gethtbyaddr,
+ NULL}, /* force -DHESIOD */
+ NS_NIS_CB(_yp_gethtbyaddr, NULL) NS_NULL_CB};
- _DIAGASSERT(addr != NULL);
+ _DIAGASSERT(addr != NULL);
- if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
- (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)addr) ||
- IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)addr))) {
- *he = HOST_NOT_FOUND;
- return NULL;
- }
- if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
- (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)addr) ||
- IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)addr))) {
- /* Unmap. */
- uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
- addr = uaddr;
- af = AF_INET;
- len = NS_INADDRSZ;
- }
- switch (af) {
- case AF_INET:
- size = NS_INADDRSZ;
- break;
- case AF_INET6:
- size = NS_IN6ADDRSZ;
- break;
- default:
- errno = EAFNOSUPPORT;
- *he = NETDB_INTERNAL;
- return NULL;
- }
- if (size != len) {
- errno = EINVAL;
- *he = NETDB_INTERNAL;
- return NULL;
- }
- info.hp = hp;
- info.buf = buf;
- info.buflen = buflen;
- info.he = he;
- *he = NETDB_INTERNAL;
- if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyaddr",
- default_dns_files, uaddr, len, af, netcontext) != NS_SUCCESS)
- return NULL;
- *he = NETDB_SUCCESS;
- return hp;
+ if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
+ (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
+ IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
+ *he = HOST_NOT_FOUND;
+ return NULL;
+ }
+ if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
+ (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
+ IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
+ /* Unmap. */
+ uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
+ addr = uaddr;
+ af = AF_INET;
+ len = NS_INADDRSZ;
+ }
+ switch (af) {
+ case AF_INET:
+ size = NS_INADDRSZ;
+ break;
+ case AF_INET6:
+ size = NS_IN6ADDRSZ;
+ break;
+ default:
+ errno = EAFNOSUPPORT;
+ *he = NETDB_INTERNAL;
+ return NULL;
+ }
+ if (size != len) {
+ errno = EINVAL;
+ *he = NETDB_INTERNAL;
+ return NULL;
+ }
+ info.hp = hp;
+ info.buf = buf;
+ info.buflen = buflen;
+ info.he = he;
+ *he = NETDB_INTERNAL;
+ if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyaddr", default_dns_files, uaddr, len, af,
+ netcontext) != NS_SUCCESS)
+ return NULL;
+ *he = NETDB_SUCCESS;
+ return hp;
}
-static struct hostent*
-android_gethostbyaddrfornetcontext_proxy_internal(const void* addr, socklen_t len, int af,
- struct hostent *hp, char *hbuf, size_t hbuflen, int *he,
- const struct android_net_context *netcontext)
-{
- FILE* proxy = android_open_proxy();
- if (proxy == NULL) {
- // Either we're not supposed to be using the proxy or the proxy is unavailable.
- return android_gethostbyaddrfornetcontext_real(addr,len, af, hp, hbuf, hbuflen, he, netcontext);
- }
+static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
+ const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
+ int* he, const struct android_net_context* netcontext) {
+ FILE* proxy = android_open_proxy();
+ if (proxy == NULL) {
+ // Either we're not supposed to be using the proxy or the proxy is unavailable.
+ return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, he,
+ netcontext);
+ }
- char buf[INET6_ADDRSTRLEN]; //big enough for IPv4 and IPv6
- const char * addrStr = inet_ntop(af, addr, buf, sizeof(buf));
- if (addrStr == NULL) {
- fclose(proxy);
- return NULL;
- }
+ char buf[INET6_ADDRSTRLEN]; // big enough for IPv4 and IPv6
+ const char* addrStr = inet_ntop(af, addr, buf, sizeof(buf));
+ if (addrStr == NULL) {
+ fclose(proxy);
+ return NULL;
+ }
- unsigned netid = __netdClientDispatch.netIdForResolv(netcontext->app_netid);
+ unsigned netid = __netdClientDispatch.netIdForResolv(netcontext->app_netid);
- if (fprintf(proxy, "gethostbyaddr %s %d %d %u",
- addrStr, len, af, netid) < 0) {
- fclose(proxy);
- return NULL;
- }
+ if (fprintf(proxy, "gethostbyaddr %s %d %d %u", addrStr, len, af, netid) < 0) {
+ fclose(proxy);
+ return NULL;
+ }
- if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
- fclose(proxy);
- return NULL;
- }
+ if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
+ fclose(proxy);
+ return NULL;
+ }
- struct hostent *result = android_read_hostent(proxy, hp, hbuf, hbuflen, he);
- fclose(proxy);
- return result;
+ struct hostent* result = android_read_hostent(proxy, hp, hbuf, hbuflen, he);
+ fclose(proxy);
+ return result;
}
-struct hostent*
-netbsd_gethostent_r(FILE *hf, struct hostent *hent, char *buf, size_t buflen, int *he)
-{
- char *p, *name;
- char *cp, **q;
- int af, len;
- size_t anum;
- char **aliases;
- size_t maxaliases;
- struct in6_addr host_addr;
+struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
+ int* he) {
+ char *p, *name;
+ char *cp, **q;
+ int af, len;
+ size_t anum;
+ char** aliases;
+ size_t maxaliases;
+ struct in6_addr host_addr;
- if (hf == NULL) {
- *he = NETDB_INTERNAL;
- errno = EINVAL;
- return NULL;
- }
- p = NULL;
- setup(aliases, maxaliases);
+ if (hf == NULL) {
+ *he = NETDB_INTERNAL;
+ errno = EINVAL;
+ return NULL;
+ }
+ p = NULL;
+ setup(aliases, maxaliases);
- /* Allocate a new space to read file lines like upstream does.
- * To keep reentrancy we cannot use __res_get_static()->hostbuf here,
- * as the buffer may be used to store content for a previous hostent
- * returned by non-reentrant functions like gethostbyname().
- */
- const size_t line_buf_size = sizeof(__res_get_static()->hostbuf);
- if ((p = malloc(line_buf_size)) == NULL) {
- goto nospc;
- }
- for (;;) {
- if (!fgets(p, line_buf_size, hf)) {
- free(p);
- free(aliases);
- *he = HOST_NOT_FOUND;
- return NULL;
- }
- if (*p == '#') {
- continue;
- }
- if (!(cp = strpbrk(p, "#\n"))) {
- continue;
- }
- *cp = '\0';
- if (!(cp = strpbrk(p, " \t")))
- continue;
- *cp++ = '\0';
- if (inet_pton(AF_INET6, p, &host_addr) > 0) {
- af = AF_INET6;
- len = NS_IN6ADDRSZ;
- } else {
- if (inet_pton(AF_INET, p, &host_addr) <= 0)
- continue;
+ /* Allocate a new space to read file lines like upstream does.
+ * To keep reentrancy we cannot use __res_get_static()->hostbuf here,
+ * as the buffer may be used to store content for a previous hostent
+ * returned by non-reentrant functions like gethostbyname().
+ */
+ const size_t line_buf_size = sizeof(__res_get_static()->hostbuf);
+ if ((p = malloc(line_buf_size)) == NULL) {
+ goto nospc;
+ }
+ for (;;) {
+ if (!fgets(p, line_buf_size, hf)) {
+ free(p);
+ free(aliases);
+ *he = HOST_NOT_FOUND;
+ return NULL;
+ }
+ if (*p == '#') {
+ continue;
+ }
+ if (!(cp = strpbrk(p, "#\n"))) {
+ continue;
+ }
+ *cp = '\0';
+ if (!(cp = strpbrk(p, " \t"))) continue;
+ *cp++ = '\0';
+ if (inet_pton(AF_INET6, p, &host_addr) > 0) {
+ af = AF_INET6;
+ len = NS_IN6ADDRSZ;
+ } else {
+ if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
- res_state res = __res_get_state();
- if (res == NULL)
- goto nospc;
- if (res->options & RES_USE_INET6) {
- map_v4v6_address(buf, buf);
- af = AF_INET6;
- len = NS_IN6ADDRSZ;
- } else {
- af = AF_INET;
- len = NS_INADDRSZ;
- }
- __res_put_state(res);
- }
+ res_state res = __res_get_state();
+ if (res == NULL) goto nospc;
+ if (res->options & RES_USE_INET6) {
+ map_v4v6_address(buf, buf);
+ af = AF_INET6;
+ len = NS_IN6ADDRSZ;
+ } else {
+ af = AF_INET;
+ len = NS_INADDRSZ;
+ }
+ __res_put_state(res);
+ }
- /* if this is not something we're looking for, skip it. */
- if (hent->h_addrtype != 0 && hent->h_addrtype != af)
- continue;
- if (hent->h_length != 0 && hent->h_length != len)
- continue;
+ /* if this is not something we're looking for, skip it. */
+ if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
+ if (hent->h_length != 0 && hent->h_length != len) continue;
- while (*cp == ' ' || *cp == '\t')
- cp++;
- if ((cp = strpbrk(name = cp, " \t")) != NULL)
- *cp++ = '\0';
- q = aliases;
- while (cp && *cp) {
- if (*cp == ' ' || *cp == '\t') {
- cp++;
- continue;
- }
- addalias(q, cp, aliases, maxaliases);
- if ((cp = strpbrk(cp, " \t")) != NULL)
- *cp++ = '\0';
- }
- break;
- }
- hent->h_length = len;
- hent->h_addrtype = af;
- HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
- anum = (size_t)(q - aliases);
- HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
- HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf,
- buflen);
- hent->h_addr_list[1] = NULL;
+ while (*cp == ' ' || *cp == '\t') cp++;
+ if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
+ q = aliases;
+ while (cp && *cp) {
+ if (*cp == ' ' || *cp == '\t') {
+ cp++;
+ continue;
+ }
+ addalias(q, cp, aliases, maxaliases);
+ if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
+ }
+ break;
+ }
+ hent->h_length = len;
+ hent->h_addrtype = af;
+ HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
+ anum = (size_t)(q - aliases);
+ HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
+ HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
+ hent->h_addr_list[1] = NULL;
- HENT_SCOPY(hent->h_name, name, buf, buflen);
- for (size_t i = 0; i < anum; i++)
- HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
- hent->h_aliases[anum] = NULL;
+ HENT_SCOPY(hent->h_name, name, buf, buflen);
+ for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
+ hent->h_aliases[anum] = NULL;
- *he = NETDB_SUCCESS;
- free(p);
- free(aliases);
- return hent;
+ *he = NETDB_SUCCESS;
+ free(p);
+ free(aliases);
+ return hent;
nospc:
- free(p);
- free(aliases);
- errno = ENOSPC;
- *he = NETDB_INTERNAL;
- return NULL;
+ free(p);
+ free(aliases);
+ errno = ENOSPC;
+ *he = NETDB_INTERNAL;
+ return NULL;
}
-static void
-map_v4v6_address(const char *src, char *dst)
-{
- u_char *p = (u_char *)dst;
- char tmp[NS_INADDRSZ];
- int i;
+static void map_v4v6_address(const char* src, char* dst) {
+ u_char* p = (u_char*) dst;
+ char tmp[NS_INADDRSZ];
+ int i;
- _DIAGASSERT(src != NULL);
- _DIAGASSERT(dst != NULL);
+ _DIAGASSERT(src != NULL);
+ _DIAGASSERT(dst != NULL);
- /* Stash a temporary copy so our caller can update in place. */
- (void)memcpy(tmp, src, NS_INADDRSZ);
- /* Mark this ipv6 addr as a mapped ipv4. */
- for (i = 0; i < 10; i++)
- *p++ = 0x00;
- *p++ = 0xff;
- *p++ = 0xff;
- /* Retrieve the saved copy and we're done. */
- (void)memcpy(p, tmp, NS_INADDRSZ);
+ /* Stash a temporary copy so our caller can update in place. */
+ (void) memcpy(tmp, src, NS_INADDRSZ);
+ /* Mark this ipv6 addr as a mapped ipv4. */
+ for (i = 0; i < 10; i++) *p++ = 0x00;
+ *p++ = 0xff;
+ *p++ = 0xff;
+ /* Retrieve the saved copy and we're done. */
+ (void) memcpy(p, tmp, NS_INADDRSZ);
}
-static void
-map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep)
-{
- char **ap;
+static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
+ char** ap;
- _DIAGASSERT(hp != NULL);
- _DIAGASSERT(bpp != NULL);
- _DIAGASSERT(ep != NULL);
+ _DIAGASSERT(hp != NULL);
+ _DIAGASSERT(bpp != NULL);
+ _DIAGASSERT(ep != NULL);
- if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ)
- return;
- hp->h_addrtype = AF_INET6;
- hp->h_length = NS_IN6ADDRSZ;
- for (ap = hp->h_addr_list; *ap; ap++) {
- int i = (int)(sizeof(align) -
- (size_t)((u_long)*bpp % sizeof(align)));
+ if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
+ hp->h_addrtype = AF_INET6;
+ hp->h_length = NS_IN6ADDRSZ;
+ for (ap = hp->h_addr_list; *ap; ap++) {
+ int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align)));
- if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
- /* Out of memory. Truncate address list here. XXX */
- *ap = NULL;
- return;
- }
- *bpp += i;
- map_v4v6_address(*ap, *bpp);
- *ap = *bpp;
- *bpp += NS_IN6ADDRSZ;
- }
+ if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
+ /* Out of memory. Truncate address list here. XXX */
+ *ap = NULL;
+ return;
+ }
+ *bpp += i;
+ map_v4v6_address(*ap, *bpp);
+ *ap = *bpp;
+ *bpp += NS_IN6ADDRSZ;
+ }
}
-static void
-addrsort(char **ap, int num, res_state res)
-{
- int i, j;
- char **p;
- short aval[MAXADDRS];
- int needsort = 0;
+static void addrsort(char** ap, int num, res_state res) {
+ int i, j;
+ char** p;
+ short aval[MAXADDRS];
+ int needsort = 0;
- _DIAGASSERT(ap != NULL);
+ _DIAGASSERT(ap != NULL);
- p = ap;
- for (i = 0; i < num; i++, p++) {
- for (j = 0 ; (unsigned)j < res->nsort; j++)
- if (res->sort_list[j].addr.s_addr ==
- (((struct in_addr *)(void *)(*p))->s_addr &
- res->sort_list[j].mask))
- break;
- aval[i] = j;
- if (needsort == 0 && i > 0 && j < aval[i-1])
- needsort = i;
- }
- if (!needsort)
- return;
+ p = ap;
+ for (i = 0; i < num; i++, p++) {
+ for (j = 0; (unsigned) j < res->nsort; j++)
+ if (res->sort_list[j].addr.s_addr ==
+ (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask))
+ break;
+ aval[i] = j;
+ if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i;
+ }
+ if (!needsort) return;
- while (needsort < num) {
- for (j = needsort - 1; j >= 0; j--) {
- if (aval[j] > aval[j+1]) {
- char *hp;
+ while (needsort < num) {
+ for (j = needsort - 1; j >= 0; j--) {
+ if (aval[j] > aval[j + 1]) {
+ char* hp;
- i = aval[j];
- aval[j] = aval[j+1];
- aval[j+1] = i;
+ i = aval[j];
+ aval[j] = aval[j + 1];
+ aval[j + 1] = i;
- hp = ap[j];
- ap[j] = ap[j+1];
- ap[j+1] = hp;
- } else
- break;
- }
- needsort++;
- }
+ hp = ap[j];
+ ap[j] = ap[j + 1];
+ ap[j + 1] = hp;
+ } else
+ break;
+ }
+ needsort++;
+ }
}
/*ARGSUSED*/
-static int
-_dns_gethtbyname(void *rv, void *cb_data, va_list ap)
-{
- querybuf *buf;
- int n, type;
- struct hostent *hp;
- const char *name;
- res_state res;
- struct getnamaddr *info = rv;
+static int _dns_gethtbyname(void* rv, void* cb_data, va_list ap) {
+ querybuf* buf;
+ int n, type;
+ struct hostent* hp;
+ const char* name;
+ res_state res;
+ struct getnamaddr* info = rv;
- _DIAGASSERT(rv != NULL);
+ _DIAGASSERT(rv != NULL);
- name = va_arg(ap, char *);
- /* NOSTRICT skip string len */(void)va_arg(ap, int);
- info->hp->h_addrtype = va_arg(ap, int);
+ name = va_arg(ap, char*);
+ /* NOSTRICT skip string len */ (void) va_arg(ap, int);
+ info->hp->h_addrtype = va_arg(ap, int);
- switch (info->hp->h_addrtype) {
- case AF_INET:
- info->hp->h_length = NS_INADDRSZ;
- type = T_A;
- break;
- case AF_INET6:
- info->hp->h_length = NS_IN6ADDRSZ;
- type = T_AAAA;
- break;
- default:
- return NS_UNAVAIL;
- }
- buf = malloc(sizeof(*buf));
- if (buf == NULL) {
- *info->he = NETDB_INTERNAL;
- return NS_NOTFOUND;
- }
- res = __res_get_state();
- if (res == NULL) {
- free(buf);
- return NS_NOTFOUND;
- }
- n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf));
- if (n < 0) {
- free(buf);
- debugprintf("res_nsearch failed (%d)\n", res, n);
- __res_put_state(res);
- return NS_NOTFOUND;
- }
- hp = getanswer(buf, n, name, type, res, info->hp, info->buf,
- info->buflen, info->he);
- free(buf);
- __res_put_state(res);
- if (hp == NULL)
- switch (*info->he) {
- case HOST_NOT_FOUND:
- return NS_NOTFOUND;
- case TRY_AGAIN:
- return NS_TRYAGAIN;
- default:
- return NS_UNAVAIL;
- }
- return NS_SUCCESS;
+ switch (info->hp->h_addrtype) {
+ case AF_INET:
+ info->hp->h_length = NS_INADDRSZ;
+ type = T_A;
+ break;
+ case AF_INET6:
+ info->hp->h_length = NS_IN6ADDRSZ;
+ type = T_AAAA;
+ break;
+ default:
+ return NS_UNAVAIL;
+ }
+ buf = malloc(sizeof(*buf));
+ if (buf == NULL) {
+ *info->he = NETDB_INTERNAL;
+ return NS_NOTFOUND;
+ }
+ res = __res_get_state();
+ if (res == NULL) {
+ free(buf);
+ return NS_NOTFOUND;
+ }
+ n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf));
+ if (n < 0) {
+ free(buf);
+ debugprintf("res_nsearch failed (%d)\n", res, n);
+ __res_put_state(res);
+ return NS_NOTFOUND;
+ }
+ hp = getanswer(buf, n, name, type, res, info->hp, info->buf, info->buflen, info->he);
+ free(buf);
+ __res_put_state(res);
+ if (hp == NULL) switch (*info->he) {
+ case HOST_NOT_FOUND:
+ return NS_NOTFOUND;
+ case TRY_AGAIN:
+ return NS_TRYAGAIN;
+ default:
+ return NS_UNAVAIL;
+ }
+ return NS_SUCCESS;
}
/*ARGSUSED*/
-static int
-_dns_gethtbyaddr(void *rv, void *cb_data, va_list ap)
-{
- char qbuf[MAXDNAME + 1], *qp, *ep;
- int n;
- querybuf *buf;
- struct hostent *hp;
- const unsigned char *uaddr;
- int advance;
- res_state res;
- char *bf;
- size_t blen;
- struct getnamaddr *info = rv;
- const struct android_net_context *netcontext;
+static int _dns_gethtbyaddr(void* rv, void* cb_data, va_list ap) {
+ char qbuf[MAXDNAME + 1], *qp, *ep;
+ int n;
+ querybuf* buf;
+ struct hostent* hp;
+ const unsigned char* uaddr;
+ int advance;
+ res_state res;
+ char* bf;
+ size_t blen;
+ struct getnamaddr* info = rv;
+ const struct android_net_context* netcontext;
- _DIAGASSERT(rv != NULL);
+ _DIAGASSERT(rv != NULL);
- uaddr = va_arg(ap, unsigned char *);
- info->hp->h_length = va_arg(ap, int);
- info->hp->h_addrtype = va_arg(ap, int);
- netcontext = va_arg(ap, const struct android_net_context *);
+ uaddr = va_arg(ap, unsigned char*);
+ info->hp->h_length = va_arg(ap, int);
+ info->hp->h_addrtype = va_arg(ap, int);
+ netcontext = va_arg(ap, const struct android_net_context*);
- switch (info->hp->h_addrtype) {
- case AF_INET:
- (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
- (uaddr[3] & 0xff), (uaddr[2] & 0xff),
- (uaddr[1] & 0xff), (uaddr[0] & 0xff));
- break;
+ switch (info->hp->h_addrtype) {
+ case AF_INET:
+ (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
+ (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
+ break;
- case AF_INET6:
- qp = qbuf;
- ep = qbuf + sizeof(qbuf) - 1;
- for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
- advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.",
- uaddr[n] & 0xf,
- ((unsigned int)uaddr[n] >> 4) & 0xf);
- if (advance > 0 && qp + advance < ep)
- qp += advance;
- else {
- *info->he = NETDB_INTERNAL;
- return NS_NOTFOUND;
- }
- }
- if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
- *info->he = NETDB_INTERNAL;
- return NS_NOTFOUND;
- }
- break;
- default:
- return NS_UNAVAIL;
- }
+ case AF_INET6:
+ qp = qbuf;
+ ep = qbuf + sizeof(qbuf) - 1;
+ for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
+ advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
+ ((unsigned int) uaddr[n] >> 4) & 0xf);
+ if (advance > 0 && qp + advance < ep)
+ qp += advance;
+ else {
+ *info->he = NETDB_INTERNAL;
+ return NS_NOTFOUND;
+ }
+ }
+ if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
+ *info->he = NETDB_INTERNAL;
+ return NS_NOTFOUND;
+ }
+ break;
+ default:
+ return NS_UNAVAIL;
+ }
- buf = malloc(sizeof(*buf));
- if (buf == NULL) {
- *info->he = NETDB_INTERNAL;
- return NS_NOTFOUND;
- }
- res = __res_get_state();
- if (res == NULL) {
- free(buf);
- return NS_NOTFOUND;
- }
- res_setnetcontext(res, netcontext);
- n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf));
- if (n < 0) {
- free(buf);
- debugprintf("res_nquery failed (%d)\n", res, n);
- __res_put_state(res);
- return NS_NOTFOUND;
- }
- hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf,
- info->buflen, info->he);
- free(buf);
- if (hp == NULL) {
- __res_put_state(res);
- switch (*info->he) {
- case HOST_NOT_FOUND:
- return NS_NOTFOUND;
- case TRY_AGAIN:
- return NS_TRYAGAIN;
- default:
- return NS_UNAVAIL;
- }
- }
+ buf = malloc(sizeof(*buf));
+ if (buf == NULL) {
+ *info->he = NETDB_INTERNAL;
+ return NS_NOTFOUND;
+ }
+ res = __res_get_state();
+ if (res == NULL) {
+ free(buf);
+ return NS_NOTFOUND;
+ }
+ res_setnetcontext(res, netcontext);
+ n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf));
+ if (n < 0) {
+ free(buf);
+ debugprintf("res_nquery failed (%d)\n", res, n);
+ __res_put_state(res);
+ return NS_NOTFOUND;
+ }
+ hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, info->he);
+ free(buf);
+ if (hp == NULL) {
+ __res_put_state(res);
+ switch (*info->he) {
+ case HOST_NOT_FOUND:
+ return NS_NOTFOUND;
+ case TRY_AGAIN:
+ return NS_TRYAGAIN;
+ default:
+ return NS_UNAVAIL;
+ }
+ }
- bf = (void *)(hp->h_addr_list + 2);
- blen = (size_t)(bf - info->buf);
- if (blen + info->hp->h_length > info->buflen)
- goto nospc;
- hp->h_addr_list[0] = bf;
- hp->h_addr_list[1] = NULL;
- (void)memcpy(bf, uaddr, (size_t)info->hp->h_length);
- if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
- if (blen + NS_IN6ADDRSZ > info->buflen)
- goto nospc;
- map_v4v6_address(bf, bf);
- hp->h_addrtype = AF_INET6;
- hp->h_length = NS_IN6ADDRSZ;
- }
+ bf = (void*) (hp->h_addr_list + 2);
+ blen = (size_t)(bf - info->buf);
+ if (blen + info->hp->h_length > info->buflen) goto nospc;
+ hp->h_addr_list[0] = bf;
+ hp->h_addr_list[1] = NULL;
+ (void) memcpy(bf, uaddr, (size_t) info->hp->h_length);
+ if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
+ if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
+ map_v4v6_address(bf, bf);
+ hp->h_addrtype = AF_INET6;
+ hp->h_length = NS_IN6ADDRSZ;
+ }
- __res_put_state(res);
- *info->he = NETDB_SUCCESS;
- return NS_SUCCESS;
+ __res_put_state(res);
+ *info->he = NETDB_SUCCESS;
+ return NS_SUCCESS;
nospc:
- errno = ENOSPC;
- *info->he = NETDB_INTERNAL;
- return NS_UNAVAIL;
+ errno = ENOSPC;
+ *info->he = NETDB_INTERNAL;
+ return NS_UNAVAIL;
}
#ifdef YP
/*ARGSUSED*/
-static struct hostent *
-_yp_hostent(char *line, int af, struct getnamaddr *info)
-{
- struct in6_addr host_addrs[MAXADDRS];
- char **aliases;
- size_t maxaliases;
- char *p = line;
- char *cp, **q, *ptr;
- size_t len, anum, i;
- int addrok;
- int more;
- size_t naddrs;
- struct hostent *hp = info->hp;
+static struct hostent* _yp_hostent(char* line, int af, struct getnamaddr* info) {
+ struct in6_addr host_addrs[MAXADDRS];
+ char** aliases;
+ size_t maxaliases;
+ char* p = line;
+ char *cp, **q, *ptr;
+ size_t len, anum, i;
+ int addrok;
+ int more;
+ size_t naddrs;
+ struct hostent* hp = info->hp;
- _DIAGASSERT(line != NULL);
+ _DIAGASSERT(line != NULL);
- hp->h_name = NULL;
- hp->h_addrtype = af;
- switch (af) {
- case AF_INET:
- hp->h_length = NS_INADDRSZ;
- break;
- case AF_INET6:
- hp->h_length = NS_IN6ADDRSZ;
- break;
- default:
- return NULL;
- }
- setup(aliases, maxaliases);
- naddrs = 0;
- q = aliases;
+ hp->h_name = NULL;
+ hp->h_addrtype = af;
+ switch (af) {
+ case AF_INET:
+ hp->h_length = NS_INADDRSZ;
+ break;
+ case AF_INET6:
+ hp->h_length = NS_IN6ADDRSZ;
+ break;
+ default:
+ return NULL;
+ }
+ setup(aliases, maxaliases);
+ naddrs = 0;
+ q = aliases;
nextline:
- /* check for host_addrs overflow */
- if (naddrs >= __arraycount(host_addrs))
- goto done;
+ /* check for host_addrs overflow */
+ if (naddrs >= __arraycount(host_addrs)) goto done;
- more = 0;
- cp = strpbrk(p, " \t");
- if (cp == NULL)
- goto done;
- *cp++ = '\0';
+ more = 0;
+ cp = strpbrk(p, " \t");
+ if (cp == NULL) goto done;
+ *cp++ = '\0';
- /* p has should have an address */
- addrok = inet_pton(af, p, &host_addrs[naddrs]);
- if (addrok != 1) {
- /* skip to the next line */
- while (cp && *cp) {
- if (*cp == '\n') {
- cp++;
- goto nextline;
- }
- cp++;
- }
- goto done;
- }
- naddrs++;
+ /* p has should have an address */
+ addrok = inet_pton(af, p, &host_addrs[naddrs]);
+ if (addrok != 1) {
+ /* skip to the next line */
+ while (cp && *cp) {
+ if (*cp == '\n') {
+ cp++;
+ goto nextline;
+ }
+ cp++;
+ }
+ goto done;
+ }
+ naddrs++;
- while (*cp == ' ' || *cp == '\t')
- cp++;
- p = cp;
- cp = strpbrk(p, " \t\n");
- if (cp != NULL) {
- if (*cp == '\n')
- more = 1;
- *cp++ = '\0';
- }
- if (!hp->h_name)
- hp->h_name = p;
- else if (strcmp(hp->h_name, p) == 0)
- ;
- else
- addalias(q, p, aliases, maxaliases);
- p = cp;
- if (more)
- goto nextline;
+ while (*cp == ' ' || *cp == '\t') cp++;
+ p = cp;
+ cp = strpbrk(p, " \t\n");
+ if (cp != NULL) {
+ if (*cp == '\n') more = 1;
+ *cp++ = '\0';
+ }
+ if (!hp->h_name)
+ hp->h_name = p;
+ else if (strcmp(hp->h_name, p) == 0)
+ ;
+ else
+ addalias(q, p, aliases, maxaliases);
+ p = cp;
+ if (more) goto nextline;
- while (cp && *cp) {
- if (*cp == ' ' || *cp == '\t') {
- cp++;
- continue;
- }
- if (*cp == '\n') {
- cp++;
- goto nextline;
- }
- addalias(q, cp, aliases, maxaliases);
- cp = strpbrk(cp, " \t");
- if (cp != NULL)
- *cp++ = '\0';
- }
+ while (cp && *cp) {
+ if (*cp == ' ' || *cp == '\t') {
+ cp++;
+ continue;
+ }
+ if (*cp == '\n') {
+ cp++;
+ goto nextline;
+ }
+ addalias(q, cp, aliases, maxaliases);
+ cp = strpbrk(cp, " \t");
+ if (cp != NULL) *cp++ = '\0';
+ }
done:
- if (hp->h_name == NULL) {
- free(aliases);
- return NULL;
- }
+ if (hp->h_name == NULL) {
+ free(aliases);
+ return NULL;
+ }
- ptr = info->buf;
- len = info->buflen;
+ ptr = info->buf;
+ len = info->buflen;
- anum = (size_t)(q - aliases);
- HENT_ARRAY(hp->h_addr_list, naddrs, ptr, len);
- HENT_ARRAY(hp->h_aliases, anum, ptr, len);
+ anum = (size_t)(q - aliases);
+ HENT_ARRAY(hp->h_addr_list, naddrs, ptr, len);
+ HENT_ARRAY(hp->h_aliases, anum, ptr, len);
- for (i = 0; i < naddrs; i++)
- HENT_COPY(hp->h_addr_list[i], &host_addrs[i], hp->h_length,
- ptr, len);
- hp->h_addr_list[naddrs] = NULL;
+ for (i = 0; i < naddrs; i++)
+ HENT_COPY(hp->h_addr_list[i], &host_addrs[i], hp->h_length, ptr, len);
+ hp->h_addr_list[naddrs] = NULL;
- HENT_SCOPY(hp->h_name, hp->h_name, ptr, len);
+ HENT_SCOPY(hp->h_name, hp->h_name, ptr, len);
- for (i = 0; i < anum; i++)
- HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
- hp->h_aliases[anum] = NULL;
- free(aliases);
+ for (i = 0; i < anum; i++) HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
+ hp->h_aliases[anum] = NULL;
+ free(aliases);
- return hp;
+ return hp;
nospc:
- free(aliases);
- *info->he = NETDB_INTERNAL;
- errno = ENOSPC;
- return NULL;
+ free(aliases);
+ *info->he = NETDB_INTERNAL;
+ errno = ENOSPC;
+ return NULL;
}
/*ARGSUSED*/
-int
-_yp_gethtbyaddr(void *rv, void *cb_data, va_list ap)
-{
- struct hostent *hp = NULL;
- char *ypcurrent;
- int ypcurrentlen, r;
- char name[INET6_ADDRSTRLEN]; /* XXX enough? */
- const unsigned char *uaddr;
- int af;
- const char *map;
- struct getnamaddr *info = rv;
+int _yp_gethtbyaddr(void* rv, void* cb_data, va_list ap) {
+ struct hostent* hp = NULL;
+ char* ypcurrent;
+ int ypcurrentlen, r;
+ char name[INET6_ADDRSTRLEN]; /* XXX enough? */
+ const unsigned char* uaddr;
+ int af;
+ const char* map;
+ struct getnamaddr* info = rv;
- _DIAGASSERT(rv != NULL);
+ _DIAGASSERT(rv != NULL);
- uaddr = va_arg(ap, unsigned char *);
- /* NOSTRICT skip len */(void)va_arg(ap, int);
- af = va_arg(ap, int);
+ uaddr = va_arg(ap, unsigned char*);
+ /* NOSTRICT skip len */ (void) va_arg(ap, int);
+ af = va_arg(ap, int);
- if (!__ypdomain) {
- if (_yp_check(&__ypdomain) == 0)
- return NS_UNAVAIL;
- }
- /*
- * XXX unfortunately, we cannot support IPv6 extended scoped address
- * notation here. gethostbyaddr() is not scope-aware. too bad.
- */
- if (inet_ntop(af, uaddr, name, (socklen_t)sizeof(name)) == NULL)
- return NS_UNAVAIL;
- switch (af) {
- case AF_INET:
- map = "hosts.byaddr";
- break;
- default:
- map = "ipnodes.byaddr";
- break;
- }
- ypcurrent = NULL;
- r = yp_match(__ypdomain, map, name,
- (int)strlen(name), &ypcurrent, &ypcurrentlen);
- if (r == 0)
- hp = _yp_hostent(ypcurrent, af, info);
- else
- hp = NULL;
- free(ypcurrent);
- if (hp == NULL) {
- *info->he = HOST_NOT_FOUND;
- return NS_NOTFOUND;
- }
- return NS_SUCCESS;
+ if (!__ypdomain) {
+ if (_yp_check(&__ypdomain) == 0) return NS_UNAVAIL;
+ }
+ /*
+ * XXX unfortunately, we cannot support IPv6 extended scoped address
+ * notation here. gethostbyaddr() is not scope-aware. too bad.
+ */
+ if (inet_ntop(af, uaddr, name, (socklen_t) sizeof(name)) == NULL) return NS_UNAVAIL;
+ switch (af) {
+ case AF_INET:
+ map = "hosts.byaddr";
+ break;
+ default:
+ map = "ipnodes.byaddr";
+ break;
+ }
+ ypcurrent = NULL;
+ r = yp_match(__ypdomain, map, name, (int) strlen(name), &ypcurrent, &ypcurrentlen);
+ if (r == 0)
+ hp = _yp_hostent(ypcurrent, af, info);
+ else
+ hp = NULL;
+ free(ypcurrent);
+ if (hp == NULL) {
+ *info->he = HOST_NOT_FOUND;
+ return NS_NOTFOUND;
+ }
+ return NS_SUCCESS;
}
/*ARGSUSED*/
-int
-_yp_gethtbyname(void *rv, void *cb_data, va_list ap)
-{
- struct hostent *hp;
- char *ypcurrent;
- int ypcurrentlen, r;
- const char *name;
- int af;
- const char *map;
- struct getnamaddr *info = rv;
+int _yp_gethtbyname(void* rv, void* cb_data, va_list ap) {
+ struct hostent* hp;
+ char* ypcurrent;
+ int ypcurrentlen, r;
+ const char* name;
+ int af;
+ const char* map;
+ struct getnamaddr* info = rv;
- _DIAGASSERT(rv != NULL);
+ _DIAGASSERT(rv != NULL);
- name = va_arg(ap, char *);
- /* NOSTRICT skip string len */(void)va_arg(ap, int);
- af = va_arg(ap, int);
+ name = va_arg(ap, char*);
+ /* NOSTRICT skip string len */ (void) va_arg(ap, int);
+ af = va_arg(ap, int);
- if (!__ypdomain) {
- if (_yp_check(&__ypdomain) == 0)
- return NS_UNAVAIL;
- }
- switch (af) {
- case AF_INET:
- map = "hosts.byname";
- break;
- default:
- map = "ipnodes.byname";
- break;
- }
- ypcurrent = NULL;
- r = yp_match(__ypdomain, map, name,
- (int)strlen(name), &ypcurrent, &ypcurrentlen);
- if (r == 0)
- hp = _yp_hostent(ypcurrent, af, info);
- else
- hp = NULL;
- free(ypcurrent);
- if (hp == NULL) {
- *info->he = HOST_NOT_FOUND;
- return NS_NOTFOUND;
- }
- return NS_SUCCESS;
+ if (!__ypdomain) {
+ if (_yp_check(&__ypdomain) == 0) return NS_UNAVAIL;
+ }
+ switch (af) {
+ case AF_INET:
+ map = "hosts.byname";
+ break;
+ default:
+ map = "ipnodes.byname";
+ break;
+ }
+ ypcurrent = NULL;
+ r = yp_match(__ypdomain, map, name, (int) strlen(name), &ypcurrent, &ypcurrentlen);
+ if (r == 0)
+ hp = _yp_hostent(ypcurrent, af, info);
+ else
+ hp = NULL;
+ free(ypcurrent);
+ if (hp == NULL) {
+ *info->he = HOST_NOT_FOUND;
+ return NS_NOTFOUND;
+ }
+ return NS_SUCCESS;
}
#endif
@@ -1564,24 +1450,20 @@
* Non-reentrant versions.
*/
-struct hostent *
-gethostbyname(const char *name)
-{
- struct hostent *result = NULL;
- res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
+struct hostent* gethostbyname(const char* name) {
+ struct hostent* result = NULL;
+ res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
- gethostbyname_r(name, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
- return result;
+ gethostbyname_r(name, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
+ return result;
}
-struct hostent *
-gethostbyname2(const char *name, int af)
-{
- struct hostent *result = NULL;
- res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
+struct hostent* gethostbyname2(const char* name, int af) {
+ struct hostent* result = NULL;
+ res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
- gethostbyname2_r(name, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
- return result;
+ gethostbyname2_r(name, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
+ return result;
}
// android_gethostby*fornet can be called in two different contexts.
@@ -1591,76 +1473,63 @@
// Therefore, we have to populate both fields, and rely on the downstream code to check whether
// |proxy == NULL|, and use that info to query the field that matches the caller's intent.
static struct android_net_context make_context(unsigned netid, unsigned mark) {
- struct android_net_context netcontext = NETCONTEXT_UNSET;
- netcontext.app_netid = netid;
- netcontext.app_mark = mark;
- netcontext.dns_netid = netid;
- netcontext.dns_mark = mark;
- return netcontext;
+ struct android_net_context netcontext = NETCONTEXT_UNSET;
+ netcontext.app_netid = netid;
+ netcontext.app_mark = mark;
+ netcontext.dns_netid = netid;
+ netcontext.dns_mark = mark;
+ return netcontext;
}
-struct hostent *
-android_gethostbynamefornet(const char *name, int af, unsigned netid, unsigned mark)
-{
- const struct android_net_context netcontext = make_context(netid, mark);
- return android_gethostbynamefornetcontext(name, af, &netcontext);
+struct hostent* android_gethostbynamefornet(const char* name, int af, unsigned netid,
+ unsigned mark) {
+ const struct android_net_context netcontext = make_context(netid, mark);
+ return android_gethostbynamefornetcontext(name, af, &netcontext);
}
-struct hostent *
-android_gethostbynamefornetcontext(const char *name, int af,
- const struct android_net_context *netcontext)
-{
- struct hostent *hp;
- res_state res = __res_get_state();
- if (res == NULL)
- return NULL;
- res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
- hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
- &h_errno, netcontext);
- __res_put_state(res);
- return hp;
+struct hostent* android_gethostbynamefornetcontext(const char* name, int af,
+ const struct android_net_context* netcontext) {
+ struct hostent* hp;
+ res_state res = __res_get_state();
+ if (res == NULL) return NULL;
+ res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
+ hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
+ &h_errno, netcontext);
+ __res_put_state(res);
+ return hp;
}
-struct hostent *
-gethostbyaddr(const void *addr, socklen_t len, int af)
-{
- return android_gethostbyaddrfornetcontext_proxy(addr, len, af, &NETCONTEXT_UNSET);
+struct hostent* gethostbyaddr(const void* addr, socklen_t len, int af) {
+ return android_gethostbyaddrfornetcontext_proxy(addr, len, af, &NETCONTEXT_UNSET);
}
-struct hostent *
-android_gethostbyaddrfornet(const void *addr, socklen_t len, int af, unsigned netid, unsigned mark)
-{
- const struct android_net_context netcontext = make_context(netid, mark);
- return android_gethostbyaddrfornetcontext(addr, len, af, &netcontext);
+struct hostent* android_gethostbyaddrfornet(const void* addr, socklen_t len, int af, unsigned netid,
+ unsigned mark) {
+ const struct android_net_context netcontext = make_context(netid, mark);
+ return android_gethostbyaddrfornetcontext(addr, len, af, &netcontext);
}
-struct hostent *
-android_gethostbyaddrfornetcontext(const void *addr, socklen_t len, int af,
- const struct android_net_context *netcontext)
-{
- return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext);
+struct hostent* android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
+ const struct android_net_context* netcontext) {
+ return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext);
}
-__LIBC_HIDDEN__ struct hostent*
-android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af,
- const struct android_net_context *netcontext)
-{
- res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
- return android_gethostbyaddrfornetcontext_proxy_internal(addr, len, af, &rs->host, rs->hostbuf,
- sizeof(rs->hostbuf), &h_errno, netcontext);
+__LIBC_HIDDEN__ struct hostent* android_gethostbyaddrfornetcontext_proxy(
+ const void* addr, socklen_t len, int af, const struct android_net_context* netcontext) {
+ res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
+ return android_gethostbyaddrfornetcontext_proxy_internal(
+ addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno, netcontext);
}
-struct hostent *
-gethostent(void)
-{
- res_static rs = __res_get_static();
- if (!rs->hostf) {
- sethostent_r(&rs->hostf);
- if (!rs->hostf) {
- h_errno = NETDB_INTERNAL;
- return NULL;
- }
- }
- memset(&rs->host, 0, sizeof(rs->host));
- return netbsd_gethostent_r(rs->hostf, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno);
+struct hostent* gethostent(void) {
+ res_static rs = __res_get_static();
+ if (!rs->hostf) {
+ sethostent_r(&rs->hostf);
+ if (!rs->hostf) {
+ h_errno = NETDB_INTERNAL;
+ return NULL;
+ }
+ }
+ memset(&rs->host, 0, sizeof(rs->host));
+ return netbsd_gethostent_r(rs->hostf, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno);
}
diff --git a/resolv/res_cache.c b/resolv/res_cache.c
index d0673ef..958e981 100644
--- a/resolv/res_cache.c
+++ b/resolv/res_cache.c
@@ -36,16 +36,16 @@
#include <time.h>
#include "pthread.h"
-#include <errno.h>
#include <arpa/nameser.h>
+#include <errno.h>
+#include <linux/if.h>
#include <net/if.h>
#include <netdb.h>
-#include <linux/if.h>
#include <arpa/inet.h>
-#include "resolv_private.h"
-#include "resolv_netid.h"
#include "res_private.h"
+#include "resolv_netid.h"
+#include "resolv_private.h"
#include <async_safe/log.h>
@@ -129,7 +129,7 @@
* * Upping by another 5x for the centralized nature
* *****************************************
*/
-#define CONFIG_MAX_ENTRIES 64 * 2 * 5
+#define CONFIG_MAX_ENTRIES 64 * 2 * 5
/****************************************************************************/
/****************************************************************************/
@@ -140,10 +140,10 @@
/****************************************************************************/
/* set to 1 to debug cache operations */
-#define DEBUG 0
+#define DEBUG 0
/* set to 1 to debug query data */
-#define DEBUG_DATA 0
+#define DEBUG_DATA 0
#if DEBUG
#define __DEBUG__
@@ -153,13 +153,14 @@
#undef XLOG
-#define XLOG(...) ({ \
- if (DEBUG) { \
- async_safe_format_log(ANDROID_LOG_DEBUG,"libc",__VA_ARGS__); \
- } else { \
- ((void)0); \
- } \
-})
+#define XLOG(...) \
+ ({ \
+ if (DEBUG) { \
+ async_safe_format_log(ANDROID_LOG_DEBUG, "libc", __VA_ARGS__); \
+ } else { \
+ ((void) 0); \
+ } \
+ })
/** BOUNDED BUFFER FORMATTING
**/
@@ -206,33 +207,27 @@
*/
/* add a char to a bounded buffer */
-char*
-_bprint_c( char* p, char* end, int c )
-{
+char* _bprint_c(char* p, char* end, int c) {
if (p < end) {
- if (p+1 == end)
+ if (p + 1 == end)
*p++ = 0;
else {
*p++ = (char) c;
- *p = 0;
+ *p = 0;
}
}
return p;
}
/* add a sequence of bytes to a bounded buffer */
-char*
-_bprint_b( char* p, char* end, const char* buf, int len )
-{
- int avail = end - p;
+char* _bprint_b(char* p, char* end, const char* buf, int len) {
+ int avail = end - p;
- if (avail <= 0 || len <= 0)
- return p;
+ if (avail <= 0 || len <= 0) return p;
- if (avail > len)
- avail = len;
+ if (avail > len) avail = len;
- memcpy( p, buf, avail );
+ memcpy(p, buf, avail);
p += avail;
if (p < end)
@@ -244,111 +239,96 @@
}
/* add a string to a bounded buffer */
-char*
-_bprint_s( char* p, char* end, const char* str )
-{
+char* _bprint_s(char* p, char* end, const char* str) {
return _bprint_b(p, end, str, strlen(str));
}
/* add a formatted string to a bounded buffer */
-char* _bprint( char* p, char* end, const char* format, ... ) __DEBUG__;
-char* _bprint( char* p, char* end, const char* format, ... )
-{
- int avail, n;
- va_list args;
+char* _bprint(char* p, char* end, const char* format, ...) __DEBUG__;
+char* _bprint(char* p, char* end, const char* format, ...) {
+ int avail, n;
+ va_list args;
avail = end - p;
- if (avail <= 0)
- return p;
+ if (avail <= 0) return p;
va_start(args, format);
- n = vsnprintf( p, avail, format, args);
+ n = vsnprintf(p, avail, format, args);
va_end(args);
/* certain C libraries return -1 in case of truncation */
- if (n < 0 || n > avail)
- n = avail;
+ if (n < 0 || n > avail) n = avail;
p += n;
/* certain C libraries do not zero-terminate in case of truncation */
- if (p == end)
- p[-1] = 0;
+ if (p == end) p[-1] = 0;
return p;
}
/* add a hex value to a bounded buffer, up to 8 digits */
-char*
-_bprint_hex( char* p, char* end, unsigned value, int numDigits )
-{
- char text[sizeof(unsigned)*2];
- int nn = 0;
+char* _bprint_hex(char* p, char* end, unsigned value, int numDigits) {
+ char text[sizeof(unsigned) * 2];
+ int nn = 0;
while (numDigits-- > 0) {
- text[nn++] = "0123456789abcdef"[(value >> (numDigits*4)) & 15];
+ text[nn++] = "0123456789abcdef"[(value >> (numDigits * 4)) & 15];
}
return _bprint_b(p, end, text, nn);
}
/* add the hexadecimal dump of some memory area to a bounded buffer */
-char*
-_bprint_hexdump( char* p, char* end, const uint8_t* data, int datalen )
-{
- int lineSize = 16;
+char* _bprint_hexdump(char* p, char* end, const uint8_t* data, int datalen) {
+ int lineSize = 16;
while (datalen > 0) {
- int avail = datalen;
- int nn;
+ int avail = datalen;
+ int nn;
- if (avail > lineSize)
- avail = lineSize;
+ if (avail > lineSize) avail = lineSize;
for (nn = 0; nn < avail; nn++) {
- if (nn > 0)
- p = _bprint_c(p, end, ' ');
+ if (nn > 0) p = _bprint_c(p, end, ' ');
p = _bprint_hex(p, end, data[nn], 2);
}
- for ( ; nn < lineSize; nn++ ) {
+ for (; nn < lineSize; nn++) {
p = _bprint_s(p, end, " ");
}
p = _bprint_s(p, end, " ");
for (nn = 0; nn < avail; nn++) {
- int c = data[nn];
+ int c = data[nn];
- if (c < 32 || c > 127)
- c = '.';
+ if (c < 32 || c > 127) c = '.';
p = _bprint_c(p, end, c);
}
p = _bprint_c(p, end, '\n');
- data += avail;
+ data += avail;
datalen -= avail;
}
return p;
}
/* dump the content of a query of packet to the log */
-void XLOG_BYTES( const void* base, int len ) __DEBUG__;
-void XLOG_BYTES( const void* base, int len )
-{
+void XLOG_BYTES(const void* base, int len) __DEBUG__;
+void XLOG_BYTES(const void* base, int len) {
if (DEBUG_DATA) {
- char buff[1024];
- char* p = buff, *end = p + sizeof(buff);
+ char buff[1024];
+ char *p = buff, *end = p + sizeof(buff);
p = _bprint_hexdump(p, end, base, len);
- XLOG("%s",buff);
+ XLOG("%s", buff);
}
-} __DEBUG__
+}
+__DEBUG__
-static time_t
-_time_now( void )
-{
- struct timeval tv;
+static time_t _time_now(void) {
+ struct timeval tv;
- gettimeofday( &tv, NULL );
+ gettimeofday(&tv, NULL);
return tv.tv_sec;
}
@@ -437,57 +417,47 @@
* records, only QNAMEs.
*/
-#define DNS_HEADER_SIZE 12
+#define DNS_HEADER_SIZE 12
-#define DNS_TYPE_A "\00\01" /* big-endian decimal 1 */
-#define DNS_TYPE_PTR "\00\014" /* big-endian decimal 12 */
-#define DNS_TYPE_MX "\00\017" /* big-endian decimal 15 */
-#define DNS_TYPE_AAAA "\00\034" /* big-endian decimal 28 */
-#define DNS_TYPE_ALL "\00\0377" /* big-endian decimal 255 */
+#define DNS_TYPE_A "\00\01" /* big-endian decimal 1 */
+#define DNS_TYPE_PTR "\00\014" /* big-endian decimal 12 */
+#define DNS_TYPE_MX "\00\017" /* big-endian decimal 15 */
+#define DNS_TYPE_AAAA "\00\034" /* big-endian decimal 28 */
+#define DNS_TYPE_ALL "\00\0377" /* big-endian decimal 255 */
-#define DNS_CLASS_IN "\00\01" /* big-endian decimal 1 */
+#define DNS_CLASS_IN "\00\01" /* big-endian decimal 1 */
typedef struct {
- const uint8_t* base;
- const uint8_t* end;
- const uint8_t* cursor;
+ const uint8_t* base;
+ const uint8_t* end;
+ const uint8_t* cursor;
} DnsPacket;
-static void
-_dnsPacket_init( DnsPacket* packet, const uint8_t* buff, int bufflen )
-{
- packet->base = buff;
- packet->end = buff + bufflen;
+static void _dnsPacket_init(DnsPacket* packet, const uint8_t* buff, int bufflen) {
+ packet->base = buff;
+ packet->end = buff + bufflen;
packet->cursor = buff;
}
-static void
-_dnsPacket_rewind( DnsPacket* packet )
-{
+static void _dnsPacket_rewind(DnsPacket* packet) {
packet->cursor = packet->base;
}
-static void
-_dnsPacket_skip( DnsPacket* packet, int count )
-{
- const uint8_t* p = packet->cursor + count;
+static void _dnsPacket_skip(DnsPacket* packet, int count) {
+ const uint8_t* p = packet->cursor + count;
- if (p > packet->end)
- p = packet->end;
+ if (p > packet->end) p = packet->end;
packet->cursor = p;
}
-static int
-_dnsPacket_readInt16( DnsPacket* packet )
-{
- const uint8_t* p = packet->cursor;
+static int _dnsPacket_readInt16(DnsPacket* packet) {
+ const uint8_t* p = packet->cursor;
- if (p+2 > packet->end)
- return -1;
+ if (p + 2 > packet->end) return -1;
- packet->cursor = p+2;
- return (p[0]<< 8) | p[1];
+ packet->cursor = p + 2;
+ return (p[0] << 8) | p[1];
}
/** QUERY CHECKING
@@ -496,16 +466,12 @@
/* check bytes in a dns packet. returns 1 on success, 0 on failure.
* the cursor is only advanced in the case of success
*/
-static int
-_dnsPacket_checkBytes( DnsPacket* packet, int numBytes, const void* bytes )
-{
- const uint8_t* p = packet->cursor;
+static int _dnsPacket_checkBytes(DnsPacket* packet, int numBytes, const void* bytes) {
+ const uint8_t* p = packet->cursor;
- if (p + numBytes > packet->end)
- return 0;
+ if (p + numBytes > packet->end) return 0;
- if (memcmp(p, bytes, numBytes) != 0)
- return 0;
+ if (memcmp(p, bytes, numBytes) != 0) return 0;
packet->cursor = p + numBytes;
return 1;
@@ -515,17 +481,14 @@
* from the current cursor position. returns 1 on success,
* or 0 for malformed data.
*/
-static int
-_dnsPacket_checkQName( DnsPacket* packet )
-{
- const uint8_t* p = packet->cursor;
- const uint8_t* end = packet->end;
+static int _dnsPacket_checkQName(DnsPacket* packet) {
+ const uint8_t* p = packet->cursor;
+ const uint8_t* end = packet->end;
for (;;) {
- int c;
+ int c;
- if (p >= end)
- break;
+ if (p >= end) break;
c = *p++;
@@ -535,8 +498,7 @@
}
/* we don't expect label compression in QNAMEs */
- if (c >= 64)
- break;
+ if (c >= 64) break;
p += c;
/* we rely on the bound check at the start
@@ -550,19 +512,15 @@
/* parse and skip a given QR stored in a packet.
* returns 1 on success, and 0 on failure
*/
-static int
-_dnsPacket_checkQR( DnsPacket* packet )
-{
- if (!_dnsPacket_checkQName(packet))
- return 0;
+static int _dnsPacket_checkQR(DnsPacket* packet) {
+ if (!_dnsPacket_checkQName(packet)) return 0;
/* TYPE must be one of the things we support */
if (!_dnsPacket_checkBytes(packet, 2, DNS_TYPE_A) &&
!_dnsPacket_checkBytes(packet, 2, DNS_TYPE_PTR) &&
!_dnsPacket_checkBytes(packet, 2, DNS_TYPE_MX) &&
!_dnsPacket_checkBytes(packet, 2, DNS_TYPE_AAAA) &&
- !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_ALL))
- {
+ !_dnsPacket_checkBytes(packet, 2, DNS_TYPE_ALL)) {
XLOG("unsupported TYPE");
return 0;
}
@@ -578,11 +536,9 @@
/* check the header of a DNS Query packet, return 1 if it is one
* type of query we can cache, or 0 otherwise
*/
-static int
-_dnsPacket_checkQuery( DnsPacket* packet )
-{
- const uint8_t* p = packet->base;
- int qdCount, anCount, dnCount, arCount;
+static int _dnsPacket_checkQuery(DnsPacket* packet) {
+ const uint8_t* p = packet->base;
+ int qdCount, anCount, dnCount, arCount;
if (p + DNS_HEADER_SIZE > packet->end) {
XLOG("query packet too small");
@@ -618,7 +574,7 @@
qdCount = (p[4] << 8) | p[5];
anCount = (p[6] << 8) | p[7];
dnCount = (p[8] << 8) | p[9];
- arCount = (p[10]<< 8) | p[11];
+ arCount = (p[10] << 8) | p[11];
if (anCount != 0 || dnCount != 0 || arCount > 1) {
XLOG("query packet contains non-query records");
@@ -633,9 +589,8 @@
/* Check QDCOUNT QRs */
packet->cursor = p + DNS_HEADER_SIZE;
- for (;qdCount > 0; qdCount--)
- if (!_dnsPacket_checkQR(packet))
- return 0;
+ for (; qdCount > 0; qdCount--)
+ if (!_dnsPacket_checkQR(packet)) return 0;
return 1;
}
@@ -643,18 +598,15 @@
/** QUERY DEBUGGING
**/
#if DEBUG
-static char*
-_dnsPacket_bprintQName(DnsPacket* packet, char* bp, char* bend)
-{
- const uint8_t* p = packet->cursor;
- const uint8_t* end = packet->end;
- int first = 1;
+static char* _dnsPacket_bprintQName(DnsPacket* packet, char* bp, char* bend) {
+ const uint8_t* p = packet->cursor;
+ const uint8_t* end = packet->end;
+ int first = 1;
for (;;) {
- int c;
+ int c;
- if (p >= end)
- break;
+ if (p >= end) break;
c = *p++;
@@ -664,15 +616,14 @@
}
/* we don't expect label compression in QNAMEs */
- if (c >= 64)
- break;
+ if (c >= 64) break;
if (first)
first = 0;
else
bp = _bprint_c(bp, bend, '.');
- bp = _bprint_b(bp, bend, (const char*)p, c);
+ bp = _bprint_b(bp, bend, (const char*) p, c);
p += c;
/* we rely on the bound check at the start
@@ -683,20 +634,15 @@
return bp;
}
-static char*
-_dnsPacket_bprintQR(DnsPacket* packet, char* p, char* end)
-{
-#define QQ(x) { DNS_TYPE_##x, #x }
+static char* _dnsPacket_bprintQR(DnsPacket* packet, char* p, char* end) {
+#define QQ(x) \
+ { DNS_TYPE_##x, #x }
static const struct {
- const char* typeBytes;
- const char* typeString;
- } qTypes[] =
- {
- QQ(A), QQ(PTR), QQ(MX), QQ(AAAA), QQ(ALL),
- { NULL, NULL }
- };
- int nn;
- const char* typeString = NULL;
+ const char* typeBytes;
+ const char* typeString;
+ } qTypes[] = {QQ(A), QQ(PTR), QQ(MX), QQ(AAAA), QQ(ALL), {NULL, NULL}};
+ int nn;
+ const char* typeString = NULL;
/* dump QNAME */
p = _dnsPacket_bprintQName(packet, p, end);
@@ -714,7 +660,7 @@
if (typeString != NULL)
p = _bprint_s(p, end, typeString);
else {
- int typeCode = _dnsPacket_readInt16(packet);
+ int typeCode = _dnsPacket_readInt16(packet);
p = _bprint(p, end, "UNKNOWN-%d", typeCode);
}
@@ -726,10 +672,8 @@
}
/* this function assumes the packet has already been checked */
-static char*
-_dnsPacket_bprintQuery( DnsPacket* packet, char* p, char* end )
-{
- int qdCount;
+static char* _dnsPacket_bprintQuery(DnsPacket* packet, char* p, char* end) {
+ int qdCount;
if (packet->base[2] & 0x1) {
p = _bprint_s(p, end, "RECURSIVE ");
@@ -739,14 +683,13 @@
qdCount = _dnsPacket_readInt16(packet);
_dnsPacket_skip(packet, 6);
- for ( ; qdCount > 0; qdCount-- ) {
+ for (; qdCount > 0; qdCount--) {
p = _dnsPacket_bprintQR(packet, p, end);
}
return p;
}
#endif
-
/** QUERY HASHING SUPPORT
**
** THE FOLLOWING CODE ASSUMES THAT THE INPUT PACKET HAS ALREADY
@@ -754,71 +697,60 @@
**/
/* use 32-bit FNV hash function */
-#define FNV_MULT 16777619U
-#define FNV_BASIS 2166136261U
+#define FNV_MULT 16777619U
+#define FNV_BASIS 2166136261U
-static unsigned
-_dnsPacket_hashBytes( DnsPacket* packet, int numBytes, unsigned hash )
-{
- const uint8_t* p = packet->cursor;
- const uint8_t* end = packet->end;
+static unsigned _dnsPacket_hashBytes(DnsPacket* packet, int numBytes, unsigned hash) {
+ const uint8_t* p = packet->cursor;
+ const uint8_t* end = packet->end;
while (numBytes > 0 && p < end) {
- hash = hash*FNV_MULT ^ *p++;
+ hash = hash * FNV_MULT ^ *p++;
}
packet->cursor = p;
return hash;
}
-
-static unsigned
-_dnsPacket_hashQName( DnsPacket* packet, unsigned hash )
-{
- const uint8_t* p = packet->cursor;
- const uint8_t* end = packet->end;
+static unsigned _dnsPacket_hashQName(DnsPacket* packet, unsigned hash) {
+ const uint8_t* p = packet->cursor;
+ const uint8_t* end = packet->end;
for (;;) {
- int c;
+ int c;
- if (p >= end) { /* should not happen */
+ if (p >= end) { /* should not happen */
XLOG("%s: INTERNAL_ERROR: read-overflow !!\n", __FUNCTION__);
break;
}
c = *p++;
- if (c == 0)
- break;
+ if (c == 0) break;
if (c >= 64) {
XLOG("%s: INTERNAL_ERROR: malformed domain !!\n", __FUNCTION__);
break;
}
if (p + c >= end) {
- XLOG("%s: INTERNAL_ERROR: simple label read-overflow !!\n",
- __FUNCTION__);
+ XLOG("%s: INTERNAL_ERROR: simple label read-overflow !!\n", __FUNCTION__);
break;
}
while (c > 0) {
- hash = hash*FNV_MULT ^ *p++;
- c -= 1;
+ hash = hash * FNV_MULT ^ *p++;
+ c -= 1;
}
}
packet->cursor = p;
return hash;
}
-static unsigned
-_dnsPacket_hashQR( DnsPacket* packet, unsigned hash )
-{
+static unsigned _dnsPacket_hashQR(DnsPacket* packet, unsigned hash) {
hash = _dnsPacket_hashQName(packet, hash);
hash = _dnsPacket_hashBytes(packet, 4, hash); /* TYPE and CLASS */
return hash;
}
-static unsigned
-_dnsPacket_hashRR( DnsPacket* packet, unsigned hash )
-{
+static unsigned _dnsPacket_hashRR(DnsPacket* packet, unsigned hash) {
int rdlength;
hash = _dnsPacket_hashQR(packet, hash);
hash = _dnsPacket_hashBytes(packet, 4, hash); /* TTL */
@@ -827,11 +759,9 @@
return hash;
}
-static unsigned
-_dnsPacket_hashQuery( DnsPacket* packet )
-{
- unsigned hash = FNV_BASIS;
- int count, arcount;
+static unsigned _dnsPacket_hashQuery(DnsPacket* packet) {
+ unsigned hash = FNV_BASIS;
+ int count, arcount;
_dnsPacket_rewind(packet);
/* ignore the ID */
@@ -844,7 +774,7 @@
* between answers for recursive and non-recursive
* queries.
*/
- hash = hash*FNV_MULT ^ (packet->base[2] & 1);
+ hash = hash * FNV_MULT ^ (packet->base[2] & 1);
/* mark the first header byte as processed */
_dnsPacket_skip(packet, 1);
@@ -862,33 +792,28 @@
arcount = _dnsPacket_readInt16(packet);
/* hash QDCOUNT QRs */
- for ( ; count > 0; count-- )
- hash = _dnsPacket_hashQR(packet, hash);
+ for (; count > 0; count--) hash = _dnsPacket_hashQR(packet, hash);
/* hash ARCOUNT RRs */
- for ( ; arcount > 0; arcount-- )
- hash = _dnsPacket_hashRR(packet, hash);
+ for (; arcount > 0; arcount--) hash = _dnsPacket_hashRR(packet, hash);
return hash;
}
-
/** QUERY COMPARISON
**
** THE FOLLOWING CODE ASSUMES THAT THE INPUT PACKETS HAVE ALREADY
** BEEN SUCCESFULLY CHECKED.
**/
-static int
-_dnsPacket_isEqualDomainName( DnsPacket* pack1, DnsPacket* pack2 )
-{
- const uint8_t* p1 = pack1->cursor;
- const uint8_t* end1 = pack1->end;
- const uint8_t* p2 = pack2->cursor;
- const uint8_t* end2 = pack2->end;
+static int _dnsPacket_isEqualDomainName(DnsPacket* pack1, DnsPacket* pack2) {
+ const uint8_t* p1 = pack1->cursor;
+ const uint8_t* end1 = pack1->end;
+ const uint8_t* p2 = pack2->cursor;
+ const uint8_t* end2 = pack2->end;
for (;;) {
- int c1, c2;
+ int c1, c2;
if (p1 >= end1 || p2 >= end2) {
XLOG("%s: INTERNAL_ERROR: read-overflow !!\n", __FUNCTION__);
@@ -896,8 +821,7 @@
}
c1 = *p1++;
c2 = *p2++;
- if (c1 != c2)
- break;
+ if (c1 != c2) break;
if (c1 == 0) {
pack1->cursor = p1;
@@ -908,13 +832,11 @@
XLOG("%s: INTERNAL_ERROR: malformed domain !!\n", __FUNCTION__);
break;
}
- if ((p1+c1 > end1) || (p2+c1 > end2)) {
- XLOG("%s: INTERNAL_ERROR: simple label read-overflow !!\n",
- __FUNCTION__);
+ if ((p1 + c1 > end1) || (p2 + c1 > end2)) {
+ XLOG("%s: INTERNAL_ERROR: simple label read-overflow !!\n", __FUNCTION__);
break;
}
- if (memcmp(p1, p2, c1) != 0)
- break;
+ if (memcmp(p1, p2, c1) != 0) break;
p1 += c1;
p2 += c1;
/* we rely on the bound checks at the start of the loop */
@@ -924,57 +846,43 @@
return 0;
}
-static int
-_dnsPacket_isEqualBytes( DnsPacket* pack1, DnsPacket* pack2, int numBytes )
-{
- const uint8_t* p1 = pack1->cursor;
- const uint8_t* p2 = pack2->cursor;
+static int _dnsPacket_isEqualBytes(DnsPacket* pack1, DnsPacket* pack2, int numBytes) {
+ const uint8_t* p1 = pack1->cursor;
+ const uint8_t* p2 = pack2->cursor;
- if ( p1 + numBytes > pack1->end || p2 + numBytes > pack2->end )
- return 0;
+ if (p1 + numBytes > pack1->end || p2 + numBytes > pack2->end) return 0;
- if ( memcmp(p1, p2, numBytes) != 0 )
- return 0;
+ if (memcmp(p1, p2, numBytes) != 0) return 0;
pack1->cursor += numBytes;
pack2->cursor += numBytes;
return 1;
}
-static int
-_dnsPacket_isEqualQR( DnsPacket* pack1, DnsPacket* pack2 )
-{
+static int _dnsPacket_isEqualQR(DnsPacket* pack1, DnsPacket* pack2) {
/* compare domain name encoding + TYPE + CLASS */
- if ( !_dnsPacket_isEqualDomainName(pack1, pack2) ||
- !_dnsPacket_isEqualBytes(pack1, pack2, 2+2) )
+ if (!_dnsPacket_isEqualDomainName(pack1, pack2) ||
+ !_dnsPacket_isEqualBytes(pack1, pack2, 2 + 2))
return 0;
return 1;
}
-static int
-_dnsPacket_isEqualRR( DnsPacket* pack1, DnsPacket* pack2 )
-{
+static int _dnsPacket_isEqualRR(DnsPacket* pack1, DnsPacket* pack2) {
int rdlength1, rdlength2;
/* compare query + TTL */
- if ( !_dnsPacket_isEqualQR(pack1, pack2) ||
- !_dnsPacket_isEqualBytes(pack1, pack2, 4) )
- return 0;
+ if (!_dnsPacket_isEqualQR(pack1, pack2) || !_dnsPacket_isEqualBytes(pack1, pack2, 4)) return 0;
/* compare RDATA */
rdlength1 = _dnsPacket_readInt16(pack1);
rdlength2 = _dnsPacket_readInt16(pack2);
- if ( rdlength1 != rdlength2 ||
- !_dnsPacket_isEqualBytes(pack1, pack2, rdlength1) )
- return 0;
+ if (rdlength1 != rdlength2 || !_dnsPacket_isEqualBytes(pack1, pack2, rdlength1)) return 0;
return 1;
}
-static int
-_dnsPacket_isEqualQuery( DnsPacket* pack1, DnsPacket* pack2 )
-{
- int count1, count2, arcount1, arcount2;
+static int _dnsPacket_isEqualQuery(DnsPacket* pack1, DnsPacket* pack2) {
+ int count1, count2, arcount1, arcount2;
/* compare the headers, ignore most fields */
_dnsPacket_rewind(pack1);
@@ -1016,7 +924,7 @@
}
/* compare the QDCOUNT QRs */
- for ( ; count1 > 0; count1-- ) {
+ for (; count1 > 0; count1--) {
if (!_dnsPacket_isEqualQR(pack1, pack2)) {
XLOG("different QR");
return 0;
@@ -1024,7 +932,7 @@
}
/* compare the ARCOUNT RRs */
- for ( ; arcount1 > 0; arcount1-- ) {
+ for (; arcount1 > 0; arcount1--) {
if (!_dnsPacket_isEqualRR(pack1, pack2)) {
XLOG("different additional RR");
return 0;
@@ -1047,17 +955,17 @@
* similarly, mru_next and mru_prev are part of the global MRU list
*/
typedef struct Entry {
- unsigned int hash; /* hash value */
- struct Entry* hlink; /* next in collision chain */
- struct Entry* mru_prev;
- struct Entry* mru_next;
+ unsigned int hash; /* hash value */
+ struct Entry* hlink; /* next in collision chain */
+ struct Entry* mru_prev;
+ struct Entry* mru_next;
- const uint8_t* query;
- int querylen;
- const uint8_t* answer;
- int answerlen;
- time_t expires; /* time_t when the entry isn't valid any more */
- int id; /* for debugging purpose */
+ const uint8_t* query;
+ int querylen;
+ const uint8_t* answer;
+ int answerlen;
+ time_t expires; /* time_t when the entry isn't valid any more */
+ int id; /* for debugging purpose */
} Entry;
/**
@@ -1066,8 +974,7 @@
*
* Return 0 if not found.
*/
-static u_long
-answer_getNegativeTTL(ns_msg handle) {
+static u_long answer_getNegativeTTL(ns_msg handle) {
int n, nscount;
u_long result = 0;
ns_rr rr;
@@ -1075,23 +982,23 @@
nscount = ns_msg_count(handle, ns_s_ns);
for (n = 0; n < nscount; n++) {
if ((ns_parserr(&handle, ns_s_ns, n, &rr) == 0) && (ns_rr_type(rr) == ns_t_soa)) {
- const u_char *rdata = ns_rr_rdata(rr); // find the data
- const u_char *edata = rdata + ns_rr_rdlen(rr); // add the len to find the end
+ const u_char* rdata = ns_rr_rdata(rr); // find the data
+ const u_char* edata = rdata + ns_rr_rdlen(rr); // add the len to find the end
int len;
u_long ttl, rec_result = ns_rr_ttl(rr);
// find the MINIMUM-TTL field from the blob of binary data for this record
// skip the server name
len = dn_skipname(rdata, edata);
- if (len == -1) continue; // error skipping
+ if (len == -1) continue; // error skipping
rdata += len;
// skip the admin name
len = dn_skipname(rdata, edata);
- if (len == -1) continue; // error skipping
+ if (len == -1) continue; // error skipping
rdata += len;
- if (edata - rdata != 5*NS_INT32SZ) continue;
+ if (edata - rdata != 5 * NS_INT32SZ) continue;
// skip: serial number + refresh interval + retry interval + expiry
rdata += NS_INT32SZ * 4;
// finally read the MINIMUM TTL
@@ -1120,9 +1027,7 @@
* In case of parse error zero (0) is returned which
* indicates that the answer shall not be cached.
*/
-static u_long
-answer_getTTL(const void* answer, int answerlen)
-{
+static u_long answer_getTTL(const void* answer, int answerlen) {
ns_msg handle;
int ancount, n;
u_long result, ttl;
@@ -1157,40 +1062,32 @@
return result;
}
-static void
-entry_free( Entry* e )
-{
+static void entry_free(Entry* e) {
/* everything is allocated in a single memory block */
if (e) {
free(e);
}
}
-static __inline__ void
-entry_mru_remove( Entry* e )
-{
+static __inline__ void entry_mru_remove(Entry* e) {
e->mru_prev->mru_next = e->mru_next;
e->mru_next->mru_prev = e->mru_prev;
}
-static __inline__ void
-entry_mru_add( Entry* e, Entry* list )
-{
- Entry* first = list->mru_next;
+static __inline__ void entry_mru_add(Entry* e, Entry* list) {
+ Entry* first = list->mru_next;
e->mru_next = first;
e->mru_prev = list;
- list->mru_next = e;
+ list->mru_next = e;
first->mru_prev = e;
}
/* compute the hash of a given entry, this is a hash of most
* data in the query (key) */
-static unsigned
-entry_hash( const Entry* e )
-{
- DnsPacket pack[1];
+static unsigned entry_hash(const Entry* e) {
+ DnsPacket pack[1];
_dnsPacket_init(pack, e->query, e->querylen);
return _dnsPacket_hashQuery(pack);
@@ -1198,16 +1095,14 @@
/* initialize an Entry as a search key, this also checks the input query packet
* returns 1 on success, or 0 in case of unsupported/malformed data */
-static int
-entry_init_key( Entry* e, const void* query, int querylen )
-{
- DnsPacket pack[1];
+static int entry_init_key(Entry* e, const void* query, int querylen) {
+ DnsPacket pack[1];
memset(e, 0, sizeof(*e));
- e->query = query;
+ e->query = query;
e->querylen = querylen;
- e->hash = entry_hash(e);
+ e->hash = entry_hash(e);
_dnsPacket_init(pack, query, querylen);
@@ -1215,35 +1110,30 @@
}
/* allocate a new entry as a cache node */
-static Entry*
-entry_alloc( const Entry* init, const void* answer, int answerlen )
-{
- Entry* e;
- int size;
+static Entry* entry_alloc(const Entry* init, const void* answer, int answerlen) {
+ Entry* e;
+ int size;
size = sizeof(*e) + init->querylen + answerlen;
- e = calloc(size, 1);
- if (e == NULL)
- return e;
+ e = calloc(size, 1);
+ if (e == NULL) return e;
- e->hash = init->hash;
- e->query = (const uint8_t*)(e+1);
+ e->hash = init->hash;
+ e->query = (const uint8_t*) (e + 1);
e->querylen = init->querylen;
- memcpy( (char*)e->query, init->query, e->querylen );
+ memcpy((char*) e->query, init->query, e->querylen);
- e->answer = e->query + e->querylen;
+ e->answer = e->query + e->querylen;
e->answerlen = answerlen;
- memcpy( (char*)e->answer, answer, e->answerlen );
+ memcpy((char*) e->answer, answer, e->answerlen);
return e;
}
-static int
-entry_equals( const Entry* e1, const Entry* e2 )
-{
- DnsPacket pack1[1], pack2[1];
+static int entry_equals(const Entry* e1, const Entry* e2) {
+ DnsPacket pack1[1], pack2[1];
if (e1->querylen != e2->querylen) {
return 0;
@@ -1271,37 +1161,37 @@
#define PENDING_REQUEST_TIMEOUT 20;
typedef struct pending_req_info {
- unsigned int hash;
- pthread_cond_t cond;
- struct pending_req_info* next;
+ unsigned int hash;
+ pthread_cond_t cond;
+ struct pending_req_info* next;
} PendingReqInfo;
typedef struct resolv_cache {
- int max_entries;
- int num_entries;
- Entry mru_list;
- int last_id;
- Entry* entries;
- PendingReqInfo pending_requests;
+ int max_entries;
+ int num_entries;
+ Entry mru_list;
+ int last_id;
+ Entry* entries;
+ PendingReqInfo pending_requests;
} Cache;
struct resolv_cache_info {
- unsigned netid;
- Cache* cache;
- struct resolv_cache_info* next;
- int nscount;
- char* nameservers[MAXNS];
- struct addrinfo* nsaddrinfo[MAXNS];
- int revision_id; // # times the nameservers have been replaced
- struct __res_params params;
- struct __res_stats nsstats[MAXNS];
- char defdname[MAXDNSRCHPATH];
- int dnsrch_offset[MAXDNSRCH+1]; // offsets into defdname
+ unsigned netid;
+ Cache* cache;
+ struct resolv_cache_info* next;
+ int nscount;
+ char* nameservers[MAXNS];
+ struct addrinfo* nsaddrinfo[MAXNS];
+ int revision_id; // # times the nameservers have been replaced
+ struct __res_params params;
+ struct __res_stats nsstats[MAXNS];
+ char defdname[MAXDNSRCHPATH];
+ int dnsrch_offset[MAXDNSRCH + 1]; // offsets into defdname
};
-#define HTABLE_VALID(x) ((x) != NULL && (x) != HTABLE_DELETED)
+#define HTABLE_VALID(x) ((x) != NULL && (x) != HTABLE_DELETED)
-static pthread_once_t _res_cache_once = PTHREAD_ONCE_INIT;
+static pthread_once_t _res_cache_once = PTHREAD_ONCE_INIT;
static void _res_cache_init(void);
// lock protecting everything in the _resolve_cache_info structs (next ptr, etc)
@@ -1310,9 +1200,7 @@
/* gets cache associated with a network, or NULL if none exists */
static struct resolv_cache* _find_named_cache_locked(unsigned netid);
-static void
-_cache_flush_pending_requests_locked( struct resolv_cache* cache )
-{
+static void _cache_flush_pending_requests_locked(struct resolv_cache* cache) {
struct pending_req_info *ri, *tmp;
if (cache) {
ri = cache->pending_requests.next;
@@ -1333,9 +1221,8 @@
/* Return 0 if no pending request is found matching the key.
* If a matching request is found the calling thread will wait until
* the matching request completes, then update *cache and return 1. */
-static int
-_cache_check_pending_request_locked( struct resolv_cache** cache, Entry* key, unsigned netid )
-{
+static int _cache_check_pending_request_locked(struct resolv_cache** cache, Entry* key,
+ unsigned netid) {
struct pending_req_info *ri, *prev;
int exist = 0;
@@ -1359,7 +1246,7 @@
prev->next = ri;
}
} else {
- struct timespec ts = {0,0};
+ struct timespec ts = {0, 0};
XLOG("Waiting for previous request");
ts.tv_sec = _time_now() + PENDING_REQUEST_TIMEOUT;
pthread_cond_timedwait(&ri->cond, &_res_cache_list_lock, &ts);
@@ -1373,9 +1260,7 @@
/* notify any waiting thread that waiting on a request
* matching the key has been added to the cache */
-static void
-_cache_notify_waiting_tid_locked( struct resolv_cache* cache, Entry* key )
-{
+static void _cache_notify_waiting_tid_locked(struct resolv_cache* cache, Entry* key) {
struct pending_req_info *ri, *prev;
if (cache && key) {
@@ -1400,16 +1285,11 @@
}
/* notify the cache that the query failed */
-void
-_resolv_cache_query_failed( unsigned netid,
- const void* query,
- int querylen)
-{
- Entry key[1];
- Cache* cache;
+void _resolv_cache_query_failed(unsigned netid, const void* query, int querylen) {
+ Entry key[1];
+ Cache* cache;
- if (!entry_init_key(key, query, querylen))
- return;
+ if (!entry_init_key(key, query, querylen)) return;
pthread_mutex_lock(&_res_cache_list_lock);
@@ -1424,17 +1304,14 @@
static struct resolv_cache_info* _find_cache_info_locked(unsigned netid);
-static void
-_cache_flush_locked( Cache* cache )
-{
- int nn;
+static void _cache_flush_locked(Cache* cache) {
+ int nn;
- for (nn = 0; nn < cache->max_entries; nn++)
- {
- Entry** pnode = (Entry**) &cache->entries[nn];
+ for (nn = 0; nn < cache->max_entries; nn++) {
+ Entry** pnode = (Entry**) &cache->entries[nn];
while (*pnode != NULL) {
- Entry* node = *pnode;
+ Entry* node = *pnode;
*pnode = node->hlink;
entry_free(node);
}
@@ -1444,17 +1321,15 @@
_cache_flush_pending_requests_locked(cache);
cache->mru_list.mru_next = cache->mru_list.mru_prev = &cache->mru_list;
- cache->num_entries = 0;
- cache->last_id = 0;
+ cache->num_entries = 0;
+ cache->last_id = 0;
XLOG("*************************\n"
"*** DNS CACHE FLUSHED ***\n"
"*************************");
}
-static int
-_res_cache_get_max_entries( void )
-{
+static int _res_cache_get_max_entries(void) {
int cache_size = CONFIG_MAX_ENTRIES;
const char* cache_mode = getenv("ANDROID_DNS_MODE");
@@ -1467,10 +1342,8 @@
return cache_size;
}
-static struct resolv_cache*
-_resolv_cache_create( void )
-{
- struct resolv_cache* cache;
+static struct resolv_cache* _resolv_cache_create(void) {
+ struct resolv_cache* cache;
cache = calloc(sizeof(*cache), 1);
if (cache) {
@@ -1487,24 +1360,19 @@
return cache;
}
-
#if DEBUG
-static void
-_dump_query( const uint8_t* query, int querylen )
-{
- char temp[256], *p=temp, *end=p+sizeof(temp);
- DnsPacket pack[1];
+static void _dump_query(const uint8_t* query, int querylen) {
+ char temp[256], *p = temp, *end = p + sizeof(temp);
+ DnsPacket pack[1];
_dnsPacket_init(pack, query, querylen);
p = _dnsPacket_bprintQuery(pack, p, end);
XLOG("QUERY: %s", temp);
}
-static void
-_cache_dump_mru( Cache* cache )
-{
- char temp[512], *p=temp, *end=p+sizeof(temp);
- Entry* e;
+static void _cache_dump_mru(Cache* cache) {
+ char temp[512], *p = temp, *end = p + sizeof(temp);
+ Entry* e;
p = _bprint(temp, end, "MRU LIST (%2d): ", cache->num_entries);
for (e = cache->mru_list.mru_next; e != &cache->mru_list; e = e->mru_next)
@@ -1513,9 +1381,7 @@
XLOG("%s", temp);
}
-static void
-_dump_answer(const void* answer, int answerlen)
-{
+static void _dump_answer(const void* answer, int answerlen) {
res_state statep;
FILE* fp;
char* buf;
@@ -1527,33 +1393,32 @@
res_pquery(statep, answer, answerlen, fp);
- //Get file length
+ // Get file length
fseek(fp, 0, SEEK_END);
- fileLen=ftell(fp);
+ fileLen = ftell(fp);
fseek(fp, 0, SEEK_SET);
- buf = (char *)malloc(fileLen+1);
+ buf = (char*) malloc(fileLen + 1);
if (buf != NULL) {
- //Read file contents into buffer
+ // Read file contents into buffer
fread(buf, fileLen, 1, fp);
XLOG("%s\n", buf);
free(buf);
}
fclose(fp);
remove("/data/reslog.txt");
- }
- else {
- errno = 0; // else debug is introducing error signals
+ } else {
+ errno = 0; // else debug is introducing error signals
XLOG("%s: can't open file\n", __FUNCTION__);
}
}
#endif
#if DEBUG
-# define XLOG_QUERY(q,len) _dump_query((q), (len))
-# define XLOG_ANSWER(a, len) _dump_answer((a), (len))
+#define XLOG_QUERY(q, len) _dump_query((q), (len))
+#define XLOG_ANSWER(a, len) _dump_answer((a), (len))
#else
-# define XLOG_QUERY(q,len) ((void)0)
-# define XLOG_ANSWER(a,len) ((void)0)
+#define XLOG_QUERY(q, len) ((void) 0)
+#define XLOG_ANSWER(a, len) ((void) 0)
#endif
/* This function tries to find a key within the hash table
@@ -1570,21 +1435,16 @@
* The result of a lookup_p is only valid until you alter the hash
* table.
*/
-static Entry**
-_cache_lookup_p( Cache* cache,
- Entry* key )
-{
- int index = key->hash % cache->max_entries;
- Entry** pnode = (Entry**) &cache->entries[ index ];
+static Entry** _cache_lookup_p(Cache* cache, Entry* key) {
+ int index = key->hash % cache->max_entries;
+ Entry** pnode = (Entry**) &cache->entries[index];
while (*pnode != NULL) {
- Entry* node = *pnode;
+ Entry* node = *pnode;
- if (node == NULL)
- break;
+ if (node == NULL) break;
- if (node->hash == key->hash && entry_equals(node, key))
- break;
+ if (node->hash == key->hash && entry_equals(node, key)) break;
pnode = &node->hlink;
}
@@ -1596,32 +1456,23 @@
* (i.e. with *lookup == NULL), and 'e' is the pointer to the
* newly created entry
*/
-static void
-_cache_add_p( Cache* cache,
- Entry** lookup,
- Entry* e )
-{
+static void _cache_add_p(Cache* cache, Entry** lookup, Entry* e) {
*lookup = e;
e->id = ++cache->last_id;
entry_mru_add(e, &cache->mru_list);
cache->num_entries += 1;
- XLOG("%s: entry %d added (count=%d)", __FUNCTION__,
- e->id, cache->num_entries);
+ XLOG("%s: entry %d added (count=%d)", __FUNCTION__, e->id, cache->num_entries);
}
/* Remove an existing entry from the hash table,
* 'lookup' must be the result of an immediate previous
* and succesful _lookup_p() call.
*/
-static void
-_cache_remove_p( Cache* cache,
- Entry** lookup )
-{
- Entry* e = *lookup;
+static void _cache_remove_p(Cache* cache, Entry** lookup) {
+ Entry* e = *lookup;
- XLOG("%s: entry %d removed (count=%d)", __FUNCTION__,
- e->id, cache->num_entries-1);
+ XLOG("%s: entry %d removed (count=%d)", __FUNCTION__, e->id, cache->num_entries - 1);
entry_mru_remove(e);
*lookup = e->hlink;
@@ -1631,11 +1482,9 @@
/* Remove the oldest entry from the hash table.
*/
-static void
-_cache_remove_oldest( Cache* cache )
-{
- Entry* oldest = cache->mru_list.mru_prev;
- Entry** lookup = _cache_lookup_p(cache, oldest);
+static void _cache_remove_oldest(Cache* cache) {
+ Entry* oldest = cache->mru_list.mru_prev;
+ Entry** lookup = _cache_lookup_p(cache, oldest);
if (*lookup == NULL) { /* should not happen */
XLOG("%s: OLDEST NOT IN HTABLE ?", __FUNCTION__);
@@ -1670,21 +1519,15 @@
}
}
-ResolvCacheStatus
-_resolv_cache_lookup( unsigned netid,
- const void* query,
- int querylen,
- void* answer,
- int answersize,
- int *answerlen )
-{
- Entry key[1];
- Entry** lookup;
- Entry* e;
- time_t now;
- Cache* cache;
+ResolvCacheStatus _resolv_cache_lookup(unsigned netid, const void* query, int querylen,
+ void* answer, int answersize, int* answerlen) {
+ Entry key[1];
+ Entry** lookup;
+ Entry* e;
+ time_t now;
+ Cache* cache;
- ResolvCacheStatus result = RESOLV_CACHE_NOTFOUND;
+ ResolvCacheStatus result = RESOLV_CACHE_NOTFOUND;
XLOG("%s: lookup", __FUNCTION__);
XLOG_QUERY(query, querylen);
@@ -1708,10 +1551,10 @@
* the function always return a non-NULL pointer.
*/
lookup = _cache_lookup_p(cache, key);
- e = *lookup;
+ e = *lookup;
if (e == NULL) {
- XLOG( "NOT IN CACHE");
+ XLOG("NOT IN CACHE");
// calling thread will wait if an outstanding request is found
// that matching this query
if (!_cache_check_pending_request_locked(&cache, key, netid) || cache == NULL) {
@@ -1729,7 +1572,7 @@
/* remove stale entries here */
if (now >= e->expires) {
- XLOG( " NOT IN CACHE (STALE ENTRY %p DISCARDED)", *lookup );
+ XLOG(" NOT IN CACHE (STALE ENTRY %p DISCARDED)", *lookup);
XLOG_QUERY(e->query, e->querylen);
_cache_remove_p(cache, lookup);
goto Exit;
@@ -1743,15 +1586,15 @@
goto Exit;
}
- memcpy( answer, e->answer, e->answerlen );
+ memcpy(answer, e->answer, e->answerlen);
/* bump up this entry to the top of the MRU list */
if (e != cache->mru_list.mru_next) {
- entry_mru_remove( e );
- entry_mru_add( e, &cache->mru_list );
+ entry_mru_remove(e);
+ entry_mru_add(e, &cache->mru_list);
}
- XLOG( "FOUND IN CACHE entry=%p", e );
+ XLOG("FOUND IN CACHE entry=%p", e);
result = RESOLV_CACHE_FOUND;
Exit:
@@ -1759,24 +1602,18 @@
return result;
}
-
-void
-_resolv_cache_add( unsigned netid,
- const void* query,
- int querylen,
- const void* answer,
- int answerlen )
-{
- Entry key[1];
- Entry* e;
- Entry** lookup;
- u_long ttl;
- Cache* cache = NULL;
+void _resolv_cache_add(unsigned netid, const void* query, int querylen, const void* answer,
+ int answerlen) {
+ Entry key[1];
+ Entry* e;
+ Entry** lookup;
+ u_long ttl;
+ Cache* cache = NULL;
/* don't assume that the query has already been cached
*/
- if (!entry_init_key( key, query, querylen )) {
- XLOG( "%s: passed invalid query ?", __FUNCTION__);
+ if (!entry_init_key(key, query, querylen)) {
+ XLOG("%s: passed invalid query ?", __FUNCTION__);
return;
}
@@ -1787,20 +1624,19 @@
goto Exit;
}
- XLOG( "%s: query:", __FUNCTION__ );
- XLOG_QUERY(query,querylen);
+ XLOG("%s: query:", __FUNCTION__);
+ XLOG_QUERY(query, querylen);
XLOG_ANSWER(answer, answerlen);
#if DEBUG_DATA
- XLOG( "answer:");
- XLOG_BYTES(answer,answerlen);
+ XLOG("answer:");
+ XLOG_BYTES(answer, answerlen);
#endif
lookup = _cache_lookup_p(cache, key);
- e = *lookup;
+ e = *lookup;
if (e != NULL) { /* should not happen */
- XLOG("%s: ALREADY IN CACHE (%p) ? IGNORING ADD",
- __FUNCTION__, e);
+ XLOG("%s: ALREADY IN CACHE (%p) ? IGNORING ADD", __FUNCTION__, e);
goto Exit;
}
@@ -1811,10 +1647,9 @@
}
/* need to lookup again */
lookup = _cache_lookup_p(cache, key);
- e = *lookup;
+ e = *lookup;
if (e != NULL) {
- XLOG("%s: ALREADY IN CACHE (%p) ? IGNORING ADD",
- __FUNCTION__, e);
+ XLOG("%s: ALREADY IN CACHE (%p) ? IGNORING ADD", __FUNCTION__, e);
goto Exit;
}
}
@@ -1832,7 +1667,7 @@
#endif
Exit:
if (cache != NULL) {
- _cache_notify_waiting_tid_locked(cache, key);
+ _cache_notify_waiting_tid_locked(cache, key);
}
pthread_mutex_unlock(&_res_cache_list_lock);
}
@@ -1851,7 +1686,7 @@
/* insert resolv_cache_info into the list of resolv_cache_infos */
static void _insert_cache_info_locked(struct resolv_cache_info* cache_info);
/* creates a resolv_cache_info */
-static struct resolv_cache_info* _create_cache_info( void );
+static struct resolv_cache_info* _create_cache_info(void);
/* gets a resolv_cache_info associated with a network, or NULL if not found */
static struct resolv_cache_info* _find_cache_info_locked(unsigned netid);
/* look up the named cache, and creates one if needed */
@@ -1863,20 +1698,16 @@
/* return 1 if the provided list of name servers differs from the list of name servers
* currently attached to the provided cache_info */
static int _resolv_is_nameservers_equal_locked(struct resolv_cache_info* cache_info,
- const char** servers, int numservers);
+ const char** servers, int numservers);
/* clears the stats samples contained withing the given cache_info */
static void _res_cache_clear_stats_locked(struct resolv_cache_info* cache_info);
-static void
-_res_cache_init(void)
-{
+static void _res_cache_init(void) {
memset(&_res_cache_list, 0, sizeof(_res_cache_list));
pthread_mutex_init(&_res_cache_list_lock, NULL);
}
-static struct resolv_cache*
-_get_res_cache_for_net_locked(unsigned netid)
-{
+static struct resolv_cache* _get_res_cache_for_net_locked(unsigned netid) {
struct resolv_cache* cache = _find_named_cache_locked(netid);
if (!cache) {
struct resolv_cache_info* cache_info = _create_cache_info();
@@ -1894,9 +1725,7 @@
return cache;
}
-void
-_resolv_flush_cache_for_net(unsigned netid)
-{
+void _resolv_flush_cache_for_net(unsigned netid) {
pthread_once(&_res_cache_once, _res_cache_init);
pthread_mutex_lock(&_res_cache_list_lock);
@@ -1905,9 +1734,7 @@
pthread_mutex_unlock(&_res_cache_list_lock);
}
-static void
-_flush_cache_for_net_locked(unsigned netid)
-{
+static void _flush_cache_for_net_locked(unsigned netid) {
struct resolv_cache* cache = _find_named_cache_locked(netid);
if (cache) {
_cache_flush_locked(cache);
@@ -1918,8 +1745,7 @@
_res_cache_clear_stats_locked(cache_info);
}
-void _resolv_delete_cache_for_net(unsigned netid)
-{
+void _resolv_delete_cache_for_net(unsigned netid) {
pthread_once(&_res_cache_once, _res_cache_init);
pthread_mutex_lock(&_res_cache_list_lock);
@@ -1944,29 +1770,23 @@
pthread_mutex_unlock(&_res_cache_list_lock);
}
-static struct resolv_cache_info*
-_create_cache_info(void)
-{
+static struct resolv_cache_info* _create_cache_info(void) {
struct resolv_cache_info* cache_info;
cache_info = calloc(sizeof(*cache_info), 1);
return cache_info;
}
-static void
-_insert_cache_info_locked(struct resolv_cache_info* cache_info)
-{
+static void _insert_cache_info_locked(struct resolv_cache_info* cache_info) {
struct resolv_cache_info* last;
- for (last = &_res_cache_list; last->next; last = last->next);
+ for (last = &_res_cache_list; last->next; last = last->next)
+ ;
last->next = cache_info;
-
}
-static struct resolv_cache*
-_find_named_cache_locked(unsigned netid) {
-
+static struct resolv_cache* _find_named_cache_locked(unsigned netid) {
struct resolv_cache_info* info = _find_cache_info_locked(netid);
if (info != NULL) return info->cache;
@@ -1974,9 +1794,7 @@
return NULL;
}
-static struct resolv_cache_info*
-_find_cache_info_locked(unsigned netid)
-{
+static struct resolv_cache_info* _find_cache_info_locked(unsigned netid) {
struct resolv_cache_info* cache_info = _res_cache_list.next;
while (cache_info) {
@@ -1989,8 +1807,7 @@
return cache_info;
}
-void
-_resolv_set_default_params(struct __res_params* params) {
+void _resolv_set_default_params(struct __res_params* params) {
params->sample_validity = NSSAMPLE_VALIDITY;
params->success_threshold = SUCCESS_THRESHOLD;
params->min_samples = 0;
@@ -1998,13 +1815,11 @@
params->base_timeout_msec = 0; // 0 = legacy algorithm
}
-int
-_resolv_set_nameservers_for_net(unsigned netid, const char** servers, unsigned numservers,
- const char *domains, const struct __res_params* params)
-{
+int _resolv_set_nameservers_for_net(unsigned netid, const char** servers, unsigned numservers,
+ const char* domains, const struct __res_params* params) {
char sbuf[NI_MAXSERV];
- register char *cp;
- int *offset;
+ register char* cp;
+ int* offset;
struct addrinfo* nsaddrinfo[MAXNS];
if (numservers > MAXNS) {
@@ -2015,16 +1830,13 @@
// Parse the addresses before actually locking or changing any state, in case there is an error.
// As a side effect this also reduces the time the lock is kept.
struct addrinfo hints = {
- .ai_family = AF_UNSPEC,
- .ai_socktype = SOCK_DGRAM,
- .ai_flags = AI_NUMERICHOST
- };
+ .ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM, .ai_flags = AI_NUMERICHOST};
snprintf(sbuf, sizeof(sbuf), "%u", NAMESERVER_PORT);
for (unsigned i = 0; i < numservers; i++) {
// The addrinfo structures allocated here are freed in _free_nameservers_locked().
int rt = getaddrinfo(servers[i], sbuf, &hints, &nsaddrinfo[i]);
if (rt != 0) {
- for (unsigned j = 0 ; j < i ; j++) {
+ for (unsigned j = 0; j < i; j++) {
freeaddrinfo(nsaddrinfo[j]);
nsaddrinfo[j] = NULL;
}
@@ -2084,8 +1896,7 @@
// just the host name.
// code moved from res_init.c, load_domain_search_list
strlcpy(cache_info->defdname, domains, sizeof(cache_info->defdname));
- if ((cp = strchr(cache_info->defdname, '\n')) != NULL)
- *cp = '\0';
+ if ((cp = strchr(cache_info->defdname, '\n')) != NULL) *cp = '\0';
cp = cache_info->defdname;
offset = cache_info->dnsrch_offset;
@@ -2095,8 +1906,8 @@
if (*cp == '\0') /* stop if nothing more to do */
break;
*offset++ = cp - cache_info->defdname; /* record this search domain */
- while (*cp) { /* zero-terminate it */
- if (*cp == ' '|| *cp == '\t') {
+ while (*cp) { /* zero-terminate it */
+ if (*cp == ' ' || *cp == '\t') {
*cp++ = '\0';
break;
}
@@ -2110,10 +1921,8 @@
return 0;
}
-static int
-_resolv_is_nameservers_equal_locked(struct resolv_cache_info* cache_info,
- const char** servers, int numservers)
-{
+static int _resolv_is_nameservers_equal_locked(struct resolv_cache_info* cache_info,
+ const char** servers, int numservers) {
if (cache_info->nscount != numservers) {
return 0;
}
@@ -2125,7 +1934,7 @@
// insensitive to the order of the nameservers; we should probably fix that
// too.
for (int i = 0; i < numservers; i++) {
- for (int j = 0 ; ; j++) {
+ for (int j = 0;; j++) {
if (j >= numservers) {
return 0;
}
@@ -2138,9 +1947,7 @@
return 1;
}
-static void
-_free_nameservers_locked(struct resolv_cache_info* cache_info)
-{
+static void _free_nameservers_locked(struct resolv_cache_info* cache_info) {
int i;
for (i = 0; i < cache_info->nscount; i++) {
free(cache_info->nameservers[i]);
@@ -2149,17 +1956,14 @@
freeaddrinfo(cache_info->nsaddrinfo[i]);
cache_info->nsaddrinfo[i] = NULL;
}
- cache_info->nsstats[i].sample_count =
- cache_info->nsstats[i].sample_next = 0;
+ cache_info->nsstats[i].sample_count = cache_info->nsstats[i].sample_next = 0;
}
cache_info->nscount = 0;
_res_cache_clear_stats_locked(cache_info);
++cache_info->revision_id;
}
-void
-_resolv_populate_res_for_net(res_state statp)
-{
+void _resolv_populate_res_for_net(res_state statp) {
if (statp == NULL) {
return;
}
@@ -2183,10 +1987,8 @@
memcpy(&statp->_u._ext.ext->nsaddrs[nserv], ai->ai_addr, ai->ai_addrlen);
statp->nsaddr_list[nserv].sin_family = AF_UNSPEC;
} else {
- if ((size_t) ai->ai_addrlen
- <= sizeof(statp->nsaddr_list[0])) {
- memcpy(&statp->nsaddr_list[nserv], ai->ai_addr,
- ai->ai_addrlen);
+ if ((size_t) ai->ai_addrlen <= sizeof(statp->nsaddr_list[0])) {
+ memcpy(&statp->nsaddr_list[nserv], ai->ai_addr, ai->ai_addrlen);
} else {
statp->nsaddr_list[nserv].sin_family = AF_UNSPEC;
}
@@ -2200,8 +2002,8 @@
// but the setting/offset-computer only runs when set/changed
// WARNING: Don't use str*cpy() here, this string contains zeroes.
memcpy(statp->defdname, info->defdname, sizeof(statp->defdname));
- register char **pp = statp->dnsrch;
- register int *p = info->dnsrch_offset;
+ register char** pp = statp->dnsrch;
+ register int* p = info->dnsrch_offset;
while (pp < statp->dnsrch + MAXDNSRCH && *p != -1) {
*pp++ = &statp->defdname[0] + *p++;
}
@@ -2211,13 +2013,12 @@
/* Resolver reachability statistics. */
-static void
-_res_cache_add_stats_sample_locked(struct __res_stats* stats, const struct __res_sample* sample,
- int max_samples) {
+static void _res_cache_add_stats_sample_locked(struct __res_stats* stats,
+ const struct __res_sample* sample, int max_samples) {
// Note: This function expects max_samples > 0, otherwise a (harmless) modification of the
// allocated but supposedly unused memory for samples[0] will happen
- XLOG("%s: adding sample to stats, next = %d, count = %d", __FUNCTION__,
- stats->sample_next, stats->sample_count);
+ XLOG("%s: adding sample to stats, next = %d, count = %d", __FUNCTION__, stats->sample_next,
+ stats->sample_count);
stats->samples[stats->sample_next] = *sample;
if (stats->sample_count < max_samples) {
++stats->sample_count;
@@ -2227,19 +2028,19 @@
}
}
-static void
-_res_cache_clear_stats_locked(struct resolv_cache_info* cache_info) {
+static void _res_cache_clear_stats_locked(struct resolv_cache_info* cache_info) {
if (cache_info) {
- for (int i = 0 ; i < MAXNS ; ++i) {
+ for (int i = 0; i < MAXNS; ++i) {
cache_info->nsstats->sample_count = cache_info->nsstats->sample_next = 0;
}
}
}
-int
-android_net_res_stats_get_info_for_net(unsigned netid, int* nscount,
- struct sockaddr_storage servers[MAXNS], int* dcount, char domains[MAXDNSRCH][MAXDNSRCHPATH],
- struct __res_params* params, struct __res_stats stats[MAXNS]) {
+int android_net_res_stats_get_info_for_net(unsigned netid, int* nscount,
+ struct sockaddr_storage servers[MAXNS], int* dcount,
+ char domains[MAXDNSRCH][MAXDNSRCHPATH],
+ struct __res_params* params,
+ struct __res_stats stats[MAXNS]) {
int revision_id = -1;
pthread_mutex_lock(&_res_cache_list_lock);
@@ -2258,8 +2059,7 @@
// - all addresses are valid
// - there is only one address per addrinfo thanks to numeric resolution
int addrlen = info->nsaddrinfo[i]->ai_addrlen;
- if (addrlen < (int) sizeof(struct sockaddr) ||
- addrlen > (int) sizeof(servers[0])) {
+ if (addrlen < (int) sizeof(struct sockaddr) || addrlen > (int) sizeof(servers[0])) {
pthread_mutex_unlock(&_res_cache_list_lock);
XLOG("%s: nsaddrinfo[%d].ai_addrlen == %d", __FUNCTION__, i, addrlen);
errno = EMSGSIZE;
@@ -2290,7 +2090,7 @@
// TODO: Pass in a search domain array instead of a string to
// _resolv_set_nameservers_for_net() and make this double check unnecessary.
if (info->dnsrch_offset[i] < 0 ||
- ((size_t)info->dnsrch_offset[i]) >= sizeof(info->defdname) || !cur_domain[0]) {
+ ((size_t) info->dnsrch_offset[i]) >= sizeof(info->defdname) || !cur_domain[0]) {
break;
}
strlcpy(domains[i], cur_domain, MAXDNSRCHPATH);
@@ -2304,9 +2104,8 @@
return revision_id;
}
-int
-_resolv_cache_get_resolver_stats( unsigned netid, struct __res_params* params,
- struct __res_stats stats[MAXNS]) {
+int _resolv_cache_get_resolver_stats(unsigned netid, struct __res_params* params,
+ struct __res_stats stats[MAXNS]) {
int revision_id = -1;
pthread_mutex_lock(&_res_cache_list_lock);
@@ -2321,9 +2120,8 @@
return revision_id;
}
-void
-_resolv_cache_add_resolver_stats_sample( unsigned netid, int revision_id, int ns,
- const struct __res_sample* sample, int max_samples) {
+void _resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, int ns,
+ const struct __res_sample* sample, int max_samples) {
if (max_samples <= 0) return;
pthread_mutex_lock(&_res_cache_list_lock);
diff --git a/resolv/res_data.c b/resolv/res_data.c
index 9bc02e7..8c35fcd 100644
--- a/resolv/res_data.c
+++ b/resolv/res_data.c
@@ -26,52 +26,37 @@
#endif
#endif /* LIBC_SCCS and not lint */
-
-
-#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/time.h>
+#include <sys/types.h>
-#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
+#include <netinet/in.h>
#include <ctype.h>
#include <netdb.h>
-#include "resolv_private.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-
+#include "resolv_private.h"
__LIBC_HIDDEN__
-const char * const _res_opcodes[] = {
- "QUERY",
- "IQUERY",
- "CQUERYM",
- "CQUERYU", /* experimental */
- "NOTIFY", /* experimental */
- "UPDATE",
- "6",
- "7",
- "8",
- "9",
- "10",
- "11",
- "12",
- "13",
- "ZONEINIT",
- "ZONEREF",
+const char* const _res_opcodes[] = {
+ "QUERY", "IQUERY", "CQUERYM", "CQUERYU", /* experimental */
+ "NOTIFY", /* experimental */
+ "UPDATE", "6", "7", "8", "9", "10",
+ "11", "12", "13", "ZONEINIT", "ZONEREF",
};
#ifdef BIND_UPDATE
-const char * const _res_sectioncodes[] = {
- "ZONE",
- "PREREQUISITES",
- "UPDATE",
- "ADDITIONAL",
+const char* const _res_sectioncodes[] = {
+ "ZONE",
+ "PREREQUISITES",
+ "UPDATE",
+ "ADDITIONAL",
};
#endif
@@ -80,247 +65,210 @@
/* Proto. */
-int res_ourserver_p(const res_state, const struct sockaddr *);
+int res_ourserver_p(const res_state, const struct sockaddr*);
-#define res_need_init() ((_nres.options & RES_INIT) == 0U)
+#define res_need_init() ((_nres.options & RES_INIT) == 0U)
-int
-res_init(void) {
- int rv;
- extern int __res_vinit(res_state, int);
+int res_init(void) {
+ int rv;
+ extern int __res_vinit(res_state, int);
#ifdef COMPAT__RES
- /*
- * Compatibility with program that were accessing _res directly
- * to set options. We keep another struct res that is the same
- * size as the original res structure, and then copy fields to
- * it so that we achieve the same initialization
- */
- extern void *__res_get_old_state(void);
- extern void __res_put_old_state(void *);
- res_state ores = __res_get_old_state();
+ /*
+ * Compatibility with program that were accessing _res directly
+ * to set options. We keep another struct res that is the same
+ * size as the original res structure, and then copy fields to
+ * it so that we achieve the same initialization
+ */
+ extern void* __res_get_old_state(void);
+ extern void __res_put_old_state(void*);
+ res_state ores = __res_get_old_state();
- if (ores->options != 0)
- _nres.options = ores->options;
- if (ores->retrans != 0)
- _nres.retrans = ores->retrans;
- if (ores->retry != 0)
- _nres.retry = ores->retry;
+ if (ores->options != 0) _nres.options = ores->options;
+ if (ores->retrans != 0) _nres.retrans = ores->retrans;
+ if (ores->retry != 0) _nres.retry = ores->retry;
#endif
- /*
- * These three fields used to be statically initialized. This made
- * it hard to use this code in a shared library. It is necessary,
- * now that we're doing dynamic initialization here, that we preserve
- * the old semantics: if an application modifies one of these three
- * fields of _res before res_init() is called, res_init() will not
- * alter them. Of course, if an application is setting them to
- * _zero_ before calling res_init(), hoping to override what used
- * to be the static default, we can't detect it and unexpected results
- * will follow. Zero for any of these fields would make no sense,
- * so one can safely assume that the applications were already getting
- * unexpected results.
- *
- * _nres.options is tricky since some apps were known to diddle the bits
- * before res_init() was first called. We can't replicate that semantic
- * with dynamic initialization (they may have turned bits off that are
- * set in RES_DEFAULT). Our solution is to declare such applications
- * "broken". They could fool us by setting RES_INIT but none do (yet).
- */
- if (!_nres.retrans)
- _nres.retrans = RES_TIMEOUT;
- if (!_nres.retry)
- _nres.retry = 4;
- if (!(_nres.options & RES_INIT))
- _nres.options = RES_DEFAULT;
+ /*
+ * These three fields used to be statically initialized. This made
+ * it hard to use this code in a shared library. It is necessary,
+ * now that we're doing dynamic initialization here, that we preserve
+ * the old semantics: if an application modifies one of these three
+ * fields of _res before res_init() is called, res_init() will not
+ * alter them. Of course, if an application is setting them to
+ * _zero_ before calling res_init(), hoping to override what used
+ * to be the static default, we can't detect it and unexpected results
+ * will follow. Zero for any of these fields would make no sense,
+ * so one can safely assume that the applications were already getting
+ * unexpected results.
+ *
+ * _nres.options is tricky since some apps were known to diddle the bits
+ * before res_init() was first called. We can't replicate that semantic
+ * with dynamic initialization (they may have turned bits off that are
+ * set in RES_DEFAULT). Our solution is to declare such applications
+ * "broken". They could fool us by setting RES_INIT but none do (yet).
+ */
+ if (!_nres.retrans) _nres.retrans = RES_TIMEOUT;
+ if (!_nres.retry) _nres.retry = 4;
+ if (!(_nres.options & RES_INIT)) _nres.options = RES_DEFAULT;
- /*
- * This one used to initialize implicitly to zero, so unless the app
- * has set it to something in particular, we can randomize it now.
- */
- if (!_nres.id)
- _nres.id = res_randomid();
+ /*
+ * This one used to initialize implicitly to zero, so unless the app
+ * has set it to something in particular, we can randomize it now.
+ */
+ if (!_nres.id) _nres.id = res_randomid();
- rv = __res_vinit(&_nres, 1);
+ rv = __res_vinit(&_nres, 1);
#ifdef COMPAT__RES
- __res_put_old_state(&_nres);
+ __res_put_old_state(&_nres);
#endif
- return rv;
+ return rv;
}
-void
-p_query(const u_char *msg) {
- fp_query(msg, stdout);
+void p_query(const u_char* msg) {
+ fp_query(msg, stdout);
}
-void
-fp_query(const u_char *msg, FILE *file) {
- fp_nquery(msg, PACKETSZ, file);
+void fp_query(const u_char* msg, FILE* file) {
+ fp_nquery(msg, PACKETSZ, file);
}
-void
-fp_nquery(const u_char *msg, int len, FILE *file) {
- if (res_need_init() && res_init() == -1)
- return;
+void fp_nquery(const u_char* msg, int len, FILE* file) {
+ if (res_need_init() && res_init() == -1) return;
- res_pquery(&_nres, msg, len, file);
+ res_pquery(&_nres, msg, len, file);
}
-int
-res_mkquery(int op, /* opcode of query */
- const char *dname, /* domain name */
- int class, int type, /* class and type of query */
- const u_char *data, /* resource record data */
- int datalen, /* length of data */
- const u_char *newrr_in, /* new rr for modify or append */
- u_char *buf, /* buffer to put query */
- int buflen) /* size of buffer */
+int res_mkquery(int op, /* opcode of query */
+ const char* dname, /* domain name */
+ int class, int type, /* class and type of query */
+ const u_char* data, /* resource record data */
+ int datalen, /* length of data */
+ const u_char* newrr_in, /* new rr for modify or append */
+ u_char* buf, /* buffer to put query */
+ int buflen) /* size of buffer */
{
- if (res_need_init() && res_init() == -1) {
- RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
- return (-1);
- }
- return (res_nmkquery(&_nres, op, dname, class, type,
- data, datalen,
- newrr_in, buf, buflen));
+ if (res_need_init() && res_init() == -1) {
+ RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
+ return (-1);
+ }
+ return (res_nmkquery(&_nres, op, dname, class, type, data, datalen, newrr_in, buf, buflen));
}
#ifdef _LIBRESOLV
-int
-res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
- if (res_need_init() && res_init() == -1) {
- RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
- return (-1);
- }
+int res_mkupdate(ns_updrec* rrecp_in, u_char* buf, int buflen) {
+ if (res_need_init() && res_init() == -1) {
+ RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
+ return (-1);
+ }
- return (res_nmkupdate(&_nres, rrecp_in, buf, buflen));
+ return (res_nmkupdate(&_nres, rrecp_in, buf, buflen));
}
#endif
-int
-res_query(const char *name, /* domain name */
- int class, int type, /* class and type of query */
- u_char *answer, /* buffer to put answer */
- int anslen) /* size of answer buffer */
+int res_query(const char* name, /* domain name */
+ int class, int type, /* class and type of query */
+ u_char* answer, /* buffer to put answer */
+ int anslen) /* size of answer buffer */
{
- if (res_need_init() && res_init() == -1) {
- RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
- return (-1);
- }
- return (res_nquery(&_nres, name, class, type, answer, anslen));
+ if (res_need_init() && res_init() == -1) {
+ RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
+ return (-1);
+ }
+ return (res_nquery(&_nres, name, class, type, answer, anslen));
}
-void
-res_send_setqhook(res_send_qhook hook) {
- _nres.qhook = hook;
+void res_send_setqhook(res_send_qhook hook) {
+ _nres.qhook = hook;
}
-void
-res_send_setrhook(res_send_rhook hook) {
- _nres.rhook = hook;
+void res_send_setrhook(res_send_rhook hook) {
+ _nres.rhook = hook;
}
-int
-res_isourserver(const struct sockaddr_in *inp) {
- return (res_ourserver_p(&_nres, (const struct sockaddr *)(const void *)inp));
+int res_isourserver(const struct sockaddr_in* inp) {
+ return (res_ourserver_p(&_nres, (const struct sockaddr*) (const void*) inp));
}
-int
-res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
- if (res_need_init() && res_init() == -1) {
- /* errno should have been set by res_init() in this case. */
- return (-1);
- }
+int res_send(const u_char* buf, int buflen, u_char* ans, int anssiz) {
+ if (res_need_init() && res_init() == -1) {
+ /* errno should have been set by res_init() in this case. */
+ return (-1);
+ }
- return (res_nsend(&_nres, buf, buflen, ans, anssiz));
+ return (res_nsend(&_nres, buf, buflen, ans, anssiz));
}
#ifdef _LIBRESOLV
-int
-res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
- u_char *ans, int anssiz)
-{
- if (res_need_init() && res_init() == -1) {
- /* errno should have been set by res_init() in this case. */
- return (-1);
- }
+int res_sendsigned(const u_char* buf, int buflen, ns_tsig_key* key, u_char* ans, int anssiz) {
+ if (res_need_init() && res_init() == -1) {
+ /* errno should have been set by res_init() in this case. */
+ return (-1);
+ }
- return (res_nsendsigned(&_nres, buf, buflen, key, ans, anssiz));
+ return (res_nsendsigned(&_nres, buf, buflen, key, ans, anssiz));
}
#endif
-void
-res_close(void) {
- res_nclose(&_nres);
+void res_close(void) {
+ res_nclose(&_nres);
}
#ifdef _LIBRESOLV
-int
-res_update(ns_updrec *rrecp_in) {
- if (res_need_init() && res_init() == -1) {
- RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
- return (-1);
- }
+int res_update(ns_updrec* rrecp_in) {
+ if (res_need_init() && res_init() == -1) {
+ RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
+ return (-1);
+ }
- return (res_nupdate(&_nres, rrecp_in, NULL));
+ return (res_nupdate(&_nres, rrecp_in, NULL));
}
#endif
-int
-res_search(const char *name, /* domain name */
- int class, int type, /* class and type of query */
- u_char *answer, /* buffer to put answer */
- int anslen) /* size of answer */
+int res_search(const char* name, /* domain name */
+ int class, int type, /* class and type of query */
+ u_char* answer, /* buffer to put answer */
+ int anslen) /* size of answer */
{
- if (res_need_init() && res_init() == -1) {
- RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
- return (-1);
- }
+ if (res_need_init() && res_init() == -1) {
+ RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
+ return (-1);
+ }
- return (res_nsearch(&_nres, name, class, type, answer, anslen));
+ return (res_nsearch(&_nres, name, class, type, answer, anslen));
}
-int
-res_querydomain(const char *name,
- const char *domain,
- int class, int type, /* class and type of query */
- u_char *answer, /* buffer to put answer */
- int anslen) /* size of answer */
+int res_querydomain(const char* name, const char* domain, int class,
+ int type, /* class and type of query */
+ u_char* answer, /* buffer to put answer */
+ int anslen) /* size of answer */
{
- if (res_need_init() && res_init() == -1) {
- RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
- return (-1);
- }
+ if (res_need_init() && res_init() == -1) {
+ RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
+ return (-1);
+ }
- return (res_nquerydomain(&_nres, name, domain,
- class, type,
- answer, anslen));
+ return (res_nquerydomain(&_nres, name, domain, class, type, answer, anslen));
}
-int
-res_opt(int a, u_char *b, int c, int d)
-{
- return res_nopt(&_nres, a, b, c, d);
+int res_opt(int a, u_char* b, int c, int d) {
+ return res_nopt(&_nres, a, b, c, d);
}
-const char *
-hostalias(const char *name) {
- return NULL;
+const char* hostalias(const char* name) {
+ return NULL;
}
#ifdef ultrix
-int
-local_hostname_length(const char *hostname) {
- int len_host, len_domain;
+int local_hostname_length(const char* hostname) {
+ int len_host, len_domain;
- if (!*_nres.defdname)
- res_init();
- len_host = strlen(hostname);
- len_domain = strlen(_nres.defdname);
- if (len_host > len_domain &&
- !strcasecmp(hostname + len_host - len_domain, _nres.defdname) &&
- hostname[len_host - len_domain - 1] == '.')
- return (len_host - len_domain - 1);
- return (0);
+ if (!*_nres.defdname) res_init();
+ len_host = strlen(hostname);
+ len_domain = strlen(_nres.defdname);
+ if (len_host > len_domain && !strcasecmp(hostname + len_host - len_domain, _nres.defdname) &&
+ hostname[len_host - len_domain - 1] == '.')
+ return (len_host - len_domain - 1);
+ return (0);
}
#endif /*ultrix*/
diff --git a/resolv/res_debug.c b/resolv/res_debug.c
index 5f889cb..331d0c4 100644
--- a/resolv/res_debug.c
+++ b/resolv/res_debug.c
@@ -105,301 +105,245 @@
#endif
#endif /* LIBC_SCCS and not lint */
-
-
-#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
+#include <sys/types.h>
-#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
+#include <netinet/in.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <netdb.h>
-#include "resolv_private.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
+#include "resolv_private.h"
-extern const char * const _res_opcodes[];
-extern const char * const _res_sectioncodes[];
+extern const char* const _res_opcodes[];
+extern const char* const _res_sectioncodes[];
#ifndef _LIBC
/*
* Print the current options.
*/
-void
-fp_resstat(const res_state statp, FILE *file) {
- u_long mask;
+void fp_resstat(const res_state statp, FILE* file) {
+ u_long mask;
- fprintf(file, ";; res options:");
- for (mask = 1; mask != 0U; mask <<= 1)
- if (statp->options & mask)
- fprintf(file, " %s", p_option(mask));
- putc('\n', file);
+ fprintf(file, ";; res options:");
+ for (mask = 1; mask != 0U; mask <<= 1)
+ if (statp->options & mask) fprintf(file, " %s", p_option(mask));
+ putc('\n', file);
}
#endif
-static void
-do_section(const res_state statp,
- ns_msg *handle, ns_sect section,
- int pflag, FILE *file)
-{
- int n, sflag, rrnum;
- int buflen = 2048;
- char *buf;
- ns_opcode opcode;
- ns_rr rr;
+static void do_section(const res_state statp, ns_msg* handle, ns_sect section, int pflag,
+ FILE* file) {
+ int n, sflag, rrnum;
+ int buflen = 2048;
+ char* buf;
+ ns_opcode opcode;
+ ns_rr rr;
- /*
- * Print answer records.
- */
- sflag = (int)(statp->pfcode & pflag);
- if (statp->pfcode && !sflag)
- return;
+ /*
+ * Print answer records.
+ */
+ sflag = (int) (statp->pfcode & pflag);
+ if (statp->pfcode && !sflag) return;
- buf = malloc((size_t)buflen);
- if (buf == NULL) {
- fprintf(file, ";; memory allocation failure\n");
- return;
- }
+ buf = malloc((size_t) buflen);
+ if (buf == NULL) {
+ fprintf(file, ";; memory allocation failure\n");
+ return;
+ }
- opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
- rrnum = 0;
- for (;;) {
- if (ns_parserr(handle, section, rrnum, &rr)) {
- if (errno != ENODEV)
- fprintf(file, ";; ns_parserr: %s\n",
- strerror(errno));
- else if (rrnum > 0 && sflag != 0 &&
- (statp->pfcode & RES_PRF_HEAD1))
- putc('\n', file);
- goto cleanup;
- }
- if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
- fprintf(file, ";; %s SECTION:\n",
- p_section(section, opcode));
- if (section == ns_s_qd)
- fprintf(file, ";;\t%s, type = %s, class = %s\n",
- ns_rr_name(rr),
- p_type(ns_rr_type(rr)),
- p_class(ns_rr_class(rr)));
- else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
- size_t rdatalen, ttl;
- uint16_t optcode, optlen;
+ opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
+ rrnum = 0;
+ for (;;) {
+ if (ns_parserr(handle, section, rrnum, &rr)) {
+ if (errno != ENODEV)
+ fprintf(file, ";; ns_parserr: %s\n", strerror(errno));
+ else if (rrnum > 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
+ putc('\n', file);
+ goto cleanup;
+ }
+ if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
+ fprintf(file, ";; %s SECTION:\n", p_section(section, opcode));
+ if (section == ns_s_qd)
+ fprintf(file, ";;\t%s, type = %s, class = %s\n", ns_rr_name(rr), p_type(ns_rr_type(rr)),
+ p_class(ns_rr_class(rr)));
+ else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
+ size_t rdatalen, ttl;
+ uint16_t optcode, optlen;
- rdatalen = ns_rr_rdlen(rr);
- ttl = ns_rr_ttl(rr);
- fprintf(file,
- "; EDNS: version: %zu, udp=%u, flags=%04zx\n",
- (ttl>>16)&0xff, ns_rr_class(rr), ttl&0xffff);
- while (rdatalen >= 4) {
- const u_char *cp = ns_rr_rdata(rr);
- int i;
+ rdatalen = ns_rr_rdlen(rr);
+ ttl = ns_rr_ttl(rr);
+ fprintf(file, "; EDNS: version: %zu, udp=%u, flags=%04zx\n", (ttl >> 16) & 0xff,
+ ns_rr_class(rr), ttl & 0xffff);
+ while (rdatalen >= 4) {
+ const u_char* cp = ns_rr_rdata(rr);
+ int i;
- GETSHORT(optcode, cp);
- GETSHORT(optlen, cp);
+ GETSHORT(optcode, cp);
+ GETSHORT(optlen, cp);
- if (optcode == NS_OPT_NSID) {
- fputs("; NSID: ", file);
- if (optlen == 0) {
- fputs("; NSID\n", file);
- } else {
- fputs("; NSID: ", file);
- for (i = 0; i < optlen; i++)
- fprintf(file, "%02x ",
- cp[i]);
- fputs(" (",file);
- for (i = 0; i < optlen; i++)
- fprintf(file, "%c",
- isprint(cp[i])?
- cp[i] : '.');
- fputs(")\n", file);
- }
- } else {
- if (optlen == 0) {
- fprintf(file, "; OPT=%u\n",
- optcode);
- } else {
- fprintf(file, "; OPT=%u: ",
- optcode);
- for (i = 0; i < optlen; i++)
- fprintf(file, "%02x ",
- cp[i]);
- fputs(" (",file);
- for (i = 0; i < optlen; i++)
- fprintf(file, "%c",
- isprint(cp[i]) ?
- cp[i] : '.');
- fputs(")\n", file);
- }
- }
- rdatalen -= 4 + optlen;
- }
- } else {
- n = ns_sprintrr(handle, &rr, NULL, NULL,
- buf, (u_int)buflen);
- if (n < 0) {
- if (errno == ENOSPC) {
- free(buf);
- buf = NULL;
- if (buflen < 131072)
- buf = malloc((size_t)(buflen += 1024));
- if (buf == NULL) {
- fprintf(file,
- ";; memory allocation failure\n");
- return;
- }
- continue;
- }
- fprintf(file, ";; ns_sprintrr: %s\n",
- strerror(errno));
- goto cleanup;
- }
- fputs(buf, file);
- fputc('\n', file);
- }
- rrnum++;
- }
- cleanup:
- if (buf != NULL)
- free(buf);
+ if (optcode == NS_OPT_NSID) {
+ fputs("; NSID: ", file);
+ if (optlen == 0) {
+ fputs("; NSID\n", file);
+ } else {
+ fputs("; NSID: ", file);
+ for (i = 0; i < optlen; i++) fprintf(file, "%02x ", cp[i]);
+ fputs(" (", file);
+ for (i = 0; i < optlen; i++)
+ fprintf(file, "%c", isprint(cp[i]) ? cp[i] : '.');
+ fputs(")\n", file);
+ }
+ } else {
+ if (optlen == 0) {
+ fprintf(file, "; OPT=%u\n", optcode);
+ } else {
+ fprintf(file, "; OPT=%u: ", optcode);
+ for (i = 0; i < optlen; i++) fprintf(file, "%02x ", cp[i]);
+ fputs(" (", file);
+ for (i = 0; i < optlen; i++)
+ fprintf(file, "%c", isprint(cp[i]) ? cp[i] : '.');
+ fputs(")\n", file);
+ }
+ }
+ rdatalen -= 4 + optlen;
+ }
+ } else {
+ n = ns_sprintrr(handle, &rr, NULL, NULL, buf, (u_int) buflen);
+ if (n < 0) {
+ if (errno == ENOSPC) {
+ free(buf);
+ buf = NULL;
+ if (buflen < 131072) buf = malloc((size_t)(buflen += 1024));
+ if (buf == NULL) {
+ fprintf(file, ";; memory allocation failure\n");
+ return;
+ }
+ continue;
+ }
+ fprintf(file, ";; ns_sprintrr: %s\n", strerror(errno));
+ goto cleanup;
+ }
+ fputs(buf, file);
+ fputc('\n', file);
+ }
+ rrnum++;
+ }
+cleanup:
+ if (buf != NULL) free(buf);
}
/*
* Print the contents of a query.
* This is intended to be primarily a debugging routine.
*/
-void
-res_pquery(const res_state statp, const u_char *msg, int len, FILE *file) {
- ns_msg handle;
- int qdcount, ancount, nscount, arcount;
- u_int opcode, rcode, id;
+void res_pquery(const res_state statp, const u_char* msg, int len, FILE* file) {
+ ns_msg handle;
+ int qdcount, ancount, nscount, arcount;
+ u_int opcode, rcode, id;
- if (ns_initparse(msg, len, &handle) < 0) {
- fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
- return;
- }
- opcode = ns_msg_getflag(handle, ns_f_opcode);
- rcode = ns_msg_getflag(handle, ns_f_rcode);
- id = ns_msg_id(handle);
- qdcount = ns_msg_count(handle, ns_s_qd);
- ancount = ns_msg_count(handle, ns_s_an);
- nscount = ns_msg_count(handle, ns_s_ns);
- arcount = ns_msg_count(handle, ns_s_ar);
+ if (ns_initparse(msg, len, &handle) < 0) {
+ fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
+ return;
+ }
+ opcode = ns_msg_getflag(handle, ns_f_opcode);
+ rcode = ns_msg_getflag(handle, ns_f_rcode);
+ id = ns_msg_id(handle);
+ qdcount = ns_msg_count(handle, ns_s_qd);
+ ancount = ns_msg_count(handle, ns_s_an);
+ nscount = ns_msg_count(handle, ns_s_ns);
+ arcount = ns_msg_count(handle, ns_s_ar);
- /*
- * Print header fields.
- */
- if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
- fprintf(file,
- ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
- _res_opcodes[opcode], p_rcode((int)rcode), id);
- if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX))
- putc(';', file);
- if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
- fprintf(file, "; flags:");
- if (ns_msg_getflag(handle, ns_f_qr))
- fprintf(file, " qr");
- if (ns_msg_getflag(handle, ns_f_aa))
- fprintf(file, " aa");
- if (ns_msg_getflag(handle, ns_f_tc))
- fprintf(file, " tc");
- if (ns_msg_getflag(handle, ns_f_rd))
- fprintf(file, " rd");
- if (ns_msg_getflag(handle, ns_f_ra))
- fprintf(file, " ra");
- if (ns_msg_getflag(handle, ns_f_z))
- fprintf(file, " ??");
- if (ns_msg_getflag(handle, ns_f_ad))
- fprintf(file, " ad");
- if (ns_msg_getflag(handle, ns_f_cd))
- fprintf(file, " cd");
- }
- if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
- fprintf(file, "; %s: %d",
- p_section(ns_s_qd, (int)opcode), qdcount);
- fprintf(file, ", %s: %d",
- p_section(ns_s_an, (int)opcode), ancount);
- fprintf(file, ", %s: %d",
- p_section(ns_s_ns, (int)opcode), nscount);
- fprintf(file, ", %s: %d",
- p_section(ns_s_ar, (int)opcode), arcount);
- }
- if ((!statp->pfcode) || (statp->pfcode &
- (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
- putc('\n',file);
- }
- /*
- * Print the various sections.
- */
- do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
- do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
- do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
- do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
- if (qdcount == 0 && ancount == 0 &&
- nscount == 0 && arcount == 0)
- putc('\n', file);
+ /*
+ * Print header fields.
+ */
+ if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
+ fprintf(file, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n", _res_opcodes[opcode],
+ p_rcode((int) rcode), id);
+ if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX)) putc(';', file);
+ if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
+ fprintf(file, "; flags:");
+ if (ns_msg_getflag(handle, ns_f_qr)) fprintf(file, " qr");
+ if (ns_msg_getflag(handle, ns_f_aa)) fprintf(file, " aa");
+ if (ns_msg_getflag(handle, ns_f_tc)) fprintf(file, " tc");
+ if (ns_msg_getflag(handle, ns_f_rd)) fprintf(file, " rd");
+ if (ns_msg_getflag(handle, ns_f_ra)) fprintf(file, " ra");
+ if (ns_msg_getflag(handle, ns_f_z)) fprintf(file, " ??");
+ if (ns_msg_getflag(handle, ns_f_ad)) fprintf(file, " ad");
+ if (ns_msg_getflag(handle, ns_f_cd)) fprintf(file, " cd");
+ }
+ if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
+ fprintf(file, "; %s: %d", p_section(ns_s_qd, (int) opcode), qdcount);
+ fprintf(file, ", %s: %d", p_section(ns_s_an, (int) opcode), ancount);
+ fprintf(file, ", %s: %d", p_section(ns_s_ns, (int) opcode), nscount);
+ fprintf(file, ", %s: %d", p_section(ns_s_ar, (int) opcode), arcount);
+ }
+ if ((!statp->pfcode) || (statp->pfcode & (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
+ putc('\n', file);
+ }
+ /*
+ * Print the various sections.
+ */
+ do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
+ do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
+ do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
+ do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
+ if (qdcount == 0 && ancount == 0 && nscount == 0 && arcount == 0) putc('\n', file);
}
-const u_char *
-p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
- char name[MAXDNAME];
- int n;
+const u_char* p_cdnname(const u_char* cp, const u_char* msg, int len, FILE* file) {
+ char name[MAXDNAME];
+ int n;
- if ((n = dn_expand(msg, msg + len, cp, name, (int)sizeof name)) < 0)
- return (NULL);
- if (name[0] == '\0')
- putc('.', file);
- else
- fputs(name, file);
- return (cp + n);
+ if ((n = dn_expand(msg, msg + len, cp, name, (int) sizeof name)) < 0) return (NULL);
+ if (name[0] == '\0')
+ putc('.', file);
+ else
+ fputs(name, file);
+ return (cp + n);
}
-const u_char *
-p_cdname(const u_char *cp, const u_char *msg, FILE *file) {
- return (p_cdnname(cp, msg, PACKETSZ, file));
+const u_char* p_cdname(const u_char* cp, const u_char* msg, FILE* file) {
+ return (p_cdnname(cp, msg, PACKETSZ, file));
}
/* Return a fully-qualified domain name from a compressed name (with
length supplied). */
-const u_char *
-p_fqnname(const u_char *cp, const u_char *msg, int msglen, char *name,
- int namelen)
-{
- int n;
- size_t newlen;
+const u_char* p_fqnname(const u_char* cp, const u_char* msg, int msglen, char* name, int namelen) {
+ int n;
+ size_t newlen;
- if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
- return (NULL);
- newlen = strlen(name);
- if (newlen == 0 || name[newlen - 1] != '.') {
- if ((int)newlen + 1 >= namelen) /* Lack space for final dot */
- return (NULL);
- else
- strcpy(name + newlen, ".");
- }
- return (cp + n);
+ if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0) return (NULL);
+ newlen = strlen(name);
+ if (newlen == 0 || name[newlen - 1] != '.') {
+ if ((int) newlen + 1 >= namelen) /* Lack space for final dot */
+ return (NULL);
+ else
+ strcpy(name + newlen, ".");
+ }
+ return (cp + n);
}
/* XXX: the rest of these functions need to become length-limited, too. */
-const u_char *
-p_fqname(const u_char *cp, const u_char *msg, FILE *file) {
- char name[MAXDNAME];
- const u_char *n;
+const u_char* p_fqname(const u_char* cp, const u_char* msg, FILE* file) {
+ char name[MAXDNAME];
+ const u_char* n;
- n = p_fqnname(cp, msg, MAXCDNAME, name, (int)sizeof name);
- if (n == NULL)
- return (NULL);
- fputs(name, file);
- return (n);
+ n = p_fqnname(cp, msg, MAXCDNAME, name, (int) sizeof name);
+ if (n == NULL) return (NULL);
+ fputs(name, file);
+ return (n);
}
/*
@@ -408,52 +352,39 @@
* C_ANY, but you can't have any records of that class in the database.)
*/
const struct res_sym __p_class_syms[] = {
- {C_IN, "IN", (char *)0},
- {C_CHAOS, "CH", (char *)0},
- {C_CHAOS, "CHAOS", (char *)0},
- {C_HS, "HS", (char *)0},
- {C_HS, "HESIOD", (char *)0},
- {C_ANY, "ANY", (char *)0},
- {C_NONE, "NONE", (char *)0},
- {C_IN, (char *)0, (char *)0}
-};
+ {C_IN, "IN", (char*) 0}, {C_CHAOS, "CH", (char*) 0}, {C_CHAOS, "CHAOS", (char*) 0},
+ {C_HS, "HS", (char*) 0}, {C_HS, "HESIOD", (char*) 0}, {C_ANY, "ANY", (char*) 0},
+ {C_NONE, "NONE", (char*) 0}, {C_IN, (char*) 0, (char*) 0}};
/*
* Names of message sections.
*/
-static const struct res_sym __p_default_section_syms[] = {
- {ns_s_qd, "QUERY", (char *)0},
- {ns_s_an, "ANSWER", (char *)0},
- {ns_s_ns, "AUTHORITY", (char *)0},
- {ns_s_ar, "ADDITIONAL", (char *)0},
- {0, (char *)0, (char *)0}
-};
+static const struct res_sym __p_default_section_syms[] = {{ns_s_qd, "QUERY", (char*) 0},
+ {ns_s_an, "ANSWER", (char*) 0},
+ {ns_s_ns, "AUTHORITY", (char*) 0},
+ {ns_s_ar, "ADDITIONAL", (char*) 0},
+ {0, (char*) 0, (char*) 0}};
-static const struct res_sym __p_update_section_syms[] = {
- {S_ZONE, "ZONE", (char *)0},
- {S_PREREQ, "PREREQUISITE", (char *)0},
- {S_UPDATE, "UPDATE", (char *)0},
- {S_ADDT, "ADDITIONAL", (char *)0},
- {0, (char *)0, (char *)0}
-};
+static const struct res_sym __p_update_section_syms[] = {{S_ZONE, "ZONE", (char*) 0},
+ {S_PREREQ, "PREREQUISITE", (char*) 0},
+ {S_UPDATE, "UPDATE", (char*) 0},
+ {S_ADDT, "ADDITIONAL", (char*) 0},
+ {0, (char*) 0, (char*) 0}};
const struct res_sym __p_key_syms[] = {
- {NS_ALG_MD5RSA, "RSA", "RSA KEY with MD5 hash"},
- {NS_ALG_DH, "DH", "Diffie Hellman"},
- {NS_ALG_DSA, "DSA", "Digital Signature Algorithm"},
- {NS_ALG_EXPIRE_ONLY, "EXPIREONLY", "No algorithm"},
- {NS_ALG_PRIVATE_OID, "PRIVATE", "Algorithm obtained from OID"},
- {0, NULL, NULL}
-};
+ {NS_ALG_MD5RSA, "RSA", "RSA KEY with MD5 hash"},
+ {NS_ALG_DH, "DH", "Diffie Hellman"},
+ {NS_ALG_DSA, "DSA", "Digital Signature Algorithm"},
+ {NS_ALG_EXPIRE_ONLY, "EXPIREONLY", "No algorithm"},
+ {NS_ALG_PRIVATE_OID, "PRIVATE", "Algorithm obtained from OID"},
+ {0, NULL, NULL}};
-const struct res_sym __p_cert_syms[] = {
- {cert_t_pkix, "PKIX", "PKIX (X.509v3) Certificate"},
- {cert_t_spki, "SPKI", "SPKI certificate"},
- {cert_t_pgp, "PGP", "PGP certificate"},
- {cert_t_url, "URL", "URL Private"},
- {cert_t_oid, "OID", "OID Private"},
- {0, NULL, NULL}
-};
+const struct res_sym __p_cert_syms[] = {{cert_t_pkix, "PKIX", "PKIX (X.509v3) Certificate"},
+ {cert_t_spki, "SPKI", "SPKI certificate"},
+ {cert_t_pgp, "PGP", "PGP certificate"},
+ {cert_t_url, "URL", "URL Private"},
+ {cert_t_oid, "OID", "OID Private"},
+ {0, NULL, NULL}};
/*
* Names of RR types and qtypes. Types and qtypes are the same, except
@@ -461,290 +392,287 @@
* T_ANY, but you can't have any records of that type in the database.)
*/
const struct res_sym __p_type_syms[] = {
- {ns_t_a, "A", "address"},
- {ns_t_ns, "NS", "name server"},
- {ns_t_md, "MD", "mail destination (deprecated)"},
- {ns_t_mf, "MF", "mail forwarder (deprecated)"},
- {ns_t_cname, "CNAME", "canonical name"},
- {ns_t_soa, "SOA", "start of authority"},
- {ns_t_mb, "MB", "mailbox"},
- {ns_t_mg, "MG", "mail group member"},
- {ns_t_mr, "MR", "mail rename"},
- {ns_t_null, "NULL", "null"},
- {ns_t_wks, "WKS", "well-known service (deprecated)"},
- {ns_t_ptr, "PTR", "domain name pointer"},
- {ns_t_hinfo, "HINFO", "host information"},
- {ns_t_minfo, "MINFO", "mailbox information"},
- {ns_t_mx, "MX", "mail exchanger"},
- {ns_t_txt, "TXT", "text"},
- {ns_t_rp, "RP", "responsible person"},
- {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
- {ns_t_x25, "X25", "X25 address"},
- {ns_t_isdn, "ISDN", "ISDN address"},
- {ns_t_rt, "RT", "router"},
- {ns_t_nsap, "NSAP", "nsap address"},
- {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
- {ns_t_sig, "SIG", "signature"},
- {ns_t_key, "KEY", "key"},
- {ns_t_px, "PX", "mapping information"},
- {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
- {ns_t_aaaa, "AAAA", "IPv6 address"},
- {ns_t_loc, "LOC", "location"},
- {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
- {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
- {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
- {ns_t_srv, "SRV", "server selection"},
- {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
- {ns_t_naptr, "NAPTR", "naptr"},
- {ns_t_kx, "KX", "key exchange"},
- {ns_t_cert, "CERT", "certificate"},
- {ns_t_a6, "A", "IPv6 address (experminental)"},
- {ns_t_dname, "DNAME", "non-terminal redirection"},
- {ns_t_opt, "OPT", "opt"},
- {ns_t_apl, "apl", "apl"},
- {ns_t_ds, "DS", "delegation signer"},
- {ns_t_sshfp, "SSFP", "SSH fingerprint"},
- {ns_t_ipseckey, "IPSECKEY", "IPSEC key"},
- {ns_t_rrsig, "RRSIG", "rrsig"},
- {ns_t_nsec, "NSEC", "nsec"},
- {ns_t_dnskey, "DNSKEY", "DNS key"},
- {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"},
- {ns_t_nsec3, "NSEC3", "nsec3"},
- {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
- {ns_t_hip, "HIP", "host identity protocol"},
- {ns_t_spf, "SPF", "sender policy framework"},
- {ns_t_tkey, "TKEY", "tkey"},
- {ns_t_tsig, "TSIG", "transaction signature"},
- {ns_t_ixfr, "IXFR", "incremental zone transfer"},
- {ns_t_axfr, "AXFR", "zone transfer"},
- {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
- {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
- {ns_t_maila, "MAILA", "mail agent (deprecated)"},
- {ns_t_naptr, "NAPTR", "URN Naming Authority"},
- {ns_t_kx, "KX", "Key Exchange"},
- {ns_t_cert, "CERT", "Certificate"},
- {ns_t_a6, "A6", "IPv6 Address"},
- {ns_t_dname, "DNAME", "dname"},
- {ns_t_sink, "SINK", "Kitchen Sink (experimental)"},
- {ns_t_opt, "OPT", "EDNS Options"},
- {ns_t_any, "ANY", "\"any\""},
- {ns_t_dlv, "DLV", "DNSSEC look-aside validation"},
- {0, NULL, NULL}
-};
+ {ns_t_a, "A", "address"},
+ {ns_t_ns, "NS", "name server"},
+ {ns_t_md, "MD", "mail destination (deprecated)"},
+ {ns_t_mf, "MF", "mail forwarder (deprecated)"},
+ {ns_t_cname, "CNAME", "canonical name"},
+ {ns_t_soa, "SOA", "start of authority"},
+ {ns_t_mb, "MB", "mailbox"},
+ {ns_t_mg, "MG", "mail group member"},
+ {ns_t_mr, "MR", "mail rename"},
+ {ns_t_null, "NULL", "null"},
+ {ns_t_wks, "WKS", "well-known service (deprecated)"},
+ {ns_t_ptr, "PTR", "domain name pointer"},
+ {ns_t_hinfo, "HINFO", "host information"},
+ {ns_t_minfo, "MINFO", "mailbox information"},
+ {ns_t_mx, "MX", "mail exchanger"},
+ {ns_t_txt, "TXT", "text"},
+ {ns_t_rp, "RP", "responsible person"},
+ {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
+ {ns_t_x25, "X25", "X25 address"},
+ {ns_t_isdn, "ISDN", "ISDN address"},
+ {ns_t_rt, "RT", "router"},
+ {ns_t_nsap, "NSAP", "nsap address"},
+ {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
+ {ns_t_sig, "SIG", "signature"},
+ {ns_t_key, "KEY", "key"},
+ {ns_t_px, "PX", "mapping information"},
+ {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
+ {ns_t_aaaa, "AAAA", "IPv6 address"},
+ {ns_t_loc, "LOC", "location"},
+ {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
+ {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
+ {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
+ {ns_t_srv, "SRV", "server selection"},
+ {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
+ {ns_t_naptr, "NAPTR", "naptr"},
+ {ns_t_kx, "KX", "key exchange"},
+ {ns_t_cert, "CERT", "certificate"},
+ {ns_t_a6, "A", "IPv6 address (experminental)"},
+ {ns_t_dname, "DNAME", "non-terminal redirection"},
+ {ns_t_opt, "OPT", "opt"},
+ {ns_t_apl, "apl", "apl"},
+ {ns_t_ds, "DS", "delegation signer"},
+ {ns_t_sshfp, "SSFP", "SSH fingerprint"},
+ {ns_t_ipseckey, "IPSECKEY", "IPSEC key"},
+ {ns_t_rrsig, "RRSIG", "rrsig"},
+ {ns_t_nsec, "NSEC", "nsec"},
+ {ns_t_dnskey, "DNSKEY", "DNS key"},
+ {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"},
+ {ns_t_nsec3, "NSEC3", "nsec3"},
+ {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
+ {ns_t_hip, "HIP", "host identity protocol"},
+ {ns_t_spf, "SPF", "sender policy framework"},
+ {ns_t_tkey, "TKEY", "tkey"},
+ {ns_t_tsig, "TSIG", "transaction signature"},
+ {ns_t_ixfr, "IXFR", "incremental zone transfer"},
+ {ns_t_axfr, "AXFR", "zone transfer"},
+ {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
+ {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
+ {ns_t_maila, "MAILA", "mail agent (deprecated)"},
+ {ns_t_naptr, "NAPTR", "URN Naming Authority"},
+ {ns_t_kx, "KX", "Key Exchange"},
+ {ns_t_cert, "CERT", "Certificate"},
+ {ns_t_a6, "A6", "IPv6 Address"},
+ {ns_t_dname, "DNAME", "dname"},
+ {ns_t_sink, "SINK", "Kitchen Sink (experimental)"},
+ {ns_t_opt, "OPT", "EDNS Options"},
+ {ns_t_any, "ANY", "\"any\""},
+ {ns_t_dlv, "DLV", "DNSSEC look-aside validation"},
+ {0, NULL, NULL}};
/*
* Names of DNS rcodes.
*/
-const struct res_sym __p_rcode_syms[] = {
- {ns_r_noerror, "NOERROR", "no error"},
- {ns_r_formerr, "FORMERR", "format error"},
- {ns_r_servfail, "SERVFAIL", "server failed"},
- {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
- {ns_r_notimpl, "NOTIMP", "not implemented"},
- {ns_r_refused, "REFUSED", "refused"},
- {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
- {ns_r_yxrrset, "YXRRSET", "rrset exists"},
- {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
- {ns_r_notauth, "NOTAUTH", "not authoritative"},
- {ns_r_notzone, "NOTZONE", "Not in zone"},
- {ns_r_max, "", ""},
- {ns_r_badsig, "BADSIG", "bad signature"},
- {ns_r_badkey, "BADKEY", "bad key"},
- {ns_r_badtime, "BADTIME", "bad time"},
- {0, NULL, NULL}
-};
+const struct res_sym __p_rcode_syms[] = {{ns_r_noerror, "NOERROR", "no error"},
+ {ns_r_formerr, "FORMERR", "format error"},
+ {ns_r_servfail, "SERVFAIL", "server failed"},
+ {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
+ {ns_r_notimpl, "NOTIMP", "not implemented"},
+ {ns_r_refused, "REFUSED", "refused"},
+ {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
+ {ns_r_yxrrset, "YXRRSET", "rrset exists"},
+ {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
+ {ns_r_notauth, "NOTAUTH", "not authoritative"},
+ {ns_r_notzone, "NOTZONE", "Not in zone"},
+ {ns_r_max, "", ""},
+ {ns_r_badsig, "BADSIG", "bad signature"},
+ {ns_r_badkey, "BADKEY", "bad key"},
+ {ns_r_badtime, "BADTIME", "bad time"},
+ {0, NULL, NULL}};
-int
-sym_ston(const struct res_sym *syms, const char *name, int *success) {
- for (; syms->name != 0; syms++) {
- if (strcasecmp (name, syms->name) == 0) {
- if (success)
- *success = 1;
- return (syms->number);
- }
- }
- if (success)
- *success = 0;
- return (syms->number); /* The default value. */
+int sym_ston(const struct res_sym* syms, const char* name, int* success) {
+ for (; syms->name != 0; syms++) {
+ if (strcasecmp(name, syms->name) == 0) {
+ if (success) *success = 1;
+ return (syms->number);
+ }
+ }
+ if (success) *success = 0;
+ return (syms->number); /* The default value. */
}
-const char *
-sym_ntos(const struct res_sym *syms, int number, int *success) {
- static char unname[20];
+const char* sym_ntos(const struct res_sym* syms, int number, int* success) {
+ static char unname[20];
- for (; syms->name != 0; syms++) {
- if (number == syms->number) {
- if (success)
- *success = 1;
- return (syms->name);
- }
- }
+ for (; syms->name != 0; syms++) {
+ if (number == syms->number) {
+ if (success) *success = 1;
+ return (syms->name);
+ }
+ }
- snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
- if (success)
- *success = 0;
- return (unname);
+ snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
+ if (success) *success = 0;
+ return (unname);
}
-const char *
-sym_ntop(const struct res_sym *syms, int number, int *success) {
- static char unname[20];
+const char* sym_ntop(const struct res_sym* syms, int number, int* success) {
+ static char unname[20];
- for (; syms->name != 0; syms++) {
- if (number == syms->number) {
- if (success)
- *success = 1;
- return (syms->humanname);
- }
- }
- snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
- if (success)
- *success = 0;
- return (unname);
+ for (; syms->name != 0; syms++) {
+ if (number == syms->number) {
+ if (success) *success = 1;
+ return (syms->humanname);
+ }
+ }
+ snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
+ if (success) *success = 0;
+ return (unname);
}
/*
* Return a string for the type.
*/
-const char *
-p_type(int type) {
- int success;
- const char *result;
- static char typebuf[20];
+const char* p_type(int type) {
+ int success;
+ const char* result;
+ static char typebuf[20];
- result = sym_ntos(__p_type_syms, type, &success);
- if (success)
- return (result);
- if (type < 0 || type > 0xffff)
- return ("BADTYPE");
- snprintf(typebuf, sizeof(typebuf), "TYPE%d", type);
- return (typebuf);
+ result = sym_ntos(__p_type_syms, type, &success);
+ if (success) return (result);
+ if (type < 0 || type > 0xffff) return ("BADTYPE");
+ snprintf(typebuf, sizeof(typebuf), "TYPE%d", type);
+ return (typebuf);
}
/*
* Return a string for the type.
*/
-const char *
-p_section(int section, int opcode) {
- const struct res_sym *symbols;
+const char* p_section(int section, int opcode) {
+ const struct res_sym* symbols;
- switch (opcode) {
- case ns_o_update:
- symbols = __p_update_section_syms;
- break;
- default:
- symbols = __p_default_section_syms;
- break;
- }
- return (sym_ntos(symbols, section, (int *)0));
+ switch (opcode) {
+ case ns_o_update:
+ symbols = __p_update_section_syms;
+ break;
+ default:
+ symbols = __p_default_section_syms;
+ break;
+ }
+ return (sym_ntos(symbols, section, (int*) 0));
}
/*
* Return a mnemonic for class.
*/
-const char *
-p_class(int class) {
- int success;
- const char *result;
- static char classbuf[20];
+const char* p_class(int class) {
+ int success;
+ const char* result;
+ static char classbuf[20];
- result = sym_ntos(__p_class_syms, class, &success);
- if (success)
- return (result);
- if (class < 0 || class > 0xffff)
- return ("BADCLASS");
- snprintf(classbuf, sizeof(classbuf), "CLASS%d", class);
- return (classbuf);
+ result = sym_ntos(__p_class_syms, class, &success);
+ if (success) return (result);
+ if (class < 0 || class > 0xffff) return ("BADCLASS");
+ snprintf(classbuf, sizeof(classbuf), "CLASS%d", class);
+ return (classbuf);
}
/*
* Return a mnemonic for an option
*/
-const char *
-p_option(u_long option) {
- static char nbuf[40];
+const char* p_option(u_long option) {
+ static char nbuf[40];
- switch (option) {
- case RES_INIT: return "init";
- case RES_DEBUG: return "debug";
- case RES_AAONLY: return "aaonly(unimpl)";
- case RES_USEVC: return "usevc";
- case RES_PRIMARY: return "primry(unimpl)";
- case RES_IGNTC: return "igntc";
- case RES_RECURSE: return "recurs";
- case RES_DEFNAMES: return "defnam";
- case RES_STAYOPEN: return "styopn";
- case RES_DNSRCH: return "dnsrch";
- case RES_INSECURE1: return "insecure1";
- case RES_INSECURE2: return "insecure2";
- case RES_NOALIASES: return "noaliases";
- case RES_USE_INET6: return "inet6";
-#ifdef RES_USE_EDNS0 /* KAME extension */
- case RES_USE_EDNS0: return "edns0";
+ switch (option) {
+ case RES_INIT:
+ return "init";
+ case RES_DEBUG:
+ return "debug";
+ case RES_AAONLY:
+ return "aaonly(unimpl)";
+ case RES_USEVC:
+ return "usevc";
+ case RES_PRIMARY:
+ return "primry(unimpl)";
+ case RES_IGNTC:
+ return "igntc";
+ case RES_RECURSE:
+ return "recurs";
+ case RES_DEFNAMES:
+ return "defnam";
+ case RES_STAYOPEN:
+ return "styopn";
+ case RES_DNSRCH:
+ return "dnsrch";
+ case RES_INSECURE1:
+ return "insecure1";
+ case RES_INSECURE2:
+ return "insecure2";
+ case RES_NOALIASES:
+ return "noaliases";
+ case RES_USE_INET6:
+ return "inet6";
+#ifdef RES_USE_EDNS0 /* KAME extension */
+ case RES_USE_EDNS0:
+ return "edns0";
#endif
#ifdef RES_USE_DNAME
- case RES_USE_DNAME: return "dname";
+ case RES_USE_DNAME:
+ return "dname";
#endif
#ifdef RES_USE_DNSSEC
- case RES_USE_DNSSEC: return "dnssec";
+ case RES_USE_DNSSEC:
+ return "dnssec";
#endif
#ifdef RES_NOTLDQUERY
- case RES_NOTLDQUERY: return "no-tld-query";
+ case RES_NOTLDQUERY:
+ return "no-tld-query";
#endif
#ifdef RES_NO_NIBBLE2
- case RES_NO_NIBBLE2: return "no-nibble2";
+ case RES_NO_NIBBLE2:
+ return "no-nibble2";
#endif
- /* XXX nonreentrant */
- default: snprintf(nbuf, sizeof(nbuf), "?0x%lx?", (u_long)option);
- return (nbuf);
- }
+ /* XXX nonreentrant */
+ default:
+ snprintf(nbuf, sizeof(nbuf), "?0x%lx?", (u_long) option);
+ return (nbuf);
+ }
}
/*
* Return a mnemonic for a time to live.
*/
-const char *
-p_time(u_int32_t value) {
- static char nbuf[40]; /* XXX nonreentrant */
+const char* p_time(u_int32_t value) {
+ static char nbuf[40]; /* XXX nonreentrant */
- if (ns_format_ttl((u_long)value, nbuf, sizeof nbuf) < 0)
- snprintf(nbuf, sizeof(nbuf), "%u", value);
- return (nbuf);
+ if (ns_format_ttl((u_long) value, nbuf, sizeof nbuf) < 0)
+ snprintf(nbuf, sizeof(nbuf), "%u", value);
+ return (nbuf);
}
/*
* Return a string for the rcode.
*/
-const char *
-p_rcode(int rcode) {
- return (sym_ntos(__p_rcode_syms, rcode, (int *)0));
+const char* p_rcode(int rcode) {
+ return (sym_ntos(__p_rcode_syms, rcode, (int*) 0));
}
/*
* Return a string for a res_sockaddr_union.
*/
-const char *
-p_sockun(union res_sockaddr_union u, char *buf, size_t size) {
- char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"];
+const char* p_sockun(union res_sockaddr_union u, char* buf, size_t size) {
+ char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"];
- switch (u.sin.sin_family) {
- case AF_INET:
- inet_ntop(AF_INET, &u.sin.sin_addr, ret, (socklen_t)sizeof ret);
- break;
+ switch (u.sin.sin_family) {
+ case AF_INET:
+ inet_ntop(AF_INET, &u.sin.sin_addr, ret, (socklen_t) sizeof ret);
+ break;
#ifdef HAS_INET6_STRUCTS
- case AF_INET6:
- inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret);
- break;
+ case AF_INET6:
+ inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret);
+ break;
#endif
- default:
- snprintf(ret, sizeof(ret), "[af%d]", u.sin.sin_family);
- break;
- }
- if (size > 0U) {
- strncpy(buf, ret, size - 1);
- buf[size - 1] = '0';
- }
- return (buf);
+ default:
+ snprintf(ret, sizeof(ret), "[af%d]", u.sin.sin_family);
+ break;
+ }
+ if (size > 0U) {
+ strncpy(buf, ret, size - 1);
+ buf[size - 1] = '0';
+ }
+ return (buf);
}
/*
@@ -753,473 +681,422 @@
* by 60*60*1000 for that.
*/
-static const unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
- 1000000,10000000,100000000,1000000000};
+static const unsigned int poweroften[10] = {1, 10, 100, 1000, 10000,
+ 100000, 1000000, 10000000, 100000000, 1000000000};
/* takes an XeY precision/size value, returns a string representation. */
-static const char *
-precsize_ntoa(u_int32_t prec)
-{
- static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */
- unsigned long val;
- int mantissa, exponent;
+static const char* precsize_ntoa(u_int32_t prec) {
+ static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */
+ unsigned long val;
+ int mantissa, exponent;
- mantissa = (int)((prec >> 4) & 0x0f) % 10;
- exponent = (int)((prec >> 0) & 0x0f) % 10;
+ mantissa = (int) ((prec >> 4) & 0x0f) % 10;
+ exponent = (int) ((prec >> 0) & 0x0f) % 10;
- val = mantissa * poweroften[exponent];
+ val = mantissa * poweroften[exponent];
- (void) snprintf(retbuf, sizeof(retbuf), "%lu.%.2lu", val/100, val%100);
- return (retbuf);
+ (void) snprintf(retbuf, sizeof(retbuf), "%lu.%.2lu", val / 100, val % 100);
+ return (retbuf);
}
/* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
-static u_int8_t
-precsize_aton(const char **strptr) {
- unsigned int mval = 0, cmval = 0;
- u_int8_t retval = 0;
- const char *cp;
- int exponent;
- int mantissa;
+static u_int8_t precsize_aton(const char** strptr) {
+ unsigned int mval = 0, cmval = 0;
+ u_int8_t retval = 0;
+ const char* cp;
+ int exponent;
+ int mantissa;
- cp = *strptr;
+ cp = *strptr;
- while (isdigit((unsigned char)*cp))
- mval = mval * 10 + (*cp++ - '0');
+ while (isdigit((unsigned char) *cp)) mval = mval * 10 + (*cp++ - '0');
- if (*cp == '.') { /* centimeters */
- cp++;
- if (isdigit((unsigned char)*cp)) {
- cmval = (*cp++ - '0') * 10;
- if (isdigit((unsigned char)*cp)) {
- cmval += (*cp++ - '0');
- }
- }
- }
- cmval = (mval * 100) + cmval;
+ if (*cp == '.') { /* centimeters */
+ cp++;
+ if (isdigit((unsigned char) *cp)) {
+ cmval = (*cp++ - '0') * 10;
+ if (isdigit((unsigned char) *cp)) {
+ cmval += (*cp++ - '0');
+ }
+ }
+ }
+ cmval = (mval * 100) + cmval;
- for (exponent = 0; exponent < 9; exponent++)
- if (cmval < poweroften[exponent+1])
- break;
+ for (exponent = 0; exponent < 9; exponent++)
+ if (cmval < poweroften[exponent + 1]) break;
- mantissa = cmval / poweroften[exponent];
- if (mantissa > 9)
- mantissa = 9;
+ mantissa = cmval / poweroften[exponent];
+ if (mantissa > 9) mantissa = 9;
- retval = (mantissa << 4) | exponent;
+ retval = (mantissa << 4) | exponent;
- *strptr = cp;
+ *strptr = cp;
- return (retval);
+ return (retval);
}
/* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
-static u_int32_t
-latlon2ul(const char **latlonstrptr, int *which) {
- const char *cp;
- u_int32_t retval;
- int deg = 0, min = 0, secs = 0, secsfrac = 0;
+static u_int32_t latlon2ul(const char** latlonstrptr, int* which) {
+ const char* cp;
+ u_int32_t retval;
+ int deg = 0, min = 0, secs = 0, secsfrac = 0;
- cp = *latlonstrptr;
+ cp = *latlonstrptr;
- while (isdigit((unsigned char)*cp))
- deg = deg * 10 + (*cp++ - '0');
+ while (isdigit((unsigned char) *cp)) deg = deg * 10 + (*cp++ - '0');
- while (isspace((unsigned char)*cp))
- cp++;
+ while (isspace((unsigned char) *cp)) cp++;
- if (!(isdigit((unsigned char)*cp)))
- goto fndhemi;
+ if (!(isdigit((unsigned char) *cp))) goto fndhemi;
- while (isdigit((unsigned char)*cp))
- min = min * 10 + (*cp++ - '0');
+ while (isdigit((unsigned char) *cp)) min = min * 10 + (*cp++ - '0');
- while (isspace((unsigned char)*cp))
- cp++;
+ while (isspace((unsigned char) *cp)) cp++;
- if (!(isdigit((unsigned char)*cp)))
- goto fndhemi;
+ if (!(isdigit((unsigned char) *cp))) goto fndhemi;
- while (isdigit((unsigned char)*cp))
- secs = secs * 10 + (*cp++ - '0');
+ while (isdigit((unsigned char) *cp)) secs = secs * 10 + (*cp++ - '0');
- if (*cp == '.') { /* decimal seconds */
- cp++;
- if (isdigit((unsigned char)*cp)) {
- secsfrac = (*cp++ - '0') * 100;
- if (isdigit((unsigned char)*cp)) {
- secsfrac += (*cp++ - '0') * 10;
- if (isdigit((unsigned char)*cp)) {
- secsfrac += (*cp++ - '0');
- }
- }
- }
- }
+ if (*cp == '.') { /* decimal seconds */
+ cp++;
+ if (isdigit((unsigned char) *cp)) {
+ secsfrac = (*cp++ - '0') * 100;
+ if (isdigit((unsigned char) *cp)) {
+ secsfrac += (*cp++ - '0') * 10;
+ if (isdigit((unsigned char) *cp)) {
+ secsfrac += (*cp++ - '0');
+ }
+ }
+ }
+ }
- while (!isspace((unsigned char)*cp)) /* if any trailing garbage */
- cp++;
+ while (!isspace((unsigned char) *cp)) /* if any trailing garbage */
+ cp++;
- while (isspace((unsigned char)*cp))
- cp++;
+ while (isspace((unsigned char) *cp)) cp++;
- fndhemi:
- switch (*cp) {
- case 'N': case 'n':
- case 'E': case 'e':
- retval = ((unsigned)1<<31)
- + (((((deg * 60) + min) * 60) + secs) * 1000)
- + secsfrac;
- break;
- case 'S': case 's':
- case 'W': case 'w':
- retval = ((unsigned)1<<31)
- - (((((deg * 60) + min) * 60) + secs) * 1000)
- - secsfrac;
- break;
- default:
- retval = 0; /* invalid value -- indicates error */
- break;
- }
+fndhemi:
+ switch (*cp) {
+ case 'N':
+ case 'n':
+ case 'E':
+ case 'e':
+ retval = ((unsigned) 1 << 31) + (((((deg * 60) + min) * 60) + secs) * 1000) + secsfrac;
+ break;
+ case 'S':
+ case 's':
+ case 'W':
+ case 'w':
+ retval = ((unsigned) 1 << 31) - (((((deg * 60) + min) * 60) + secs) * 1000) - secsfrac;
+ break;
+ default:
+ retval = 0; /* invalid value -- indicates error */
+ break;
+ }
- switch (*cp) {
- case 'N': case 'n':
- case 'S': case 's':
- *which = 1; /* latitude */
- break;
- case 'E': case 'e':
- case 'W': case 'w':
- *which = 2; /* longitude */
- break;
- default:
- *which = 0; /* error */
- break;
- }
+ switch (*cp) {
+ case 'N':
+ case 'n':
+ case 'S':
+ case 's':
+ *which = 1; /* latitude */
+ break;
+ case 'E':
+ case 'e':
+ case 'W':
+ case 'w':
+ *which = 2; /* longitude */
+ break;
+ default:
+ *which = 0; /* error */
+ break;
+ }
- cp++; /* skip the hemisphere */
+ cp++; /* skip the hemisphere */
- while (!isspace((unsigned char)*cp)) /* if any trailing garbage */
- cp++;
+ while (!isspace((unsigned char) *cp)) /* if any trailing garbage */
+ cp++;
- while (isspace((unsigned char)*cp)) /* move to next field */
- cp++;
+ while (isspace((unsigned char) *cp)) /* move to next field */
+ cp++;
- *latlonstrptr = cp;
+ *latlonstrptr = cp;
- return (retval);
+ return (retval);
}
/* converts a zone file representation in a string to an RDATA on-the-wire
* representation. */
-int
-loc_aton(const char *ascii, u_char *binary)
-{
- const char *cp, *maxcp;
- u_char *bcp;
+int loc_aton(const char* ascii, u_char* binary) {
+ const char *cp, *maxcp;
+ u_char* bcp;
- u_int32_t latit = 0, longit = 0, alt = 0;
- u_int32_t lltemp1 = 0, lltemp2 = 0;
- int altmeters = 0, altfrac = 0, altsign = 1;
- u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
- u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
- u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
- int which1 = 0, which2 = 0;
+ u_int32_t latit = 0, longit = 0, alt = 0;
+ u_int32_t lltemp1 = 0, lltemp2 = 0;
+ int altmeters = 0, altfrac = 0, altsign = 1;
+ u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
+ u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
+ u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
+ int which1 = 0, which2 = 0;
- cp = ascii;
- maxcp = cp + strlen(ascii);
+ cp = ascii;
+ maxcp = cp + strlen(ascii);
- lltemp1 = latlon2ul(&cp, &which1);
+ lltemp1 = latlon2ul(&cp, &which1);
- lltemp2 = latlon2ul(&cp, &which2);
+ lltemp2 = latlon2ul(&cp, &which2);
- switch (which1 + which2) {
- case 3: /* 1 + 2, the only valid combination */
- if ((which1 == 1) && (which2 == 2)) { /* normal case */
- latit = lltemp1;
- longit = lltemp2;
- } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
- longit = lltemp1;
- latit = lltemp2;
- } else { /* some kind of brokenness */
- return (0);
- }
- break;
- default: /* we didn't get one of each */
- return (0);
- }
+ switch (which1 + which2) {
+ case 3: /* 1 + 2, the only valid combination */
+ if ((which1 == 1) && (which2 == 2)) { /* normal case */
+ latit = lltemp1;
+ longit = lltemp2;
+ } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
+ longit = lltemp1;
+ latit = lltemp2;
+ } else { /* some kind of brokenness */
+ return (0);
+ }
+ break;
+ default: /* we didn't get one of each */
+ return (0);
+ }
- /* altitude */
- if (*cp == '-') {
- altsign = -1;
- cp++;
- }
+ /* altitude */
+ if (*cp == '-') {
+ altsign = -1;
+ cp++;
+ }
- if (*cp == '+')
- cp++;
+ if (*cp == '+') cp++;
- while (isdigit((unsigned char)*cp))
- altmeters = altmeters * 10 + (*cp++ - '0');
+ while (isdigit((unsigned char) *cp)) altmeters = altmeters * 10 + (*cp++ - '0');
- if (*cp == '.') { /* decimal meters */
- cp++;
- if (isdigit((unsigned char)*cp)) {
- altfrac = (*cp++ - '0') * 10;
- if (isdigit((unsigned char)*cp)) {
- altfrac += (*cp++ - '0');
- }
- }
- }
+ if (*cp == '.') { /* decimal meters */
+ cp++;
+ if (isdigit((unsigned char) *cp)) {
+ altfrac = (*cp++ - '0') * 10;
+ if (isdigit((unsigned char) *cp)) {
+ altfrac += (*cp++ - '0');
+ }
+ }
+ }
- alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
+ alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
- while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */
- cp++;
+ while (!isspace((unsigned char) *cp) && (cp < maxcp)) /* if trailing garbage or m */
+ cp++;
- while (isspace((unsigned char)*cp) && (cp < maxcp))
- cp++;
+ while (isspace((unsigned char) *cp) && (cp < maxcp)) cp++;
- if (cp >= maxcp)
- goto defaults;
+ if (cp >= maxcp) goto defaults;
- siz = precsize_aton(&cp);
+ siz = precsize_aton(&cp);
- while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */
- cp++;
+ while (!isspace((unsigned char) *cp) && (cp < maxcp)) /* if trailing garbage or m */
+ cp++;
- while (isspace((unsigned char)*cp) && (cp < maxcp))
- cp++;
+ while (isspace((unsigned char) *cp) && (cp < maxcp)) cp++;
- if (cp >= maxcp)
- goto defaults;
+ if (cp >= maxcp) goto defaults;
- hp = precsize_aton(&cp);
+ hp = precsize_aton(&cp);
- while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */
- cp++;
+ while (!isspace((unsigned char) *cp) && (cp < maxcp)) /* if trailing garbage or m */
+ cp++;
- while (isspace((unsigned char)*cp) && (cp < maxcp))
- cp++;
+ while (isspace((unsigned char) *cp) && (cp < maxcp)) cp++;
- if (cp >= maxcp)
- goto defaults;
+ if (cp >= maxcp) goto defaults;
- vp = precsize_aton(&cp);
+ vp = precsize_aton(&cp);
- defaults:
+defaults:
- bcp = binary;
- *bcp++ = (u_int8_t) 0; /* version byte */
- *bcp++ = siz;
- *bcp++ = hp;
- *bcp++ = vp;
- PUTLONG(latit,bcp);
- PUTLONG(longit,bcp);
- PUTLONG(alt,bcp);
+ bcp = binary;
+ *bcp++ = (u_int8_t) 0; /* version byte */
+ *bcp++ = siz;
+ *bcp++ = hp;
+ *bcp++ = vp;
+ PUTLONG(latit, bcp);
+ PUTLONG(longit, bcp);
+ PUTLONG(alt, bcp);
- return (16); /* size of RR in octets */
+ return (16); /* size of RR in octets */
}
/* takes an on-the-wire LOC RR and formats it in a human readable format. */
-const char *
-loc_ntoa(const u_char *binary, char *ascii, size_t bufsiz)
-{
- static const char *error = "?";
- static char tmpbuf[sizeof
+const char* loc_ntoa(const u_char* binary, char* ascii, size_t bufsiz) {
+ static const char* error = "?";
+ static char tmpbuf[sizeof
"1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
- const u_char *cp = binary;
+ const u_char* cp = binary;
- int latdeg, latmin, latsec, latsecfrac;
- int longdeg, longmin, longsec, longsecfrac;
- char northsouth, eastwest;
- const char *altsign;
- int altmeters, altfrac;
+ int latdeg, latmin, latsec, latsecfrac;
+ int longdeg, longmin, longsec, longsecfrac;
+ char northsouth, eastwest;
+ const char* altsign;
+ int altmeters, altfrac;
- const u_int32_t referencealt = 100000 * 100;
+ const u_int32_t referencealt = 100000 * 100;
- int32_t latval, longval, altval;
- u_int32_t templ;
- u_int8_t sizeval, hpval, vpval, versionval;
+ int32_t latval, longval, altval;
+ u_int32_t templ;
+ u_int8_t sizeval, hpval, vpval, versionval;
- char *sizestr, *hpstr, *vpstr;
+ char *sizestr, *hpstr, *vpstr;
- versionval = *cp++;
+ versionval = *cp++;
- if (ascii == NULL)
- ascii = tmpbuf;
+ if (ascii == NULL) ascii = tmpbuf;
- if (versionval) {
- (void) snprintf(ascii, bufsiz, "; error: unknown LOC RR version");
- return (ascii);
- }
+ if (versionval) {
+ (void) snprintf(ascii, bufsiz, "; error: unknown LOC RR version");
+ return (ascii);
+ }
- sizeval = *cp++;
+ sizeval = *cp++;
- hpval = *cp++;
- vpval = *cp++;
+ hpval = *cp++;
+ vpval = *cp++;
- GETLONG(templ, cp);
- latval = (templ - ((unsigned)1<<31));
+ GETLONG(templ, cp);
+ latval = (templ - ((unsigned) 1 << 31));
- GETLONG(templ, cp);
- longval = (templ - ((unsigned)1<<31));
+ GETLONG(templ, cp);
+ longval = (templ - ((unsigned) 1 << 31));
- GETLONG(templ, cp);
- if (templ < referencealt) { /* below WGS 84 spheroid */
- altval = referencealt - templ;
- altsign = "-";
- } else {
- altval = templ - referencealt;
- altsign = "";
- }
+ GETLONG(templ, cp);
+ if (templ < referencealt) { /* below WGS 84 spheroid */
+ altval = referencealt - templ;
+ altsign = "-";
+ } else {
+ altval = templ - referencealt;
+ altsign = "";
+ }
- if (latval < 0) {
- northsouth = 'S';
- latval = -latval;
- } else
- northsouth = 'N';
+ if (latval < 0) {
+ northsouth = 'S';
+ latval = -latval;
+ } else
+ northsouth = 'N';
- latsecfrac = latval % 1000;
- latval = latval / 1000;
- latsec = latval % 60;
- latval = latval / 60;
- latmin = latval % 60;
- latval = latval / 60;
- latdeg = latval;
+ latsecfrac = latval % 1000;
+ latval = latval / 1000;
+ latsec = latval % 60;
+ latval = latval / 60;
+ latmin = latval % 60;
+ latval = latval / 60;
+ latdeg = latval;
- if (longval < 0) {
- eastwest = 'W';
- longval = -longval;
- } else
- eastwest = 'E';
+ if (longval < 0) {
+ eastwest = 'W';
+ longval = -longval;
+ } else
+ eastwest = 'E';
- longsecfrac = longval % 1000;
- longval = longval / 1000;
- longsec = longval % 60;
- longval = longval / 60;
- longmin = longval % 60;
- longval = longval / 60;
- longdeg = longval;
+ longsecfrac = longval % 1000;
+ longval = longval / 1000;
+ longsec = longval % 60;
+ longval = longval / 60;
+ longmin = longval % 60;
+ longval = longval / 60;
+ longdeg = longval;
- altfrac = altval % 100;
- altmeters = (altval / 100);
+ altfrac = altval % 100;
+ altmeters = (altval / 100);
- sizestr = strdup(precsize_ntoa((u_int32_t)sizeval));
- hpstr = strdup(precsize_ntoa((u_int32_t)hpval));
- vpstr = strdup(precsize_ntoa((u_int32_t)vpval));
+ sizestr = strdup(precsize_ntoa((u_int32_t) sizeval));
+ hpstr = strdup(precsize_ntoa((u_int32_t) hpval));
+ vpstr = strdup(precsize_ntoa((u_int32_t) vpval));
- snprintf(ascii, bufsiz,
- "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm",
- latdeg, latmin, latsec, latsecfrac, northsouth,
- longdeg, longmin, longsec, longsecfrac, eastwest,
- altsign, altmeters, altfrac,
- (sizestr != NULL) ? sizestr : error,
- (hpstr != NULL) ? hpstr : error,
- (vpstr != NULL) ? vpstr : error);
+ snprintf(ascii, bufsiz, "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm",
+ latdeg, latmin, latsec, latsecfrac, northsouth, longdeg, longmin, longsec, longsecfrac,
+ eastwest, altsign, altmeters, altfrac, (sizestr != NULL) ? sizestr : error,
+ (hpstr != NULL) ? hpstr : error, (vpstr != NULL) ? vpstr : error);
- if (sizestr != NULL)
- free(sizestr);
- if (hpstr != NULL)
- free(hpstr);
- if (vpstr != NULL)
- free(vpstr);
+ if (sizestr != NULL) free(sizestr);
+ if (hpstr != NULL) free(hpstr);
+ if (vpstr != NULL) free(vpstr);
- return (ascii);
+ return (ascii);
}
-
/* Return the number of DNS hierarchy levels in the name. */
-int
-dn_count_labels(const char *name) {
- size_t len, i, count;
+int dn_count_labels(const char* name) {
+ size_t len, i, count;
- len = strlen(name);
- for (i = 0, count = 0; i < len; i++) {
- /* XXX need to check for \. or use named's nlabels(). */
- if (name[i] == '.')
- count++;
- }
+ len = strlen(name);
+ for (i = 0, count = 0; i < len; i++) {
+ /* XXX need to check for \. or use named's nlabels(). */
+ if (name[i] == '.') count++;
+ }
- /* don't count initial wildcard */
- if (name[0] == '*')
- if (count)
- count--;
+ /* don't count initial wildcard */
+ if (name[0] == '*')
+ if (count) count--;
- /* don't count the null label for root. */
- /* if terminating '.' not found, must adjust */
- /* count to include last label */
- if (len > 0 && name[len-1] != '.')
- count++;
- _DIAGASSERT(__type_fit(int, count));
- return (int)count;
+ /* don't count the null label for root. */
+ /* if terminating '.' not found, must adjust */
+ /* count to include last label */
+ if (len > 0 && name[len - 1] != '.') count++;
+ _DIAGASSERT(__type_fit(int, count));
+ return (int) count;
}
-
/*
* Make dates expressed in seconds-since-Jan-1-1970 easy to read.
* SIG records are required to be printed like this, by the Secure DNS RFC.
*/
-char *
-p_secstodate (u_long secs) {
- /* XXX nonreentrant */
- static char output[15]; /* YYYYMMDDHHMMSS and null */
- time_t myclock = secs;
- struct tm *mytime;
+char* p_secstodate(u_long secs) {
+ /* XXX nonreentrant */
+ static char output[15]; /* YYYYMMDDHHMMSS and null */
+ time_t myclock = secs;
+ struct tm* mytime;
#ifdef HAVE_TIME_R
- struct tm res;
+ struct tm res;
- mytime = gmtime_r(&myclock, &res);
+ mytime = gmtime_r(&myclock, &res);
#else
- mytime = gmtime(&myclock);
+ mytime = gmtime(&myclock);
#endif
- mytime->tm_year += 1900;
- mytime->tm_mon += 1;
- snprintf(output, sizeof(output), "%04d%02d%02d%02d%02d%02d",
- mytime->tm_year, mytime->tm_mon, mytime->tm_mday,
- mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
- return (output);
+ mytime->tm_year += 1900;
+ mytime->tm_mon += 1;
+ snprintf(output, sizeof(output), "%04d%02d%02d%02d%02d%02d", mytime->tm_year, mytime->tm_mon,
+ mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
+ return (output);
}
-u_int16_t
-res_nametoclass(const char *buf, int *successp) {
- unsigned long result;
- char *endptr;
- int success;
+u_int16_t res_nametoclass(const char* buf, int* successp) {
+ unsigned long result;
+ char* endptr;
+ int success;
- result = sym_ston(__p_class_syms, buf, &success);
- if (success)
- goto done;
+ result = sym_ston(__p_class_syms, buf, &success);
+ if (success) goto done;
- if (strncasecmp(buf, "CLASS", 5) != 0 ||
- !isdigit((unsigned char)buf[5]))
- goto done;
- errno = 0;
- result = strtoul(buf + 5, &endptr, 10);
- if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
- success = 1;
- done:
- if (successp)
- *successp = success;
- return (u_int16_t)(result);
+ if (strncasecmp(buf, "CLASS", 5) != 0 || !isdigit((unsigned char) buf[5])) goto done;
+ errno = 0;
+ result = strtoul(buf + 5, &endptr, 10);
+ if (errno == 0 && *endptr == '\0' && result <= 0xffffU) success = 1;
+done:
+ if (successp) *successp = success;
+ return (u_int16_t)(result);
}
-u_int16_t
-res_nametotype(const char *buf, int *successp) {
- unsigned long result;
- char *endptr;
- int success;
+u_int16_t res_nametotype(const char* buf, int* successp) {
+ unsigned long result;
+ char* endptr;
+ int success;
- result = sym_ston(__p_type_syms, buf, &success);
- if (success)
- goto done;
+ result = sym_ston(__p_type_syms, buf, &success);
+ if (success) goto done;
- if (strncasecmp(buf, "type", 4) != 0 ||
- !isdigit((unsigned char)buf[4]))
- goto done;
- errno = 0;
- result = strtoul(buf + 4, &endptr, 10);
- if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
- success = 1;
- done:
- if (successp)
- *successp = success;
- return (u_int16_t)(result);
+ if (strncasecmp(buf, "type", 4) != 0 || !isdigit((unsigned char) buf[4])) goto done;
+ errno = 0;
+ result = strtoul(buf + 4, &endptr, 10);
+ if (errno == 0 && *endptr == '\0' && result <= 0xffffU) success = 1;
+done:
+ if (successp) *successp = success;
+ return (u_int16_t)(result);
}
diff --git a/resolv/res_debug.h b/resolv/res_debug.h
index 4341c5a..ea2744b 100644
--- a/resolv/res_debug.h
+++ b/resolv/res_debug.h
@@ -21,16 +21,22 @@
#define _RES_DEBUG_H_
#ifndef DEBUG
-# define Dprint(cond, args) /*empty*/
-# define DprintQ(cond, args, query, size) /*empty*/
-# define Aerror(statp, file, string, error, address) /*empty*/
-# define Perror(statp, file, string, error) /*empty*/
+#define Dprint(cond, args) /*empty*/
+#define DprintQ(cond, args, query, size) /*empty*/
+#define Aerror(statp, file, string, error, address) /*empty*/
+#define Perror(statp, file, string, error) /*empty*/
#else
-# define Dprint(cond, args) if (cond) {fprintf args;} else {}
-# define DprintQ(cond, args, query, size) if (cond) {\
- fprintf args;\
- res_pquery(statp, query, size, stdout);\
- } else {}
+#define Dprint(cond, args) \
+ if (cond) { \
+ fprintf args; \
+ } else { \
+ }
+#define DprintQ(cond, args, query, size) \
+ if (cond) { \
+ fprintf args; \
+ res_pquery(statp, query, size, stdout); \
+ } else { \
+ }
#endif
-#endif /* _RES_DEBUG_H_ */
+#endif /* _RES_DEBUG_H_ */
diff --git a/resolv/res_init.c b/resolv/res_init.c
index 2fb2a3d..a4167a2 100644
--- a/resolv/res_init.c
+++ b/resolv/res_init.c
@@ -80,23 +80,21 @@
#endif
#endif /* LIBC_SCCS and not lint */
-
-
-#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/time.h>
+#include <sys/types.h>
-#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
+#include <netinet/in.h>
#include <ctype.h>
+#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <netdb.h>
#ifdef ANDROID_CHANGES
#include <errno.h>
@@ -118,7 +116,7 @@
#define DEBUG
#endif
-static void res_setoptions __P((res_state, const char *, const char *));
+static void res_setoptions __P((res_state, const char*, const char*) );
#ifdef RESOLVSORT
static const char sort_mask[] = "/&";
@@ -126,8 +124,8 @@
static uint32_t net_mask(struct in_addr);
#endif
-#if !defined(isascii) /* XXX - could be a function */
-# define isascii(c) (!(c & 0200))
+#if !defined(isascii) /* XXX - could be a function */
+#define isascii(c) (!(c & 0200))
#endif
/*
@@ -155,465 +153,415 @@
*
* Return 0 if completes successfully, -1 on error
*/
-int
-res_ninit(res_state statp) {
- extern int __res_vinit(res_state, int);
+int res_ninit(res_state statp) {
+ extern int __res_vinit(res_state, int);
- return (__res_vinit(statp, 0));
+ return (__res_vinit(statp, 0));
}
/* This function has to be reachable by res_data.c but not publicly. */
-int
-__res_vinit(res_state statp, int preinit) {
+int __res_vinit(res_state statp, int preinit) {
#if !defined(__BIONIC__)
- register FILE *fp;
+ register FILE* fp;
#endif
- register char *cp, **pp;
+ register char *cp, **pp;
#if !defined(__BIONIC__)
- register int n;
+ register int n;
#endif
- char buf[BUFSIZ];
- int nserv = 0; /* number of nameserver records read from file */
+ char buf[BUFSIZ];
+ int nserv = 0; /* number of nameserver records read from file */
#if !defined(__BIONIC__)
- int haveenv = 0;
+ int haveenv = 0;
#endif
- int havesearch = 0;
+ int havesearch = 0;
#ifdef RESOLVSORT
- int nsort = 0;
+ int nsort = 0;
#endif
#if !defined(__BIONIC__)
- char *net;
+ char* net;
#endif
- int dots;
- union res_sockaddr_union u[2];
+ int dots;
+ union res_sockaddr_union u[2];
- if ((statp->options & RES_INIT) != 0U)
- res_ndestroy(statp);
+ if ((statp->options & RES_INIT) != 0U) res_ndestroy(statp);
- if (!preinit) {
- statp->netid = NETID_UNSET;
- statp->retrans = RES_TIMEOUT;
- statp->retry = RES_DFLRETRY;
- statp->options = RES_DEFAULT;
- statp->id = res_randomid();
- statp->_mark = MARK_UNSET;
- }
+ if (!preinit) {
+ statp->netid = NETID_UNSET;
+ statp->retrans = RES_TIMEOUT;
+ statp->retry = RES_DFLRETRY;
+ statp->options = RES_DEFAULT;
+ statp->id = res_randomid();
+ statp->_mark = MARK_UNSET;
+ }
- memset(u, 0, sizeof(u));
+ memset(u, 0, sizeof(u));
#ifdef USELOOPBACK
- u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
+ u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
#else
- u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
+ u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
#endif
- u[nserv].sin.sin_family = AF_INET;
- u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
+ u[nserv].sin.sin_family = AF_INET;
+ u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
#ifdef HAVE_SA_LEN
- u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
+ u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
#endif
- nserv++;
+ nserv++;
#ifdef HAS_INET6_STRUCTS
#ifdef USELOOPBACK
- u[nserv].sin6.sin6_addr = in6addr_loopback;
+ u[nserv].sin6.sin6_addr = in6addr_loopback;
#else
- u[nserv].sin6.sin6_addr = in6addr_any;
+ u[nserv].sin6.sin6_addr = in6addr_any;
#endif
- u[nserv].sin6.sin6_family = AF_INET6;
- u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
+ u[nserv].sin6.sin6_family = AF_INET6;
+ u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
#ifdef HAVE_SA_LEN
- u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
+ u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
#endif
- nserv++;
+ nserv++;
#endif
- statp->nscount = 0;
- statp->ndots = 1;
- statp->pfcode = 0;
- statp->_vcsock = -1;
- statp->_flags = 0;
- statp->qhook = NULL;
- statp->rhook = NULL;
- statp->_u._ext.nscount = 0;
- statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
- if (statp->_u._ext.ext != NULL) {
- memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
- statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
- strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
- strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
- }
- statp->nsort = 0;
- res_setservers(statp, u, nserv);
+ statp->nscount = 0;
+ statp->ndots = 1;
+ statp->pfcode = 0;
+ statp->_vcsock = -1;
+ statp->_flags = 0;
+ statp->qhook = NULL;
+ statp->rhook = NULL;
+ statp->_u._ext.nscount = 0;
+ statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
+ if (statp->_u._ext.ext != NULL) {
+ memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
+ statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
+ strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
+ strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
+ }
+ statp->nsort = 0;
+ res_setservers(statp, u, nserv);
#if defined(__BIONIC__)
- /* Ignore the environment. */
+ /* Ignore the environment. */
#else
- /* Allow user to override the local domain definition */
- if ((cp = getenv("LOCALDOMAIN")) != NULL) {
- (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
- statp->defdname[sizeof(statp->defdname) - 1] = '\0';
- haveenv++;
+ /* Allow user to override the local domain definition */
+ if ((cp = getenv("LOCALDOMAIN")) != NULL) {
+ (void) strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
+ statp->defdname[sizeof(statp->defdname) - 1] = '\0';
+ haveenv++;
- /*
- * Set search list to be blank-separated strings
- * from rest of env value. Permits users of LOCALDOMAIN
- * to still have a search list, and anyone to set the
- * one that they want to use as an individual (even more
- * important now that the rfc1535 stuff restricts searches)
- */
- cp = statp->defdname;
- pp = statp->dnsrch;
- *pp++ = cp;
- for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
- if (*cp == '\n') /* silly backwards compat */
- break;
- else if (*cp == ' ' || *cp == '\t') {
- *cp = 0;
- n = 1;
- } else if (n) {
- *pp++ = cp;
- n = 0;
- havesearch = 1;
- }
- }
- /* null terminate last domain if there are excess */
- while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
- cp++;
- *cp = '\0';
- *pp++ = 0;
- }
- if (nserv > 0)
- statp->nscount = nserv;
+ /*
+ * Set search list to be blank-separated strings
+ * from rest of env value. Permits users of LOCALDOMAIN
+ * to still have a search list, and anyone to set the
+ * one that they want to use as an individual (even more
+ * important now that the rfc1535 stuff restricts searches)
+ */
+ cp = statp->defdname;
+ pp = statp->dnsrch;
+ *pp++ = cp;
+ for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
+ if (*cp == '\n') /* silly backwards compat */
+ break;
+ else if (*cp == ' ' || *cp == '\t') {
+ *cp = 0;
+ n = 1;
+ } else if (n) {
+ *pp++ = cp;
+ n = 0;
+ havesearch = 1;
+ }
+ }
+ /* null terminate last domain if there are excess */
+ while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') cp++;
+ *cp = '\0';
+ *pp++ = 0;
+ }
+ if (nserv > 0) statp->nscount = nserv;
#endif
#ifndef ANDROID_CHANGES /* !ANDROID_CHANGES - IGNORE resolv.conf in Android */
-#define MATCH(line, name) \
- (!strncmp(line, name, sizeof(name) - 1) && \
- (line[sizeof(name) - 1] == ' ' || \
- line[sizeof(name) - 1] == '\t'))
+#define MATCH(line, name) \
+ (!strncmp(line, name, sizeof(name) - 1) && \
+ (line[sizeof(name) - 1] == ' ' || line[sizeof(name) - 1] == '\t'))
- nserv = 0;
- if ((fp = fopen(_PATH_RESCONF, "re")) != NULL) {
- /* read the config file */
- while (fgets(buf, sizeof(buf), fp) != NULL) {
- /* skip comments */
- if (*buf == ';' || *buf == '#')
- continue;
- /* read default domain name */
- if (MATCH(buf, "domain")) {
- if (haveenv) /* skip if have from environ */
- continue;
- cp = buf + sizeof("domain") - 1;
- while (*cp == ' ' || *cp == '\t')
- cp++;
- if ((*cp == '\0') || (*cp == '\n'))
- continue;
- strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
- statp->defdname[sizeof(statp->defdname) - 1] = '\0';
- if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
- *cp = '\0';
- havesearch = 0;
- continue;
- }
- /* set search list */
- if (MATCH(buf, "search")) {
- if (haveenv) /* skip if have from environ */
- continue;
- cp = buf + sizeof("search") - 1;
- while (*cp == ' ' || *cp == '\t')
- cp++;
- if ((*cp == '\0') || (*cp == '\n'))
- continue;
- strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
- statp->defdname[sizeof(statp->defdname) - 1] = '\0';
- if ((cp = strchr(statp->defdname, '\n')) != NULL)
- *cp = '\0';
- /*
- * Set search list to be blank-separated strings
- * on rest of line.
- */
- cp = statp->defdname;
- pp = statp->dnsrch;
- *pp++ = cp;
- for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
- if (*cp == ' ' || *cp == '\t') {
- *cp = 0;
- n = 1;
- } else if (n) {
- *pp++ = cp;
- n = 0;
- }
- }
- /* null terminate last domain if there are excess */
- while (*cp != '\0' && *cp != ' ' && *cp != '\t')
- cp++;
- *cp = '\0';
- *pp++ = 0;
- havesearch = 1;
- continue;
- }
- /* read nameservers to query */
- if (MATCH(buf, "nameserver") && nserv < MAXNS) {
- struct addrinfo hints, *ai;
- char sbuf[NI_MAXSERV];
- const size_t minsiz =
- sizeof(statp->_u._ext.ext->nsaddrs[0]);
+ nserv = 0;
+ if ((fp = fopen(_PATH_RESCONF, "re")) != NULL) {
+ /* read the config file */
+ while (fgets(buf, sizeof(buf), fp) != NULL) {
+ /* skip comments */
+ if (*buf == ';' || *buf == '#') continue;
+ /* read default domain name */
+ if (MATCH(buf, "domain")) {
+ if (haveenv) /* skip if have from environ */
+ continue;
+ cp = buf + sizeof("domain") - 1;
+ while (*cp == ' ' || *cp == '\t') cp++;
+ if ((*cp == '\0') || (*cp == '\n')) continue;
+ strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
+ statp->defdname[sizeof(statp->defdname) - 1] = '\0';
+ if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL) *cp = '\0';
+ havesearch = 0;
+ continue;
+ }
+ /* set search list */
+ if (MATCH(buf, "search")) {
+ if (haveenv) /* skip if have from environ */
+ continue;
+ cp = buf + sizeof("search") - 1;
+ while (*cp == ' ' || *cp == '\t') cp++;
+ if ((*cp == '\0') || (*cp == '\n')) continue;
+ strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
+ statp->defdname[sizeof(statp->defdname) - 1] = '\0';
+ if ((cp = strchr(statp->defdname, '\n')) != NULL) *cp = '\0';
+ /*
+ * Set search list to be blank-separated strings
+ * on rest of line.
+ */
+ cp = statp->defdname;
+ pp = statp->dnsrch;
+ *pp++ = cp;
+ for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
+ if (*cp == ' ' || *cp == '\t') {
+ *cp = 0;
+ n = 1;
+ } else if (n) {
+ *pp++ = cp;
+ n = 0;
+ }
+ }
+ /* null terminate last domain if there are excess */
+ while (*cp != '\0' && *cp != ' ' && *cp != '\t') cp++;
+ *cp = '\0';
+ *pp++ = 0;
+ havesearch = 1;
+ continue;
+ }
+ /* read nameservers to query */
+ if (MATCH(buf, "nameserver") && nserv < MAXNS) {
+ struct addrinfo hints, *ai;
+ char sbuf[NI_MAXSERV];
+ const size_t minsiz = sizeof(statp->_u._ext.ext->nsaddrs[0]);
- cp = buf + sizeof("nameserver") - 1;
- while (*cp == ' ' || *cp == '\t')
- cp++;
- cp[strcspn(cp, ";# \t\n")] = '\0';
- if ((*cp != '\0') && (*cp != '\n')) {
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = PF_UNSPEC;
- hints.ai_socktype = SOCK_DGRAM; /*dummy*/
- hints.ai_flags = AI_NUMERICHOST;
- sprintf(sbuf, "%u", NAMESERVER_PORT);
- if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
- ai->ai_addrlen <= minsiz) {
- if (statp->_u._ext.ext != NULL) {
- memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
- ai->ai_addr, ai->ai_addrlen);
- }
- if (ai->ai_addrlen <=
- sizeof(statp->nsaddr_list[nserv])) {
- memcpy(&statp->nsaddr_list[nserv],
- ai->ai_addr, ai->ai_addrlen);
- } else
- statp->nsaddr_list[nserv].sin_family = 0;
- freeaddrinfo(ai);
- nserv++;
- }
- }
- continue;
- }
- if (MATCH(buf, "sortlist")) {
- struct in_addr a;
+ cp = buf + sizeof("nameserver") - 1;
+ while (*cp == ' ' || *cp == '\t') cp++;
+ cp[strcspn(cp, ";# \t\n")] = '\0';
+ if ((*cp != '\0') && (*cp != '\n')) {
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_socktype = SOCK_DGRAM; /*dummy*/
+ hints.ai_flags = AI_NUMERICHOST;
+ sprintf(sbuf, "%u", NAMESERVER_PORT);
+ if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 && ai->ai_addrlen <= minsiz) {
+ if (statp->_u._ext.ext != NULL) {
+ memcpy(&statp->_u._ext.ext->nsaddrs[nserv], ai->ai_addr,
+ ai->ai_addrlen);
+ }
+ if (ai->ai_addrlen <= sizeof(statp->nsaddr_list[nserv])) {
+ memcpy(&statp->nsaddr_list[nserv], ai->ai_addr, ai->ai_addrlen);
+ } else
+ statp->nsaddr_list[nserv].sin_family = 0;
+ freeaddrinfo(ai);
+ nserv++;
+ }
+ }
+ continue;
+ }
+ if (MATCH(buf, "sortlist")) {
+ struct in_addr a;
- cp = buf + sizeof("sortlist") - 1;
- while (nsort < MAXRESOLVSORT) {
- while (*cp == ' ' || *cp == '\t')
- cp++;
- if (*cp == '\0' || *cp == '\n' || *cp == ';')
- break;
- net = cp;
- while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
- isascii(*cp) && !isspace((unsigned char)*cp))
- cp++;
- n = *cp;
- *cp = 0;
- if (inet_aton(net, &a)) {
- statp->sort_list[nsort].addr = a;
- if (ISSORTMASK(n)) {
- *cp++ = n;
- net = cp;
- while (*cp && *cp != ';' &&
- isascii(*cp) &&
- !isspace((unsigned char)*cp))
- cp++;
- n = *cp;
- *cp = 0;
- if (inet_aton(net, &a)) {
- statp->sort_list[nsort].mask = a.s_addr;
- } else {
- statp->sort_list[nsort].mask =
- net_mask(statp->sort_list[nsort].addr);
- }
- } else {
- statp->sort_list[nsort].mask =
- net_mask(statp->sort_list[nsort].addr);
- }
- nsort++;
- }
- *cp = n;
- }
- continue;
- }
- if (MATCH(buf, "options")) {
- res_setoptions(statp, buf + sizeof("options") - 1, "conf");
- continue;
- }
- }
- if (nserv > 0)
- statp->nscount = nserv;
- statp->nsort = nsort;
- (void) fclose(fp);
- }
+ cp = buf + sizeof("sortlist") - 1;
+ while (nsort < MAXRESOLVSORT) {
+ while (*cp == ' ' || *cp == '\t') cp++;
+ if (*cp == '\0' || *cp == '\n' || *cp == ';') break;
+ net = cp;
+ while (*cp && !ISSORTMASK(*cp) && *cp != ';' && isascii(*cp) &&
+ !isspace((unsigned char) *cp))
+ cp++;
+ n = *cp;
+ *cp = 0;
+ if (inet_aton(net, &a)) {
+ statp->sort_list[nsort].addr = a;
+ if (ISSORTMASK(n)) {
+ *cp++ = n;
+ net = cp;
+ while (*cp && *cp != ';' && isascii(*cp) &&
+ !isspace((unsigned char) *cp))
+ cp++;
+ n = *cp;
+ *cp = 0;
+ if (inet_aton(net, &a)) {
+ statp->sort_list[nsort].mask = a.s_addr;
+ } else {
+ statp->sort_list[nsort].mask =
+ net_mask(statp->sort_list[nsort].addr);
+ }
+ } else {
+ statp->sort_list[nsort].mask = net_mask(statp->sort_list[nsort].addr);
+ }
+ nsort++;
+ }
+ *cp = n;
+ }
+ continue;
+ }
+ if (MATCH(buf, "options")) {
+ res_setoptions(statp, buf + sizeof("options") - 1, "conf");
+ continue;
+ }
+ }
+ if (nserv > 0) statp->nscount = nserv;
+ statp->nsort = nsort;
+ (void) fclose(fp);
+ }
#endif /* !ANDROID_CHANGES */
/*
* Last chance to get a nameserver. This should not normally
* be necessary
*/
#ifdef NO_RESOLV_CONF
- if(nserv == 0)
- nserv = get_nameservers(statp);
+ if (nserv == 0) nserv = get_nameservers(statp);
#endif
- if (statp->defdname[0] == 0 &&
- gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
- (cp = strchr(buf, '.')) != NULL)
- strcpy(statp->defdname, cp + 1);
+ if (statp->defdname[0] == 0 && gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
+ (cp = strchr(buf, '.')) != NULL)
+ strcpy(statp->defdname, cp + 1);
- /* find components of local domain that might be searched */
- if (havesearch == 0) {
- pp = statp->dnsrch;
- *pp++ = statp->defdname;
- *pp = NULL;
+ /* find components of local domain that might be searched */
+ if (havesearch == 0) {
+ pp = statp->dnsrch;
+ *pp++ = statp->defdname;
+ *pp = NULL;
- dots = 0;
- for (cp = statp->defdname; *cp; cp++)
- dots += (*cp == '.');
+ dots = 0;
+ for (cp = statp->defdname; *cp; cp++) dots += (*cp == '.');
- cp = statp->defdname;
- while (pp < statp->dnsrch + MAXDFLSRCH) {
- if (dots < LOCALDOMAINPARTS)
- break;
- cp = strchr(cp, '.') + 1; /* we know there is one */
- *pp++ = cp;
- dots--;
- }
- *pp = NULL;
+ cp = statp->defdname;
+ while (pp < statp->dnsrch + MAXDFLSRCH) {
+ if (dots < LOCALDOMAINPARTS) break;
+ cp = strchr(cp, '.') + 1; /* we know there is one */
+ *pp++ = cp;
+ dots--;
+ }
+ *pp = NULL;
#ifdef DEBUG
- if (statp->options & RES_DEBUG) {
- printf(";; res_init()... default dnsrch list:\n");
- for (pp = statp->dnsrch; *pp; pp++)
- printf(";;\t%s\n", *pp);
- printf(";;\t..END..\n");
- }
+ if (statp->options & RES_DEBUG) {
+ printf(";; res_init()... default dnsrch list:\n");
+ for (pp = statp->dnsrch; *pp; pp++) printf(";;\t%s\n", *pp);
+ printf(";;\t..END..\n");
+ }
#endif
- }
+ }
- if ((cp = getenv("RES_OPTIONS")) != NULL)
- res_setoptions(statp, cp, "env");
- if (nserv > 0) {
- statp->nscount = nserv;
- statp->options |= RES_INIT;
- }
- return (0);
+ if ((cp = getenv("RES_OPTIONS")) != NULL) res_setoptions(statp, cp, "env");
+ if (nserv > 0) {
+ statp->nscount = nserv;
+ statp->options |= RES_INIT;
+ }
+ return (0);
}
-static void
-res_setoptions(res_state statp, const char *options, const char *source)
-{
- const char *cp = options;
- int i;
- struct __res_state_ext *ext = statp->_u._ext.ext;
+static void res_setoptions(res_state statp, const char* options, const char* source) {
+ const char* cp = options;
+ int i;
+ struct __res_state_ext* ext = statp->_u._ext.ext;
#ifdef DEBUG
- if (statp->options & RES_DEBUG)
- printf(";; res_setoptions(\"%s\", \"%s\")...\n",
- options, source);
+ if (statp->options & RES_DEBUG)
+ printf(";; res_setoptions(\"%s\", \"%s\")...\n", options, source);
#endif
- while (*cp) {
- /* skip leading and inner runs of spaces */
- while (*cp == ' ' || *cp == '\t')
- cp++;
- /* search for and process individual options */
- if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
- i = atoi(cp + sizeof("ndots:") - 1);
- if (i <= RES_MAXNDOTS)
- statp->ndots = i;
- else
- statp->ndots = RES_MAXNDOTS;
+ while (*cp) {
+ /* skip leading and inner runs of spaces */
+ while (*cp == ' ' || *cp == '\t') cp++;
+ /* search for and process individual options */
+ if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
+ i = atoi(cp + sizeof("ndots:") - 1);
+ if (i <= RES_MAXNDOTS)
+ statp->ndots = i;
+ else
+ statp->ndots = RES_MAXNDOTS;
#ifdef DEBUG
- if (statp->options & RES_DEBUG)
- printf(";;\tndots=%d\n", statp->ndots);
+ if (statp->options & RES_DEBUG) printf(";;\tndots=%d\n", statp->ndots);
#endif
- } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
- i = atoi(cp + sizeof("timeout:") - 1);
- if (i <= RES_MAXRETRANS)
- statp->retrans = i;
- else
- statp->retrans = RES_MAXRETRANS;
+ } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
+ i = atoi(cp + sizeof("timeout:") - 1);
+ if (i <= RES_MAXRETRANS)
+ statp->retrans = i;
+ else
+ statp->retrans = RES_MAXRETRANS;
#ifdef DEBUG
- if (statp->options & RES_DEBUG)
- printf(";;\ttimeout=%d\n", statp->retrans);
+ if (statp->options & RES_DEBUG) printf(";;\ttimeout=%d\n", statp->retrans);
#endif
- } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
- i = atoi(cp + sizeof("attempts:") - 1);
- if (i <= RES_MAXRETRY)
- statp->retry = i;
- else
- statp->retry = RES_MAXRETRY;
+ } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)) {
+ i = atoi(cp + sizeof("attempts:") - 1);
+ if (i <= RES_MAXRETRY)
+ statp->retry = i;
+ else
+ statp->retry = RES_MAXRETRY;
#ifdef DEBUG
- if (statp->options & RES_DEBUG)
- printf(";;\tattempts=%d\n", statp->retry);
+ if (statp->options & RES_DEBUG) printf(";;\tattempts=%d\n", statp->retry);
#endif
- } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
+ } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
#ifdef DEBUG
- if (!(statp->options & RES_DEBUG)) {
- printf(";; res_setoptions(\"%s\", \"%s\")..\n",
- options, source);
- statp->options |= RES_DEBUG;
- }
- printf(";;\tdebug\n");
+ if (!(statp->options & RES_DEBUG)) {
+ printf(";; res_setoptions(\"%s\", \"%s\")..\n", options, source);
+ statp->options |= RES_DEBUG;
+ }
+ printf(";;\tdebug\n");
#endif
- } else if (!strncmp(cp, "no_tld_query",
- sizeof("no_tld_query") - 1) ||
- !strncmp(cp, "no-tld-query",
- sizeof("no-tld-query") - 1)) {
- statp->options |= RES_NOTLDQUERY;
- } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
- statp->options |= RES_USE_INET6;
- } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
- statp->options |= RES_ROTATE;
- } else if (!strncmp(cp, "no-check-names",
- sizeof("no-check-names") - 1)) {
- statp->options |= RES_NOCHECKNAME;
- }
+ } else if (!strncmp(cp, "no_tld_query", sizeof("no_tld_query") - 1) ||
+ !strncmp(cp, "no-tld-query", sizeof("no-tld-query") - 1)) {
+ statp->options |= RES_NOTLDQUERY;
+ } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
+ statp->options |= RES_USE_INET6;
+ } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
+ statp->options |= RES_ROTATE;
+ } else if (!strncmp(cp, "no-check-names", sizeof("no-check-names") - 1)) {
+ statp->options |= RES_NOCHECKNAME;
+ }
#ifdef RES_USE_EDNS0
- else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
- statp->options |= RES_USE_EDNS0;
- }
+ else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
+ statp->options |= RES_USE_EDNS0;
+ }
#endif
- else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
- statp->options |= RES_USE_DNAME;
- }
- else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
- if (ext == NULL)
- goto skip;
- cp += sizeof("nibble:") - 1;
- i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
- strncpy(ext->nsuffix, cp, (size_t)i);
- ext->nsuffix[i] = '\0';
- }
- else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
- if (ext == NULL)
- goto skip;
- cp += sizeof("nibble2:") - 1;
- i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
- strncpy(ext->nsuffix2, cp, (size_t)i);
- ext->nsuffix2[i] = '\0';
- }
- else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
- cp += sizeof("v6revmode:") - 1;
- /* "nibble" and "bitstring" used to be valid */
- if (!strncmp(cp, "single", sizeof("single") - 1)) {
- statp->options |= RES_NO_NIBBLE2;
- } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
- statp->options &=
- ~RES_NO_NIBBLE2;
- }
- }
- else {
- /* XXX - print a warning here? */
- }
- skip:
- /* skip to next run of spaces */
- while (*cp && *cp != ' ' && *cp != '\t')
- cp++;
- }
+ else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
+ statp->options |= RES_USE_DNAME;
+ } else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
+ if (ext == NULL) goto skip;
+ cp += sizeof("nibble:") - 1;
+ i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
+ strncpy(ext->nsuffix, cp, (size_t) i);
+ ext->nsuffix[i] = '\0';
+ } else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
+ if (ext == NULL) goto skip;
+ cp += sizeof("nibble2:") - 1;
+ i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
+ strncpy(ext->nsuffix2, cp, (size_t) i);
+ ext->nsuffix2[i] = '\0';
+ } else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
+ cp += sizeof("v6revmode:") - 1;
+ /* "nibble" and "bitstring" used to be valid */
+ if (!strncmp(cp, "single", sizeof("single") - 1)) {
+ statp->options |= RES_NO_NIBBLE2;
+ } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
+ statp->options &= ~RES_NO_NIBBLE2;
+ }
+ } else {
+ /* XXX - print a warning here? */
+ }
+ skip:
+ /* skip to next run of spaces */
+ while (*cp && *cp != ' ' && *cp != '\t') cp++;
+ }
}
#ifdef RESOLVSORT
/* XXX - should really support CIDR which means explicit masks always. */
-static uint32_t
-net_mask(struct in_addr in) /*!< XXX - should really use system's version of this */
+static uint32_t net_mask(struct in_addr in) /*!< XXX - should really use system's version of this */
{
- register uint32_t i = ntohl(in.s_addr);
+ register uint32_t i = ntohl(in.s_addr);
- if (IN_CLASSA(i))
- return (htonl(IN_CLASSA_NET));
- else if (IN_CLASSB(i))
- return (htonl(IN_CLASSB_NET));
- return (htonl(IN_CLASSC_NET));
+ if (IN_CLASSA(i))
+ return (htonl(IN_CLASSA_NET));
+ else if (IN_CLASSB(i))
+ return (htonl(IN_CLASSB_NET));
+ return (htonl(IN_CLASSC_NET));
}
#endif
@@ -624,160 +572,133 @@
*
* This routine is not expected to be user visible.
*/
-void
-res_nclose(res_state statp)
-{
- int ns;
+void res_nclose(res_state statp) {
+ int ns;
- if (statp->_vcsock >= 0) {
- (void) close(statp->_vcsock);
- statp->_vcsock = -1;
- statp->_flags &= ~(RES_F_VC | RES_F_CONN);
- }
- for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
- if (statp->_u._ext.nssocks[ns] != -1) {
- (void) close(statp->_u._ext.nssocks[ns]);
- statp->_u._ext.nssocks[ns] = -1;
- }
- }
+ if (statp->_vcsock >= 0) {
+ (void) close(statp->_vcsock);
+ statp->_vcsock = -1;
+ statp->_flags &= ~(RES_F_VC | RES_F_CONN);
+ }
+ for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
+ if (statp->_u._ext.nssocks[ns] != -1) {
+ (void) close(statp->_u._ext.nssocks[ns]);
+ statp->_u._ext.nssocks[ns] = -1;
+ }
+ }
}
-void
-res_ndestroy(res_state statp)
-{
- res_nclose(statp);
- if (statp->_u._ext.ext != NULL)
- free(statp->_u._ext.ext);
- statp->options &= ~RES_INIT;
- statp->_u._ext.ext = NULL;
+void res_ndestroy(res_state statp) {
+ res_nclose(statp);
+ if (statp->_u._ext.ext != NULL) free(statp->_u._ext.ext);
+ statp->options &= ~RES_INIT;
+ statp->_u._ext.ext = NULL;
}
-const char *
-res_get_nibblesuffix(res_state statp)
-{
- if (statp->_u._ext.ext)
- return (statp->_u._ext.ext->nsuffix);
- return ("ip6.arpa");
+const char* res_get_nibblesuffix(res_state statp) {
+ if (statp->_u._ext.ext) return (statp->_u._ext.ext->nsuffix);
+ return ("ip6.arpa");
}
-const char *
-res_get_nibblesuffix2(res_state statp)
-{
- if (statp->_u._ext.ext)
- return (statp->_u._ext.ext->nsuffix2);
- return ("ip6.int");
+const char* res_get_nibblesuffix2(res_state statp) {
+ if (statp->_u._ext.ext) return (statp->_u._ext.ext->nsuffix2);
+ return ("ip6.int");
}
-void
-res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt)
-{
- int i, nserv;
- size_t size;
+void res_setservers(res_state statp, const union res_sockaddr_union* set, int cnt) {
+ int i, nserv;
+ size_t size;
- /* close open servers */
- res_nclose(statp);
+ /* close open servers */
+ res_nclose(statp);
- /* cause rtt times to be forgotten */
- statp->_u._ext.nscount = 0;
+ /* cause rtt times to be forgotten */
+ statp->_u._ext.nscount = 0;
- nserv = 0;
- for (i = 0; i < cnt && nserv < MAXNS; i++) {
- switch (set->sin.sin_family) {
- case AF_INET:
- size = sizeof(set->sin);
- if (statp->_u._ext.ext)
- memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
- &set->sin, size);
- if (size <= sizeof(statp->nsaddr_list[nserv]))
- memcpy(&statp->nsaddr_list[nserv],
- &set->sin, size);
- else
- statp->nsaddr_list[nserv].sin_family = 0;
- nserv++;
- break;
+ nserv = 0;
+ for (i = 0; i < cnt && nserv < MAXNS; i++) {
+ switch (set->sin.sin_family) {
+ case AF_INET:
+ size = sizeof(set->sin);
+ if (statp->_u._ext.ext)
+ memcpy(&statp->_u._ext.ext->nsaddrs[nserv], &set->sin, size);
+ if (size <= sizeof(statp->nsaddr_list[nserv]))
+ memcpy(&statp->nsaddr_list[nserv], &set->sin, size);
+ else
+ statp->nsaddr_list[nserv].sin_family = 0;
+ nserv++;
+ break;
#ifdef HAS_INET6_STRUCTS
- case AF_INET6:
- size = sizeof(set->sin6);
- if (statp->_u._ext.ext)
- memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
- &set->sin6, size);
- if (size <= sizeof(statp->nsaddr_list[nserv]))
- memcpy(&statp->nsaddr_list[nserv],
- &set->sin6, size);
- else
- statp->nsaddr_list[nserv].sin_family = 0;
- nserv++;
- break;
+ case AF_INET6:
+ size = sizeof(set->sin6);
+ if (statp->_u._ext.ext)
+ memcpy(&statp->_u._ext.ext->nsaddrs[nserv], &set->sin6, size);
+ if (size <= sizeof(statp->nsaddr_list[nserv]))
+ memcpy(&statp->nsaddr_list[nserv], &set->sin6, size);
+ else
+ statp->nsaddr_list[nserv].sin_family = 0;
+ nserv++;
+ break;
#endif
- default:
- break;
- }
- set++;
- }
- statp->nscount = nserv;
-
+ default:
+ break;
+ }
+ set++;
+ }
+ statp->nscount = nserv;
}
-int
-res_getservers(res_state statp, union res_sockaddr_union *set, int cnt)
-{
- int i;
- size_t size;
- uint16_t family;
+int res_getservers(res_state statp, union res_sockaddr_union* set, int cnt) {
+ int i;
+ size_t size;
+ uint16_t family;
- for (i = 0; i < statp->nscount && i < cnt; i++) {
- if (statp->_u._ext.ext)
- family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
- else
- family = statp->nsaddr_list[i].sin_family;
+ for (i = 0; i < statp->nscount && i < cnt; i++) {
+ if (statp->_u._ext.ext)
+ family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
+ else
+ family = statp->nsaddr_list[i].sin_family;
- switch (family) {
- case AF_INET:
- size = sizeof(set->sin);
- if (statp->_u._ext.ext)
- memcpy(&set->sin,
- &statp->_u._ext.ext->nsaddrs[i],
- size);
- else
- memcpy(&set->sin, &statp->nsaddr_list[i],
- size);
- break;
+ switch (family) {
+ case AF_INET:
+ size = sizeof(set->sin);
+ if (statp->_u._ext.ext)
+ memcpy(&set->sin, &statp->_u._ext.ext->nsaddrs[i], size);
+ else
+ memcpy(&set->sin, &statp->nsaddr_list[i], size);
+ break;
#ifdef HAS_INET6_STRUCTS
- case AF_INET6:
- size = sizeof(set->sin6);
- if (statp->_u._ext.ext)
- memcpy(&set->sin6,
- &statp->_u._ext.ext->nsaddrs[i],
- size);
- else
- memcpy(&set->sin6, &statp->nsaddr_list[i],
- size);
- break;
+ case AF_INET6:
+ size = sizeof(set->sin6);
+ if (statp->_u._ext.ext)
+ memcpy(&set->sin6, &statp->_u._ext.ext->nsaddrs[i], size);
+ else
+ memcpy(&set->sin6, &statp->nsaddr_list[i], size);
+ break;
#endif
- default:
- set->sin.sin_family = 0;
- break;
- }
- set++;
- }
- return (statp->nscount);
+ default:
+ set->sin.sin_family = 0;
+ break;
+ }
+ set++;
+ }
+ return (statp->nscount);
}
#ifdef ANDROID_CHANGES
-void res_setnetcontext(res_state statp, const struct android_net_context *netcontext)
-{
- if (statp != NULL) {
- statp->netid = netcontext->dns_netid;
- statp->_mark = netcontext->dns_mark;
- statp->qhook = netcontext->qhook;
- if (netcontext->flags & NET_CONTEXT_FLAG_USE_EDNS) {
- statp->options |= RES_USE_EDNS0 | RES_USE_DNSSEC;
- }
- }
+void res_setnetcontext(res_state statp, const struct android_net_context* netcontext) {
+ if (statp != NULL) {
+ statp->netid = netcontext->dns_netid;
+ statp->_mark = netcontext->dns_mark;
+ statp->qhook = netcontext->qhook;
+ if (netcontext->flags & NET_CONTEXT_FLAG_USE_EDNS) {
+ statp->options |= RES_USE_EDNS0 | RES_USE_DNSSEC;
+ }
+ }
}
#endif /* ANDROID_CHANGES */
diff --git a/resolv/res_mkquery.c b/resolv/res_mkquery.c
index 1b4c4af..7a2a3f7 100644
--- a/resolv/res_mkquery.c
+++ b/resolv/res_mkquery.c
@@ -80,13 +80,11 @@
#endif
#endif /* LIBC_SCCS and not lint */
-
-
-#include <sys/types.h>
-#include <sys/param.h>
-#include <netinet/in.h>
#include <arpa/nameser.h>
#include <netdb.h>
+#include <netinet/in.h>
+#include <sys/param.h>
+#include <sys/types.h>
#ifdef ANDROID_CHANGES
#include "resolv_private.h"
#else
@@ -101,198 +99,181 @@
#endif
#ifndef lint
-#define UNUSED(a) (void)&a
+#define UNUSED(a) (void) &a
#else
-#define UNUSED(a) a = a
+#define UNUSED(a) a = a
#endif
-extern const char *_res_opcodes[];
+extern const char* _res_opcodes[];
/*
* Form all types of queries.
* Returns the size of the result or -1.
*/
-int
-res_nmkquery(res_state statp,
- int op, /* opcode of query */
- const char *dname, /* domain name */
- int class, int type, /* class and type of query */
- const u_char *data, /* resource record data */
- int datalen, /* length of data */
- const u_char *newrr_in, /* new rr for modify or append */
- u_char *buf, /* buffer to put query */
- int buflen) /* size of buffer */
+int res_nmkquery(res_state statp, int op, /* opcode of query */
+ const char* dname, /* domain name */
+ int class, int type, /* class and type of query */
+ const u_char* data, /* resource record data */
+ int datalen, /* length of data */
+ const u_char* newrr_in, /* new rr for modify or append */
+ u_char* buf, /* buffer to put query */
+ int buflen) /* size of buffer */
{
- register HEADER *hp;
- register u_char *cp, *ep;
- register int n;
- u_char *dnptrs[20], **dpp, **lastdnptr;
+ register HEADER* hp;
+ register u_char *cp, *ep;
+ register int n;
+ u_char *dnptrs[20], **dpp, **lastdnptr;
- UNUSED(newrr_in);
+ UNUSED(newrr_in);
#ifdef DEBUG
- if (statp->options & RES_DEBUG)
- printf(";; res_nmkquery(%s, %s, %s, %s)\n",
- _res_opcodes[op], dname, p_class(class), p_type(type));
+ if (statp->options & RES_DEBUG)
+ printf(";; res_nmkquery(%s, %s, %s, %s)\n", _res_opcodes[op], dname, p_class(class),
+ p_type(type));
#endif
- /*
- * Initialize header fields.
- */
- if ((buf == NULL) || (buflen < HFIXEDSZ))
- return (-1);
- memset(buf, 0, HFIXEDSZ);
- hp = (HEADER *)(void *)buf;
- hp->id = htons(res_randomid());
- hp->opcode = op;
- hp->rd = (statp->options & RES_RECURSE) != 0U;
- hp->ad = (statp->options & RES_USE_DNSSEC) != 0U;
- hp->rcode = NOERROR;
- cp = buf + HFIXEDSZ;
- ep = buf + buflen;
- dpp = dnptrs;
- *dpp++ = buf;
- *dpp++ = NULL;
- lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
- /*
- * perform opcode specific processing
- */
- switch (op) {
- case QUERY: /*FALLTHROUGH*/
- case NS_NOTIFY_OP:
- if (ep - cp < QFIXEDSZ)
- return (-1);
- if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs,
- lastdnptr)) < 0)
- return (-1);
- cp += n;
- ns_put16(type, cp);
- cp += INT16SZ;
- ns_put16(class, cp);
- cp += INT16SZ;
- hp->qdcount = htons(1);
- if (op == QUERY || data == NULL)
- break;
- /*
- * Make an additional record for completion domain.
- */
- if ((ep - cp) < RRFIXEDSZ)
- return (-1);
- n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ,
- dnptrs, lastdnptr);
- if (n < 0)
- return (-1);
- cp += n;
- ns_put16(T_NULL, cp);
- cp += INT16SZ;
- ns_put16(class, cp);
- cp += INT16SZ;
- ns_put32(0, cp);
- cp += INT32SZ;
- ns_put16(0, cp);
- cp += INT16SZ;
- hp->arcount = htons(1);
- break;
+ /*
+ * Initialize header fields.
+ */
+ if ((buf == NULL) || (buflen < HFIXEDSZ)) return (-1);
+ memset(buf, 0, HFIXEDSZ);
+ hp = (HEADER*) (void*) buf;
+ hp->id = htons(res_randomid());
+ hp->opcode = op;
+ hp->rd = (statp->options & RES_RECURSE) != 0U;
+ hp->ad = (statp->options & RES_USE_DNSSEC) != 0U;
+ hp->rcode = NOERROR;
+ cp = buf + HFIXEDSZ;
+ ep = buf + buflen;
+ dpp = dnptrs;
+ *dpp++ = buf;
+ *dpp++ = NULL;
+ lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
+ /*
+ * perform opcode specific processing
+ */
+ switch (op) {
+ case QUERY: /*FALLTHROUGH*/
+ case NS_NOTIFY_OP:
+ if (ep - cp < QFIXEDSZ) return (-1);
+ if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, lastdnptr)) < 0) return (-1);
+ cp += n;
+ ns_put16(type, cp);
+ cp += INT16SZ;
+ ns_put16(class, cp);
+ cp += INT16SZ;
+ hp->qdcount = htons(1);
+ if (op == QUERY || data == NULL) break;
+ /*
+ * Make an additional record for completion domain.
+ */
+ if ((ep - cp) < RRFIXEDSZ) return (-1);
+ n = dn_comp((const char*) data, cp, ep - cp - RRFIXEDSZ, dnptrs, lastdnptr);
+ if (n < 0) return (-1);
+ cp += n;
+ ns_put16(T_NULL, cp);
+ cp += INT16SZ;
+ ns_put16(class, cp);
+ cp += INT16SZ;
+ ns_put32(0, cp);
+ cp += INT32SZ;
+ ns_put16(0, cp);
+ cp += INT16SZ;
+ hp->arcount = htons(1);
+ break;
- case IQUERY:
- /*
- * Initialize answer section
- */
- if (ep - cp < 1 + RRFIXEDSZ + datalen)
- return (-1);
- *cp++ = '\0'; /* no domain name */
- ns_put16(type, cp);
- cp += INT16SZ;
- ns_put16(class, cp);
- cp += INT16SZ;
- ns_put32(0, cp);
- cp += INT32SZ;
- ns_put16(datalen, cp);
- cp += INT16SZ;
- if (datalen) {
- memcpy(cp, data, (size_t)datalen);
- cp += datalen;
- }
- hp->ancount = htons(1);
- break;
+ case IQUERY:
+ /*
+ * Initialize answer section
+ */
+ if (ep - cp < 1 + RRFIXEDSZ + datalen) return (-1);
+ *cp++ = '\0'; /* no domain name */
+ ns_put16(type, cp);
+ cp += INT16SZ;
+ ns_put16(class, cp);
+ cp += INT16SZ;
+ ns_put32(0, cp);
+ cp += INT32SZ;
+ ns_put16(datalen, cp);
+ cp += INT16SZ;
+ if (datalen) {
+ memcpy(cp, data, (size_t) datalen);
+ cp += datalen;
+ }
+ hp->ancount = htons(1);
+ break;
- default:
- return (-1);
- }
- return (cp - buf);
+ default:
+ return (-1);
+ }
+ return (cp - buf);
}
#ifdef RES_USE_EDNS0
/* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
#ifndef T_OPT
-#define T_OPT 41
+#define T_OPT 41
#endif
-int
-res_nopt(res_state statp,
- int n0, /* current offset in buffer */
- u_char *buf, /* buffer to put query */
- int buflen, /* size of buffer */
- int anslen) /* UDP answer buffer size */
+int res_nopt(res_state statp, int n0, /* current offset in buffer */
+ u_char* buf, /* buffer to put query */
+ int buflen, /* size of buffer */
+ int anslen) /* UDP answer buffer size */
{
- register HEADER *hp;
- register u_char *cp, *ep;
- u_int16_t flags = 0;
+ register HEADER* hp;
+ register u_char *cp, *ep;
+ u_int16_t flags = 0;
#ifdef DEBUG
- if ((statp->options & RES_DEBUG) != 0U)
- printf(";; res_nopt()\n");
+ if ((statp->options & RES_DEBUG) != 0U) printf(";; res_nopt()\n");
#endif
- hp = (HEADER *)(void *)buf;
- cp = buf + n0;
- ep = buf + buflen;
+ hp = (HEADER*) (void*) buf;
+ cp = buf + n0;
+ ep = buf + buflen;
- if ((ep - cp) < 1 + RRFIXEDSZ)
- return (-1);
+ if ((ep - cp) < 1 + RRFIXEDSZ) return (-1);
- *cp++ = 0; /* "." */
+ *cp++ = 0; /* "." */
- ns_put16(T_OPT, cp); /* TYPE */
- cp += INT16SZ;
- if (anslen > 0xffff)
- anslen = 0xffff;
- ns_put16(anslen, cp); /* CLASS = UDP payload size */
- cp += INT16SZ;
- *cp++ = NOERROR; /* extended RCODE */
- *cp++ = 0; /* EDNS version */
- if (statp->options & RES_USE_DNSSEC) {
+ ns_put16(T_OPT, cp); /* TYPE */
+ cp += INT16SZ;
+ if (anslen > 0xffff) anslen = 0xffff;
+ ns_put16(anslen, cp); /* CLASS = UDP payload size */
+ cp += INT16SZ;
+ *cp++ = NOERROR; /* extended RCODE */
+ *cp++ = 0; /* EDNS version */
+ if (statp->options & RES_USE_DNSSEC) {
#ifdef DEBUG
- if (statp->options & RES_DEBUG)
- printf(";; res_opt()... ENDS0 DNSSEC\n");
+ if (statp->options & RES_DEBUG) printf(";; res_opt()... ENDS0 DNSSEC\n");
#endif
- flags |= NS_OPT_DNSSEC_OK;
- }
- ns_put16(flags, cp);
- cp += INT16SZ;
+ flags |= NS_OPT_DNSSEC_OK;
+ }
+ ns_put16(flags, cp);
+ cp += INT16SZ;
#ifdef EDNS0_PADDING
- {
- u_int16_t minlen = (cp - buf) + 3 * INT16SZ;
- u_int16_t extra = minlen % EDNS0_PADDING;
- u_int16_t padlen = (EDNS0_PADDING - extra) % EDNS0_PADDING;
- if (minlen > buflen) {
- return (-1);
- }
- padlen = MIN(padlen, buflen - minlen);
- ns_put16(padlen + 2 * INT16SZ, cp); /* RDLEN */
- cp += INT16SZ;
- ns_put16(NS_OPT_PADDING, cp); /* OPTION-CODE */
- cp += INT16SZ;
- ns_put16(padlen, cp); /* OPTION-LENGTH */
- cp += INT16SZ;
- memset(cp, 0, padlen);
- cp += padlen;
- }
+ {
+ u_int16_t minlen = (cp - buf) + 3 * INT16SZ;
+ u_int16_t extra = minlen % EDNS0_PADDING;
+ u_int16_t padlen = (EDNS0_PADDING - extra) % EDNS0_PADDING;
+ if (minlen > buflen) {
+ return (-1);
+ }
+ padlen = MIN(padlen, buflen - minlen);
+ ns_put16(padlen + 2 * INT16SZ, cp); /* RDLEN */
+ cp += INT16SZ;
+ ns_put16(NS_OPT_PADDING, cp); /* OPTION-CODE */
+ cp += INT16SZ;
+ ns_put16(padlen, cp); /* OPTION-LENGTH */
+ cp += INT16SZ;
+ memset(cp, 0, padlen);
+ cp += padlen;
+ }
#else
- ns_put16(0, cp); /* RDLEN */
- cp += INT16SZ;
+ ns_put16(0, cp); /* RDLEN */
+ cp += INT16SZ;
#endif
- hp->arcount = htons(ntohs(hp->arcount) + 1);
+ hp->arcount = htons(ntohs(hp->arcount) + 1);
- return (cp - buf);
+ return (cp - buf);
}
#endif
diff --git a/resolv/res_private.h b/resolv/res_private.h
index 8341913..ec73cba 100644
--- a/resolv/res_private.h
+++ b/resolv/res_private.h
@@ -4,19 +4,18 @@
#define res_private_h
struct __res_state_ext {
- union res_sockaddr_union nsaddrs[MAXNS];
- struct sort_list {
- int af;
- union {
- struct in_addr ina;
- struct in6_addr in6a;
- } addr, mask;
- } sort_list[MAXRESOLVSORT];
- char nsuffix[64];
- char nsuffix2[64];
+ union res_sockaddr_union nsaddrs[MAXNS];
+ struct sort_list {
+ int af;
+ union {
+ struct in_addr ina;
+ struct in6_addr in6a;
+ } addr, mask;
+ } sort_list[MAXRESOLVSORT];
+ char nsuffix[64];
+ char nsuffix2[64];
};
-extern int
-res_ourserver_p(const res_state statp, const struct sockaddr *sa);
+extern int res_ourserver_p(const res_state statp, const struct sockaddr* sa);
#endif
diff --git a/resolv/res_query.c b/resolv/res_query.c
index 09be8b4..e205485 100644
--- a/resolv/res_query.c
+++ b/resolv/res_query.c
@@ -80,16 +80,14 @@
#endif
#endif /* LIBC_SCCS and not lint */
-
-
-#include <sys/types.h>
-#include <sys/param.h>
-#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
+#include <netinet/in.h>
+#include <sys/param.h>
+#include <sys/types.h>
#ifdef ANDROID_CHANGES
#include "resolv_cache.h"
#include "resolv_private.h"
@@ -98,8 +96,8 @@
#endif
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#include <string.h>
+#include <unistd.h>
#define DISABLE_HOST_ALIAS 1
@@ -109,9 +107,9 @@
#endif
#if PACKETSZ > 1024
-#define MAXPACKET PACKETSZ
+#define MAXPACKET PACKETSZ
#else
-#define MAXPACKET 1024
+#define MAXPACKET 1024
#endif
/*
@@ -124,92 +122,82 @@
*
* Caller must parse answer and determine whether it answers the question.
*/
-int
-res_nquery(res_state statp,
- const char *name, /* domain name */
- int class, int type, /* class and type of query */
- u_char *answer, /* buffer to put answer */
- int anslen) /* size of answer buffer */
+int res_nquery(res_state statp, const char* name, /* domain name */
+ int class, int type, /* class and type of query */
+ u_char* answer, /* buffer to put answer */
+ int anslen) /* size of answer buffer */
{
- u_char buf[MAXPACKET];
- HEADER *hp = (HEADER *)(void *)answer;
- int n;
- u_int oflags;
+ u_char buf[MAXPACKET];
+ HEADER* hp = (HEADER*) (void*) answer;
+ int n;
+ u_int oflags;
- oflags = statp->_flags;
+ oflags = statp->_flags;
again:
- hp->rcode = NOERROR; /* default */
+ hp->rcode = NOERROR; /* default */
#ifdef DEBUG
- if (statp->options & RES_DEBUG)
- printf(";; res_query(%s, %d, %d)\n", name, class, type);
+ if (statp->options & RES_DEBUG) printf(";; res_query(%s, %d, %d)\n", name, class, type);
#endif
- n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
- buf, sizeof(buf));
+ n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL, buf, sizeof(buf));
#ifdef RES_USE_EDNS0
- if (n > 0 && (statp->_flags & RES_F_EDNS0ERR) == 0 &&
- (statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0U)
- n = res_nopt(statp, n, buf, sizeof(buf), anslen);
+ if (n > 0 && (statp->_flags & RES_F_EDNS0ERR) == 0 &&
+ (statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U)
+ n = res_nopt(statp, n, buf, sizeof(buf), anslen);
#endif
- if (n <= 0) {
+ if (n <= 0) {
#ifdef DEBUG
- if (statp->options & RES_DEBUG)
- printf(";; res_query: mkquery failed\n");
+ if (statp->options & RES_DEBUG) printf(";; res_query: mkquery failed\n");
#endif
- RES_SET_H_ERRNO(statp, NO_RECOVERY);
- return (n);
- }
- n = res_nsend(statp, buf, n, answer, anslen);
- if (n < 0) {
+ RES_SET_H_ERRNO(statp, NO_RECOVERY);
+ return (n);
+ }
+ n = res_nsend(statp, buf, n, answer, anslen);
+ if (n < 0) {
#ifdef RES_USE_EDNS0
- /* if the query choked with EDNS0, retry without EDNS0 */
- if ((statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0U &&
- ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
- statp->_flags |= RES_F_EDNS0ERR;
- if (statp->options & RES_DEBUG)
- printf(";; res_nquery: retry without EDNS0\n");
- goto again;
- }
+ /* if the query choked with EDNS0, retry without EDNS0 */
+ if ((statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U &&
+ ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
+ statp->_flags |= RES_F_EDNS0ERR;
+ if (statp->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
+ goto again;
+ }
#endif
#ifdef DEBUG
- if (statp->options & RES_DEBUG)
- printf(";; res_query: send error\n");
+ if (statp->options & RES_DEBUG) printf(";; res_query: send error\n");
#endif
- RES_SET_H_ERRNO(statp, TRY_AGAIN);
- return (n);
- }
+ RES_SET_H_ERRNO(statp, TRY_AGAIN);
+ return (n);
+ }
- if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
+ if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
#ifdef DEBUG
- if (statp->options & RES_DEBUG)
- printf(";; rcode = (%s), counts = an:%d ns:%d ar:%d\n",
- p_rcode(hp->rcode),
- ntohs(hp->ancount),
- ntohs(hp->nscount),
- ntohs(hp->arcount));
+ if (statp->options & RES_DEBUG)
+ printf(";; rcode = (%s), counts = an:%d ns:%d ar:%d\n", p_rcode(hp->rcode),
+ ntohs(hp->ancount), ntohs(hp->nscount), ntohs(hp->arcount));
#endif
- switch (hp->rcode) {
- case NXDOMAIN:
- RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
- break;
- case SERVFAIL:
- RES_SET_H_ERRNO(statp, TRY_AGAIN);
- break;
- case NOERROR:
- RES_SET_H_ERRNO(statp, NO_DATA);
- break;
- case FORMERR:
- case NOTIMP:
- case REFUSED:
- default:
- RES_SET_H_ERRNO(statp, NO_RECOVERY);
- break;
- }
- return (-1);
- }
- return (n);
+ switch (hp->rcode) {
+ case NXDOMAIN:
+ RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
+ break;
+ case SERVFAIL:
+ RES_SET_H_ERRNO(statp, TRY_AGAIN);
+ break;
+ case NOERROR:
+ RES_SET_H_ERRNO(statp, NO_DATA);
+ break;
+ case FORMERR:
+ case NOTIMP:
+ case REFUSED:
+ default:
+ RES_SET_H_ERRNO(statp, NO_RECOVERY);
+ break;
+ }
+ return (-1);
+ }
+ return (n);
}
/*
@@ -218,208 +206,189 @@
* If enabled, implement search rules until answer or unrecoverable failure
* is detected. Error code, if any, is left in H_ERRNO.
*/
-int
-res_nsearch(res_state statp,
- const char *name, /* domain name */
- int class, int type, /* class and type of query */
- u_char *answer, /* buffer to put answer */
- int anslen) /* size of answer */
+int res_nsearch(res_state statp, const char* name, /* domain name */
+ int class, int type, /* class and type of query */
+ u_char* answer, /* buffer to put answer */
+ int anslen) /* size of answer */
{
- const char *cp, * const *domain;
- HEADER *hp = (HEADER *)(void *)answer;
- char tmp[NS_MAXDNAME];
- u_int dots;
- int trailing_dot, ret, saved_herrno;
- int got_nodata = 0, got_servfail = 0, root_on_list = 0;
- int tried_as_is = 0;
- int searched = 0;
+ const char *cp, *const *domain;
+ HEADER* hp = (HEADER*) (void*) answer;
+ char tmp[NS_MAXDNAME];
+ u_int dots;
+ int trailing_dot, ret, saved_herrno;
+ int got_nodata = 0, got_servfail = 0, root_on_list = 0;
+ int tried_as_is = 0;
+ int searched = 0;
- errno = 0;
- RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /* True if we never query. */
+ errno = 0;
+ RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /* True if we never query. */
- dots = 0;
- for (cp = name; *cp != '\0'; cp++)
- dots += (*cp == '.');
- trailing_dot = 0;
- if (cp > name && *--cp == '.')
- trailing_dot++;
+ dots = 0;
+ for (cp = name; *cp != '\0'; cp++) dots += (*cp == '.');
+ trailing_dot = 0;
+ if (cp > name && *--cp == '.') trailing_dot++;
- /* If there aren't any dots, it could be a user-level alias. */
- if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL)
- return (res_nquery(statp, cp, class, type, answer, anslen));
+ /* If there aren't any dots, it could be a user-level alias. */
+ if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp)) != NULL)
+ return (res_nquery(statp, cp, class, type, answer, anslen));
- /*
- * If there are enough dots in the name, let's just give it a
- * try 'as is'. The threshold can be set with the "ndots" option.
- * Also, query 'as is', if there is a trailing dot in the name.
- */
- saved_herrno = -1;
- if (dots >= statp->ndots || trailing_dot) {
- ret = res_nquerydomain(statp, name, NULL, class, type,
- answer, anslen);
- if (ret > 0 || trailing_dot)
- return (ret);
- saved_herrno = statp->res_h_errno;
- tried_as_is++;
- }
+ /*
+ * If there are enough dots in the name, let's just give it a
+ * try 'as is'. The threshold can be set with the "ndots" option.
+ * Also, query 'as is', if there is a trailing dot in the name.
+ */
+ saved_herrno = -1;
+ if (dots >= statp->ndots || trailing_dot) {
+ ret = res_nquerydomain(statp, name, NULL, class, type, answer, anslen);
+ if (ret > 0 || trailing_dot) return (ret);
+ saved_herrno = statp->res_h_errno;
+ tried_as_is++;
+ }
- /*
- * We do at least one level of search if
- * - there is no dot and RES_DEFNAME is set, or
- * - there is at least one dot, there is no trailing dot,
- * and RES_DNSRCH is set.
- */
- if ((!dots && (statp->options & RES_DEFNAMES) != 0U) ||
- (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0U)) {
- int done = 0;
+ /*
+ * We do at least one level of search if
+ * - there is no dot and RES_DEFNAME is set, or
+ * - there is at least one dot, there is no trailing dot,
+ * and RES_DNSRCH is set.
+ */
+ if ((!dots && (statp->options & RES_DEFNAMES) != 0U) ||
+ (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0U)) {
+ int done = 0;
- /* Unfortunately we need to load network-specific info
- * (dns servers, search domains) before
- * the domain stuff is tried. Will have a better
- * fix after thread pools are used as this will
- * be loaded once for the thread instead of each
- * time a query is tried.
- */
- _resolv_populate_res_for_net(statp);
+ /* Unfortunately we need to load network-specific info
+ * (dns servers, search domains) before
+ * the domain stuff is tried. Will have a better
+ * fix after thread pools are used as this will
+ * be loaded once for the thread instead of each
+ * time a query is tried.
+ */
+ _resolv_populate_res_for_net(statp);
- for (domain = (const char * const *)statp->dnsrch;
- *domain && !done;
- domain++) {
- searched = 1;
+ for (domain = (const char* const*) statp->dnsrch; *domain && !done; domain++) {
+ searched = 1;
- if (domain[0][0] == '\0' ||
- (domain[0][0] == '.' && domain[0][1] == '\0'))
- root_on_list++;
+ if (domain[0][0] == '\0' || (domain[0][0] == '.' && domain[0][1] == '\0'))
+ root_on_list++;
- ret = res_nquerydomain(statp, name, *domain,
- class, type,
- answer, anslen);
- if (ret > 0)
- return (ret);
+ ret = res_nquerydomain(statp, name, *domain, class, type, answer, anslen);
+ if (ret > 0) return (ret);
- /*
- * If no server present, give up.
- * If name isn't found in this domain,
- * keep trying higher domains in the search list
- * (if that's enabled).
- * On a NO_DATA error, keep trying, otherwise
- * a wildcard entry of another type could keep us
- * from finding this entry higher in the domain.
- * If we get some other error (negative answer or
- * server failure), then stop searching up,
- * but try the input name below in case it's
- * fully-qualified.
- */
- if (errno == ECONNREFUSED) {
- RES_SET_H_ERRNO(statp, TRY_AGAIN);
- return (-1);
- }
+ /*
+ * If no server present, give up.
+ * If name isn't found in this domain,
+ * keep trying higher domains in the search list
+ * (if that's enabled).
+ * On a NO_DATA error, keep trying, otherwise
+ * a wildcard entry of another type could keep us
+ * from finding this entry higher in the domain.
+ * If we get some other error (negative answer or
+ * server failure), then stop searching up,
+ * but try the input name below in case it's
+ * fully-qualified.
+ */
+ if (errno == ECONNREFUSED) {
+ RES_SET_H_ERRNO(statp, TRY_AGAIN);
+ return (-1);
+ }
- switch (statp->res_h_errno) {
- case NO_DATA:
- got_nodata++;
- /* FALLTHROUGH */
- case HOST_NOT_FOUND:
- /* keep trying */
- break;
- case TRY_AGAIN:
- if (hp->rcode == SERVFAIL) {
- /* try next search element, if any */
- got_servfail++;
- break;
- }
- /* FALLTHROUGH */
- default:
- /* anything else implies that we're done */
- done++;
- }
+ switch (statp->res_h_errno) {
+ case NO_DATA:
+ got_nodata++;
+ /* FALLTHROUGH */
+ case HOST_NOT_FOUND:
+ /* keep trying */
+ break;
+ case TRY_AGAIN:
+ if (hp->rcode == SERVFAIL) {
+ /* try next search element, if any */
+ got_servfail++;
+ break;
+ }
+ /* FALLTHROUGH */
+ default:
+ /* anything else implies that we're done */
+ done++;
+ }
- /* if we got here for some reason other than DNSRCH,
- * we only wanted one iteration of the loop, so stop.
- */
- if ((statp->options & RES_DNSRCH) == 0U)
- done++;
- }
- }
+ /* if we got here for some reason other than DNSRCH,
+ * we only wanted one iteration of the loop, so stop.
+ */
+ if ((statp->options & RES_DNSRCH) == 0U) done++;
+ }
+ }
- /*
- * If the query has not already been tried as is then try it
- * unless RES_NOTLDQUERY is set and there were no dots.
- */
- if ((dots || !searched || (statp->options & RES_NOTLDQUERY) == 0U) &&
- !(tried_as_is || root_on_list)) {
- ret = res_nquerydomain(statp, name, NULL, class, type,
- answer, anslen);
- if (ret > 0)
- return (ret);
- }
+ /*
+ * If the query has not already been tried as is then try it
+ * unless RES_NOTLDQUERY is set and there were no dots.
+ */
+ if ((dots || !searched || (statp->options & RES_NOTLDQUERY) == 0U) &&
+ !(tried_as_is || root_on_list)) {
+ ret = res_nquerydomain(statp, name, NULL, class, type, answer, anslen);
+ if (ret > 0) return (ret);
+ }
- /* if we got here, we didn't satisfy the search.
- * if we did an initial full query, return that query's H_ERRNO
- * (note that we wouldn't be here if that query had succeeded).
- * else if we ever got a nodata, send that back as the reason.
- * else send back meaningless H_ERRNO, that being the one from
- * the last DNSRCH we did.
- */
- if (saved_herrno != -1)
- RES_SET_H_ERRNO(statp, saved_herrno);
- else if (got_nodata)
- RES_SET_H_ERRNO(statp, NO_DATA);
- else if (got_servfail)
- RES_SET_H_ERRNO(statp, TRY_AGAIN);
- return (-1);
+ /* if we got here, we didn't satisfy the search.
+ * if we did an initial full query, return that query's H_ERRNO
+ * (note that we wouldn't be here if that query had succeeded).
+ * else if we ever got a nodata, send that back as the reason.
+ * else send back meaningless H_ERRNO, that being the one from
+ * the last DNSRCH we did.
+ */
+ if (saved_herrno != -1)
+ RES_SET_H_ERRNO(statp, saved_herrno);
+ else if (got_nodata)
+ RES_SET_H_ERRNO(statp, NO_DATA);
+ else if (got_servfail)
+ RES_SET_H_ERRNO(statp, TRY_AGAIN);
+ return (-1);
}
/*
* Perform a call on res_query on the concatenation of name and domain,
* removing a trailing dot from name if domain is NULL.
*/
-int
-res_nquerydomain(res_state statp,
- const char *name,
- const char *domain,
- int class, int type, /* class and type of query */
- u_char *answer, /* buffer to put answer */
- int anslen) /* size of answer */
+int res_nquerydomain(res_state statp, const char* name, const char* domain, int class,
+ int type, /* class and type of query */
+ u_char* answer, /* buffer to put answer */
+ int anslen) /* size of answer */
{
- char nbuf[MAXDNAME];
- const char *longname = nbuf;
- int n, d;
+ char nbuf[MAXDNAME];
+ const char* longname = nbuf;
+ int n, d;
#ifdef DEBUG
- if (statp->options & RES_DEBUG)
- printf(";; res_nquerydomain(%s, %s, %d, %d)\n",
- name, domain?domain:"<Nil>", class, type);
+ if (statp->options & RES_DEBUG)
+ printf(";; res_nquerydomain(%s, %s, %d, %d)\n", name, domain ? domain : "<Nil>", class,
+ type);
#endif
- if (domain == NULL) {
- /*
- * Check for trailing '.';
- * copy without '.' if present.
- */
- n = strlen(name);
- if (n >= MAXDNAME) {
- RES_SET_H_ERRNO(statp, NO_RECOVERY);
- return (-1);
- }
- n--;
- if (n >= 0 && name[n] == '.') {
- strncpy(nbuf, name, (size_t)n);
- nbuf[n] = '\0';
- } else
- longname = name;
- } else {
- n = strlen(name);
- d = strlen(domain);
- if (n + d + 1 >= MAXDNAME) {
- RES_SET_H_ERRNO(statp, NO_RECOVERY);
- return (-1);
- }
- snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
- }
- return (res_nquery(statp, longname, class, type, answer, anslen));
+ if (domain == NULL) {
+ /*
+ * Check for trailing '.';
+ * copy without '.' if present.
+ */
+ n = strlen(name);
+ if (n >= MAXDNAME) {
+ RES_SET_H_ERRNO(statp, NO_RECOVERY);
+ return (-1);
+ }
+ n--;
+ if (n >= 0 && name[n] == '.') {
+ strncpy(nbuf, name, (size_t) n);
+ nbuf[n] = '\0';
+ } else
+ longname = name;
+ } else {
+ n = strlen(name);
+ d = strlen(domain);
+ if (n + d + 1 >= MAXDNAME) {
+ RES_SET_H_ERRNO(statp, NO_RECOVERY);
+ return (-1);
+ }
+ snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
+ }
+ return (res_nquery(statp, longname, class, type, answer, anslen));
}
-const char *
-res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) {
- return (NULL);
+const char* res_hostalias(const res_state statp, const char* name, char* dst, size_t siz) {
+ return (NULL);
}
diff --git a/resolv/res_send.c b/resolv/res_send.c
index a645a6b..f2563fb 100644
--- a/resolv/res_send.c
+++ b/resolv/res_send.c
@@ -84,24 +84,24 @@
* Send query to name server and wait for reply.
*/
-#include <sys/types.h>
#include <sys/param.h>
-#include <sys/time.h>
#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/types.h>
#include <sys/uio.h>
-#include <netinet/in.h>
-#include <arpa/nameser.h>
#include <arpa/inet.h>
+#include <arpa/nameser.h>
+#include <netinet/in.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <poll.h>
#ifdef ANDROID_CHANGES
+#include "private/android_filesystem_config.h"
#include "resolv_netid.h"
#include "resolv_private.h"
-#include "private/android_filesystem_config.h"
#else
#include <resolv.h>
#endif
@@ -119,8 +119,8 @@
#include <async_safe/log.h>
#ifndef DE_CONST
-#define DE_CONST(c,v) v = ((c) ? \
- strchr((const void *)(c), *(const char *)(const void *)(c)) : NULL)
+#define DE_CONST(c, v) \
+ v = ((c) ? strchr((const void*) (c), *(const char*) (const void*) (c)) : NULL)
#endif
/* Options. Leave them on. */
@@ -136,46 +136,43 @@
/* Forward. */
-static int get_salen __P((const struct sockaddr *));
-static struct sockaddr * get_nsaddr __P((res_state, size_t));
-static int send_vc(res_state, struct __res_params *params, const u_char *, int,
- u_char *, int, int *, int, time_t *, int *, int *);
-static int send_dg(res_state, struct __res_params *params, const u_char *, int,
- u_char *, int, int *, int, int *, int *, time_t *, int *, int *);
-static void Aerror(const res_state, FILE *, const char *, int,
- const struct sockaddr *, int);
-static void Perror(const res_state, FILE *, const char *, int);
-static int sock_eq(struct sockaddr *, struct sockaddr *);
-void res_pquery(const res_state, const u_char *, int, FILE *);
-static int connect_with_timeout(int sock, const struct sockaddr *nsap,
- socklen_t salen, const struct timespec timeout);
+static int get_salen __P((const struct sockaddr*) );
+static struct sockaddr* get_nsaddr __P((res_state, size_t));
+static int send_vc(res_state, struct __res_params* params, const u_char*, int, u_char*, int, int*,
+ int, time_t*, int*, int*);
+static int send_dg(res_state, struct __res_params* params, const u_char*, int, u_char*, int, int*,
+ int, int*, int*, time_t*, int*, int*);
+static void Aerror(const res_state, FILE*, const char*, int, const struct sockaddr*, int);
+static void Perror(const res_state, FILE*, const char*, int);
+static int sock_eq(struct sockaddr*, struct sockaddr*);
+void res_pquery(const res_state, const u_char*, int, FILE*);
+static int connect_with_timeout(int sock, const struct sockaddr* nsap, socklen_t salen,
+ const struct timespec timeout);
static int retrying_poll(const int sock, short events, const struct timespec* finish);
/* BIONIC-BEGIN: implement source port randomization */
typedef union {
- struct sockaddr sa;
- struct sockaddr_in sin;
- struct sockaddr_in6 sin6;
+ struct sockaddr sa;
+ struct sockaddr_in sin;
+ struct sockaddr_in6 sin6;
} _sockaddr_union;
-static int
-random_bind( int s, int family )
-{
- _sockaddr_union u;
- int j;
- socklen_t slen;
+static int random_bind(int s, int family) {
+ _sockaddr_union u;
+ int j;
+ socklen_t slen;
/* clear all, this also sets the IP4/6 address to 'any' */
- memset( &u, 0, sizeof u );
+ memset(&u, 0, sizeof u);
switch (family) {
case AF_INET:
u.sin.sin_family = family;
- slen = sizeof u.sin;
+ slen = sizeof u.sin;
break;
case AF_INET6:
u.sin6.sin6_family = family;
- slen = sizeof u.sin6;
+ slen = sizeof u.sin6;
break;
default:
errno = EPROTO;
@@ -185,14 +182,13 @@
/* first try to bind to a random source port a few times */
for (j = 0; j < 10; j++) {
/* find a random port between 1025 .. 65534 */
- int port = 1025 + (res_randomid() % (65535-1025));
+ int port = 1025 + (res_randomid() % (65535 - 1025));
if (family == AF_INET)
u.sin.sin_port = htons(port);
else
u.sin6.sin6_port = htons(port);
- if ( !bind( s, &u.sa, slen ) )
- return 0;
+ if (!bind(s, &u.sa, slen)) return 0;
}
/* nothing after 10 tries, our network table is probably busy */
@@ -202,7 +198,7 @@
else
u.sin6.sin6_port = 0;
- return bind( s, &u.sa, slen );
+ return bind(s, &u.sa, slen);
}
/* BIONIC-END */
@@ -219,45 +215,40 @@
* author:
* paul vixie, 29may94
*/
-__LIBC_HIDDEN__ int
-res_ourserver_p(const res_state statp, const struct sockaddr *sa) {
- const struct sockaddr_in *inp, *srv;
- const struct sockaddr_in6 *in6p, *srv6;
- int ns;
+__LIBC_HIDDEN__ int res_ourserver_p(const res_state statp, const struct sockaddr* sa) {
+ const struct sockaddr_in *inp, *srv;
+ const struct sockaddr_in6 *in6p, *srv6;
+ int ns;
- switch (sa->sa_family) {
- case AF_INET:
- inp = (const struct sockaddr_in *)(const void *)sa;
- for (ns = 0; ns < statp->nscount; ns++) {
- srv = (struct sockaddr_in *)(void *)get_nsaddr(statp, (size_t)ns);
- if (srv->sin_family == inp->sin_family &&
- srv->sin_port == inp->sin_port &&
- (srv->sin_addr.s_addr == INADDR_ANY ||
- srv->sin_addr.s_addr == inp->sin_addr.s_addr))
- return (1);
- }
- break;
- case AF_INET6:
- if (EXT(statp).ext == NULL)
- break;
- in6p = (const struct sockaddr_in6 *)(const void *)sa;
- for (ns = 0; ns < statp->nscount; ns++) {
- srv6 = (struct sockaddr_in6 *)(void *)get_nsaddr(statp, (size_t)ns);
- if (srv6->sin6_family == in6p->sin6_family &&
- srv6->sin6_port == in6p->sin6_port &&
+ switch (sa->sa_family) {
+ case AF_INET:
+ inp = (const struct sockaddr_in*) (const void*) sa;
+ for (ns = 0; ns < statp->nscount; ns++) {
+ srv = (struct sockaddr_in*) (void*) get_nsaddr(statp, (size_t) ns);
+ if (srv->sin_family == inp->sin_family && srv->sin_port == inp->sin_port &&
+ (srv->sin_addr.s_addr == INADDR_ANY ||
+ srv->sin_addr.s_addr == inp->sin_addr.s_addr))
+ return (1);
+ }
+ break;
+ case AF_INET6:
+ if (EXT(statp).ext == NULL) break;
+ in6p = (const struct sockaddr_in6*) (const void*) sa;
+ for (ns = 0; ns < statp->nscount; ns++) {
+ srv6 = (struct sockaddr_in6*) (void*) get_nsaddr(statp, (size_t) ns);
+ if (srv6->sin6_family == in6p->sin6_family && srv6->sin6_port == in6p->sin6_port &&
#ifdef HAVE_SIN6_SCOPE_ID
- (srv6->sin6_scope_id == 0 ||
- srv6->sin6_scope_id == in6p->sin6_scope_id) &&
+ (srv6->sin6_scope_id == 0 || srv6->sin6_scope_id == in6p->sin6_scope_id) &&
#endif
- (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
- IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
- return (1);
- }
- break;
- default:
- break;
- }
- return (0);
+ (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
+ IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
+ return (1);
+ }
+ break;
+ default:
+ break;
+ }
+ return (0);
}
/* int
@@ -272,30 +263,25 @@
* author:
* paul vixie, 29may94
*/
-int
-res_nameinquery(const char *name, int type, int class,
- const u_char *buf, const u_char *eom)
-{
- const u_char *cp = buf + HFIXEDSZ;
- int qdcount = ntohs(((const HEADER*)(const void *)buf)->qdcount);
+int res_nameinquery(const char* name, int type, int class, const u_char* buf, const u_char* eom) {
+ const u_char* cp = buf + HFIXEDSZ;
+ int qdcount = ntohs(((const HEADER*) (const void*) buf)->qdcount);
- while (qdcount-- > 0) {
- char tname[MAXDNAME+1];
- int n, ttype, tclass;
+ while (qdcount-- > 0) {
+ char tname[MAXDNAME + 1];
+ int n, ttype, tclass;
- n = dn_expand(buf, eom, cp, tname, sizeof tname);
- if (n < 0)
- return (-1);
- cp += n;
- if (cp + 2 * INT16SZ > eom)
- return (-1);
- ttype = ns_get16(cp); cp += INT16SZ;
- tclass = ns_get16(cp); cp += INT16SZ;
- if (ttype == type && tclass == class &&
- ns_samename(tname, name) == 1)
- return (1);
- }
- return (0);
+ n = dn_expand(buf, eom, cp, tname, sizeof tname);
+ if (n < 0) return (-1);
+ cp += n;
+ if (cp + 2 * INT16SZ > eom) return (-1);
+ ttype = ns_get16(cp);
+ cp += INT16SZ;
+ tclass = ns_get16(cp);
+ cp += INT16SZ;
+ if (ttype == type && tclass == class && ns_samename(tname, name) == 1) return (1);
+ }
+ return (0);
}
/* int
@@ -309,1040 +295,953 @@
* author:
* paul vixie, 29may94
*/
-int
-res_queriesmatch(const u_char *buf1, const u_char *eom1,
- const u_char *buf2, const u_char *eom2)
-{
- const u_char *cp = buf1 + HFIXEDSZ;
- int qdcount = ntohs(((const HEADER*)(const void *)buf1)->qdcount);
+int res_queriesmatch(const u_char* buf1, const u_char* eom1, const u_char* buf2,
+ const u_char* eom2) {
+ const u_char* cp = buf1 + HFIXEDSZ;
+ int qdcount = ntohs(((const HEADER*) (const void*) buf1)->qdcount);
- if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
- return (-1);
+ if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2) return (-1);
- /*
- * Only header section present in replies to
- * dynamic update packets.
- */
- if ((((const HEADER *)(const void *)buf1)->opcode == ns_o_update) &&
- (((const HEADER *)(const void *)buf2)->opcode == ns_o_update))
- return (1);
+ /*
+ * Only header section present in replies to
+ * dynamic update packets.
+ */
+ if ((((const HEADER*) (const void*) buf1)->opcode == ns_o_update) &&
+ (((const HEADER*) (const void*) buf2)->opcode == ns_o_update))
+ return (1);
- if (qdcount != ntohs(((const HEADER*)(const void *)buf2)->qdcount))
- return (0);
- while (qdcount-- > 0) {
- char tname[MAXDNAME+1];
- int n, ttype, tclass;
+ if (qdcount != ntohs(((const HEADER*) (const void*) buf2)->qdcount)) return (0);
+ while (qdcount-- > 0) {
+ char tname[MAXDNAME + 1];
+ int n, ttype, tclass;
- n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
- if (n < 0)
- return (-1);
- cp += n;
- if (cp + 2 * INT16SZ > eom1)
- return (-1);
- ttype = ns_get16(cp); cp += INT16SZ;
- tclass = ns_get16(cp); cp += INT16SZ;
- if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
- return (0);
- }
- return (1);
+ n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
+ if (n < 0) return (-1);
+ cp += n;
+ if (cp + 2 * INT16SZ > eom1) return (-1);
+ ttype = ns_get16(cp);
+ cp += INT16SZ;
+ tclass = ns_get16(cp);
+ cp += INT16SZ;
+ if (!res_nameinquery(tname, ttype, tclass, buf2, eom2)) return (0);
+ }
+ return (1);
}
-int
-res_nsend(res_state statp,
- const u_char *buf, int buflen, u_char *ans, int anssiz)
-{
- int gotsomewhere, terrno, try, v_circuit, resplen, ns, n;
- char abuf[NI_MAXHOST];
- ResolvCacheStatus cache_status = RESOLV_CACHE_UNSUPPORTED;
+int res_nsend(res_state statp, const u_char* buf, int buflen, u_char* ans, int anssiz) {
+ int gotsomewhere, terrno, try
+ , v_circuit, resplen, ns, n;
+ char abuf[NI_MAXHOST];
+ ResolvCacheStatus cache_status = RESOLV_CACHE_UNSUPPORTED;
- if (anssiz < HFIXEDSZ) {
- errno = EINVAL;
- return (-1);
- }
- DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
- (stdout, ";; res_send()\n"), buf, buflen);
- v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
- gotsomewhere = 0;
- terrno = ETIMEDOUT;
+ if (anssiz < HFIXEDSZ) {
+ errno = EINVAL;
+ return (-1);
+ }
+ DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
+ (stdout, ";; res_send()\n"), buf, buflen);
+ v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
+ gotsomewhere = 0;
+ terrno = ETIMEDOUT;
- int anslen = 0;
- cache_status = _resolv_cache_lookup(
- statp->netid, buf, buflen,
- ans, anssiz, &anslen);
+ int anslen = 0;
+ cache_status = _resolv_cache_lookup(statp->netid, buf, buflen, ans, anssiz, &anslen);
- if (cache_status == RESOLV_CACHE_FOUND) {
- return anslen;
- } else if (cache_status != RESOLV_CACHE_UNSUPPORTED) {
- // had a cache miss for a known network, so populate the thread private
- // data so the normal resolve path can do its thing
- _resolv_populate_res_for_net(statp);
- }
- if (statp->nscount == 0) {
- // We have no nameservers configured, so there's no point trying.
- // Tell the cache the query failed, or any retries and anyone else asking the same
- // question will block for PENDING_REQUEST_TIMEOUT seconds instead of failing fast.
- _resolv_cache_query_failed(statp->netid, buf, buflen);
- errno = ESRCH;
- return (-1);
- }
+ if (cache_status == RESOLV_CACHE_FOUND) {
+ return anslen;
+ } else if (cache_status != RESOLV_CACHE_UNSUPPORTED) {
+ // had a cache miss for a known network, so populate the thread private
+ // data so the normal resolve path can do its thing
+ _resolv_populate_res_for_net(statp);
+ }
+ if (statp->nscount == 0) {
+ // We have no nameservers configured, so there's no point trying.
+ // Tell the cache the query failed, or any retries and anyone else asking the same
+ // question will block for PENDING_REQUEST_TIMEOUT seconds instead of failing fast.
+ _resolv_cache_query_failed(statp->netid, buf, buflen);
+ errno = ESRCH;
+ return (-1);
+ }
- /*
- * If the ns_addr_list in the resolver context has changed, then
- * invalidate our cached copy and the associated timing data.
- */
- if (EXT(statp).nscount != 0) {
- int needclose = 0;
- struct sockaddr_storage peer;
- socklen_t peerlen;
+ /*
+ * If the ns_addr_list in the resolver context has changed, then
+ * invalidate our cached copy and the associated timing data.
+ */
+ if (EXT(statp).nscount != 0) {
+ int needclose = 0;
+ struct sockaddr_storage peer;
+ socklen_t peerlen;
- if (EXT(statp).nscount != statp->nscount) {
- needclose++;
- } else {
- for (ns = 0; ns < statp->nscount; ns++) {
- if (statp->nsaddr_list[ns].sin_family &&
- !sock_eq((struct sockaddr *)(void *)&statp->nsaddr_list[ns],
- (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[ns])) {
- needclose++;
- break;
- }
+ if (EXT(statp).nscount != statp->nscount) {
+ needclose++;
+ } else {
+ for (ns = 0; ns < statp->nscount; ns++) {
+ if (statp->nsaddr_list[ns].sin_family &&
+ !sock_eq((struct sockaddr*) (void*) &statp->nsaddr_list[ns],
+ (struct sockaddr*) (void*) &EXT(statp).ext->nsaddrs[ns])) {
+ needclose++;
+ break;
+ }
- if (EXT(statp).nssocks[ns] == -1)
- continue;
- peerlen = sizeof(peer);
- if (getpeername(EXT(statp).nssocks[ns],
- (struct sockaddr *)(void *)&peer, &peerlen) < 0) {
- needclose++;
- break;
- }
- if (!sock_eq((struct sockaddr *)(void *)&peer,
- get_nsaddr(statp, (size_t)ns))) {
- needclose++;
- break;
- }
- }
- }
- if (needclose) {
- res_nclose(statp);
- EXT(statp).nscount = 0;
- }
- }
+ if (EXT(statp).nssocks[ns] == -1) continue;
+ peerlen = sizeof(peer);
+ if (getpeername(EXT(statp).nssocks[ns], (struct sockaddr*) (void*) &peer,
+ &peerlen) < 0) {
+ needclose++;
+ break;
+ }
+ if (!sock_eq((struct sockaddr*) (void*) &peer, get_nsaddr(statp, (size_t) ns))) {
+ needclose++;
+ break;
+ }
+ }
+ }
+ if (needclose) {
+ res_nclose(statp);
+ EXT(statp).nscount = 0;
+ }
+ }
- /*
- * Maybe initialize our private copy of the ns_addr_list.
- */
- if (EXT(statp).nscount == 0) {
- for (ns = 0; ns < statp->nscount; ns++) {
- EXT(statp).nstimes[ns] = RES_MAXTIME;
- EXT(statp).nssocks[ns] = -1;
- if (!statp->nsaddr_list[ns].sin_family)
- continue;
- EXT(statp).ext->nsaddrs[ns].sin =
- statp->nsaddr_list[ns];
- }
- EXT(statp).nscount = statp->nscount;
- }
+ /*
+ * Maybe initialize our private copy of the ns_addr_list.
+ */
+ if (EXT(statp).nscount == 0) {
+ for (ns = 0; ns < statp->nscount; ns++) {
+ EXT(statp).nstimes[ns] = RES_MAXTIME;
+ EXT(statp).nssocks[ns] = -1;
+ if (!statp->nsaddr_list[ns].sin_family) continue;
+ EXT(statp).ext->nsaddrs[ns].sin = statp->nsaddr_list[ns];
+ }
+ EXT(statp).nscount = statp->nscount;
+ }
- /*
- * Some resolvers want to even out the load on their nameservers.
- * Note that RES_BLAST overrides RES_ROTATE.
- */
- if ((statp->options & RES_ROTATE) != 0U &&
- (statp->options & RES_BLAST) == 0U) {
- union res_sockaddr_union inu;
- struct sockaddr_in ina;
- int lastns = statp->nscount - 1;
- int fd;
- u_int16_t nstime;
+ /*
+ * Some resolvers want to even out the load on their nameservers.
+ * Note that RES_BLAST overrides RES_ROTATE.
+ */
+ if ((statp->options & RES_ROTATE) != 0U && (statp->options & RES_BLAST) == 0U) {
+ union res_sockaddr_union inu;
+ struct sockaddr_in ina;
+ int lastns = statp->nscount - 1;
+ int fd;
+ u_int16_t nstime;
- if (EXT(statp).ext != NULL)
- inu = EXT(statp).ext->nsaddrs[0];
- ina = statp->nsaddr_list[0];
- fd = EXT(statp).nssocks[0];
- nstime = EXT(statp).nstimes[0];
- for (ns = 0; ns < lastns; ns++) {
- if (EXT(statp).ext != NULL)
- EXT(statp).ext->nsaddrs[ns] =
- EXT(statp).ext->nsaddrs[ns + 1];
- statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
- EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
- EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
- }
- if (EXT(statp).ext != NULL)
- EXT(statp).ext->nsaddrs[lastns] = inu;
- statp->nsaddr_list[lastns] = ina;
- EXT(statp).nssocks[lastns] = fd;
- EXT(statp).nstimes[lastns] = nstime;
- }
+ if (EXT(statp).ext != NULL) inu = EXT(statp).ext->nsaddrs[0];
+ ina = statp->nsaddr_list[0];
+ fd = EXT(statp).nssocks[0];
+ nstime = EXT(statp).nstimes[0];
+ for (ns = 0; ns < lastns; ns++) {
+ if (EXT(statp).ext != NULL)
+ EXT(statp).ext->nsaddrs[ns] = EXT(statp).ext->nsaddrs[ns + 1];
+ statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
+ EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
+ EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
+ }
+ if (EXT(statp).ext != NULL) EXT(statp).ext->nsaddrs[lastns] = inu;
+ statp->nsaddr_list[lastns] = ina;
+ EXT(statp).nssocks[lastns] = fd;
+ EXT(statp).nstimes[lastns] = nstime;
+ }
- /*
- * Send request, RETRY times, or until successful.
- */
- for (try = 0; try < statp->retry; try++) {
- struct __res_stats stats[MAXNS];
- struct __res_params params;
- int revision_id = _resolv_cache_get_resolver_stats(statp->netid, ¶ms, stats);
- bool usable_servers[MAXNS];
- android_net_res_stats_get_usable_servers(¶ms, stats, statp->nscount,
- usable_servers);
+ /*
+ * Send request, RETRY times, or until successful.
+ */
+ for (try = 0; try < statp->retry; try ++) {
+ struct __res_stats stats[MAXNS];
+ struct __res_params params;
+ int revision_id = _resolv_cache_get_resolver_stats(statp->netid, ¶ms, stats);
+ bool usable_servers[MAXNS];
+ android_net_res_stats_get_usable_servers(¶ms, stats, statp->nscount, usable_servers);
- for (ns = 0; ns < statp->nscount; ns++) {
- if (!usable_servers[ns]) continue;
- struct sockaddr *nsap;
- int nsaplen;
- time_t now = 0;
- int rcode = RCODE_INTERNAL_ERROR;
- int delay = 0;
- nsap = get_nsaddr(statp, (size_t)ns);
- nsaplen = get_salen(nsap);
- statp->_flags &= ~RES_F_LASTMASK;
- statp->_flags |= (ns << RES_F_LASTSHIFT);
+ for (ns = 0; ns < statp->nscount; ns++) {
+ if (!usable_servers[ns]) continue;
+ struct sockaddr* nsap;
+ int nsaplen;
+ time_t now = 0;
+ int rcode = RCODE_INTERNAL_ERROR;
+ int delay = 0;
+ nsap = get_nsaddr(statp, (size_t) ns);
+ nsaplen = get_salen(nsap);
+ statp->_flags &= ~RES_F_LASTMASK;
+ statp->_flags |= (ns << RES_F_LASTSHIFT);
- same_ns:
- if (statp->qhook) {
- int done = 0, loops = 0;
+ same_ns:
+ if (statp->qhook) {
+ int done = 0, loops = 0;
- do {
- res_sendhookact act;
+ do {
+ res_sendhookact act;
- act = (*statp->qhook)(&nsap, &buf, &buflen,
- ans, anssiz, &resplen);
- switch (act) {
- case res_goahead:
- done = 1;
- break;
- case res_nextns:
- res_nclose(statp);
- goto next_ns;
- case res_done:
- if (cache_status == RESOLV_CACHE_NOTFOUND) {
- _resolv_cache_add(statp->netid, buf, buflen,
- ans, resplen);
- }
- return (resplen);
- case res_modified:
- /* give the hook another try */
- if (++loops < 42) /*doug adams*/
- break;
- /*FALLTHROUGH*/
- case res_error:
- /*FALLTHROUGH*/
- default:
- goto fail;
- }
- } while (!done);
- }
+ act = (*statp->qhook)(&nsap, &buf, &buflen, ans, anssiz, &resplen);
+ switch (act) {
+ case res_goahead:
+ done = 1;
+ break;
+ case res_nextns:
+ res_nclose(statp);
+ goto next_ns;
+ case res_done:
+ if (cache_status == RESOLV_CACHE_NOTFOUND) {
+ _resolv_cache_add(statp->netid, buf, buflen, ans, resplen);
+ }
+ return (resplen);
+ case res_modified:
+ /* give the hook another try */
+ if (++loops < 42) /*doug adams*/
+ break;
+ /*FALLTHROUGH*/
+ case res_error:
+ /*FALLTHROUGH*/
+ default:
+ goto fail;
+ }
+ } while (!done);
+ }
- Dprint(((statp->options & RES_DEBUG) &&
- getnameinfo(nsap, (socklen_t)nsaplen, abuf, sizeof(abuf),
- NULL, 0, niflags) == 0),
- (stdout, ";; Querying server (# %d) address = %s\n",
- ns + 1, abuf));
+ Dprint(((statp->options & RES_DEBUG) &&
+ getnameinfo(nsap, (socklen_t) nsaplen, abuf, sizeof(abuf), NULL, 0, niflags) ==
+ 0),
+ (stdout, ";; Querying server (# %d) address = %s\n", ns + 1, abuf));
+ if (v_circuit) {
+ /* Use VC; at most one attempt per server. */
+ try
+ = statp->retry;
- if (v_circuit) {
- /* Use VC; at most one attempt per server. */
- try = statp->retry;
+ n = send_vc(statp, ¶ms, buf, buflen, ans, anssiz, &terrno, ns, &now, &rcode,
+ &delay);
- n = send_vc(statp, ¶ms, buf, buflen, ans, anssiz, &terrno,
- ns, &now, &rcode, &delay);
+ /*
+ * Only record stats the first time we try a query. This ensures that
+ * queries that deterministically fail (e.g., a name that always returns
+ * SERVFAIL or times out) do not unduly affect the stats.
+ */
+ if (try == 0) {
+ struct __res_sample sample;
+ _res_stats_set_sample(&sample, now, rcode, delay);
+ _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample,
+ params.max_samples);
+ }
- /*
- * Only record stats the first time we try a query. This ensures that
- * queries that deterministically fail (e.g., a name that always returns
- * SERVFAIL or times out) do not unduly affect the stats.
- */
- if (try == 0) {
- struct __res_sample sample;
- _res_stats_set_sample(&sample, now, rcode, delay);
- _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id,
- ns, &sample, params.max_samples);
- }
+ if (DBG) {
+ async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "used send_vc %d\n", n);
+ }
- if (DBG) {
- async_safe_format_log(ANDROID_LOG_DEBUG, "libc",
- "used send_vc %d\n", n);
- }
+ if (n < 0) goto fail;
+ if (n == 0) goto next_ns;
+ resplen = n;
+ } else {
+ /* Use datagrams. */
+ if (DBG) {
+ async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "using send_dg\n");
+ }
- if (n < 0)
- goto fail;
- if (n == 0)
- goto next_ns;
- resplen = n;
- } else {
- /* Use datagrams. */
- if (DBG) {
- async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "using send_dg\n");
- }
+ n = send_dg(statp, ¶ms, buf, buflen, ans, anssiz, &terrno, ns, &v_circuit,
+ &gotsomewhere, &now, &rcode, &delay);
- n = send_dg(statp, ¶ms, buf, buflen, ans, anssiz, &terrno,
- ns, &v_circuit, &gotsomewhere, &now, &rcode, &delay);
+ /* Only record stats the first time we try a query. See above. */
+ if (try == 0) {
+ struct __res_sample sample;
+ _res_stats_set_sample(&sample, now, rcode, delay);
+ _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample,
+ params.max_samples);
+ }
- /* Only record stats the first time we try a query. See above. */
- if (try == 0) {
- struct __res_sample sample;
- _res_stats_set_sample(&sample, now, rcode, delay);
- _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id,
- ns, &sample, params.max_samples);
- }
+ if (DBG) {
+ async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "used send_dg %d\n", n);
+ }
- if (DBG) {
- async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "used send_dg %d\n",n);
- }
+ if (n < 0) goto fail;
+ if (n == 0) goto next_ns;
+ if (DBG) {
+ async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "time=%ld\n", time(NULL));
+ }
+ if (v_circuit) goto same_ns;
+ resplen = n;
+ }
- if (n < 0)
- goto fail;
- if (n == 0)
- goto next_ns;
- if (DBG) {
- async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "time=%ld\n",
- time(NULL));
- }
- if (v_circuit)
- goto same_ns;
- resplen = n;
- }
+ Dprint((statp->options & RES_DEBUG) ||
+ ((statp->pfcode & RES_PRF_REPLY) && (statp->pfcode & RES_PRF_HEAD1)),
+ (stdout, ";; got answer:\n"));
- Dprint((statp->options & RES_DEBUG) ||
- ((statp->pfcode & RES_PRF_REPLY) &&
- (statp->pfcode & RES_PRF_HEAD1)),
- (stdout, ";; got answer:\n"));
+ DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY),
+ (stdout, "%s", ""), ans, (resplen > anssiz) ? anssiz : resplen);
- DprintQ((statp->options & RES_DEBUG) ||
- (statp->pfcode & RES_PRF_REPLY),
- (stdout, "%s", ""),
- ans, (resplen > anssiz) ? anssiz : resplen);
+ if (cache_status == RESOLV_CACHE_NOTFOUND) {
+ _resolv_cache_add(statp->netid, buf, buflen, ans, resplen);
+ }
+ /*
+ * If we have temporarily opened a virtual circuit,
+ * or if we haven't been asked to keep a socket open,
+ * close the socket.
+ */
+ if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
+ (statp->options & RES_STAYOPEN) == 0U) {
+ res_nclose(statp);
+ }
+ if (statp->rhook) {
+ int done = 0, loops = 0;
- if (cache_status == RESOLV_CACHE_NOTFOUND) {
- _resolv_cache_add(statp->netid, buf, buflen,
- ans, resplen);
- }
- /*
- * If we have temporarily opened a virtual circuit,
- * or if we haven't been asked to keep a socket open,
- * close the socket.
- */
- if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
- (statp->options & RES_STAYOPEN) == 0U) {
- res_nclose(statp);
- }
- if (statp->rhook) {
- int done = 0, loops = 0;
+ do {
+ res_sendhookact act;
- do {
- res_sendhookact act;
+ act = (*statp->rhook)(nsap, buf, buflen, ans, anssiz, &resplen);
+ switch (act) {
+ case res_goahead:
+ case res_done:
+ done = 1;
+ break;
+ case res_nextns:
+ res_nclose(statp);
+ goto next_ns;
+ case res_modified:
+ /* give the hook another try */
+ if (++loops < 42) /*doug adams*/
+ break;
+ /*FALLTHROUGH*/
+ case res_error:
+ /*FALLTHROUGH*/
+ default:
+ goto fail;
+ }
+ } while (!done);
+ }
+ return (resplen);
+ next_ns:;
+ } /*foreach ns*/
+ } /*foreach retry*/
+ res_nclose(statp);
+ if (!v_circuit) {
+ if (!gotsomewhere)
+ errno = ECONNREFUSED; /* no nameservers found */
+ else
+ errno = ETIMEDOUT; /* no answer obtained */
+ } else
+ errno = terrno;
- act = (*statp->rhook)(nsap, buf, buflen,
- ans, anssiz, &resplen);
- switch (act) {
- case res_goahead:
- case res_done:
- done = 1;
- break;
- case res_nextns:
- res_nclose(statp);
- goto next_ns;
- case res_modified:
- /* give the hook another try */
- if (++loops < 42) /*doug adams*/
- break;
- /*FALLTHROUGH*/
- case res_error:
- /*FALLTHROUGH*/
- default:
- goto fail;
- }
- } while (!done);
+ _resolv_cache_query_failed(statp->netid, buf, buflen);
- }
- return (resplen);
- next_ns: ;
- } /*foreach ns*/
- } /*foreach retry*/
- res_nclose(statp);
- if (!v_circuit) {
- if (!gotsomewhere)
- errno = ECONNREFUSED; /* no nameservers found */
- else
- errno = ETIMEDOUT; /* no answer obtained */
- } else
- errno = terrno;
+ return (-1);
+fail:
- _resolv_cache_query_failed(statp->netid, buf, buflen);
-
- return (-1);
- fail:
-
- _resolv_cache_query_failed(statp->netid, buf, buflen);
- res_nclose(statp);
- return (-1);
+ _resolv_cache_query_failed(statp->netid, buf, buflen);
+ res_nclose(statp);
+ return (-1);
}
/* Private */
-static int
-get_salen(sa)
- const struct sockaddr *sa;
+static int get_salen(sa) const struct sockaddr* sa;
{
-
#ifdef HAVE_SA_LEN
- /* There are people do not set sa_len. Be forgiving to them. */
- if (sa->sa_len)
- return (sa->sa_len);
+ /* There are people do not set sa_len. Be forgiving to them. */
+ if (sa->sa_len) return (sa->sa_len);
#endif
- if (sa->sa_family == AF_INET)
- return (sizeof(struct sockaddr_in));
- else if (sa->sa_family == AF_INET6)
- return (sizeof(struct sockaddr_in6));
- else
- return (0); /* unknown, die on connect */
+ if (sa->sa_family == AF_INET)
+ return (sizeof(struct sockaddr_in));
+ else if (sa->sa_family == AF_INET6)
+ return (sizeof(struct sockaddr_in6));
+ else
+ return (0); /* unknown, die on connect */
}
/*
* pick appropriate nsaddr_list for use. see res_init() for initialization.
*/
-static struct sockaddr *
-get_nsaddr(statp, n)
- res_state statp;
- size_t n;
+static struct sockaddr* get_nsaddr(statp, n) res_state statp;
+size_t n;
{
-
- if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
- /*
- * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
- * than struct sockaddr, and
- * - user code did not update statp->nsaddr_list[n].
- */
- return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
- } else {
- /*
- * - user code updated statp->nsaddr_list[n], or
- * - statp->nsaddr_list[n] has the same content as
- * EXT(statp).ext->nsaddrs[n].
- */
- return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
- }
+ if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
+ /*
+ * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
+ * than struct sockaddr, and
+ * - user code did not update statp->nsaddr_list[n].
+ */
+ return (struct sockaddr*) (void*) &EXT(statp).ext->nsaddrs[n];
+ } else {
+ /*
+ * - user code updated statp->nsaddr_list[n], or
+ * - statp->nsaddr_list[n] has the same content as
+ * EXT(statp).ext->nsaddrs[n].
+ */
+ return (struct sockaddr*) (void*) &statp->nsaddr_list[n];
+ }
}
-static struct timespec get_timeout(const res_state statp, const struct __res_params* params, const int ns)
-{
- int msec;
- if (params->base_timeout_msec != 0) {
- // TODO: scale the timeout by retry attempt and maybe number of servers
- msec = params->base_timeout_msec;
- } else {
- // Legacy algorithm which scales the timeout by nameserver number.
- // For instance, with 4 nameservers: 5s, 2.5s, 5s, 10s
- // This has no effect with 1 or 2 nameservers
- msec = (statp->retrans * 1000) << ns;
- if (ns > 0) {
- msec /= statp->nscount;
- }
- if (msec < 1000) {
- msec = 1000; // Use at least 100ms
- }
- }
- if (DBG) {
- async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "using timeout of %d msec\n", msec);
- }
+static struct timespec get_timeout(const res_state statp, const struct __res_params* params,
+ const int ns) {
+ int msec;
+ if (params->base_timeout_msec != 0) {
+ // TODO: scale the timeout by retry attempt and maybe number of servers
+ msec = params->base_timeout_msec;
+ } else {
+ // Legacy algorithm which scales the timeout by nameserver number.
+ // For instance, with 4 nameservers: 5s, 2.5s, 5s, 10s
+ // This has no effect with 1 or 2 nameservers
+ msec = (statp->retrans * 1000) << ns;
+ if (ns > 0) {
+ msec /= statp->nscount;
+ }
+ if (msec < 1000) {
+ msec = 1000; // Use at least 100ms
+ }
+ }
+ if (DBG) {
+ async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "using timeout of %d msec\n", msec);
+ }
- struct timespec result;
- result.tv_sec = msec / 1000;
- result.tv_nsec = (msec % 1000) * 1000000;
- return result;
+ struct timespec result;
+ result.tv_sec = msec / 1000;
+ result.tv_nsec = (msec % 1000) * 1000000;
+ return result;
}
-static int
-send_vc(res_state statp, struct __res_params* params,
- const u_char *buf, int buflen, u_char *ans, int anssiz,
- int *terrno, int ns, time_t* at, int* rcode, int* delay)
-{
- *at = time(NULL);
- *rcode = RCODE_INTERNAL_ERROR;
- *delay = 0;
- const HEADER *hp = (const HEADER *)(const void *)buf;
- HEADER *anhp = (HEADER *)(void *)ans;
- struct sockaddr *nsap;
- int nsaplen;
- int truncating, connreset, resplen, n;
- struct iovec iov[2];
- u_short len;
- u_char *cp;
- void *tmp;
+static int send_vc(res_state statp, struct __res_params* params, const u_char* buf, int buflen,
+ u_char* ans, int anssiz, int* terrno, int ns, time_t* at, int* rcode,
+ int* delay) {
+ *at = time(NULL);
+ *rcode = RCODE_INTERNAL_ERROR;
+ *delay = 0;
+ const HEADER* hp = (const HEADER*) (const void*) buf;
+ HEADER* anhp = (HEADER*) (void*) ans;
+ struct sockaddr* nsap;
+ int nsaplen;
+ int truncating, connreset, resplen, n;
+ struct iovec iov[2];
+ u_short len;
+ u_char* cp;
+ void* tmp;
- if (DBG) {
- async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "using send_vc\n");
- }
+ if (DBG) {
+ async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "using send_vc\n");
+ }
- nsap = get_nsaddr(statp, (size_t)ns);
- nsaplen = get_salen(nsap);
+ nsap = get_nsaddr(statp, (size_t) ns);
+ nsaplen = get_salen(nsap);
- connreset = 0;
- same_ns:
- truncating = 0;
+ connreset = 0;
+same_ns:
+ truncating = 0;
- struct timespec now = evNowTime();
+ struct timespec now = evNowTime();
- /* Are we still talking to whom we want to talk to? */
- if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
- struct sockaddr_storage peer;
- socklen_t size = sizeof peer;
- unsigned old_mark;
- socklen_t mark_size = sizeof(old_mark);
- if (getpeername(statp->_vcsock,
- (struct sockaddr *)(void *)&peer, &size) < 0 ||
- !sock_eq((struct sockaddr *)(void *)&peer, nsap) ||
- getsockopt(statp->_vcsock, SOL_SOCKET, SO_MARK, &old_mark, &mark_size) < 0 ||
- old_mark != statp->_mark) {
- res_nclose(statp);
- statp->_flags &= ~RES_F_VC;
- }
- }
+ /* Are we still talking to whom we want to talk to? */
+ if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
+ struct sockaddr_storage peer;
+ socklen_t size = sizeof peer;
+ unsigned old_mark;
+ socklen_t mark_size = sizeof(old_mark);
+ if (getpeername(statp->_vcsock, (struct sockaddr*) (void*) &peer, &size) < 0 ||
+ !sock_eq((struct sockaddr*) (void*) &peer, nsap) ||
+ getsockopt(statp->_vcsock, SOL_SOCKET, SO_MARK, &old_mark, &mark_size) < 0 ||
+ old_mark != statp->_mark) {
+ res_nclose(statp);
+ statp->_flags &= ~RES_F_VC;
+ }
+ }
- if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
- if (statp->_vcsock >= 0)
- res_nclose(statp);
+ if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
+ if (statp->_vcsock >= 0) res_nclose(statp);
- statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
- if (statp->_vcsock < 0) {
- switch (errno) {
- case EPROTONOSUPPORT:
+ statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ if (statp->_vcsock < 0) {
+ switch (errno) {
+ case EPROTONOSUPPORT:
#ifdef EPFNOSUPPORT
- case EPFNOSUPPORT:
+ case EPFNOSUPPORT:
#endif
- case EAFNOSUPPORT:
- Perror(statp, stderr, "socket(vc)", errno);
- return (0);
- default:
- *terrno = errno;
- Perror(statp, stderr, "socket(vc)", errno);
- return (-1);
- }
- }
- fchown(statp->_vcsock, AID_DNS, -1);
- if (statp->_mark != MARK_UNSET) {
- if (setsockopt(statp->_vcsock, SOL_SOCKET,
- SO_MARK, &statp->_mark, sizeof(statp->_mark)) < 0) {
- *terrno = errno;
- Perror(statp, stderr, "setsockopt", errno);
- return -1;
- }
- }
- errno = 0;
- if (random_bind(statp->_vcsock,nsap->sa_family) < 0) {
- *terrno = errno;
- Aerror(statp, stderr, "bind/vc", errno, nsap,
- nsaplen);
- res_nclose(statp);
- return (0);
- }
- if (connect_with_timeout(statp->_vcsock, nsap, (socklen_t)nsaplen,
- get_timeout(statp, params, ns)) < 0) {
- *terrno = errno;
- Aerror(statp, stderr, "connect/vc", errno, nsap,
- nsaplen);
- res_nclose(statp);
- /*
- * The way connect_with_timeout() is implemented prevents us from reliably
- * determining whether this was really a timeout or e.g. ECONNREFUSED. Since
- * currently both cases are handled in the same way, there is no need to
- * change this (yet). If we ever need to reliably distinguish between these
- * cases, both connect_with_timeout() and retrying_poll() need to be
- * modified, though.
- */
- *rcode = RCODE_TIMEOUT;
- return (0);
- }
- statp->_flags |= RES_F_VC;
- }
+ case EAFNOSUPPORT:
+ Perror(statp, stderr, "socket(vc)", errno);
+ return (0);
+ default:
+ *terrno = errno;
+ Perror(statp, stderr, "socket(vc)", errno);
+ return (-1);
+ }
+ }
+ fchown(statp->_vcsock, AID_DNS, -1);
+ if (statp->_mark != MARK_UNSET) {
+ if (setsockopt(statp->_vcsock, SOL_SOCKET, SO_MARK, &statp->_mark,
+ sizeof(statp->_mark)) < 0) {
+ *terrno = errno;
+ Perror(statp, stderr, "setsockopt", errno);
+ return -1;
+ }
+ }
+ errno = 0;
+ if (random_bind(statp->_vcsock, nsap->sa_family) < 0) {
+ *terrno = errno;
+ Aerror(statp, stderr, "bind/vc", errno, nsap, nsaplen);
+ res_nclose(statp);
+ return (0);
+ }
+ if (connect_with_timeout(statp->_vcsock, nsap, (socklen_t) nsaplen,
+ get_timeout(statp, params, ns)) < 0) {
+ *terrno = errno;
+ Aerror(statp, stderr, "connect/vc", errno, nsap, nsaplen);
+ res_nclose(statp);
+ /*
+ * The way connect_with_timeout() is implemented prevents us from reliably
+ * determining whether this was really a timeout or e.g. ECONNREFUSED. Since
+ * currently both cases are handled in the same way, there is no need to
+ * change this (yet). If we ever need to reliably distinguish between these
+ * cases, both connect_with_timeout() and retrying_poll() need to be
+ * modified, though.
+ */
+ *rcode = RCODE_TIMEOUT;
+ return (0);
+ }
+ statp->_flags |= RES_F_VC;
+ }
- /*
- * Send length & message
- */
- ns_put16((u_short)buflen, (u_char*)(void *)&len);
- iov[0] = evConsIovec(&len, INT16SZ);
- DE_CONST(buf, tmp);
- iov[1] = evConsIovec(tmp, (size_t)buflen);
- if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
- *terrno = errno;
- Perror(statp, stderr, "write failed", errno);
- res_nclose(statp);
- return (0);
- }
- /*
- * Receive length & response
- */
- read_len:
- cp = ans;
- len = INT16SZ;
- while ((n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0) {
- cp += n;
- if ((len -= n) == 0)
- break;
- }
- if (n <= 0) {
- *terrno = errno;
- Perror(statp, stderr, "read failed", errno);
- res_nclose(statp);
- /*
- * A long running process might get its TCP
- * connection reset if the remote server was
- * restarted. Requery the server instead of
- * trying a new one. When there is only one
- * server, this means that a query might work
- * instead of failing. We only allow one reset
- * per query to prevent looping.
- */
- if (*terrno == ECONNRESET && !connreset) {
- connreset = 1;
- res_nclose(statp);
- goto same_ns;
- }
- res_nclose(statp);
- return (0);
- }
- resplen = ns_get16(ans);
- if (resplen > anssiz) {
- Dprint(statp->options & RES_DEBUG,
- (stdout, ";; response truncated\n")
- );
- truncating = 1;
- len = anssiz;
- } else
- len = resplen;
- if (len < HFIXEDSZ) {
- /*
- * Undersized message.
- */
- Dprint(statp->options & RES_DEBUG,
- (stdout, ";; undersized: %d\n", len));
- *terrno = EMSGSIZE;
- res_nclose(statp);
- return (0);
- }
- cp = ans;
- while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0){
- cp += n;
- len -= n;
- }
- if (n <= 0) {
- *terrno = errno;
- Perror(statp, stderr, "read(vc)", errno);
- res_nclose(statp);
- return (0);
- }
+ /*
+ * Send length & message
+ */
+ ns_put16((u_short) buflen, (u_char*) (void*) &len);
+ iov[0] = evConsIovec(&len, INT16SZ);
+ DE_CONST(buf, tmp);
+ iov[1] = evConsIovec(tmp, (size_t) buflen);
+ if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
+ *terrno = errno;
+ Perror(statp, stderr, "write failed", errno);
+ res_nclose(statp);
+ return (0);
+ }
+ /*
+ * Receive length & response
+ */
+read_len:
+ cp = ans;
+ len = INT16SZ;
+ while ((n = read(statp->_vcsock, (char*) cp, (size_t) len)) > 0) {
+ cp += n;
+ if ((len -= n) == 0) break;
+ }
+ if (n <= 0) {
+ *terrno = errno;
+ Perror(statp, stderr, "read failed", errno);
+ res_nclose(statp);
+ /*
+ * A long running process might get its TCP
+ * connection reset if the remote server was
+ * restarted. Requery the server instead of
+ * trying a new one. When there is only one
+ * server, this means that a query might work
+ * instead of failing. We only allow one reset
+ * per query to prevent looping.
+ */
+ if (*terrno == ECONNRESET && !connreset) {
+ connreset = 1;
+ res_nclose(statp);
+ goto same_ns;
+ }
+ res_nclose(statp);
+ return (0);
+ }
+ resplen = ns_get16(ans);
+ if (resplen > anssiz) {
+ Dprint(statp->options & RES_DEBUG, (stdout, ";; response truncated\n"));
+ truncating = 1;
+ len = anssiz;
+ } else
+ len = resplen;
+ if (len < HFIXEDSZ) {
+ /*
+ * Undersized message.
+ */
+ Dprint(statp->options & RES_DEBUG, (stdout, ";; undersized: %d\n", len));
+ *terrno = EMSGSIZE;
+ res_nclose(statp);
+ return (0);
+ }
+ cp = ans;
+ while (len != 0 && (n = read(statp->_vcsock, (char*) cp, (size_t) len)) > 0) {
+ cp += n;
+ len -= n;
+ }
+ if (n <= 0) {
+ *terrno = errno;
+ Perror(statp, stderr, "read(vc)", errno);
+ res_nclose(statp);
+ return (0);
+ }
- if (truncating) {
- /*
- * Flush rest of answer so connection stays in synch.
- */
- anhp->tc = 1;
- len = resplen - anssiz;
- while (len != 0) {
- char junk[PACKETSZ];
+ if (truncating) {
+ /*
+ * Flush rest of answer so connection stays in synch.
+ */
+ anhp->tc = 1;
+ len = resplen - anssiz;
+ while (len != 0) {
+ char junk[PACKETSZ];
- n = read(statp->_vcsock, junk,
- (len > sizeof junk) ? sizeof junk : len);
- if (n > 0)
- len -= n;
- else
- break;
- }
- }
- /*
- * If the calling applicating has bailed out of
- * a previous call and failed to arrange to have
- * the circuit closed or the server has got
- * itself confused, then drop the packet and
- * wait for the correct one.
- */
- if (hp->id != anhp->id) {
- DprintQ((statp->options & RES_DEBUG) ||
- (statp->pfcode & RES_PRF_REPLY),
- (stdout, ";; old answer (unexpected):\n"),
- ans, (resplen > anssiz) ? anssiz: resplen);
- goto read_len;
- }
+ n = read(statp->_vcsock, junk, (len > sizeof junk) ? sizeof junk : len);
+ if (n > 0)
+ len -= n;
+ else
+ break;
+ }
+ }
+ /*
+ * If the calling applicating has bailed out of
+ * a previous call and failed to arrange to have
+ * the circuit closed or the server has got
+ * itself confused, then drop the packet and
+ * wait for the correct one.
+ */
+ if (hp->id != anhp->id) {
+ DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY),
+ (stdout, ";; old answer (unexpected):\n"), ans,
+ (resplen > anssiz) ? anssiz : resplen);
+ goto read_len;
+ }
- /*
- * All is well, or the error is fatal. Signal that the
- * next nameserver ought not be tried.
- */
- if (resplen > 0) {
- struct timespec done = evNowTime();
- *delay = _res_stats_calculate_rtt(&done, &now);
- *rcode = anhp->rcode;
- }
- return (resplen);
+ /*
+ * All is well, or the error is fatal. Signal that the
+ * next nameserver ought not be tried.
+ */
+ if (resplen > 0) {
+ struct timespec done = evNowTime();
+ *delay = _res_stats_calculate_rtt(&done, &now);
+ *rcode = anhp->rcode;
+ }
+ return (resplen);
}
/* return -1 on error (errno set), 0 on success */
-static int
-connect_with_timeout(int sock, const struct sockaddr *nsap, socklen_t salen,
- const struct timespec timeout)
-{
- int res, origflags;
+static int connect_with_timeout(int sock, const struct sockaddr* nsap, socklen_t salen,
+ const struct timespec timeout) {
+ int res, origflags;
- origflags = fcntl(sock, F_GETFL, 0);
- fcntl(sock, F_SETFL, origflags | O_NONBLOCK);
+ origflags = fcntl(sock, F_GETFL, 0);
+ fcntl(sock, F_SETFL, origflags | O_NONBLOCK);
- res = __connect(sock, nsap, salen);
- if (res < 0 && errno != EINPROGRESS) {
- res = -1;
- goto done;
- }
- if (res != 0) {
- struct timespec now = evNowTime();
- struct timespec finish = evAddTime(now, timeout);
- if (DBG) {
- async_safe_format_log(ANDROID_LOG_DEBUG, "libc", " %d send_vc\n", sock);
- }
+ res = __connect(sock, nsap, salen);
+ if (res < 0 && errno != EINPROGRESS) {
+ res = -1;
+ goto done;
+ }
+ if (res != 0) {
+ struct timespec now = evNowTime();
+ struct timespec finish = evAddTime(now, timeout);
+ if (DBG) {
+ async_safe_format_log(ANDROID_LOG_DEBUG, "libc", " %d send_vc\n", sock);
+ }
- res = retrying_poll(sock, POLLIN | POLLOUT, &finish);
- if (res <= 0) {
- res = -1;
- }
- }
+ res = retrying_poll(sock, POLLIN | POLLOUT, &finish);
+ if (res <= 0) {
+ res = -1;
+ }
+ }
done:
- fcntl(sock, F_SETFL, origflags);
- if (DBG) {
- async_safe_format_log(ANDROID_LOG_DEBUG, "libc",
- " %d connect_with_const timeout returning %d\n", sock, res);
- }
- return res;
+ fcntl(sock, F_SETFL, origflags);
+ if (DBG) {
+ async_safe_format_log(ANDROID_LOG_DEBUG, "libc",
+ " %d connect_with_const timeout returning %d\n", sock, res);
+ }
+ return res;
}
-static int
-retrying_poll(const int sock, const short events, const struct timespec* finish) {
- struct timespec now, timeout;
+static int retrying_poll(const int sock, const short events, const struct timespec* finish) {
+ struct timespec now, timeout;
retry:
- if (DBG) {
- async_safe_format_log(ANDROID_LOG_DEBUG, "libc", " %d retrying_poll\n", sock);
- }
+ if (DBG) {
+ async_safe_format_log(ANDROID_LOG_DEBUG, "libc", " %d retrying_poll\n", sock);
+ }
- now = evNowTime();
- if (evCmpTime(*finish, now) > 0)
- timeout = evSubTime(*finish, now);
- else
- timeout = evConsTime(0L, 0L);
- struct pollfd fds = { .fd = sock, .events = events };
- int n = ppoll(&fds, 1, &timeout, /*sigmask=*/NULL);
- if (n == 0) {
- if (DBG) {
- async_safe_format_log(ANDROID_LOG_DEBUG, "libc",
- " %d retrying_poll timeout\n", sock);
- }
- errno = ETIMEDOUT;
- return 0;
- }
- if (n < 0) {
- if (errno == EINTR)
- goto retry;
- if (DBG) {
- async_safe_format_log(ANDROID_LOG_DEBUG, "libc",
- " %d retrying_poll got error %d\n",sock, n);
- }
- return n;
- }
- if (fds.revents & (POLLIN | POLLOUT | POLLERR)) {
- int error;
- socklen_t len = sizeof(error);
- if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) {
- errno = error;
- if (DBG) {
- async_safe_format_log(ANDROID_LOG_DEBUG, "libc",
- " %d retrying_poll dot error2 %d\n", sock, errno);
- }
+ now = evNowTime();
+ if (evCmpTime(*finish, now) > 0)
+ timeout = evSubTime(*finish, now);
+ else
+ timeout = evConsTime(0L, 0L);
+ struct pollfd fds = {.fd = sock, .events = events};
+ int n = ppoll(&fds, 1, &timeout, /*sigmask=*/NULL);
+ if (n == 0) {
+ if (DBG) {
+ async_safe_format_log(ANDROID_LOG_DEBUG, "libc", " %d retrying_poll timeout\n", sock);
+ }
+ errno = ETIMEDOUT;
+ return 0;
+ }
+ if (n < 0) {
+ if (errno == EINTR) goto retry;
+ if (DBG) {
+ async_safe_format_log(ANDROID_LOG_DEBUG, "libc", " %d retrying_poll got error %d\n",
+ sock, n);
+ }
+ return n;
+ }
+ if (fds.revents & (POLLIN | POLLOUT | POLLERR)) {
+ int error;
+ socklen_t len = sizeof(error);
+ if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) {
+ errno = error;
+ if (DBG) {
+ async_safe_format_log(ANDROID_LOG_DEBUG, "libc",
+ " %d retrying_poll dot error2 %d\n", sock, errno);
+ }
- return -1;
- }
- }
- if (DBG) {
- async_safe_format_log(ANDROID_LOG_DEBUG, "libc",
- " %d retrying_poll returning %d\n",sock, n);
- }
+ return -1;
+ }
+ }
+ if (DBG) {
+ async_safe_format_log(ANDROID_LOG_DEBUG, "libc", " %d retrying_poll returning %d\n", sock,
+ n);
+ }
- return n;
+ return n;
}
-static int
-send_dg(res_state statp, struct __res_params* params,
- const u_char *buf, int buflen, u_char *ans, int anssiz,
- int *terrno, int ns, int *v_circuit, int *gotsomewhere,
- time_t *at, int *rcode, int* delay)
-{
- *at = time(NULL);
- *rcode = RCODE_INTERNAL_ERROR;
- *delay = 0;
- const HEADER *hp = (const HEADER *)(const void *)buf;
- HEADER *anhp = (HEADER *)(void *)ans;
- const struct sockaddr *nsap;
- int nsaplen;
- struct timespec now, timeout, finish, done;
- struct sockaddr_storage from;
- socklen_t fromlen;
- int resplen, n, s;
+static int send_dg(res_state statp, struct __res_params* params, const u_char* buf, int buflen,
+ u_char* ans, int anssiz, int* terrno, int ns, int* v_circuit, int* gotsomewhere,
+ time_t* at, int* rcode, int* delay) {
+ *at = time(NULL);
+ *rcode = RCODE_INTERNAL_ERROR;
+ *delay = 0;
+ const HEADER* hp = (const HEADER*) (const void*) buf;
+ HEADER* anhp = (HEADER*) (void*) ans;
+ const struct sockaddr* nsap;
+ int nsaplen;
+ struct timespec now, timeout, finish, done;
+ struct sockaddr_storage from;
+ socklen_t fromlen;
+ int resplen, n, s;
- nsap = get_nsaddr(statp, (size_t)ns);
- nsaplen = get_salen(nsap);
- if (EXT(statp).nssocks[ns] == -1) {
- EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
- if (EXT(statp).nssocks[ns] < 0) {
- switch (errno) {
- case EPROTONOSUPPORT:
+ nsap = get_nsaddr(statp, (size_t) ns);
+ nsaplen = get_salen(nsap);
+ if (EXT(statp).nssocks[ns] == -1) {
+ EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+ if (EXT(statp).nssocks[ns] < 0) {
+ switch (errno) {
+ case EPROTONOSUPPORT:
#ifdef EPFNOSUPPORT
- case EPFNOSUPPORT:
+ case EPFNOSUPPORT:
#endif
- case EAFNOSUPPORT:
- Perror(statp, stderr, "socket(dg)", errno);
- return (0);
- default:
- *terrno = errno;
- Perror(statp, stderr, "socket(dg)", errno);
- return (-1);
- }
- }
+ case EAFNOSUPPORT:
+ Perror(statp, stderr, "socket(dg)", errno);
+ return (0);
+ default:
+ *terrno = errno;
+ Perror(statp, stderr, "socket(dg)", errno);
+ return (-1);
+ }
+ }
- fchown(EXT(statp).nssocks[ns], AID_DNS, -1);
- if (statp->_mark != MARK_UNSET) {
- if (setsockopt(EXT(statp).nssocks[ns], SOL_SOCKET,
- SO_MARK, &(statp->_mark), sizeof(statp->_mark)) < 0) {
- res_nclose(statp);
- return -1;
- }
- }
+ fchown(EXT(statp).nssocks[ns], AID_DNS, -1);
+ if (statp->_mark != MARK_UNSET) {
+ if (setsockopt(EXT(statp).nssocks[ns], SOL_SOCKET, SO_MARK, &(statp->_mark),
+ sizeof(statp->_mark)) < 0) {
+ res_nclose(statp);
+ return -1;
+ }
+ }
#ifndef CANNOT_CONNECT_DGRAM
- /*
- * On a 4.3BSD+ machine (client and server,
- * actually), sending to a nameserver datagram
- * port with no nameserver will cause an
- * ICMP port unreachable message to be returned.
- * If our datagram socket is "connected" to the
- * server, we get an ECONNREFUSED error on the next
- * socket operation, and select returns if the
- * error message is received. We can thus detect
- * the absence of a nameserver without timing out.
- */
- if (random_bind(EXT(statp).nssocks[ns], nsap->sa_family) < 0) {
- Aerror(statp, stderr, "bind(dg)", errno, nsap,
- nsaplen);
- res_nclose(statp);
- return (0);
- }
- if (__connect(EXT(statp).nssocks[ns], nsap, (socklen_t)nsaplen) < 0) {
- Aerror(statp, stderr, "connect(dg)", errno, nsap,
- nsaplen);
- res_nclose(statp);
- return (0);
- }
+ /*
+ * On a 4.3BSD+ machine (client and server,
+ * actually), sending to a nameserver datagram
+ * port with no nameserver will cause an
+ * ICMP port unreachable message to be returned.
+ * If our datagram socket is "connected" to the
+ * server, we get an ECONNREFUSED error on the next
+ * socket operation, and select returns if the
+ * error message is received. We can thus detect
+ * the absence of a nameserver without timing out.
+ */
+ if (random_bind(EXT(statp).nssocks[ns], nsap->sa_family) < 0) {
+ Aerror(statp, stderr, "bind(dg)", errno, nsap, nsaplen);
+ res_nclose(statp);
+ return (0);
+ }
+ if (__connect(EXT(statp).nssocks[ns], nsap, (socklen_t) nsaplen) < 0) {
+ Aerror(statp, stderr, "connect(dg)", errno, nsap, nsaplen);
+ res_nclose(statp);
+ return (0);
+ }
#endif /* !CANNOT_CONNECT_DGRAM */
- Dprint(statp->options & RES_DEBUG,
- (stdout, ";; new DG socket\n"))
-
- }
- s = EXT(statp).nssocks[ns];
+ Dprint(statp->options & RES_DEBUG, (stdout, ";; new DG socket\n"))
+ }
+ s = EXT(statp).nssocks[ns];
#ifndef CANNOT_CONNECT_DGRAM
- if (send(s, (const char*)buf, (size_t)buflen, 0) != buflen) {
- Perror(statp, stderr, "send", errno);
- res_nclose(statp);
- return (0);
- }
-#else /* !CANNOT_CONNECT_DGRAM */
- if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
- {
- Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
- res_nclose(statp);
- return (0);
- }
+ if (send(s, (const char*) buf, (size_t) buflen, 0) != buflen) {
+ Perror(statp, stderr, "send", errno);
+ res_nclose(statp);
+ return (0);
+ }
+#else /* !CANNOT_CONNECT_DGRAM */
+ if (sendto(s, (const char*) buf, buflen, 0, nsap, nsaplen) != buflen) {
+ Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
+ res_nclose(statp);
+ return (0);
+ }
#endif /* !CANNOT_CONNECT_DGRAM */
- /*
- * Wait for reply.
- */
- timeout = get_timeout(statp, params, ns);
- now = evNowTime();
- finish = evAddTime(now, timeout);
+ /*
+ * Wait for reply.
+ */
+ timeout = get_timeout(statp, params, ns);
+ now = evNowTime();
+ finish = evAddTime(now, timeout);
retry:
- n = retrying_poll(s, POLLIN, &finish);
+ n = retrying_poll(s, POLLIN, &finish);
- if (n == 0) {
- *rcode = RCODE_TIMEOUT;
- Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
- *gotsomewhere = 1;
- return (0);
- }
- if (n < 0) {
- Perror(statp, stderr, "poll", errno);
- res_nclose(statp);
- return (0);
- }
- errno = 0;
- fromlen = sizeof(from);
- resplen = recvfrom(s, (char*)ans, (size_t)anssiz,0,
- (struct sockaddr *)(void *)&from, &fromlen);
- if (resplen <= 0) {
- Perror(statp, stderr, "recvfrom", errno);
- res_nclose(statp);
- return (0);
- }
- *gotsomewhere = 1;
- if (resplen < HFIXEDSZ) {
- /*
- * Undersized message.
- */
- Dprint(statp->options & RES_DEBUG,
- (stdout, ";; undersized: %d\n",
- resplen));
- *terrno = EMSGSIZE;
- res_nclose(statp);
- return (0);
- }
- if (hp->id != anhp->id) {
- /*
- * response from old query, ignore it.
- * XXX - potential security hazard could
- * be detected here.
- */
- DprintQ((statp->options & RES_DEBUG) ||
- (statp->pfcode & RES_PRF_REPLY),
- (stdout, ";; old answer:\n"),
- ans, (resplen > anssiz) ? anssiz : resplen);
- goto retry;
- }
- if (!(statp->options & RES_INSECURE1) &&
- !res_ourserver_p(statp, (struct sockaddr *)(void *)&from)) {
- /*
- * response from wrong server? ignore it.
- * XXX - potential security hazard could
- * be detected here.
- */
- DprintQ((statp->options & RES_DEBUG) ||
- (statp->pfcode & RES_PRF_REPLY),
- (stdout, ";; not our server:\n"),
- ans, (resplen > anssiz) ? anssiz : resplen);
- goto retry;
- }
+ if (n == 0) {
+ *rcode = RCODE_TIMEOUT;
+ Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
+ *gotsomewhere = 1;
+ return (0);
+ }
+ if (n < 0) {
+ Perror(statp, stderr, "poll", errno);
+ res_nclose(statp);
+ return (0);
+ }
+ errno = 0;
+ fromlen = sizeof(from);
+ resplen = recvfrom(s, (char*) ans, (size_t) anssiz, 0, (struct sockaddr*) (void*) &from,
+ &fromlen);
+ if (resplen <= 0) {
+ Perror(statp, stderr, "recvfrom", errno);
+ res_nclose(statp);
+ return (0);
+ }
+ *gotsomewhere = 1;
+ if (resplen < HFIXEDSZ) {
+ /*
+ * Undersized message.
+ */
+ Dprint(statp->options & RES_DEBUG, (stdout, ";; undersized: %d\n", resplen));
+ *terrno = EMSGSIZE;
+ res_nclose(statp);
+ return (0);
+ }
+ if (hp->id != anhp->id) {
+ /*
+ * response from old query, ignore it.
+ * XXX - potential security hazard could
+ * be detected here.
+ */
+ DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY),
+ (stdout, ";; old answer:\n"), ans, (resplen > anssiz) ? anssiz : resplen);
+ goto retry;
+ }
+ if (!(statp->options & RES_INSECURE1) &&
+ !res_ourserver_p(statp, (struct sockaddr*) (void*) &from)) {
+ /*
+ * response from wrong server? ignore it.
+ * XXX - potential security hazard could
+ * be detected here.
+ */
+ DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY),
+ (stdout, ";; not our server:\n"), ans, (resplen > anssiz) ? anssiz : resplen);
+ goto retry;
+ }
#ifdef RES_USE_EDNS0
- if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
- /*
- * Do not retry if the server do not understand EDNS0.
- * The case has to be captured here, as FORMERR packet do not
- * carry query section, hence res_queriesmatch() returns 0.
- */
- DprintQ(statp->options & RES_DEBUG,
- (stdout, "server rejected query with EDNS0:\n"),
- ans, (resplen > anssiz) ? anssiz : resplen);
- /* record the error */
- statp->_flags |= RES_F_EDNS0ERR;
- res_nclose(statp);
- return (0);
- }
+ if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
+ /*
+ * Do not retry if the server do not understand EDNS0.
+ * The case has to be captured here, as FORMERR packet do not
+ * carry query section, hence res_queriesmatch() returns 0.
+ */
+ DprintQ(statp->options & RES_DEBUG, (stdout, "server rejected query with EDNS0:\n"), ans,
+ (resplen > anssiz) ? anssiz : resplen);
+ /* record the error */
+ statp->_flags |= RES_F_EDNS0ERR;
+ res_nclose(statp);
+ return (0);
+ }
#endif
- if (!(statp->options & RES_INSECURE2) &&
- !res_queriesmatch(buf, buf + buflen,
- ans, ans + anssiz)) {
- /*
- * response contains wrong query? ignore it.
- * XXX - potential security hazard could
- * be detected here.
- */
- DprintQ((statp->options & RES_DEBUG) ||
- (statp->pfcode & RES_PRF_REPLY),
- (stdout, ";; wrong query name:\n"),
- ans, (resplen > anssiz) ? anssiz : resplen);
- goto retry;;
- }
- done = evNowTime();
- *delay = _res_stats_calculate_rtt(&done, &now);
- if (anhp->rcode == SERVFAIL ||
- anhp->rcode == NOTIMP ||
- anhp->rcode == REFUSED) {
- DprintQ(statp->options & RES_DEBUG,
- (stdout, "server rejected query:\n"),
- ans, (resplen > anssiz) ? anssiz : resplen);
- res_nclose(statp);
- /* don't retry if called from dig */
- if (!statp->pfcode) {
- *rcode = anhp->rcode;
- return (0);
- }
- }
- if (!(statp->options & RES_IGNTC) && anhp->tc) {
- /*
- * To get the rest of answer,
- * use TCP with same server.
- */
- Dprint(statp->options & RES_DEBUG,
- (stdout, ";; truncated answer\n"));
- *v_circuit = 1;
- res_nclose(statp);
- return (1);
- }
- /*
- * All is well, or the error is fatal. Signal that the
- * next nameserver ought not be tried.
- */
- if (resplen > 0) {
- *rcode = anhp->rcode;
- }
- return (resplen);
+ if (!(statp->options & RES_INSECURE2) &&
+ !res_queriesmatch(buf, buf + buflen, ans, ans + anssiz)) {
+ /*
+ * response contains wrong query? ignore it.
+ * XXX - potential security hazard could
+ * be detected here.
+ */
+ DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY),
+ (stdout, ";; wrong query name:\n"), ans, (resplen > anssiz) ? anssiz : resplen);
+ goto retry;
+ ;
+ }
+ done = evNowTime();
+ *delay = _res_stats_calculate_rtt(&done, &now);
+ if (anhp->rcode == SERVFAIL || anhp->rcode == NOTIMP || anhp->rcode == REFUSED) {
+ DprintQ(statp->options & RES_DEBUG, (stdout, "server rejected query:\n"), ans,
+ (resplen > anssiz) ? anssiz : resplen);
+ res_nclose(statp);
+ /* don't retry if called from dig */
+ if (!statp->pfcode) {
+ *rcode = anhp->rcode;
+ return (0);
+ }
+ }
+ if (!(statp->options & RES_IGNTC) && anhp->tc) {
+ /*
+ * To get the rest of answer,
+ * use TCP with same server.
+ */
+ Dprint(statp->options & RES_DEBUG, (stdout, ";; truncated answer\n"));
+ *v_circuit = 1;
+ res_nclose(statp);
+ return (1);
+ }
+ /*
+ * All is well, or the error is fatal. Signal that the
+ * next nameserver ought not be tried.
+ */
+ if (resplen > 0) {
+ *rcode = anhp->rcode;
+ }
+ return (resplen);
}
-static void
-Aerror(const res_state statp, FILE *file, const char *string, int error,
- const struct sockaddr *address, int alen)
-{
- int save = errno;
- char hbuf[NI_MAXHOST];
- char sbuf[NI_MAXSERV];
+static void Aerror(const res_state statp, FILE* file, const char* string, int error,
+ const struct sockaddr* address, int alen) {
+ int save = errno;
+ char hbuf[NI_MAXHOST];
+ char sbuf[NI_MAXSERV];
- if ((statp->options & RES_DEBUG) != 0U) {
- if (getnameinfo(address, (socklen_t)alen, hbuf, sizeof(hbuf),
- sbuf, sizeof(sbuf), niflags)) {
- strncpy(hbuf, "?", sizeof(hbuf) - 1);
- hbuf[sizeof(hbuf) - 1] = '\0';
- strncpy(sbuf, "?", sizeof(sbuf) - 1);
- sbuf[sizeof(sbuf) - 1] = '\0';
- }
- fprintf(file, "res_send: %s ([%s].%s): %s\n",
- string, hbuf, sbuf, strerror(error));
- }
- errno = save;
+ if ((statp->options & RES_DEBUG) != 0U) {
+ if (getnameinfo(address, (socklen_t) alen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
+ niflags)) {
+ strncpy(hbuf, "?", sizeof(hbuf) - 1);
+ hbuf[sizeof(hbuf) - 1] = '\0';
+ strncpy(sbuf, "?", sizeof(sbuf) - 1);
+ sbuf[sizeof(sbuf) - 1] = '\0';
+ }
+ fprintf(file, "res_send: %s ([%s].%s): %s\n", string, hbuf, sbuf, strerror(error));
+ }
+ errno = save;
}
-static void
-Perror(const res_state statp, FILE *file, const char *string, int error) {
- int save = errno;
+static void Perror(const res_state statp, FILE* file, const char* string, int error) {
+ int save = errno;
- if ((statp->options & RES_DEBUG) != 0U)
- fprintf(file, "res_send: %s: %s\n",
- string, strerror(error));
- errno = save;
+ if ((statp->options & RES_DEBUG) != 0U)
+ fprintf(file, "res_send: %s: %s\n", string, strerror(error));
+ errno = save;
}
-static int
-sock_eq(struct sockaddr *a, struct sockaddr *b) {
- struct sockaddr_in *a4, *b4;
- struct sockaddr_in6 *a6, *b6;
+static int sock_eq(struct sockaddr* a, struct sockaddr* b) {
+ struct sockaddr_in *a4, *b4;
+ struct sockaddr_in6 *a6, *b6;
- if (a->sa_family != b->sa_family)
- return 0;
- switch (a->sa_family) {
- case AF_INET:
- a4 = (struct sockaddr_in *)(void *)a;
- b4 = (struct sockaddr_in *)(void *)b;
- return a4->sin_port == b4->sin_port &&
- a4->sin_addr.s_addr == b4->sin_addr.s_addr;
- case AF_INET6:
- a6 = (struct sockaddr_in6 *)(void *)a;
- b6 = (struct sockaddr_in6 *)(void *)b;
- return a6->sin6_port == b6->sin6_port &&
+ if (a->sa_family != b->sa_family) return 0;
+ switch (a->sa_family) {
+ case AF_INET:
+ a4 = (struct sockaddr_in*) (void*) a;
+ b4 = (struct sockaddr_in*) (void*) b;
+ return a4->sin_port == b4->sin_port && a4->sin_addr.s_addr == b4->sin_addr.s_addr;
+ case AF_INET6:
+ a6 = (struct sockaddr_in6*) (void*) a;
+ b6 = (struct sockaddr_in6*) (void*) b;
+ return a6->sin6_port == b6->sin6_port &&
#ifdef HAVE_SIN6_SCOPE_ID
- a6->sin6_scope_id == b6->sin6_scope_id &&
+ a6->sin6_scope_id == b6->sin6_scope_id &&
#endif
- IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
- default:
- return 0;
- }
+ IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
+ default:
+ return 0;
+ }
}
diff --git a/resolv/res_state.c b/resolv/res_state.c
index 94124ff..be349fb 100644
--- a/resolv/res_state.c
+++ b/resolv/res_state.c
@@ -25,40 +25,39 @@
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
-#include <sys/cdefs.h>
-#include <sys/types.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <netdb.h>
-#include "resolv_private.h"
-#include "resolv_cache.h"
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
-
+#include <sys/cdefs.h>
+#include <sys/types.h>
+#include "resolv_cache.h"
+#include "resolv_private.h"
/* Set to 1 to enable debug traces */
#define DEBUG 0
#if DEBUG
-# include <async_safe/log.h>
-# include <unistd.h> /* for gettid() */
-# define D(...) async_safe_format_log(ANDROID_LOG_DEBUG,"libc", __VA_ARGS__)
+#include <async_safe/log.h>
+#include <unistd.h> /* for gettid() */
+#define D(...) async_safe_format_log(ANDROID_LOG_DEBUG, "libc", __VA_ARGS__)
#else
-# define D(...) do{}while(0)
+#define D(...) \
+ do { \
+ } while (0)
#endif
typedef struct {
- int _h_errno;
+ int _h_errno;
// TODO: Have one __res_state per network so we don't have to repopulate frequently.
- struct __res_state _nres[1];
- struct res_static _rstatic[1];
+ struct __res_state _nres[1];
+ struct res_static _rstatic[1];
} _res_thread;
-static _res_thread*
-_res_thread_alloc(void)
-{
- _res_thread* rt = calloc(1, sizeof(*rt));
+static _res_thread* _res_thread_alloc(void) {
+ _res_thread* rt = calloc(1, sizeof(*rt));
if (rt) {
rt->_h_errno = 0;
@@ -67,24 +66,20 @@
return rt;
}
-static void
-_res_static_done( res_static rs )
-{
+static void _res_static_done(res_static rs) {
/* fortunately, there is nothing to do here, since the
* points in h_addr_ptrs and host_aliases should all
* point to 'hostbuf'
*/
- if (rs->hostf) { /* should not happen in theory, but just be safe */
+ if (rs->hostf) { /* should not happen in theory, but just be safe */
fclose(rs->hostf);
rs->hostf = NULL;
}
free(rs->servent.s_aliases);
}
-static void
-_res_thread_free( void* _rt )
-{
- _res_thread* rt = _rt;
+static void _res_thread_free(void* _rt) {
+ _res_thread* rt = _rt;
D("%s: rt=%p for thread=%d", __FUNCTION__, rt, gettid());
@@ -99,11 +94,9 @@
pthread_key_create(&_res_key, _res_thread_free);
}
-static _res_thread*
-_res_thread_get(void)
-{
- _res_thread* rt;
- rt = pthread_getspecific( _res_key );
+static _res_thread* _res_thread_get(void) {
+ _res_thread* rt;
+ rt = pthread_getspecific(_res_key);
if (rt != NULL) {
return rt;
@@ -115,21 +108,19 @@
if (rt == NULL) {
return NULL;
}
- pthread_setspecific( _res_key, rt );
- D("%s: tid=%d Created new DNS state rt=%p",
- __FUNCTION__, gettid(), rt);
+ pthread_setspecific(_res_key, rt);
+ D("%s: tid=%d Created new DNS state rt=%p", __FUNCTION__, gettid(), rt);
/* Reset the state, note that res_ninit() can now properly reset
* an existing state without leaking memory.
*/
- D("%s: tid=%d, rt=%p, setting DNS state (options RES_INIT=%d)",
- __FUNCTION__, gettid(), rt, (rt->_nres->options & RES_INIT) != 0);
- if ( res_ninit( rt->_nres ) < 0 ) {
+ D("%s: tid=%d, rt=%p, setting DNS state (options RES_INIT=%d)", __FUNCTION__, gettid(), rt,
+ (rt->_nres->options & RES_INIT) != 0);
+ if (res_ninit(rt->_nres) < 0) {
/* This should not happen */
- D("%s: tid=%d rt=%p, woot, res_ninit() returned < 0",
- __FUNCTION__, gettid(), rt);
+ D("%s: tid=%d rt=%p, woot, res_ninit() returned < 0", __FUNCTION__, gettid(), rt);
_res_thread_free(rt);
- pthread_setspecific( _res_key, NULL );
+ pthread_setspecific(_res_key, NULL);
return NULL;
}
return rt;
@@ -154,33 +145,25 @@
}
#endif
-int*
-__get_h_errno(void)
-{
- _res_thread* rt = _res_thread_get();
- static int panic = NETDB_INTERNAL;
+int* __get_h_errno(void) {
+ _res_thread* rt = _res_thread_get();
+ static int panic = NETDB_INTERNAL;
return rt ? &rt->_h_errno : &panic;
}
-res_state
-__res_get_state(void)
-{
- _res_thread* rt = _res_thread_get();
+res_state __res_get_state(void) {
+ _res_thread* rt = _res_thread_get();
return rt ? rt->_nres : NULL;
}
-void
-__res_put_state(res_state res __unused)
-{
+void __res_put_state(res_state res __unused) {
/* nothing to do */
}
-res_static
-__res_get_static(void)
-{
- _res_thread* rt = _res_thread_get();
+res_static __res_get_static(void) {
+ _res_thread* rt = _res_thread_get();
return rt ? rt->_rstatic : NULL;
}
diff --git a/resolv/res_stats.c b/resolv/res_stats.c
index 97d54d2..7ac5a4c 100644
--- a/resolv/res_stats.c
+++ b/resolv/res_stats.c
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-#include <stdbool.h>
#include <arpa/nameser.h>
+#include <stdbool.h>
#include <string.h>
#include <async_safe/log.h>
@@ -26,8 +26,7 @@
#define DBG 0
/* Calculate the round-trip-time from start time t0 and end time t1. */
-int
-_res_stats_calculate_rtt(const struct timespec* t1, const struct timespec* t0) {
+int _res_stats_calculate_rtt(const struct timespec* t1, const struct timespec* t0) {
// Divide ns by one million to get ms, multiply s by thousand to get ms (obvious)
long ms0 = t0->tv_sec * 1000 + t0->tv_nsec / 1000000;
long ms1 = t1->tv_sec * 1000 + t1->tv_nsec / 1000000;
@@ -35,9 +34,7 @@
}
/* Create a sample for calculating server reachability statistics. */
-void
-_res_stats_set_sample(struct __res_sample* sample, time_t now, int rcode, int rtt)
-{
+void _res_stats_set_sample(struct __res_sample* sample, time_t now, int rcode, int rtt) {
if (DBG) {
async_safe_format_log(ANDROID_LOG_INFO, "libc", "rcode = %d, sec = %d", rcode, rtt);
}
@@ -47,17 +44,14 @@
}
/* Clears all stored samples for the given server. */
-void
-_res_stats_clear_samples(struct __res_stats* stats)
-{
+void _res_stats_clear_samples(struct __res_stats* stats) {
stats->sample_count = stats->sample_next = 0;
}
/* Aggregates the reachability statistics for the given server based on on the stored samples. */
-void
-android_net_res_stats_aggregate(struct __res_stats* stats, int* successes, int* errors,
- int* timeouts, int* internal_errors, int* rtt_avg, time_t* last_sample_time)
-{
+void android_net_res_stats_aggregate(struct __res_stats* stats, int* successes, int* errors,
+ int* timeouts, int* internal_errors, int* rtt_avg,
+ time_t* last_sample_time) {
int s = 0; // successes
int e = 0; // errors
int t = 0; // timouts
@@ -65,7 +59,7 @@
long rtt_sum = 0;
time_t last = 0;
int rtt_count = 0;
- for (int i = 0 ; i < stats->sample_count ; ++i) {
+ for (int i = 0; i < stats->sample_count; ++i) {
// Treat everything as an error that the code in send_dg() already considers a
// rejection by the server, i.e. SERVFAIL, NOTIMP and REFUSED. Assume that NXDOMAIN
// and NOTAUTH can actually occur for user queries. NOERROR with empty answer section
@@ -74,25 +68,25 @@
// as an indication of a broken server is unclear, though. For now treat such responses,
// as well as unknown codes as errors.
switch (stats->samples[i].rcode) {
- case NOERROR:
- case NOTAUTH:
- case NXDOMAIN:
- ++s;
- rtt_sum += stats->samples[i].rtt;
- ++rtt_count;
- break;
- case RCODE_TIMEOUT:
- ++t;
- break;
- case RCODE_INTERNAL_ERROR:
- ++ie;
- break;
- case SERVFAIL:
- case NOTIMP:
- case REFUSED:
- default:
- ++e;
- break;
+ case NOERROR:
+ case NOTAUTH:
+ case NXDOMAIN:
+ ++s;
+ rtt_sum += stats->samples[i].rtt;
+ ++rtt_count;
+ break;
+ case RCODE_TIMEOUT:
+ ++t;
+ break;
+ case RCODE_INTERNAL_ERROR:
+ ++ie;
+ break;
+ case SERVFAIL:
+ case NOTIMP:
+ case REFUSED:
+ default:
+ ++e;
+ break;
}
}
*successes = s;
@@ -116,8 +110,7 @@
*last_sample_time = last;
}
-bool
-_res_stats_usable_server(const struct __res_params* params, struct __res_stats* stats) {
+bool _res_stats_usable_server(const struct __res_params* params, struct __res_stats* stats) {
int successes = -1;
int errors = -1;
int timeouts = -1;
@@ -125,13 +118,15 @@
int rtt_avg = -1;
time_t last_sample_time = 0;
android_net_res_stats_aggregate(stats, &successes, &errors, &timeouts, &internal_errors,
- &rtt_avg, &last_sample_time);
+ &rtt_avg, &last_sample_time);
if (successes >= 0 && errors >= 0 && timeouts >= 0) {
int total = successes + errors + timeouts;
if (DBG) {
- async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "NS stats: S %d + E %d + T %d + I %d "
- "= %d, rtt = %d, min_samples = %d\n", successes, errors, timeouts, internal_errors,
- total, rtt_avg, params->min_samples);
+ async_safe_format_log(ANDROID_LOG_DEBUG, "libc",
+ "NS stats: S %d + E %d + T %d + I %d "
+ "= %d, rtt = %d, min_samples = %d\n",
+ successes, errors, timeouts, internal_errors, total, rtt_avg,
+ params->min_samples);
}
if (total >= params->min_samples && (errors > 0 || timeouts > 0)) {
int success_rate = successes * 100 / total;
@@ -149,13 +144,13 @@
// previous non-circular state would induce additional complexity.
if (DBG) {
async_safe_format_log(ANDROID_LOG_INFO, "libc",
- "samples stale, retrying server\n");
+ "samples stale, retrying server\n");
}
_res_stats_clear_samples(stats);
} else {
if (DBG) {
async_safe_format_log(ANDROID_LOG_INFO, "libc",
- "too many resolution errors, ignoring server\n");
+ "too many resolution errors, ignoring server\n");
}
return 0;
}
@@ -165,9 +160,9 @@
return 1;
}
-void
-android_net_res_stats_get_usable_servers(const struct __res_params* params,
- struct __res_stats stats[], int nscount, bool usable_servers[]) {
+void android_net_res_stats_get_usable_servers(const struct __res_params* params,
+ struct __res_stats stats[], int nscount,
+ bool usable_servers[]) {
unsigned usable_servers_found = 0;
for (int ns = 0; ns < nscount; ns++) {
bool usable = _res_stats_usable_server(params, &stats[ns]);
diff --git a/resolv/resolv_cache.h b/resolv/resolv_cache.h
index e049d95..24fbf9b 100644
--- a/resolv/resolv_cache.h
+++ b/resolv/resolv_cache.h
@@ -40,37 +40,25 @@
extern void _resolv_populate_res_for_net(struct __res_state* statp);
typedef enum {
- RESOLV_CACHE_UNSUPPORTED, /* the cache can't handle that kind of queries */
- /* or the answer buffer is too small */
- RESOLV_CACHE_NOTFOUND, /* the cache doesn't know about this query */
- RESOLV_CACHE_FOUND /* the cache found the answer */
+ RESOLV_CACHE_UNSUPPORTED, /* the cache can't handle that kind of queries */
+ /* or the answer buffer is too small */
+ RESOLV_CACHE_NOTFOUND, /* the cache doesn't know about this query */
+ RESOLV_CACHE_FOUND /* the cache found the answer */
} ResolvCacheStatus;
__LIBC_HIDDEN__
-extern ResolvCacheStatus
-_resolv_cache_lookup( unsigned netid,
- const void* query,
- int querylen,
- void* answer,
- int answersize,
- int *answerlen );
+extern ResolvCacheStatus _resolv_cache_lookup(unsigned netid, const void* query, int querylen,
+ void* answer, int answersize, int* answerlen);
/* add a (query,answer) to the cache, only call if _resolv_cache_lookup
* did return RESOLV_CACHE_NOTFOUND
*/
__LIBC_HIDDEN__
-extern void
-_resolv_cache_add( unsigned netid,
- const void* query,
- int querylen,
- const void* answer,
- int answerlen );
+extern void _resolv_cache_add(unsigned netid, const void* query, int querylen, const void* answer,
+ int answerlen);
/* Notify the cache a request failed */
__LIBC_HIDDEN__
-extern void
-_resolv_cache_query_failed( unsigned netid,
- const void* query,
- int querylen);
+extern void _resolv_cache_query_failed(unsigned netid, const void* query, int querylen);
#endif /* _RESOLV_CACHE_H_ */
diff --git a/resolv/resolv_netid.h b/resolv/resolv_netid.h
index c7e2823..c92c374 100644
--- a/resolv/resolv_netid.h
+++ b/resolv/resolv_netid.h
@@ -32,10 +32,10 @@
* server selection. They are used by system/netd/ and should not be
* exposed by the C library's public NDK headers.
*/
-#include <sys/cdefs.h>
#include <netinet/in.h>
-#include "resolv_params.h"
#include <stdio.h>
+#include <sys/cdefs.h>
+#include "resolv_params.h"
/*
* Passing NETID_UNSET as the netId causes system/netd/server/DnsProxyListener.cpp to
@@ -53,7 +53,7 @@
struct __res_params;
struct addrinfo;
-#define __used_in_netd __attribute__((visibility ("default")))
+#define __used_in_netd __attribute__((visibility("default")))
/*
* A struct to capture context relevant to network operations.
@@ -76,27 +76,32 @@
res_send_qhook qhook;
};
-#define NET_CONTEXT_INVALID_UID ((uid_t)-1)
+#define NET_CONTEXT_INVALID_UID ((uid_t) -1)
-#define NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS 0x00000001
-#define NET_CONTEXT_FLAG_USE_EDNS 0x00000002
+#define NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS 0x00000001
+#define NET_CONTEXT_FLAG_USE_EDNS 0x00000002
-struct hostent *android_gethostbyaddrfornet(const void *, socklen_t, int, unsigned, unsigned) __used_in_netd;
-struct hostent *android_gethostbynamefornet(const char *, int, unsigned, unsigned) __used_in_netd;
-int android_getaddrinfofornet(const char *, const char *, const struct addrinfo *, unsigned,
- unsigned, struct addrinfo **) __used_in_netd;
+struct hostent* android_gethostbyaddrfornet(const void*, socklen_t, int, unsigned,
+ unsigned) __used_in_netd;
+struct hostent* android_gethostbynamefornet(const char*, int, unsigned, unsigned) __used_in_netd;
+int android_getaddrinfofornet(const char*, const char*, const struct addrinfo*, unsigned, unsigned,
+ struct addrinfo**) __used_in_netd;
/*
* TODO: consider refactoring android_getaddrinfo_proxy() to serve as an
* explore_fqdn() dispatch table method, with the below function only making DNS calls.
*/
-struct hostent *android_gethostbyaddrfornetcontext(const void *, socklen_t, int, const struct android_net_context *) __used_in_netd;
-struct hostent *android_gethostbynamefornetcontext(const char *, int, const struct android_net_context *) __used_in_netd;
-int android_getaddrinfofornetcontext(const char *, const char *, const struct addrinfo *,
- const struct android_net_context *, struct addrinfo **) __used_in_netd;
+struct hostent* android_gethostbyaddrfornetcontext(
+ const void*, socklen_t, int, const struct android_net_context*) __used_in_netd;
+struct hostent* android_gethostbynamefornetcontext(
+ const char*, int, const struct android_net_context*) __used_in_netd;
+int android_getaddrinfofornetcontext(const char*, const char*, const struct addrinfo*,
+ const struct android_net_context*,
+ struct addrinfo**) __used_in_netd;
/* set name servers for a network */
extern int _resolv_set_nameservers_for_net(unsigned netid, const char** servers,
- unsigned numservers, const char *domains, const struct __res_params* params) __used_in_netd;
+ unsigned numservers, const char* domains,
+ const struct __res_params* params) __used_in_netd;
/* flush the cache associated with a certain network */
extern void _resolv_flush_cache_for_net(unsigned netid) __used_in_netd;
@@ -105,8 +110,10 @@
extern void _resolv_delete_cache_for_net(unsigned netid) __used_in_netd;
/* Internal use only. */
-struct hostent *android_gethostbyaddrfornetcontext_proxy(const void *, socklen_t, int , const struct android_net_context *) __LIBC_HIDDEN__;
-int android_getnameinfofornet(const struct sockaddr *, socklen_t, char *, size_t, char *, size_t, int, unsigned, unsigned) __LIBC_HIDDEN__;
+struct hostent* android_gethostbyaddrfornetcontext_proxy(
+ const void*, socklen_t, int, const struct android_net_context*) __LIBC_HIDDEN__;
+int android_getnameinfofornet(const struct sockaddr*, socklen_t, char*, size_t, char*, size_t, int,
+ unsigned, unsigned) __LIBC_HIDDEN__;
FILE* android_open_proxy(void) __LIBC_HIDDEN__;
__END_DECLS
diff --git a/resolv/resolv_params.h b/resolv/resolv_params.h
index ecc1cc3..e2c7e48 100644
--- a/resolv/resolv_params.h
+++ b/resolv/resolv_params.h
@@ -20,43 +20,42 @@
#include <stdint.h>
/* Hard-coded defines */
-#define MAXNS 4 /* max # name servers we'll track */
-#define MAXDNSRCH 6 /* max # domains in search path */
-#define MAXDNSRCHPATH 256 /* max length of domain search paths */
-#define MAXNSSAMPLES 64 /* max # samples to store per server */
+#define MAXNS 4 /* max # name servers we'll track */
+#define MAXDNSRCH 6 /* max # domains in search path */
+#define MAXDNSRCHPATH 256 /* max length of domain search paths */
+#define MAXNSSAMPLES 64 /* max # samples to store per server */
/* Defaults used for initializing __res_params */
-#define SUCCESS_THRESHOLD 75 /* if successes * 100 / total_samples is less than
- * this value, the server is considered failing
- */
-#define NSSAMPLE_VALIDITY 1800 /* Sample validity in seconds.
- * Set to -1 to disable skipping failing
- * servers.
- */
+#define SUCCESS_THRESHOLD \
+ 75 /* if successes * 100 / total_samples is less than \
+ * this value, the server is considered failing \
+ */
+#define NSSAMPLE_VALIDITY \
+ 1800 /* Sample validity in seconds. \
+ * Set to -1 to disable skipping failing \
+ * servers. \
+ */
/* If EDNS0_PADDING is defined, queries will be padded to a multiple of this length
when EDNS0 is active. */
-#define EDNS0_PADDING 128
+#define EDNS0_PADDING 128
/* per-netid configuration parameters passed from netd to the resolver */
struct __res_params {
- uint16_t sample_validity; // sample lifetime in s
+ uint16_t sample_validity; // sample lifetime in s
// threshold of success / total samples below which a server is considered broken
- uint8_t success_threshold; // 0: disable, value / 100 otherwise
- uint8_t min_samples; // min # samples needed for statistics to be considered meaningful
- uint8_t max_samples; // max # samples taken into account for statistics
- int base_timeout_msec; // base query retry timeout (if 0, use RES_TIMEOUT)
+ uint8_t success_threshold; // 0: disable, value / 100 otherwise
+ uint8_t min_samples; // min # samples needed for statistics to be considered meaningful
+ uint8_t max_samples; // max # samples taken into account for statistics
+ int base_timeout_msec; // base query retry timeout (if 0, use RES_TIMEOUT)
};
-typedef enum { res_goahead, res_nextns, res_modified, res_done, res_error }
- res_sendhookact;
+typedef enum { res_goahead, res_nextns, res_modified, res_done, res_error } res_sendhookact;
-typedef res_sendhookact (*res_send_qhook)(struct sockaddr * const *,
- const u_char **, int *,
- u_char *, int, int *);
+typedef res_sendhookact (*res_send_qhook)(struct sockaddr* const*, const u_char**, int*, u_char*,
+ int, int*);
-typedef res_sendhookact (*res_send_rhook)(const struct sockaddr *,
- const u_char *, int, u_char *,
- int, int *);
+typedef res_sendhookact (*res_send_rhook)(const struct sockaddr*, const u_char*, int, u_char*, int,
+ int*);
-#endif // _RESOLV_PARAMS_H
+#endif // _RESOLV_PARAMS_H
diff --git a/resolv/resolv_private.h b/resolv/resolv_private.h
index 77b03bf..0db0cea 100644
--- a/resolv/resolv_private.h
+++ b/resolv/resolv_private.h
@@ -52,16 +52,16 @@
*/
#ifndef _RESOLV_PRIVATE_H_
-#define _RESOLV_PRIVATE_H_
+#define _RESOLV_PRIVATE_H_
#include <sys/cdefs.h>
-#include <resolv.h>
-#include "resolv_static.h"
-#include "resolv_params.h"
-#include "resolv_stats.h"
#include <net/if.h>
+#include <resolv.h>
#include <time.h>
+#include "resolv_params.h"
+#include "resolv_static.h"
+#include "resolv_stats.h"
// Linux defines MAXHOSTNAMELEN as 64, while the domain name limit in
// RFC 1034 and RFC 1035 is 255 octets.
@@ -78,7 +78,7 @@
* is new enough to contain a certain feature.
*/
-#define __RES 20030124
+#define __RES 20030124
/*
* This used to be defined in res_query.c, now it's in herror.c.
@@ -99,7 +99,7 @@
* #endif
*/
-#define RES_SET_H_ERRNO(r,x) (h_errno = (r)->res_h_errno = (x))
+#define RES_SET_H_ERRNO(r, x) (h_errno = (r)->res_h_errno = (x))
struct __res_state; /* forward */
/*
@@ -110,240 +110,237 @@
#ifndef _PATH_RESCONF
#ifdef ANDROID_CHANGES
-#define _PATH_RESCONF "/etc/ppp/resolv.conf"
+#define _PATH_RESCONF "/etc/ppp/resolv.conf"
#else
-#define _PATH_RESCONF "/etc/resolv.conf"
+#define _PATH_RESCONF "/etc/resolv.conf"
#endif
#endif
struct res_sym {
- int number; /* Identifying number, like T_MX */
- const char * name; /* Its symbolic name, like "MX" */
- const char * humanname; /* Its fun name, like "mail exchanger" */
+ int number; /* Identifying number, like T_MX */
+ const char* name; /* Its symbolic name, like "MX" */
+ const char* humanname; /* Its fun name, like "mail exchanger" */
};
/*
* Global defines and variables for resolver stub.
*/
-#define MAXDFLSRCH 3 /* # default domain levels to try */
-#define LOCALDOMAINPARTS 2 /* min levels in name that is "local" */
+#define MAXDFLSRCH 3 /* # default domain levels to try */
+#define LOCALDOMAINPARTS 2 /* min levels in name that is "local" */
-#define RES_TIMEOUT 5 /* min. seconds between retries */
-#define MAXRESOLVSORT 10 /* number of net to sort on */
-#define RES_MAXNDOTS 15 /* should reflect bit field size */
-#define RES_MAXRETRANS 30 /* only for resolv.conf/RES_OPTIONS */
-#define RES_MAXRETRY 5 /* only for resolv.conf/RES_OPTIONS */
-#define RES_DFLRETRY 2 /* Default #/tries. */
-#define RES_MAXTIME 65535 /* Infinity, in milliseconds. */
+#define RES_TIMEOUT 5 /* min. seconds between retries */
+#define MAXRESOLVSORT 10 /* number of net to sort on */
+#define RES_MAXNDOTS 15 /* should reflect bit field size */
+#define RES_MAXRETRANS 30 /* only for resolv.conf/RES_OPTIONS */
+#define RES_MAXRETRY 5 /* only for resolv.conf/RES_OPTIONS */
+#define RES_DFLRETRY 2 /* Default #/tries. */
+#define RES_MAXTIME 65535 /* Infinity, in milliseconds. */
struct __res_state_ext;
struct __res_state {
- unsigned netid; /* NetId: cache key and socket mark */
- int retrans; /* retransmission time interval */
- int retry; /* number of times to retransmit */
+ unsigned netid; /* NetId: cache key and socket mark */
+ int retrans; /* retransmission time interval */
+ int retry; /* number of times to retransmit */
#ifdef sun
- u_int options; /* option flags - see below. */
+ u_int options; /* option flags - see below. */
#else
- u_long options; /* option flags - see below. */
+ u_long options; /* option flags - see below. */
#endif
- int nscount; /* number of name servers */
- struct sockaddr_in
- nsaddr_list[MAXNS]; /* address of name server */
-#define nsaddr nsaddr_list[0] /* for backward compatibility */
- u_short id; /* current message id */
- char *dnsrch[MAXDNSRCH+1]; /* components of domain to search */
- char defdname[256]; /* default domain (deprecated) */
+ int nscount; /* number of name servers */
+ struct sockaddr_in nsaddr_list[MAXNS]; /* address of name server */
+#define nsaddr nsaddr_list[0] /* for backward compatibility */
+ u_short id; /* current message id */
+ char* dnsrch[MAXDNSRCH + 1]; /* components of domain to search */
+ char defdname[256]; /* default domain (deprecated) */
#ifdef sun
- u_int pfcode; /* RES_PRF_ flags - see below. */
+ u_int pfcode; /* RES_PRF_ flags - see below. */
#else
- u_long pfcode; /* RES_PRF_ flags - see below. */
+ u_long pfcode; /* RES_PRF_ flags - see below. */
#endif
- unsigned ndots:4; /* threshold for initial abs. query */
- unsigned nsort:4; /* number of elements in sort_list[] */
- char unused[3];
- struct {
- struct in_addr addr;
- uint32_t mask;
- } sort_list[MAXRESOLVSORT];
+ unsigned ndots : 4; /* threshold for initial abs. query */
+ unsigned nsort : 4; /* number of elements in sort_list[] */
+ char unused[3];
+ struct {
+ struct in_addr addr;
+ uint32_t mask;
+ } sort_list[MAXRESOLVSORT];
#ifdef __OLD_RES_STATE
- char lookups[4];
+ char lookups[4];
#else
- res_send_qhook qhook; /* query hook */
- res_send_rhook rhook; /* response hook */
- int res_h_errno; /* last one set for this context */
- unsigned _mark; /* If non-0 SET_MARK to _mark on all request sockets */
- int _vcsock; /* PRIVATE: for res_send VC i/o */
- u_int _flags; /* PRIVATE: see below */
- u_int _pad; /* make _u 64 bit aligned */
- union {
- /* On an 32-bit arch this means 512b total. */
- char pad[72 - 4*sizeof (int) - 2*sizeof (void *)];
- struct {
- uint16_t nscount;
- uint16_t nstimes[MAXNS]; /* ms. */
- int nssocks[MAXNS];
- struct __res_state_ext *ext; /* extention for IPv6 */
- } _ext;
- } _u;
+ res_send_qhook qhook; /* query hook */
+ res_send_rhook rhook; /* response hook */
+ int res_h_errno; /* last one set for this context */
+ unsigned _mark; /* If non-0 SET_MARK to _mark on all request sockets */
+ int _vcsock; /* PRIVATE: for res_send VC i/o */
+ u_int _flags; /* PRIVATE: see below */
+ u_int _pad; /* make _u 64 bit aligned */
+ union {
+ /* On an 32-bit arch this means 512b total. */
+ char pad[72 - 4 * sizeof(int) - 2 * sizeof(void*)];
+ struct {
+ uint16_t nscount;
+ uint16_t nstimes[MAXNS]; /* ms. */
+ int nssocks[MAXNS];
+ struct __res_state_ext* ext; /* extention for IPv6 */
+ } _ext;
+ } _u;
#endif
- struct res_static rstatic[1];
+ struct res_static rstatic[1];
};
-typedef struct __res_state *res_state;
+typedef struct __res_state* res_state;
/* Retrieve a local copy of the stats for the given netid. The buffer must have space for
* MAXNS __resolver_stats. Returns the revision id of the resolvers used.
*/
__LIBC_HIDDEN__
-extern int
-_resolv_cache_get_resolver_stats( unsigned netid, struct __res_params* params,
- struct __res_stats stats[MAXNS]);
+extern int _resolv_cache_get_resolver_stats(unsigned netid, struct __res_params* params,
+ struct __res_stats stats[MAXNS]);
/* Add a sample to the shared struct for the given netid and server, provided that the
* revision_id of the stored servers has not changed.
*/
__LIBC_HIDDEN__
-extern void
-_resolv_cache_add_resolver_stats_sample( unsigned netid, int revision_id, int ns,
- const struct __res_sample* sample, int max_samples);
+extern void _resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, int ns,
+ const struct __res_sample* sample,
+ int max_samples);
/* End of stats related definitions */
union res_sockaddr_union {
- struct sockaddr_in sin;
+ struct sockaddr_in sin;
#ifdef IN6ADDR_ANY_INIT
- struct sockaddr_in6 sin6;
+ struct sockaddr_in6 sin6;
#endif
#ifdef ISC_ALIGN64
- int64_t __align64; /* 64bit alignment */
+ int64_t __align64; /* 64bit alignment */
#else
- int32_t __align32; /* 32bit alignment */
+ int32_t __align32; /* 32bit alignment */
#endif
- char __space[128]; /* max size */
+ char __space[128]; /* max size */
};
/*
* Resolver flags (used to be discrete per-module statics ints).
*/
-#define RES_F_VC 0x00000001 /* socket is TCP */
-#define RES_F_CONN 0x00000002 /* socket is connected */
-#define RES_F_EDNS0ERR 0x00000004 /* EDNS0 caused errors */
-#define RES_F__UNUSED 0x00000008 /* (unused) */
-#define RES_F_LASTMASK 0x000000F0 /* ordinal server of last res_nsend */
-#define RES_F_LASTSHIFT 4 /* bit position of LASTMASK "flag" */
-#define RES_GETLAST(res) (((res)._flags & RES_F_LASTMASK) >> RES_F_LASTSHIFT)
+#define RES_F_VC 0x00000001 /* socket is TCP */
+#define RES_F_CONN 0x00000002 /* socket is connected */
+#define RES_F_EDNS0ERR 0x00000004 /* EDNS0 caused errors */
+#define RES_F__UNUSED 0x00000008 /* (unused) */
+#define RES_F_LASTMASK 0x000000F0 /* ordinal server of last res_nsend */
+#define RES_F_LASTSHIFT 4 /* bit position of LASTMASK "flag" */
+#define RES_GETLAST(res) (((res)._flags & RES_F_LASTMASK) >> RES_F_LASTSHIFT)
/* res_findzonecut2() options */
-#define RES_EXHAUSTIVE 0x00000001 /* always do all queries */
-#define RES_IPV4ONLY 0x00000002 /* IPv4 only */
-#define RES_IPV6ONLY 0x00000004 /* IPv6 only */
+#define RES_EXHAUSTIVE 0x00000001 /* always do all queries */
+#define RES_IPV4ONLY 0x00000002 /* IPv4 only */
+#define RES_IPV6ONLY 0x00000004 /* IPv6 only */
/*
* Resolver options (keep these in synch with res_debug.c, please)
*/
-#define RES_INIT 0x00000001 /* address initialized */
-#define RES_DEBUG 0x00000002 /* print debug messages */
-#define RES_AAONLY 0x00000004 /* authoritative answers only (!IMPL)*/
-#define RES_USEVC 0x00000008 /* use virtual circuit */
-#define RES_PRIMARY 0x00000010 /* query primary server only (!IMPL) */
-#define RES_IGNTC 0x00000020 /* ignore trucation errors */
-#define RES_RECURSE 0x00000040 /* recursion desired */
-#define RES_DEFNAMES 0x00000080 /* use default domain name */
-#define RES_STAYOPEN 0x00000100 /* Keep TCP socket open */
-#define RES_DNSRCH 0x00000200 /* search up local domain tree */
-#define RES_INSECURE1 0x00000400 /* type 1 security disabled */
-#define RES_INSECURE2 0x00000800 /* type 2 security disabled */
-#define RES_NOALIASES 0x00001000 /* shuts off HOSTALIASES feature */
-#define RES_USE_INET6 0x00002000 /* use/map IPv6 in gethostbyname() */
-#define RES_ROTATE 0x00004000 /* rotate ns list after each query */
-#define RES_NOCHECKNAME 0x00008000 /* do not check names for sanity. */
-#define RES_KEEPTSIG 0x00010000 /* do not strip TSIG records */
-#define RES_BLAST 0x00020000 /* blast all recursive servers */
-#define RES_NOTLDQUERY 0x00100000 /* don't unqualified name as a tld */
-#define RES_USE_DNSSEC 0x00200000 /* use DNSSEC using OK bit in OPT */
-/* #define RES_DEBUG2 0x00400000 */ /* nslookup internal */
+#define RES_INIT 0x00000001 /* address initialized */
+#define RES_DEBUG 0x00000002 /* print debug messages */
+#define RES_AAONLY 0x00000004 /* authoritative answers only (!IMPL)*/
+#define RES_USEVC 0x00000008 /* use virtual circuit */
+#define RES_PRIMARY 0x00000010 /* query primary server only (!IMPL) */
+#define RES_IGNTC 0x00000020 /* ignore trucation errors */
+#define RES_RECURSE 0x00000040 /* recursion desired */
+#define RES_DEFNAMES 0x00000080 /* use default domain name */
+#define RES_STAYOPEN 0x00000100 /* Keep TCP socket open */
+#define RES_DNSRCH 0x00000200 /* search up local domain tree */
+#define RES_INSECURE1 0x00000400 /* type 1 security disabled */
+#define RES_INSECURE2 0x00000800 /* type 2 security disabled */
+#define RES_NOALIASES 0x00001000 /* shuts off HOSTALIASES feature */
+#define RES_USE_INET6 0x00002000 /* use/map IPv6 in gethostbyname() */
+#define RES_ROTATE 0x00004000 /* rotate ns list after each query */
+#define RES_NOCHECKNAME 0x00008000 /* do not check names for sanity. */
+#define RES_KEEPTSIG 0x00010000 /* do not strip TSIG records */
+#define RES_BLAST 0x00020000 /* blast all recursive servers */
+#define RES_NOTLDQUERY 0x00100000 /* don't unqualified name as a tld */
+#define RES_USE_DNSSEC 0x00200000 /* use DNSSEC using OK bit in OPT */
+/* #define RES_DEBUG2 0x00400000 */ /* nslookup internal */
/* KAME extensions: use higher bit to avoid conflict with ISC use */
-#define RES_USE_DNAME 0x10000000 /* use DNAME */
-#define RES_USE_EDNS0 0x40000000 /* use EDNS0 if configured */
-#define RES_NO_NIBBLE2 0x80000000 /* disable alternate nibble lookup */
+#define RES_USE_DNAME 0x10000000 /* use DNAME */
+#define RES_USE_EDNS0 0x40000000 /* use EDNS0 if configured */
+#define RES_NO_NIBBLE2 0x80000000 /* disable alternate nibble lookup */
-#define RES_DEFAULT (RES_RECURSE | RES_DEFNAMES | \
- RES_DNSRCH | RES_NO_NIBBLE2)
+#define RES_DEFAULT (RES_RECURSE | RES_DEFNAMES | RES_DNSRCH | RES_NO_NIBBLE2)
/*
* Resolver "pfcode" values. Used by dig.
*/
-#define RES_PRF_STATS 0x00000001
-#define RES_PRF_UPDATE 0x00000002
-#define RES_PRF_CLASS 0x00000004
-#define RES_PRF_CMD 0x00000008
-#define RES_PRF_QUES 0x00000010
-#define RES_PRF_ANS 0x00000020
-#define RES_PRF_AUTH 0x00000040
-#define RES_PRF_ADD 0x00000080
-#define RES_PRF_HEAD1 0x00000100
-#define RES_PRF_HEAD2 0x00000200
-#define RES_PRF_TTLID 0x00000400
-#define RES_PRF_HEADX 0x00000800
-#define RES_PRF_QUERY 0x00001000
-#define RES_PRF_REPLY 0x00002000
-#define RES_PRF_INIT 0x00004000
-#define RES_PRF_TRUNC 0x00008000
+#define RES_PRF_STATS 0x00000001
+#define RES_PRF_UPDATE 0x00000002
+#define RES_PRF_CLASS 0x00000004
+#define RES_PRF_CMD 0x00000008
+#define RES_PRF_QUES 0x00000010
+#define RES_PRF_ANS 0x00000020
+#define RES_PRF_AUTH 0x00000040
+#define RES_PRF_ADD 0x00000080
+#define RES_PRF_HEAD1 0x00000100
+#define RES_PRF_HEAD2 0x00000200
+#define RES_PRF_TTLID 0x00000400
+#define RES_PRF_HEADX 0x00000800
+#define RES_PRF_QUERY 0x00001000
+#define RES_PRF_REPLY 0x00002000
+#define RES_PRF_INIT 0x00004000
+#define RES_PRF_TRUNC 0x00008000
/* 0x00010000 */
/* Things involving an internal (static) resolver context. */
__BEGIN_DECLS
-__LIBC_HIDDEN__ extern struct __res_state *__res_get_state(void);
-__LIBC_HIDDEN__ extern void __res_put_state(struct __res_state *);
+__LIBC_HIDDEN__ extern struct __res_state* __res_get_state(void);
+__LIBC_HIDDEN__ extern void __res_put_state(struct __res_state*);
#ifndef ANDROID_CHANGES
/*
* Source and Binary compatibility; _res will not work properly
* with multi-threaded programs.
*/
-extern struct __res_state *__res_state(void);
+extern struct __res_state* __res_state(void);
#define _res (*__res_state())
#endif
__END_DECLS
#ifndef __BIND_NOSTATIC
-#define fp_nquery __fp_nquery
-#define fp_query __fp_query
-#define hostalias __hostalias
-#define p_query __p_query
-#define res_close __res_close
-#define res_opt __res_opt
-#define res_isourserver __res_isourserver
-#define res_querydomain __res_querydomain
-#define res_send __res_send
-#define res_sendsigned __res_sendsigned
+#define fp_nquery __fp_nquery
+#define fp_query __fp_query
+#define hostalias __hostalias
+#define p_query __p_query
+#define res_close __res_close
+#define res_opt __res_opt
+#define res_isourserver __res_isourserver
+#define res_querydomain __res_querydomain
+#define res_send __res_send
+#define res_sendsigned __res_sendsigned
#ifdef BIND_RES_POSIX3
-#define dn_expand __dn_expand
-#define res_init __res_init
-#define res_query __res_query
-#define res_search __res_search
-#define res_mkquery __res_mkquery
+#define dn_expand __dn_expand
+#define res_init __res_init
+#define res_query __res_query
+#define res_search __res_search
+#define res_mkquery __res_mkquery
#endif
__BEGIN_DECLS
-void fp_nquery(const u_char *, int, FILE *);
-void fp_query(const u_char *, FILE *);
-const char * hostalias(const char *);
-void p_query(const u_char *);
-void res_close(void);
-int res_init(void);
-__LIBC_HIDDEN__ int res_opt(int, u_char *, int, int);
-int res_isourserver(const struct sockaddr_in *);
-int res_mkquery(int, const char *, int, int, const u_char *, int, const u_char *, u_char *, int);
-int res_query(const char *, int, int, u_char *, int);
-int res_querydomain(const char *, const char *, int, int, u_char *, int);
-int res_search(const char *, int, int, u_char *, int);
-int res_send(const u_char *, int, u_char *, int);
-int res_sendsigned(const u_char *, int, ns_tsig_key *, u_char *, int);
+void fp_nquery(const u_char*, int, FILE*);
+void fp_query(const u_char*, FILE*);
+const char* hostalias(const char*);
+void p_query(const u_char*);
+void res_close(void);
+int res_init(void);
+__LIBC_HIDDEN__ int res_opt(int, u_char*, int, int);
+int res_isourserver(const struct sockaddr_in*);
+int res_mkquery(int, const char*, int, int, const u_char*, int, const u_char*, u_char*, int);
+int res_query(const char*, int, int, u_char*, int);
+int res_querydomain(const char*, const char*, int, int, u_char*, int);
+int res_search(const char*, int, int, u_char*, int);
+int res_send(const u_char*, int, u_char*, int);
+int res_sendsigned(const u_char*, int, ns_tsig_key*, u_char*, int);
__END_DECLS
#endif
@@ -363,149 +360,139 @@
#endif /* SHARED_LIBBIND */
#ifndef ANDROID_CHANGES
-#define dn_comp __dn_comp
+#define dn_comp __dn_comp
#endif
-#define dn_count_labels __dn_count_labels
-#define dn_skipname __dn_skipname
-#define fp_resstat __fp_resstat
-#define loc_aton __loc_aton
-#define loc_ntoa __loc_ntoa
-#define p_cdname __p_cdname
-#define p_cdnname __p_cdnname
-#define p_class __p_class
-#define p_fqname __p_fqname
-#define p_fqnname __p_fqnname
-#define p_option __p_option
-#define p_secstodate __p_secstodate
-#define p_section __p_section
-#define p_time __p_time
-#define p_type __p_type
-#define p_rcode __p_rcode
-#define p_sockun __p_sockun
-#define putlong __putlong
-#define putshort __putshort
-#define res_dnok __res_dnok
-#define res_findzonecut __res_findzonecut
-#define res_findzonecut2 __res_findzonecut2
-#define res_hnok __res_hnok
-#define res_hostalias __res_hostalias
-#define res_mailok __res_mailok
-#define res_nameinquery __res_nameinquery
-#define res_nclose __res_nclose
-#define res_ninit __res_ninit
-#define res_nmkquery __res_nmkquery
-#define res_pquery __res_pquery
-#define res_nquery __res_nquery
-#define res_nquerydomain __res_nquerydomain
-#define res_nsearch __res_nsearch
-#define res_nsend __res_nsend
-#define res_nsendsigned __res_nsendsigned
-#define res_nisourserver __res_nisourserver
-#define res_ownok __res_ownok
-#define res_queriesmatch __res_queriesmatch
-#define sym_ntop __sym_ntop
-#define sym_ntos __sym_ntos
-#define sym_ston __sym_ston
-#define res_nopt __res_nopt
-#define res_ndestroy __res_ndestroy
-#define res_nametoclass __res_nametoclass
-#define res_nametotype __res_nametotype
-#define res_setservers __res_setservers
-#define res_getservers __res_getservers
-#define res_buildprotolist __res_buildprotolist
-#define res_destroyprotolist __res_destroyprotolist
-#define res_destroyservicelist __res_destroyservicelist
-#define res_get_nibblesuffix __res_get_nibblesuffix
-#define res_get_nibblesuffix2 __res_get_nibblesuffix2
-#define res_ourserver_p __res_ourserver_p
-#define res_protocolname __res_protocolname
-#define res_protocolnumber __res_protocolnumber
-#define res_send_setqhook __res_send_setqhook
-#define res_send_setrhook __res_send_setrhook
-#define res_servicename __res_servicename
-#define res_servicenumber __res_servicenumber
+#define dn_count_labels __dn_count_labels
+#define dn_skipname __dn_skipname
+#define fp_resstat __fp_resstat
+#define loc_aton __loc_aton
+#define loc_ntoa __loc_ntoa
+#define p_cdname __p_cdname
+#define p_cdnname __p_cdnname
+#define p_class __p_class
+#define p_fqname __p_fqname
+#define p_fqnname __p_fqnname
+#define p_option __p_option
+#define p_secstodate __p_secstodate
+#define p_section __p_section
+#define p_time __p_time
+#define p_type __p_type
+#define p_rcode __p_rcode
+#define p_sockun __p_sockun
+#define putlong __putlong
+#define putshort __putshort
+#define res_dnok __res_dnok
+#define res_findzonecut __res_findzonecut
+#define res_findzonecut2 __res_findzonecut2
+#define res_hnok __res_hnok
+#define res_hostalias __res_hostalias
+#define res_mailok __res_mailok
+#define res_nameinquery __res_nameinquery
+#define res_nclose __res_nclose
+#define res_ninit __res_ninit
+#define res_nmkquery __res_nmkquery
+#define res_pquery __res_pquery
+#define res_nquery __res_nquery
+#define res_nquerydomain __res_nquerydomain
+#define res_nsearch __res_nsearch
+#define res_nsend __res_nsend
+#define res_nsendsigned __res_nsendsigned
+#define res_nisourserver __res_nisourserver
+#define res_ownok __res_ownok
+#define res_queriesmatch __res_queriesmatch
+#define sym_ntop __sym_ntop
+#define sym_ntos __sym_ntos
+#define sym_ston __sym_ston
+#define res_nopt __res_nopt
+#define res_ndestroy __res_ndestroy
+#define res_nametoclass __res_nametoclass
+#define res_nametotype __res_nametotype
+#define res_setservers __res_setservers
+#define res_getservers __res_getservers
+#define res_buildprotolist __res_buildprotolist
+#define res_destroyprotolist __res_destroyprotolist
+#define res_destroyservicelist __res_destroyservicelist
+#define res_get_nibblesuffix __res_get_nibblesuffix
+#define res_get_nibblesuffix2 __res_get_nibblesuffix2
+#define res_ourserver_p __res_ourserver_p
+#define res_protocolname __res_protocolname
+#define res_protocolnumber __res_protocolnumber
+#define res_send_setqhook __res_send_setqhook
+#define res_send_setrhook __res_send_setrhook
+#define res_servicename __res_servicename
+#define res_servicenumber __res_servicenumber
__BEGIN_DECLS
-int res_hnok(const char *);
-int res_ownok(const char *);
-int res_mailok(const char *);
-int res_dnok(const char *);
-int sym_ston(const struct res_sym *, const char *, int *);
-const char * sym_ntos(const struct res_sym *, int, int *);
-const char * sym_ntop(const struct res_sym *, int, int *);
+int res_hnok(const char*);
+int res_ownok(const char*);
+int res_mailok(const char*);
+int res_dnok(const char*);
+int sym_ston(const struct res_sym*, const char*, int*);
+const char* sym_ntos(const struct res_sym*, int, int*);
+const char* sym_ntop(const struct res_sym*, int, int*);
#ifndef ANDROID_CHANGES
-int b64_ntop(u_char const *, size_t, char *, size_t);
-int b64_pton(char const *, u_char *, size_t);
+int b64_ntop(u_char const*, size_t, char*, size_t);
+int b64_pton(char const*, u_char*, size_t);
#endif
-int loc_aton(const char *, u_char *);
-const char * loc_ntoa(const u_char *, char *, size_t);
-int dn_skipname(const u_char *, const u_char *);
-void putlong(uint32_t, u_char *);
-void putshort(uint16_t, u_char *);
+int loc_aton(const char*, u_char*);
+const char* loc_ntoa(const u_char*, char*, size_t);
+int dn_skipname(const u_char*, const u_char*);
+void putlong(uint32_t, u_char*);
+void putshort(uint16_t, u_char*);
#ifndef __ultrix__
-uint16_t _getshort(const u_char *);
-uint32_t _getlong(const u_char *);
+uint16_t _getshort(const u_char*);
+uint32_t _getlong(const u_char*);
#endif
-const char * p_class(int);
-const char * p_time(uint32_t);
-const char * p_type(int);
-const char * p_rcode(int);
-__LIBC_HIDDEN__ const char * p_sockun(union res_sockaddr_union, char *, size_t);
-const u_char * p_cdnname(const u_char *, const u_char *, int, FILE *);
-const u_char * p_cdname(const u_char *, const u_char *, FILE *);
-const u_char * p_fqnname(const u_char *, const u_char *,
- int, char *, int);
-const u_char * p_fqname(const u_char *, const u_char *, FILE *);
-const char * p_option(u_long);
-char * p_secstodate(u_long);
-int dn_count_labels(const char *);
-int res_nameinquery(const char *, int, int, const u_char *,
- const u_char *);
-int res_queriesmatch(const u_char *, const u_char *,
- const u_char *, const u_char *);
-__LIBC_HIDDEN__ const char * p_section(int, int);
+const char* p_class(int);
+const char* p_time(uint32_t);
+const char* p_type(int);
+const char* p_rcode(int);
+__LIBC_HIDDEN__ const char* p_sockun(union res_sockaddr_union, char*, size_t);
+const u_char* p_cdnname(const u_char*, const u_char*, int, FILE*);
+const u_char* p_cdname(const u_char*, const u_char*, FILE*);
+const u_char* p_fqnname(const u_char*, const u_char*, int, char*, int);
+const u_char* p_fqname(const u_char*, const u_char*, FILE*);
+const char* p_option(u_long);
+char* p_secstodate(u_long);
+int dn_count_labels(const char*);
+int res_nameinquery(const char*, int, int, const u_char*, const u_char*);
+int res_queriesmatch(const u_char*, const u_char*, const u_char*, const u_char*);
+__LIBC_HIDDEN__ const char* p_section(int, int);
/* Things involving a resolver context. */
-int res_ninit(res_state);
-int res_nisourserver(const res_state, const struct sockaddr_in *);
-void fp_resstat(const res_state, FILE *);
-__LIBC_HIDDEN__ void res_pquery(const res_state, const u_char *, int, FILE *);
-const char * res_hostalias(const res_state, const char *, char *, size_t);
-int res_nquery(res_state, const char *, int, int, u_char *, int);
-int res_nsearch(res_state, const char *, int, int, u_char *, int);
-int res_nquerydomain(res_state, const char *, const char *,
- int, int, u_char *, int);
-int res_nmkquery(res_state, int, const char *, int, int,
- const u_char *, int, const u_char *,
- u_char *, int);
-int res_nsend(res_state, const u_char *, int, u_char *, int);
-int res_nsendsigned(res_state, const u_char *, int,
- ns_tsig_key *, u_char *, int);
-int res_findzonecut(res_state, const char *, ns_class, int,
- char *, size_t, struct in_addr *, int);
-int res_findzonecut2(res_state, const char *, ns_class, int,
- char *, size_t,
- union res_sockaddr_union *, int);
-void res_nclose(res_state);
-__LIBC_HIDDEN__ int res_nopt(res_state, int, u_char *, int, int);
-void res_send_setqhook(res_send_qhook);
-void res_send_setrhook(res_send_rhook);
-__LIBC_HIDDEN__ int __res_vinit(res_state, int);
-void res_destroyservicelist(void);
-const char * res_servicename(uint16_t, const char *);
-const char * res_protocolname(int);
-void res_destroyprotolist(void);
-void res_buildprotolist(void);
-__LIBC_HIDDEN__ const char * res_get_nibblesuffix(res_state);
-__LIBC_HIDDEN__ const char * res_get_nibblesuffix2(res_state);
-__LIBC_HIDDEN__ void res_ndestroy(res_state);
-__LIBC_HIDDEN__ uint16_t res_nametoclass(const char *, int *);
-__LIBC_HIDDEN__ uint16_t res_nametotype(const char *, int *);
-__LIBC_HIDDEN__ void res_setservers(res_state,
- const union res_sockaddr_union *, int);
-__LIBC_HIDDEN__ int res_getservers(res_state,
- union res_sockaddr_union *, int);
+int res_ninit(res_state);
+int res_nisourserver(const res_state, const struct sockaddr_in*);
+void fp_resstat(const res_state, FILE*);
+__LIBC_HIDDEN__ void res_pquery(const res_state, const u_char*, int, FILE*);
+const char* res_hostalias(const res_state, const char*, char*, size_t);
+int res_nquery(res_state, const char*, int, int, u_char*, int);
+int res_nsearch(res_state, const char*, int, int, u_char*, int);
+int res_nquerydomain(res_state, const char*, const char*, int, int, u_char*, int);
+int res_nmkquery(res_state, int, const char*, int, int, const u_char*, int, const u_char*, u_char*,
+ int);
+int res_nsend(res_state, const u_char*, int, u_char*, int);
+int res_nsendsigned(res_state, const u_char*, int, ns_tsig_key*, u_char*, int);
+int res_findzonecut(res_state, const char*, ns_class, int, char*, size_t, struct in_addr*, int);
+int res_findzonecut2(res_state, const char*, ns_class, int, char*, size_t,
+ union res_sockaddr_union*, int);
+void res_nclose(res_state);
+__LIBC_HIDDEN__ int res_nopt(res_state, int, u_char*, int, int);
+void res_send_setqhook(res_send_qhook);
+void res_send_setrhook(res_send_rhook);
+__LIBC_HIDDEN__ int __res_vinit(res_state, int);
+void res_destroyservicelist(void);
+const char* res_servicename(uint16_t, const char*);
+const char* res_protocolname(int);
+void res_destroyprotolist(void);
+void res_buildprotolist(void);
+__LIBC_HIDDEN__ const char* res_get_nibblesuffix(res_state);
+__LIBC_HIDDEN__ const char* res_get_nibblesuffix2(res_state);
+__LIBC_HIDDEN__ void res_ndestroy(res_state);
+__LIBC_HIDDEN__ uint16_t res_nametoclass(const char*, int*);
+__LIBC_HIDDEN__ uint16_t res_nametotype(const char*, int*);
+__LIBC_HIDDEN__ void res_setservers(res_state, const union res_sockaddr_union*, int);
+__LIBC_HIDDEN__ int res_getservers(res_state, union res_sockaddr_union*, int);
struct android_net_context; /* forward */
-__LIBC_HIDDEN__ void res_setnetcontext(res_state, const struct android_net_context *);
+__LIBC_HIDDEN__ void res_setnetcontext(res_state, const struct android_net_context*);
// We use the OpenBSD __res_randomid...
u_int __res_randomid(void);
@@ -513,9 +500,9 @@
#define res_randomid __res_randomid
#ifdef __i386__
-# define __socketcall extern __attribute__((__cdecl__))
+#define __socketcall extern __attribute__((__cdecl__))
#else
-# define __socketcall extern
+#define __socketcall extern
#endif
__socketcall int __connect(int, const struct sockaddr*, socklen_t);
diff --git a/resolv/resolv_static.h b/resolv/resolv_static.h
index 8f2a095..e92b83b 100644
--- a/resolv/resolv_static.h
+++ b/resolv/resolv_static.h
@@ -12,20 +12,20 @@
* (note: the OpenBSD/FreeBSD resolver has similar 'issues')
*/
-#define MAXALIASES 35
-#define MAXADDRS 35
+#define MAXALIASES 35
+#define MAXADDRS 35
typedef struct res_static {
- char* h_addr_ptrs[MAXADDRS + 1];
- char* host_aliases[MAXALIASES];
- char hostbuf[8*1024];
- u_int32_t host_addr[16 / sizeof(u_int32_t)]; /* IPv4 or IPv6 */
- FILE* hostf;
- int stayopen;
- const char* servent_ptr;
- struct servent servent;
- struct hostent host;
-} *res_static;
+ char* h_addr_ptrs[MAXADDRS + 1];
+ char* host_aliases[MAXALIASES];
+ char hostbuf[8 * 1024];
+ u_int32_t host_addr[16 / sizeof(u_int32_t)]; /* IPv4 or IPv6 */
+ FILE* hostf;
+ int stayopen;
+ const char* servent_ptr;
+ struct servent servent;
+ struct hostent host;
+} * res_static;
extern res_static __res_get_static(void);
diff --git a/resolv/resolv_stats.h b/resolv/resolv_stats.h
index c7901d7..18f884e 100644
--- a/resolv/resolv_stats.h
+++ b/resolv/resolv_stats.h
@@ -17,68 +17,64 @@
#ifndef _RES_STATS_H
#define _RES_STATS_H
-#include <sys/socket.h>
#include <stdbool.h>
#include <stdint.h>
+#include <sys/socket.h>
#include <time.h>
#include "resolv_params.h"
-#define RCODE_INTERNAL_ERROR 254
-#define RCODE_TIMEOUT 255
+#define RCODE_INTERNAL_ERROR 254
+#define RCODE_TIMEOUT 255
/*
* Resolver reachability statistics and run-time parameters.
*/
struct __res_sample {
- time_t at; // time in s at which the sample was recorded
- uint16_t rtt; // round-trip time in ms
- uint8_t rcode; // the DNS rcode or RCODE_XXX defined above
+ time_t at; // time in s at which the sample was recorded
+ uint16_t rtt; // round-trip time in ms
+ uint8_t rcode; // the DNS rcode or RCODE_XXX defined above
};
struct __res_stats {
// Stats of the last <sample_count> queries.
- struct __res_sample samples[MAXNSSAMPLES];
+ struct __res_sample samples[MAXNSSAMPLES];
// The number of samples stored.
- uint8_t sample_count;
+ uint8_t sample_count;
// The next sample to modify.
- uint8_t sample_next;
+ uint8_t sample_next;
};
/* Calculate the round-trip-time from start time t0 and end time t1. */
-extern int
-_res_stats_calculate_rtt(const struct timespec* t1, const struct timespec* t0);
+extern int _res_stats_calculate_rtt(const struct timespec* t1, const struct timespec* t0);
/* Initialize a sample for calculating server reachability statistics. */
-extern void
-_res_stats_set_sample(struct __res_sample* sample, time_t now, int rcode, int rtt);
+extern void _res_stats_set_sample(struct __res_sample* sample, time_t now, int rcode, int rtt);
/* Returns true if the server is considered unusable, i.e. if the success rate is not lower than the
* threshold for the stored stored samples. If not enough samples are stored, the server is
* considered usable.
*/
-extern bool
-_res_stats_usable_server(const struct __res_params* params, struct __res_stats* stats);
+extern bool _res_stats_usable_server(const struct __res_params* params, struct __res_stats* stats);
__BEGIN_DECLS
/* Aggregates the reachability statistics for the given server based on on the stored samples. */
-extern void
-android_net_res_stats_aggregate(struct __res_stats* stats, int* successes, int* errors,
- int* timeouts, int* internal_errors, int* rtt_avg, time_t* last_sample_time)
- __attribute__((visibility ("default")));
+extern void android_net_res_stats_aggregate(struct __res_stats* stats, int* successes, int* errors,
+ int* timeouts, int* internal_errors, int* rtt_avg,
+ time_t* last_sample_time)
+ __attribute__((visibility("default")));
-extern int
-android_net_res_stats_get_info_for_net(unsigned netid, int* nscount,
- struct sockaddr_storage servers[MAXNS], int* dcount, char domains[MAXDNSRCH][MAXDNSRCHPATH],
- struct __res_params* params, struct __res_stats stats[MAXNS])
- __attribute__((visibility ("default")));
+extern int android_net_res_stats_get_info_for_net(
+ unsigned netid, int* nscount, struct sockaddr_storage servers[MAXNS], int* dcount,
+ char domains[MAXDNSRCH][MAXDNSRCHPATH], struct __res_params* params,
+ struct __res_stats stats[MAXNS]) __attribute__((visibility("default")));
/* Returns an array of bools indicating which servers are considered good */
-extern void
-android_net_res_stats_get_usable_servers(const struct __res_params* params,
- struct __res_stats stats[], int nscount, bool valid_servers[])
- __attribute__((visibility ("default")));
+extern void android_net_res_stats_get_usable_servers(const struct __res_params* params,
+ struct __res_stats stats[], int nscount,
+ bool valid_servers[])
+ __attribute__((visibility("default")));
__END_DECLS
#endif // _RES_STATS_H
diff --git a/resolv/sethostent.c b/resolv/sethostent.c
index a743a54..8b1c5ab 100644
--- a/resolv/sethostent.c
+++ b/resolv/sethostent.c
@@ -39,78 +39,69 @@
#endif
#endif /* LIBC_SCCS and not lint */
-#include "namespace.h"
-#include <sys/param.h>
-#include <netinet/in.h>
-#include <arpa/nameser.h>
#include <arpa/inet.h>
+#include <arpa/nameser.h>
#include <assert.h>
-#include <string.h>
-#include <nsswitch.h>
-#include <netdb.h>
-#include <resolv.h>
#include <errno.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <nsswitch.h>
+#include <resolv.h>
#include <stdlib.h>
+#include <string.h>
+#include <sys/param.h>
+#include "namespace.h"
#include "hostent.h"
#include "resolv_private.h"
#define ALIGNBYTES (sizeof(uintptr_t) - 1)
-#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES)
+#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
#ifndef _REENTRANT
-void res_close(void);
+void res_close(void);
#endif
-static struct hostent *_hf_gethtbyname2(const char *, int, struct getnamaddr *);
+static struct hostent* _hf_gethtbyname2(const char*, int, struct getnamaddr*);
void
/*ARGSUSED*/
-sethostent(int stayopen)
-{
- res_static rs = __res_get_static();
- if (rs) sethostent_r(&rs->hostf);
+sethostent(int stayopen) {
+ res_static rs = __res_get_static();
+ if (rs) sethostent_r(&rs->hostf);
}
-void
-endhostent(void)
-{
- res_static rs = __res_get_static();
- if (rs) endhostent_r(&rs->hostf);
+void endhostent(void) {
+ res_static rs = __res_get_static();
+ if (rs) endhostent_r(&rs->hostf);
}
-void
-sethostent_r(FILE **hf)
-{
- if (!*hf)
- *hf = fopen(_PATH_HOSTS, "re");
- else
- rewind(*hf);
+void sethostent_r(FILE** hf) {
+ if (!*hf)
+ *hf = fopen(_PATH_HOSTS, "re");
+ else
+ rewind(*hf);
}
-void
-endhostent_r(FILE **hf)
-{
- if (*hf) {
- (void)fclose(*hf);
- *hf = NULL;
- }
+void endhostent_r(FILE** hf) {
+ if (*hf) {
+ (void) fclose(*hf);
+ *hf = NULL;
+ }
}
/*ARGSUSED*/
-int
-_hf_gethtbyname(void *rv, void *cb_data, va_list ap)
-{
- struct hostent *hp;
- const char *name;
- int af;
- struct getnamaddr *info = rv;
+int _hf_gethtbyname(void* rv, void* cb_data, va_list ap) {
+ struct hostent* hp;
+ const char* name;
+ int af;
+ struct getnamaddr* info = rv;
- _DIAGASSERT(rv != NULL);
+ _DIAGASSERT(rv != NULL);
- name = va_arg(ap, char *);
- /* NOSTRICT skip string len */(void)va_arg(ap, int);
- af = va_arg(ap, int);
+ name = va_arg(ap, char*);
+ /* NOSTRICT skip string len */ (void) va_arg(ap, int);
+ af = va_arg(ap, int);
#if 0
{
@@ -126,160 +117,144 @@
__res_put_state(res);
}
#else
- hp = _hf_gethtbyname2(name, af, info);
+ hp = _hf_gethtbyname2(name, af, info);
#endif
- if (hp == NULL) {
- if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
- return NS_UNAVAIL; // glibc compatibility.
- }
- *info->he = HOST_NOT_FOUND;
- return NS_NOTFOUND;
- }
- return NS_SUCCESS;
+ if (hp == NULL) {
+ if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
+ return NS_UNAVAIL; // glibc compatibility.
+ }
+ *info->he = HOST_NOT_FOUND;
+ return NS_NOTFOUND;
+ }
+ return NS_SUCCESS;
}
-struct hostent *
-_hf_gethtbyname2(const char *name, int af, struct getnamaddr *info)
-{
- struct hostent *hp, hent;
- char *buf, *ptr;
- size_t len, anum, num, i;
- FILE *hf;
- char *aliases[MAXALIASES];
- char *addr_ptrs[MAXADDRS];
+struct hostent* _hf_gethtbyname2(const char* name, int af, struct getnamaddr* info) {
+ struct hostent *hp, hent;
+ char *buf, *ptr;
+ size_t len, anum, num, i;
+ FILE* hf;
+ char* aliases[MAXALIASES];
+ char* addr_ptrs[MAXADDRS];
- _DIAGASSERT(name != NULL);
+ _DIAGASSERT(name != NULL);
- hf = NULL;
- sethostent_r(&hf);
- if (hf == NULL) {
- errno = EINVAL;
- *info->he = NETDB_INTERNAL;
- return NULL;
- }
+ hf = NULL;
+ sethostent_r(&hf);
+ if (hf == NULL) {
+ errno = EINVAL;
+ *info->he = NETDB_INTERNAL;
+ return NULL;
+ }
- if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
- *info->he = NETDB_INTERNAL;
- return NULL;
- }
+ if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
+ *info->he = NETDB_INTERNAL;
+ return NULL;
+ }
- anum = 0; /* XXX: gcc */
- hent.h_name = NULL; /* XXX: gcc */
- hent.h_addrtype = 0; /* XXX: gcc */
- hent.h_length = 0; /* XXX: gcc */
+ anum = 0; /* XXX: gcc */
+ hent.h_name = NULL; /* XXX: gcc */
+ hent.h_addrtype = 0; /* XXX: gcc */
+ hent.h_length = 0; /* XXX: gcc */
- for (num = 0; num < MAXADDRS;) {
- info->hp->h_addrtype = af;
- info->hp->h_length = 0;
+ for (num = 0; num < MAXADDRS;) {
+ info->hp->h_addrtype = af;
+ info->hp->h_length = 0;
- hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
- info->he);
- if (hp == NULL) {
- if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
- goto nospc; // glibc compatibility.
- }
- break;
- }
+ hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, info->he);
+ if (hp == NULL) {
+ if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
+ goto nospc; // glibc compatibility.
+ }
+ break;
+ }
- if (strcasecmp(hp->h_name, name) != 0) {
- char **cp;
- for (cp = hp->h_aliases; *cp != NULL; cp++)
- if (strcasecmp(*cp, name) == 0)
- break;
- if (*cp == NULL) continue;
- }
+ if (strcasecmp(hp->h_name, name) != 0) {
+ char** cp;
+ for (cp = hp->h_aliases; *cp != NULL; cp++)
+ if (strcasecmp(*cp, name) == 0) break;
+ if (*cp == NULL) continue;
+ }
- if (num == 0) {
- hent.h_addrtype = af = hp->h_addrtype;
- hent.h_length = hp->h_length;
+ if (num == 0) {
+ hent.h_addrtype = af = hp->h_addrtype;
+ hent.h_length = hp->h_length;
- HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
- for (anum = 0; hp->h_aliases[anum]; anum++) {
- if (anum >= MAXALIASES)
- goto nospc;
- HENT_SCOPY(aliases[anum], hp->h_aliases[anum],
- ptr, len);
- }
- ptr = (void *)ALIGN(ptr);
- if ((size_t)(ptr - buf) >= info->buflen)
- goto nospc;
- }
+ HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
+ for (anum = 0; hp->h_aliases[anum]; anum++) {
+ if (anum >= MAXALIASES) goto nospc;
+ HENT_SCOPY(aliases[anum], hp->h_aliases[anum], ptr, len);
+ }
+ ptr = (void*) ALIGN(ptr);
+ if ((size_t)(ptr - buf) >= info->buflen) goto nospc;
+ }
- if (num >= MAXADDRS)
- goto nospc;
- HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
- len);
- num++;
- }
- endhostent_r(&hf);
+ if (num >= MAXADDRS) goto nospc;
+ HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr, len);
+ num++;
+ }
+ endhostent_r(&hf);
- if (num == 0) {
- *info->he = HOST_NOT_FOUND;
- free(buf);
- return NULL;
- }
+ if (num == 0) {
+ *info->he = HOST_NOT_FOUND;
+ free(buf);
+ return NULL;
+ }
- hp = info->hp;
- ptr = info->buf;
- len = info->buflen;
+ hp = info->hp;
+ ptr = info->buf;
+ len = info->buflen;
- hp->h_addrtype = hent.h_addrtype;
- hp->h_length = hent.h_length;
+ hp->h_addrtype = hent.h_addrtype;
+ hp->h_length = hent.h_length;
- HENT_ARRAY(hp->h_aliases, anum, ptr, len);
- HENT_ARRAY(hp->h_addr_list, num, ptr, len);
+ HENT_ARRAY(hp->h_aliases, anum, ptr, len);
+ HENT_ARRAY(hp->h_addr_list, num, ptr, len);
- for (i = 0; i < num; i++)
- HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr,
- len);
- hp->h_addr_list[num] = NULL;
+ for (i = 0; i < num; i++) HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr, len);
+ hp->h_addr_list[num] = NULL;
- HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
+ HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
- for (i = 0; i < anum; i++)
- HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
- hp->h_aliases[anum] = NULL;
+ for (i = 0; i < anum; i++) HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
+ hp->h_aliases[anum] = NULL;
- free(buf);
- return hp;
+ free(buf);
+ return hp;
nospc:
- *info->he = NETDB_INTERNAL;
- free(buf);
- errno = ENOSPC;
- return NULL;
+ *info->he = NETDB_INTERNAL;
+ free(buf);
+ errno = ENOSPC;
+ return NULL;
}
/*ARGSUSED*/
-int
-_hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
-{
- struct hostent *hp;
- const unsigned char *addr;
- struct getnamaddr *info = rv;
- FILE *hf;
+int _hf_gethtbyaddr(void* rv, void* cb_data, va_list ap) {
+ struct hostent* hp;
+ const unsigned char* addr;
+ struct getnamaddr* info = rv;
+ FILE* hf;
- _DIAGASSERT(rv != NULL);
+ _DIAGASSERT(rv != NULL);
- addr = va_arg(ap, unsigned char *);
- info->hp->h_length = va_arg(ap, int);
- info->hp->h_addrtype = va_arg(ap, int);
+ addr = va_arg(ap, unsigned char*);
+ info->hp->h_length = va_arg(ap, int);
+ info->hp->h_addrtype = va_arg(ap, int);
- hf = NULL;
- sethostent_r(&hf);
- if (hf == NULL) {
- *info->he = NETDB_INTERNAL;
- return NS_UNAVAIL;
- }
- while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
- info->he)) != NULL)
- if (!memcmp(hp->h_addr_list[0], addr, (size_t)hp->h_length))
- break;
- endhostent_r(&hf);
+ hf = NULL;
+ sethostent_r(&hf);
+ if (hf == NULL) {
+ *info->he = NETDB_INTERNAL;
+ return NS_UNAVAIL;
+ }
+ while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, info->he)) != NULL)
+ if (!memcmp(hp->h_addr_list[0], addr, (size_t) hp->h_length)) break;
+ endhostent_r(&hf);
- if (hp == NULL) {
- if (errno == ENOSPC) return NS_UNAVAIL; // glibc compatibility.
- *info->he = HOST_NOT_FOUND;
- return NS_NOTFOUND;
- }
- return NS_SUCCESS;
+ if (hp == NULL) {
+ if (errno == ENOSPC) return NS_UNAVAIL; // glibc compatibility.
+ *info->he = HOST_NOT_FOUND;
+ return NS_NOTFOUND;
+ }
+ return NS_SUCCESS;
}