]>
The Tcpdump Group git mirrors - tcpdump/blob - missing/getnameinfo.c
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Issues to be discussed:
32 * - Thread safe-ness must be checked
33 * - Return values. There seems to be no standard for return value (RFC2553)
34 * but INRIA implementation returns EAI_xxx defined for getaddrinfo().
42 static const char rcsid
[] =
43 "@(#) $Header: /tcpdump/master/tcpdump/missing/getnameinfo.c,v 1.4 2000-01-09 21:35:44 fenner Exp $";
46 #include <sys/types.h>
47 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
51 #include <arpa/nameser.h>
58 #ifndef HAVE_PORTABLE_PROTOTYPE
59 #include "cdecl_ext.h"
76 {PF_INET6
, sizeof(struct in6_addr
), sizeof(struct sockaddr_in6
),
77 offsetof(struct sockaddr_in6
, sin6_addr
)},
79 {PF_INET
, sizeof(struct in_addr
), sizeof(struct sockaddr_in
),
80 offsetof(struct sockaddr_in
, sin_addr
)},
90 #define ENI_NOSOCKET 0
91 #define ENI_NOSERVNAME 1
92 #define ENI_NOHOSTNAME 2
99 getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
)
100 const struct sockaddr
*sa
;
122 #ifdef HAVE_SA_LEN /*XXX*/
123 if (sa
->sa_len
!= salen
)
127 family
= sa
->sa_family
;
128 for (i
= 0; afdl
[i
].a_af
; i
++)
129 if (afdl
[i
].a_af
== family
) {
136 if (salen
!= afd
->a_socklen
)
139 port
= ((struct sockinet
*)sa
)->si_port
; /* network byte order */
140 addr
= (char *)sa
+ afd
->a_off
;
142 if (serv
== NULL
|| servlen
== 0) {
143 /* what we should do? */
145 if (flags
& NI_NUMERICSERV
)
148 sp
= getservbyport(port
,
149 (flags
& NI_DGRAM
) ? "udp" : "tcp");
152 if (strlen(sp
->s_name
) > servlen
)
154 strcpy(serv
, sp
->s_name
);
156 sprintf(numserv
, "%d", ntohs(port
));
157 if (strlen(numserv
) > servlen
)
159 strcpy(serv
, numserv
);
163 switch (sa
->sa_family
) {
165 v4a
= ntohl(((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
);
166 if (IN_MULTICAST(v4a
) || IN_EXPERIMENTAL(v4a
))
167 flags
|= NI_NUMERICHOST
;
168 v4a
>>= IN_CLASSA_NSHIFT
;
169 if (v4a
== 0 || v4a
== IN_LOOPBACKNET
)
170 flags
|= NI_NUMERICHOST
;
175 struct sockaddr_in6
*sin6
;
176 sin6
= (struct sockaddr_in6
*)sa
;
177 switch (sin6
->sin6_addr
.s6_addr
[0]) {
179 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
181 else if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
184 flags
|= NI_NUMERICHOST
;
187 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
)) {
188 flags
|= NI_NUMERICHOST
;
190 else if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
191 flags
|= NI_NUMERICHOST
;
198 if (host
== NULL
|| hostlen
== 0) {
199 /* what should we do? */
200 } else if (flags
& NI_NUMERICHOST
) {
201 /* NUMERICHOST and NAMEREQD conflicts with each other */
202 if (flags
& NI_NAMEREQD
)
203 return ENI_NOHOSTNAME
;
204 if (inet_ntop(afd
->a_af
, addr
, numaddr
, sizeof(numaddr
))
207 if (strlen(numaddr
) > hostlen
)
209 strcpy(host
, numaddr
);
211 if (afd
->a_af
== AF_INET6
&&
212 (IN6_IS_ADDR_LINKLOCAL((struct in6_addr
*)addr
) ||
213 IN6_IS_ADDR_MULTICAST((struct in6_addr
*)addr
)) &&
214 ((struct sockaddr_in6
*)sa
)->sin6_scope_id
) {
215 #ifndef ALWAYS_WITHSCOPE
216 if (flags
& NI_WITHSCOPEID
)
217 #endif /* !ALWAYS_WITHSCOPE */
219 char *ep
= strchr(host
, '\0');
220 unsigned int ifindex
=
221 ((struct sockaddr_in6
*)sa
)->sin6_scope_id
;
223 *ep
= SCOPE_DELIMITER
;
224 if ((if_indextoname(ifindex
, ep
+ 1)) == NULL
)
225 /* XXX what should we do? */
226 strncpy(ep
+ 1, "???", 3);
231 #ifdef USE_GETIPNODEBY
232 hp
= getipnodebyaddr(addr
, afd
->a_addrlen
, afd
->a_af
, &h_error
);
234 hp
= gethostbyaddr(addr
, afd
->a_addrlen
, afd
->a_af
);
243 if (flags
& NI_NOFQDN
) {
244 p
= strchr(hp
->h_name
, '.');
247 if (strlen(hp
->h_name
) > hostlen
) {
248 #ifdef USE_GETIPNODEBY
253 strcpy(host
, hp
->h_name
);
254 #ifdef USE_GETIPNODEBY
258 if (flags
& NI_NAMEREQD
)
259 return ENI_NOHOSTNAME
;
260 if (inet_ntop(afd
->a_af
, addr
, numaddr
, sizeof(numaddr
))
262 return ENI_NOHOSTNAME
;
263 if (strlen(numaddr
) > hostlen
)
265 strcpy(host
, numaddr
);