Update.
[glibc.git] / nss / digits_dots.c
blobc3c6de7ce1dd5646179d992709fe05f0d07c5e3a
1 /* Copyright (C) 1997, 1999 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
21 * disallow names consisting only of digits/dots, unless
22 * they end in a dot.
24 if (isdigit (name[0]) || (isxdigit (name[0]) || name[0] == ':'))
26 const char *cp;
27 char *hostname;
28 typedef unsigned char host_addr_t [16];
29 host_addr_t *host_addr;
30 typedef char *host_addr_list_t [2];
31 host_addr_list_t *h_addr_ptrs;
32 size_t size_needed;
33 int addr_size;
34 #ifdef HAVE_TYPE
35 int af = type;
36 #else
37 # ifndef HAVE_AF
38 int af = -1;
39 # endif
40 #endif
42 switch (af)
44 case AF_INET:
45 addr_size = INADDRSZ;
46 break;
48 case AF_INET6:
49 addr_size = IN6ADDRSZ;
50 break;
52 default:
53 #ifdef HAVE_TYPE
54 /* This must not happen. */
55 *h_errnop = HOST_NOT_FOUND;
56 goto done;
57 #else
58 af = (_res.options & RES_USE_INET6) ? AF_INET6 : AF_INET;
59 addr_size = af == AF_INET6 ? IN6ADDRSZ : INADDRSZ;
60 break;
61 #endif
64 size_needed = (sizeof (*host_addr)
65 + sizeof (*h_addr_ptrs) + strlen (name) + 1);
67 #ifdef HAVE_LOOKUP_BUFFER
68 if (buflen < size_needed)
70 # ifdef NEED_H_ERRNO
71 *h_errnop = TRY_AGAIN;
72 # endif
73 __set_errno (ERANGE);
74 goto done;
76 #else
77 if (buffer_size < size_needed)
79 char *new_buf;
80 buffer_size = size_needed;
81 new_buf = realloc (buffer, buffer_size);
83 if (new_buf == NULL)
85 save = errno;
86 free (buffer);
87 buffer = NULL;
88 buffer_size = 0;
89 __set_errno (save);
90 # ifdef NEED_H_ERRNO
91 *h_errnop = TRY_AGAIN;
92 # endif
93 result = (struct hostent *) NULL;
94 goto done;
96 buffer = new_buf;
98 #endif /* HAVE_LOOKUP_BUFFER */
100 memset (buffer, 0, size_needed);
102 host_addr = (host_addr_t *) buffer;
103 h_addr_ptrs = (host_addr_list_t *)
104 ((char *) host_addr + sizeof (*host_addr));
105 hostname = (char *) h_addr_ptrs + sizeof (*h_addr_ptrs);
107 if (isdigit (name[0]))
109 for (cp = name;; ++cp)
111 if (!*cp)
113 int ok;
115 if (*--cp == '.')
116 break;
118 /* All-numeric, no dot at the end. Fake up a hostent as if
119 we'd actually done a lookup. What if someone types
120 255.255.255.255? The test below will succeed
121 spuriously... ??? */
122 if (af == AF_INET)
123 ok = inet_aton (name, (struct in_addr *) host_addr);
124 else
126 assert (af == AF_INET6);
127 ok = (inet_pton (af, name, host_addr) > 0);
129 if (! ok)
131 *h_errnop = HOST_NOT_FOUND;
132 #ifndef HAVE_LOOKUP_BUFFER
133 result = (struct hostent *) NULL;
134 #endif
135 goto done;
138 resbuf.h_name = strcpy (hostname, name);
139 resbuf.h_aliases = NULL;
140 (*h_addr_ptrs)[0] = (char *)host_addr;
141 (*h_addr_ptrs)[1] = (char *)0;
142 resbuf.h_addr_list = *h_addr_ptrs;
143 if (
144 #ifdef HAVE_TYPE
145 type == AF_INET6
146 #else
147 af == AF_INET && (_res.options & RES_USE_INET6)
148 #endif
151 #ifdef HAVE_TYPE
152 if ((flags & AI_V4MAPPED) == 0)
154 /* That's bad. The user hasn't specified that she
155 allows IPv4 numeric addresses. */
156 result = NULL;
157 *h_errnop = HOST_NOT_FOUND;
158 goto done;
160 else
161 #endif
163 /* We need to change the IP v4 address into the
164 IP v6 address. */
165 char tmp[INADDRSZ], *p = (char *) host_addr;
166 int i;
168 /* Save a copy of the IP v4 address. */
169 memcpy (tmp, host_addr, INADDRSZ);
170 /* Mark this ipv6 addr as a mapped ipv4. */
171 for (i = 0; i < 10; i++)
172 *p++ = 0x00;
173 *p++ = 0xff;
174 *p++ = 0xff;
175 /* Copy the IP v4 address. */
176 memcpy (p, tmp, INADDRSZ);
177 resbuf.h_addrtype = AF_INET6;
178 resbuf.h_length = IN6ADDRSZ;
181 else
183 resbuf.h_addrtype = af;
184 resbuf.h_length = addr_size;
186 *h_errnop = NETDB_SUCCESS;
187 #ifdef HAVE_LOOKUP_BUFFER
188 status = NSS_STATUS_SUCCESS;
189 #else
190 result = &resbuf;
191 #endif
192 goto done;
195 if (!isdigit (*cp) && *cp != '.') break;
199 if ((isxdigit (name[0]) && strchr (name, ':') != NULL)
200 || name[0] == ':')
202 const char *cp;
203 char *hostname;
204 typedef unsigned char host_addr_t [16];
205 host_addr_t *host_addr;
206 typedef char *host_addr_list_t [2];
207 host_addr_list_t *h_addr_ptrs;
208 size_t size_needed;
209 int addr_size;
210 #ifdef HAVE_TYPE
211 int af = type;
212 #else
213 # ifndef HAVE_AF
214 int af = -1;
215 # endif
216 #endif
218 switch (af)
220 default:
221 af = (_res.options & RES_USE_INET6) ? AF_INET6 : AF_INET;
222 if (af == AF_INET6)
224 addr_size = IN6ADDRSZ;
225 break;
227 /* FALLTHROUGH */
229 case AF_INET:
230 /* This is not possible. We cannot represent an IPv6 address
231 in an `struct in_addr' variable. */
232 *h_errnop = HOST_NOT_FOUND;
233 result = NULL;
234 goto done;
236 case AF_INET6:
237 addr_size = IN6ADDRSZ;
238 break;
241 size_needed = (sizeof (*host_addr)
242 + sizeof (*h_addr_ptrs) + strlen (name) + 1);
244 #ifdef HAVE_LOOKUP_BUFFER
245 if (buflen < size_needed)
247 # ifdef NEED_H_ERRNO
248 *h_errnop = TRY_AGAIN;
249 # endif
250 __set_errno (ERANGE);
251 goto done;
253 #else
254 if (buffer_size < size_needed)
256 char *new_buf;
257 buffer_size = size_needed;
258 new_buf = realloc (buffer, buffer_size);
260 if (new_buf == NULL)
262 save = errno;
263 free (buffer);
264 __set_errno (save);
265 buffer = NULL;
266 buffer_size = 0;
267 result = (struct hostent *) NULL;
268 goto done;
270 buffer = new_buf;
272 #endif /* HAVE_LOOKUP_BUFFER */
274 memset (buffer, 0, size_needed);
276 host_addr = (host_addr_t *) buffer;
277 h_addr_ptrs = (host_addr_list_t *)
278 ((char *) host_addr + sizeof (*host_addr));
279 hostname = (char *) h_addr_ptrs + sizeof (*h_addr_ptrs);
281 for (cp = name;; ++cp)
283 if (!*cp)
285 if (*--cp == '.')
286 break;
288 /* All-IPv6-legal, no dot at the end. Fake up a
289 hostent as if we'd actually done a lookup. */
290 if (inet_pton (AF_INET6, name, host_addr) <= 0)
292 *h_errnop = HOST_NOT_FOUND;
293 #ifndef HAVE_LOOKUP_BUFFER
294 result = (struct hostent *) NULL;
295 #endif
296 goto done;
299 resbuf.h_name = strcpy (hostname, name);
300 resbuf.h_aliases = NULL;
301 (*h_addr_ptrs)[0] = (char *) host_addr;
302 (*h_addr_ptrs)[1] = (char *) 0;
303 resbuf.h_addr_list = *h_addr_ptrs;
304 resbuf.h_addrtype = AF_INET6;
305 resbuf.h_length = addr_size;
306 *h_errnop = NETDB_SUCCESS;
307 #ifdef HAVE_LOOKUP_BUFFER
308 status = NSS_STATUS_SUCCESS;
309 #else
310 result = &resbuf;
311 #endif
312 goto done;
315 if (!isxdigit (*cp) && *cp != ':' && *cp != '.') break;