getnameinfo.c revision 1.33 1 1.33 itojun /* $NetBSD: getnameinfo.c,v 1.33 2001/11/15 04:38:32 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.32 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.33 itojun __RCSID("$NetBSD: getnameinfo.c,v 1.33 2001/11/15 04:38:32 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.32 bjh21 #include <net/if_dl.h>
56 1.32 bjh21 #include <net/if_ieee1394.h>
57 1.32 bjh21 #include <net/if_types.h>
58 1.1 itojun #include <netinet/in.h>
59 1.1 itojun #include <arpa/inet.h>
60 1.1 itojun #include <arpa/nameser.h>
61 1.24 lukem #include <assert.h>
62 1.32 bjh21 #include <limits.h>
63 1.1 itojun #include <netdb.h>
64 1.1 itojun #include <resolv.h>
65 1.24 lukem #include <stddef.h>
66 1.1 itojun #include <string.h>
67 1.14 itojun
68 1.14 itojun #ifdef __weak_alias
69 1.14 itojun __weak_alias(getnameinfo,_getnameinfo)
70 1.14 itojun #endif
71 1.1 itojun
72 1.25 jdolecek static const struct afd {
73 1.27 kleink int a_af;
74 1.27 kleink socklen_t a_addrlen;
75 1.27 kleink socklen_t a_socklen;
76 1.27 kleink int a_off;
77 1.1 itojun } afdl [] = {
78 1.1 itojun #ifdef INET6
79 1.1 itojun {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
80 1.1 itojun offsetof(struct sockaddr_in6, sin6_addr)},
81 1.1 itojun #endif
82 1.1 itojun {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
83 1.1 itojun offsetof(struct sockaddr_in, sin_addr)},
84 1.1 itojun {0, 0, 0},
85 1.1 itojun };
86 1.1 itojun
87 1.1 itojun struct sockinet {
88 1.1 itojun u_char si_len;
89 1.1 itojun u_char si_family;
90 1.1 itojun u_short si_port;
91 1.1 itojun };
92 1.1 itojun
93 1.32 bjh21 static int getnameinfo_inet __P((const struct sockaddr *, socklen_t, char *,
94 1.32 bjh21 size_t, char *, size_t, int));
95 1.10 itojun #ifdef INET6
96 1.16 itojun static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
97 1.16 itojun size_t, int));
98 1.16 itojun static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
99 1.18 itojun #endif
100 1.32 bjh21 static int getnameinfo_link __P((const struct sockaddr *, socklen_t, char *,
101 1.32 bjh21 size_t, char *, size_t, int));
102 1.32 bjh21 static int hexname __P((const u_int8_t *, size_t, char *, size_t));
103 1.10 itojun
104 1.32 bjh21 /*
105 1.32 bjh21 * Top-level getnameinfo() code. Look at the address family, and pick an
106 1.32 bjh21 * appropriate function to call.
107 1.32 bjh21 */
108 1.1 itojun int
109 1.1 itojun getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
110 1.1 itojun const struct sockaddr *sa;
111 1.17 itojun socklen_t salen;
112 1.32 bjh21 char *host, *serv;
113 1.32 bjh21 size_t hostlen, servlen;
114 1.32 bjh21 int flags;
115 1.32 bjh21 {
116 1.32 bjh21
117 1.32 bjh21 switch (sa->sa_family) {
118 1.32 bjh21 case AF_INET:
119 1.32 bjh21 case AF_INET6:
120 1.32 bjh21 return getnameinfo_inet(sa, salen, host, hostlen,
121 1.32 bjh21 serv, servlen, flags);
122 1.32 bjh21 case AF_LINK:
123 1.32 bjh21 return getnameinfo_link(sa, salen, host, hostlen,
124 1.32 bjh21 serv, servlen, flags);
125 1.32 bjh21 default:
126 1.32 bjh21 return EAI_FAMILY;
127 1.32 bjh21 }
128 1.32 bjh21 }
129 1.32 bjh21
130 1.32 bjh21
131 1.32 bjh21 /*
132 1.32 bjh21 * getnameinfo_inet():
133 1.32 bjh21 * Format an IPv4 or IPv6 sockaddr into a printable string.
134 1.32 bjh21 */
135 1.32 bjh21 static int
136 1.32 bjh21 getnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags)
137 1.32 bjh21 const struct sockaddr *sa;
138 1.32 bjh21 socklen_t salen;
139 1.1 itojun char *host;
140 1.1 itojun size_t hostlen;
141 1.1 itojun char *serv;
142 1.1 itojun size_t servlen;
143 1.1 itojun int flags;
144 1.1 itojun {
145 1.25 jdolecek const struct afd *afd;
146 1.1 itojun struct servent *sp;
147 1.1 itojun struct hostent *hp;
148 1.1 itojun u_short port;
149 1.5 itojun int family, i;
150 1.16 itojun const char *addr;
151 1.5 itojun u_int32_t v4a;
152 1.1 itojun char numserv[512];
153 1.1 itojun char numaddr[512];
154 1.1 itojun
155 1.24 lukem /* sa is checked below */
156 1.24 lukem /* host may be NULL */
157 1.24 lukem /* serv may be NULL */
158 1.24 lukem
159 1.1 itojun if (sa == NULL)
160 1.33 itojun return EAI_FAIL;
161 1.1 itojun
162 1.13 christos #ifdef BSD4_4
163 1.5 itojun if (sa->sa_len != salen)
164 1.33 itojun return EAI_FAIL;
165 1.13 christos #endif
166 1.33 itojun
167 1.1 itojun family = sa->sa_family;
168 1.1 itojun for (i = 0; afdl[i].a_af; i++)
169 1.1 itojun if (afdl[i].a_af == family) {
170 1.1 itojun afd = &afdl[i];
171 1.1 itojun goto found;
172 1.1 itojun }
173 1.33 itojun return EAI_FAMILY;
174 1.33 itojun
175 1.1 itojun found:
176 1.5 itojun if (salen != afd->a_socklen)
177 1.33 itojun return EAI_FAIL;
178 1.33 itojun
179 1.16 itojun /* network byte order */
180 1.20 christos port = ((const struct sockinet *)(const void *)sa)->si_port;
181 1.20 christos addr = (const char *)(const void *)sa + afd->a_off;
182 1.1 itojun
183 1.1 itojun if (serv == NULL || servlen == 0) {
184 1.5 itojun /*
185 1.5 itojun * do nothing in this case.
186 1.5 itojun * in case you are wondering if "&&" is more correct than
187 1.33 itojun * "||" here: rfc2553bis-03 says that serv == NULL OR
188 1.33 itojun * servlen == 0 means that the caller does not want the result.
189 1.5 itojun */
190 1.1 itojun } else {
191 1.5 itojun if (flags & NI_NUMERICSERV)
192 1.5 itojun sp = NULL;
193 1.5 itojun else {
194 1.5 itojun sp = getservbyport(port,
195 1.5 itojun (flags & NI_DGRAM) ? "udp" : "tcp");
196 1.5 itojun }
197 1.1 itojun if (sp) {
198 1.22 itojun if (strlen(sp->s_name) + 1 > servlen)
199 1.33 itojun return EAI_MEMORY;
200 1.1 itojun strcpy(serv, sp->s_name);
201 1.5 itojun } else {
202 1.5 itojun snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
203 1.22 itojun if (strlen(numserv) + 1 > servlen)
204 1.33 itojun return EAI_MEMORY;
205 1.5 itojun strcpy(serv, numserv);
206 1.5 itojun }
207 1.1 itojun }
208 1.1 itojun
209 1.1 itojun switch (sa->sa_family) {
210 1.1 itojun case AF_INET:
211 1.5 itojun v4a = (u_int32_t)
212 1.20 christos ntohl(((const struct sockaddr_in *)
213 1.20 christos (const void *)sa)->sin_addr.s_addr);
214 1.1 itojun if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
215 1.1 itojun flags |= NI_NUMERICHOST;
216 1.1 itojun v4a >>= IN_CLASSA_NSHIFT;
217 1.7 itojun if (v4a == 0)
218 1.1 itojun flags |= NI_NUMERICHOST;
219 1.1 itojun break;
220 1.1 itojun #ifdef INET6
221 1.1 itojun case AF_INET6:
222 1.3 itojun {
223 1.16 itojun const struct sockaddr_in6 *sin6;
224 1.20 christos sin6 = (const struct sockaddr_in6 *)(const void *)sa;
225 1.5 itojun switch (sin6->sin6_addr.s6_addr[0]) {
226 1.3 itojun case 0x00:
227 1.3 itojun if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
228 1.3 itojun ;
229 1.3 itojun else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
230 1.3 itojun ;
231 1.3 itojun else
232 1.3 itojun flags |= NI_NUMERICHOST;
233 1.3 itojun break;
234 1.3 itojun default:
235 1.5 itojun if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
236 1.3 itojun flags |= NI_NUMERICHOST;
237 1.5 itojun }
238 1.3 itojun else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
239 1.3 itojun flags |= NI_NUMERICHOST;
240 1.3 itojun break;
241 1.3 itojun }
242 1.3 itojun }
243 1.1 itojun break;
244 1.1 itojun #endif
245 1.1 itojun }
246 1.1 itojun if (host == NULL || hostlen == 0) {
247 1.5 itojun /*
248 1.5 itojun * do nothing in this case.
249 1.5 itojun * in case you are wondering if "&&" is more correct than
250 1.33 itojun * "||" here: rfc2553bis-03 says that host == NULL or
251 1.33 itojun * hostlen == 0 means that the caller does not want the result.
252 1.5 itojun */
253 1.1 itojun } else if (flags & NI_NUMERICHOST) {
254 1.10 itojun int numaddrlen;
255 1.10 itojun
256 1.3 itojun /* NUMERICHOST and NAMEREQD conflicts with each other */
257 1.3 itojun if (flags & NI_NAMEREQD)
258 1.33 itojun return EAI_NONAME;
259 1.5 itojun
260 1.16 itojun switch(afd->a_af) {
261 1.16 itojun #ifdef INET6
262 1.16 itojun case AF_INET6:
263 1.16 itojun {
264 1.16 itojun int error;
265 1.16 itojun
266 1.16 itojun if ((error = ip6_parsenumeric(sa, addr, host,
267 1.16 itojun hostlen, flags)) != 0)
268 1.16 itojun return(error);
269 1.16 itojun break;
270 1.16 itojun }
271 1.11 itojun #endif
272 1.16 itojun default:
273 1.16 itojun if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
274 1.16 itojun == NULL)
275 1.33 itojun return EAI_SYSTEM;
276 1.16 itojun numaddrlen = strlen(numaddr);
277 1.16 itojun if (numaddrlen + 1 > hostlen) /* don't forget terminator */
278 1.33 itojun return EAI_MEMORY;
279 1.16 itojun strcpy(host, numaddr);
280 1.16 itojun break;
281 1.5 itojun }
282 1.1 itojun } else {
283 1.1 itojun hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
284 1.1 itojun
285 1.1 itojun if (hp) {
286 1.16 itojun #if 0
287 1.16 itojun /*
288 1.16 itojun * commented out, since "for local host" is not
289 1.16 itojun * implemented here - see RFC2553 p30
290 1.16 itojun */
291 1.1 itojun if (flags & NI_NOFQDN) {
292 1.16 itojun char *p;
293 1.1 itojun p = strchr(hp->h_name, '.');
294 1.16 itojun if (p)
295 1.16 itojun *p = '\0';
296 1.1 itojun }
297 1.16 itojun #endif
298 1.22 itojun if (strlen(hp->h_name) + 1 > hostlen) {
299 1.33 itojun return EAI_MEMORY;
300 1.1 itojun }
301 1.1 itojun strcpy(host, hp->h_name);
302 1.1 itojun } else {
303 1.1 itojun if (flags & NI_NAMEREQD)
304 1.33 itojun return EAI_NONAME;
305 1.16 itojun switch(afd->a_af) {
306 1.16 itojun #ifdef INET6
307 1.16 itojun case AF_INET6:
308 1.16 itojun {
309 1.16 itojun int error;
310 1.16 itojun
311 1.16 itojun if ((error = ip6_parsenumeric(sa, addr, host,
312 1.16 itojun hostlen,
313 1.16 itojun flags)) != 0)
314 1.16 itojun return(error);
315 1.16 itojun break;
316 1.16 itojun }
317 1.16 itojun #endif
318 1.16 itojun default:
319 1.16 itojun if (inet_ntop(afd->a_af, addr, host,
320 1.16 itojun hostlen) == NULL)
321 1.33 itojun return EAI_SYSTEM;
322 1.16 itojun break;
323 1.16 itojun }
324 1.1 itojun }
325 1.1 itojun }
326 1.33 itojun return(0);
327 1.1 itojun }
328 1.10 itojun
329 1.10 itojun #ifdef INET6
330 1.16 itojun static int
331 1.16 itojun ip6_parsenumeric(sa, addr, host, hostlen, flags)
332 1.16 itojun const struct sockaddr *sa;
333 1.16 itojun const char *addr;
334 1.16 itojun char *host;
335 1.16 itojun size_t hostlen;
336 1.16 itojun int flags;
337 1.16 itojun {
338 1.16 itojun int numaddrlen;
339 1.16 itojun char numaddr[512];
340 1.16 itojun
341 1.24 lukem _DIAGASSERT(sa != NULL);
342 1.24 lukem _DIAGASSERT(addr != NULL);
343 1.24 lukem _DIAGASSERT(host != NULL);
344 1.24 lukem
345 1.33 itojun if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
346 1.33 itojun return EAI_SYSTEM;
347 1.16 itojun
348 1.16 itojun numaddrlen = strlen(numaddr);
349 1.16 itojun if (numaddrlen + 1 > hostlen) /* don't forget terminator */
350 1.33 itojun return EAI_MEMORY;
351 1.16 itojun strcpy(host, numaddr);
352 1.16 itojun
353 1.16 itojun #ifdef NI_WITHSCOPEID
354 1.20 christos if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
355 1.16 itojun if (flags & NI_WITHSCOPEID)
356 1.16 itojun {
357 1.16 itojun char scopebuf[MAXHOSTNAMELEN];
358 1.16 itojun int scopelen;
359 1.16 itojun
360 1.20 christos scopelen = ip6_sa2str(
361 1.20 christos (const struct sockaddr_in6 *)(const void *)sa,
362 1.20 christos scopebuf, sizeof(scopebuf), 0);
363 1.28 itojun if (scopelen < 0)
364 1.33 itojun return EAI_MEMORY;
365 1.16 itojun if (scopelen + 1 + numaddrlen + 1 > hostlen)
366 1.33 itojun return EAI_MEMORY;
367 1.16 itojun /*
368 1.16 itojun * construct <numeric-addr><delim><scopeid>
369 1.16 itojun */
370 1.16 itojun memcpy(host + numaddrlen + 1, scopebuf,
371 1.20 christos (size_t)scopelen);
372 1.16 itojun host[numaddrlen] = SCOPE_DELIMITER;
373 1.16 itojun host[numaddrlen + 1 + scopelen] = '\0';
374 1.16 itojun }
375 1.16 itojun }
376 1.16 itojun #endif /* NI_WITHSCOPEID */
377 1.16 itojun
378 1.16 itojun return 0;
379 1.16 itojun }
380 1.16 itojun
381 1.10 itojun /* ARGSUSED */
382 1.16 itojun static int
383 1.16 itojun ip6_sa2str(sa6, buf, bufsiz, flags)
384 1.16 itojun const struct sockaddr_in6 *sa6;
385 1.10 itojun char *buf;
386 1.16 itojun size_t bufsiz;
387 1.10 itojun int flags;
388 1.10 itojun {
389 1.24 lukem unsigned int ifindex;
390 1.24 lukem const struct in6_addr *a6;
391 1.28 itojun int n;
392 1.24 lukem
393 1.24 lukem _DIAGASSERT(sa6 != NULL);
394 1.24 lukem _DIAGASSERT(buf != NULL);
395 1.24 lukem
396 1.24 lukem ifindex = (unsigned int)sa6->sin6_scope_id;
397 1.24 lukem a6 = &sa6->sin6_addr;
398 1.10 itojun
399 1.28 itojun #ifdef NI_NUMERICSCOPE
400 1.28 itojun if ((flags & NI_NUMERICSCOPE) != 0) {
401 1.28 itojun n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
402 1.28 itojun if (n < 0 || n >= bufsiz)
403 1.28 itojun return -1;
404 1.28 itojun else
405 1.28 itojun return n;
406 1.10 itojun }
407 1.10 itojun #endif
408 1.18 itojun
409 1.16 itojun /* if_indextoname() does not take buffer size. not a good api... */
410 1.16 itojun if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
411 1.16 itojun bufsiz >= IF_NAMESIZE) {
412 1.16 itojun char *p = if_indextoname(ifindex, buf);
413 1.16 itojun if (p) {
414 1.16 itojun return(strlen(p));
415 1.16 itojun }
416 1.16 itojun }
417 1.10 itojun
418 1.16 itojun /* last resort */
419 1.28 itojun n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
420 1.28 itojun if (n < 0 || n >= bufsiz)
421 1.28 itojun return -1;
422 1.28 itojun else
423 1.28 itojun return n;
424 1.10 itojun }
425 1.16 itojun #endif /* INET6 */
426 1.32 bjh21
427 1.32 bjh21
428 1.32 bjh21 /*
429 1.32 bjh21 * getnameinfo_link():
430 1.32 bjh21 * Format a link-layer address into a printable format, paying attention to
431 1.32 bjh21 * the interface type.
432 1.32 bjh21 */
433 1.32 bjh21 /* ARGSUSED */
434 1.32 bjh21 static int
435 1.32 bjh21 getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
436 1.32 bjh21 char *host, size_t hostlen, char *serv, size_t servlen, int flags)
437 1.32 bjh21 {
438 1.32 bjh21 const struct sockaddr_dl *sdl =
439 1.32 bjh21 (const struct sockaddr_dl *)(const void *)sa;
440 1.32 bjh21 const struct ieee1394_hwaddr *iha;
441 1.32 bjh21 int n;
442 1.32 bjh21
443 1.32 bjh21 if (serv != NULL && servlen > 0)
444 1.32 bjh21 *serv = '\0';
445 1.32 bjh21
446 1.32 bjh21 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
447 1.32 bjh21 n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
448 1.32 bjh21 if (n > hostlen) {
449 1.32 bjh21 *host = '\0';
450 1.32 bjh21 return EAI_MEMORY;
451 1.32 bjh21 }
452 1.32 bjh21 return 0;
453 1.32 bjh21 }
454 1.32 bjh21
455 1.32 bjh21 switch (sdl->sdl_type) {
456 1.32 bjh21 #ifdef IFT_ECONET
457 1.32 bjh21 case IFT_ECONET:
458 1.32 bjh21 if (sdl->sdl_alen < 2)
459 1.32 bjh21 return EAI_FAMILY;
460 1.32 bjh21 if (LLADDR(sdl)[1] == 0)
461 1.32 bjh21 n = snprintf(host, hostlen, "%d", LLADDR(sdl)[0]);
462 1.32 bjh21 else
463 1.32 bjh21 n = snprintf(host, hostlen, "%d.%d",
464 1.32 bjh21 LLADDR(sdl)[1], LLADDR(sdl)[0]);
465 1.32 bjh21 if (n >= hostlen) {
466 1.32 bjh21 *host = '\0';
467 1.32 bjh21 return EAI_MEMORY;
468 1.32 bjh21 } else
469 1.32 bjh21 return 0;
470 1.32 bjh21 #endif
471 1.32 bjh21 case IFT_IEEE1394:
472 1.32 bjh21 if (sdl->sdl_alen < sizeof(iha->iha_uid))
473 1.32 bjh21 return EAI_FAMILY;
474 1.32 bjh21 iha =
475 1.32 bjh21 (const struct ieee1394_hwaddr *)(const void *)LLADDR(sdl);
476 1.32 bjh21 return hexname(iha->iha_uid, sizeof(iha->iha_uid),
477 1.32 bjh21 host, hostlen);
478 1.32 bjh21 /*
479 1.32 bjh21 * The following have zero-length addresses.
480 1.32 bjh21 * IFT_ATM (net/if_atmsubr.c)
481 1.32 bjh21 * IFT_FAITH (net/if_faith.c)
482 1.32 bjh21 * IFT_GIF (net/if_gif.c)
483 1.32 bjh21 * IFT_LOOP (net/if_loop.c)
484 1.32 bjh21 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
485 1.32 bjh21 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
486 1.32 bjh21 * IFT_STF (net/if_stf.c)
487 1.32 bjh21 * IFT_L2VLAN (net/if_vlan.c)
488 1.32 bjh21 * IFT_PROPVIRTUAL (net/if_bridge.h>
489 1.32 bjh21 */
490 1.32 bjh21 /*
491 1.32 bjh21 * The following use IPv4 addresses as link-layer addresses:
492 1.32 bjh21 * IFT_OTHER (net/if_gre.c)
493 1.32 bjh21 * IFT_OTHER (netinet/ip_ipip.c)
494 1.32 bjh21 */
495 1.32 bjh21 case IFT_ARCNET: /* default below is believed correct for all these. */
496 1.32 bjh21 case IFT_ETHER:
497 1.32 bjh21 case IFT_FDDI:
498 1.32 bjh21 case IFT_HIPPI:
499 1.32 bjh21 case IFT_ISO88025:
500 1.32 bjh21 default:
501 1.32 bjh21 return hexname((u_int8_t *)LLADDR(sdl), (size_t)sdl->sdl_alen,
502 1.32 bjh21 host, hostlen);
503 1.32 bjh21 }
504 1.32 bjh21 }
505 1.32 bjh21
506 1.32 bjh21 static int
507 1.32 bjh21 hexname(cp, len, host, hostlen)
508 1.32 bjh21 const u_int8_t *cp;
509 1.32 bjh21 char *host;
510 1.32 bjh21 size_t len, hostlen;
511 1.32 bjh21 {
512 1.32 bjh21 int i, n;
513 1.32 bjh21 char *outp = host;
514 1.32 bjh21
515 1.32 bjh21 *outp = '\0';
516 1.32 bjh21 for (i = 0; i < len; i++) {
517 1.32 bjh21 n = snprintf(outp, hostlen, "%s%02x",
518 1.32 bjh21 i ? ":" : "", cp[i]);
519 1.32 bjh21 if (n < 0 || n >= hostlen) {
520 1.32 bjh21 *host = '\0';
521 1.32 bjh21 return EAI_MEMORY;
522 1.32 bjh21 }
523 1.32 bjh21 outp += n;
524 1.32 bjh21 hostlen -= n;
525 1.32 bjh21 }
526 1.32 bjh21 return 0;
527 1.32 bjh21 }
528