tst-getaddrinfo.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Copyright (C) 1999-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <netinet/in.h>
  19. #include <netdb.h>
  20. static int
  21. do_test (void)
  22. {
  23. const int family[2] = { AF_INET, AF_INET6 };
  24. int result = 0;
  25. int gaierr;
  26. size_t index;
  27. struct addrinfo hints, *ai, *aitop;
  28. for (index = 0; index < sizeof (family) / sizeof (family[0]); ++index)
  29. {
  30. memset (&hints, '\0', sizeof (hints));
  31. hints.ai_family = family[index];
  32. hints.ai_socktype = SOCK_STREAM;
  33. gaierr = getaddrinfo (NULL, "54321", &hints, &aitop);
  34. if (gaierr != 0)
  35. {
  36. gai_strerror (gaierr);
  37. result = 1;
  38. }
  39. else
  40. {
  41. for (ai = aitop; ai != NULL; ai = ai->ai_next)
  42. {
  43. printf ("Should return family: %d. Returned: %d\n",
  44. family[index], ai->ai_family);
  45. result |= family[index] != ai->ai_family;
  46. }
  47. while (aitop != NULL)
  48. {
  49. ai = aitop;
  50. aitop = aitop->ai_next;
  51. freeaddrinfo (ai);
  52. }
  53. }
  54. }
  55. return result;
  56. }
  57. #define TEST_FUNCTION do_test ()
  58. #include "../test-skeleton.c"