PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

lib/libc/net/getnameinfo.c

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