realhostname.c revision 80056
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: head/lib/libutil/realhostname.c 80056 2001-07-21 00:18:54Z brian $
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
46struct sockinet {
47	u_char	si_len;
48	u_char	si_family;
49	u_short	si_port;
50};
51
52int
53realhostname(char *host, size_t hsize, const struct in_addr *ip)
54{
55	char trimmed[MAXHOSTNAMELEN];
56	int result;
57	struct hostent *hp;
58
59	result = HOSTNAME_INVALIDADDR;
60	hp = gethostbyaddr((char *)ip, sizeof(*ip), AF_INET);
61
62	if (hp != NULL) {
63		strlcpy(trimmed, hp->h_name, sizeof(trimmed));
64		trimdomain(trimmed, strlen(trimmed));
65		if (strlen(trimmed) <= hsize) {
66			char lookup[MAXHOSTNAMELEN];
67
68			strncpy(lookup, hp->h_name, sizeof(lookup) - 1);
69			lookup[sizeof(lookup) - 1] = '\0';
70			hp = gethostbyname(lookup);
71			if (hp == NULL)
72				result = HOSTNAME_INVALIDNAME;
73			else for (; ; hp->h_addr_list++) {
74				if (*hp->h_addr_list == NULL) {
75					result = HOSTNAME_INCORRECTNAME;
76					break;
77				}
78				if (!memcmp(*hp->h_addr_list, ip, sizeof(*ip))) {
79					strncpy(host, trimmed, hsize);
80					return HOSTNAME_FOUND;
81				}
82			}
83		}
84	}
85
86	strncpy(host, inet_ntoa(*ip), hsize);
87
88	return result;
89}
90
91int
92realhostname_sa(char *host, size_t hsize, struct sockaddr *addr, int addrlen)
93{
94	int result, error;
95	char buf[NI_MAXHOST];
96
97	result = HOSTNAME_INVALIDADDR;
98
99	error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, 0);
100	if (error == 0) {
101		struct addrinfo hints, *res, *ores;
102		struct sockaddr *sa;
103
104		memset(&hints, 0, sizeof(struct addrinfo));
105		switch (addr->sa_family) {
106		case AF_INET:
107		case AF_INET6:
108			hints.ai_family = addr->sa_family;
109			break;
110		default:
111			hints.ai_family = AF_UNSPEC;
112			break;
113		}
114		hints.ai_flags = AI_CANONNAME;
115
116		error = getaddrinfo(buf, NULL, &hints, &res);
117		if (error) {
118			result = HOSTNAME_INVALIDNAME;
119			goto numeric;
120		} else for (ores = res; ; res = res->ai_next) {
121			if (res == NULL) {
122				freeaddrinfo(ores);
123				result = HOSTNAME_INCORRECTNAME;
124				goto numeric;
125			}
126			sa = res->ai_addr;
127			if (sa == NULL) {
128				freeaddrinfo(ores);
129				result = HOSTNAME_INCORRECTNAME;
130				goto numeric;
131			}
132			if (sa->sa_len == addrlen &&
133			    sa->sa_family == addr->sa_family) {
134				u_int16_t port;
135
136				port = ((struct sockinet *)addr)->si_port;
137				((struct sockinet *)addr)->si_port = 0;
138				if (!memcmp(sa, addr, sa->sa_len)) {
139					result = HOSTNAME_FOUND;
140					((struct sockinet *)addr)->si_port =
141						port;
142					if (ores->ai_canonname == 0) {
143						freeaddrinfo(ores);
144						goto numeric;
145					}
146					strlcpy(buf, ores->ai_canonname,
147						sizeof(buf));
148					trimdomain(buf, hsize);
149					if (strlen(buf) > hsize &&
150					    addr->sa_family == AF_INET) {
151						freeaddrinfo(ores);
152						goto numeric;
153					}
154					strncpy(host, buf, hsize);
155					break;
156				}
157				((struct sockinet *)addr)->si_port = port;
158			}
159#ifdef INET6
160			/*
161			 * XXX IPv4 mapped IPv6 addr consideraton,
162			 * specified in rfc2373.
163			 */
164			if (sa->sa_family == AF_INET &&
165			    addr->sa_family == AF_INET6) {
166				struct in_addr *in;
167				struct in6_addr *in6;
168
169				in = &((struct sockaddr_in *)sa)->sin_addr;
170				in6 = &((struct sockaddr_in6 *)addr)->sin6_addr;
171				if (IN6_IS_ADDR_V4MAPPED(in6) &&
172				    !memcmp(&in6->s6_addr[12], in,
173					    sizeof(*in))) {
174					result = HOSTNAME_FOUND;
175					if (ores->ai_canonname == 0 ||
176					    strlen(ores->ai_canonname) > hsize) {
177						freeaddrinfo(ores);
178						goto numeric;
179					}
180					strncpy(host, ores->ai_canonname,
181						hsize);
182					break;
183				}
184			}
185#endif
186		}
187		freeaddrinfo(ores);
188	} else {
189		struct sockaddr_in sin;
190    numeric:
191#ifdef INET6
192		if (addr->sa_family == AF_INET6) {
193			struct sockaddr_in6 *sin6;
194
195			sin6 = (struct sockaddr_in6 *)addr;
196			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
197				memset(&sin, 0, sizeof(sin));
198				sin.sin_len = sizeof(struct sockaddr_in);
199				sin.sin_family = AF_INET;
200				sin.sin_port = sin6->sin6_port;
201				memcpy(&sin.sin_addr,
202				       &sin6->sin6_addr.s6_addr[12],
203				       sizeof(struct in_addr));
204				addr = (struct sockaddr *)&sin;
205				addrlen = sin.sin_len;
206			}
207		}
208#endif
209		if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
210				NI_NUMERICHOST|NI_WITHSCOPEID) == 0)
211			strncpy(host, buf, hsize);
212	}
213
214	return result;
215}
216
217
218