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/>. */
24 #include <resolv/resolv-internal.h>
25 #include <resolv/resolv_context.h>
27 #include <arpa/inet.h>
31 # include <nscd/nscd_proto.h>
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
42 struct resolv_context
*ctx
= __resolv_context_get ();
46 *h_errnop
= NETDB_INTERNAL
;
47 if (buffer_size
== NULL
)
48 *status
= NSS_STATUS_TRYAGAIN
;
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
);
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
)
70 * disallow names consisting only of digits/dots, unless
73 if (isdigit (name
[0]) || isxdigit (name
[0]) || name
[0] == ':')
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
;
92 addr_size
= IN6ADDRSZ
;
96 af
= res_use_inet6 () ? AF_INET6
: AF_INET
;
97 addr_size
= af
== AF_INET6
? IN6ADDRSZ
: INADDRSZ
;
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
);
116 else if (buffer_size
!= NULL
&& *buffer_size
< size_needed
)
119 *buffer_size
= size_needed
;
120 new_buf
= (char *) realloc (*buffer
, *buffer_size
);
129 if (h_errnop
!= NULL
)
130 *h_errnop
= NETDB_INTERNAL
;
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
)
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
161 ok
= __inet_aton_exact (name
, (struct in_addr
*) host_addr
);
164 assert (af
== AF_INET6
);
165 ok
= inet_pton (af
, name
, host_addr
) > 0;
169 *h_errnop
= HOST_NOT_FOUND
;
170 if (buffer_size
== NULL
)
171 *status
= NSS_STATUS_NOTFOUND
;
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
188 char *p
= (char *) host_addr
;
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
++)
198 /* Copy the IP v4 address. */
199 memcpy (p
, tmp
, INADDRSZ
);
200 resbuf
->h_addrtype
= AF_INET6
;
201 resbuf
->h_length
= IN6ADDRSZ
;
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
;
217 if (!isdigit (*cp
) && *cp
!= '.')
222 if ((isxdigit (name
[0]) && strchr (name
, ':') != NULL
) || name
[0] == ':')
227 af
= res_use_inet6 () ? AF_INET6
: AF_INET
;
230 addr_size
= IN6ADDRSZ
;
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
;
246 addr_size
= IN6ADDRSZ
;
250 for (cp
= name
;; ++cp
)
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
;
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
;
285 if (!isxdigit (*cp
) && *cp
!= ':' && *cp
!= '.')
296 libc_hidden_def (__nss_hostname_digits_dots
)