1 /* Copyright (C) 1997-2015 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/>. */
27 #include <arpa/inet.h>
31 # define inet_aton __inet_aton
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
)
43 /* We have to test for the use of IPv6 which can only be done by
45 if (__res_maybe_init (&_res
, 0) == -1)
48 *h_errnop
= NETDB_INTERNAL
;
49 if (buffer_size
== NULL
)
50 *status
= NSS_STATUS_TRYAGAIN
;
57 * disallow names consisting only of digits/dots, unless
60 if (isdigit (name
[0]) || isxdigit (name
[0]) || name
[0] == ':')
64 typedef unsigned char host_addr_t
[16];
65 host_addr_t
*host_addr
;
66 typedef char *host_addr_list_t
[2];
67 host_addr_list_t
*h_addr_ptrs
;
79 addr_size
= IN6ADDRSZ
;
83 af
= (_res
.options
& RES_USE_INET6
) ? AF_INET6
: AF_INET
;
84 addr_size
= af
== AF_INET6
? IN6ADDRSZ
: INADDRSZ
;
88 size_needed
= (sizeof (*host_addr
)
89 + sizeof (*h_addr_ptrs
)
90 + sizeof (*h_alias_ptr
) + strlen (name
) + 1);
92 if (buffer_size
== NULL
)
94 if (buflen
< size_needed
)
96 *status
= NSS_STATUS_TRYAGAIN
;
98 *h_errnop
= NETDB_INTERNAL
;
103 else if (buffer_size
!= NULL
&& *buffer_size
< size_needed
)
106 *buffer_size
= size_needed
;
107 new_buf
= (char *) realloc (*buffer
, *buffer_size
);
116 if (h_errnop
!= NULL
)
117 *h_errnop
= NETDB_INTERNAL
;
124 memset (*buffer
, '\0', size_needed
);
126 host_addr
= (host_addr_t
*) *buffer
;
127 h_addr_ptrs
= (host_addr_list_t
*)
128 ((char *) host_addr
+ sizeof (*host_addr
));
129 h_alias_ptr
= (char **) ((char *) h_addr_ptrs
+ sizeof (*h_addr_ptrs
));
130 hostname
= (char *) h_alias_ptr
+ sizeof (*h_alias_ptr
);
132 if (isdigit (name
[0]))
134 for (cp
= name
;; ++cp
)
143 /* All-numeric, no dot at the end. Fake up a hostent as if
144 we'd actually done a lookup. What if someone types
145 255.255.255.255? The test below will succeed
148 ok
= __inet_aton (name
, (struct in_addr
*) host_addr
);
151 assert (af
== AF_INET6
);
152 ok
= inet_pton (af
, name
, host_addr
) > 0;
156 *h_errnop
= HOST_NOT_FOUND
;
157 if (buffer_size
== NULL
)
158 *status
= NSS_STATUS_NOTFOUND
;
164 resbuf
->h_name
= strcpy (hostname
, name
);
165 h_alias_ptr
[0] = NULL
;
166 resbuf
->h_aliases
= h_alias_ptr
;
167 (*h_addr_ptrs
)[0] = (char *) host_addr
;
168 (*h_addr_ptrs
)[1] = NULL
;
169 resbuf
->h_addr_list
= *h_addr_ptrs
;
170 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
))
172 /* We need to change the IP v4 address into the
175 char *p
= (char *) host_addr
;
178 /* Save a copy of the IP v4 address. */
179 memcpy (tmp
, host_addr
, INADDRSZ
);
180 /* Mark this ipv6 addr as a mapped ipv4. */
181 for (i
= 0; i
< 10; i
++)
185 /* Copy the IP v4 address. */
186 memcpy (p
, tmp
, INADDRSZ
);
187 resbuf
->h_addrtype
= AF_INET6
;
188 resbuf
->h_length
= IN6ADDRSZ
;
192 resbuf
->h_addrtype
= af
;
193 resbuf
->h_length
= addr_size
;
195 if (h_errnop
!= NULL
)
196 *h_errnop
= NETDB_SUCCESS
;
197 if (buffer_size
== NULL
)
198 *status
= NSS_STATUS_SUCCESS
;
204 if (!isdigit (*cp
) && *cp
!= '.')
209 if ((isxdigit (name
[0]) && strchr (name
, ':') != NULL
) || name
[0] == ':')
214 af
= (_res
.options
& RES_USE_INET6
) ? AF_INET6
: AF_INET
;
217 addr_size
= IN6ADDRSZ
;
223 /* This is not possible. We cannot represent an IPv6 address
224 in an `struct in_addr' variable. */
225 *h_errnop
= HOST_NOT_FOUND
;
226 if (buffer_size
== NULL
)
227 *status
= NSS_STATUS_NOTFOUND
;
233 addr_size
= IN6ADDRSZ
;
237 for (cp
= name
;; ++cp
)
244 /* All-IPv6-legal, no dot at the end. Fake up a
245 hostent as if we'd actually done a lookup. */
246 if (inet_pton (AF_INET6
, name
, host_addr
) <= 0)
248 *h_errnop
= HOST_NOT_FOUND
;
249 if (buffer_size
== NULL
)
250 *status
= NSS_STATUS_NOTFOUND
;
256 resbuf
->h_name
= strcpy (hostname
, name
);
257 h_alias_ptr
[0] = NULL
;
258 resbuf
->h_aliases
= h_alias_ptr
;
259 (*h_addr_ptrs
)[0] = (char *) host_addr
;
260 (*h_addr_ptrs
)[1] = (char *) 0;
261 resbuf
->h_addr_list
= *h_addr_ptrs
;
262 resbuf
->h_addrtype
= AF_INET6
;
263 resbuf
->h_length
= addr_size
;
264 *h_errnop
= NETDB_SUCCESS
;
265 if (buffer_size
== NULL
)
266 *status
= NSS_STATUS_SUCCESS
;
272 if (!isxdigit (*cp
) && *cp
!= ':' && *cp
!= '.')
283 libc_hidden_def (__nss_hostname_digits_dots
)