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
11 1. All terms of the all other applicable copyrights and licenses must be
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
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>
50 #include <sys/socket.h>
53 #include <sys/utsname.h>
55 #include <netinet/in.h>
58 #include <arpa/inet.h>
60 #define GAIH_OKIFUNSPEC 0x0100
61 #define GAIH_EAI ~(GAIH_OKIFUNSPEC)
64 #define UNIX_PATH_MAX 108
75 struct gaih_servtuple
*next
;
81 static struct gaih_servtuple nullserv
= { NULL
, 0, 0, 0 };
85 struct gaih_addrtuple
*next
;
97 static struct gaih_typeproto gaih_inet_typeproto
[] =
100 { SOCK_STREAM
, IPPROTO_TCP
, (char *) "tcp" },
101 { SOCK_DGRAM
, IPPROTO_UDP
, (char *) "udp" },
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
};
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
))
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));
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
);
150 ((struct sockaddr_un
*) (*pai
)->ai_addr
)->sun_len
=
151 sizeof (struct sockaddr_un
);
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
);
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
);
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
);
179 if (tmpnam (((struct sockaddr_un
*) (*pai
)->ai_addr
)->sun_path
) == NULL
)
183 if (req
->ai_flags
& AI_CANONNAME
)
184 strcpy ((*pai
)->ai_canonname
= (char *)(*pai
) + sizeof(struct addrinfo
) +
185 sizeof(struct sockaddr_un
), utsname
.nodename
);
187 (*pai
)->ai_canonname
= NULL
;
192 gaih_inet_serv (const char *servicename
, struct gaih_typeproto
*tp
,
193 struct gaih_servtuple
**st
)
196 size_t tmpbuflen
= 1024;
203 tmpbuf
= __alloca (tmpbuflen
);
207 r
= __getservbyname_r (servicename
, tp
->name
, &ts
, tmpbuf
, tmpbuflen
,
214 return GAIH_OKIFUNSPEC
| -EAI_SERVICE
;
219 *st
= malloc (sizeof (struct gaih_servtuple
));
224 (*st
)->socktype
= tp
->socktype
;
225 (*st
)->protocol
= tp
->protocol
;
226 (*st
)->port
= s
->s_port
;
231 #define gethosts(_family, _type) \
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; \
254 for (i = 0; h->h_addr_list[i]; i++) \
258 *pat = __alloca (sizeof(struct gaih_addrtuple)); \
260 return -EAI_MEMORY; \
262 (*pat)->next = NULL; \
263 (*pat)->family = _family; \
264 memcpy ((*pat)->addr, h->h_addr_list[i], \
266 pat = &((*pat)->next); \
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
;
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
);
290 return (GAIH_OKIFUNSPEC
| -EAI_SERVICE
);
296 if (service
->num
< 0)
298 if (tp
->name
!= NULL
)
300 if ((rc
= gaih_inet_serv (service
->name
, tp
, &st
)))
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
)
314 pst
= &((*pst
)->next
);
317 return (GAIH_OKIFUNSPEC
| -EAI_SERVICE
);
322 st
= __alloca (sizeof(struct gaih_servtuple
));
327 st
->socktype
= tp
->socktype
;
328 st
->protocol
= tp
->protocol
;
329 st
->port
= htons (service
->num
);
335 at
= __alloca (sizeof(struct gaih_addrtuple
));
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
)
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
);
368 at
= __alloca (sizeof(struct gaih_addrtuple
));
372 memset (at
, 0, sizeof(struct gaih_addrtuple
));
374 at
->next
= __alloca (sizeof(struct gaih_addrtuple
));
375 if (at
->next
== NULL
)
378 at
->family
= AF_INET6
;
380 memset (at
->next
, 0, sizeof(struct gaih_addrtuple
));
381 at
->next
->family
= AF_INET
;
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"];
400 if (req
->ai_flags
& AI_CANONNAME
)
402 struct hostent
*h
= NULL
;
406 size_t tmpbuflen
= 512;
412 tmpbuf
= __alloca (tmpbuflen
);
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
,
425 while ((rc
!= 0) && (herrno
== NETDB_INTERNAL
)
426 && (errno
== ERANGE
));
428 if ((rc
!= 0) && (herrno
== NETDB_INTERNAL
))
430 __set_h_errno (herrno
);
435 c
= inet_ntop (at2
->family
, at2
->addr
, buffer
, sizeof(buffer
));
440 return GAIH_OKIFUNSPEC
| -EAI_NONAME
;
442 namelen
= strlen (c
) + 1;
447 if (at2
->family
== AF_INET6
)
448 socklen
= sizeof (struct sockaddr_in6
);
450 socklen
= sizeof (struct sockaddr_in
);
452 for (st2
= st
; st2
!= NULL
; st2
= st2
->next
)
454 *pai
= malloc (sizeof (struct addrinfo
) + socklen
+ namelen
);
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
);
465 ((struct sockaddr_in
*) (*pai
)->ai_addr
)->sin_len
= i
;
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
));
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
));
490 (*pai
)->ai_canonname
= ((void *) (*pai
) +
491 sizeof (struct addrinfo
) + socklen
);
492 strcpy ((*pai
)->ai_canonname
, c
);
495 (*pai
)->ai_canonname
= NULL
;
497 (*pai
)->ai_next
= NULL
;
498 pai
= &((*pai
)->ai_next
);
507 static struct gaih gaih
[] =
509 { PF_INET6
, gaih_inet
},
510 { PF_INET
, gaih_inet
},
511 { PF_LOCAL
, gaih_local
},
516 getaddrinfo (const char *name
, const char *service
,
517 const struct addrinfo
*hints
, struct addrinfo
**pai
)
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)
527 if (service
!= NULL
&& service
[0] == '*' && service
[1] == 0)
530 if (name
== NULL
&& service
== NULL
)
534 hints
= &default_hints
;
536 if (hints
->ai_flags
& ~3)
539 if ((hints
->ai_flags
& AI_CANONNAME
) && name
== NULL
)
542 if (service
&& service
[0])
545 gaih_service
.name
= service
;
546 gaih_service
.num
= strtoul (gaih_service
.name
, &c
, 10);
548 gaih_service
.num
= -1;
550 /* Can't specify a numerical socket unless a protocol family was
552 if (hints
->ai_socktype
== 0)
554 pservice
= &gaih_service
;
566 if ((hints
->ai_family
== g
->family
) || (hints
->ai_family
== AF_UNSPEC
))
569 if ((pg
== NULL
) || (pg
->gaih
!= g
->gaih
))
572 if ((i
= g
->gaih (name
, pservice
, hints
, end
)))
574 if ((hints
->ai_family
== AF_UNSPEC
) && (i
& GAIH_OKIFUNSPEC
))
580 return (i
)?-(i
& GAIH_EAI
):EAI_NONAME
;
583 while(*end
) end
= &((*end
)->ai_next
);
598 if (pai
== NULL
&& i
== 0)
604 return i
? -(i
& GAIH_EAI
) : EAI_NONAME
;
608 freeaddrinfo (struct addrinfo
*ai
)