getnameinfo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /* Convert socket address to string using Name Service Switch modules.
  2. Copyright (C) 1997-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* The Inner Net License, Version 2.00
  16. The author(s) grant permission for redistribution and use in source and
  17. binary forms, with or without modification, of the software and documentation
  18. provided that the following conditions are met:
  19. 0. If you receive a version of the software that is specifically labelled
  20. as not being for redistribution (check the version message and/or README),
  21. you are not permitted to redistribute that version of the software in any
  22. way or form.
  23. 1. All terms of the all other applicable copyrights and licenses must be
  24. followed.
  25. 2. Redistributions of source code must retain the authors' copyright
  26. notice(s), this list of conditions, and the following disclaimer.
  27. 3. Redistributions in binary form must reproduce the authors' copyright
  28. notice(s), this list of conditions, and the following disclaimer in the
  29. documentation and/or other materials provided with the distribution.
  30. 4. [The copyright holder has authorized the removal of this clause.]
  31. 5. Neither the name(s) of the author(s) nor the names of its contributors
  32. may be used to endorse or promote products derived from this software
  33. without specific prior written permission.
  34. THIS SOFTWARE IS PROVIDED BY ITS AUTHORS AND CONTRIBUTORS ``AS IS'' AND ANY
  35. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  36. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  37. DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY
  38. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  39. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  43. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. If these license terms cause you a real problem, contact the author. */
  45. /* This software is Copyright 1996 by Craig Metz, All Rights Reserved. */
  46. #include <errno.h>
  47. #include <netdb.h>
  48. #include <stddef.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <string.h>
  52. #include <unistd.h>
  53. #include <stdint.h>
  54. #include <arpa/inet.h>
  55. #include <net/if.h>
  56. #include <netinet/in.h>
  57. #include <sys/param.h>
  58. #include <sys/socket.h>
  59. #include <sys/types.h>
  60. #include <sys/un.h>
  61. #include <sys/utsname.h>
  62. #include <libc-lock.h>
  63. #include <scratch_buffer.h>
  64. #include <inet/net-internal.h>
  65. #include <set-freeres.h>
  66. #ifndef min
  67. # define min(x,y) (((x) > (y)) ? (y) : (x))
  68. #endif /* min */
  69. static char *domain;
  70. /* Former NI_IDN_ALLOW_UNASSIGNED, NI_IDN_USE_STD3_ASCII_RULES flags,
  71. now ignored. */
  72. #define DEPRECATED_NI_IDN 192
  73. /* Return true if no memory allocation failure happened (even if domain
  74. name could not be obtained) or false otherwise. */
  75. static bool
  76. nrl_domainname_core (struct scratch_buffer *tmpbuf)
  77. {
  78. char *c;
  79. struct hostent *h, th;
  80. int herror;
  81. while (__gethostbyname_r ("localhost", &th, tmpbuf->data, tmpbuf->length,
  82. &h, &herror))
  83. {
  84. if (herror == NETDB_INTERNAL && errno == ERANGE)
  85. {
  86. if (!scratch_buffer_grow (tmpbuf))
  87. return false;
  88. }
  89. else
  90. break;
  91. }
  92. if (h != NULL && (c = strchr (h->h_name, '.')) != NULL)
  93. {
  94. domain = __strdup (++c);
  95. return domain != NULL;
  96. }
  97. /* The name contains no domain information. Use the name
  98. now to get more information. */
  99. while (__gethostname (tmpbuf->data, tmpbuf->length))
  100. if (!scratch_buffer_grow (tmpbuf))
  101. return false;
  102. if ((c = strchr (tmpbuf->data, '.')) != NULL)
  103. {
  104. domain = __strdup (++c);
  105. return domain != NULL;
  106. }
  107. /* We need to preserve the hostname. */
  108. size_t hstnamelen = strlen (tmpbuf->data) + 1;
  109. while (__gethostbyname_r (tmpbuf->data, &th, tmpbuf->data + hstnamelen,
  110. tmpbuf->length - hstnamelen, &h, &herror))
  111. {
  112. if (herror == NETDB_INTERNAL && errno == ERANGE)
  113. {
  114. if (!scratch_buffer_grow_preserve (tmpbuf))
  115. return false;
  116. }
  117. else
  118. break;
  119. }
  120. if (h != NULL && (c = strchr(h->h_name, '.')) != NULL)
  121. {
  122. domain = __strdup (++c);
  123. return domain != NULL;
  124. }
  125. struct in_addr in_addr = { .s_addr = htonl (INADDR_LOOPBACK) };
  126. while (__gethostbyaddr_r ((const char *) &in_addr, sizeof (struct in_addr),
  127. AF_INET, &th, tmpbuf->data, tmpbuf->length, &h,
  128. &herror))
  129. {
  130. if (herror == NETDB_INTERNAL && errno == ERANGE)
  131. {
  132. if (!scratch_buffer_grow (tmpbuf))
  133. return false;
  134. }
  135. else
  136. break;
  137. }
  138. if (h != NULL && (c = strchr (h->h_name, '.')) != NULL)
  139. {
  140. domain = __strdup (++c);
  141. return domain != NULL;
  142. }
  143. return true;
  144. }
  145. static bool
  146. nrl_domainname (void)
  147. {
  148. static int not_first;
  149. if (__glibc_likely (atomic_load_acquire (&not_first) != 0))
  150. return true;
  151. int r = true;
  152. __libc_lock_define_initialized (static, lock);
  153. __libc_lock_lock (lock);
  154. if (atomic_load_relaxed (&not_first) == 0)
  155. {
  156. struct scratch_buffer tmpbuf;
  157. scratch_buffer_init (&tmpbuf);
  158. if ((r = nrl_domainname_core (&tmpbuf)))
  159. atomic_store_release (&not_first, 1);
  160. scratch_buffer_free (&tmpbuf);
  161. }
  162. __libc_lock_unlock (lock);
  163. return r;
  164. };
  165. /* Copy a string to a destination buffer with length checking. Return
  166. EAI_OVERFLOW if the buffer is not large enough, and 0 on
  167. success. */
  168. static int
  169. checked_copy (char *dest, size_t destlen, const char *source)
  170. {
  171. size_t source_length = strlen (source);
  172. if (source_length + 1 > destlen)
  173. return EAI_OVERFLOW;
  174. memcpy (dest, source, source_length + 1);
  175. return 0;
  176. }
  177. /* Helper function for CHECKED_SNPRINTF below. */
  178. static int
  179. check_sprintf_result (int result, size_t destlen)
  180. {
  181. if (result < 0)
  182. return EAI_SYSTEM;
  183. if ((size_t) result >= destlen)
  184. /* If ret == destlen, there was no room for the terminating NUL
  185. character. */
  186. return EAI_OVERFLOW;
  187. return 0;
  188. }
  189. /* Format a string in the destination buffer. Return 0 on success,
  190. EAI_OVERFLOW in case the buffer is too small, or EAI_SYSTEM on any
  191. other error. */
  192. #define CHECKED_SNPRINTF(dest, destlen, format, ...) \
  193. check_sprintf_result \
  194. (__snprintf (dest, destlen, format, __VA_ARGS__), destlen)
  195. /* Convert host name, AF_INET/AF_INET6 case, name only. */
  196. static int
  197. gni_host_inet_name (struct scratch_buffer *tmpbuf,
  198. const struct sockaddr *sa, socklen_t addrlen,
  199. char *host, socklen_t hostlen, int flags)
  200. {
  201. int herrno;
  202. struct hostent th;
  203. struct hostent *h = NULL;
  204. if (sa->sa_family == AF_INET6)
  205. {
  206. const struct sockaddr_in6 *sin6p = (const struct sockaddr_in6 *) sa;
  207. while (__gethostbyaddr_r (&sin6p->sin6_addr, sizeof(struct in6_addr),
  208. AF_INET6, &th, tmpbuf->data, tmpbuf->length,
  209. &h, &herrno))
  210. if (herrno == NETDB_INTERNAL && errno == ERANGE)
  211. {
  212. if (!scratch_buffer_grow (tmpbuf))
  213. {
  214. __set_h_errno (herrno);
  215. return EAI_MEMORY;
  216. }
  217. }
  218. else
  219. break;
  220. }
  221. else
  222. {
  223. const struct sockaddr_in *sinp = (const struct sockaddr_in *) sa;
  224. while (__gethostbyaddr_r (&sinp->sin_addr, sizeof(struct in_addr),
  225. AF_INET, &th, tmpbuf->data, tmpbuf->length,
  226. &h, &herrno))
  227. if (herrno == NETDB_INTERNAL && errno == ERANGE)
  228. {
  229. if (!scratch_buffer_grow (tmpbuf))
  230. {
  231. __set_h_errno (herrno);
  232. return EAI_MEMORY;
  233. }
  234. }
  235. else
  236. break;
  237. }
  238. if (h == NULL)
  239. {
  240. if (herrno == NETDB_INTERNAL)
  241. {
  242. __set_h_errno (herrno);
  243. return EAI_SYSTEM;
  244. }
  245. if (herrno == TRY_AGAIN)
  246. {
  247. __set_h_errno (herrno);
  248. return EAI_AGAIN;
  249. }
  250. }
  251. if (h)
  252. {
  253. if (flags & NI_NOFQDN)
  254. {
  255. if (!nrl_domainname ())
  256. return EAI_MEMORY;
  257. char *c = domain;
  258. if (c != NULL && (c = strstr (h->h_name, c))
  259. && (c != h->h_name) && (*(--c) == '.'))
  260. /* Terminate the string after the prefix. */
  261. *c = '\0';
  262. }
  263. /* If requested, convert from the IDN format. */
  264. bool do_idn = flags & NI_IDN;
  265. char *h_name;
  266. if (do_idn)
  267. {
  268. int rc = __idna_from_dns_encoding (h->h_name, &h_name);
  269. if (rc == EAI_IDN_ENCODE)
  270. /* Use the punycode name as a fallback. */
  271. do_idn = false;
  272. else if (rc != 0)
  273. return rc;
  274. }
  275. if (!do_idn)
  276. h_name = h->h_name;
  277. size_t len = strlen (h_name) + 1;
  278. if (len > hostlen)
  279. return EAI_OVERFLOW;
  280. memcpy (host, h_name, len);
  281. if (do_idn)
  282. free (h_name);
  283. return 0;
  284. }
  285. return EAI_NONAME;
  286. }
  287. /* Convert host name, AF_INET/AF_INET6 case, numeric conversion. */
  288. static int
  289. gni_host_inet_numeric (struct scratch_buffer *tmpbuf,
  290. const struct sockaddr *sa, socklen_t addrlen,
  291. char *host, socklen_t hostlen, int flags)
  292. {
  293. if (sa->sa_family == AF_INET6)
  294. {
  295. const struct sockaddr_in6 *sin6p = (const struct sockaddr_in6 *) sa;
  296. if (__inet_ntop (AF_INET6, &sin6p->sin6_addr, host, hostlen) == NULL)
  297. return EAI_OVERFLOW;
  298. uint32_t scopeid = sin6p->sin6_scope_id;
  299. if (scopeid != 0)
  300. {
  301. size_t used_hostlen = __strnlen (host, hostlen);
  302. /* Location of the scope string in the host buffer. */
  303. char *scope_start = host + used_hostlen;
  304. size_t scope_length = hostlen - used_hostlen;
  305. if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
  306. || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr))
  307. {
  308. char scopebuf[IFNAMSIZ];
  309. if (if_indextoname (scopeid, scopebuf) != NULL)
  310. return CHECKED_SNPRINTF
  311. (scope_start, scope_length,
  312. "%c%s", SCOPE_DELIMITER, scopebuf);
  313. }
  314. return CHECKED_SNPRINTF
  315. (scope_start, scope_length, "%c%u", SCOPE_DELIMITER, scopeid);
  316. }
  317. }
  318. else
  319. {
  320. const struct sockaddr_in *sinp = (const struct sockaddr_in *) sa;
  321. if (__inet_ntop (AF_INET, &sinp->sin_addr, host, hostlen) == NULL)
  322. return EAI_OVERFLOW;
  323. }
  324. return 0;
  325. }
  326. /* Convert AF_INET or AF_INET6 socket address, host part. */
  327. static int
  328. gni_host_inet (struct scratch_buffer *tmpbuf,
  329. const struct sockaddr *sa, socklen_t addrlen,
  330. char *host, socklen_t hostlen, int flags)
  331. {
  332. if (!(flags & NI_NUMERICHOST))
  333. {
  334. int result = gni_host_inet_name
  335. (tmpbuf, sa, addrlen, host, hostlen, flags);
  336. if (result != EAI_NONAME)
  337. return result;
  338. }
  339. if (flags & NI_NAMEREQD)
  340. return EAI_NONAME;
  341. else
  342. return gni_host_inet_numeric
  343. (tmpbuf, sa, addrlen, host, hostlen, flags);
  344. }
  345. /* Convert AF_LOCAL socket address, host part. */
  346. static int
  347. gni_host_local (struct scratch_buffer *tmpbuf,
  348. const struct sockaddr *sa, socklen_t addrlen,
  349. char *host, socklen_t hostlen, int flags)
  350. {
  351. if (!(flags & NI_NUMERICHOST))
  352. {
  353. struct utsname utsname;
  354. if (uname (&utsname) == 0)
  355. return checked_copy (host, hostlen, utsname.nodename);
  356. }
  357. if (flags & NI_NAMEREQD)
  358. return EAI_NONAME;
  359. return checked_copy (host, hostlen, "localhost");
  360. }
  361. /* Convert the host part of an AF_LOCAK socket address. */
  362. static int
  363. gni_host (struct scratch_buffer *tmpbuf,
  364. const struct sockaddr *sa, socklen_t addrlen,
  365. char *host, socklen_t hostlen, int flags)
  366. {
  367. switch (sa->sa_family)
  368. {
  369. case AF_INET:
  370. case AF_INET6:
  371. return gni_host_inet (tmpbuf, sa, addrlen, host, hostlen, flags);
  372. case AF_LOCAL:
  373. return gni_host_local (tmpbuf, sa, addrlen, host, hostlen, flags);
  374. default:
  375. return EAI_FAMILY;
  376. }
  377. }
  378. /* Convert service to string, AF_INET and AF_INET6 variant. */
  379. static int
  380. gni_serv_inet (struct scratch_buffer *tmpbuf,
  381. const struct sockaddr *sa, socklen_t addrlen,
  382. char *serv, socklen_t servlen, int flags)
  383. {
  384. _Static_assert
  385. (offsetof (struct sockaddr_in, sin_port)
  386. == offsetof (struct sockaddr_in6, sin6_port)
  387. && sizeof (((struct sockaddr_in) {}).sin_port) == sizeof (in_port_t)
  388. && sizeof (((struct sockaddr_in6) {}).sin6_port) == sizeof (in_port_t),
  389. "AF_INET and AF_INET6 port consistency");
  390. const struct sockaddr_in *sinp = (const struct sockaddr_in *) sa;
  391. if (!(flags & NI_NUMERICSERV))
  392. {
  393. struct servent *s, ts;
  394. int e;
  395. while ((e = __getservbyport_r (sinp->sin_port,
  396. ((flags & NI_DGRAM)
  397. ? "udp" : "tcp"), &ts,
  398. tmpbuf->data, tmpbuf->length, &s)))
  399. {
  400. if (e == ERANGE)
  401. {
  402. if (!scratch_buffer_grow (tmpbuf))
  403. return EAI_MEMORY;
  404. }
  405. else
  406. break;
  407. }
  408. if (s)
  409. return checked_copy (serv, servlen, s->s_name);
  410. /* Fall through to numeric conversion. */
  411. }
  412. return CHECKED_SNPRINTF (serv, servlen, "%d", ntohs (sinp->sin_port));
  413. }
  414. /* Convert service to string, AF_LOCAL variant. */
  415. static int
  416. gni_serv_local (struct scratch_buffer *tmpbuf,
  417. const struct sockaddr *sa, socklen_t addrlen,
  418. char *serv, socklen_t servlen, int flags)
  419. {
  420. return checked_copy
  421. (serv, servlen, ((const struct sockaddr_un *) sa)->sun_path);
  422. }
  423. /* Convert service to string, dispatching to the implementations
  424. above. */
  425. static int
  426. gni_serv (struct scratch_buffer *tmpbuf,
  427. const struct sockaddr *sa, socklen_t addrlen,
  428. char *serv, socklen_t servlen, int flags)
  429. {
  430. switch (sa->sa_family)
  431. {
  432. case AF_INET:
  433. case AF_INET6:
  434. return gni_serv_inet (tmpbuf, sa, addrlen, serv, servlen, flags);
  435. case AF_LOCAL:
  436. return gni_serv_local (tmpbuf, sa, addrlen, serv, servlen, flags);
  437. default:
  438. return EAI_FAMILY;
  439. }
  440. }
  441. int
  442. getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
  443. socklen_t hostlen, char *serv, socklen_t servlen,
  444. int flags)
  445. {
  446. if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM
  447. |NI_IDN|DEPRECATED_NI_IDN))
  448. return EAI_BADFLAGS;
  449. if (sa == NULL || addrlen < sizeof (sa_family_t))
  450. return EAI_FAMILY;
  451. if ((flags & NI_NAMEREQD) && host == NULL && serv == NULL)
  452. return EAI_NONAME;
  453. switch (sa->sa_family)
  454. {
  455. case AF_LOCAL:
  456. if (addrlen < (socklen_t) offsetof (struct sockaddr_un, sun_path))
  457. return EAI_FAMILY;
  458. break;
  459. case AF_INET:
  460. if (addrlen < sizeof (struct sockaddr_in))
  461. return EAI_FAMILY;
  462. break;
  463. case AF_INET6:
  464. if (addrlen < sizeof (struct sockaddr_in6))
  465. return EAI_FAMILY;
  466. break;
  467. default:
  468. return EAI_FAMILY;
  469. }
  470. struct scratch_buffer tmpbuf;
  471. scratch_buffer_init (&tmpbuf);
  472. if (host != NULL && hostlen > 0)
  473. {
  474. int result = gni_host (&tmpbuf, sa, addrlen, host, hostlen, flags);
  475. if (result != 0)
  476. {
  477. scratch_buffer_free (&tmpbuf);
  478. return result;
  479. }
  480. }
  481. if (serv && (servlen > 0))
  482. {
  483. int result = gni_serv (&tmpbuf, sa, addrlen, serv, servlen, flags);
  484. if (result != 0)
  485. {
  486. scratch_buffer_free (&tmpbuf);
  487. return result;
  488. }
  489. }
  490. scratch_buffer_free (&tmpbuf);
  491. return 0;
  492. }
  493. libc_hidden_def (getnameinfo)
  494. weak_alias (domain, __libc_getnameinfo_freemem_ptr)