Temporarily remove union and nullfs from the vfs build list, they are
[dragonfly.git] / lib / libutil / 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  * $FreeBSD: src/lib/libutil/realhostname.c,v 1.6.2.6 2002/06/29 18:26:37 ume Exp $
27  * $DragonFly: src/lib/libutil/realhostname.c,v 1.2 2003/06/17 04:26:52 dillon Exp $
28  */
29
30 #include <sys/param.h>
31
32 #include <sys/socket.h>
33 #include <netdb.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36
37 #include <stdio.h>
38 #include <string.h>
39
40 #include "libutil.h"
41
42 /* wrapper for KAME-special getnameinfo() */
43 #ifndef NI_WITHSCOPEID
44 #define NI_WITHSCOPEID  0
45 #endif
46
47 struct sockinet {
48         u_char  si_len;
49         u_char  si_family;
50         u_short si_port;
51 };
52
53 int
54 realhostname(char *host, size_t hsize, const struct in_addr *ip)
55 {
56         char trimmed[MAXHOSTNAMELEN];
57         int result;
58         struct hostent *hp;
59
60         result = HOSTNAME_INVALIDADDR;
61         hp = gethostbyaddr((char *)ip, sizeof(*ip), AF_INET);
62
63         if (hp != NULL) {
64                 strlcpy(trimmed, hp->h_name, sizeof(trimmed));
65                 trimdomain(trimmed, strlen(trimmed));
66                 if (strlen(trimmed) <= hsize) {
67                         char lookup[MAXHOSTNAMELEN];
68
69                         strncpy(lookup, hp->h_name, sizeof(lookup) - 1);
70                         lookup[sizeof(lookup) - 1] = '\0';
71                         hp = gethostbyname(lookup);
72                         if (hp == NULL)
73                                 result = HOSTNAME_INVALIDNAME;
74                         else for (; ; hp->h_addr_list++) {
75                                 if (*hp->h_addr_list == NULL) {
76                                         result = HOSTNAME_INCORRECTNAME;
77                                         break;
78                                 }
79                                 if (!memcmp(*hp->h_addr_list, ip, sizeof(*ip))) {
80                                         strncpy(host, trimmed, hsize);
81                                         return HOSTNAME_FOUND;
82                                 }
83                         }
84                 }
85         }
86
87         strncpy(host, inet_ntoa(*ip), hsize);
88
89         return result;
90 }
91
92 int
93 realhostname_sa(char *host, size_t hsize, struct sockaddr *addr, int addrlen)
94 {
95         int result, error;
96         char buf[NI_MAXHOST];
97         struct sockaddr_in sin;
98
99         result = HOSTNAME_INVALIDADDR;
100
101 #ifdef INET6
102         /* IPv4 mapped IPv6 addr consideraton, specified in rfc2373. */
103         if (addr->sa_family == AF_INET6 &&
104             addrlen == sizeof(struct sockaddr_in6) &&
105             IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr)) {
106                 struct sockaddr_in6 *sin6;
107
108                 sin6 = (struct sockaddr_in6 *)addr;
109
110                 memset(&sin, 0, sizeof(sin));
111                 sin.sin_len = sizeof(struct sockaddr_in);
112                 sin.sin_family = AF_INET;
113                 sin.sin_port = sin6->sin6_port;
114                 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
115                        sizeof(struct in_addr));
116                 addr = (struct sockaddr *)&sin;
117                 addrlen = sin.sin_len;
118         }
119 #endif
120
121         error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
122                             NI_WITHSCOPEID | NI_NAMEREQD);
123         if (error == 0) {
124                 struct addrinfo hints, *res, *ores;
125                 struct sockaddr *sa;
126
127                 memset(&hints, 0, sizeof(struct addrinfo));
128                 hints.ai_family = addr->sa_family;
129                 hints.ai_flags = AI_CANONNAME | AI_PASSIVE;
130                 hints.ai_socktype = SOCK_STREAM;
131
132                 error = getaddrinfo(buf, NULL, &hints, &res);
133                 if (error) {
134                         result = HOSTNAME_INVALIDNAME;
135                         goto numeric;
136                 }
137                 for (ores = res; ; res = res->ai_next) {
138                         if (res == NULL) {
139                                 freeaddrinfo(ores);
140                                 result = HOSTNAME_INCORRECTNAME;
141                                 goto numeric;
142                         }
143                         sa = res->ai_addr;
144                         if (sa == NULL) {
145                                 freeaddrinfo(ores);
146                                 result = HOSTNAME_INCORRECTNAME;
147                                 goto numeric;
148                         }
149                         if (sa->sa_len == addrlen &&
150                             sa->sa_family == addr->sa_family) {
151                                 ((struct sockinet *)sa)->si_port = ((struct sockinet *)addr)->si_port;
152 #ifdef INET6
153                                 /*
154                                  * XXX: sin6_socpe_id may not been
155                                  * filled by DNS
156                                  */
157                                 if (sa->sa_family == AF_INET6 &&
158                                     ((struct sockaddr_in6 *)sa)->sin6_scope_id == 0)
159                                         ((struct sockaddr_in6 *)sa)->sin6_scope_id = ((struct sockaddr_in6 *)addr)->sin6_scope_id;
160 #endif
161                                 if (!memcmp(sa, addr, sa->sa_len)) {
162                                         result = HOSTNAME_FOUND;
163                                         if (ores->ai_canonname == NULL) {
164                                                 freeaddrinfo(ores);
165                                                 goto numeric;
166                                         }
167                                         strlcpy(buf, ores->ai_canonname,
168                                                 sizeof(buf));
169                                         trimdomain(buf, hsize);
170                                         if (strlen(buf) > hsize &&
171                                             addr->sa_family == AF_INET) {
172                                                 freeaddrinfo(ores);
173                                                 goto numeric;
174                                         }
175                                         strncpy(host, buf, hsize);
176                                         break;
177                                 }
178                         }
179                 }
180                 freeaddrinfo(ores);
181         } else {
182     numeric:
183                 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
184                                 NI_NUMERICHOST|NI_WITHSCOPEID) == 0)
185                         strncpy(host, buf, hsize);
186         }
187
188         return result;
189 }
190
191