Update.
[glibc.git] / sysdeps / posix / getaddrinfo.c
blobbee95acabc31124ada1bc13e44431cee80fe2bb5
1 /* The Inner Net License, Version 2.00
3 The author(s) grant permission for redistribution and use in source and
4 binary forms, with or without modification, of the software and documentation
5 provided that the following conditions are met:
7 0. If you receive a version of the software that is specifically labelled
8 as not being for redistribution (check the version message and/or README),
9 you are not permitted to redistribute that version of the software in any
10 way or form.
11 1. All terms of the all other applicable copyrights and licenses must be
12 followed.
13 2. Redistributions of source code must retain the authors' copyright
14 notice(s), this list of conditions, and the following disclaimer.
15 3. Redistributions in binary form must reproduce the authors' copyright
16 notice(s), this list of conditions, and the following disclaimer in the
17 documentation and/or other materials provided with the distribution.
18 4. All advertising materials mentioning features or use of this software
19 must display the following acknowledgement with the name(s) of the
20 authors as specified in the copyright notice(s) substituted where
21 indicated:
23 This product includes software developed by <name(s)>, The Inner
24 Net, and other contributors.
26 5. Neither the name(s) of the author(s) nor the names of its contributors
27 may be used to endorse or promote products derived from this software
28 without specific prior written permission.
30 THIS SOFTWARE IS PROVIDED BY ITS AUTHORS AND CONTRIBUTORS ``AS IS'' AND ANY
31 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY
34 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
37 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 If these license terms cause you a real problem, contact the author. */
43 /* This software is Copyright 1996 by Craig Metz, All Rights Reserved. */
45 /* getaddrinfo() v1.13 */
47 #include <sys/types.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <sys/socket.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <sys/utsname.h>
54 #include <sys/un.h>
55 #include <netinet/in.h>
56 #include <netdb.h>
57 #include <errno.h>
58 #include <arpa/inet.h>
60 #define GAIH_OKIFUNSPEC 0x0100
61 #define GAIH_EAI ~(GAIH_OKIFUNSPEC)
63 #ifndef UNIX_PATH_MAX
64 #define UNIX_PATH_MAX 108
65 #endif
67 struct gaih_service
69 const char *name;
70 int num;
73 struct gaih_servtuple
75 struct gaih_servtuple *next;
76 int socktype;
77 int protocol;
78 int port;
81 static struct gaih_servtuple nullserv = { NULL, 0, 0, 0 };
83 struct gaih_addrtuple
85 struct gaih_addrtuple *next;
86 int family;
87 char addr[16];
90 struct gaih_typeproto
92 int socktype;
93 int protocol;
94 char *name;
97 static struct gaih_typeproto gaih_inet_typeproto[] =
99 { 0, 0, NULL },
100 { SOCK_STREAM, IPPROTO_TCP, (char *) "tcp" },
101 { SOCK_DGRAM, IPPROTO_UDP, (char *) "udp" },
102 { 0, 0, NULL }
105 struct gaih
107 int family;
108 int (*gaih)(const char *name, const struct gaih_service *service,
109 const struct addrinfo *req, struct addrinfo **pai);
112 static struct addrinfo default_hints =
113 { 0, PF_UNSPEC, 0, 0, 0, NULL, NULL, NULL };
116 static int
117 gaih_local (const char *name, const struct gaih_service *service,
118 const struct addrinfo *req, struct addrinfo **pai)
120 struct utsname utsname;
122 if ((name != NULL) || (req->ai_flags & AI_CANONNAME))
123 if (uname(&utsname))
124 return -EAI_SYSTEM;
126 if (name != NULL)
128 if (strcmp(name, "localhost") &&
129 strcmp(name, "local") &&
130 strcmp(name, "unix") &&
131 strcmp(name, utsname.nodename))
132 return GAIH_OKIFUNSPEC | -EAI_NONAME;
135 *pai = malloc (sizeof(struct addrinfo) + sizeof(struct sockaddr_un)
136 + ((req->ai_flags & AI_CANONNAME)
137 ? (strlen(utsname.nodename) + 1): 0));
138 if (*pai == NULL)
139 return -EAI_MEMORY;
141 (*pai)->ai_next = NULL;
142 (*pai)->ai_flags = req->ai_flags;
143 (*pai)->ai_family = AF_LOCAL;
144 (*pai)->ai_socktype = req->ai_socktype ? req->ai_socktype : SOCK_STREAM;
145 (*pai)->ai_protocol = req->ai_protocol;
146 (*pai)->ai_addrlen = sizeof(struct sockaddr_un);
147 (*pai)->ai_addr = (void *)(*pai) + sizeof(struct addrinfo);
149 #if SALEN
150 ((struct sockaddr_un *) (*pai)->ai_addr)->sun_len =
151 sizeof (struct sockaddr_un);
152 #endif /* SALEN */
154 ((struct sockaddr_un *)(*pai)->ai_addr)->sun_family = AF_LOCAL;
155 memset(((struct sockaddr_un *)(*pai)->ai_addr)->sun_path, 0, UNIX_PATH_MAX);
157 if (service)
159 struct sockaddr_un *sunp = (struct sockaddr_un *) (*pai)->ai_addr;
161 if (strchr (service->name, '/') != NULL)
163 if (strlen (service->name) >= sizeof (sunp->sun_path))
164 return GAIH_OKIFUNSPEC | -EAI_SERVICE;
166 strcpy (sunp->sun_path, service->name);
168 else
170 if (strlen (P_tmpdir "/") + 1 + strlen (service->name) >=
171 sizeof (sunp->sun_path))
172 return GAIH_OKIFUNSPEC | -EAI_SERVICE;
174 __stpcpy (__stpcpy (sunp->sun_path, P_tmpdir "/"), service->name);
177 else
179 if (tmpnam (((struct sockaddr_un *) (*pai)->ai_addr)->sun_path) == NULL)
180 return -EAI_SYSTEM;
183 if (req->ai_flags & AI_CANONNAME)
184 strcpy ((*pai)->ai_canonname = (char *)(*pai) + sizeof(struct addrinfo) +
185 sizeof(struct sockaddr_un), utsname.nodename);
186 else
187 (*pai)->ai_canonname = NULL;
188 return 0;
191 static int
192 gaih_inet_serv (const char *servicename, struct gaih_typeproto *tp,
193 struct gaih_servtuple **st)
195 struct servent *s;
196 size_t tmpbuflen = 1024;
197 struct servent ts;
198 char *tmpbuf;
199 int r;
203 tmpbuf = __alloca (tmpbuflen);
204 if (tmpbuf == NULL)
205 return -EAI_MEMORY;
207 r = __getservbyname_r (servicename, tp->name, &ts, tmpbuf, tmpbuflen,
208 &s);
209 if (r)
211 if (errno == ERANGE)
212 tmpbuflen *= 2;
213 else
214 return GAIH_OKIFUNSPEC | -EAI_SERVICE;
217 while (r);
219 *st = malloc (sizeof (struct gaih_servtuple));
220 if (*st == NULL)
221 return -EAI_MEMORY;
223 (*st)->next = NULL;
224 (*st)->socktype = tp->socktype;
225 (*st)->protocol = tp->protocol;
226 (*st)->port = s->s_port;
228 return 0;
231 #define gethosts(_family, _type) \
233 int i, herrno; \
234 size_t tmpbuflen; \
235 struct hostent th; \
236 char *tmpbuf; \
237 tmpbuflen = 512; \
238 do { \
239 tmpbuflen *= 2; \
240 tmpbuf = __alloca (tmpbuflen); \
241 if (tmpbuf == NULL) \
242 return -EAI_MEMORY; \
243 rc = __gethostbyname2_r (name, _family, &th, tmpbuf, \
244 tmpbuflen, &h, &herrno); \
245 } while ((rc != 0) && \
246 (herrno == NETDB_INTERNAL) && (errno == ERANGE)); \
247 if ((rc != 0) && (herrno == NETDB_INTERNAL)) \
249 __set_h_errno (herrno); \
250 return -EAI_SYSTEM; \
252 if (h != NULL) \
254 for (i = 0; h->h_addr_list[i]; i++) \
256 if (*pat == NULL) \
258 *pat = __alloca (sizeof(struct gaih_addrtuple)); \
259 if (*pat == NULL) \
260 return -EAI_MEMORY; \
262 (*pat)->next = NULL; \
263 (*pat)->family = _family; \
264 memcpy ((*pat)->addr, h->h_addr_list[i], \
265 sizeof(_type)); \
266 pat = &((*pat)->next); \
271 static int
272 gaih_inet (const char *name, const struct gaih_service *service,
273 const struct addrinfo *req, struct addrinfo **pai)
275 struct gaih_typeproto *tp = gaih_inet_typeproto;
276 struct gaih_servtuple *st = &nullserv;
277 struct gaih_addrtuple *at = NULL;
278 int rc;
280 if (req->ai_protocol || req->ai_socktype)
282 for (tp++; tp->name &&
283 ((req->ai_socktype != tp->socktype) || !req->ai_socktype) &&
284 ((req->ai_protocol != tp->protocol) || !req->ai_protocol); tp++);
285 if (tp->name == NULL)
287 if (req->ai_socktype)
288 return (GAIH_OKIFUNSPEC | -EAI_SOCKTYPE);
289 else
290 return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
294 if (service != NULL)
296 if (service->num < 0)
298 if (tp->name != NULL)
300 if ((rc = gaih_inet_serv (service->name, tp, &st)))
301 return rc;
303 else
305 struct gaih_servtuple **pst = &st;
306 for (tp++; tp->name; tp++)
308 if ((rc = gaih_inet_serv (service->name, tp, pst)))
310 if (rc & GAIH_OKIFUNSPEC)
311 continue;
312 return rc;
314 pst = &((*pst)->next);
316 if (st == &nullserv)
317 return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
320 else
322 st = __alloca (sizeof(struct gaih_servtuple));
323 if (st == NULL)
324 return -EAI_MEMORY;
326 st->next = NULL;
327 st->socktype = tp->socktype;
328 st->protocol = tp->protocol;
329 st->port = htons (service->num);
333 if (name != NULL)
335 at = __alloca (sizeof(struct gaih_addrtuple));
336 if (at == NULL)
337 return -EAI_MEMORY;
339 at->family = 0;
340 at->next = NULL;
342 if (at->family || !req->ai_family || (req->ai_family == AF_INET))
343 if (inet_pton (AF_INET, name, at->addr) > 0)
344 at->family = AF_INET;
346 if (!at->family && (!req->ai_family || (req->ai_family == AF_INET6)))
347 if (inet_pton (AF_INET6, name, at->addr) > 0)
348 at->family = AF_INET6;
350 if (at->family == AF_UNSPEC)
352 struct hostent *h;
353 struct gaih_addrtuple **pat = &at;
355 if ((req->ai_family == AF_UNSPEC) || (req->ai_family == AF_INET6))
356 gethosts (AF_INET6, struct in6_addr);
358 if ((req->ai_family == AF_UNSPEC) || (req->ai_family == AF_INET))
359 gethosts (AF_INET, struct in_addr);
362 if (at->family == AF_UNSPEC)
363 return (GAIH_OKIFUNSPEC | -EAI_NONAME);
366 else
368 at = __alloca (sizeof(struct gaih_addrtuple));
369 if (at == NULL)
370 return -EAI_MEMORY;
372 memset (at, 0, sizeof(struct gaih_addrtuple));
374 at->next = __alloca (sizeof(struct gaih_addrtuple));
375 if (at->next == NULL)
376 return -EAI_MEMORY;
378 at->family = AF_INET6;
380 memset (at->next, 0, sizeof(struct gaih_addrtuple));
381 at->next->family = AF_INET;
384 if (pai == NULL)
385 return 0;
388 const char *c = NULL;
389 struct gaih_servtuple *st2;
390 struct gaih_addrtuple *at2 = at;
391 size_t socklen, namelen;
394 buffer is the size of an unformatted IPv6 address in printable format.
396 char buffer[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
398 while (at2 != NULL)
400 if (req->ai_flags & AI_CANONNAME)
402 struct hostent *h = NULL;
404 int herrno;
405 struct hostent th;
406 size_t tmpbuflen = 512;
407 char *tmpbuf;
411 tmpbuflen *= 2;
412 tmpbuf = __alloca (tmpbuflen);
414 if (tmpbuf == NULL)
415 return -EAI_MEMORY;
417 rc = __gethostbyaddr_r (at2->addr,
418 ((at2->family == AF_INET6)
419 ? sizeof(struct in6_addr)
420 : sizeof(struct in_addr)),
421 at2->family, &th, tmpbuf, tmpbuflen,
422 &h, &herrno);
425 while ((rc != 0) && (herrno == NETDB_INTERNAL)
426 && (errno == ERANGE));
428 if ((rc != 0) && (herrno == NETDB_INTERNAL))
430 __set_h_errno (herrno);
431 return -EAI_SYSTEM;
434 if (h == NULL)
435 c = inet_ntop (at2->family, at2->addr, buffer, sizeof(buffer));
436 else
437 c = h->h_name;
439 if (c == NULL)
440 return GAIH_OKIFUNSPEC | -EAI_NONAME;
442 namelen = strlen (c) + 1;
444 else
445 namelen = 0;
447 if (at2->family == AF_INET6)
448 socklen = sizeof (struct sockaddr_in6);
449 else
450 socklen = sizeof (struct sockaddr_in);
452 for (st2 = st; st2 != NULL; st2 = st2->next)
454 *pai = malloc (sizeof (struct addrinfo) + socklen + namelen);
455 if (*pai == NULL)
456 return -EAI_MEMORY;
458 (*pai)->ai_flags = req->ai_flags;
459 (*pai)->ai_family = at2->family;
460 (*pai)->ai_socktype = st2->socktype;
461 (*pai)->ai_protocol = st2->protocol;
462 (*pai)->ai_addrlen = socklen;
463 (*pai)->ai_addr = (void *) (*pai) + sizeof(struct addrinfo);
464 #if SALEN
465 ((struct sockaddr_in *) (*pai)->ai_addr)->sin_len = i;
466 #endif /* SALEN */
467 ((struct sockaddr_in *) (*pai)->ai_addr)->sin_family = at2->family;
468 ((struct sockaddr_in *) (*pai)->ai_addr)->sin_port = st2->port;
470 if (at2->family == AF_INET6)
472 struct sockaddr_in6 *sin6p =
473 (struct sockaddr_in6 *) (*pai)->ai_addr;
475 sin6p->sin6_flowinfo = 0;
476 memcpy (&sin6p->sin6_addr,
477 at2->addr, sizeof (struct in6_addr));
479 else
481 struct sockaddr_in *sinp =
482 (struct sockaddr_in *) (*pai)->ai_addr;
483 memcpy (&sinp->sin_addr,
484 at2->addr, sizeof (struct in_addr));
485 memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero));
488 if (c)
490 (*pai)->ai_canonname = ((void *) (*pai) +
491 sizeof (struct addrinfo) + socklen);
492 strcpy ((*pai)->ai_canonname, c);
494 else
495 (*pai)->ai_canonname = NULL;
497 (*pai)->ai_next = NULL;
498 pai = &((*pai)->ai_next);
501 at2 = at2->next;
504 return 0;
507 static struct gaih gaih[] =
509 { PF_INET6, gaih_inet },
510 { PF_INET, gaih_inet },
511 { PF_LOCAL, gaih_local },
512 { PF_UNSPEC, NULL }
516 getaddrinfo (const char *name, const char *service,
517 const struct addrinfo *hints, struct addrinfo **pai)
519 int i = 0, j = 0;
520 struct addrinfo *p = NULL, **end;
521 struct gaih *g = gaih, *pg = NULL;
522 struct gaih_service gaih_service, *pservice;
524 if (name != NULL && name[0] == '*' && name[1] == 0)
525 name = NULL;
527 if (service != NULL && service[0] == '*' && service[1] == 0)
528 service = NULL;
530 if (name == NULL && service == NULL)
531 return EAI_NONAME;
533 if (hints == NULL)
534 hints = &default_hints;
536 if (hints->ai_flags & ~(AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST))
537 return EAI_BADFLAGS;
539 if ((hints->ai_flags & AI_CANONNAME) && name == NULL)
540 return EAI_BADFLAGS;
542 if (service && service[0])
544 char *c;
545 gaih_service.name = service;
546 gaih_service.num = strtoul (gaih_service.name, &c, 10);
547 if (*c)
548 gaih_service.num = -1;
549 else
550 /* Can't specify a numerical socket unless a protocol family was
551 given. */
552 if (hints->ai_socktype == 0)
553 return EAI_SERVICE;
554 pservice = &gaih_service;
556 else
557 pservice = NULL;
559 if (pai)
560 end = &p;
561 else
562 end = NULL;
564 while (g->gaih)
566 if ((hints->ai_family == g->family) || (hints->ai_family == AF_UNSPEC))
568 j++;
569 if ((pg == NULL) || (pg->gaih != g->gaih))
571 pg = g;
572 if ((i = g->gaih (name, pservice, hints, end)))
574 if ((hints->ai_family == AF_UNSPEC) && (i & GAIH_OKIFUNSPEC))
575 continue;
577 if (p)
578 freeaddrinfo (p);
580 return (i)?-(i & GAIH_EAI):EAI_NONAME;
582 if (end)
583 while(*end) end = &((*end)->ai_next);
586 ++g;
589 if (j == 0)
590 return EAI_FAMILY;
592 if (p)
594 *pai = p;
595 return 0;
598 if (pai == NULL && i == 0)
599 return 0;
601 if (p)
602 freeaddrinfo (p);
604 return i ? -(i & GAIH_EAI) : EAI_NONAME;
607 void
608 freeaddrinfo (struct addrinfo *ai)
610 struct addrinfo *p;
612 while (ai != NULL)
614 p = ai;
615 ai = ai->ai_next;
616 free (p);