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