Deleted Added
full compact
realhostname.c (63164) realhostname.c (71753)
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 *
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 63164 2000-07-14 18:08:19Z ume $
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{
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];
55 int result;
56 struct hostent *hp;
57
58 result = HOSTNAME_INVALIDADDR;
59 hp = gethostbyaddr((char *)ip, sizeof(*ip), AF_INET);
60
56 int result;
57 struct hostent *hp;
58
59 result = HOSTNAME_INVALIDADDR;
60 hp = gethostbyaddr((char *)ip, sizeof(*ip), AF_INET);
61
61 if (hp != NULL && strlen(hp->h_name) <= hsize) {
62 char lookup[MAXHOSTNAMELEN];
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];
63
67
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[0] == NULL) {
71 result = HOSTNAME_INCORRECTNAME;
72 break;
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 }
73 }
82 }
74 if (!memcmp(*hp->h_addr_list, ip, sizeof(*ip))) {
75 strncpy(host, lookup, hsize);
76 return HOSTNAME_FOUND;
77 }
78 }
79 }
80
81 strncpy(host, inet_ntoa(*ip), hsize);
82
83 return result;
84}
85

--- 40 unchanged lines hidden (view full) ---

126 if (!memcmp(sa, addr, sa->sa_len)) {
127 result = HOSTNAME_FOUND;
128 ((struct sockinet *)addr)->si_port =
129 port;
130 if (ores->ai_canonname == 0) {
131 freeaddrinfo(ores);
132 goto numeric;
133 }
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 }
134 if (strlen(ores->ai_canonname) > hsize) {
135 if (addr->sa_family == AF_INET) {
136 freeaddrinfo(ores);
137 goto numeric;
138 }
139 strncpy(buf,
140 ores->ai_canonname,
141 sizeof(buf));
142 trimdomain(buf, hsize);
143 strncpy(host, buf, hsize);
144 } else
145 strncpy(host,
146 ores->ai_canonname,
147 hsize);
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 ---
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 ---