PowerPC: Change sched_getcpu to use vDSO getcpu instead of syscall.
[glibc.git] / nss / digits_dots.c
blob5b31ebc7e1f09c24c2231034674b6d42106907e6
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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include <wctype.h>
25 #include <resolv.h>
26 #include <netdb.h>
27 #include <arpa/inet.h>
28 #include "nsswitch.h"
30 #ifdef USE_NSCD
31 # define inet_aton __inet_aton
32 # include <nscd/nscd_proto.h>
33 #endif
35 int
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)
41 int save;
43 /* We have to test for the use of IPv6 which can only be done by
44 examining `_res'. */
45 if (__res_maybe_init (&_res, 0) == -1)
47 if (h_errnop)
48 *h_errnop = NETDB_INTERNAL;
49 *result = NULL;
50 return -1;
54 * disallow names consisting only of digits/dots, unless
55 * they end in a dot.
57 if (isdigit (name[0]) || isxdigit (name[0]) || name[0] == ':')
59 const char *cp;
60 char *hostname;
61 typedef unsigned char host_addr_t[16];
62 host_addr_t *host_addr;
63 typedef char *host_addr_list_t[2];
64 host_addr_list_t *h_addr_ptrs;
65 char **h_alias_ptr;
66 size_t size_needed;
67 int addr_size;
69 switch (af)
71 case AF_INET:
72 addr_size = INADDRSZ;
73 break;
75 case AF_INET6:
76 addr_size = IN6ADDRSZ;
77 break;
79 default:
80 af = (_res.options & RES_USE_INET6) ? AF_INET6 : AF_INET;
81 addr_size = af == AF_INET6 ? IN6ADDRSZ : INADDRSZ;
82 break;
85 size_needed = (sizeof (*host_addr)
86 + sizeof (*h_addr_ptrs) + strlen (name) + 1);
88 if (buffer_size == NULL)
90 if (buflen < size_needed)
92 if (h_errnop != NULL)
93 *h_errnop = TRY_AGAIN;
94 __set_errno (ERANGE);
95 goto done;
98 else if (buffer_size != NULL && *buffer_size < size_needed)
100 char *new_buf;
101 *buffer_size = size_needed;
102 new_buf = (char *) realloc (*buffer, *buffer_size);
104 if (new_buf == NULL)
106 save = errno;
107 free (*buffer);
108 *buffer = NULL;
109 *buffer_size = 0;
110 __set_errno (save);
111 if (h_errnop != NULL)
112 *h_errnop = TRY_AGAIN;
113 *result = NULL;
114 goto done;
116 *buffer = new_buf;
119 memset (*buffer, '\0', size_needed);
121 host_addr = (host_addr_t *) *buffer;
122 h_addr_ptrs = (host_addr_list_t *)
123 ((char *) host_addr + sizeof (*host_addr));
124 h_alias_ptr = (char **) ((char *) h_addr_ptrs + sizeof (*h_addr_ptrs));
125 hostname = (char *) h_alias_ptr + sizeof (*h_alias_ptr);
127 if (isdigit (name[0]))
129 for (cp = name;; ++cp)
131 if (*cp == '\0')
133 int ok;
135 if (*--cp == '.')
136 break;
138 /* All-numeric, no dot at the end. Fake up a hostent as if
139 we'd actually done a lookup. What if someone types
140 255.255.255.255? The test below will succeed
141 spuriously... ??? */
142 if (af == AF_INET)
143 ok = __inet_aton (name, (struct in_addr *) host_addr);
144 else
146 assert (af == AF_INET6);
147 ok = inet_pton (af, name, host_addr) > 0;
149 if (! ok)
151 *h_errnop = HOST_NOT_FOUND;
152 if (buffer_size)
153 *result = NULL;
154 goto done;
157 resbuf->h_name = strcpy (hostname, name);
158 h_alias_ptr[0] = NULL;
159 resbuf->h_aliases = h_alias_ptr;
160 (*h_addr_ptrs)[0] = (char *) host_addr;
161 (*h_addr_ptrs)[1] = NULL;
162 resbuf->h_addr_list = *h_addr_ptrs;
163 if (af == AF_INET && (_res.options & RES_USE_INET6))
165 /* We need to change the IP v4 address into the
166 IP v6 address. */
167 char tmp[INADDRSZ];
168 char *p = (char *) host_addr;
169 int i;
171 /* Save a copy of the IP v4 address. */
172 memcpy (tmp, host_addr, INADDRSZ);
173 /* Mark this ipv6 addr as a mapped ipv4. */
174 for (i = 0; i < 10; i++)
175 *p++ = 0x00;
176 *p++ = 0xff;
177 *p++ = 0xff;
178 /* Copy the IP v4 address. */
179 memcpy (p, tmp, INADDRSZ);
180 resbuf->h_addrtype = AF_INET6;
181 resbuf->h_length = IN6ADDRSZ;
183 else
185 resbuf->h_addrtype = af;
186 resbuf->h_length = addr_size;
188 if (h_errnop != NULL)
189 *h_errnop = NETDB_SUCCESS;
190 if (buffer_size == NULL)
191 *status = NSS_STATUS_SUCCESS;
192 else
193 *result = resbuf;
194 goto done;
197 if (!isdigit (*cp) && *cp != '.')
198 break;
202 if ((isxdigit (name[0]) && strchr (name, ':') != NULL) || name[0] == ':')
204 const char *cp;
205 char *hostname;
206 typedef unsigned char host_addr_t[16];
207 host_addr_t *host_addr;
208 typedef char *host_addr_list_t[2];
209 host_addr_list_t *h_addr_ptrs;
210 size_t size_needed;
211 int addr_size;
213 switch (af)
215 default:
216 af = (_res.options & RES_USE_INET6) ? AF_INET6 : AF_INET;
217 if (af == AF_INET6)
219 addr_size = IN6ADDRSZ;
220 break;
222 /* FALLTHROUGH */
224 case AF_INET:
225 /* This is not possible. We cannot represent an IPv6 address
226 in an `struct in_addr' variable. */
227 *h_errnop = HOST_NOT_FOUND;
228 *result = NULL;
229 goto done;
231 case AF_INET6:
232 addr_size = IN6ADDRSZ;
233 break;
236 size_needed = (sizeof (*host_addr)
237 + sizeof (*h_addr_ptrs) + strlen (name) + 1);
239 if (buffer_size == NULL && buflen < size_needed)
241 if (h_errnop != NULL)
242 *h_errnop = TRY_AGAIN;
243 __set_errno (ERANGE);
244 goto done;
246 else if (buffer_size != NULL && *buffer_size < size_needed)
248 char *new_buf;
249 *buffer_size = size_needed;
250 new_buf = realloc (*buffer, *buffer_size);
252 if (new_buf == NULL)
254 save = errno;
255 free (*buffer);
256 __set_errno (save);
257 *buffer = NULL;
258 *buffer_size = 0;
259 *result = NULL;
260 goto done;
262 *buffer = new_buf;
265 memset (*buffer, '\0', size_needed);
267 host_addr = (host_addr_t *) *buffer;
268 h_addr_ptrs = (host_addr_list_t *)
269 ((char *) host_addr + sizeof (*host_addr));
270 hostname = (char *) h_addr_ptrs + sizeof (*h_addr_ptrs);
272 for (cp = name;; ++cp)
274 if (!*cp)
276 if (*--cp == '.')
277 break;
279 /* All-IPv6-legal, no dot at the end. Fake up a
280 hostent as if we'd actually done a lookup. */
281 if (inet_pton (AF_INET6, name, host_addr) <= 0)
283 *h_errnop = HOST_NOT_FOUND;
284 if (buffer_size)
285 *result = NULL;
286 goto done;
289 resbuf->h_name = strcpy (hostname, name);
290 h_alias_ptr[0] = NULL;
291 resbuf->h_aliases = h_alias_ptr;
292 (*h_addr_ptrs)[0] = (char *) host_addr;
293 (*h_addr_ptrs)[1] = (char *) 0;
294 resbuf->h_addr_list = *h_addr_ptrs;
295 resbuf->h_addrtype = AF_INET6;
296 resbuf->h_length = addr_size;
297 *h_errnop = NETDB_SUCCESS;
298 if (buffer_size == NULL)
299 *status = NSS_STATUS_SUCCESS;
300 else
301 *result = resbuf;
302 goto done;
305 if (!isxdigit (*cp) && *cp != ':' && *cp != '.')
306 break;
311 return 0;
313 done:
314 return 1;
316 libc_hidden_def (__nss_hostname_digits_dots)