xref: /freebsd/crypto/heimdal/lib/roken/getnameinfo.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov  * Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include <config.h>
35b528cefcSMark Murray 
36b528cefcSMark Murray #include "roken.h"
37b528cefcSMark Murray 
38b528cefcSMark Murray static int
doit(int af,const void * addr,size_t addrlen,int port,char * host,size_t hostlen,char * serv,size_t servlen,int flags)39b528cefcSMark Murray doit (int af,
40b528cefcSMark Murray       const void *addr,
41b528cefcSMark Murray       size_t addrlen,
42b528cefcSMark Murray       int port,
43b528cefcSMark Murray       char *host, size_t hostlen,
44b528cefcSMark Murray       char *serv, size_t servlen,
45b528cefcSMark Murray       int flags)
46b528cefcSMark Murray {
47b528cefcSMark Murray     if (host != NULL) {
48b528cefcSMark Murray 	if (flags & NI_NUMERICHOST) {
49b528cefcSMark Murray 	    if (inet_ntop (af, addr, host, hostlen) == NULL)
50b528cefcSMark Murray 		return EAI_SYSTEM;
51b528cefcSMark Murray 	} else {
52b528cefcSMark Murray 	    struct hostent *he = gethostbyaddr (addr,
53b528cefcSMark Murray 						addrlen,
54b528cefcSMark Murray 						af);
55b528cefcSMark Murray 	    if (he != NULL) {
564137ff4cSJacques Vidrine 		strlcpy (host, hostent_find_fqdn(he), hostlen);
57b528cefcSMark Murray 		if (flags & NI_NOFQDN) {
58b528cefcSMark Murray 		    char *dot = strchr (host, '.');
59b528cefcSMark Murray 		    if (dot != NULL)
60b528cefcSMark Murray 			*dot = '\0';
61b528cefcSMark Murray 		}
62b528cefcSMark Murray 	    } else if (flags & NI_NAMEREQD) {
63b528cefcSMark Murray 		return EAI_NONAME;
64adb0ddaeSAssar Westerlund 	    } else if (inet_ntop (af, addr, host, hostlen) == NULL)
65b528cefcSMark Murray 		return EAI_SYSTEM;
66b528cefcSMark Murray 	}
67b528cefcSMark Murray     }
68b528cefcSMark Murray 
69b528cefcSMark Murray     if (serv != NULL) {
70b528cefcSMark Murray 	if (flags & NI_NUMERICSERV) {
71b528cefcSMark Murray 	    snprintf (serv, servlen, "%u", ntohs(port));
72b528cefcSMark Murray 	} else {
73b528cefcSMark Murray 	    const char *proto = "tcp";
74b528cefcSMark Murray 	    struct servent *se;
75b528cefcSMark Murray 
76b528cefcSMark Murray 	    if (flags & NI_DGRAM)
77b528cefcSMark Murray 		proto = "udp";
78b528cefcSMark Murray 
79b528cefcSMark Murray 	    se = getservbyport (port, proto);
80b528cefcSMark Murray 	    if (se == NULL) {
81b528cefcSMark Murray 		snprintf (serv, servlen, "%u", ntohs(port));
82b528cefcSMark Murray 	    } else {
83b528cefcSMark Murray 		strlcpy (serv, se->s_name, servlen);
84b528cefcSMark Murray 	    }
85b528cefcSMark Murray 	}
86b528cefcSMark Murray     }
87b528cefcSMark Murray     return 0;
88b528cefcSMark Murray }
89b528cefcSMark Murray 
90b528cefcSMark Murray /*
91b528cefcSMark Murray  *
92b528cefcSMark Murray  */
93b528cefcSMark Murray 
94*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
getnameinfo(const struct sockaddr * sa,socklen_t salen,char * host,size_t hostlen,char * serv,size_t servlen,int flags)95b528cefcSMark Murray getnameinfo(const struct sockaddr *sa, socklen_t salen,
96b528cefcSMark Murray 	    char *host, size_t hostlen,
97b528cefcSMark Murray 	    char *serv, size_t servlen,
98b528cefcSMark Murray 	    int flags)
99b528cefcSMark Murray {
100b528cefcSMark Murray     switch (sa->sa_family) {
101b528cefcSMark Murray #ifdef HAVE_IPV6
102b528cefcSMark Murray     case AF_INET6 : {
103b528cefcSMark Murray 	const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
104b528cefcSMark Murray 
105b528cefcSMark Murray 	return doit (AF_INET6, &sin6->sin6_addr, sizeof(sin6->sin6_addr),
106b528cefcSMark Murray 		     sin6->sin6_port,
107b528cefcSMark Murray 		     host, hostlen,
108b528cefcSMark Murray 		     serv, servlen,
109b528cefcSMark Murray 		     flags);
110b528cefcSMark Murray     }
111b528cefcSMark Murray #endif
112b528cefcSMark Murray     case AF_INET : {
113c19800e8SDoug Rabson 	const struct sockaddr_in *sin4 = (const struct sockaddr_in *)sa;
114b528cefcSMark Murray 
115c19800e8SDoug Rabson 	return doit (AF_INET, &sin4->sin_addr, sizeof(sin4->sin_addr),
116c19800e8SDoug Rabson 		     sin4->sin_port,
117b528cefcSMark Murray 		     host, hostlen,
118b528cefcSMark Murray 		     serv, servlen,
119b528cefcSMark Murray 		     flags);
120b528cefcSMark Murray     }
121b528cefcSMark Murray     default :
122b528cefcSMark Murray 	return EAI_FAMILY;
123b528cefcSMark Murray     }
124b528cefcSMark Murray }
125