1 /* Copyright (C) 1997, 1999, 2000, 2001, 2004 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
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
)
44 /* We have to test for the use of IPv6 which can only be done by
46 if (__res_maybe_init (&_res
, 0) == -1)
49 *h_errnop
= NETDB_INTERNAL
;
55 * disallow names consisting only of digits/dots, unless
58 if (isdigit (name
[0]) || isxdigit (name
[0]) || name
[0] == ':')
62 typedef unsigned char host_addr_t
[16];
63 host_addr_t
*host_addr
;
64 typedef char *host_addr_list_t
[2];
65 host_addr_list_t
*h_addr_ptrs
;
77 addr_size
= IN6ADDRSZ
;
81 af
= (_res
.options
& RES_USE_INET6
) ? AF_INET6
: AF_INET
;
82 addr_size
= af
== AF_INET6
? IN6ADDRSZ
: INADDRSZ
;
86 size_needed
= (sizeof (*host_addr
)
87 + sizeof (*h_addr_ptrs
) + strlen (name
) + 1);
89 if (buffer_size
== NULL
)
91 if (buflen
< size_needed
)
94 *h_errnop
= TRY_AGAIN
;
99 else if (buffer_size
!= NULL
&& *buffer_size
< size_needed
)
102 *buffer_size
= size_needed
;
103 new_buf
= (char *) realloc (*buffer
, *buffer_size
);
112 if (h_errnop
!= NULL
)
113 *h_errnop
= TRY_AGAIN
;
120 memset (*buffer
, '\0', size_needed
);
122 host_addr
= (host_addr_t
*) *buffer
;
123 h_addr_ptrs
= (host_addr_list_t
*)
124 ((char *) host_addr
+ sizeof (*host_addr
));
125 h_alias_ptr
= (char **) ((char *) h_addr_ptrs
+ sizeof (*h_addr_ptrs
));
126 hostname
= (char *) h_alias_ptr
+ sizeof (*h_alias_ptr
);
128 if (isdigit (name
[0]))
130 for (cp
= name
;; ++cp
)
139 /* All-numeric, no dot at the end. Fake up a hostent as if
140 we'd actually done a lookup. What if someone types
141 255.255.255.255? The test below will succeed
144 ok
= __inet_aton (name
, (struct in_addr
*) host_addr
);
147 assert (af
== AF_INET6
);
148 ok
= inet_pton (af
, name
, host_addr
) > 0;
152 *h_errnop
= HOST_NOT_FOUND
;
158 resbuf
->h_name
= strcpy (hostname
, name
);
159 h_alias_ptr
[0] = NULL
;
160 resbuf
->h_aliases
= h_alias_ptr
;
161 (*h_addr_ptrs
)[0] = (char *) host_addr
;
162 (*h_addr_ptrs
)[1] = NULL
;
163 resbuf
->h_addr_list
= *h_addr_ptrs
;
164 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
))
166 /* We need to change the IP v4 address into the
169 char *p
= (char *) host_addr
;
172 /* Save a copy of the IP v4 address. */
173 memcpy (tmp
, host_addr
, INADDRSZ
);
174 /* Mark this ipv6 addr as a mapped ipv4. */
175 for (i
= 0; i
< 10; i
++)
179 /* Copy the IP v4 address. */
180 memcpy (p
, tmp
, INADDRSZ
);
181 resbuf
->h_addrtype
= AF_INET6
;
182 resbuf
->h_length
= IN6ADDRSZ
;
186 resbuf
->h_addrtype
= af
;
187 resbuf
->h_length
= addr_size
;
189 if (h_errnop
!= NULL
)
190 *h_errnop
= NETDB_SUCCESS
;
191 if (buffer_size
== NULL
)
192 *status
= NSS_STATUS_SUCCESS
;
198 if (!isdigit (*cp
) && *cp
!= '.')
203 if ((isxdigit (name
[0]) && strchr (name
, ':') != NULL
) || name
[0] == ':')
207 typedef unsigned char host_addr_t
[16];
208 host_addr_t
*host_addr
;
209 typedef char *host_addr_list_t
[2];
210 host_addr_list_t
*h_addr_ptrs
;
217 af
= (_res
.options
& RES_USE_INET6
) ? AF_INET6
: AF_INET
;
220 addr_size
= IN6ADDRSZ
;
226 /* This is not possible. We cannot represent an IPv6 address
227 in an `struct in_addr' variable. */
228 *h_errnop
= HOST_NOT_FOUND
;
233 addr_size
= IN6ADDRSZ
;
237 size_needed
= (sizeof (*host_addr
)
238 + sizeof (*h_addr_ptrs
) + strlen (name
) + 1);
240 if (buffer_size
== NULL
&& buflen
< size_needed
)
242 if (h_errnop
!= NULL
)
243 *h_errnop
= TRY_AGAIN
;
244 __set_errno (ERANGE
);
247 else if (buffer_size
!= NULL
&& *buffer_size
< size_needed
)
250 *buffer_size
= size_needed
;
251 new_buf
= realloc (*buffer
, *buffer_size
);
266 memset (*buffer
, '\0', size_needed
);
268 host_addr
= (host_addr_t
*) *buffer
;
269 h_addr_ptrs
= (host_addr_list_t
*)
270 ((char *) host_addr
+ sizeof (*host_addr
));
271 hostname
= (char *) h_addr_ptrs
+ sizeof (*h_addr_ptrs
);
273 for (cp
= name
;; ++cp
)
280 /* All-IPv6-legal, no dot at the end. Fake up a
281 hostent as if we'd actually done a lookup. */
282 if (inet_pton (AF_INET6
, name
, host_addr
) <= 0)
284 *h_errnop
= HOST_NOT_FOUND
;
290 resbuf
->h_name
= strcpy (hostname
, name
);
291 h_alias_ptr
[0] = NULL
;
292 resbuf
->h_aliases
= h_alias_ptr
;
293 (*h_addr_ptrs
)[0] = (char *) host_addr
;
294 (*h_addr_ptrs
)[1] = (char *) 0;
295 resbuf
->h_addr_list
= *h_addr_ptrs
;
296 resbuf
->h_addrtype
= AF_INET6
;
297 resbuf
->h_length
= addr_size
;
298 *h_errnop
= NETDB_SUCCESS
;
299 if (buffer_size
== NULL
)
300 *status
= NSS_STATUS_SUCCESS
;
306 if (!isxdigit (*cp
) && *cp
!= ':' && *cp
!= '.')
317 libc_hidden_def (__nss_hostname_digits_dots
)