2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
51 realhostname(char *host, size_t hsize, const struct in_addr *ip)
53 char trimmed[MAXHOSTNAMELEN];
57 result = HOSTNAME_INVALIDADDR;
58 hp = gethostbyaddr((const char *)ip, sizeof(*ip), AF_INET);
61 strlcpy(trimmed, hp->h_name, sizeof(trimmed));
62 trimdomain(trimmed, strlen(trimmed));
63 if (strlen(trimmed) <= hsize) {
64 char lookup[MAXHOSTNAMELEN];
66 strlcpy(lookup, hp->h_name, sizeof(lookup));
67 hp = gethostbyname(lookup);
69 result = HOSTNAME_INVALIDNAME;
70 else for (; ; hp->h_addr_list++) {
71 if (*hp->h_addr_list == NULL) {
72 result = HOSTNAME_INCORRECTNAME;
75 if (!memcmp(*hp->h_addr_list, ip, sizeof(*ip))) {
76 strncpy(host, trimmed, hsize);
77 return HOSTNAME_FOUND;
83 strncpy(host, inet_ntoa(*ip), hsize);
89 * struct sockaddr has very lax alignment requirements, since all its
90 * members are char or equivalent. This is a problem when trying to
91 * dereference a struct sockaddr_in6 * that was passed in as a struct
92 * sockaddr *. Although we know (or trust) that the passed-in struct was
93 * properly aligned, the compiler doesn't, and (rightly) complains. These
94 * macros perform the cast in a way that the compiler will accept.
96 #define SOCKADDR_IN6(p) ((struct sockaddr_in6 *)(void *)(p))
97 #define SOCKADDR_IN(p) ((struct sockaddr_in *)(void *)(p))
98 #define SOCKINET(p) ((struct sockinet *)(void *)(p))
101 realhostname_sa(char *host, size_t hsize, struct sockaddr *addr, int addrlen)
104 char buf[NI_MAXHOST];
106 struct sockaddr_in lsin;
109 result = HOSTNAME_INVALIDADDR;
112 /* IPv4 mapped IPv6 addr consideraton, specified in rfc2373. */
113 if (addr->sa_family == AF_INET6 &&
114 addrlen == sizeof(struct sockaddr_in6) &&
115 IN6_IS_ADDR_V4MAPPED(&SOCKADDR_IN6(addr)->sin6_addr)) {
116 struct sockaddr_in6 *sin6;
118 sin6 = SOCKADDR_IN6(addr);
120 memset(&lsin, 0, sizeof(lsin));
121 lsin.sin_len = sizeof(struct sockaddr_in);
122 lsin.sin_family = AF_INET;
123 lsin.sin_port = sin6->sin6_port;
124 memcpy(&lsin.sin_addr, &sin6->sin6_addr.s6_addr[12],
125 sizeof(struct in_addr));
126 addr = (struct sockaddr *)&lsin;
127 addrlen = lsin.sin_len;
131 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
134 struct addrinfo hints, *res, *ores;
137 memset(&hints, 0, sizeof(struct addrinfo));
138 hints.ai_family = addr->sa_family;
139 hints.ai_flags = AI_CANONNAME | AI_PASSIVE;
140 hints.ai_socktype = SOCK_STREAM;
142 error = getaddrinfo(buf, NULL, &hints, &res);
144 result = HOSTNAME_INVALIDNAME;
147 for (ores = res; ; res = res->ai_next) {
150 result = HOSTNAME_INCORRECTNAME;
156 result = HOSTNAME_INCORRECTNAME;
159 if (sa->sa_len == addrlen &&
160 sa->sa_family == addr->sa_family) {
161 SOCKINET(sa)->si_port = SOCKINET(addr)->si_port;
164 * XXX: sin6_socpe_id may not been
167 if (sa->sa_family == AF_INET6 &&
168 SOCKADDR_IN6(sa)->sin6_scope_id == 0)
169 SOCKADDR_IN6(sa)->sin6_scope_id =
170 SOCKADDR_IN6(addr)->sin6_scope_id;
172 if (!memcmp(sa, addr, sa->sa_len)) {
173 result = HOSTNAME_FOUND;
174 if (ores->ai_canonname == NULL) {
178 strlcpy(buf, ores->ai_canonname,
180 trimdomain(buf, hsize);
181 if (strlen(buf) > hsize &&
182 addr->sa_family == AF_INET) {
186 strncpy(host, buf, hsize);
194 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
195 NI_NUMERICHOST) == 0)
196 strncpy(host, buf, hsize);