4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1991, 1999 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
31 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/sockio.h>
36 #include <arpa/inet.h>
38 #include <netinet/in.h>
39 #include <netinet/in_systm.h>
40 #include <netinet/if_ether.h>
41 #include <netinet/ip.h>
48 * Note: If making changes to this file, check also the file
49 * cmd/cmd-inet/usr.sbin/snoop/snoop_ipaddr.c
50 * as it has the same functions there.
52 static jmp_buf nisjmp
;
54 #define MAXHASH 1024 /* must be a power of 2 */
57 struct hostdata
*h_next
;
64 struct hostdata4
*h4_next
;
68 struct in_addr h4_addr
;
72 struct hostdata6
*h6_next
;
76 struct in6_addr h6_addr
;
79 static struct hostdata
*addhost(int, void *, char *);
81 static struct hostdata4
*h_table4
[MAXHASH
];
82 static struct hostdata6
*h_table6
[MAXHASH
];
84 #define iphash(e) ((e) & (MAXHASH-1))
93 extern char *inet_ntoa();
95 static struct hostdata
*
97 struct in_addr
*ipaddr
;
99 register struct hostdata4
*h
;
100 struct hostent
*hp
= NULL
;
104 for (h
= h_table4
[iphash(ipaddr
->s_addr
)]; h
; h
= h
->h4_next
) {
105 if (h
->h4_addr
.s_addr
== ipaddr
->s_addr
)
106 return ((struct hostdata
*)h
);
109 /* not found. Put it in */
111 if (ipaddr
->s_addr
== htonl(INADDR_BROADCAST
))
112 return (addhost(AF_INET
, ipaddr
, "BROADCAST"));
113 if (ipaddr
->s_addr
== htonl(INADDR_ANY
))
114 return (addhost(AF_INET
, ipaddr
, "OLD-BROADCAST"));
117 * Set an alarm here so we don't get held up by
118 * an unresponsive name server.
119 * Give it 3 sec to do its work.
121 if (setjmp(nisjmp
) == 0) {
122 (void) signal(SIGALRM
, wakeup
);
124 hp
= getipnodebyaddr((char *)ipaddr
, sizeof (struct in_addr
),
125 AF_INET
, &error_num
);
126 if (hp
== NULL
&& inet_lnaof(*ipaddr
) == 0) {
127 np
= getnetbyaddr(inet_netof(*ipaddr
), AF_INET
);
129 return (addhost(AF_INET
, ipaddr
, np
->n_name
));
136 return (addhost(AF_INET
, ipaddr
, hp
? hp
->h_name
: inet_ntoa(*ipaddr
)));
139 static struct hostdata
*
141 struct in6_addr
*ip6addr
;
144 struct hostent
*hp
= NULL
;
146 char addrstr
[INET6_ADDRSTRLEN
];
148 struct hostdata
*retval
;
150 for (h
= h_table6
[iphash(((uint32_t *)ip6addr
)[3])]; h
;
152 if (IN6_ARE_ADDR_EQUAL(&h
->h6_addr
, ip6addr
))
153 return ((struct hostdata
*)h
);
156 /* not in the hash table, put it in */
157 if (IN6_IS_ADDR_UNSPECIFIED(ip6addr
))
158 return (addhost(AF_INET6
, ip6addr
, "UNSPECIFIED"));
161 * Set an alarm here so we don't get held up by
162 * an unresponsive name server.
163 * Give it 3 sec to do its work.
165 if (setjmp(nisjmp
) == 0) {
166 (void) signal(SIGALRM
, wakeup
);
168 hp
= getipnodebyaddr(ip6addr
, sizeof (struct in6_addr
),
169 AF_INET6
, &error_num
);
176 addname
= hp
->h_name
;
178 (void) inet_ntop(AF_INET6
, ip6addr
, addrstr
, INET6_ADDRSTRLEN
);
182 retval
= addhost(AF_INET6
, ip6addr
, addname
);
187 static struct hostdata
*
188 addhost(family
, ipaddr
, name
)
193 register struct hostdata
**hp
, *n
;
198 n
= (struct hostdata
*)malloc(sizeof (struct hostdata4
));
202 (void) memset(n
, 0, sizeof (struct hostdata4
));
203 n
->h_hostname
= strdup(name
);
204 if (n
->h_hostname
== NULL
)
207 ((struct hostdata4
*)n
)->h4_addr
= *(struct in_addr
*)ipaddr
;
208 hashval
= ((struct in_addr
*)ipaddr
)->s_addr
;
209 hp
= (struct hostdata
**)&h_table4
[iphash(hashval
)];
212 n
= (struct hostdata
*)malloc(sizeof (struct hostdata6
));
216 (void) memset(n
, 0, sizeof (struct hostdata6
));
217 n
->h_hostname
= strdup(name
);
218 if (n
->h_hostname
== NULL
)
221 (void) memcpy(&((struct hostdata6
*)n
)->h6_addr
, ipaddr
,
222 sizeof (struct in6_addr
));
223 hashval
= ((int *)ipaddr
)[3];
224 hp
= (struct hostdata
**)&h_table6
[iphash(hashval
)];
227 (void) fprintf(stderr
,
228 "nfslog: addhost ERROR: Unknown address family: %d",
239 (void) fprintf(stderr
, "addhost: no mem\n");
246 addrtoname(void *sockp
)
248 struct hostdata
*hostp
;
249 int family
= ((struct sockaddr_in
*)sockp
)->sin_family
;
253 hostp
= iplookup(&((struct sockaddr_in
*)sockp
)->sin_addr
);
256 hostp
= ip6lookup(&((struct sockaddr_in6
*)sockp
)->sin6_addr
);
259 (void) fprintf(stderr
, "nfslog: ERROR: unknown address " \
260 "family: %d\n", family
);
263 return ((hostp
!= NULL
) ? hostp
->h_hostname
: NULL
);