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