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
, int flags
)
172 int tmpbuflen
= 1024;
174 char *tmpbuf
= alloca (tmpbuflen
);
178 if (sa
== NULL
|| addrlen
< sizeof (sa_family_t
))
181 switch (sa
->sa_family
)
184 if (addrlen
< (socklen_t
) (((struct sockaddr_un
*) NULL
)->sun_path
))
188 if (addrlen
< sizeof (struct sockaddr_in
))
192 if (addrlen
< sizeof (struct sockaddr_in6
))
199 if (host
!= NULL
&& hostlen
> 0)
200 switch (sa
->sa_family
)
204 if (!(flags
& NI_NUMERICHOST
))
206 struct hostent
*h
= NULL
;
209 if (sa
->sa_family
== AF_INET6
)
211 while (__gethostbyaddr_r ((void *) &(((struct sockaddr_in6
*) sa
)->sin6_addr
),
212 sizeof(struct in6_addr
),
213 AF_INET6
, &th
, tmpbuf
, tmpbuflen
,
216 if (herrno
== NETDB_INTERNAL
)
221 tmpbuf
= alloca (tmpbuflen
);
225 __set_h_errno (herrno
);
226 __set_errno (serrno
);
238 while (__gethostbyaddr_r ((void *) &(((struct sockaddr_in
*)sa
)->sin_addr
),
239 sizeof(struct in_addr
), AF_INET
,
240 &th
, tmpbuf
, tmpbuflen
,
246 tmpbuf
= alloca (tmpbuflen
);
258 if (flags
& NI_NOFQDN
)
261 if ((c
= nrl_domainname ()) && (c
= strstr(h
->h_name
, c
))
262 && (c
!= h
->h_name
) && (*(--c
) == '.'))
264 strncpy (host
, h
->h_name
,
265 min(hostlen
, (size_t) (c
- h
->h_name
)));
266 host
[min(hostlen
- 1, (size_t) (c
- h
->h_name
))]
272 strncpy (host
, h
->h_name
, hostlen
);
276 strncpy (host
, h
->h_name
, hostlen
);
283 if (flags
& NI_NAMEREQD
)
285 __set_errno (serrno
);
291 if (sa
->sa_family
== AF_INET6
)
293 struct sockaddr_in6
*sin6p
= (struct sockaddr_in6
*) sa
;
296 c
= inet_ntop (AF_INET6
,
297 (void *) &sin6p
->sin6_addr
, host
, hostlen
);
298 if (addrlen
> sizeof (struct sockaddr_in6
)
299 && (scopeid
= sin6p
->sin6_scope_id
))
301 /* Buffer is >= IFNAMSIZ+1. */
302 char scopebuf
[IFNAMSIZ
+ 1];
303 int ni_numericscope
= 0;
305 if (IN6_IS_ADDR_LINKLOCAL (&sin6p
->sin6_addr
)
306 || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p
->sin6_addr
))
308 if (if_indextoname (scopeid
, scopebuf
) == NULL
)
316 char *scopeptr
= &scopebuf
[1];
320 scopebuf
[0] = SCOPE_DELIMITER
;
321 scopelen
= 1 + snprintf (scopeptr
,
327 real_hostlen
= __strnlen (host
, hostlen
);
328 if (real_hostlen
+ scopelen
+ 1 > hostlen
)
330 memcpy (host
+ real_hostlen
, scopebuf
, scopelen
);
335 c
= inet_ntop (AF_INET
,
336 (void *) &(((struct sockaddr_in
*) sa
)->sin_addr
),
340 __set_errno (serrno
);
349 if (!(flags
& NI_NUMERICHOST
))
351 struct utsname utsname
;
353 if (!uname (&utsname
))
355 strncpy (host
, utsname
.nodename
, hostlen
);
360 if (flags
& NI_NAMEREQD
)
362 __set_errno (serrno
);
366 strncpy (host
, "localhost", hostlen
);
373 if (serv
&& (servlen
> 0))
374 switch (sa
->sa_family
)
378 if (!(flags
& NI_NUMERICSERV
))
380 struct servent
*s
, ts
;
381 while (__getservbyport_r (((struct sockaddr_in
*) sa
)->sin_port
,
382 ((flags
& NI_DGRAM
) ? "udp" : "tcp"),
383 &ts
, tmpbuf
, tmpbuflen
, &s
))
385 if (herrno
== NETDB_INTERNAL
)
390 tmpbuf
= __alloca (tmpbuflen
);
394 __set_errno (serrno
);
405 strncpy (serv
, s
->s_name
, servlen
);
409 __snprintf (serv
, servlen
, "%d",
410 ntohs (((struct sockaddr_in
*) sa
)->sin_port
));
414 strncpy (serv
, ((struct sockaddr_un
*) sa
)->sun_path
, servlen
);
418 if (host
&& (hostlen
> 0))
420 if (serv
&& (servlen
> 0))