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. */
50 #include <sys/types.h>
51 #include <sys/socket.h>
53 #include <netinet/in.h>
56 #include <sys/utsname.h>
64 #include <bits/libc-lock.h>
65 #include <arpa/inet.h>
68 #define AF_LOCAL AF_UNIX
72 struct hostent *_addr2hostname_hosts(const char *, int, int);
73 #endif /* HOSTTABLE */
75 #ifndef MAXHOSTNAMELEN
76 #define MAXHOSTNAMELEN 128
80 #define min(x,y) (((x) > (y)) ? (y) : (x))
84 static char domainbuffer[MAXHOSTNAMELEN];
86 static char *nrl_domainname(void)
92 __libc_lock_define_initialized (static, lock);
93 __libc_lock_lock (lock);
97 struct hostent *h, th;
99 char *tmpbuf = __alloca(tmpbuflen);
104 while (__gethostbyname_r("localhost", &th, tmpbuf, tmpbuflen, &h,
106 if (herror == NETDB_INTERNAL) {
107 if (errno == ERANGE) {
109 tmpbuf = __alloca(tmpbuflen);
116 if (h && (c = strchr(h->h_name, '.'))) {
117 strcpy(domain = domainbuffer, ++c);
121 if (!gethostname(domainbuffer, sizeof(domainbuffer))) {
122 if (c = strchr(domainbuffer, '.')) {
127 while (__gethostbyname_r(domainbuffer, &th, tmpbuf, tmpbuflen, &h,
129 if (herror == NETDB_INTERNAL) {
130 if (errno == ERANGE) {
132 tmpbuf = __alloca(tmpbuflen);
139 if (h && (c = strchr(h->h_name, '.'))) {
140 strcpy(domain = domainbuffer, ++c);
146 struct in_addr in_addr;
148 in_addr.s_addr = htonl(0x7f000001);
150 while (__gethostbyaddr_r((const char *)&in_addr, sizeof(struct in_addr), AF_INET, &th, tmpbuf, tmpbuflen, &h, &herror)) {
151 if (herror == NETDB_INTERNAL) {
152 if (errno == ERANGE) {
154 tmpbuf = __alloca(tmpbuflen);
161 if (h && (c = strchr(h->h_name, '.'))) {
162 domain = domainbuffer, ++c;
170 __libc_lock_unlock (lock);
176 int getnameinfo(const struct sockaddr *sa, size_t addrlen, char *host, size_t hostlen, char *serv, size_t servlen, int flags)
179 int tmpbuflen = 1024;
181 char *tmpbuf = __alloca(tmpbuflen);
187 if (host && (hostlen > 0))
188 switch(sa->sa_family) {
193 if (!(flags & NI_NUMERICHOST)) {
194 struct hostent *h = NULL;
197 if (sa->sa_family == AF_INET6)
198 h = _addr2hostname_hosts((void *)&(((struct sockaddr_in6 *)sa)->sin6_addr), sizeof(struct in6_addr), AF_INET6);
201 h = _addr2hostname_hosts((void *)&(((struct sockaddr_in *)sa)->sin_addr), sizeof(struct in_addr), AF_INET);
202 #endif /* HOSTTABLE */
207 if (sa->sa_family == AF_INET6) {
208 while (__gethostbyaddr_r((void *)&(((struct sockaddr_in6 *)sa)->sin6_addr), sizeof(struct in6_addr), AF_INET6, &th, tmpbuf, tmpbuflen, &h, &herrno)) {
209 if (herrno == NETDB_INTERNAL) {
210 if (errno == ERANGE) {
212 tmpbuf = __alloca(tmpbuflen);
214 __set_h_errno(herrno);
223 while (__gethostbyaddr_r((void *)&(((struct sockaddr_in *)sa)->sin_addr), sizeof(struct in_addr), AF_INET, &th, tmpbuf, tmpbuflen, &h, &herrno)) {
224 if (errno == ERANGE) {
226 tmpbuf = __alloca(tmpbuflen);
233 #endif /* RESOLVER */
236 if (flags & NI_NOFQDN) {
238 if ((c = nrl_domainname()) && (c = strstr(h->h_name, c)) && (c != h->h_name) && (*(--c) == '.')) {
239 strncpy(host, h->h_name, min(hostlen, (size_t) (c - h->h_name)));
243 strncpy(host, h->h_name, hostlen);
248 if (flags & NI_NAMEREQD)
254 if (sa->sa_family == AF_INET6)
255 c = inet_ntop(AF_INET6, (void *)&(((struct sockaddr_in6 *)sa)->sin6_addr), host, hostlen);
258 c = inet_ntop(AF_INET, (void *)&(((struct sockaddr_in *)sa)->sin_addr), host, hostlen);
266 if (!(flags & NI_NUMERICHOST)) {
267 struct utsname utsname;
269 if (!uname(&utsname)) {
270 strncpy(host, utsname.nodename, hostlen);
275 if (flags & NI_NAMEREQD)
278 strncpy(host, "localhost", hostlen);
285 if (serv && (servlen > 0))
286 switch(sa->sa_family) {
291 if (!(flags & NI_NUMERICSERV)) {
292 struct servent *s, ts;
293 while (__getservbyport_r(((struct sockaddr_in *)sa)->sin_port, ((flags & NI_DGRAM) ? "udp" : "tcp"), &ts, tmpbuf, tmpbuflen, &s)) {
294 if (herrno == NETDB_INTERNAL) {
295 if (errno == ERANGE) {
297 tmpbuf = __alloca(tmpbuflen);
306 strncpy(serv, s->s_name, servlen);
310 snprintf(serv, servlen, "%d", ntohs(((struct sockaddr_in *)sa)->sin_port));
314 strncpy(serv, ((struct sockaddr_un *)sa)->sun_path, servlen);
318 if (host && (hostlen > 0))
320 if (serv && (servlen > 0))