diff options
author | Ricardo Cerqueira <cyanogenmod@cerqueira.org> | 2013-11-01 17:20:25 +0000 |
---|---|---|
committer | Ricardo Cerqueira <cyanogenmod@cerqueira.org> | 2013-11-02 19:50:28 +0000 |
commit | c4050b03011a732c494443a87c2d14c23e5c08ea (patch) | |
tree | f5addc099699605ae861ecba92a211c30ce0b9a1 /libc/netbsd | |
parent | 3fcf18a70f7efd0ee1b91f11327114553a9f2565 (diff) | |
parent | 8e39d2f9f403639195e430f81a47ad3940bd6975 (diff) | |
download | bionic-c4050b03011a732c494443a87c2d14c23e5c08ea.zip bionic-c4050b03011a732c494443a87c2d14c23e5c08ea.tar.gz bionic-c4050b03011a732c494443a87c2d14c23e5c08ea.tar.bz2 |
Merge tag 'android-4.4_r1' into cm-11.0
Android 4.4 Release 1.0
Conflicts:
libc/arch-arm/arm.mk
libc/upstream-freebsd/lib/libc/stdlib/getopt_long.c
linker/linker_phdr.cpp
Diffstat (limited to 'libc/netbsd')
-rw-r--r-- | libc/netbsd/gethnamaddr.c | 32 | ||||
-rw-r--r-- | libc/netbsd/net/getaddrinfo.c | 42 | ||||
-rw-r--r-- | libc/netbsd/net/getnameinfo.c | 19 | ||||
-rw-r--r-- | libc/netbsd/net/reentrant.h | 1 | ||||
-rw-r--r-- | libc/netbsd/resolv/res_cache.c | 252 | ||||
-rw-r--r-- | libc/netbsd/resolv/res_init.c | 7 | ||||
-rw-r--r-- | libc/netbsd/resolv/res_send.c | 32 | ||||
-rw-r--r-- | libc/netbsd/resolv/res_state.c | 6 |
8 files changed, 307 insertions, 84 deletions
diff --git a/libc/netbsd/gethnamaddr.c b/libc/netbsd/gethnamaddr.c index ee5052e..5b2f987 100644 --- a/libc/netbsd/gethnamaddr.c +++ b/libc/netbsd/gethnamaddr.c @@ -126,7 +126,7 @@ static struct hostent *_gethtbyname2(const char *, int); 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, const char *); +static struct hostent *gethostbyname_internal(const char *, int, res_state, const char *, int); static const ns_src default_dns_files[] = { { NSSRC_FILES, NS_SUCCESS }, @@ -497,13 +497,13 @@ gethostbyname(const char *name) /* try IPv6 first - if that fails do IPv4 */ if (res->options & RES_USE_INET6) { - hp = gethostbyname_internal(name, AF_INET6, res, NULL); + hp = gethostbyname_internal(name, AF_INET6, res, NULL, 0); if (hp) { __res_put_state(res); return hp; } } - hp = gethostbyname_internal(name, AF_INET, res, NULL); + hp = gethostbyname_internal(name, AF_INET, res, NULL, 0); __res_put_state(res); return hp; } @@ -511,18 +511,18 @@ gethostbyname(const char *name) struct hostent * gethostbyname2(const char *name, int af) { - return android_gethostbynameforiface(name, af, NULL); + return android_gethostbynameforiface(name, af, NULL, 0); } struct hostent * -android_gethostbynameforiface(const char *name, int af, const char *iface) +android_gethostbynameforiface(const char *name, int af, const char *iface, int mark) { struct hostent *hp; res_state res = __res_get_state(); if (res == NULL) return NULL; - hp = gethostbyname_internal(name, af, res, iface); + hp = gethostbyname_internal(name, af, res, iface, mark); __res_put_state(res); return hp; } @@ -741,7 +741,7 @@ gethostbyname_internal_real(const char *name, int af, res_state res) // very similar in proxy-ness to android_getaddrinfo_proxy static struct hostent * -gethostbyname_internal(const char *name, int af, res_state res, const char *iface) +gethostbyname_internal(const char *name, int af, res_state res, const char *iface, int mark) { const char *cache_mode = getenv("ANDROID_DNS_MODE"); FILE* proxy = NULL; @@ -749,6 +749,7 @@ gethostbyname_internal(const char *name, int af, res_state res, const char *ifac if (cache_mode != NULL && strcmp(cache_mode, "local") == 0) { res_setiface(res, iface); + res_setmark(res, mark); return gethostbyname_internal_real(name, af, res); } @@ -780,7 +781,7 @@ exit: struct hostent * android_gethostbyaddrforiface_proxy(const void *addr, - socklen_t len, int af, const char* iface) + socklen_t len, int af, const char* iface, int mark) { struct hostent *result = NULL; FILE* proxy = android_open_proxy(); @@ -810,7 +811,7 @@ exit: struct hostent * android_gethostbyaddrforiface_real(const void *addr, - socklen_t len, int af, const char* iface) + socklen_t len, int af, const char* iface, int mark) { const u_char *uaddr = (const u_char *)addr; socklen_t size; @@ -858,28 +859,28 @@ android_gethostbyaddrforiface_real(const void *addr, hp = NULL; h_errno = NETDB_INTERNAL; if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyaddr", - default_dns_files, uaddr, len, af, iface) != NS_SUCCESS) + default_dns_files, uaddr, len, af, iface, mark) != NS_SUCCESS) return NULL; h_errno = NETDB_SUCCESS; return hp; } struct hostent * -android_gethostbyaddrforiface(const void *addr, socklen_t len, int af, const char* iface) +android_gethostbyaddrforiface(const void *addr, socklen_t len, int af, const char* iface, int mark) { const char *cache_mode = getenv("ANDROID_DNS_MODE"); if (cache_mode == NULL || strcmp(cache_mode, "local") != 0) { - return android_gethostbyaddrforiface_proxy(addr, len, af, iface); + return android_gethostbyaddrforiface_proxy(addr, len, af, iface, mark); } else { - return android_gethostbyaddrforiface_real(addr,len, af,iface); + return android_gethostbyaddrforiface_real(addr,len, af, iface, mark); } } struct hostent * gethostbyaddr(const void *addr, socklen_t len, int af) { - return android_gethostbyaddrforiface(addr, len, af, NULL); + return android_gethostbyaddrforiface(addr, len, af, NULL, 0); } @@ -1315,6 +1316,7 @@ _dns_gethtbyaddr(void *rv, void *cb_data, va_list ap) int len, af, advance; res_state res; const char* iface; + int mark; res_static rs = __res_get_static(); assert(rv != NULL); @@ -1323,6 +1325,7 @@ _dns_gethtbyaddr(void *rv, void *cb_data, va_list ap) len = va_arg(ap, int); af = va_arg(ap, int); iface = va_arg(ap, char *); + mark = va_arg(ap, int); switch (af) { case AF_INET: @@ -1365,6 +1368,7 @@ _dns_gethtbyaddr(void *rv, void *cb_data, va_list ap) return NS_NOTFOUND; } res_setiface(res, iface); + res_setmark(res, mark); n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf)); if (n < 0) { free(buf); diff --git a/libc/netbsd/net/getaddrinfo.c b/libc/netbsd/net/getaddrinfo.c index 401bc6e..937c423 100644 --- a/libc/netbsd/net/getaddrinfo.c +++ b/libc/netbsd/net/getaddrinfo.c @@ -93,6 +93,7 @@ #include <errno.h> #include <netdb.h> #include "resolv_private.h" +#include <stdbool.h> #include <stddef.h> #include <stdio.h> #include <stdlib.h> @@ -214,7 +215,7 @@ struct res_target { static int str2number(const char *); static int explore_fqdn(const struct addrinfo *, const char *, - const char *, struct addrinfo **, const char *iface); + const char *, struct addrinfo **, const char *iface, int mark); static int explore_null(const struct addrinfo *, const char *, struct addrinfo **); static int explore_numeric(const struct addrinfo *, const char *, @@ -577,12 +578,12 @@ int getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res) { - return android_getaddrinfoforiface(hostname, servname, hints, NULL, res); + return android_getaddrinfoforiface(hostname, servname, hints, NULL, 0, res); } int android_getaddrinfoforiface(const char *hostname, const char *servname, - const struct addrinfo *hints, const char *iface, struct addrinfo **res) + const struct addrinfo *hints, const char *iface, int mark, struct addrinfo **res) { struct addrinfo sentinel; struct addrinfo *cur; @@ -761,7 +762,7 @@ android_getaddrinfoforiface(const char *hostname, const char *servname, pai->ai_protocol = ex->e_protocol; error = explore_fqdn(pai, hostname, servname, - &cur->ai_next, iface); + &cur->ai_next, iface, mark); while (cur && cur->ai_next) cur = cur->ai_next; @@ -794,7 +795,7 @@ android_getaddrinfoforiface(const char *hostname, const char *servname, */ static int explore_fqdn(const struct addrinfo *pai, const char *hostname, - const char *servname, struct addrinfo **res, const char *iface) + const char *servname, struct addrinfo **res, const char *iface, int mark) { struct addrinfo *result; struct addrinfo *cur; @@ -820,7 +821,7 @@ explore_fqdn(const struct addrinfo *pai, const char *hostname, return 0; switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", - default_dns_files, hostname, pai, iface)) { + default_dns_files, hostname, pai, iface, mark)) { case NS_TRYAGAIN: error = EAI_AGAIN; goto free; @@ -1864,17 +1865,19 @@ error: free(elems); } -static int _using_alt_dns() +static bool _using_default_dns(const char *iface) { - char propname[PROP_NAME_MAX]; - char propvalue[PROP_VALUE_MAX]; + char buf[IF_NAMESIZE+1]; + size_t if_len; - propvalue[0] = 0; - snprintf(propname, sizeof(propname), "net.dns1.%d", getpid()); - if (__system_property_get(propname, propvalue) > 0 ) { - return 1; + // common case + if (iface == NULL || *iface == '\0') return true; + + if_len = _resolv_get_default_iface(buf, sizeof(buf)); + if (if_len != 0 && if_len + 1 <= sizeof(buf)) { + if (strcmp(buf, iface) == 0) return true; } - return 0; + return false; } /*ARGSUSED*/ @@ -1889,10 +1892,12 @@ _dns_getaddrinfo(void *rv, void *cb_data, va_list ap) struct res_target q, q2; res_state res; const char* iface; + int mark; name = va_arg(ap, char *); pai = va_arg(ap, const struct addrinfo *); iface = va_arg(ap, char *); + mark = va_arg(ap, int); //fprintf(stderr, "_dns_getaddrinfo() name = '%s'\n", name); memset(&q, 0, sizeof(q)); @@ -1924,7 +1929,7 @@ _dns_getaddrinfo(void *rv, void *cb_data, va_list ap) // Only implement AI_ADDRCONFIG if the application is not // using its own DNS servers, since our implementation // only works on the default connection. - if (!_using_alt_dns()) { + if (_using_default_dns(iface)) { query_ipv6 = _have_ipv6(); query_ipv4 = _have_ipv4(); } @@ -1980,6 +1985,7 @@ _dns_getaddrinfo(void *rv, void *cb_data, va_list ap) * and have a cache hit that would be wasted, so we do the rest there on miss */ res_setiface(res, iface); + res_setmark(res, mark); if (res_searchN(name, &q, res) < 0) { __res_put_state(res); free(buf); @@ -2310,6 +2316,12 @@ res_searchN(const char *name, struct res_target *target, res_state res) (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_iface(res); + for (domain = (const char * const *)res->dnsrch; *domain && !done; domain++) { diff --git a/libc/netbsd/net/getnameinfo.c b/libc/netbsd/net/getnameinfo.c index ade5240..15d2675 100644 --- a/libc/netbsd/net/getnameinfo.c +++ b/libc/netbsd/net/getnameinfo.c @@ -93,7 +93,7 @@ struct sockinet { }; static int getnameinfo_inet(const struct sockaddr *, socklen_t, char *, - socklen_t, char *, socklen_t, int, const char*); + socklen_t, char *, socklen_t, int, const char*, int); #ifdef INET6 static int ip6_parsenumeric(const struct sockaddr *, const char *, char *, socklen_t, int); @@ -108,16 +108,16 @@ static int getnameinfo_local(const struct sockaddr *, socklen_t, char *, */ int getnameinfo(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags) { - return android_getnameinfoforiface(sa, salen, host, hostlen, serv, servlen, flags, NULL); + return android_getnameinfoforiface(sa, salen, host, hostlen, serv, servlen, flags, NULL, 0); } -int android_getnameinfoforiface(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags, const char* iface) +int android_getnameinfoforiface(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags, const char* iface, int mark) { switch (sa->sa_family) { case AF_INET: case AF_INET6: return getnameinfo_inet(sa, salen, host, hostlen, - serv, servlen, flags, iface); + serv, servlen, flags, iface, mark); case AF_LOCAL: return getnameinfo_local(sa, salen, host, hostlen, serv, servlen, flags); @@ -158,10 +158,10 @@ getnameinfo_local(const struct sockaddr *sa, socklen_t salen, * the address. On failure -1 is returned in which case * normal execution flow shall continue. */ static int -android_gethostbyaddr_proxy(char* nameBuf, size_t nameBufLen, const void *addr, socklen_t addrLen, int addrFamily, const char* iface) +android_gethostbyaddr_proxy(char* nameBuf, size_t nameBufLen, const void *addr, socklen_t addrLen, int addrFamily, const char* iface, int mark) { struct hostent *hostResult = - android_gethostbyaddrforiface_proxy(addr, addrLen, addrFamily, iface); + android_gethostbyaddrforiface_proxy(addr, addrLen, addrFamily, iface, mark); if (hostResult == NULL) return 0; @@ -179,7 +179,7 @@ static int getnameinfo_inet(const struct sockaddr* sa, socklen_t salen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, - int flags, const char* iface) + int flags, const char* iface, int mark) { const struct afd *afd; struct servent *sp; @@ -321,14 +321,15 @@ getnameinfo_inet(const struct sockaddr* sa, socklen_t salen, char android_proxy_buf[MAXDNAME]; int hostnamelen = android_gethostbyaddr_proxy(android_proxy_buf, - MAXDNAME, addr, afd->a_addrlen, afd->a_af, iface); + MAXDNAME, addr, afd->a_addrlen, afd->a_af, iface, mark); if (hostnamelen > 0) { hp = &android_proxy_hostent; hp->h_name = android_proxy_buf; } else if (!hostnamelen) { hp = NULL; } else { - hp = android_gethostbyaddrforiface(addr, afd->a_addrlen, afd->a_af, iface); + hp = android_gethostbyaddrforiface(addr, afd->a_addrlen, afd->a_af, + iface, mark); } if (hp) { diff --git a/libc/netbsd/net/reentrant.h b/libc/netbsd/net/reentrant.h index 15507eb..60bff08 100644 --- a/libc/netbsd/net/reentrant.h +++ b/libc/netbsd/net/reentrant.h @@ -102,6 +102,7 @@ #include <pthread.h> #include <signal.h> +#include <sys/cdefs.h> #define mutex_t pthread_mutex_t #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER diff --git a/libc/netbsd/resolv/res_cache.c b/libc/netbsd/resolv/res_cache.c index 829bf10..8a6dc83 100644 --- a/libc/netbsd/resolv/res_cache.c +++ b/libc/netbsd/resolv/res_cache.c @@ -1258,6 +1258,12 @@ typedef struct resolv_pidiface_info { char ifname[IF_NAMESIZE + 1]; struct resolv_pidiface_info* next; } PidIfaceInfo; +typedef struct resolv_uidiface_info { + int uid_start; + int uid_end; + char ifname[IF_NAMESIZE + 1]; + struct resolv_uidiface_info* next; +} UidIfaceInfo; #define HTABLE_VALID(x) ((x) != NULL && (x) != HTABLE_DELETED) @@ -1796,6 +1802,9 @@ static struct resolv_cache_info _res_cache_list; // List of pid iface pairs static struct resolv_pidiface_info _res_pidiface_list; +// List of uid iface pairs +static struct resolv_uidiface_info _res_uidiface_list; + // name of the current default inteface static char _res_default_ifname[IF_NAMESIZE + 1]; @@ -1805,6 +1814,9 @@ static pthread_mutex_t _res_cache_list_lock; // lock protecting the _res_pid_iface_list static pthread_mutex_t _res_pidiface_list_lock; +// lock protecting the _res_uidiface_list +static pthread_mutex_t _res_uidiface_list_lock; + /* lookup the default interface name */ static char *_get_default_iface_locked(); /* find the first cache that has an associated interface and return the name of the interface */ @@ -1833,12 +1845,19 @@ static struct in_addr* _get_addr_locked(const char * ifname); /* 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, - char** servers, int numservers); + const char** servers, int numservers); /* remove a resolv_pidiface_info structure from _res_pidiface_list */ static void _remove_pidiface_info_locked(int pid); /* get a resolv_pidiface_info structure from _res_pidiface_list with a certain pid */ static struct resolv_pidiface_info* _get_pid_iface_info_locked(int pid); +/* remove a resolv_pidiface_info structure from _res_uidiface_list */ +static int _remove_uidiface_info_locked(int uid_start, int uid_end); +/* check if a range [low,high] overlaps with any already existing ranges in the uid=>iface map*/ +static int _resolv_check_uid_range_overlap_locked(int uid_start, int uid_end); +/* get a resolv_uidiface_info structure from _res_uidiface_list with a certain uid */ +static struct resolv_uidiface_info* _get_uid_iface_info_locked(int uid); + static void _res_cache_init(void) { @@ -1852,8 +1871,10 @@ _res_cache_init(void) memset(&_res_default_ifname, 0, sizeof(_res_default_ifname)); memset(&_res_cache_list, 0, sizeof(_res_cache_list)); memset(&_res_pidiface_list, 0, sizeof(_res_pidiface_list)); + memset(&_res_uidiface_list, 0, sizeof(_res_uidiface_list)); pthread_mutex_init(&_res_cache_list_lock, NULL); pthread_mutex_init(&_res_pidiface_list_lock, NULL); + pthread_mutex_init(&_res_uidiface_list_lock, NULL); } struct resolv_cache* @@ -2076,7 +2097,7 @@ _resolv_set_default_iface(const char* ifname) } void -_resolv_set_nameservers_for_iface(const char* ifname, char** servers, int numservers, +_resolv_set_nameservers_for_iface(const char* ifname, const char** servers, int numservers, const char *domains) { int i, rt, index; @@ -2149,7 +2170,7 @@ _resolv_set_nameservers_for_iface(const char* ifname, char** servers, int numser static int _resolv_is_nameservers_equal_locked(struct resolv_cache_info* cache_info, - char** servers, int numservers) + const char** servers, int numservers) { int i; char** ns; @@ -2271,8 +2292,8 @@ _resolv_set_addr_of_iface(const char* ifname, struct in_addr* addr) memcpy(&cache_info->ifaddr, addr, sizeof(*addr)); if (DEBUG) { - char* addr_s = inet_ntoa(cache_info->ifaddr); - XLOG("address of interface %s is %s\n", ifname, addr_s); + XLOG("address of interface %s is %s\n", + ifname, inet_ntoa(cache_info->ifaddr)); } } pthread_mutex_unlock(&_res_cache_list_lock); @@ -2411,33 +2432,188 @@ _resolv_get_pids_associated_interface(int pid, char* buff, int buffLen) return len; } +static int +_remove_uidiface_info_locked(int uid_start, int uid_end) { + struct resolv_uidiface_info* result = _res_uidiface_list.next; + struct resolv_uidiface_info* prev = &_res_uidiface_list; + + while (result != NULL && result->uid_start != uid_start && result->uid_end != uid_end) { + prev = result; + result = result->next; + } + if (prev != NULL && result != NULL) { + prev->next = result->next; + free(result); + return 0; + } + errno = EINVAL; + return -1; +} + +static struct resolv_uidiface_info* +_get_uid_iface_info_locked(int uid) +{ + struct resolv_uidiface_info* result = _res_uidiface_list.next; + while (result != NULL && !(result->uid_start <= uid && result->uid_end >= uid)) { + result = result->next; + } + + return result; +} + +static int +_resolv_check_uid_range_overlap_locked(int uid_start, int uid_end) +{ + struct resolv_uidiface_info* cur = _res_uidiface_list.next; + while (cur != NULL) { + if (cur->uid_start <= uid_end && cur->uid_end >= uid_start) { + return -1; + } + cur = cur->next; + } + return 0; +} + +void +_resolv_clear_iface_uid_range_mapping() +{ + pthread_once(&_res_cache_once, _res_cache_init); + pthread_mutex_lock(&_res_uidiface_list_lock); + struct resolv_uidiface_info *current = _res_uidiface_list.next; + struct resolv_uidiface_info *next; + while (current != NULL) { + next = current->next; + free(current); + current = next; + } + _res_uidiface_list.next = NULL; + pthread_mutex_unlock(&_res_uidiface_list_lock); +} + +void +_resolv_clear_iface_pid_mapping() +{ + pthread_once(&_res_cache_once, _res_cache_init); + pthread_mutex_lock(&_res_pidiface_list_lock); + struct resolv_pidiface_info *current = _res_pidiface_list.next; + struct resolv_pidiface_info *next; + while (current != NULL) { + next = current->next; + free(current); + current = next; + } + _res_pidiface_list.next = NULL; + pthread_mutex_unlock(&_res_pidiface_list_lock); +} + int -_resolv_get_default_iface(char* buff, int buffLen) +_resolv_set_iface_for_uid_range(const char* ifname, int uid_start, int uid_end) +{ + int rv = 0; + struct resolv_uidiface_info* uidiface_info; + // make sure the uid iface list is created + pthread_once(&_res_cache_once, _res_cache_init); + if (uid_start > uid_end) { + errno = EINVAL; + return -1; + } + pthread_mutex_lock(&_res_uidiface_list_lock); + //check that we aren't adding an overlapping range + if (!_resolv_check_uid_range_overlap_locked(uid_start, uid_end)) { + uidiface_info = calloc(sizeof(*uidiface_info), 1); + if (uidiface_info) { + uidiface_info->uid_start = uid_start; + uidiface_info->uid_end = uid_end; + int len = sizeof(uidiface_info->ifname); + strncpy(uidiface_info->ifname, ifname, len - 1); + uidiface_info->ifname[len - 1] = '\0'; + + uidiface_info->next = _res_uidiface_list.next; + _res_uidiface_list.next = uidiface_info; + + XLOG("_resolv_set_iface_for_uid_range: [%d,%d], iface %s\n", uid_start, uid_end, + ifname); + } else { + XLOG("_resolv_set_iface_for_uid_range failing calloc\n"); + rv = -1; + errno = EINVAL; + } + } else { + XLOG("_resolv_set_iface_for_uid_range range [%d,%d] overlaps\n", uid_start, uid_end); + rv = -1; + errno = EINVAL; + } + + pthread_mutex_unlock(&_res_uidiface_list_lock); + return rv; +} + +int +_resolv_clear_iface_for_uid_range(int uid_start, int uid_end) +{ + pthread_once(&_res_cache_once, _res_cache_init); + pthread_mutex_lock(&_res_uidiface_list_lock); + + int rv = _remove_uidiface_info_locked(uid_start, uid_end); + + XLOG("_resolv_clear_iface_for_uid_range: [%d,%d]\n", uid_start, uid_end); + + pthread_mutex_unlock(&_res_uidiface_list_lock); + + return rv; +} + +int +_resolv_get_uids_associated_interface(int uid, char* buff, int buffLen) { - char* ifname; int len = 0; - if (!buff || buffLen == 0) { + if (!buff) { return -1; } pthread_once(&_res_cache_once, _res_cache_init); + pthread_mutex_lock(&_res_uidiface_list_lock); + + struct resolv_uidiface_info* uidiface_info = _get_uid_iface_info_locked(uid); + buff[0] = '\0'; + if (uidiface_info) { + len = strlen(uidiface_info->ifname); + if (len < buffLen) { + strncpy(buff, uidiface_info->ifname, len); + buff[len] = '\0'; + } + } + + XLOG("_resolv_get_uids_associated_interface buff: %s\n", buff); + + pthread_mutex_unlock(&_res_uidiface_list_lock); + + return len; +} + +size_t +_resolv_get_default_iface(char* buff, size_t buffLen) +{ + if (!buff || buffLen == 0) { + return 0; + } + + pthread_once(&_res_cache_once, _res_cache_init); pthread_mutex_lock(&_res_cache_list_lock); - ifname = _get_default_iface_locked(); // never null, but may be empty + char* ifname = _get_default_iface_locked(); // never null, but may be empty - // if default interface not set. Get first cache with an interface + // if default interface not set give up. if (ifname[0] == '\0') { - ifname = _find_any_iface_name_locked(); // may be null + pthread_mutex_unlock(&_res_cache_list_lock); + return 0; } - // if we got the default iface or if (no-default) the find_any call gave an answer - if (ifname) { - len = strlen(ifname); - if (len < buffLen) { - strncpy(buff, ifname, len); - buff[len] = '\0'; - } + size_t len = strlen(ifname); + if (len < buffLen) { + strncpy(buff, ifname, len); + buff[len] = '\0'; } else { buff[0] = '\0'; } @@ -2447,28 +2623,32 @@ _resolv_get_default_iface(char* buff, int buffLen) return len; } -int +void _resolv_populate_res_for_iface(res_state statp) { - int nserv; - struct resolv_cache_info* info = NULL; - - if (statp) { - struct addrinfo* ai; + if (statp == NULL) { + return; + } - if (statp->iface[0] == '\0') { // no interface set assign default - _resolv_get_default_iface(statp->iface, sizeof(statp->iface)); + if (statp->iface[0] == '\0') { // no interface set assign default + size_t if_len = _resolv_get_default_iface(statp->iface, sizeof(statp->iface)); + if (if_len + 1 > sizeof(statp->iface)) { + XLOG("%s: INTERNAL_ERROR: can't fit interface name into statp->iface.\n", __FUNCTION__); + return; } - - pthread_once(&_res_cache_once, _res_cache_init); - pthread_mutex_lock(&_res_cache_list_lock); - info = _find_cache_info_locked(statp->iface); - - if (info == NULL) { - pthread_mutex_unlock(&_res_cache_list_lock); - return 0; + if (if_len == 0) { + XLOG("%s: INTERNAL_ERROR: can't find any suitable interfaces.\n", __FUNCTION__); + return; } + } + pthread_once(&_res_cache_once, _res_cache_init); + pthread_mutex_lock(&_res_cache_list_lock); + + struct resolv_cache_info* info = _find_cache_info_locked(statp->iface); + if (info != NULL) { + int nserv; + struct addrinfo* ai; XLOG("_resolv_populate_res_for_iface: %s\n", statp->iface); for (nserv = 0; nserv < MAXNS; nserv++) { ai = info->nsaddrinfo[nserv]; @@ -2502,8 +2682,6 @@ _resolv_populate_res_for_iface(res_state statp) while (pp < statp->dnsrch + MAXDNSRCH && *p != -1) { *pp++ = &statp->defdname + *p++; } - - pthread_mutex_unlock(&_res_cache_list_lock); } - return nserv; + pthread_mutex_unlock(&_res_cache_list_lock); } diff --git a/libc/netbsd/resolv/res_init.c b/libc/netbsd/resolv/res_init.c index ff65299..ceb412b 100644 --- a/libc/netbsd/resolv/res_init.c +++ b/libc/netbsd/resolv/res_init.c @@ -806,4 +806,11 @@ void res_setiface(res_state statp, const char* iface) } } } + +void res_setmark(res_state statp, int mark) +{ + if (statp != NULL) { + statp->_mark = mark; + } +} #endif /* ANDROID_CHANGES */ diff --git a/libc/netbsd/resolv/res_send.c b/libc/netbsd/resolv/res_send.c index d407ac8..f65b015 100644 --- a/libc/netbsd/resolv/res_send.c +++ b/libc/netbsd/resolv/res_send.c @@ -581,8 +581,8 @@ res_nsend(res_state statp, if (n == 0) goto next_ns; if (DBG) { - __libc_format_log(ANDROID_LOG_DEBUG, "libc", - "time=%d, %d\n",time(NULL), time(NULL)%2); + __libc_format_log(ANDROID_LOG_DEBUG, "libc", "time=%ld\n", + time(NULL)); } if (v_circuit) goto same_ns; @@ -762,10 +762,13 @@ send_vc(res_state statp, if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) { struct sockaddr_storage peer; socklen_t size = sizeof peer; - + int old_mark; + int mark_size = sizeof(old_mark); if (getpeername(statp->_vcsock, (struct sockaddr *)(void *)&peer, &size) < 0 || - !sock_eq((struct sockaddr *)(void *)&peer, nsap)) { + !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; } @@ -795,6 +798,14 @@ send_vc(res_state statp, return (-1); } } + if (statp->_mark != 0) { + 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; @@ -961,7 +972,7 @@ done: fcntl(sock, F_SETFL, origflags); if (DBG) { __libc_format_log(ANDROID_LOG_DEBUG, "libc", - " %d connect_with_timeout returning %s\n", sock, res); + " %d connect_with_timeout returning %d\n", sock, res); } return res; } @@ -1025,7 +1036,7 @@ retry: } if (DBG) { __libc_format_log(ANDROID_LOG_DEBUG, "libc", - " %d retrying_select returning %d for %d\n",sock, n); + " %d retrying_select returning %d\n",sock, n); } return n; @@ -1070,6 +1081,14 @@ send_dg(res_state statp, return (-1); } } + + if (statp->_mark != 0) { + 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, @@ -1097,6 +1116,7 @@ send_dg(res_state statp, #endif /* !CANNOT_CONNECT_DGRAM */ Dprint(statp->options & RES_DEBUG, (stdout, ";; new DG socket\n")) + } s = EXT(statp).nssocks[ns]; #ifndef CANNOT_CONNECT_DGRAM diff --git a/libc/netbsd/resolv/res_state.c b/libc/netbsd/resolv/res_state.c index 32ffdca..de82e1a 100644 --- a/libc/netbsd/resolv/res_state.c +++ b/libc/netbsd/resolv/res_state.c @@ -71,7 +71,7 @@ _res_thread_alloc(void) rt->_serial = 0; rt->_pi = (struct prop_info*) __system_property_find("net.change"); if (rt->_pi) { - rt->_serial = rt->_pi->serial; + rt->_serial = __system_property_serial(rt->_pi); } memset(rt->_rstatic, 0, sizeof rt->_rstatic); } @@ -135,14 +135,14 @@ _res_thread_get(void) return rt; } } - if (rt->_serial == rt->_pi->serial) { + if (rt->_serial == __system_property_serial(rt->_pi)) { /* Nothing changed, so return the current state */ D("%s: tid=%d rt=%p nothing changed, returning", __FUNCTION__, gettid(), rt); return rt; } /* Update the recorded serial number, and go reset the state */ - rt->_serial = rt->_pi->serial; + rt->_serial = __system_property_serial(rt->_pi); goto RESET_STATE; } |