123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- #define LOG_TAG "res_state"
- #include <arpa/inet.h>
- #include <arpa/nameser.h>
- #include <netdb.h>
- #include <pthread.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <unistd.h> /* for gettid() */
- #include <android-base/logging.h>
- #include "resolv_cache.h"
- #include "resolv_private.h"
- typedef struct {
-
- struct __res_state _nres[1];
- struct res_static _rstatic[1];
- } _res_thread;
- static _res_thread* res_thread_alloc(void) {
- _res_thread* rt = (_res_thread*) calloc(1, sizeof(*rt));
- if (rt) {
- memset(rt->_rstatic, 0, sizeof rt->_rstatic);
- }
- return rt;
- }
- static void res_static_done(struct res_static* rs) {
-
- if (rs->hostf) {
- fclose(rs->hostf);
- rs->hostf = NULL;
- }
- free(rs->servent.s_aliases);
- }
- static void res_thread_free(void* _rt) {
- _res_thread* rt = (_res_thread*) _rt;
- LOG(VERBOSE) << __func__ << ": rt=" << rt << " for thread=" << gettid();
- res_static_done(rt->_rstatic);
- res_ndestroy(rt->_nres);
- free(rt);
- }
- static pthread_key_t _res_key;
- __attribute__((constructor)) static void __res_key_init() {
- pthread_key_create(&_res_key, res_thread_free);
- }
- static _res_thread* res_thread_get(void) {
- _res_thread* rt = (_res_thread*) pthread_getspecific(_res_key);
- if (rt != NULL) {
- return rt;
- }
-
- rt = res_thread_alloc();
- if (rt == NULL) {
- return NULL;
- }
- pthread_setspecific(_res_key, rt);
-
- LOG(VERBOSE) << __func__ << ": tid=" << gettid() << ", rt=" << rt
- << " setting DNS state (options=" << rt->_nres->options << ")";
- if (res_ninit(rt->_nres) < 0) {
-
- LOG(VERBOSE) << __func__ << ": tid=" << gettid() << " rt=" << rt
- << ", res_ninit() returned < 0";
- res_thread_free(rt);
- pthread_setspecific(_res_key, NULL);
- return NULL;
- }
- return rt;
- }
- struct __res_state _nres;
- res_state res_get_state(void) {
- _res_thread* rt = res_thread_get();
- return rt ? rt->_nres : NULL;
- }
- res_static* res_get_static(void) {
- _res_thread* rt = res_thread_get();
- return rt ? rt->_rstatic : NULL;
- }
|