tst-getaddrinfo5.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Test host lookup with double dots at the end, [BZ #16469].
  2. Copyright (C) 2014-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. #include <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <netdb.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. static int
  21. test (void)
  22. {
  23. static char host1[] = "localhost..";
  24. static char host2[] = "www.gnu.org..";
  25. static char *hosts[] = { host1, host2 };
  26. int i;
  27. int pass = 0;
  28. for (i = 0; i < sizeof (hosts) / sizeof (*hosts); i++)
  29. {
  30. char *host = hosts[i];
  31. size_t len = strlen (host);
  32. struct addrinfo *ai;
  33. /* If the name doesn't resolve with a single dot at the
  34. end, skip it. */
  35. host[len-1] = 0;
  36. if (getaddrinfo (host, NULL, NULL, &ai) != 0)
  37. {
  38. printf ("resolving \"%s\" failed, skipping this hostname\n", host);
  39. continue;
  40. }
  41. printf ("resolving \"%s\" worked, proceeding to test\n", host);
  42. freeaddrinfo (ai);
  43. /* If it resolved with a single dot, check that it doesn't with
  44. a second trailing dot. */
  45. host[len-1] = '.';
  46. if (getaddrinfo (host, NULL, NULL, &ai) == 0)
  47. {
  48. printf ("resolving \"%s\" worked, test failed\n", host);
  49. return 1;
  50. }
  51. printf ("resolving \"%s\" failed, test passed\n", host);
  52. pass = 1;
  53. }
  54. /* We want at least one successful name resolution for the test to
  55. succeed. */
  56. return pass ? 0 : 2;
  57. }
  58. #define TEST_FUNCTION test ()
  59. #include "../test-skeleton.c"