MIPSr6/math: Use builtin fma and fmaf
[glibc.git] / nss / digits_dots.c
blob15f8c383b89fd6fc9b5973e347f60aee872fffcd
1 /* Copyright (C) 1997-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
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.
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
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <assert.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <wctype.h>
24 #include <resolv/resolv-internal.h>
25 #include <resolv/resolv_context.h>
26 #include <netdb.h>
27 #include <arpa/inet.h>
28 #include "nsswitch.h"
30 #ifdef USE_NSCD
31 # include <nscd/nscd_proto.h>
32 #endif
34 int
35 __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
36 char **buffer, size_t *buffer_size,
37 size_t buflen, struct hostent **result,
38 enum nss_status *status, int af, int *h_errnop)
40 /* We have to test for the use of IPv6 which can only be done by
41 examining `_res'. */
42 struct resolv_context *ctx = __resolv_context_get ();
43 if (ctx == NULL)
45 if (h_errnop)
46 *h_errnop = NETDB_INTERNAL;
47 if (buffer_size == NULL)
48 *status = NSS_STATUS_TRYAGAIN;
49 else
50 *result = NULL;
51 return -1;
53 int ret = __nss_hostname_digits_dots_context
54 (ctx, name, resbuf, buffer, buffer_size, buflen,
55 result, status, af, h_errnop);
56 __resolv_context_put (ctx);
57 return ret;
60 int
61 __nss_hostname_digits_dots_context (struct resolv_context *ctx,
62 const char *name, struct hostent *resbuf,
63 char **buffer, size_t *buffer_size,
64 size_t buflen, struct hostent **result,
65 enum nss_status *status, int af, int *h_errnop)
67 int save;
70 * disallow names consisting only of digits/dots, unless
71 * they end in a dot.
73 if (isdigit (name[0]) || isxdigit (name[0]) || name[0] == ':')
75 const char *cp;
76 char *hostname;
77 typedef unsigned char host_addr_t[16];
78 host_addr_t *host_addr;
79 typedef char *host_addr_list_t[2];
80 host_addr_list_t *h_addr_ptrs;
81 char **h_alias_ptr;
82 size_t size_needed;
83 int addr_size;
85 switch (af)
87 case AF_INET:
88 addr_size = INADDRSZ;
89 break;
91 case AF_INET6:
92 addr_size = IN6ADDRSZ;
93 break;
95 default:
96 af = res_use_inet6 () ? AF_INET6 : AF_INET;
97 addr_size = af == AF_INET6 ? IN6ADDRSZ : INADDRSZ;
98 break;
101 size_needed = (sizeof (*host_addr)
102 + sizeof (*h_addr_ptrs)
103 + sizeof (*h_alias_ptr) + strlen (name) + 1);
105 if (buffer_size == NULL)
107 if (buflen < size_needed)
109 *status = NSS_STATUS_TRYAGAIN;
110 if (h_errnop != NULL)
111 *h_errnop = NETDB_INTERNAL;
112 __set_errno (ERANGE);
113 goto done;
116 else if (buffer_size != NULL && *buffer_size < size_needed)
118 char *new_buf;
119 *buffer_size = size_needed;
120 new_buf = (char *) realloc (*buffer, *buffer_size);
122 if (new_buf == NULL)
124 save = errno;
125 free (*buffer);
126 *buffer = NULL;
127 *buffer_size = 0;
128 __set_errno (save);
129 if (h_errnop != NULL)
130 *h_errnop = NETDB_INTERNAL;
131 *result = NULL;
132 goto done;
134 *buffer = new_buf;
137 memset (*buffer, '\0', size_needed);
139 host_addr = (host_addr_t *) *buffer;
140 h_addr_ptrs = (host_addr_list_t *)
141 ((char *) host_addr + sizeof (*host_addr));
142 h_alias_ptr = (char **) ((char *) h_addr_ptrs + sizeof (*h_addr_ptrs));
143 hostname = (char *) h_alias_ptr + sizeof (*h_alias_ptr);
145 if (isdigit (name[0]))
147 for (cp = name;; ++cp)
149 if (*cp == '\0')
151 int ok;
153 if (*--cp == '.')
154 break;
156 /* All-numeric, no dot at the end. Fake up a hostent as if
157 we'd actually done a lookup. What if someone types
158 255.255.255.255? The test below will succeed
159 spuriously... ??? */
160 if (af == AF_INET)
161 ok = __inet_aton_exact (name, (struct in_addr *) host_addr);
162 else
164 assert (af == AF_INET6);
165 ok = inet_pton (af, name, host_addr) > 0;
167 if (! ok)
169 *h_errnop = HOST_NOT_FOUND;
170 if (buffer_size == NULL)
171 *status = NSS_STATUS_NOTFOUND;
172 else
173 *result = NULL;
174 goto done;
177 resbuf->h_name = strcpy (hostname, name);
178 h_alias_ptr[0] = NULL;
179 resbuf->h_aliases = h_alias_ptr;
180 (*h_addr_ptrs)[0] = (char *) host_addr;
181 (*h_addr_ptrs)[1] = NULL;
182 resbuf->h_addr_list = *h_addr_ptrs;
183 if (af == AF_INET && res_use_inet6 ())
185 /* We need to change the IP v4 address into the
186 IP v6 address. */
187 char tmp[INADDRSZ];
188 char *p = (char *) host_addr;
189 int i;
191 /* Save a copy of the IP v4 address. */
192 memcpy (tmp, host_addr, INADDRSZ);
193 /* Mark this ipv6 addr as a mapped ipv4. */
194 for (i = 0; i < 10; i++)
195 *p++ = 0x00;
196 *p++ = 0xff;
197 *p++ = 0xff;
198 /* Copy the IP v4 address. */
199 memcpy (p, tmp, INADDRSZ);
200 resbuf->h_addrtype = AF_INET6;
201 resbuf->h_length = IN6ADDRSZ;
203 else
205 resbuf->h_addrtype = af;
206 resbuf->h_length = addr_size;
208 if (h_errnop != NULL)
209 *h_errnop = NETDB_SUCCESS;
210 if (buffer_size == NULL)
211 *status = NSS_STATUS_SUCCESS;
212 else
213 *result = resbuf;
214 goto done;
217 if (!isdigit (*cp) && *cp != '.')
218 break;
222 if ((isxdigit (name[0]) && strchr (name, ':') != NULL) || name[0] == ':')
224 switch (af)
226 default:
227 af = res_use_inet6 () ? AF_INET6 : AF_INET;
228 if (af == AF_INET6)
230 addr_size = IN6ADDRSZ;
231 break;
233 /* FALLTHROUGH */
235 case AF_INET:
236 /* This is not possible. We cannot represent an IPv6 address
237 in an `struct in_addr' variable. */
238 *h_errnop = HOST_NOT_FOUND;
239 if (buffer_size == NULL)
240 *status = NSS_STATUS_NOTFOUND;
241 else
242 *result = NULL;
243 goto done;
245 case AF_INET6:
246 addr_size = IN6ADDRSZ;
247 break;
250 for (cp = name;; ++cp)
252 if (!*cp)
254 if (*--cp == '.')
255 break;
257 /* All-IPv6-legal, no dot at the end. Fake up a
258 hostent as if we'd actually done a lookup. */
259 if (inet_pton (AF_INET6, name, host_addr) <= 0)
261 *h_errnop = HOST_NOT_FOUND;
262 if (buffer_size == NULL)
263 *status = NSS_STATUS_NOTFOUND;
264 else
265 *result = NULL;
266 goto done;
269 resbuf->h_name = strcpy (hostname, name);
270 h_alias_ptr[0] = NULL;
271 resbuf->h_aliases = h_alias_ptr;
272 (*h_addr_ptrs)[0] = (char *) host_addr;
273 (*h_addr_ptrs)[1] = (char *) 0;
274 resbuf->h_addr_list = *h_addr_ptrs;
275 resbuf->h_addrtype = AF_INET6;
276 resbuf->h_length = addr_size;
277 *h_errnop = NETDB_SUCCESS;
278 if (buffer_size == NULL)
279 *status = NSS_STATUS_SUCCESS;
280 else
281 *result = resbuf;
282 goto done;
285 if (!isxdigit (*cp) && *cp != ':' && *cp != '.')
286 break;
291 return 0;
293 done:
294 return 1;
296 libc_hidden_def (__nss_hostname_digits_dots)