1 /* Copyright (C) 1997, 1999, 2000, 2001 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 *typep
,
41 int flags
, int *afp
, int *h_errnop
)
45 /* We have to test for the use of IPv6 which can only be done by
47 if ((_res
.options
& RES_INIT
) == 0 && __res_ninit (&_res
) == -1)
50 *h_errnop
= NETDB_INTERNAL
;
56 * disallow names consisting only of digits/dots, unless
59 if (isdigit (name
[0]) || isxdigit (name
[0]) || name
[0] == ':')
63 typedef unsigned char host_addr_t
[16];
64 host_addr_t
*host_addr
;
65 typedef char *host_addr_list_t
[2];
66 host_addr_list_t
*h_addr_ptrs
;
86 addr_size
= IN6ADDRSZ
;
92 /* This must not happen. */
94 *h_errnop
= HOST_NOT_FOUND
;
99 af
= (_res
.options
& RES_USE_INET6
) ? AF_INET6
: AF_INET
;
100 addr_size
= af
== AF_INET6
? IN6ADDRSZ
: INADDRSZ
;
105 size_needed
= (sizeof (*host_addr
)
106 + sizeof (*h_addr_ptrs
) + strlen (name
) + 1);
108 if (buffer_size
== NULL
)
110 if (buflen
< size_needed
)
112 if (h_errnop
!= NULL
)
113 *h_errnop
= TRY_AGAIN
;
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
= TRY_AGAIN
;
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
;
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 ((typep
!= NULL
&& *typep
== AF_INET6
)
185 && (_res
.options
& RES_USE_INET6
)))
187 if (typep
!= NULL
&& (flags
& AI_V4MAPPED
) == 0)
189 /* That's bad. The user hasn't specified that she
190 allows IPv4 numeric addresses. */
192 *h_errnop
= HOST_NOT_FOUND
;
197 /* We need to change the IP v4 address into the
200 char *p
= (char *) host_addr
;
203 /* Save a copy of the IP v4 address. */
204 memcpy (tmp
, host_addr
, INADDRSZ
);
205 /* Mark this ipv6 addr as a mapped ipv4. */
206 for (i
= 0; i
< 10; i
++)
210 /* Copy the IP v4 address. */
211 memcpy (p
, tmp
, INADDRSZ
);
212 resbuf
->h_addrtype
= AF_INET6
;
213 resbuf
->h_length
= IN6ADDRSZ
;
218 resbuf
->h_addrtype
= af
;
219 resbuf
->h_length
= addr_size
;
221 if (h_errnop
!= NULL
)
222 *h_errnop
= NETDB_SUCCESS
;
223 if (buffer_size
== NULL
)
224 *status
= NSS_STATUS_SUCCESS
;
230 if (!isdigit (*cp
) && *cp
!= '.')
235 if ((isxdigit (name
[0]) && strchr (name
, ':') != NULL
) || name
[0] == ':')
239 typedef unsigned char host_addr_t
[16];
240 host_addr_t
*host_addr
;
241 typedef char *host_addr_list_t
[2];
242 host_addr_list_t
*h_addr_ptrs
;
249 else if (afp
!= NULL
)
257 af
= (_res
.options
& RES_USE_INET6
) ? AF_INET6
: AF_INET
;
260 addr_size
= IN6ADDRSZ
;
266 /* This is not possible. We cannot represent an IPv6 address
267 in an `struct in_addr' variable. */
268 *h_errnop
= HOST_NOT_FOUND
;
273 addr_size
= IN6ADDRSZ
;
277 size_needed
= (sizeof (*host_addr
)
278 + sizeof (*h_addr_ptrs
) + strlen (name
) + 1);
280 if (buffer_size
== NULL
&& buflen
< size_needed
)
282 if (h_errnop
!= NULL
)
283 *h_errnop
= TRY_AGAIN
;
284 __set_errno (ERANGE
);
287 else if (buffer_size
!= NULL
&& *buffer_size
< size_needed
)
290 *buffer_size
= size_needed
;
291 new_buf
= realloc (*buffer
, *buffer_size
);
306 memset (*buffer
, '\0', size_needed
);
308 host_addr
= (host_addr_t
*) *buffer
;
309 h_addr_ptrs
= (host_addr_list_t
*)
310 ((char *) host_addr
+ sizeof (*host_addr
));
311 hostname
= (char *) h_addr_ptrs
+ sizeof (*h_addr_ptrs
);
313 for (cp
= name
;; ++cp
)
320 /* All-IPv6-legal, no dot at the end. Fake up a
321 hostent as if we'd actually done a lookup. */
322 if (inet_pton (AF_INET6
, name
, host_addr
) <= 0)
324 *h_errnop
= HOST_NOT_FOUND
;
330 resbuf
->h_name
= strcpy (hostname
, name
);
331 h_alias_ptr
[0] = NULL
;
332 resbuf
->h_aliases
= h_alias_ptr
;
333 (*h_addr_ptrs
)[0] = (char *) host_addr
;
334 (*h_addr_ptrs
)[1] = (char *) 0;
335 resbuf
->h_addr_list
= *h_addr_ptrs
;
336 resbuf
->h_addrtype
= AF_INET6
;
337 resbuf
->h_length
= addr_size
;
338 *h_errnop
= NETDB_SUCCESS
;
339 if (buffer_size
== NULL
)
340 *status
= NSS_STATUS_SUCCESS
;
346 if (!isxdigit (*cp
) && *cp
!= ':' && *cp
!= '.')
357 libc_hidden_def (__nss_hostname_digits_dots
)