tst-bug18665.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Test for __libc_res_nsend buffer mismanagent (bug 18665), UDP case.
  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 <errno.h>
  16. #include <netdb.h>
  17. #include <resolv.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <support/check.h>
  21. #include <support/resolv_test.h>
  22. #include <support/xthread.h>
  23. static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  24. static int initial_address_count;
  25. static int response_count;
  26. static void
  27. response (const struct resolv_response_context *ctx,
  28. struct resolv_response_builder *b,
  29. const char *qname, uint16_t qclass, uint16_t qtype)
  30. {
  31. TEST_VERIFY_EXIT (qname != NULL);
  32. struct resolv_response_flags flags = {};
  33. resolv_response_init (b, flags);
  34. resolv_response_add_question (b, qname, qclass, qtype);
  35. resolv_response_section (b, ns_s_an);
  36. /* Add many A/AAAA records to the second response. */
  37. int address_count;
  38. xpthread_mutex_lock (&lock);
  39. if (response_count == 0)
  40. address_count = initial_address_count;
  41. else
  42. address_count = 2000;
  43. ++response_count;
  44. xpthread_mutex_unlock (&lock);
  45. for (int i = 0; i < address_count; ++i)
  46. {
  47. resolv_response_open_record (b, qname, qclass, qtype, 0);
  48. switch (qtype)
  49. {
  50. case T_A:
  51. {
  52. char ipv4[4] = {10, i >> 8, i, 0};
  53. ipv4[3] = 2 * ctx->tcp + 4 * ctx->server_index;
  54. resolv_response_add_data (b, &ipv4, sizeof (ipv4));
  55. }
  56. break;
  57. case T_AAAA:
  58. {
  59. char ipv6[16]
  60. = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  61. i >> 8, i, 0};
  62. ipv6[15] = 2 * ctx->tcp + 4 * ctx->server_index;
  63. resolv_response_add_data (b, &ipv6, sizeof (ipv6));
  64. }
  65. break;
  66. default:
  67. support_record_failure ();
  68. printf ("error: unexpected QTYPE: %s/%u/%u\n",
  69. qname, qclass, qtype);
  70. }
  71. resolv_response_close_record (b);
  72. }
  73. }
  74. static void
  75. test_different_sizes (void)
  76. {
  77. struct addrinfo hints = { .ai_family = AF_UNSPEC, };
  78. struct addrinfo *ai;
  79. int ret;
  80. /* This magic number produces a response size close to 2048
  81. bytes. */
  82. initial_address_count = 126;
  83. response_count = 0;
  84. ret = getaddrinfo ("www.example", "80", &hints, &ai);
  85. if (ret == 0)
  86. freeaddrinfo (ai);
  87. response_count = 0;
  88. ret = getaddrinfo ("www123.example", "80", &hints, &ai);
  89. if (ret == 0)
  90. freeaddrinfo (ai);
  91. response_count = 0;
  92. ret = getaddrinfo ("www1234.example", "80", &hints, &ai);
  93. if (ret == 0)
  94. freeaddrinfo (ai);
  95. response_count = 0;
  96. ret = getaddrinfo ("www12345.example", "80", &hints, &ai);
  97. if (ret == 0)
  98. freeaddrinfo (ai);
  99. }
  100. static int
  101. do_test (void)
  102. {
  103. struct resolv_test *obj = resolv_test_start
  104. ((struct resolv_redirect_config)
  105. {
  106. .response_callback = response
  107. });
  108. test_different_sizes ();
  109. _res.options |= RES_SNGLKUP;
  110. test_different_sizes ();
  111. _res.options |= RES_SNGLKUPREOP;
  112. test_different_sizes ();
  113. resolv_test_end (obj);
  114. return 0;
  115. }
  116. #include <support/test-driver.c>