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 unchanged lines hidden (view full) --- 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: head/lib/libutil/realhostname.c 71753 2001-01-28 21:51:25Z brian $ |
27 */ 28 29#include <sys/param.h> 30 31#include <sys/socket.h> 32#include <netdb.h> 33#include <netinet/in.h> 34#include <arpa/inet.h> --- 12 unchanged lines hidden (view full) --- 47 u_char si_len; 48 u_char si_family; 49 u_short si_port; 50}; 51 52int 53realhostname(char *host, size_t hsize, const struct in_addr *ip) 54{ |
55 char trimmed[MAXHOSTNAMELEN+1]; |
56 int result; 57 struct hostent *hp; 58 59 result = HOSTNAME_INVALIDADDR; 60 hp = gethostbyaddr((char *)ip, sizeof(*ip), AF_INET); 61 |
62 if (hp != NULL) { 63 strlcpy(trimmed, hp->h_name, sizeof(trimmed)); 64 trimdomain(trimmed, strlen(trimmed)); 65 if (strlen(trimmed) <= hsize) { 66 char lookup[MAXHOSTNAMELEN]; |
67 |
68 strncpy(lookup, hp->h_name, sizeof(lookup) - 1); 69 lookup[sizeof(lookup) - 1] = '\0'; 70 hp = gethostbyname(lookup); 71 if (hp == NULL) 72 result = HOSTNAME_INVALIDNAME; 73 else for (; ; hp->h_addr_list++) { 74 if (*hp->h_addr_list == NULL) { 75 result = HOSTNAME_INCORRECTNAME; 76 break; 77 } 78 if (!memcmp(*hp->h_addr_list, ip, sizeof(*ip))) { 79 strncpy(host, trimmed, hsize); 80 return HOSTNAME_FOUND; 81 } |
82 } |
83 } 84 } 85 86 strncpy(host, inet_ntoa(*ip), hsize); 87 88 return result; 89} 90 --- 40 unchanged lines hidden (view full) --- 131 if (!memcmp(sa, addr, sa->sa_len)) { 132 result = HOSTNAME_FOUND; 133 ((struct sockinet *)addr)->si_port = 134 port; 135 if (ores->ai_canonname == 0) { 136 freeaddrinfo(ores); 137 goto numeric; 138 } |
139 strncpy(buf, ores->ai_canonname, 140 sizeof(buf)); 141 trimdomain(buf, hsize); 142 strncpy(host, buf, hsize); 143 if (strlen(host) > hsize && 144 addr->sa_family == AF_INET) { 145 freeaddrinfo(ores); 146 goto numeric; 147 } |
148 break; 149 } 150 ((struct sockinet *)addr)->si_port = port; 151 } 152#ifdef INET6 153 /* 154 * XXX IPv4 mapped IPv6 addr consideraton, 155 * specified in rfc2373. --- 55 unchanged lines hidden --- |