Loading gethnamaddr.cpp +3 −51 Original line number Diff line number Diff line Loading @@ -106,13 +106,10 @@ typedef union { char ac; } align; static struct hostent* getanswer(const querybuf*, int, const char*, int, res_state, struct hostent*, char*, size_t, int*); static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep, const std::function<void(struct hostent* hp)>& mapping_param, const std::function<void(char* src, char* dst)>& mapping_addr); static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep); static void addrsort(char**, int, res_state); static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af, const android_net_context* netcontext, getnamaddr* info, Loading Loading @@ -145,8 +142,7 @@ static int android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t } while (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) { struct hostent* hent, char* buf, size_t buflen, int* he) { const HEADER* hp; const uint8_t* cp; int n; Loading Loading @@ -369,12 +365,6 @@ static struct hostent* getanswer(const querybuf* answer, int anslen, const char* } if (haveanswer) { *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; Loading Loading @@ -691,44 +681,6 @@ static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) { }); } static void addrsort(char** ap, int num, res_state res) { int i, j; char** p; short aval[MAXADDRS]; int needsort = 0; _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; 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; hp = ap[j]; ap[j] = ap[j + 1]; ap[j + 1] = hp; } else break; } needsort++; } } static int dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) { int n, type; info->hp->h_addrtype = addr_type; Loading Loading @@ -759,7 +711,7 @@ static int dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) { // See also herrnoToAiErrno(). return herrnoToAiErrno(he); } hostent* hp = getanswer(buf.get(), n, name, type, res, info->hp, info->buf, info->buflen, &he); hostent* hp = getanswer(buf.get(), n, name, type, info->hp, info->buf, info->buflen, &he); if (hp == NULL) return herrnoToAiErrno(he); return 0; Loading Loading @@ -824,7 +776,7 @@ static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af, // See also herrnoToAiErrno(). return herrnoToAiErrno(he); } hostent* hp = getanswer(buf.get(), n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, &he); hostent* hp = getanswer(buf.get(), n, qbuf, T_PTR, info->hp, info->buf, info->buflen, &he); if (hp == NULL) return herrnoToAiErrno(he); char* bf = (char*) (hp->h_addr_list + 2); Loading res_cache.cpp +1 −10 Original line number Diff line number Diff line Loading @@ -1583,16 +1583,7 @@ void _resolv_populate_res_for_net(res_state statp) { } if ((size_t) ai->ai_addrlen <= sizeof(statp->_u._ext.ext->nsaddrs[0])) { if (statp->_u._ext.ext != NULL) { 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); } else { statp->nsaddr_list[nserv].sin_family = AF_UNSPEC; } } } else { LOG(INFO) << __func__ << ": found too long addrlen"; } Loading res_init.cpp +12 −106 Original line number Diff line number Diff line Loading @@ -94,22 +94,12 @@ #include "resolv_private.h" // Set up Resolver state default settings. // Note that res_ninit() is called with an initialized res_state, // the memories it allocated must be freed after the task is done. // Or memory leak will happen. int res_ninit(res_state statp) { int nserv = 0; // number of nameserver records sockaddr_union u[2]; // Note: this is called with statp zero-initialized void res_init(res_state statp) { statp->netid = NETID_UNSET; statp->id = arc4random_uniform(65536); statp->_mark = MARK_UNSET; memset(u, 0, sizeof(u)); u[nserv].sin.sin_addr.s_addr = INADDR_ANY; u[nserv].sin.sin_family = AF_INET; u[nserv].sin.sin_port = htons(NAMESERVER_PORT); nserv++; statp->nscount = 0; statp->ndots = 1; statp->_vcsock = -1; Loading @@ -119,20 +109,20 @@ int res_ninit(res_state statp) { statp->netcontext_flags = 0; 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 (nserv > 0) { statp->nscount = nserv; } return (0); // The following dummy initialization is probably useless because // it's overwritten later by _resolv_populate_res_for_net(). // TODO: check if it's safe to remove. const sockaddr_union u{ .sin.sin_addr.s_addr = INADDR_ANY, .sin.sin_family = AF_INET, .sin.sin_port = htons(NAMESERVER_PORT), }; memcpy(&statp->_u._ext.ext->nsaddrs[0], &u, sizeof(u)); statp->nscount = 1; } /* * This routine is for closing the socket if a virtual circuit is used and * the program wants to close it. This provides support for endhostent() Loading Loading @@ -162,90 +152,6 @@ void res_ndestroy(res_state statp) { statp->_u._ext.ext = NULL; } void res_setservers(res_state statp, const sockaddr_union* set, int cnt) { int i, nserv; size_t size; /* close open servers */ res_nclose(statp); /* 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; #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; #endif default: break; } set++; } statp->nscount = nserv; } int res_getservers(res_state statp, 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; 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; #endif default: set->sin.sin_family = 0; break; } set++; } return (statp->nscount); } void res_setnetcontext(res_state statp, const struct android_net_context* netcontext, android::net::NetworkDnsEventReported* _Nonnull event) { if (statp != nullptr) { Loading res_init.h 0 → 100644 +19 −0 Original line number Diff line number Diff line /* * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once struct __res_state; void res_init(__res_state* statp); res_send.cpp +1 −27 Original line number Diff line number Diff line Loading @@ -470,13 +470,6 @@ int res_nsend(res_state statp, const uint8_t* buf, int buflen, uint8_t* ans, int needclose++; } else { for (int 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*) &statp->_u._ext.ext->nsaddrs[ns])) { needclose++; break; } if (statp->_u._ext.nssocks[ns] == -1) continue; peerlen = sizeof(peer); if (getpeername(statp->_u._ext.nssocks[ns], (struct sockaddr*) (void*) &peer, Loading @@ -503,8 +496,6 @@ int res_nsend(res_state statp, const uint8_t* buf, int buflen, uint8_t* ans, int for (int ns = 0; ns < statp->nscount; ns++) { statp->_u._ext.nstimes[ns] = RES_MAXTIME; statp->_u._ext.nssocks[ns] = -1; if (!statp->nsaddr_list[ns].sin_family) continue; statp->_u._ext.ext->nsaddrs[ns].sin = statp->nsaddr_list[ns]; } statp->_u._ext.nscount = statp->nscount; } Loading Loading @@ -692,25 +683,8 @@ static int get_salen(const struct sockaddr* sa) { return (0); /* unknown, die on connect */ } /* * pick appropriate nsaddr_list for use. see res_init() for initialization. */ static struct sockaddr* get_nsaddr(res_state statp, size_t n) { if (!statp->nsaddr_list[n].sin_family && statp->_u._ext.ext) { /* * - statp->_u._ext.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*)&statp->_u._ext.ext->nsaddrs[n]; } else { /* * - user code updated statp->nsaddr_list[n], or * - statp->nsaddr_list[n] has the same content as * statp->_u._ext.ext->nsaddrs[n]. */ return (struct sockaddr*) (void*) &statp->nsaddr_list[n]; } } static struct timespec get_timeout(res_state statp, const res_params* params, const int ns) { Loading Loading
gethnamaddr.cpp +3 −51 Original line number Diff line number Diff line Loading @@ -106,13 +106,10 @@ typedef union { char ac; } align; static struct hostent* getanswer(const querybuf*, int, const char*, int, res_state, struct hostent*, char*, size_t, int*); static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep, const std::function<void(struct hostent* hp)>& mapping_param, const std::function<void(char* src, char* dst)>& mapping_addr); static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep); static void addrsort(char**, int, res_state); static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af, const android_net_context* netcontext, getnamaddr* info, Loading Loading @@ -145,8 +142,7 @@ static int android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t } while (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) { struct hostent* hent, char* buf, size_t buflen, int* he) { const HEADER* hp; const uint8_t* cp; int n; Loading Loading @@ -369,12 +365,6 @@ static struct hostent* getanswer(const querybuf* answer, int anslen, const char* } if (haveanswer) { *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; Loading Loading @@ -691,44 +681,6 @@ static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) { }); } static void addrsort(char** ap, int num, res_state res) { int i, j; char** p; short aval[MAXADDRS]; int needsort = 0; _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; 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; hp = ap[j]; ap[j] = ap[j + 1]; ap[j + 1] = hp; } else break; } needsort++; } } static int dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) { int n, type; info->hp->h_addrtype = addr_type; Loading Loading @@ -759,7 +711,7 @@ static int dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) { // See also herrnoToAiErrno(). return herrnoToAiErrno(he); } hostent* hp = getanswer(buf.get(), n, name, type, res, info->hp, info->buf, info->buflen, &he); hostent* hp = getanswer(buf.get(), n, name, type, info->hp, info->buf, info->buflen, &he); if (hp == NULL) return herrnoToAiErrno(he); return 0; Loading Loading @@ -824,7 +776,7 @@ static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af, // See also herrnoToAiErrno(). return herrnoToAiErrno(he); } hostent* hp = getanswer(buf.get(), n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, &he); hostent* hp = getanswer(buf.get(), n, qbuf, T_PTR, info->hp, info->buf, info->buflen, &he); if (hp == NULL) return herrnoToAiErrno(he); char* bf = (char*) (hp->h_addr_list + 2); Loading
res_cache.cpp +1 −10 Original line number Diff line number Diff line Loading @@ -1583,16 +1583,7 @@ void _resolv_populate_res_for_net(res_state statp) { } if ((size_t) ai->ai_addrlen <= sizeof(statp->_u._ext.ext->nsaddrs[0])) { if (statp->_u._ext.ext != NULL) { 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); } else { statp->nsaddr_list[nserv].sin_family = AF_UNSPEC; } } } else { LOG(INFO) << __func__ << ": found too long addrlen"; } Loading
res_init.cpp +12 −106 Original line number Diff line number Diff line Loading @@ -94,22 +94,12 @@ #include "resolv_private.h" // Set up Resolver state default settings. // Note that res_ninit() is called with an initialized res_state, // the memories it allocated must be freed after the task is done. // Or memory leak will happen. int res_ninit(res_state statp) { int nserv = 0; // number of nameserver records sockaddr_union u[2]; // Note: this is called with statp zero-initialized void res_init(res_state statp) { statp->netid = NETID_UNSET; statp->id = arc4random_uniform(65536); statp->_mark = MARK_UNSET; memset(u, 0, sizeof(u)); u[nserv].sin.sin_addr.s_addr = INADDR_ANY; u[nserv].sin.sin_family = AF_INET; u[nserv].sin.sin_port = htons(NAMESERVER_PORT); nserv++; statp->nscount = 0; statp->ndots = 1; statp->_vcsock = -1; Loading @@ -119,20 +109,20 @@ int res_ninit(res_state statp) { statp->netcontext_flags = 0; 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 (nserv > 0) { statp->nscount = nserv; } return (0); // The following dummy initialization is probably useless because // it's overwritten later by _resolv_populate_res_for_net(). // TODO: check if it's safe to remove. const sockaddr_union u{ .sin.sin_addr.s_addr = INADDR_ANY, .sin.sin_family = AF_INET, .sin.sin_port = htons(NAMESERVER_PORT), }; memcpy(&statp->_u._ext.ext->nsaddrs[0], &u, sizeof(u)); statp->nscount = 1; } /* * This routine is for closing the socket if a virtual circuit is used and * the program wants to close it. This provides support for endhostent() Loading Loading @@ -162,90 +152,6 @@ void res_ndestroy(res_state statp) { statp->_u._ext.ext = NULL; } void res_setservers(res_state statp, const sockaddr_union* set, int cnt) { int i, nserv; size_t size; /* close open servers */ res_nclose(statp); /* 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; #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; #endif default: break; } set++; } statp->nscount = nserv; } int res_getservers(res_state statp, 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; 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; #endif default: set->sin.sin_family = 0; break; } set++; } return (statp->nscount); } void res_setnetcontext(res_state statp, const struct android_net_context* netcontext, android::net::NetworkDnsEventReported* _Nonnull event) { if (statp != nullptr) { Loading
res_init.h 0 → 100644 +19 −0 Original line number Diff line number Diff line /* * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once struct __res_state; void res_init(__res_state* statp);
res_send.cpp +1 −27 Original line number Diff line number Diff line Loading @@ -470,13 +470,6 @@ int res_nsend(res_state statp, const uint8_t* buf, int buflen, uint8_t* ans, int needclose++; } else { for (int 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*) &statp->_u._ext.ext->nsaddrs[ns])) { needclose++; break; } if (statp->_u._ext.nssocks[ns] == -1) continue; peerlen = sizeof(peer); if (getpeername(statp->_u._ext.nssocks[ns], (struct sockaddr*) (void*) &peer, Loading @@ -503,8 +496,6 @@ int res_nsend(res_state statp, const uint8_t* buf, int buflen, uint8_t* ans, int for (int ns = 0; ns < statp->nscount; ns++) { statp->_u._ext.nstimes[ns] = RES_MAXTIME; statp->_u._ext.nssocks[ns] = -1; if (!statp->nsaddr_list[ns].sin_family) continue; statp->_u._ext.ext->nsaddrs[ns].sin = statp->nsaddr_list[ns]; } statp->_u._ext.nscount = statp->nscount; } Loading Loading @@ -692,25 +683,8 @@ static int get_salen(const struct sockaddr* sa) { return (0); /* unknown, die on connect */ } /* * pick appropriate nsaddr_list for use. see res_init() for initialization. */ static struct sockaddr* get_nsaddr(res_state statp, size_t n) { if (!statp->nsaddr_list[n].sin_family && statp->_u._ext.ext) { /* * - statp->_u._ext.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*)&statp->_u._ext.ext->nsaddrs[n]; } else { /* * - user code updated statp->nsaddr_list[n], or * - statp->nsaddr_list[n] has the same content as * statp->_u._ext.ext->nsaddrs[n]. */ return (struct sockaddr*) (void*) &statp->nsaddr_list[n]; } } static struct timespec get_timeout(res_state statp, const res_params* params, const int ns) { Loading