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 #include <sys/types.h>
46 #include <sys/socket.h>
48 #include <netinet/in.h>
50 #include <sys/utsname.h>
57 #include <bits/libc-lock.h>
58 #include <arpa/inet.h>
61 # define min(x,y) (((x) > (y)) ? (y) : (x))
74 __libc_lock_define_initialized (static, lock
);
75 __libc_lock_lock (lock
);
80 struct hostent
*h
, th
;
81 size_t tmpbuflen
= 1024;
82 char *tmpbuf
= alloca (tmpbuflen
);
87 while (__gethostbyname_r ("localhost", &th
, tmpbuf
, tmpbuflen
, &h
,
90 if (herror
== NETDB_INTERNAL
&& errno
== ERANGE
)
93 tmpbuf
= alloca (tmpbuflen
);
99 if (h
&& (c
= strchr (h
->h_name
, '.')))
100 domain
= __strdup (++c
);
103 /* The name contains no domain information. Use the name
104 now to get more information. */
105 while (__gethostname (tmpbuf
, tmpbuflen
))
108 tmpbuf
= alloca (tmpbuflen
);
111 if ((c
= strchr (tmpbuf
, '.')))
112 domain
= __strdup (++c
);
115 /* We need to preserve the hostname. */
116 const char *hstname
= strdupa (tmpbuf
);
118 while (__gethostbyname_r (hstname
, &th
, tmpbuf
, tmpbuflen
,
121 if (herror
== NETDB_INTERNAL
&& errno
== ERANGE
)
124 tmpbuf
= alloca (tmpbuflen
);
130 if (h
&& (c
= strchr(h
->h_name
, '.')))
131 domain
= __strdup (++c
);
134 struct in_addr in_addr
;
136 in_addr
.s_addr
= htonl (0x7f000001);
138 while (__gethostbyaddr_r ((const char *) &in_addr
,
139 sizeof (struct in_addr
),
140 AF_INET
, &th
, tmpbuf
,
141 tmpbuflen
, &h
, &herror
))
143 if (herror
== NETDB_INTERNAL
&& errno
== ERANGE
)
146 tmpbuf
= alloca (tmpbuflen
);
152 if (h
&& (c
= strchr (h
->h_name
, '.')))
153 domain
= __strdup (++c
);
159 __libc_lock_unlock (lock
);
167 getnameinfo (const struct sockaddr
*sa
, socklen_t addrlen
, char *host
,
168 size_t hostlen
, char *serv
, size_t servlen
, int flags
)
171 int tmpbuflen
= 1024;
173 char *tmpbuf
= alloca (tmpbuflen
);
179 if (host
!= NULL
&& hostlen
> 0)
180 switch(sa
->sa_family
)
184 if (!(flags
& NI_NUMERICHOST
))
186 struct hostent
*h
= NULL
;
189 if (sa
->sa_family
== AF_INET6
)
191 while (__gethostbyaddr_r ((void *) &(((struct sockaddr_in6
*) sa
)->sin6_addr
),
192 sizeof(struct in6_addr
),
193 AF_INET6
, &th
, tmpbuf
, tmpbuflen
,
196 if (herrno
== NETDB_INTERNAL
)
201 tmpbuf
= alloca (tmpbuflen
);
205 __set_h_errno (herrno
);
206 __set_errno (serrno
);
218 while (__gethostbyaddr_r ((void *) &(((struct sockaddr_in
*)sa
)->sin_addr
),
219 sizeof(struct in_addr
), AF_INET
,
220 &th
, tmpbuf
, tmpbuflen
,
226 tmpbuf
= alloca (tmpbuflen
);
238 if (flags
& NI_NOFQDN
)
241 if ((c
= nrl_domainname ()) && (c
= strstr(h
->h_name
, c
))
242 && (c
!= h
->h_name
) && (*(--c
) == '.'))
244 strncpy (host
, h
->h_name
,
245 min(hostlen
, (size_t) (c
- h
->h_name
)));
249 strncpy (host
, h
->h_name
, hostlen
);
254 if (flags
& NI_NAMEREQD
)
256 __set_errno (serrno
);
262 if (sa
->sa_family
== AF_INET6
)
263 c
= inet_ntop (AF_INET6
,
264 (void *) &(((struct sockaddr_in6
*) sa
)->sin6_addr
),
267 c
= inet_ntop (AF_INET
,
268 (void *) &(((struct sockaddr_in
*) sa
)->sin_addr
),
273 __set_errno (serrno
);
280 if (!(flags
& NI_NUMERICHOST
))
282 struct utsname utsname
;
284 if (!uname (&utsname
))
286 strncpy (host
, utsname
.nodename
, hostlen
);
291 if (flags
& NI_NAMEREQD
)
293 __set_errno (serrno
);
297 strncpy (host
, "localhost", hostlen
);
304 if (serv
&& (servlen
> 0))
305 switch(sa
->sa_family
)
309 if (!(flags
& NI_NUMERICSERV
))
311 struct servent
*s
, ts
;
312 while (__getservbyport_r (((struct sockaddr_in
*) sa
)->sin_port
,
313 ((flags
& NI_DGRAM
) ? "udp" : "tcp"),
314 &ts
, tmpbuf
, tmpbuflen
, &s
))
316 if (herrno
== NETDB_INTERNAL
)
321 tmpbuf
= __alloca (tmpbuflen
);
325 __set_errno (serrno
);
336 strncpy (serv
, s
->s_name
, servlen
);
340 __snprintf (serv
, servlen
, "%d",
341 ntohs (((struct sockaddr_in
*) sa
)->sin_port
));
345 strncpy (serv
, ((struct sockaddr_un
*) sa
)->sun_path
, servlen
);
349 if (host
&& (hostlen
> 0))
351 if (serv
&& (servlen
> 0))