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;
50 if (buffer_size == NULL)
51 *status = NSS_STATUS_TRYAGAIN;
58 * disallow names consisting only of digits/dots, unless
61 if (isdigit (name[0]) || isxdigit (name[0]) || name[0] == ':')
65 typedef unsigned char host_addr_t[16];
66 host_addr_t *host_addr;
67 typedef char *host_addr_list_t[2];
68 host_addr_list_t *h_addr_ptrs;
80 addr_size = IN6ADDRSZ;
84 af = (_res.options & RES_USE_INET6) ? AF_INET6 : AF_INET;
85 addr_size = af == AF_INET6 ? IN6ADDRSZ : INADDRSZ;
89 size_needed = (sizeof (*host_addr)
90 + sizeof (*h_addr_ptrs)
91 + sizeof (*h_alias_ptr) + strlen (name) + 1);
93 if (buffer_size == NULL)
95 if (buflen < size_needed)
97 *status = NSS_STATUS_TRYAGAIN;
99 *h_errnop = NETDB_INTERNAL;
100 __set_errno (ERANGE);
104 else if (buffer_size != NULL && *buffer_size < size_needed)
107 *buffer_size = size_needed;
108 new_buf = (char *) realloc (*buffer, *buffer_size);
117 if (h_errnop != NULL)
118 *h_errnop = NETDB_INTERNAL;
125 memset (*buffer, '\0', size_needed);
127 host_addr = (host_addr_t *) *buffer;
128 h_addr_ptrs = (host_addr_list_t *)
129 ((char *) host_addr + sizeof (*host_addr));
130 h_alias_ptr = (char **) ((char *) h_addr_ptrs + sizeof (*h_addr_ptrs));
131 hostname = (char *) h_alias_ptr + sizeof (*h_alias_ptr);
133 if (isdigit (name[0]))
135 for (cp = name;; ++cp)
144 /* All-numeric, no dot at the end. Fake up a hostent as if
145 we'd actually done a lookup. What if someone types
146 255.255.255.255? The test below will succeed
149 ok = __inet_aton (name, (struct in_addr *) host_addr);
152 assert (af == AF_INET6);
153 ok = inet_pton (af, name, host_addr) > 0;
157 *h_errnop = HOST_NOT_FOUND;
158 if (buffer_size == NULL)
159 *status = NSS_STATUS_NOTFOUND;
165 resbuf->h_name = strcpy (hostname, name);
166 h_alias_ptr[0] = NULL;
167 resbuf->h_aliases = h_alias_ptr;
168 (*h_addr_ptrs)[0] = (char *) host_addr;
169 (*h_addr_ptrs)[1] = NULL;
170 resbuf->h_addr_list = *h_addr_ptrs;
171 if (af == AF_INET && (_res.options & RES_USE_INET6))
173 /* We need to change the IP v4 address into the
176 char *p = (char *) host_addr;
179 /* Save a copy of the IP v4 address. */
180 memcpy (tmp, host_addr, INADDRSZ);
181 /* Mark this ipv6 addr as a mapped ipv4. */
182 for (i = 0; i < 10; i++)
186 /* Copy the IP v4 address. */
187 memcpy (p, tmp, INADDRSZ);
188 resbuf->h_addrtype = AF_INET6;
189 resbuf->h_length = IN6ADDRSZ;
193 resbuf->h_addrtype = af;
194 resbuf->h_length = addr_size;
196 if (h_errnop != NULL)
197 *h_errnop = NETDB_SUCCESS;
198 if (buffer_size == NULL)
199 *status = NSS_STATUS_SUCCESS;
205 if (!isdigit (*cp) && *cp != '.')
210 if ((isxdigit (name[0]) && strchr (name, ':') != NULL) || name[0] == ':')
215 af = (_res.options & RES_USE_INET6) ? AF_INET6 : AF_INET;
218 addr_size = IN6ADDRSZ;
224 /* This is not possible. We cannot represent an IPv6 address
225 in an `struct in_addr' variable. */
226 *h_errnop = HOST_NOT_FOUND;
227 if (buffer_size == NULL)
228 *status = NSS_STATUS_NOTFOUND;
234 addr_size = IN6ADDRSZ;
238 for (cp = name;; ++cp)
245 /* All-IPv6-legal, no dot at the end. Fake up a
246 hostent as if we'd actually done a lookup. */
247 if (inet_pton (AF_INET6, name, host_addr) <= 0)
249 *h_errnop = HOST_NOT_FOUND;
250 if (buffer_size == NULL)
251 *status = NSS_STATUS_NOTFOUND;
257 resbuf->h_name = strcpy (hostname, name);
258 h_alias_ptr[0] = NULL;
259 resbuf->h_aliases = h_alias_ptr;
260 (*h_addr_ptrs)[0] = (char *) host_addr;
261 (*h_addr_ptrs)[1] = (char *) 0;
262 resbuf->h_addr_list = *h_addr_ptrs;
263 resbuf->h_addrtype = AF_INET6;
264 resbuf->h_length = addr_size;
265 *h_errnop = NETDB_SUCCESS;
266 if (buffer_size == NULL)
267 *status = NSS_STATUS_SUCCESS;
273 if (!isxdigit (*cp) && *cp != ':' && *cp != '.')
284 libc_hidden_def (__nss_hostname_digits_dots)