xref: /freebsd/lib/libutil/realhostname.c (revision 830940567b49bb0c08dfaed40418999e76616909)
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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/socket.h>
32 
33 #include <netdb.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 
37 #include <stdio.h>
38 #include <string.h>
39 
40 #include "libutil.h"
41 
42 struct sockinet {
43 	u_char	si_len;
44 	u_char	si_family;
45 	u_short	si_port;
46 };
47 
48 int
49 realhostname(char *host, size_t hsize, const struct in_addr *ip)
50 {
51 	char trimmed[MAXHOSTNAMELEN];
52 	int result;
53 	struct hostent *hp;
54 
55 	result = HOSTNAME_INVALIDADDR;
56 	hp = gethostbyaddr((const char *)ip, sizeof(*ip), AF_INET);
57 
58 	if (hp != NULL) {
59 		strlcpy(trimmed, hp->h_name, sizeof(trimmed));
60 		trimdomain(trimmed, strlen(trimmed));
61 		if (strlen(trimmed) <= hsize) {
62 			char lookup[MAXHOSTNAMELEN];
63 
64 			strlcpy(lookup, hp->h_name, sizeof(lookup));
65 			hp = gethostbyname(lookup);
66 			if (hp == NULL)
67 				result = HOSTNAME_INVALIDNAME;
68 			else for (; ; hp->h_addr_list++) {
69 				if (*hp->h_addr_list == NULL) {
70 					result = HOSTNAME_INCORRECTNAME;
71 					break;
72 				}
73 				if (!memcmp(*hp->h_addr_list, ip, sizeof(*ip))) {
74 					strncpy(host, trimmed, hsize);
75 					return HOSTNAME_FOUND;
76 				}
77 			}
78 		}
79 	}
80 
81 	strncpy(host, inet_ntoa(*ip), hsize);
82 
83 	return result;
84 }
85 
86 /*
87  * struct sockaddr has very lax alignment requirements, since all its
88  * members are char or equivalent.  This is a problem when trying to
89  * dereference a struct sockaddr_in6 * that was passed in as a struct
90  * sockaddr *.  Although we know (or trust) that the passed-in struct was
91  * properly aligned, the compiler doesn't, and (rightly) complains.  These
92  * macros perform the cast in a way that the compiler will accept.
93  */
94 #define SOCKADDR_IN6(p) ((struct sockaddr_in6 *)(void *)(p))
95 #define SOCKADDR_IN(p) ((struct sockaddr_in *)(void *)(p))
96 #define SOCKINET(p) ((struct sockinet *)(void *)(p))
97 
98 int
99 realhostname_sa(char *host, size_t hsize, struct sockaddr *addr, int addrlen)
100 {
101 	int result, error;
102 	char buf[NI_MAXHOST];
103 #ifdef INET6
104 	struct sockaddr_in lsin;
105 #endif
106 
107 	result = HOSTNAME_INVALIDADDR;
108 
109 #ifdef INET6
110 	/* IPv4 mapped IPv6 addr consideraton, specified in rfc2373. */
111 	if (addr->sa_family == AF_INET6 &&
112 	    addrlen == sizeof(struct sockaddr_in6) &&
113 	    IN6_IS_ADDR_V4MAPPED(&SOCKADDR_IN6(addr)->sin6_addr)) {
114 		struct sockaddr_in6 *sin6;
115 
116 		sin6 = SOCKADDR_IN6(addr);
117 
118 		memset(&lsin, 0, sizeof(lsin));
119 		lsin.sin_len = sizeof(struct sockaddr_in);
120 		lsin.sin_family = AF_INET;
121 		lsin.sin_port = sin6->sin6_port;
122 		memcpy(&lsin.sin_addr, &sin6->sin6_addr.s6_addr[12],
123 		       sizeof(struct in_addr));
124 		addr = (struct sockaddr *)&lsin;
125 		addrlen = lsin.sin_len;
126 	}
127 #endif
128 
129 	error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
130 			    NI_NAMEREQD);
131 	if (error == 0) {
132 		struct addrinfo hints, *res, *ores;
133 		struct sockaddr *sa;
134 
135 		memset(&hints, 0, sizeof(struct addrinfo));
136 		hints.ai_family = addr->sa_family;
137 		hints.ai_flags = AI_CANONNAME | AI_PASSIVE;
138 		hints.ai_socktype = SOCK_STREAM;
139 
140 		error = getaddrinfo(buf, NULL, &hints, &res);
141 		if (error) {
142 			result = HOSTNAME_INVALIDNAME;
143 			goto numeric;
144 		}
145 		for (ores = res; ; res = res->ai_next) {
146 			if (res == NULL) {
147 				freeaddrinfo(ores);
148 				result = HOSTNAME_INCORRECTNAME;
149 				goto numeric;
150 			}
151 			sa = res->ai_addr;
152 			if (sa == NULL) {
153 				freeaddrinfo(ores);
154 				result = HOSTNAME_INCORRECTNAME;
155 				goto numeric;
156 			}
157 			if (sa->sa_len == addrlen &&
158 			    sa->sa_family == addr->sa_family) {
159 				SOCKINET(sa)->si_port = SOCKINET(addr)->si_port;
160 #ifdef INET6
161 				/*
162 				 * XXX: sin6_socpe_id may not been
163 				 * filled by DNS
164 				 */
165 				if (sa->sa_family == AF_INET6 &&
166 				    SOCKADDR_IN6(sa)->sin6_scope_id == 0)
167 					SOCKADDR_IN6(sa)->sin6_scope_id =
168 					    SOCKADDR_IN6(addr)->sin6_scope_id;
169 #endif
170 				if (!memcmp(sa, addr, sa->sa_len)) {
171 					result = HOSTNAME_FOUND;
172 					if (ores->ai_canonname == NULL) {
173 						freeaddrinfo(ores);
174 						goto numeric;
175 					}
176 					strlcpy(buf, ores->ai_canonname,
177 						sizeof(buf));
178 					trimdomain(buf, hsize);
179 					if (strlen(buf) > hsize &&
180 					    addr->sa_family == AF_INET) {
181 						freeaddrinfo(ores);
182 						goto numeric;
183 					}
184 					strncpy(host, buf, hsize);
185 					break;
186 				}
187 			}
188 		}
189 		freeaddrinfo(ores);
190 	} else {
191     numeric:
192 		if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
193 				NI_NUMERICHOST) == 0)
194 			strncpy(host, buf, hsize);
195 	}
196 
197 	return result;
198 }
199 
200 
201