Elliott Hughes | d0bbfa8 | 2021-04-08 11:58:51 -0700 | [diff] [blame] | 1 | /* $NetBSD: res_data.c,v 1.8 2004/06/09 18:07:03 christos Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") |
| 5 | * Copyright (c) 1995-1999 by Internet Software Consortium. |
| 6 | * |
| 7 | * Permission to use, copy, modify, and distribute this software for any |
| 8 | * purpose with or without fee is hereby granted, provided that the above |
| 9 | * copyright notice and this permission notice appear in all copies. |
| 10 | * |
| 11 | * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES |
| 12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR |
| 14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 17 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 18 | */ |
| 19 | |
| 20 | #include "resolv_private.h" |
| 21 | |
| 22 | #include <pthread.h> |
| 23 | |
| 24 | extern "C" int res_ourserver_p(const res_state, const struct sockaddr*); |
| 25 | extern "C" int __res_vinit(res_state, int); |
| 26 | |
| 27 | class GlobalStateAccessor { |
| 28 | public: |
| 29 | GlobalStateAccessor() { |
| 30 | pthread_mutex_lock(&mutex); |
| 31 | if (!initialized) { |
| 32 | init(); |
| 33 | initialized = true; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | ~GlobalStateAccessor() { |
| 38 | pthread_mutex_unlock(&mutex); |
| 39 | } |
| 40 | |
| 41 | __res_state* get() { |
| 42 | return &state; |
| 43 | } |
| 44 | |
| 45 | int init(); |
| 46 | |
| 47 | private: |
| 48 | static __res_state state; |
| 49 | static bool initialized; |
| 50 | static pthread_mutex_t mutex; |
| 51 | }; |
| 52 | __res_state GlobalStateAccessor::state; |
| 53 | bool GlobalStateAccessor::initialized = false; |
| 54 | pthread_mutex_t GlobalStateAccessor::mutex = PTHREAD_MUTEX_INITIALIZER; |
| 55 | |
| 56 | int GlobalStateAccessor::init() { |
| 57 | // These three fields used to be statically initialized. This made |
| 58 | // it hard to use this code in a shared library. It is necessary, |
| 59 | // now that we're doing dynamic initialization here, that we preserve |
| 60 | // the old semantics: if an application modifies one of these three |
| 61 | // fields of _res before res_init() is called, res_init() will not |
| 62 | // alter them. Of course, if an application is setting them to |
| 63 | // _zero_ before calling res_init(), hoping to override what used |
| 64 | // to be the static default, we can't detect it and unexpected results |
| 65 | // will follow. Zero for any of these fields would make no sense, |
| 66 | // so one can safely assume that the applications were already getting |
| 67 | // unexpected results. |
| 68 | // g_nres.options is tricky since some apps were known to diddle the bits |
| 69 | // before res_init() was first called. We can't replicate that semantic |
| 70 | // with dynamic initialization (they may have turned bits off that are |
| 71 | // set in RES_DEFAULT). Our solution is to declare such applications |
| 72 | // "broken". They could fool us by setting RES_INIT but none do (yet). |
| 73 | if (!state.retrans) state.retrans = RES_TIMEOUT; |
| 74 | if (!state.retry) state.retry = 4; |
| 75 | if (!(state.options & RES_INIT)) state.options = RES_DEFAULT; |
| 76 | |
| 77 | // This one used to initialize implicitly to zero, so unless the app |
| 78 | // has set it to something in particular, we can randomize it now. |
| 79 | if (!state.id) state.id = res_randomid(); |
| 80 | |
| 81 | return __res_vinit(&state, 1); |
| 82 | } |
| 83 | |
| 84 | int res_init(void) { |
| 85 | GlobalStateAccessor gsa; |
| 86 | return gsa.init(); |
| 87 | } |
| 88 | |
| 89 | void p_query(const u_char* msg) { |
| 90 | fp_query(msg, stdout); |
| 91 | } |
| 92 | |
| 93 | void fp_query(const u_char* msg, FILE* file) { |
| 94 | fp_nquery(msg, PACKETSZ, file); |
| 95 | } |
| 96 | |
| 97 | void fp_nquery(const u_char* msg, int len, FILE* file) { |
| 98 | GlobalStateAccessor gsa; |
| 99 | res_pquery(gsa.get(), msg, len, file); |
| 100 | } |
| 101 | |
| 102 | int |
| 103 | res_mkquery(int op, const char* dname, int klass, int type, const u_char* data, |
| 104 | int datalen, const u_char* newrr_in, u_char* buf, int buflen) { |
| 105 | GlobalStateAccessor gsa; |
| 106 | return res_nmkquery(gsa.get(), op, dname, klass, type, data, datalen, newrr_in, buf, buflen); |
| 107 | } |
| 108 | |
| 109 | int res_query(const char* name, int klass, int type, u_char* answer, int anslen) { |
| 110 | GlobalStateAccessor gsa; |
| 111 | return res_nquery(gsa.get(), name, klass, type, answer, anslen); |
| 112 | } |
| 113 | |
| 114 | void res_send_setqhook(res_send_qhook hook) { |
| 115 | GlobalStateAccessor gsa; |
| 116 | gsa.get()->qhook = hook; |
| 117 | } |
| 118 | |
| 119 | void res_send_setrhook(res_send_rhook hook) { |
| 120 | GlobalStateAccessor gsa; |
| 121 | gsa.get()->rhook = hook; |
| 122 | } |
| 123 | |
| 124 | int res_isourserver(const struct sockaddr_in* inp) { |
| 125 | GlobalStateAccessor gsa; |
| 126 | return res_ourserver_p(gsa.get(), reinterpret_cast<const sockaddr*>(inp)); |
| 127 | } |
| 128 | |
| 129 | int res_send(const u_char* buf, int buflen, u_char* ans, int anssiz) { |
| 130 | GlobalStateAccessor gsa; |
| 131 | return res_nsend(gsa.get(), buf, buflen, ans, anssiz); |
| 132 | } |
| 133 | |
| 134 | void res_close(void) { |
| 135 | GlobalStateAccessor gsa; |
| 136 | res_nclose(gsa.get()); |
| 137 | } |
| 138 | |
| 139 | int res_search(const char* name, int klass, int type, u_char* answer, int anslen) { |
| 140 | GlobalStateAccessor gsa; |
| 141 | return res_nsearch(gsa.get(), name, klass, type, answer, anslen); |
| 142 | } |
| 143 | |
| 144 | int res_querydomain(const char* name, const char* domain, int klass, int type, u_char* answer, |
| 145 | int anslen) { |
| 146 | GlobalStateAccessor gsa; |
| 147 | return res_nquerydomain(gsa.get(), name, domain, klass, type, answer, anslen); |
| 148 | } |
| 149 | |
| 150 | int res_opt(int a, u_char* b, int c, int d) { |
| 151 | GlobalStateAccessor gsa; |
| 152 | return res_nopt(gsa.get(), a, b, c, d); |
| 153 | } |
| 154 | |
| 155 | const char* hostalias(const char* name) { |
| 156 | return NULL; |
| 157 | } |