1 /* Copyright (C) 1997-2019 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 <http://www.gnu.org/licenses/>. */
25 #include <resolv/resolv-internal.h>
26 #include <resolv/resolv_context.h>
28 #include <arpa/inet.h>
32 # define inet_aton __inet_aton
33 # include <nscd/nscd_proto.h>
37 __nss_hostname_digits_dots (const char *name
, struct hostent
*resbuf
,
38 char **buffer
, size_t *buffer_size
,
39 size_t buflen
, struct hostent
**result
,
40 enum nss_status
*status
, int af
, int *h_errnop
)
42 /* We have to test for the use of IPv6 which can only be done by
44 struct resolv_context
*ctx
= __resolv_context_get ();
48 *h_errnop
= NETDB_INTERNAL
;
49 if (buffer_size
== NULL
)
50 *status
= NSS_STATUS_TRYAGAIN
;
55 int ret
= __nss_hostname_digits_dots_context
56 (ctx
, name
, resbuf
, buffer
, buffer_size
, buflen
,
57 result
, status
, af
, h_errnop
);
58 __resolv_context_put (ctx
);
63 __nss_hostname_digits_dots_context (struct resolv_context
*ctx
,
64 const char *name
, struct hostent
*resbuf
,
65 char **buffer
, size_t *buffer_size
,
66 size_t buflen
, struct hostent
**result
,
67 enum nss_status
*status
, int af
, int *h_errnop
)
72 * disallow names consisting only of digits/dots, unless
75 if (isdigit (name
[0]) || isxdigit (name
[0]) || name
[0] == ':')
79 typedef unsigned char host_addr_t
[16];
80 host_addr_t
*host_addr
;
81 typedef char *host_addr_list_t
[2];
82 host_addr_list_t
*h_addr_ptrs
;
94 addr_size
= IN6ADDRSZ
;
98 af
= res_use_inet6 () ? AF_INET6
: AF_INET
;
99 addr_size
= af
== AF_INET6
? IN6ADDRSZ
: INADDRSZ
;
103 size_needed
= (sizeof (*host_addr
)
104 + sizeof (*h_addr_ptrs
)
105 + sizeof (*h_alias_ptr
) + strlen (name
) + 1);
107 if (buffer_size
== NULL
)
109 if (buflen
< size_needed
)
111 *status
= NSS_STATUS_TRYAGAIN
;
112 if (h_errnop
!= NULL
)
113 *h_errnop
= NETDB_INTERNAL
;
114 __set_errno (ERANGE
);
118 else if (buffer_size
!= NULL
&& *buffer_size
< size_needed
)
121 *buffer_size
= size_needed
;
122 new_buf
= (char *) realloc (*buffer
, *buffer_size
);
131 if (h_errnop
!= NULL
)
132 *h_errnop
= NETDB_INTERNAL
;
139 memset (*buffer
, '\0', size_needed
);
141 host_addr
= (host_addr_t
*) *buffer
;
142 h_addr_ptrs
= (host_addr_list_t
*)
143 ((char *) host_addr
+ sizeof (*host_addr
));
144 h_alias_ptr
= (char **) ((char *) h_addr_ptrs
+ sizeof (*h_addr_ptrs
));
145 hostname
= (char *) h_alias_ptr
+ sizeof (*h_alias_ptr
);
147 if (isdigit (name
[0]))
149 for (cp
= name
;; ++cp
)
158 /* All-numeric, no dot at the end. Fake up a hostent as if
159 we'd actually done a lookup. What if someone types
160 255.255.255.255? The test below will succeed
163 ok
= __inet_aton (name
, (struct in_addr
*) host_addr
);
166 assert (af
== AF_INET6
);
167 ok
= inet_pton (af
, name
, host_addr
) > 0;
171 *h_errnop
= HOST_NOT_FOUND
;
172 if (buffer_size
== NULL
)
173 *status
= NSS_STATUS_NOTFOUND
;
179 resbuf
->h_name
= strcpy (hostname
, name
);
180 h_alias_ptr
[0] = NULL
;
181 resbuf
->h_aliases
= h_alias_ptr
;
182 (*h_addr_ptrs
)[0] = (char *) host_addr
;
183 (*h_addr_ptrs
)[1] = NULL
;
184 resbuf
->h_addr_list
= *h_addr_ptrs
;
185 if (af
== AF_INET
&& res_use_inet6 ())
187 /* We need to change the IP v4 address into the
190 char *p
= (char *) host_addr
;
193 /* Save a copy of the IP v4 address. */
194 memcpy (tmp
, host_addr
, INADDRSZ
);
195 /* Mark this ipv6 addr as a mapped ipv4. */
196 for (i
= 0; i
< 10; i
++)
200 /* Copy the IP v4 address. */
201 memcpy (p
, tmp
, INADDRSZ
);
202 resbuf
->h_addrtype
= AF_INET6
;
203 resbuf
->h_length
= IN6ADDRSZ
;
207 resbuf
->h_addrtype
= af
;
208 resbuf
->h_length
= addr_size
;
210 if (h_errnop
!= NULL
)
211 *h_errnop
= NETDB_SUCCESS
;
212 if (buffer_size
== NULL
)
213 *status
= NSS_STATUS_SUCCESS
;
219 if (!isdigit (*cp
) && *cp
!= '.')
224 if ((isxdigit (name
[0]) && strchr (name
, ':') != NULL
) || name
[0] == ':')
229 af
= res_use_inet6 () ? AF_INET6
: AF_INET
;
232 addr_size
= IN6ADDRSZ
;
238 /* This is not possible. We cannot represent an IPv6 address
239 in an `struct in_addr' variable. */
240 *h_errnop
= HOST_NOT_FOUND
;
241 if (buffer_size
== NULL
)
242 *status
= NSS_STATUS_NOTFOUND
;
248 addr_size
= IN6ADDRSZ
;
252 for (cp
= name
;; ++cp
)
259 /* All-IPv6-legal, no dot at the end. Fake up a
260 hostent as if we'd actually done a lookup. */
261 if (inet_pton (AF_INET6
, name
, host_addr
) <= 0)
263 *h_errnop
= HOST_NOT_FOUND
;
264 if (buffer_size
== NULL
)
265 *status
= NSS_STATUS_NOTFOUND
;
271 resbuf
->h_name
= strcpy (hostname
, name
);
272 h_alias_ptr
[0] = NULL
;
273 resbuf
->h_aliases
= h_alias_ptr
;
274 (*h_addr_ptrs
)[0] = (char *) host_addr
;
275 (*h_addr_ptrs
)[1] = (char *) 0;
276 resbuf
->h_addr_list
= *h_addr_ptrs
;
277 resbuf
->h_addrtype
= AF_INET6
;
278 resbuf
->h_length
= addr_size
;
279 *h_errnop
= NETDB_SUCCESS
;
280 if (buffer_size
== NULL
)
281 *status
= NSS_STATUS_SUCCESS
;
287 if (!isxdigit (*cp
) && *cp
!= ':' && *cp
!= '.')
298 libc_hidden_def (__nss_hostname_digits_dots
)