]> The Tcpdump Group git mirrors - tcpdump/blob - missing/getnameinfo.c
bring in latest kame getaddrinfo (couple of bug fixes).
[tcpdump] / missing / getnameinfo.c
1 /*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
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 * 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.
16 *
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
27 * SUCH DAMAGE.
28 */
29
30 /*
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().
35 * - RFC2553 says that we should raise error on short buffer. X/Open says
36 * we need to truncate the result. We obey RFC2553 (and X/Open should be
37 * modified).
38 */
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #ifndef lint
45 static const char rcsid[] =
46 "@(#) $Header: /tcpdump/master/tcpdump/missing/getnameinfo.c,v 1.5 2000-01-19 04:42:21 itojun Exp $";
47 #endif
48
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <net/if.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <arpa/nameser.h>
55 #include <netdb.h>
56 #include <resolv.h>
57 #include <string.h>
58 #include <stddef.h>
59 #include <errno.h>
60
61 #ifndef HAVE_PORTABLE_PROTOTYPE
62 #include "cdecl_ext.h"
63 #endif
64
65 #ifndef HAVE_ADDRINFO
66 #include "addrinfo.h"
67 #endif
68
69 #define SUCCESS 0
70 #define ANY 0
71 #define YES 1
72 #define NO 0
73
74 static struct afd {
75 int a_af;
76 int a_addrlen;
77 int a_socklen;
78 int a_off;
79 } afdl [] = {
80 #ifdef INET6
81 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
82 offsetof(struct sockaddr_in6, sin6_addr)},
83 #endif
84 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
85 offsetof(struct sockaddr_in, sin_addr)},
86 {0, 0, 0},
87 };
88
89 struct sockinet {
90 u_char si_len;
91 u_char si_family;
92 u_short si_port;
93 };
94
95 #define ENI_NOSOCKET 0
96 #define ENI_NOSERVNAME 1
97 #define ENI_NOHOSTNAME 2
98 #define ENI_MEMORY 3
99 #define ENI_SYSTEM 4
100 #define ENI_FAMILY 5
101 #define ENI_SALEN 6
102
103 int
104 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
105 const struct sockaddr *sa;
106 size_t salen;
107 char *host;
108 size_t hostlen;
109 char *serv;
110 size_t servlen;
111 int flags;
112 {
113 struct afd *afd;
114 struct servent *sp;
115 struct hostent *hp;
116 u_short port;
117 int family, i;
118 char *addr, *p;
119 u_int32_t v4a;
120 int h_error;
121 char numserv[512];
122 char numaddr[512];
123
124 if (sa == NULL)
125 return ENI_NOSOCKET;
126
127 #ifdef HAVE_SA_LEN /*XXX*/
128 if (sa->sa_len != salen)
129 return ENI_SALEN;
130 #endif
131
132 family = sa->sa_family;
133 for (i = 0; afdl[i].a_af; i++)
134 if (afdl[i].a_af == family) {
135 afd = &afdl[i];
136 goto found;
137 }
138 return ENI_FAMILY;
139
140 found:
141 if (salen != afd->a_socklen)
142 return ENI_SALEN;
143
144 port = ((struct sockinet *)sa)->si_port; /* network byte order */
145 addr = (char *)sa + afd->a_off;
146
147 if (serv == NULL || servlen == 0) {
148 /*
149 * do nothing in this case.
150 * in case you are wondering if "&&" is more correct than
151 * "||" here: RFC2553 says that serv == NULL OR servlen == 0
152 * means that the caller does not want the result.
153 */
154 } else {
155 if (flags & NI_NUMERICSERV)
156 sp = NULL;
157 else {
158 sp = getservbyport(port,
159 (flags & NI_DGRAM) ? "udp" : "tcp");
160 }
161 if (sp) {
162 if (strlen(sp->s_name) > servlen)
163 return ENI_MEMORY;
164 strcpy(serv, sp->s_name);
165 } else {
166 snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
167 if (strlen(numserv) > servlen)
168 return ENI_MEMORY;
169 strcpy(serv, numserv);
170 }
171 }
172
173 switch (sa->sa_family) {
174 case AF_INET:
175 v4a = (u_int32_t)
176 ntohl(((struct sockaddr_in *)sa)->sin_addr.s_addr);
177 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
178 flags |= NI_NUMERICHOST;
179 v4a >>= IN_CLASSA_NSHIFT;
180 if (v4a == 0)
181 flags |= NI_NUMERICHOST;
182 break;
183 #ifdef INET6
184 case AF_INET6:
185 {
186 struct sockaddr_in6 *sin6;
187 sin6 = (struct sockaddr_in6 *)sa;
188 switch (sin6->sin6_addr.s6_addr[0]) {
189 case 0x00:
190 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
191 ;
192 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
193 ;
194 else
195 flags |= NI_NUMERICHOST;
196 break;
197 default:
198 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
199 flags |= NI_NUMERICHOST;
200 }
201 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
202 flags |= NI_NUMERICHOST;
203 break;
204 }
205 }
206 break;
207 #endif
208 }
209 if (host == NULL || hostlen == 0) {
210 /*
211 * do nothing in this case.
212 * in case you are wondering if "&&" is more correct than
213 * "||" here: RFC2553 says that host == NULL OR hostlen == 0
214 * means that the caller does not want the result.
215 */
216 } else if (flags & NI_NUMERICHOST) {
217 /* NUMERICHOST and NAMEREQD conflicts with each other */
218 if (flags & NI_NAMEREQD)
219 return ENI_NOHOSTNAME;
220 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
221 == NULL)
222 return ENI_SYSTEM;
223 if (strlen(numaddr) > hostlen)
224 return ENI_MEMORY;
225 strcpy(host, numaddr);
226 #if defined(INET6) && defined(NI_WITHSCOPEID)
227 if (afd->a_af == AF_INET6 &&
228 (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr) ||
229 IN6_IS_ADDR_MULTICAST((struct in6_addr *)addr)) &&
230 ((struct sockaddr_in6 *)sa)->sin6_scope_id) {
231 #ifndef ALWAYS_WITHSCOPE
232 if (flags & NI_WITHSCOPEID)
233 #endif /* !ALWAYS_WITHSCOPE */
234 {
235 char *ep = strchr(host, '\0');
236 unsigned int ifindex =
237 ((struct sockaddr_in6 *)sa)->sin6_scope_id;
238
239 *ep = SCOPE_DELIMITER;
240 if ((if_indextoname(ifindex, ep + 1)) == NULL)
241 /* XXX what should we do? */
242 strncpy(ep + 1, "???", 3);
243 }
244 }
245 #endif /* INET6 */
246 } else {
247 #ifdef USE_GETIPNODEBY
248 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
249 #else
250 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
251 #ifdef HAVE_H_ERRNO
252 h_error = h_errno;
253 #else
254 h_error = EINVAL;
255 #endif
256 #endif
257
258 if (hp) {
259 if (flags & NI_NOFQDN) {
260 p = strchr(hp->h_name, '.');
261 if (p) *p = '\0';
262 }
263 if (strlen(hp->h_name) > hostlen) {
264 #ifdef USE_GETIPNODEBY
265 freehostent(hp);
266 #endif
267 return ENI_MEMORY;
268 }
269 strcpy(host, hp->h_name);
270 #ifdef USE_GETIPNODEBY
271 freehostent(hp);
272 #endif
273 } else {
274 if (flags & NI_NAMEREQD)
275 return ENI_NOHOSTNAME;
276 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
277 == NULL)
278 return ENI_NOHOSTNAME;
279 if (strlen(numaddr) > hostlen)
280 return ENI_MEMORY;
281 strcpy(host, numaddr);
282 }
283 }
284 return SUCCESS;
285 }