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