]>
Commit | Line | Data |
---|---|---|
04277e02 | 1 | /* Copyright (C) 1996-2019 Free Software Foundation, Inc. |
26761c28 | 2 | This file is part of the GNU C Library. |
5f0e6fc7 | 3 | |
26761c28 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
5f0e6fc7 | 8 | |
26761c28 UD |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
5f0e6fc7 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
5f0e6fc7 | 17 | |
344c67b6 | 18 | #include <assert.h> |
26761c28 | 19 | #include <errno.h> |
ec999b8e | 20 | #include <libc-lock.h> |
26761c28 | 21 | #include <stdlib.h> |
b43b13ac | 22 | #include <resolv.h> |
26761c28 UD |
23 | |
24 | #include "nsswitch.h" | |
5f0e6fc7 RM |
25 | |
26 | /*******************************************************************\ | |
27 | |* Here we assume several symbols to be defined: *| | |
6b9fecdc | 28 | |* *| |
5f0e6fc7 | 29 | |* LOOKUP_TYPE - the return type of the function *| |
6b9fecdc | 30 | |* *| |
5f0e6fc7 | 31 | |* FUNCTION_NAME - name of the non-reentrant function *| |
6b9fecdc | 32 | |* *| |
5f0e6fc7 RM |
33 | |* DATABASE_NAME - name of the database the function accesses *| |
34 | |* (e.g., host, services, ...) *| | |
6b9fecdc | 35 | |* *| |
5f0e6fc7 | 36 | |* ADD_PARAMS - additional parameter, can vary in number *| |
6b9fecdc | 37 | |* *| |
5f0e6fc7 | 38 | |* ADD_VARIABLES - names of additional parameter *| |
6b9fecdc | 39 | |* *| |
5f0e6fc7 RM |
40 | |* BUFLEN - length of buffer allocated for the non *| |
41 | |* reentrant version *| | |
6b9fecdc | 42 | |* *| |
5f0e6fc7 | 43 | |* Optionally the following vars can be defined: *| |
6b9fecdc | 44 | |* *| |
5f0e6fc7 RM |
45 | |* NEED_H_ERRNO - an extra parameter will be passed to point to *| |
46 | |* the global `h_errno' variable. *| | |
6b9fecdc | 47 | |* *| |
5f0e6fc7 RM |
48 | \*******************************************************************/ |
49 | ||
352f4ff9 FW |
50 | |
51 | #ifdef HANDLE_DIGITS_DOTS | |
52 | # include <resolv/resolv_context.h> | |
53 | #endif | |
54 | ||
5f0e6fc7 RM |
55 | /* To make the real sources a bit prettier. */ |
56 | #define REENTRANT_NAME APPEND_R (FUNCTION_NAME) | |
57 | #define APPEND_R(name) APPEND_R1 (name) | |
58 | #define APPEND_R1(name) name##_r | |
23396375 UD |
59 | #define INTERNAL(name) INTERNAL1 (name) |
60 | #define INTERNAL1(name) __##name | |
5f0e6fc7 RM |
61 | |
62 | /* Sometimes we need to store error codes in the `h_errno' variable. */ | |
63 | #ifdef NEED_H_ERRNO | |
64 | # define H_ERRNO_PARM , int *h_errnop | |
54d79e99 | 65 | # define H_ERRNO_VAR , &h_errno_tmp |
a5fdf99b | 66 | # define H_ERRNO_VAR_P &h_errno_tmp |
5f0e6fc7 RM |
67 | #else |
68 | # define H_ERRNO_PARM | |
69 | # define H_ERRNO_VAR | |
a5fdf99b | 70 | # define H_ERRNO_VAR_P NULL |
5f0e6fc7 RM |
71 | #endif |
72 | ||
a5fdf99b | 73 | #ifdef HAVE_AF |
1773d1ba | 74 | # define AF_VAL af |
a5fdf99b | 75 | #else |
1773d1ba | 76 | # define AF_VAL AF_INET |
a5fdf99b | 77 | #endif |
5f0e6fc7 RM |
78 | |
79 | /* Prototype for reentrant version we use here. */ | |
ba1ffaa1 UD |
80 | extern int INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, |
81 | char *buffer, size_t buflen, | |
9635ce69 L |
82 | LOOKUP_TYPE **result H_ERRNO_PARM) |
83 | attribute_hidden; | |
5f0e6fc7 | 84 | |
26761c28 UD |
85 | /* We need to protect the dynamic buffer handling. */ |
86 | __libc_lock_define_initialized (static, lock); | |
87 | ||
d60d215c | 88 | /* This points to the static buffer used. */ |
c877418f | 89 | libc_freeres_ptr (static char *buffer); |
d60d215c | 90 | |
26761c28 | 91 | |
5f0e6fc7 RM |
92 | LOOKUP_TYPE * |
93 | FUNCTION_NAME (ADD_PARAMS) | |
94 | { | |
26761c28 | 95 | static size_t buffer_size; |
ba1ffaa1 UD |
96 | static LOOKUP_TYPE resbuf; |
97 | LOOKUP_TYPE *result; | |
54d79e99 UD |
98 | #ifdef NEED_H_ERRNO |
99 | int h_errno_tmp = 0; | |
100 | #endif | |
26761c28 | 101 | |
352f4ff9 FW |
102 | #ifdef HANDLE_DIGITS_DOTS |
103 | /* Wrap both __nss_hostname_digits_dots and the actual lookup | |
104 | function call in the same context. */ | |
105 | struct resolv_context *res_ctx = __resolv_context_get (); | |
106 | if (res_ctx == NULL) | |
107 | { | |
108 | # if NEED_H_ERRNO | |
109 | __set_h_errno (NETDB_INTERNAL); | |
110 | # endif | |
111 | return NULL; | |
112 | } | |
113 | #endif | |
114 | ||
26761c28 UD |
115 | /* Get lock. */ |
116 | __libc_lock_lock (lock); | |
117 | ||
118 | if (buffer == NULL) | |
119 | { | |
120 | buffer_size = BUFLEN; | |
a452e4e6 | 121 | buffer = (char *) malloc (buffer_size); |
26761c28 UD |
122 | } |
123 | ||
a5fdf99b | 124 | #ifdef HANDLE_DIGITS_DOTS |
61c162b5 UD |
125 | if (buffer != NULL) |
126 | { | |
352f4ff9 FW |
127 | if (__nss_hostname_digits_dots_context |
128 | (res_ctx, name, &resbuf, &buffer, &buffer_size, 0, &result, NULL, | |
129 | AF_VAL, H_ERRNO_VAR_P)) | |
a5fdf99b | 130 | goto done; |
61c162b5 | 131 | } |
a5fdf99b | 132 | #endif |
61c162b5 | 133 | |
26761c28 | 134 | while (buffer != NULL |
1670698f UD |
135 | && (INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, &resbuf, buffer, |
136 | buffer_size, &result H_ERRNO_VAR) | |
137 | == ERANGE) | |
e4cf5070 UD |
138 | #ifdef NEED_H_ERRNO |
139 | && h_errno_tmp == NETDB_INTERNAL | |
140 | #endif | |
1670698f | 141 | ) |
26761c28 UD |
142 | { |
143 | char *new_buf; | |
e1b27ffb | 144 | buffer_size *= 2; |
a452e4e6 | 145 | new_buf = (char *) realloc (buffer, buffer_size); |
26761c28 | 146 | if (new_buf == NULL) |
e4cf5070 UD |
147 | { |
148 | /* We are out of memory. Free the current buffer so that the | |
149 | process gets a chance for a normal termination. */ | |
e4cf5070 | 150 | free (buffer); |
a452e4e6 | 151 | __set_errno (ENOMEM); |
e4cf5070 | 152 | } |
26761c28 UD |
153 | buffer = new_buf; |
154 | } | |
5f0e6fc7 | 155 | |
22d57dd3 UD |
156 | if (buffer == NULL) |
157 | result = NULL; | |
158 | ||
61c162b5 UD |
159 | #ifdef HANDLE_DIGITS_DOTS |
160 | done: | |
161 | #endif | |
a452e4e6 | 162 | /* Release lock. */ |
26761c28 | 163 | __libc_lock_unlock (lock); |
ba1ffaa1 | 164 | |
352f4ff9 FW |
165 | #ifdef HANDLE_DIGITS_DOTS |
166 | __resolv_context_put (res_ctx); | |
167 | #endif | |
168 | ||
54d79e99 UD |
169 | #ifdef NEED_H_ERRNO |
170 | if (h_errno_tmp != 0) | |
171 | __set_h_errno (h_errno_tmp); | |
172 | #endif | |
173 | ||
ba1ffaa1 | 174 | return result; |
5f0e6fc7 | 175 | } |
2f7f7bc6 | 176 | |
01767843 | 177 | nss_interface_function (FUNCTION_NAME) |