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. */
51 #include <arpa/inet.h>
53 #include <netinet/in.h>
54 #include <sys/param.h>
55 #include <sys/socket.h>
56 #include <sys/types.h>
58 #include <sys/utsname.h>
59 #include <bits/libc-lock.h>
62 # define min(x,y) (((x) > (y)) ? (y) : (x))
75 __libc_lock_define_initialized (static, lock
);
76 __libc_lock_lock (lock
);
81 struct hostent
*h
, th
;
82 size_t tmpbuflen
= 1024;
83 char *tmpbuf
= alloca (tmpbuflen
);
88 while (__gethostbyname_r ("localhost", &th
, tmpbuf
, tmpbuflen
, &h
,
91 if (herror
== NETDB_INTERNAL
&& errno
== ERANGE
)
94 tmpbuf
= alloca (tmpbuflen
);
100 if (h
&& (c
= strchr (h
->h_name
, '.')))
101 domain
= __strdup (++c
);
104 /* The name contains no domain information. Use the name
105 now to get more information. */
106 while (__gethostname (tmpbuf
, tmpbuflen
))
109 tmpbuf
= alloca (tmpbuflen
);
112 if ((c
= strchr (tmpbuf
, '.')))
113 domain
= __strdup (++c
);
116 /* We need to preserve the hostname. */
117 const char *hstname
= strdupa (tmpbuf
);
119 while (__gethostbyname_r (hstname
, &th
, tmpbuf
, tmpbuflen
,
122 if (herror
== NETDB_INTERNAL
&& errno
== ERANGE
)
125 tmpbuf
= alloca (tmpbuflen
);
131 if (h
&& (c
= strchr(h
->h_name
, '.')))
132 domain
= __strdup (++c
);
135 struct in_addr in_addr
;
137 in_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
139 while (__gethostbyaddr_r ((const char *) &in_addr
,
140 sizeof (struct in_addr
),
141 AF_INET
, &th
, tmpbuf
,
142 tmpbuflen
, &h
, &herror
))
144 if (herror
== NETDB_INTERNAL
&& errno
== ERANGE
)
147 tmpbuf
= alloca (tmpbuflen
);
153 if (h
&& (c
= strchr (h
->h_name
, '.')))
154 domain
= __strdup (++c
);
160 __libc_lock_unlock (lock
);
168 getnameinfo (const struct sockaddr
*sa
, socklen_t addrlen
, char *host
,
169 socklen_t hostlen
, char *serv
, socklen_t servlen
,
173 int tmpbuflen
= 1024;
175 char *tmpbuf
= alloca (tmpbuflen
);
179 if (flags
& ~(NI_NUMERICHOST
|NI_NUMERICSERV
|NI_NOFQDN
|NI_NAMEREQD
|NI_DGRAM
))
182 if (sa
== NULL
|| addrlen
< sizeof (sa_family_t
))
185 switch (sa
->sa_family
)
188 if (addrlen
< (socklen_t
) (((struct sockaddr_un
*) NULL
)->sun_path
))
192 if (addrlen
< sizeof (struct sockaddr_in
))
196 if (addrlen
< sizeof (struct sockaddr_in6
))
203 if (host
!= NULL
&& hostlen
> 0)
204 switch (sa
->sa_family
)
208 if (!(flags
& NI_NUMERICHOST
))
210 struct hostent
*h
= NULL
;
213 if (sa
->sa_family
== AF_INET6
)
215 while (__gethostbyaddr_r ((const void *) &(((const struct sockaddr_in6
*) sa
)->sin6_addr
),
216 sizeof(struct in6_addr
),
217 AF_INET6
, &th
, tmpbuf
, tmpbuflen
,
220 if (herrno
== NETDB_INTERNAL
)
225 tmpbuf
= alloca (tmpbuflen
);
229 __set_h_errno (herrno
);
230 __set_errno (serrno
);
242 while (__gethostbyaddr_r ((const void *) &(((const struct sockaddr_in
*)sa
)->sin_addr
),
243 sizeof(struct in_addr
), AF_INET
,
244 &th
, tmpbuf
, tmpbuflen
,
250 tmpbuf
= alloca (tmpbuflen
);
263 if ((flags
& NI_NOFQDN
)
264 && (c
= nrl_domainname ())
265 && (c
= strstr (h
->h_name
, c
))
266 && (c
!= h
->h_name
) && (*(--c
) == '.'))
268 strncpy (host
, h
->h_name
,
269 min(hostlen
, (size_t) (c
- h
->h_name
)));
270 host
[min(hostlen
- 1, (size_t) (c
- h
->h_name
))]
276 strncpy (host
, h
->h_name
, hostlen
);
284 if (flags
& NI_NAMEREQD
)
286 __set_errno (serrno
);
292 if (sa
->sa_family
== AF_INET6
)
294 const struct sockaddr_in6
*sin6p
;
297 sin6p
= (const struct sockaddr_in6
*) sa
;
299 c
= inet_ntop (AF_INET6
,
300 (const void *) &sin6p
->sin6_addr
, host
, hostlen
);
301 scopeid
= sin6p
->sin6_scope_id
;
304 /* Buffer is >= IFNAMSIZ+1. */
305 char scopebuf
[IFNAMSIZ
+ 1];
307 int ni_numericscope
= 0;
308 size_t real_hostlen
= __strnlen (host
, hostlen
);
311 scopebuf
[0] = SCOPE_DELIMITER
;
313 scopeptr
= &scopebuf
[1];
315 if (IN6_IS_ADDR_LINKLOCAL (&sin6p
->sin6_addr
)
316 || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p
->sin6_addr
))
318 if (if_indextoname (scopeid
, scopeptr
) == NULL
)
321 scopelen
= strlen (scopebuf
);
327 scopelen
= 1 + __snprintf (scopeptr
,
333 if (real_hostlen
+ scopelen
+ 1 > hostlen
)
334 /* XXX We should not fail here. Simply enlarge
335 the buffer or return with out of memory. */
337 memcpy (host
+ real_hostlen
, scopebuf
, scopelen
+ 1);
341 c
= inet_ntop (AF_INET
,
342 (const void *) &(((const struct sockaddr_in
*) sa
)->sin_addr
),
346 __set_errno (serrno
);
355 if (!(flags
& NI_NUMERICHOST
))
357 struct utsname utsname
;
359 if (!uname (&utsname
))
361 strncpy (host
, utsname
.nodename
, hostlen
);
366 if (flags
& NI_NAMEREQD
)
368 __set_errno (serrno
);
372 strncpy (host
, "localhost", hostlen
);
379 if (serv
&& (servlen
> 0))
380 switch (sa
->sa_family
)
384 if (!(flags
& NI_NUMERICSERV
))
386 struct servent
*s
, ts
;
387 while (__getservbyport_r (((const struct sockaddr_in
*) sa
)->sin_port
,
388 ((flags
& NI_DGRAM
) ? "udp" : "tcp"),
389 &ts
, tmpbuf
, tmpbuflen
, &s
))
391 if (herrno
== NETDB_INTERNAL
)
396 tmpbuf
= __alloca (tmpbuflen
);
400 __set_errno (serrno
);
411 strncpy (serv
, s
->s_name
, servlen
);
415 __snprintf (serv
, servlen
, "%d",
416 ntohs (((const struct sockaddr_in
*) sa
)->sin_port
));
420 strncpy (serv
, ((const struct sockaddr_un
*) sa
)->sun_path
, servlen
);
424 if (host
&& (hostlen
> 0))
426 if (serv
&& (servlen
> 0))