2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
48 realhostname(char *host, size_t hsize, const struct in_addr *ip)
50 char trimmed[MAXHOSTNAMELEN];
54 result = HOSTNAME_INVALIDADDR;
55 hp = gethostbyaddr((const char *)ip, sizeof(*ip), AF_INET);
58 strlcpy(trimmed, hp->h_name, sizeof(trimmed));
59 trimdomain(trimmed, strlen(trimmed));
60 if (strlen(trimmed) <= hsize) {
61 char lookup[MAXHOSTNAMELEN];
63 strlcpy(lookup, hp->h_name, sizeof(lookup));
64 hp = gethostbyname(lookup);
66 result = HOSTNAME_INVALIDNAME;
67 else for (; ; hp->h_addr_list++) {
68 if (*hp->h_addr_list == NULL) {
69 result = HOSTNAME_INCORRECTNAME;
72 if (!memcmp(*hp->h_addr_list, ip, sizeof(*ip))) {
73 strncpy(host, trimmed, hsize);
74 return HOSTNAME_FOUND;
80 strncpy(host, inet_ntoa(*ip), hsize);
86 * struct sockaddr has very lax alignment requirements, since all its
87 * members are char or equivalent. This is a problem when trying to
88 * dereference a struct sockaddr_in6 * that was passed in as a struct
89 * sockaddr *. Although we know (or trust) that the passed-in struct was
90 * properly aligned, the compiler doesn't, and (rightly) complains. These
91 * macros perform the cast in a way that the compiler will accept.
93 #define SOCKADDR_IN6(p) ((struct sockaddr_in6 *)(void *)(p))
94 #define SOCKADDR_IN(p) ((struct sockaddr_in *)(void *)(p))
95 #define SOCKINET(p) ((struct sockinet *)(void *)(p))
98 realhostname_sa(char *host, size_t hsize, struct sockaddr *addr, int addrlen)
101 char buf[NI_MAXHOST];
103 struct sockaddr_in lsin;
106 result = HOSTNAME_INVALIDADDR;
109 /* IPv4 mapped IPv6 addr consideraton, specified in rfc2373. */
110 if (addr->sa_family == AF_INET6 &&
111 addrlen == sizeof(struct sockaddr_in6) &&
112 IN6_IS_ADDR_V4MAPPED(&SOCKADDR_IN6(addr)->sin6_addr)) {
113 struct sockaddr_in6 *sin6;
115 sin6 = SOCKADDR_IN6(addr);
117 memset(&lsin, 0, sizeof(lsin));
118 lsin.sin_len = sizeof(struct sockaddr_in);
119 lsin.sin_family = AF_INET;
120 lsin.sin_port = sin6->sin6_port;
121 memcpy(&lsin.sin_addr, &sin6->sin6_addr.s6_addr[12],
122 sizeof(struct in_addr));
123 addr = (struct sockaddr *)&lsin;
124 addrlen = lsin.sin_len;
128 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
131 struct addrinfo hints, *res, *ores;
134 memset(&hints, 0, sizeof(struct addrinfo));
135 hints.ai_family = addr->sa_family;
136 hints.ai_flags = AI_CANONNAME | AI_PASSIVE;
137 hints.ai_socktype = SOCK_STREAM;
139 error = getaddrinfo(buf, NULL, &hints, &res);
141 result = HOSTNAME_INVALIDNAME;
144 for (ores = res; ; res = res->ai_next) {
147 result = HOSTNAME_INCORRECTNAME;
153 result = HOSTNAME_INCORRECTNAME;
156 if (sa->sa_len == addrlen &&
157 sa->sa_family == addr->sa_family) {
158 SOCKINET(sa)->si_port = SOCKINET(addr)->si_port;
161 * XXX: sin6_socpe_id may not been
164 if (sa->sa_family == AF_INET6 &&
165 SOCKADDR_IN6(sa)->sin6_scope_id == 0)
166 SOCKADDR_IN6(sa)->sin6_scope_id =
167 SOCKADDR_IN6(addr)->sin6_scope_id;
169 if (!memcmp(sa, addr, sa->sa_len)) {
170 result = HOSTNAME_FOUND;
171 if (ores->ai_canonname == NULL) {
175 strlcpy(buf, ores->ai_canonname,
177 trimdomain(buf, hsize);
178 if (strlen(buf) > hsize &&
179 addr->sa_family == AF_INET) {
183 strncpy(host, buf, hsize);
191 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
192 NI_NUMERICHOST) == 0)
193 strncpy(host, buf, hsize);