sparc: Check PIC instead of SHARED in start.S [BZ #22638]
[glibc.git] / nss / digits_dots.c
blob39bff38865a1ac5b196d1d7a85303b42816e7c71
1 /* Copyright (C) 1997-2018 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/resolv-internal.h>
26 #include <resolv/resolv_context.h>
27 #include <netdb.h>
28 #include <arpa/inet.h>
29 #include "nsswitch.h"
31 #ifdef USE_NSCD
32 # define inet_aton __inet_aton
33 # include <nscd/nscd_proto.h>
34 #endif
36 int
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)
42 /* We have to test for the use of IPv6 which can only be done by
43 examining `_res'. */
44 struct resolv_context *ctx = __resolv_context_get ();
45 if (ctx == NULL)
47 if (h_errnop)
48 *h_errnop = NETDB_INTERNAL;
49 if (buffer_size == NULL)
50 *status = NSS_STATUS_TRYAGAIN;
51 else
52 *result = NULL;
53 return -1;
55 int ret = __nss_hostname_digits_dots_context
56 (ctx, name, resbuf, buffer, buffer_size, buflen,
57 result, status, af, h_errnop);
58 __resolv_context_put (ctx);
59 return ret;
62 int
63 __nss_hostname_digits_dots_context (struct resolv_context *ctx,
64 const char *name, struct hostent *resbuf,
65 char **buffer, size_t *buffer_size,
66 size_t buflen, struct hostent **result,
67 enum nss_status *status, int af, int *h_errnop)
69 int save;
72 * disallow names consisting only of digits/dots, unless
73 * they end in a dot.
75 if (isdigit (name[0]) || isxdigit (name[0]) || name[0] == ':')
77 const char *cp;
78 char *hostname;
79 typedef unsigned char host_addr_t[16];
80 host_addr_t *host_addr;
81 typedef char *host_addr_list_t[2];
82 host_addr_list_t *h_addr_ptrs;
83 char **h_alias_ptr;
84 size_t size_needed;
85 int addr_size;
87 switch (af)
89 case AF_INET:
90 addr_size = INADDRSZ;
91 break;
93 case AF_INET6:
94 addr_size = IN6ADDRSZ;
95 break;
97 default:
98 af = res_use_inet6 () ? AF_INET6 : AF_INET;
99 addr_size = af == AF_INET6 ? IN6ADDRSZ : INADDRSZ;
100 break;
103 size_needed = (sizeof (*host_addr)
104 + sizeof (*h_addr_ptrs)
105 + sizeof (*h_alias_ptr) + strlen (name) + 1);
107 if (buffer_size == NULL)
109 if (buflen < size_needed)
111 *status = NSS_STATUS_TRYAGAIN;
112 if (h_errnop != NULL)
113 *h_errnop = NETDB_INTERNAL;
114 __set_errno (ERANGE);
115 goto done;
118 else if (buffer_size != NULL && *buffer_size < size_needed)
120 char *new_buf;
121 *buffer_size = size_needed;
122 new_buf = (char *) realloc (*buffer, *buffer_size);
124 if (new_buf == NULL)
126 save = errno;
127 free (*buffer);
128 *buffer = NULL;
129 *buffer_size = 0;
130 __set_errno (save);
131 if (h_errnop != NULL)
132 *h_errnop = NETDB_INTERNAL;
133 *result = NULL;
134 goto done;
136 *buffer = new_buf;
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)
151 if (*cp == '\0')
153 int ok;
155 if (*--cp == '.')
156 break;
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
161 spuriously... ??? */
162 if (af == AF_INET)
163 ok = __inet_aton (name, (struct in_addr *) host_addr);
164 else
166 assert (af == AF_INET6);
167 ok = inet_pton (af, name, host_addr) > 0;
169 if (! ok)
171 *h_errnop = HOST_NOT_FOUND;
172 if (buffer_size == NULL)
173 *status = NSS_STATUS_NOTFOUND;
174 else
175 *result = NULL;
176 goto done;
179 resbuf->h_name = strcpy (hostname, name);
180 h_alias_ptr[0] = NULL;
181 resbuf->h_aliases = h_alias_ptr;
182 (*h_addr_ptrs)[0] = (char *) host_addr;
183 (*h_addr_ptrs)[1] = NULL;
184 resbuf->h_addr_list = *h_addr_ptrs;
185 if (af == AF_INET && res_use_inet6 ())
187 /* We need to change the IP v4 address into the
188 IP v6 address. */
189 char tmp[INADDRSZ];
190 char *p = (char *) host_addr;
191 int i;
193 /* Save a copy of the IP v4 address. */
194 memcpy (tmp, host_addr, INADDRSZ);
195 /* Mark this ipv6 addr as a mapped ipv4. */
196 for (i = 0; i < 10; i++)
197 *p++ = 0x00;
198 *p++ = 0xff;
199 *p++ = 0xff;
200 /* Copy the IP v4 address. */
201 memcpy (p, tmp, INADDRSZ);
202 resbuf->h_addrtype = AF_INET6;
203 resbuf->h_length = IN6ADDRSZ;
205 else
207 resbuf->h_addrtype = af;
208 resbuf->h_length = addr_size;
210 if (h_errnop != NULL)
211 *h_errnop = NETDB_SUCCESS;
212 if (buffer_size == NULL)
213 *status = NSS_STATUS_SUCCESS;
214 else
215 *result = resbuf;
216 goto done;
219 if (!isdigit (*cp) && *cp != '.')
220 break;
224 if ((isxdigit (name[0]) && strchr (name, ':') != NULL) || name[0] == ':')
226 switch (af)
228 default:
229 af = res_use_inet6 () ? AF_INET6 : AF_INET;
230 if (af == AF_INET6)
232 addr_size = IN6ADDRSZ;
233 break;
235 /* FALLTHROUGH */
237 case AF_INET:
238 /* This is not possible. We cannot represent an IPv6 address
239 in an `struct in_addr' variable. */
240 *h_errnop = HOST_NOT_FOUND;
241 if (buffer_size == NULL)
242 *status = NSS_STATUS_NOTFOUND;
243 else
244 *result = NULL;
245 goto done;
247 case AF_INET6:
248 addr_size = IN6ADDRSZ;
249 break;
252 for (cp = name;; ++cp)
254 if (!*cp)
256 if (*--cp == '.')
257 break;
259 /* All-IPv6-legal, no dot at the end. Fake up a
260 hostent as if we'd actually done a lookup. */
261 if (inet_pton (AF_INET6, name, host_addr) <= 0)
263 *h_errnop = HOST_NOT_FOUND;
264 if (buffer_size == NULL)
265 *status = NSS_STATUS_NOTFOUND;
266 else
267 *result = NULL;
268 goto done;
271 resbuf->h_name = strcpy (hostname, name);
272 h_alias_ptr[0] = NULL;
273 resbuf->h_aliases = h_alias_ptr;
274 (*h_addr_ptrs)[0] = (char *) host_addr;
275 (*h_addr_ptrs)[1] = (char *) 0;
276 resbuf->h_addr_list = *h_addr_ptrs;
277 resbuf->h_addrtype = AF_INET6;
278 resbuf->h_length = addr_size;
279 *h_errnop = NETDB_SUCCESS;
280 if (buffer_size == NULL)
281 *status = NSS_STATUS_SUCCESS;
282 else
283 *result = resbuf;
284 goto done;
287 if (!isxdigit (*cp) && *cp != ':' && *cp != '.')
288 break;
293 return 0;
295 done:
296 return 1;
298 libc_hidden_def (__nss_hostname_digits_dots)