1 /* Copyright (C) 1997-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by H.J. Lu <hjl@gnu.ai.mit.edu>, 1997.
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.
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.
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/>. */
25 #include <resolv/resolv-internal.h>
26 #include <resolv/resolv_context.h>
28 #include <arpa/inet.h>
32 # include <nscd/nscd_proto.h>
36 __nss_hostname_digits_dots (const char *name
, struct hostent
*resbuf
,
37 char **buffer
, size_t *buffer_size
,
38 size_t buflen
, struct hostent
**result
,
39 enum nss_status
*status
, int af
, int *h_errnop
)
41 /* We have to test for the use of IPv6 which can only be done by
43 struct resolv_context
*ctx
= __resolv_context_get ();
47 *h_errnop
= NETDB_INTERNAL
;
48 if (buffer_size
== NULL
)
49 *status
= NSS_STATUS_TRYAGAIN
;
54 int ret
= __nss_hostname_digits_dots_context
55 (ctx
, name
, resbuf
, buffer
, buffer_size
, buflen
,
56 result
, status
, af
, h_errnop
);
57 __resolv_context_put (ctx
);
62 __nss_hostname_digits_dots_context (struct resolv_context
*ctx
,
63 const char *name
, struct hostent
*resbuf
,
64 char **buffer
, size_t *buffer_size
,
65 size_t buflen
, struct hostent
**result
,
66 enum nss_status
*status
, int af
, int *h_errnop
)
71 * disallow names consisting only of digits/dots, unless
74 if (isdigit (name
[0]) || isxdigit (name
[0]) || name
[0] == ':')
78 typedef unsigned char host_addr_t
[16];
79 host_addr_t
*host_addr
;
80 typedef char *host_addr_list_t
[2];
81 host_addr_list_t
*h_addr_ptrs
;
93 addr_size
= IN6ADDRSZ
;
97 af
= res_use_inet6 () ? AF_INET6
: AF_INET
;
98 addr_size
= af
== AF_INET6
? IN6ADDRSZ
: INADDRSZ
;
102 size_needed
= (sizeof (*host_addr
)
103 + sizeof (*h_addr_ptrs
)
104 + sizeof (*h_alias_ptr
) + strlen (name
) + 1);
106 if (buffer_size
== NULL
)
108 if (buflen
< size_needed
)
110 *status
= NSS_STATUS_TRYAGAIN
;
111 if (h_errnop
!= NULL
)
112 *h_errnop
= NETDB_INTERNAL
;
113 __set_errno (ERANGE
);
117 else if (buffer_size
!= NULL
&& *buffer_size
< size_needed
)
120 *buffer_size
= size_needed
;
121 new_buf
= (char *) realloc (*buffer
, *buffer_size
);
130 if (h_errnop
!= NULL
)
131 *h_errnop
= NETDB_INTERNAL
;
138 memset (*buffer
, '\0', size_needed
);
140 host_addr
= (host_addr_t
*) *buffer
;
141 h_addr_ptrs
= (host_addr_list_t
*)
142 ((char *) host_addr
+ sizeof (*host_addr
));
143 h_alias_ptr
= (char **) ((char *) h_addr_ptrs
+ sizeof (*h_addr_ptrs
));
144 hostname
= (char *) h_alias_ptr
+ sizeof (*h_alias_ptr
);
146 if (isdigit (name
[0]))
148 for (cp
= name
;; ++cp
)
157 /* All-numeric, no dot at the end. Fake up a hostent as if
158 we'd actually done a lookup. What if someone types
159 255.255.255.255? The test below will succeed
162 ok
= __inet_aton_exact (name
, (struct in_addr
*) host_addr
);
165 assert (af
== AF_INET6
);
166 ok
= inet_pton (af
, name
, host_addr
) > 0;
170 *h_errnop
= HOST_NOT_FOUND
;
171 if (buffer_size
== NULL
)
172 *status
= NSS_STATUS_NOTFOUND
;
178 resbuf
->h_name
= strcpy (hostname
, name
);
179 h_alias_ptr
[0] = NULL
;
180 resbuf
->h_aliases
= h_alias_ptr
;
181 (*h_addr_ptrs
)[0] = (char *) host_addr
;
182 (*h_addr_ptrs
)[1] = NULL
;
183 resbuf
->h_addr_list
= *h_addr_ptrs
;
184 if (af
== AF_INET
&& res_use_inet6 ())
186 /* We need to change the IP v4 address into the
189 char *p
= (char *) host_addr
;
192 /* Save a copy of the IP v4 address. */
193 memcpy (tmp
, host_addr
, INADDRSZ
);
194 /* Mark this ipv6 addr as a mapped ipv4. */
195 for (i
= 0; i
< 10; i
++)
199 /* Copy the IP v4 address. */
200 memcpy (p
, tmp
, INADDRSZ
);
201 resbuf
->h_addrtype
= AF_INET6
;
202 resbuf
->h_length
= IN6ADDRSZ
;
206 resbuf
->h_addrtype
= af
;
207 resbuf
->h_length
= addr_size
;
209 if (h_errnop
!= NULL
)
210 *h_errnop
= NETDB_SUCCESS
;
211 if (buffer_size
== NULL
)
212 *status
= NSS_STATUS_SUCCESS
;
218 if (!isdigit (*cp
) && *cp
!= '.')
223 if ((isxdigit (name
[0]) && strchr (name
, ':') != NULL
) || name
[0] == ':')
228 af
= res_use_inet6 () ? AF_INET6
: AF_INET
;
231 addr_size
= IN6ADDRSZ
;
237 /* This is not possible. We cannot represent an IPv6 address
238 in an `struct in_addr' variable. */
239 *h_errnop
= HOST_NOT_FOUND
;
240 if (buffer_size
== NULL
)
241 *status
= NSS_STATUS_NOTFOUND
;
247 addr_size
= IN6ADDRSZ
;
251 for (cp
= name
;; ++cp
)
258 /* All-IPv6-legal, no dot at the end. Fake up a
259 hostent as if we'd actually done a lookup. */
260 if (inet_pton (AF_INET6
, name
, host_addr
) <= 0)
262 *h_errnop
= HOST_NOT_FOUND
;
263 if (buffer_size
== NULL
)
264 *status
= NSS_STATUS_NOTFOUND
;
270 resbuf
->h_name
= strcpy (hostname
, name
);
271 h_alias_ptr
[0] = NULL
;
272 resbuf
->h_aliases
= h_alias_ptr
;
273 (*h_addr_ptrs
)[0] = (char *) host_addr
;
274 (*h_addr_ptrs
)[1] = (char *) 0;
275 resbuf
->h_addr_list
= *h_addr_ptrs
;
276 resbuf
->h_addrtype
= AF_INET6
;
277 resbuf
->h_length
= addr_size
;
278 *h_errnop
= NETDB_SUCCESS
;
279 if (buffer_size
== NULL
)
280 *status
= NSS_STATUS_SUCCESS
;
286 if (!isxdigit (*cp
) && *cp
!= ':' && *cp
!= '.')
297 libc_hidden_def (__nss_hostname_digits_dots
)