PageRenderTime 25ms CodeModel.GetById 43ms RepoModel.GetById 11ms app.codeStats 0ms

/inet/getnameinfo.c

https://github.com/hjl-tools/glibc
C | 544 lines | 411 code | 53 blank | 80 comment | 124 complexity | 1eede9926e26fae882fee9ada8d2f4e8 MD5 | raw file
  1. /* Convert socket address to string using Name Service Switch modules.
  2. Copyright (C) 1997-2020 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 <net-internal.h>
  65. #ifndef min
  66. # define min(x,y) (((x) > (y)) ? (y) : (x))
  67. #endif /* min */
  68. libc_freeres_ptr (static char *domain);
  69. /* Former NI_IDN_ALLOW_UNASSIGNED, NI_IDN_USE_STD3_ASCII_RULES flags,
  70. now ignored. */
  71. #define DEPRECATED_NI_IDN 192
  72. static char *
  73. nrl_domainname (void)
  74. {
  75. static int not_first;
  76. if (! not_first)
  77. {
  78. __libc_lock_define_initialized (static, lock);
  79. __libc_lock_lock (lock);
  80. if (! not_first)
  81. {
  82. char *c;
  83. struct hostent *h, th;
  84. int herror;
  85. struct scratch_buffer tmpbuf;
  86. scratch_buffer_init (&tmpbuf);
  87. not_first = 1;
  88. while (__gethostbyname_r ("localhost", &th,
  89. tmpbuf.data, tmpbuf.length,
  90. &h, &herror))
  91. {
  92. if (herror == NETDB_INTERNAL && errno == ERANGE)
  93. {
  94. if (!scratch_buffer_grow (&tmpbuf))
  95. goto done;
  96. }
  97. else
  98. break;
  99. }
  100. if (h && (c = strchr (h->h_name, '.')))
  101. domain = __strdup (++c);
  102. else
  103. {
  104. /* The name contains no domain information. Use the name
  105. now to get more information. */
  106. while (__gethostname (tmpbuf.data, tmpbuf.length))
  107. if (!scratch_buffer_grow (&tmpbuf))
  108. goto done;
  109. if ((c = strchr (tmpbuf.data, '.')))
  110. domain = __strdup (++c);
  111. else
  112. {
  113. /* We need to preserve the hostname. */
  114. const char *hstname = strdupa (tmpbuf.data);
  115. while (__gethostbyname_r (hstname, &th,
  116. tmpbuf.data, tmpbuf.length,
  117. &h, &herror))
  118. {
  119. if (herror == NETDB_INTERNAL && errno == ERANGE)
  120. {
  121. if (!scratch_buffer_grow (&tmpbuf))
  122. goto done;
  123. }
  124. else
  125. break;
  126. }
  127. if (h && (c = strchr(h->h_name, '.')))
  128. domain = __strdup (++c);
  129. else
  130. {
  131. struct in_addr in_addr;
  132. in_addr.s_addr = htonl (INADDR_LOOPBACK);
  133. while (__gethostbyaddr_r ((const char *) &in_addr,
  134. sizeof (struct in_addr),
  135. AF_INET, &th,
  136. tmpbuf.data, tmpbuf.length,
  137. &h, &herror))
  138. {
  139. if (herror == NETDB_INTERNAL && errno == ERANGE)
  140. {
  141. if (!scratch_buffer_grow (&tmpbuf))
  142. goto done;
  143. }
  144. else
  145. break;
  146. }
  147. if (h && (c = strchr (h->h_name, '.')))
  148. domain = __strdup (++c);
  149. }
  150. }
  151. }
  152. done:
  153. scratch_buffer_free (&tmpbuf);
  154. }
  155. __libc_lock_unlock (lock);
  156. }
  157. return domain;
  158. };
  159. /* Copy a string to a destination buffer with length checking. Return
  160. EAI_OVERFLOW if the buffer is not large enough, and 0 on
  161. success. */
  162. static int
  163. checked_copy (char *dest, size_t destlen, const char *source)
  164. {
  165. size_t source_length = strlen (source);
  166. if (source_length + 1 > destlen)
  167. return EAI_OVERFLOW;
  168. memcpy (dest, source, source_length + 1);
  169. return 0;
  170. }
  171. /* Helper function for CHECKED_SNPRINTF below. */
  172. static int
  173. check_sprintf_result (int result, size_t destlen)
  174. {
  175. if (result < 0)
  176. return EAI_SYSTEM;
  177. if ((size_t) result >= destlen)
  178. /* If ret == destlen, there was no room for the terminating NUL
  179. character. */
  180. return EAI_OVERFLOW;
  181. return 0;
  182. }
  183. /* Format a string in the destination buffer. Return 0 on success,
  184. EAI_OVERFLOW in case the buffer is too small, or EAI_SYSTEM on any
  185. other error. */
  186. #define CHECKED_SNPRINTF(dest, destlen, format, ...) \
  187. check_sprintf_result \
  188. (__snprintf (dest, destlen, format, __VA_ARGS__), destlen)
  189. /* Convert host name, AF_INET/AF_INET6 case, name only. */
  190. static int
  191. gni_host_inet_name (struct scratch_buffer *tmpbuf,
  192. const struct sockaddr *sa, socklen_t addrlen,
  193. char *host, socklen_t hostlen, int flags)
  194. {
  195. int herrno;
  196. struct hostent th;
  197. struct hostent *h = NULL;
  198. if (sa->sa_family == AF_INET6)
  199. {
  200. const struct sockaddr_in6 *sin6p = (const struct sockaddr_in6 *) sa;
  201. while (__gethostbyaddr_r (&sin6p->sin6_addr, sizeof(struct in6_addr),
  202. AF_INET6, &th, tmpbuf->data, tmpbuf->length,
  203. &h, &herrno))
  204. if (herrno == NETDB_INTERNAL && errno == ERANGE)
  205. {
  206. if (!scratch_buffer_grow (tmpbuf))
  207. {
  208. __set_h_errno (herrno);
  209. return EAI_MEMORY;
  210. }
  211. }
  212. else
  213. break;
  214. }
  215. else
  216. {
  217. const struct sockaddr_in *sinp = (const struct sockaddr_in *) sa;
  218. while (__gethostbyaddr_r (&sinp->sin_addr, sizeof(struct in_addr),
  219. AF_INET, &th, tmpbuf->data, tmpbuf->length,
  220. &h, &herrno))
  221. if (herrno == NETDB_INTERNAL && errno == ERANGE)
  222. {
  223. if (!scratch_buffer_grow (tmpbuf))
  224. {
  225. __set_h_errno (herrno);
  226. return EAI_MEMORY;
  227. }
  228. }
  229. else
  230. break;
  231. }
  232. if (h == NULL)
  233. {
  234. if (herrno == NETDB_INTERNAL)
  235. {
  236. __set_h_errno (herrno);
  237. return EAI_SYSTEM;
  238. }
  239. if (herrno == TRY_AGAIN)
  240. {
  241. __set_h_errno (herrno);
  242. return EAI_AGAIN;
  243. }
  244. }
  245. if (h)
  246. {
  247. char *c;
  248. if ((flags & NI_NOFQDN)
  249. && (c = nrl_domainname ())
  250. && (c = strstr (h->h_name, c))
  251. && (c != h->h_name) && (*(--c) == '.'))
  252. /* Terminate the string after the prefix. */
  253. *c = '\0';
  254. /* If requested, convert from the IDN format. */
  255. bool do_idn = flags & NI_IDN;
  256. char *h_name;
  257. if (do_idn)
  258. {
  259. int rc = __idna_from_dns_encoding (h->h_name, &h_name);
  260. if (rc == EAI_IDN_ENCODE)
  261. /* Use the punycode name as a fallback. */
  262. do_idn = false;
  263. else if (rc != 0)
  264. return rc;
  265. }
  266. if (!do_idn)
  267. h_name = h->h_name;
  268. size_t len = strlen (h_name) + 1;
  269. if (len > hostlen)
  270. return EAI_OVERFLOW;
  271. memcpy (host, h_name, len);
  272. if (do_idn)
  273. free (h_name);
  274. return 0;
  275. }
  276. return EAI_NONAME;
  277. }
  278. /* Convert host name, AF_INET/AF_INET6 case, numeric conversion. */
  279. static int
  280. gni_host_inet_numeric (struct scratch_buffer *tmpbuf,
  281. const struct sockaddr *sa, socklen_t addrlen,
  282. char *host, socklen_t hostlen, int flags)
  283. {
  284. if (sa->sa_family == AF_INET6)
  285. {
  286. const struct sockaddr_in6 *sin6p = (const struct sockaddr_in6 *) sa;
  287. if (inet_ntop (AF_INET6, &sin6p->sin6_addr, host, hostlen) == NULL)
  288. return EAI_OVERFLOW;
  289. uint32_t scopeid = sin6p->sin6_scope_id;
  290. if (scopeid != 0)
  291. {
  292. size_t used_hostlen = __strnlen (host, hostlen);
  293. /* Location of the scope string in the host buffer. */
  294. char *scope_start = host + used_hostlen;
  295. size_t scope_length = hostlen - used_hostlen;
  296. if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
  297. || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr))
  298. {
  299. char scopebuf[IFNAMSIZ];
  300. if (if_indextoname (scopeid, scopebuf) != NULL)
  301. return CHECKED_SNPRINTF
  302. (scope_start, scope_length,
  303. "%c%s", SCOPE_DELIMITER, scopebuf);
  304. }
  305. return CHECKED_SNPRINTF
  306. (scope_start, scope_length, "%c%u", SCOPE_DELIMITER, scopeid);
  307. }
  308. }
  309. else
  310. {
  311. const struct sockaddr_in *sinp = (const struct sockaddr_in *) sa;
  312. if (inet_ntop (AF_INET, &sinp->sin_addr, host, hostlen) == NULL)
  313. return EAI_OVERFLOW;
  314. }
  315. return 0;
  316. }
  317. /* Convert AF_INET or AF_INET6 socket address, host part. */
  318. static int
  319. gni_host_inet (struct scratch_buffer *tmpbuf,
  320. const struct sockaddr *sa, socklen_t addrlen,
  321. char *host, socklen_t hostlen, int flags)
  322. {
  323. if (!(flags & NI_NUMERICHOST))
  324. {
  325. int result = gni_host_inet_name
  326. (tmpbuf, sa, addrlen, host, hostlen, flags);
  327. if (result != EAI_NONAME)
  328. return result;
  329. }
  330. if (flags & NI_NAMEREQD)
  331. return EAI_NONAME;
  332. else
  333. return gni_host_inet_numeric
  334. (tmpbuf, sa, addrlen, host, hostlen, flags);
  335. }
  336. /* Convert AF_LOCAL socket address, host part. */
  337. static int
  338. gni_host_local (struct scratch_buffer *tmpbuf,
  339. const struct sockaddr *sa, socklen_t addrlen,
  340. char *host, socklen_t hostlen, int flags)
  341. {
  342. if (!(flags & NI_NUMERICHOST))
  343. {
  344. struct utsname utsname;
  345. if (uname (&utsname) == 0)
  346. return checked_copy (host, hostlen, utsname.nodename);
  347. }
  348. if (flags & NI_NAMEREQD)
  349. return EAI_NONAME;
  350. return checked_copy (host, hostlen, "localhost");
  351. }
  352. /* Convert the host part of an AF_LOCAK socket address. */
  353. static int
  354. gni_host (struct scratch_buffer *tmpbuf,
  355. const struct sockaddr *sa, socklen_t addrlen,
  356. char *host, socklen_t hostlen, int flags)
  357. {
  358. switch (sa->sa_family)
  359. {
  360. case AF_INET:
  361. case AF_INET6:
  362. return gni_host_inet (tmpbuf, sa, addrlen, host, hostlen, flags);
  363. case AF_LOCAL:
  364. return gni_host_local (tmpbuf, sa, addrlen, host, hostlen, flags);
  365. default:
  366. return EAI_FAMILY;
  367. }
  368. }
  369. /* Convert service to string, AF_INET and AF_INET6 variant. */
  370. static int
  371. gni_serv_inet (struct scratch_buffer *tmpbuf,
  372. const struct sockaddr *sa, socklen_t addrlen,
  373. char *serv, socklen_t servlen, int flags)
  374. {
  375. _Static_assert
  376. (offsetof (struct sockaddr_in, sin_port)
  377. == offsetof (struct sockaddr_in6, sin6_port)
  378. && sizeof (((struct sockaddr_in) {}).sin_port) == sizeof (in_port_t)
  379. && sizeof (((struct sockaddr_in6) {}).sin6_port) == sizeof (in_port_t),
  380. "AF_INET and AF_INET6 port consistency");
  381. const struct sockaddr_in *sinp = (const struct sockaddr_in *) sa;
  382. if (!(flags & NI_NUMERICSERV))
  383. {
  384. struct servent *s, ts;
  385. int e;
  386. while ((e = __getservbyport_r (sinp->sin_port,
  387. ((flags & NI_DGRAM)
  388. ? "udp" : "tcp"), &ts,
  389. tmpbuf->data, tmpbuf->length, &s)))
  390. {
  391. if (e == ERANGE)
  392. {
  393. if (!scratch_buffer_grow (tmpbuf))
  394. return EAI_MEMORY;
  395. }
  396. else
  397. break;
  398. }
  399. if (s)
  400. return checked_copy (serv, servlen, s->s_name);
  401. /* Fall through to numeric conversion. */
  402. }
  403. return CHECKED_SNPRINTF (serv, servlen, "%d", ntohs (sinp->sin_port));
  404. }
  405. /* Convert service to string, AF_LOCAL variant. */
  406. static int
  407. gni_serv_local (struct scratch_buffer *tmpbuf,
  408. const struct sockaddr *sa, socklen_t addrlen,
  409. char *serv, socklen_t servlen, int flags)
  410. {
  411. return checked_copy
  412. (serv, servlen, ((const struct sockaddr_un *) sa)->sun_path);
  413. }
  414. /* Convert service to string, dispatching to the implementations
  415. above. */
  416. static int
  417. gni_serv (struct scratch_buffer *tmpbuf,
  418. const struct sockaddr *sa, socklen_t addrlen,
  419. char *serv, socklen_t servlen, int flags)
  420. {
  421. switch (sa->sa_family)
  422. {
  423. case AF_INET:
  424. case AF_INET6:
  425. return gni_serv_inet (tmpbuf, sa, addrlen, serv, servlen, flags);
  426. case AF_LOCAL:
  427. return gni_serv_local (tmpbuf, sa, addrlen, serv, servlen, flags);
  428. default:
  429. return EAI_FAMILY;
  430. }
  431. }
  432. int
  433. getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
  434. socklen_t hostlen, char *serv, socklen_t servlen,
  435. int flags)
  436. {
  437. if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM
  438. |NI_IDN|DEPRECATED_NI_IDN))
  439. return EAI_BADFLAGS;
  440. if (sa == NULL || addrlen < sizeof (sa_family_t))
  441. return EAI_FAMILY;
  442. if ((flags & NI_NAMEREQD) && host == NULL && serv == NULL)
  443. return EAI_NONAME;
  444. switch (sa->sa_family)
  445. {
  446. case AF_LOCAL:
  447. if (addrlen < (socklen_t) offsetof (struct sockaddr_un, sun_path))
  448. return EAI_FAMILY;
  449. break;
  450. case AF_INET:
  451. if (addrlen < sizeof (struct sockaddr_in))
  452. return EAI_FAMILY;
  453. break;
  454. case AF_INET6:
  455. if (addrlen < sizeof (struct sockaddr_in6))
  456. return EAI_FAMILY;
  457. break;
  458. default:
  459. return EAI_FAMILY;
  460. }
  461. struct scratch_buffer tmpbuf;
  462. scratch_buffer_init (&tmpbuf);
  463. if (host != NULL && hostlen > 0)
  464. {
  465. int result = gni_host (&tmpbuf, sa, addrlen, host, hostlen, flags);
  466. if (result != 0)
  467. {
  468. scratch_buffer_free (&tmpbuf);
  469. return result;
  470. }
  471. }
  472. if (serv && (servlen > 0))
  473. {
  474. int result = gni_serv (&tmpbuf, sa, addrlen, serv, servlen, flags);
  475. if (result != 0)
  476. {
  477. scratch_buffer_free (&tmpbuf);
  478. return result;
  479. }
  480. }
  481. scratch_buffer_free (&tmpbuf);
  482. return 0;
  483. }
  484. libc_hidden_def (getnameinfo)