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