tst-res_use_inet6.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* Basic functionality tests for inet6 option processing.
  2. Copyright (C) 2016-2019 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. <http://www.gnu.org/licenses/>. */
  15. #include <ctype.h>
  16. #include <netdb.h>
  17. #include <resolv.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <support/check.h>
  22. #include <support/check_nss.h>
  23. #include <support/resolv_test.h>
  24. #include <support/support.h>
  25. #include <support/xthread.h>
  26. /* Handle IPv4 reverse lookup responses. Product a PTR record
  27. A-B-C-D.v4.example. */
  28. static void
  29. response_ptr_v4 (const struct resolv_response_context *ctx,
  30. struct resolv_response_builder *b,
  31. const char *qname, uint16_t qclass, uint16_t qtype)
  32. {
  33. int bytes[4];
  34. int offset = -1;
  35. TEST_VERIFY (sscanf (qname, "%d.%d.%d.%d.in-addr.arpa%n",
  36. bytes + 0, bytes + 1, bytes + 2, bytes + 3,
  37. &offset) == 4);
  38. TEST_VERIFY (offset == strlen (qname));
  39. resolv_response_init (b, (struct resolv_response_flags) {});
  40. resolv_response_add_question (b, qname, qclass, qtype);
  41. resolv_response_section (b, ns_s_an);
  42. resolv_response_open_record (b, qname, qclass, T_PTR, 0);
  43. char *name = xasprintf ("%d-%d-%d-%d.v4.example",
  44. bytes[3], bytes[2], bytes[1], bytes[0]);
  45. resolv_response_add_name (b, name);
  46. free (name);
  47. resolv_response_close_record (b);
  48. }
  49. /* Handle IPv6 reverse lookup responses. Produce a PTR record
  50. <32 hex digits>.v6.example. */
  51. static void
  52. response_ptr_v6 (const struct resolv_response_context *ctx,
  53. struct resolv_response_builder *b,
  54. const char *qname, uint16_t qclass, uint16_t qtype)
  55. {
  56. TEST_VERIFY_EXIT (strlen (qname) > 64);
  57. char bytes[33];
  58. for (int i = 0; i < 64; ++i)
  59. if ((i % 2) == 0)
  60. {
  61. TEST_VERIFY (isxdigit ((unsigned char) qname[i]));
  62. bytes[31 - i / 2] = qname[i];
  63. }
  64. else
  65. TEST_VERIFY_EXIT (qname[i] == '.');
  66. bytes[32] = '\0';
  67. resolv_response_init (b, (struct resolv_response_flags) {});
  68. resolv_response_add_question (b, qname, qclass, qtype);
  69. resolv_response_section (b, ns_s_an);
  70. resolv_response_open_record (b, qname, qclass, T_PTR, 0);
  71. char *name = xasprintf ("%s.v6.example", bytes);
  72. resolv_response_add_name (b, name);
  73. free (name);
  74. resolv_response_close_record (b);
  75. }
  76. /* Produce a response based on QNAME: Certain characters in the first
  77. label of QNAME trigger the inclusion of resource records:
  78. 'a' A record (IPv4 address)
  79. 'q' AAAA record (quad A record, IPv6 address)
  80. 'p' PTR record
  81. 'm' record type must match QTYPE (no additional records)
  82. '6' stop flag processing if QTYPE == AAAA
  83. For 'a' and 'q', QTYPE is ignored for record type selection if 'm'
  84. is not specified.
  85. in-addr.arpa and ip6.arpa queries are handled separately in
  86. response_ptr_v4 and response_ptr_v6. */
  87. static void
  88. response (const struct resolv_response_context *ctx,
  89. struct resolv_response_builder *b,
  90. const char *qname, uint16_t qclass, uint16_t qtype)
  91. {
  92. if (strstr (qname, ".in-addr.arpa") != NULL)
  93. return response_ptr_v4 (ctx, b, qname, qclass, qtype);
  94. else if (strstr (qname, ".ip6.arpa") != NULL)
  95. return response_ptr_v6 (ctx, b, qname, qclass, qtype);
  96. bool include_a = false;
  97. bool include_aaaa = false;
  98. bool include_match = false;
  99. bool include_ptr = false;
  100. for (const char *p = qname; *p != '.' && *p != '\0'; ++p)
  101. {
  102. if (*p == 'a')
  103. include_a = true;
  104. else if (*p == 'q')
  105. include_aaaa = true;
  106. else if (*p == 'm')
  107. include_match = true;
  108. else if (*p == 'p')
  109. include_ptr = true;
  110. else if (*p == '6' && qtype == T_AAAA)
  111. break;
  112. }
  113. if (include_match)
  114. {
  115. if (qtype == T_A)
  116. include_aaaa = false;
  117. else if (qtype == T_AAAA)
  118. include_a = false;
  119. }
  120. resolv_response_init (b, (struct resolv_response_flags) {});
  121. resolv_response_add_question (b, qname, qclass, qtype);
  122. resolv_response_section (b, ns_s_an);
  123. if (include_a)
  124. {
  125. char ipv4[4] = {192, 0, 2, 17};
  126. resolv_response_open_record (b, qname, qclass, T_A, 0);
  127. resolv_response_add_data (b, &ipv4, sizeof (ipv4));
  128. resolv_response_close_record (b);
  129. }
  130. if (include_aaaa)
  131. {
  132. char ipv6[16]
  133. = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
  134. resolv_response_open_record (b, qname, qclass, T_AAAA, 0);
  135. resolv_response_add_data (b, &ipv6, sizeof (ipv6));
  136. resolv_response_close_record (b);
  137. }
  138. if (include_ptr)
  139. {
  140. resolv_response_open_record (b, qname, qclass, T_PTR, 0);
  141. resolv_response_add_name (b, "ptr-target.example");
  142. resolv_response_close_record (b);
  143. }
  144. }
  145. /* Test that getaddrinfo is not influenced by RES_USE_INET6. */
  146. static void
  147. test_gai (void)
  148. {
  149. {
  150. struct addrinfo hints =
  151. {
  152. .ai_family = AF_UNSPEC,
  153. .ai_socktype = SOCK_STREAM,
  154. .ai_protocol = IPPROTO_TCP,
  155. };
  156. struct addrinfo *ai;
  157. int ret = getaddrinfo ("qam.example", "80", &hints, &ai);
  158. check_addrinfo ("getaddrinfo AF_UNSPEC qam.example", ai, ret,
  159. "address: STREAM/TCP 192.0.2.17 80\n"
  160. "address: STREAM/TCP 2001:db8::1 80\n");
  161. if (ret == 0)
  162. freeaddrinfo (ai);
  163. ret = getaddrinfo ("am.example", "80", &hints, &ai);
  164. check_addrinfo ("getaddrinfo AF_UNSPEC am.example", ai, ret,
  165. "address: STREAM/TCP 192.0.2.17 80\n");
  166. if (ret == 0)
  167. freeaddrinfo (ai);
  168. ret = getaddrinfo ("qa.example", "80", &hints, &ai);
  169. /* Combined A/AAAA responses currently result in address
  170. duplication. */
  171. check_addrinfo ("getaddrinfo AF_UNSPEC qa.example", ai, ret,
  172. "address: STREAM/TCP 192.0.2.17 80\n"
  173. "address: STREAM/TCP 192.0.2.17 80\n"
  174. "address: STREAM/TCP 2001:db8::1 80\n"
  175. "address: STREAM/TCP 2001:db8::1 80\n");
  176. if (ret == 0)
  177. freeaddrinfo (ai);
  178. }
  179. {
  180. struct addrinfo hints =
  181. {
  182. .ai_family = AF_INET,
  183. .ai_socktype = SOCK_STREAM,
  184. .ai_protocol = IPPROTO_TCP,
  185. };
  186. struct addrinfo *ai;
  187. int ret = getaddrinfo ("qam.example", "80", &hints, &ai);
  188. check_addrinfo ("getaddrinfo AF_INET qam.example", ai, ret,
  189. "address: STREAM/TCP 192.0.2.17 80\n");
  190. if (ret == 0)
  191. freeaddrinfo (ai);
  192. ret = getaddrinfo ("am.example", "80", &hints, &ai);
  193. check_addrinfo ("getaddrinfo AF_INET am.example", ai, ret,
  194. "address: STREAM/TCP 192.0.2.17 80\n");
  195. if (ret == 0)
  196. freeaddrinfo (ai);
  197. ret = getaddrinfo ("qa.example", "80", &hints, &ai);
  198. check_addrinfo ("getaddrinfo AF_INET qa.example", ai, ret,
  199. "address: STREAM/TCP 192.0.2.17 80\n");
  200. if (ret == 0)
  201. freeaddrinfo (ai);
  202. }
  203. {
  204. struct addrinfo hints =
  205. {
  206. .ai_family = AF_INET6,
  207. .ai_socktype = SOCK_STREAM,
  208. .ai_protocol = IPPROTO_TCP,
  209. };
  210. struct addrinfo *ai;
  211. int ret = getaddrinfo ("qa.example", "80", &hints, &ai);
  212. check_addrinfo ("getaddrinfo (AF_INET6)", ai, ret,
  213. "address: STREAM/TCP 2001:db8::1 80\n");
  214. if (ret == 0)
  215. freeaddrinfo (ai);
  216. ret = getaddrinfo ("am.example", "80", &hints, &ai);
  217. check_addrinfo ("getaddrinfo AF_INET6 am.example", ai, ret,
  218. "error: No address associated with hostname\n");
  219. if (ret == 0)
  220. freeaddrinfo (ai);
  221. ret = getaddrinfo ("qam.example", "80", &hints, &ai);
  222. check_addrinfo ("getaddrinfo AF_INET6 qam.example", ai, ret,
  223. "address: STREAM/TCP 2001:db8::1 80\n");
  224. if (ret == 0)
  225. freeaddrinfo (ai);
  226. }
  227. }
  228. /* Test gethostbyaddr and getnameinfo. The results are independent of
  229. RES_USE_INET6. */
  230. static void
  231. test_reverse (void)
  232. {
  233. {
  234. char ipv4[4] = { 192, 0, 2, 17 };
  235. check_hostent ("gethostbyaddr AF_INET",
  236. gethostbyaddr (ipv4, sizeof (ipv4), AF_INET),
  237. "name: 192-0-2-17.v4.example\n"
  238. "address: 192.0.2.17\n");
  239. }
  240. {
  241. char ipv6[16]
  242. = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
  243. check_hostent ("gethostbyaddr AF_INET",
  244. gethostbyaddr (ipv6, sizeof (ipv6), AF_INET6),
  245. "name: 20010db8000000000000000000000001.v6.example\n"
  246. "address: 2001:db8::1\n");
  247. }
  248. {
  249. struct sockaddr_in addr =
  250. {
  251. .sin_family = AF_INET,
  252. .sin_addr = { .s_addr = htonl (0xc0000211) },
  253. .sin_port = htons (80)
  254. };
  255. char host[NI_MAXHOST];
  256. char service[NI_MAXSERV];
  257. int ret = getnameinfo ((struct sockaddr *) &addr, sizeof (addr),
  258. host, sizeof (host), service, sizeof (service),
  259. NI_NUMERICSERV);
  260. TEST_VERIFY (ret == 0);
  261. TEST_VERIFY (strcmp (host, "192-0-2-17.v4.example") == 0);
  262. TEST_VERIFY (strcmp (service, "80") == 0);
  263. }
  264. {
  265. char ipv6[16]
  266. = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
  267. struct sockaddr_in6 addr =
  268. {
  269. .sin6_family = AF_INET6,
  270. .sin6_port = htons (80),
  271. };
  272. TEST_VERIFY (sizeof (ipv6) == sizeof (addr.sin6_addr));
  273. memcpy (&addr.sin6_addr, ipv6, sizeof (addr.sin6_addr));
  274. char host[NI_MAXHOST];
  275. char service[NI_MAXSERV];
  276. int ret = getnameinfo ((struct sockaddr *) &addr, sizeof (addr),
  277. host, sizeof (host), service, sizeof (service),
  278. NI_NUMERICSERV);
  279. TEST_VERIFY (ret == 0);
  280. TEST_VERIFY
  281. (strcmp (host, "20010db8000000000000000000000001.v6.example") == 0);
  282. TEST_VERIFY (strcmp (service, "80") == 0);
  283. }
  284. }
  285. /* Test that gethostbyname2 is mostly not influenced by
  286. RES_USE_INET6. */
  287. static void
  288. test_get2_any (void)
  289. {
  290. check_hostent ("gethostbyname2 AF_INET am.example",
  291. gethostbyname2 ("am.example", AF_INET),
  292. "name: am.example\n"
  293. "address: 192.0.2.17\n");
  294. check_hostent ("gethostbyname2 AF_INET a.example",
  295. gethostbyname2 ("a.example", AF_INET),
  296. "name: a.example\n"
  297. "address: 192.0.2.17\n");
  298. check_hostent ("gethostbyname2 AF_INET qm.example",
  299. gethostbyname2 ("qm.example", AF_INET),
  300. "error: NO_ADDRESS\n");
  301. check_hostent ("gethostbyname2 AF_INET q.example",
  302. gethostbyname2 ("q.example", AF_INET),
  303. "error: NO_RECOVERY\n");
  304. check_hostent ("gethostbyname2 AF_INET qam.example",
  305. gethostbyname2 ("qam.example", AF_INET),
  306. "name: qam.example\n"
  307. "address: 192.0.2.17\n");
  308. check_hostent ("gethostbyname2 AF_INET qa.example",
  309. gethostbyname2 ("qa.example", AF_INET),
  310. "name: qa.example\n"
  311. "address: 192.0.2.17\n");
  312. check_hostent ("gethostbyname2 AF_INET6 qm.example",
  313. gethostbyname2 ("qm.example", AF_INET6),
  314. "name: qm.example\n"
  315. "address: 2001:db8::1\n");
  316. check_hostent ("gethostbyname2 AF_INET6 q.example",
  317. gethostbyname2 ("q.example", AF_INET6),
  318. "name: q.example\n"
  319. "address: 2001:db8::1\n");
  320. check_hostent ("gethostbyname2 AF_INET6 qam.example",
  321. gethostbyname2 ("qam.example", AF_INET6),
  322. "name: qam.example\n"
  323. "address: 2001:db8::1\n");
  324. check_hostent ("gethostbyname2 AF_INET6 qa.example",
  325. gethostbyname2 ("qa.example", AF_INET6),
  326. "name: qa.example\n"
  327. "address: 2001:db8::1\n");
  328. /* Additional AF_INET6 tests depend on RES_USE_INET6; see below. */
  329. test_reverse ();
  330. }
  331. /* gethostbyname2 tests with RES_USE_INET6 disabled. */
  332. static void
  333. test_get2_no_inet6 (void)
  334. {
  335. test_get2_any ();
  336. check_hostent ("gethostbyname2 AF_INET6 am.example",
  337. gethostbyname2 ("am.example", AF_INET6),
  338. "error: NO_ADDRESS\n");
  339. check_hostent ("gethostbyname2 AF_INET6 a.example",
  340. gethostbyname2 ("a.example", AF_INET6),
  341. "error: NO_RECOVERY\n");
  342. }
  343. /* gethostbyname2 tests with RES_USE_INET6 enabled. */
  344. static void
  345. test_get2_inet6 (void)
  346. {
  347. test_get2_any ();
  348. check_hostent ("gethostbyname2 AF_INET6 am.example",
  349. gethostbyname2 ("am.example", AF_INET6),
  350. "name: am.example\n"
  351. "address: ::ffff:192.0.2.17\n");
  352. check_hostent ("gethostbyname2 AF_INET6 a.example",
  353. gethostbyname2 ("a.example", AF_INET6),
  354. "error: NO_RECOVERY\n");
  355. }
  356. /* Collection of tests which assume no RES_USE_INET6 flag. */
  357. static void
  358. test_no_inet6 (void)
  359. {
  360. check_hostent ("gethostbyname (\"a.example\")",
  361. gethostbyname ("a.example"),
  362. "name: a.example\n"
  363. "address: 192.0.2.17\n");
  364. check_hostent ("gethostbyname (\"qa.example\")",
  365. gethostbyname ("qa.example"),
  366. "name: qa.example\n"
  367. "address: 192.0.2.17\n");
  368. check_hostent ("gethostbyname (\"am.example\")",
  369. gethostbyname ("am.example"),
  370. "name: am.example\n"
  371. "address: 192.0.2.17\n");
  372. check_hostent ("gethostbyname (\"amp.example\")",
  373. gethostbyname ("amp.example"),
  374. "name: amp.example\n"
  375. "address: 192.0.2.17\n");
  376. check_hostent ("gethostbyname (\"qam.example\")",
  377. gethostbyname ("qam.example"),
  378. "name: qam.example\n"
  379. "address: 192.0.2.17\n");
  380. check_hostent ("gethostbyname (\"q.example\")",
  381. gethostbyname ("q.example"),
  382. "error: NO_RECOVERY\n");
  383. check_hostent ("gethostbyname (\"qm.example\")",
  384. gethostbyname ("qm.example"),
  385. "error: NO_ADDRESS\n");
  386. test_get2_no_inet6 ();
  387. test_get2_no_inet6 ();
  388. test_gai ();
  389. test_get2_no_inet6 ();
  390. test_get2_no_inet6 ();
  391. }
  392. static void *
  393. threadfunc (void *ignored)
  394. {
  395. struct resolv_test *obj = resolv_test_start
  396. ((struct resolv_redirect_config)
  397. {
  398. .response_callback = response
  399. });
  400. TEST_VERIFY ((_res.options & RES_USE_INET6) == 0);
  401. test_no_inet6 ();
  402. _res.options |= RES_USE_INET6;
  403. check_hostent ("gethostbyname (\"a.inet6.example\")",
  404. gethostbyname ("a.inet6.example"),
  405. "error: NO_RECOVERY\n");
  406. check_hostent ("gethostbyname (\"am.inet6.example\")",
  407. gethostbyname ("am.inet6.example"),
  408. "name: am.inet6.example\n"
  409. "address: ::ffff:192.0.2.17\n");
  410. check_hostent ("gethostbyname (\"qa.inet6.example\")",
  411. gethostbyname ("qa.inet6.example"),
  412. "name: qa.inet6.example\n"
  413. "address: 2001:db8::1\n");
  414. check_hostent ("gethostbyname (\"qam.inet6.example\")",
  415. gethostbyname ("qam.inet6.example"),
  416. "name: qam.inet6.example\n"
  417. "address: 2001:db8::1\n");
  418. check_hostent ("gethostbyname (\"q.inet6.example\")",
  419. gethostbyname ("q.inet6.example"),
  420. "name: q.inet6.example\n"
  421. "address: 2001:db8::1\n");
  422. check_hostent ("gethostbyname (\"qm.inet6.example\")",
  423. gethostbyname ("qm.inet6.example"),
  424. "name: qm.inet6.example\n"
  425. "address: 2001:db8::1\n");
  426. check_hostent ("gethostbyname (\"amp.inet6.example\")",
  427. gethostbyname ("amp.inet6.example"),
  428. "error: NO_RECOVERY\n");
  429. check_hostent ("gethostbyname (\"qmp.inet6.example\")",
  430. gethostbyname ("qmp.inet6.example"),
  431. "name: qmp.inet6.example\n"
  432. "address: 2001:db8::1\n");
  433. check_hostent ("gethostbyname (\"ap.inet6.example\")",
  434. gethostbyname ("ap.inet6.example"),
  435. "error: NO_RECOVERY\n");
  436. check_hostent ("gethostbyname (\"6ap.inet6.example\")",
  437. gethostbyname ("6ap.inet6.example"),
  438. "name: 6ap.inet6.example\n"
  439. "address: ::ffff:192.0.2.17\n");
  440. check_hostent ("gethostbyname (\"am6p.inet6.example\")",
  441. gethostbyname ("am6p.inet6.example"),
  442. "name: am6p.inet6.example\n"
  443. "address: ::ffff:192.0.2.17\n");
  444. check_hostent ("gethostbyname (\"qp.inet6.example\")",
  445. gethostbyname ("qp.inet6.example"),
  446. "name: qp.inet6.example\n"
  447. "address: 2001:db8::1\n");
  448. test_get2_inet6 ();
  449. test_get2_inet6 ();
  450. test_gai ();
  451. test_get2_inet6 ();
  452. test_get2_inet6 ();
  453. TEST_VERIFY (_res.options & RES_USE_INET6);
  454. _res.options &= ~RES_USE_INET6;
  455. test_no_inet6 ();
  456. resolv_test_end (obj);
  457. return NULL;
  458. }
  459. static int
  460. do_test (void)
  461. {
  462. resolv_test_init ();
  463. /* Attempt to run on a non-main thread first. */
  464. {
  465. pthread_t thr = xpthread_create (NULL, threadfunc, NULL);
  466. xpthread_join (thr);
  467. }
  468. /* Try the main thread next. */
  469. threadfunc (NULL);
  470. return 0;
  471. }
  472. #include <support/test-driver.c>