xref: /freebsd/lib/libutil/realhostname.c (revision 9ed4e87cad6ce8f1e8faf39636d7580bbc769fbd)
1 /*-
2  * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 
31 #include <sys/socket.h>
32 #include <netdb.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 
36 #include <stdio.h>
37 #include <string.h>
38 
39 #include "libutil.h"
40 
41 /* wrapper for KAME-special getnameinfo() */
42 #ifndef NI_WITHSCOPEID
43 #define	NI_WITHSCOPEID	0
44 #endif
45 
46 struct sockinet {
47 	u_char	si_len;
48 	u_char	si_family;
49 	u_short	si_port;
50 };
51 
52 int
53 realhostname(char *host, size_t hsize, const struct in_addr *ip)
54 {
55 	int result;
56 	struct hostent *hp;
57 
58 	result = HOSTNAME_INVALIDADDR;
59 	hp = gethostbyaddr((char *)ip, sizeof(*ip), AF_INET);
60 
61 	if (hp != NULL && strlen(hp->h_name) <= hsize) {
62 		char lookup[MAXHOSTNAMELEN];
63 
64 		strncpy(lookup, hp->h_name, sizeof(lookup) - 1);
65 		lookup[sizeof(lookup) - 1] = '\0';
66 		hp = gethostbyname(lookup);
67 		if (hp == NULL)
68 			result = HOSTNAME_INVALIDNAME;
69 		else for (; ; hp->h_addr_list++) {
70 			if (hp->h_addr_list[0] == NULL) {
71 				result = HOSTNAME_INCORRECTNAME;
72 				break;
73 			}
74 			if (!memcmp(*hp->h_addr_list, ip, sizeof(*ip))) {
75 				strncpy(host, lookup, hsize);
76 				return HOSTNAME_FOUND;
77 			}
78 		}
79 	}
80 
81 	strncpy(host, inet_ntoa(*ip), hsize);
82 
83 	return result;
84 }
85 
86 int
87 realhostname_sa(char *host, size_t hsize, struct sockaddr *addr, int addrlen)
88 {
89 	int result, error;
90 	char buf[NI_MAXHOST];
91 
92 	result = HOSTNAME_INVALIDADDR;
93 
94 	error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, 0);
95 	if (error == 0) {
96 		struct addrinfo hints, *res, *ores;
97 		struct sockaddr *sa;
98 
99 		memset(&hints, 0, sizeof(struct addrinfo));
100 		hints.ai_family =
101 			(addr->sa_family == AF_INET) ? AF_INET : AF_UNSPEC;
102 		hints.ai_flags = AI_CANONNAME;
103 
104 		error = getaddrinfo(buf, NULL, &hints, &res);
105 		if (error) {
106 			result = HOSTNAME_INVALIDNAME;
107 			goto numeric;
108 		} else for (ores = res; ; res = res->ai_next) {
109 			if (res == NULL) {
110 				freeaddrinfo(ores);
111 				result = HOSTNAME_INCORRECTNAME;
112 				goto numeric;
113 			}
114 			sa = res->ai_addr;
115 			if (sa == NULL) {
116 				freeaddrinfo(ores);
117 				result = HOSTNAME_INCORRECTNAME;
118 				goto numeric;
119 			}
120 			if (sa->sa_len == addrlen &&
121 			    sa->sa_family == addr->sa_family) {
122 				u_int16_t port;
123 
124 				port = ((struct sockinet *)addr)->si_port;
125 				((struct sockinet *)addr)->si_port = 0;
126 				if (!memcmp(sa, addr, sa->sa_len)) {
127 					result = HOSTNAME_FOUND;
128 					((struct sockinet *)addr)->si_port =
129 						port;
130 					if (res->ai_canonname == 0) {
131 						freeaddrinfo(ores);
132 						goto numeric;
133 					}
134 					if (strlen(res->ai_canonname) > hsize &&
135 					    addr->sa_family == AF_INET) {
136 						freeaddrinfo(ores);
137 						goto numeric;
138 					}
139 					strncpy(host, res->ai_canonname,
140 						hsize);
141 					break;
142 				}
143 				((struct sockinet *)addr)->si_port = port;
144 			}
145 #ifdef INET6
146 			/*
147 			 * XXX IPv4 mapped IPv6 addr consideraton,
148 			 * specified in rfc2373.
149 			 */
150 			if (sa->sa_family == AF_INET &&
151 			    addr->sa_family == AF_INET6) {
152 				struct in_addr *in;
153 				struct in6_addr *in6;
154 
155 				in = &((struct sockaddr_in *)sa)->sin_addr;
156 				in6 = &((struct sockaddr_in6 *)addr)->sin6_addr;
157 				if (IN6_IS_ADDR_V4MAPPED(in6) &&
158 				    !memcmp(&in6->s6_addr[12], in,
159 					    sizeof(*in))) {
160 					result = HOSTNAME_FOUND;
161 					if (res->ai_canonname == 0 ||
162 					    strlen(res->ai_canonname) > hsize) {
163 						freeaddrinfo(ores);
164 						goto numeric;
165 					}
166 					strncpy(host, res->ai_canonname,
167 						hsize);
168 					break;
169 				}
170 			}
171 #endif
172 		}
173 		freeaddrinfo(ores);
174 	} else {
175 		struct sockaddr_in sin;
176     numeric:
177 #ifdef INET6
178 		if (addr->sa_family == AF_INET6) {
179 			struct sockaddr_in6 *sin6;
180 
181 			sin6 = (struct sockaddr_in6 *)addr;
182 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
183 				memset(&sin, 0, sizeof(sin));
184 				sin.sin_len = sizeof(struct sockaddr_in);
185 				sin.sin_family = AF_INET;
186 				sin.sin_port = sin6->sin6_port;
187 				memcpy(&sin.sin_addr,
188 				       &sin6->sin6_addr.s6_addr[12],
189 				       sizeof(struct in_addr));
190 				addr = (struct sockaddr *)&sin;
191 				addrlen = sin.sin_len;
192 			}
193 		}
194 #endif
195 		if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
196 				NI_NUMERICHOST|NI_WITHSCOPEID) == 0)
197 			strncpy(host, buf, hsize);
198 	}
199 
200 	return result;
201 }
202 
203 
204