| 1 | /* Definitions related to res_init linked into libc instead of libresolv. |
| 2 | Copyright (C) 1995-2023 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | /* |
| 20 | * Copyright (c) 1995-1999 by Internet Software Consortium. |
| 21 | * |
| 22 | * Permission to use, copy, modify, and distribute this software for any |
| 23 | * purpose with or without fee is hereby granted, provided that the above |
| 24 | * copyright notice and this permission notice appear in all copies. |
| 25 | * |
| 26 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS |
| 27 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES |
| 28 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE |
| 29 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 30 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 31 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 32 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 33 | * SOFTWARE. |
| 34 | */ |
| 35 | |
| 36 | #include <atomic.h> |
| 37 | #include <limits.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <netinet/in.h> |
| 40 | #include <arpa/nameser.h> |
| 41 | #include <resolv.h> |
| 42 | #include <libc-lock.h> |
| 43 | #include <resolv-internal.h> |
| 44 | |
| 45 | int |
| 46 | res_init (void) |
| 47 | { |
| 48 | /* These three fields used to be statically initialized. This made |
| 49 | it hard to use this code in a shared library. It is necessary, |
| 50 | now that we're doing dynamic initialization here, that we |
| 51 | preserve the old semantics: if an application modifies one of |
| 52 | these three fields of _res before res_init is called, |
| 53 | res_init will not alter them. Of course, if an application is |
| 54 | setting them to _zero_ before calling res_init, hoping to |
| 55 | override what used to be the static default, we can't detect it |
| 56 | and unexpected results will follow. Zero for any of these fields |
| 57 | would make no sense, so one can safely assume that the |
| 58 | applications were already getting unexpected results. |
| 59 | |
| 60 | _res.options is tricky since some apps were known to diddle the |
| 61 | bits before res_init was first called. We can't replicate that |
| 62 | semantic with dynamic initialization (they may have turned bits |
| 63 | off that are set in RES_DEFAULT). Our solution is to declare |
| 64 | such applications "broken". They could fool us by setting |
| 65 | RES_INIT but none do (yet). */ |
| 66 | if (!_res.retrans) |
| 67 | _res.retrans = RES_TIMEOUT; |
| 68 | if (!_res.retry) |
| 69 | _res.retry = RES_DFLRETRY; |
| 70 | if (!(_res.options & RES_INIT)) |
| 71 | _res.options = RES_DEFAULT; |
| 72 | else if (_res.nscount > 0) |
| 73 | __res_iclose (&_res, true); /* Close any VC sockets. */ |
| 74 | |
| 75 | /* This one used to initialize implicitly to zero, so unless the app |
| 76 | has set it to something in particular, we can randomize it * |
| 77 | now. */ |
| 78 | if (!_res.id) |
| 79 | _res.id = res_randomid (); |
| 80 | |
| 81 | return __res_vinit (&_res, 1); |
| 82 | } |
| 83 | |
| 84 | /* This needs to be after the use of _res in res_init, above. */ |
| 85 | #undef _res |
| 86 | |
| 87 | /* The resolver state for use by single-threaded programs. |
| 88 | This differs from plain `struct __res_state _res;' in that it doesn't |
| 89 | create a common definition, but a plain symbol that resides in .bss, |
| 90 | which can have an alias. */ |
| 91 | struct __res_state _res; |
| 92 | |
| 93 | #undef __resp |
| 94 | __thread struct __res_state *__resp = &_res; |
| 95 | extern __thread struct __res_state *__libc_resp |
| 96 | __attribute__ ((alias ("__resp" ))) attribute_hidden; |
| 97 | |
| 98 | #include <shlib-compat.h> |
| 99 | |
| 100 | /* We declare this with compat_symbol so that it's not |
| 101 | visible at link time. Programs must use the accessor functions. */ |
| 102 | #ifdef SHARED |
| 103 | compat_symbol (libc, _res, _res, GLIBC_2_0); |
| 104 | #endif |
| 105 | |
| 106 | #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2) |
| 107 | # undef res_init |
| 108 | extern int __res_init_weak (void); |
| 109 | weak_extern (__res_init_weak); |
| 110 | strong_alias (__res_init, __res_init_weak); |
| 111 | compat_symbol (libc, __res_init_weak, res_init, GLIBC_2_0); |
| 112 | #endif |
| 113 | |