getnameinfo.c revision 1.30 1 1.30 itojun /* $NetBSD: getnameinfo.c,v 1.30 2001/10/05 01:39:38 itojun Exp $ */
2 1.22 itojun /* $KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $ */
3 1.2 itojun
4 1.1 itojun /*
5 1.29 bjh21 * Copyright (c) 2000 Ben Harris.
6 1.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 1.1 itojun * All rights reserved.
8 1.18 itojun *
9 1.1 itojun * Redistribution and use in source and binary forms, with or without
10 1.1 itojun * modification, are permitted provided that the following conditions
11 1.1 itojun * are met:
12 1.1 itojun * 1. Redistributions of source code must retain the above copyright
13 1.1 itojun * notice, this list of conditions and the following disclaimer.
14 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 itojun * notice, this list of conditions and the following disclaimer in the
16 1.1 itojun * documentation and/or other materials provided with the distribution.
17 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
18 1.1 itojun * may be used to endorse or promote products derived from this software
19 1.1 itojun * without specific prior written permission.
20 1.18 itojun *
21 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 itojun * SUCH DAMAGE.
32 1.1 itojun */
33 1.1 itojun
34 1.1 itojun /*
35 1.1 itojun * Issues to be discussed:
36 1.1 itojun * - Thread safe-ness must be checked
37 1.5 itojun * - RFC2553 says that we should raise error on short buffer. X/Open says
38 1.5 itojun * we need to truncate the result. We obey RFC2553 (and X/Open should be
39 1.21 itojun * modified). ipngwg rough consensus seems to follow RFC2553.
40 1.16 itojun * - What is "local" in NI_FQDN?
41 1.16 itojun * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
42 1.16 itojun * - (KAME extension) NI_WITHSCOPEID when called with global address,
43 1.16 itojun * and sin6_scope_id filled
44 1.1 itojun */
45 1.15 itojun
46 1.15 itojun #include <sys/cdefs.h>
47 1.15 itojun #if defined(LIBC_SCCS) && !defined(lint)
48 1.30 itojun __RCSID("$NetBSD: getnameinfo.c,v 1.30 2001/10/05 01:39:38 itojun Exp $");
49 1.15 itojun #endif /* LIBC_SCCS and not lint */
50 1.1 itojun
51 1.14 itojun #include "namespace.h"
52 1.1 itojun #include <sys/types.h>
53 1.1 itojun #include <sys/socket.h>
54 1.5 itojun #include <net/if.h>
55 1.29 bjh21 #include <net/if_dl.h>
56 1.30 itojun #if 0
57 1.29 bjh21 #include <net/if_ieee1394.h>
58 1.30 itojun #endif
59 1.29 bjh21 #include <net/if_types.h>
60 1.1 itojun #include <netinet/in.h>
61 1.1 itojun #include <arpa/inet.h>
62 1.1 itojun #include <arpa/nameser.h>
63 1.24 lukem #include <assert.h>
64 1.1 itojun #include <netdb.h>
65 1.1 itojun #include <resolv.h>
66 1.24 lukem #include <stddef.h>
67 1.1 itojun #include <string.h>
68 1.14 itojun
69 1.14 itojun #ifdef __weak_alias
70 1.14 itojun __weak_alias(getnameinfo,_getnameinfo)
71 1.14 itojun #endif
72 1.1 itojun
73 1.1 itojun #define SUCCESS 0
74 1.1 itojun #define ANY 0
75 1.1 itojun #define YES 1
76 1.1 itojun #define NO 0
77 1.1 itojun
78 1.25 jdolecek static const struct afd {
79 1.27 kleink int a_af;
80 1.27 kleink socklen_t a_addrlen;
81 1.27 kleink socklen_t a_socklen;
82 1.27 kleink int a_off;
83 1.1 itojun } afdl [] = {
84 1.1 itojun #ifdef INET6
85 1.1 itojun {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
86 1.1 itojun offsetof(struct sockaddr_in6, sin6_addr)},
87 1.1 itojun #endif
88 1.1 itojun {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
89 1.1 itojun offsetof(struct sockaddr_in, sin_addr)},
90 1.1 itojun {0, 0, 0},
91 1.1 itojun };
92 1.1 itojun
93 1.1 itojun struct sockinet {
94 1.1 itojun u_char si_len;
95 1.1 itojun u_char si_family;
96 1.1 itojun u_short si_port;
97 1.1 itojun };
98 1.1 itojun
99 1.29 bjh21 static int getnameinfo_inet __P((const struct sockaddr *, socklen_t, char *,
100 1.29 bjh21 size_t, char *, size_t, int));
101 1.10 itojun #ifdef INET6
102 1.16 itojun static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
103 1.16 itojun size_t, int));
104 1.16 itojun static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
105 1.18 itojun #endif
106 1.29 bjh21 static int getnameinfo_link __P((const struct sockaddr *, socklen_t, char *,
107 1.29 bjh21 size_t, char *, size_t, int));
108 1.29 bjh21 static int hexname __P((u_int8_t *, size_t, char *, size_t));
109 1.10 itojun
110 1.21 itojun /* 2553bis: use EAI_xx for getnameinfo */
111 1.18 itojun #define ENI_NOSOCKET EAI_FAIL /*XXX*/
112 1.18 itojun #define ENI_NOSERVNAME EAI_NONAME
113 1.18 itojun #define ENI_NOHOSTNAME EAI_NONAME
114 1.18 itojun #define ENI_MEMORY EAI_MEMORY
115 1.18 itojun #define ENI_SYSTEM EAI_SYSTEM
116 1.18 itojun #define ENI_FAMILY EAI_FAMILY
117 1.19 itojun #define ENI_SALEN EAI_FAMILY
118 1.1 itojun
119 1.29 bjh21 /*
120 1.29 bjh21 * Top-level getnameinfo() code. Look at the address family, and pick an
121 1.29 bjh21 * appropriate function to call.
122 1.29 bjh21 */
123 1.1 itojun int
124 1.1 itojun getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
125 1.1 itojun const struct sockaddr *sa;
126 1.17 itojun socklen_t salen;
127 1.29 bjh21 char *host, *serv;
128 1.29 bjh21 size_t hostlen, servlen;
129 1.29 bjh21 int flags;
130 1.29 bjh21 {
131 1.29 bjh21
132 1.29 bjh21 switch (sa->sa_family) {
133 1.29 bjh21 case AF_INET:
134 1.29 bjh21 case AF_INET6:
135 1.29 bjh21 return getnameinfo_inet(sa, salen, host, hostlen,
136 1.29 bjh21 serv, servlen, flags);
137 1.29 bjh21 case AF_LINK:
138 1.29 bjh21 return getnameinfo_link(sa, salen, host, hostlen,
139 1.29 bjh21 serv, servlen, flags);
140 1.29 bjh21 default:
141 1.29 bjh21 return EAI_FAMILY;
142 1.29 bjh21 }
143 1.29 bjh21 }
144 1.29 bjh21
145 1.29 bjh21
146 1.29 bjh21 /*
147 1.29 bjh21 * getnameinfo_inet():
148 1.29 bjh21 * Format an IPv4 or IPv6 sockaddr into a printable string.
149 1.29 bjh21 */
150 1.29 bjh21 static int
151 1.29 bjh21 getnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags)
152 1.29 bjh21 const struct sockaddr *sa;
153 1.29 bjh21 socklen_t salen;
154 1.1 itojun char *host;
155 1.1 itojun size_t hostlen;
156 1.1 itojun char *serv;
157 1.1 itojun size_t servlen;
158 1.1 itojun int flags;
159 1.1 itojun {
160 1.25 jdolecek const struct afd *afd;
161 1.1 itojun struct servent *sp;
162 1.1 itojun struct hostent *hp;
163 1.1 itojun u_short port;
164 1.5 itojun int family, i;
165 1.16 itojun const char *addr;
166 1.5 itojun u_int32_t v4a;
167 1.1 itojun char numserv[512];
168 1.1 itojun char numaddr[512];
169 1.1 itojun
170 1.24 lukem /* sa is checked below */
171 1.24 lukem /* host may be NULL */
172 1.24 lukem /* serv may be NULL */
173 1.24 lukem
174 1.1 itojun if (sa == NULL)
175 1.1 itojun return ENI_NOSOCKET;
176 1.1 itojun
177 1.13 christos #ifdef BSD4_4
178 1.5 itojun if (sa->sa_len != salen)
179 1.5 itojun return ENI_SALEN;
180 1.13 christos #endif
181 1.1 itojun
182 1.1 itojun family = sa->sa_family;
183 1.1 itojun for (i = 0; afdl[i].a_af; i++)
184 1.1 itojun if (afdl[i].a_af == family) {
185 1.1 itojun afd = &afdl[i];
186 1.1 itojun goto found;
187 1.1 itojun }
188 1.1 itojun return ENI_FAMILY;
189 1.1 itojun
190 1.1 itojun found:
191 1.5 itojun if (salen != afd->a_socklen)
192 1.5 itojun return ENI_SALEN;
193 1.1 itojun
194 1.16 itojun /* network byte order */
195 1.20 christos port = ((const struct sockinet *)(const void *)sa)->si_port;
196 1.20 christos addr = (const char *)(const void *)sa + afd->a_off;
197 1.1 itojun
198 1.1 itojun if (serv == NULL || servlen == 0) {
199 1.5 itojun /*
200 1.5 itojun * do nothing in this case.
201 1.5 itojun * in case you are wondering if "&&" is more correct than
202 1.5 itojun * "||" here: RFC2553 says that serv == NULL OR servlen == 0
203 1.5 itojun * means that the caller does not want the result.
204 1.5 itojun */
205 1.1 itojun } else {
206 1.5 itojun if (flags & NI_NUMERICSERV)
207 1.5 itojun sp = NULL;
208 1.5 itojun else {
209 1.5 itojun sp = getservbyport(port,
210 1.5 itojun (flags & NI_DGRAM) ? "udp" : "tcp");
211 1.5 itojun }
212 1.1 itojun if (sp) {
213 1.22 itojun if (strlen(sp->s_name) + 1 > servlen)
214 1.1 itojun return ENI_MEMORY;
215 1.1 itojun strcpy(serv, sp->s_name);
216 1.5 itojun } else {
217 1.5 itojun snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
218 1.22 itojun if (strlen(numserv) + 1 > servlen)
219 1.5 itojun return ENI_MEMORY;
220 1.5 itojun strcpy(serv, numserv);
221 1.5 itojun }
222 1.1 itojun }
223 1.1 itojun
224 1.1 itojun switch (sa->sa_family) {
225 1.1 itojun case AF_INET:
226 1.5 itojun v4a = (u_int32_t)
227 1.20 christos ntohl(((const struct sockaddr_in *)
228 1.20 christos (const void *)sa)->sin_addr.s_addr);
229 1.1 itojun if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
230 1.1 itojun flags |= NI_NUMERICHOST;
231 1.1 itojun v4a >>= IN_CLASSA_NSHIFT;
232 1.7 itojun if (v4a == 0)
233 1.1 itojun flags |= NI_NUMERICHOST;
234 1.1 itojun break;
235 1.1 itojun #ifdef INET6
236 1.1 itojun case AF_INET6:
237 1.3 itojun {
238 1.16 itojun const struct sockaddr_in6 *sin6;
239 1.20 christos sin6 = (const struct sockaddr_in6 *)(const void *)sa;
240 1.5 itojun switch (sin6->sin6_addr.s6_addr[0]) {
241 1.3 itojun case 0x00:
242 1.3 itojun if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
243 1.3 itojun ;
244 1.3 itojun else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
245 1.3 itojun ;
246 1.3 itojun else
247 1.3 itojun flags |= NI_NUMERICHOST;
248 1.3 itojun break;
249 1.3 itojun default:
250 1.5 itojun if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
251 1.3 itojun flags |= NI_NUMERICHOST;
252 1.5 itojun }
253 1.3 itojun else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
254 1.3 itojun flags |= NI_NUMERICHOST;
255 1.3 itojun break;
256 1.3 itojun }
257 1.3 itojun }
258 1.1 itojun break;
259 1.1 itojun #endif
260 1.1 itojun }
261 1.1 itojun if (host == NULL || hostlen == 0) {
262 1.5 itojun /*
263 1.5 itojun * do nothing in this case.
264 1.5 itojun * in case you are wondering if "&&" is more correct than
265 1.5 itojun * "||" here: RFC2553 says that host == NULL OR hostlen == 0
266 1.5 itojun * means that the caller does not want the result.
267 1.5 itojun */
268 1.1 itojun } else if (flags & NI_NUMERICHOST) {
269 1.10 itojun int numaddrlen;
270 1.10 itojun
271 1.3 itojun /* NUMERICHOST and NAMEREQD conflicts with each other */
272 1.3 itojun if (flags & NI_NAMEREQD)
273 1.3 itojun return ENI_NOHOSTNAME;
274 1.5 itojun
275 1.16 itojun switch(afd->a_af) {
276 1.16 itojun #ifdef INET6
277 1.16 itojun case AF_INET6:
278 1.16 itojun {
279 1.16 itojun int error;
280 1.16 itojun
281 1.16 itojun if ((error = ip6_parsenumeric(sa, addr, host,
282 1.16 itojun hostlen, flags)) != 0)
283 1.16 itojun return(error);
284 1.16 itojun break;
285 1.16 itojun }
286 1.11 itojun #endif
287 1.16 itojun default:
288 1.16 itojun if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
289 1.16 itojun == NULL)
290 1.16 itojun return ENI_SYSTEM;
291 1.16 itojun numaddrlen = strlen(numaddr);
292 1.16 itojun if (numaddrlen + 1 > hostlen) /* don't forget terminator */
293 1.16 itojun return ENI_MEMORY;
294 1.16 itojun strcpy(host, numaddr);
295 1.16 itojun break;
296 1.5 itojun }
297 1.1 itojun } else {
298 1.1 itojun hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
299 1.1 itojun
300 1.1 itojun if (hp) {
301 1.16 itojun #if 0
302 1.16 itojun /*
303 1.16 itojun * commented out, since "for local host" is not
304 1.16 itojun * implemented here - see RFC2553 p30
305 1.16 itojun */
306 1.1 itojun if (flags & NI_NOFQDN) {
307 1.16 itojun char *p;
308 1.1 itojun p = strchr(hp->h_name, '.');
309 1.16 itojun if (p)
310 1.16 itojun *p = '\0';
311 1.1 itojun }
312 1.16 itojun #endif
313 1.22 itojun if (strlen(hp->h_name) + 1 > hostlen) {
314 1.1 itojun return ENI_MEMORY;
315 1.1 itojun }
316 1.1 itojun strcpy(host, hp->h_name);
317 1.1 itojun } else {
318 1.1 itojun if (flags & NI_NAMEREQD)
319 1.1 itojun return ENI_NOHOSTNAME;
320 1.16 itojun switch(afd->a_af) {
321 1.16 itojun #ifdef INET6
322 1.16 itojun case AF_INET6:
323 1.16 itojun {
324 1.16 itojun int error;
325 1.16 itojun
326 1.16 itojun if ((error = ip6_parsenumeric(sa, addr, host,
327 1.16 itojun hostlen,
328 1.16 itojun flags)) != 0)
329 1.16 itojun return(error);
330 1.16 itojun break;
331 1.16 itojun }
332 1.16 itojun #endif
333 1.16 itojun default:
334 1.16 itojun if (inet_ntop(afd->a_af, addr, host,
335 1.16 itojun hostlen) == NULL)
336 1.16 itojun return ENI_SYSTEM;
337 1.16 itojun break;
338 1.16 itojun }
339 1.1 itojun }
340 1.1 itojun }
341 1.1 itojun return SUCCESS;
342 1.1 itojun }
343 1.10 itojun
344 1.10 itojun #ifdef INET6
345 1.16 itojun static int
346 1.16 itojun ip6_parsenumeric(sa, addr, host, hostlen, flags)
347 1.16 itojun const struct sockaddr *sa;
348 1.16 itojun const char *addr;
349 1.16 itojun char *host;
350 1.16 itojun size_t hostlen;
351 1.16 itojun int flags;
352 1.16 itojun {
353 1.16 itojun int numaddrlen;
354 1.16 itojun char numaddr[512];
355 1.16 itojun
356 1.24 lukem _DIAGASSERT(sa != NULL);
357 1.24 lukem _DIAGASSERT(addr != NULL);
358 1.24 lukem _DIAGASSERT(host != NULL);
359 1.24 lukem
360 1.16 itojun if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr))
361 1.16 itojun == NULL)
362 1.16 itojun return ENI_SYSTEM;
363 1.16 itojun
364 1.16 itojun numaddrlen = strlen(numaddr);
365 1.16 itojun if (numaddrlen + 1 > hostlen) /* don't forget terminator */
366 1.16 itojun return ENI_MEMORY;
367 1.16 itojun strcpy(host, numaddr);
368 1.16 itojun
369 1.16 itojun #ifdef NI_WITHSCOPEID
370 1.20 christos if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
371 1.16 itojun if (flags & NI_WITHSCOPEID)
372 1.16 itojun {
373 1.16 itojun char scopebuf[MAXHOSTNAMELEN];
374 1.16 itojun int scopelen;
375 1.16 itojun
376 1.20 christos scopelen = ip6_sa2str(
377 1.20 christos (const struct sockaddr_in6 *)(const void *)sa,
378 1.20 christos scopebuf, sizeof(scopebuf), 0);
379 1.28 itojun if (scopelen < 0)
380 1.28 itojun return ENI_MEMORY;
381 1.16 itojun if (scopelen + 1 + numaddrlen + 1 > hostlen)
382 1.16 itojun return ENI_MEMORY;
383 1.16 itojun /*
384 1.16 itojun * construct <numeric-addr><delim><scopeid>
385 1.16 itojun */
386 1.16 itojun memcpy(host + numaddrlen + 1, scopebuf,
387 1.20 christos (size_t)scopelen);
388 1.16 itojun host[numaddrlen] = SCOPE_DELIMITER;
389 1.16 itojun host[numaddrlen + 1 + scopelen] = '\0';
390 1.16 itojun }
391 1.16 itojun }
392 1.16 itojun #endif /* NI_WITHSCOPEID */
393 1.16 itojun
394 1.16 itojun return 0;
395 1.16 itojun }
396 1.16 itojun
397 1.10 itojun /* ARGSUSED */
398 1.16 itojun static int
399 1.16 itojun ip6_sa2str(sa6, buf, bufsiz, flags)
400 1.16 itojun const struct sockaddr_in6 *sa6;
401 1.10 itojun char *buf;
402 1.16 itojun size_t bufsiz;
403 1.10 itojun int flags;
404 1.10 itojun {
405 1.24 lukem unsigned int ifindex;
406 1.24 lukem const struct in6_addr *a6;
407 1.28 itojun int n;
408 1.24 lukem
409 1.24 lukem _DIAGASSERT(sa6 != NULL);
410 1.24 lukem _DIAGASSERT(buf != NULL);
411 1.24 lukem
412 1.24 lukem ifindex = (unsigned int)sa6->sin6_scope_id;
413 1.24 lukem a6 = &sa6->sin6_addr;
414 1.10 itojun
415 1.28 itojun #ifdef NI_NUMERICSCOPE
416 1.28 itojun if ((flags & NI_NUMERICSCOPE) != 0) {
417 1.28 itojun n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
418 1.28 itojun if (n < 0 || n >= bufsiz)
419 1.28 itojun return -1;
420 1.28 itojun else
421 1.28 itojun return n;
422 1.10 itojun }
423 1.10 itojun #endif
424 1.18 itojun
425 1.16 itojun /* if_indextoname() does not take buffer size. not a good api... */
426 1.16 itojun if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
427 1.16 itojun bufsiz >= IF_NAMESIZE) {
428 1.16 itojun char *p = if_indextoname(ifindex, buf);
429 1.16 itojun if (p) {
430 1.16 itojun return(strlen(p));
431 1.16 itojun }
432 1.16 itojun }
433 1.10 itojun
434 1.16 itojun /* last resort */
435 1.28 itojun n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
436 1.28 itojun if (n < 0 || n >= bufsiz)
437 1.28 itojun return -1;
438 1.28 itojun else
439 1.28 itojun return n;
440 1.10 itojun }
441 1.16 itojun #endif /* INET6 */
442 1.29 bjh21
443 1.29 bjh21
444 1.29 bjh21 /*
445 1.29 bjh21 * getnameinfo_link():
446 1.29 bjh21 * Format a link-layer address into a printable format, paying attention to
447 1.29 bjh21 * the interface type.
448 1.29 bjh21 */
449 1.29 bjh21 static int
450 1.29 bjh21 getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
451 1.29 bjh21 char *host, size_t hostlen, char *serv, size_t servlen, int flags)
452 1.29 bjh21 {
453 1.29 bjh21 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)sa;
454 1.30 itojun #if 0
455 1.29 bjh21 struct ieee1394_hwaddr *iha;
456 1.30 itojun #endif
457 1.29 bjh21 int n;
458 1.29 bjh21
459 1.29 bjh21 if (serv != NULL && servlen > 0)
460 1.29 bjh21 *serv = '\0';
461 1.29 bjh21
462 1.29 bjh21 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
463 1.29 bjh21 n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
464 1.29 bjh21 if (n > hostlen) {
465 1.29 bjh21 *host = '\0';
466 1.29 bjh21 return EAI_MEMORY;
467 1.29 bjh21 }
468 1.29 bjh21 return 0;
469 1.29 bjh21 }
470 1.29 bjh21
471 1.29 bjh21 switch (sdl->sdl_type) {
472 1.29 bjh21 #ifdef IFT_ECONET
473 1.29 bjh21 case IFT_ECONET:
474 1.29 bjh21 if (sdl->sdl_alen < 2)
475 1.29 bjh21 return EAI_FAMILY;
476 1.29 bjh21 if (LLADDR(sdl)[1] == 0)
477 1.29 bjh21 n = snprintf(host, hostlen, "%d", LLADDR(sdl)[0]);
478 1.29 bjh21 else
479 1.29 bjh21 n = snprintf(host, hostlen, "%d.%d",
480 1.29 bjh21 LLADDR(sdl)[1], LLADDR(sdl)[0]);
481 1.29 bjh21 if (n >= hostlen) {
482 1.29 bjh21 *host = '\0';
483 1.29 bjh21 return EAI_MEMORY;
484 1.29 bjh21 } else
485 1.29 bjh21 return 0;
486 1.29 bjh21 #endif
487 1.30 itojun #if 0
488 1.29 bjh21 case IFT_IEEE1394:
489 1.29 bjh21 if (sdl->sdl_alen < sizeof(iha->iha_uid))
490 1.29 bjh21 return EAI_FAMILY;
491 1.29 bjh21 iha = (struct ieee1394_hwaddr *)LLADDR(sdl);
492 1.29 bjh21 return hexname(iha->iha_uid, sizeof(iha->iha_uid),
493 1.29 bjh21 host, hostlen);
494 1.30 itojun #endif
495 1.29 bjh21 /*
496 1.29 bjh21 * The following have zero-length addresses.
497 1.29 bjh21 * IFT_ATM (net/if_atmsubr.c)
498 1.29 bjh21 * IFT_FAITH (net/if_faith.c)
499 1.29 bjh21 * IFT_GIF (net/if_gif.c)
500 1.29 bjh21 * IFT_LOOP (net/if_loop.c)
501 1.29 bjh21 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
502 1.29 bjh21 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
503 1.29 bjh21 * IFT_STF (net/if_stf.c)
504 1.29 bjh21 * IFT_L2VLAN (net/if_vlan.c)
505 1.29 bjh21 * IFT_PROPVIRTUAL (net/if_bridge.h>
506 1.29 bjh21 */
507 1.29 bjh21 /*
508 1.29 bjh21 * The following use IPv4 addresses as link-layer addresses:
509 1.29 bjh21 * IFT_OTHER (net/if_gre.c)
510 1.29 bjh21 * IFT_OTHER (netinet/ip_ipip.c)
511 1.29 bjh21 */
512 1.29 bjh21 case IFT_ARCNET: /* default below is believed correct for all these. */
513 1.29 bjh21 case IFT_ETHER:
514 1.29 bjh21 case IFT_FDDI:
515 1.29 bjh21 case IFT_HIPPI:
516 1.29 bjh21 case IFT_ISO88025:
517 1.29 bjh21 default:
518 1.29 bjh21 return hexname(LLADDR(sdl), sdl->sdl_alen, host, hostlen);
519 1.29 bjh21 }
520 1.29 bjh21 }
521 1.29 bjh21
522 1.29 bjh21 static int
523 1.29 bjh21 hexname(cp, len, host, hostlen)
524 1.29 bjh21 u_int8_t *cp;
525 1.29 bjh21 char *host;
526 1.29 bjh21 size_t len, hostlen;
527 1.29 bjh21 {
528 1.29 bjh21 int i, n, space = hostlen;
529 1.29 bjh21 char *outp = host;
530 1.29 bjh21
531 1.29 bjh21 *outp = '\0';
532 1.29 bjh21 for (i = 0; i < len; i++) {
533 1.30 itojun n = snprintf(outp, space, "%s%02x", i ? ":" : "", cp[i]);
534 1.30 itojun if (n < 0 || n >= space) {
535 1.30 itojun *host = '\0';
536 1.30 itojun return EAI_MEMORY;
537 1.30 itojun }
538 1.29 bjh21 outp += n;
539 1.29 bjh21 space -= n;
540 1.29 bjh21 if (space <= 0) {
541 1.29 bjh21 *host = '\0';
542 1.29 bjh21 return EAI_MEMORY;
543 1.29 bjh21 }
544 1.29 bjh21 }
545 1.29 bjh21 return 0;
546 1.29 bjh21 }
547