]> git.saurik.com Git - apple/libutil.git/blob - realhostname.c
57bc6513e3b9a9d81511aeb43908e564f1605819
[apple/libutil.git] / realhostname.c
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
29 #include <sys/param.h>
30 #include <sys/socket.h>
31
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 struct sockinet {
42 u_char si_len;
43 u_char si_family;
44 u_short si_port;
45 };
46
47 int
48 realhostname(char *host, size_t hsize, const struct in_addr *ip)
49 {
50 char trimmed[MAXHOSTNAMELEN];
51 int result;
52 struct hostent *hp;
53
54 result = HOSTNAME_INVALIDADDR;
55 hp = gethostbyaddr((const char *)ip, sizeof(*ip), AF_INET);
56
57 if (hp != NULL) {
58 strlcpy(trimmed, hp->h_name, sizeof(trimmed));
59 trimdomain(trimmed, strlen(trimmed));
60 if (strlen(trimmed) <= hsize) {
61 char lookup[MAXHOSTNAMELEN];
62
63 strncpy(lookup, hp->h_name, sizeof(lookup) - 1);
64 lookup[sizeof(lookup) - 1] = '\0';
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 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 #ifdef INET6
95 /* IPv4 mapped IPv6 addr consideraton, specified in rfc2373. */
96 if (addr->sa_family == AF_INET6 &&
97 addrlen == sizeof(struct sockaddr_in6) &&
98 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr)) {
99 struct sockaddr_in6 *sin6;
100
101 sin6 = (struct sockaddr_in6 *)addr;
102
103 memset(&lsin, 0, sizeof(lsin));
104 lsin.sin_len = sizeof(struct sockaddr_in);
105 lsin.sin_family = AF_INET;
106 lsin.sin_port = sin6->sin6_port;
107 memcpy(&lsin.sin_addr, &sin6->sin6_addr.s6_addr[12],
108 sizeof(struct in_addr));
109 addr = (struct sockaddr *)&lsin;
110 addrlen = lsin.sin_len;
111 }
112 #endif
113
114 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
115 NI_NAMEREQD);
116 if (error == 0) {
117 struct addrinfo hints, *res, *ores;
118 struct sockaddr *sa;
119
120 memset(&hints, 0, sizeof(struct addrinfo));
121 hints.ai_family = addr->sa_family;
122 hints.ai_flags = AI_CANONNAME | AI_PASSIVE;
123 hints.ai_socktype = SOCK_STREAM;
124
125 error = getaddrinfo(buf, NULL, &hints, &res);
126 if (error) {
127 result = HOSTNAME_INVALIDNAME;
128 goto numeric;
129 }
130 for (ores = res; ; res = res->ai_next) {
131 if (res == NULL) {
132 freeaddrinfo(ores);
133 result = HOSTNAME_INCORRECTNAME;
134 goto numeric;
135 }
136 sa = res->ai_addr;
137 if (sa == NULL) {
138 freeaddrinfo(ores);
139 result = HOSTNAME_INCORRECTNAME;
140 goto numeric;
141 }
142 if (sa->sa_len == addrlen &&
143 sa->sa_family == addr->sa_family) {
144 ((struct sockinet *)sa)->si_port = ((struct sockinet *)addr)->si_port;
145 #ifdef INET6
146 /*
147 * XXX: sin6_socpe_id may not been
148 * filled by DNS
149 */
150 if (sa->sa_family == AF_INET6 &&
151 ((struct sockaddr_in6 *)sa)->sin6_scope_id == 0)
152 ((struct sockaddr_in6 *)sa)->sin6_scope_id = ((struct sockaddr_in6 *)addr)->sin6_scope_id;
153 #endif
154 if (!memcmp(sa, addr, sa->sa_len)) {
155 result = HOSTNAME_FOUND;
156 if (ores->ai_canonname == NULL) {
157 freeaddrinfo(ores);
158 goto numeric;
159 }
160 strlcpy(buf, ores->ai_canonname,
161 sizeof(buf));
162 trimdomain(buf, hsize);
163 if (strlen(buf) > hsize &&
164 addr->sa_family == AF_INET) {
165 freeaddrinfo(ores);
166 goto numeric;
167 }
168 strncpy(host, buf, hsize);
169 break;
170 }
171 }
172 }
173 freeaddrinfo(ores);
174 } else {
175 numeric:
176 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
177 NI_NUMERICHOST) == 0)
178 strncpy(host, buf, hsize);
179 }
180
181 return result;
182 }
183
184