tst-getaddrinfo4.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Test getaddrinfo return value, [BZ #15339].
  2. Copyright (C) 2013-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 <string.h>
  16. #include <stdio.h>
  17. #include <errno.h>
  18. #include <netdb.h>
  19. static int
  20. try (const char *service, int family, int flags)
  21. {
  22. struct addrinfo hints, *h, *ai;
  23. int res;
  24. memset (&hints, 0, sizeof hints);
  25. hints.ai_family = family;
  26. hints.ai_flags = flags;
  27. errno = 0;
  28. h = (family || flags) ? &hints : NULL;
  29. res = getaddrinfo ("example.net", service, h, &ai);
  30. switch (res)
  31. {
  32. case 0:
  33. case EAI_AGAIN:
  34. case EAI_NONAME:
  35. printf ("SUCCESS getaddrinfo(service=%s, family=%d, flags=%d): %s: %m\n",
  36. service ?: "NULL", family, flags, gai_strerror (res));
  37. return 0;
  38. }
  39. printf ("FAIL getaddrinfo(service=%s, family=%d, flags=%d): %s: %m\n",
  40. service ?: "NULL", family, flags, gai_strerror (res));
  41. return 1;
  42. }
  43. static int
  44. do_test (void)
  45. {
  46. int err = 0;
  47. err |= try (NULL, 0, 0);
  48. err |= try (NULL, AF_UNSPEC, AI_ADDRCONFIG);
  49. err |= try (NULL, AF_INET, 0);
  50. err |= try (NULL, AF_INET6, 0);
  51. err |= try ("http", 0, 0);
  52. err |= try ("http", AF_UNSPEC, AI_ADDRCONFIG);
  53. err |= try ("http", AF_INET, 0);
  54. err |= try ("http", AF_INET6, 0);
  55. return err;
  56. }
  57. #define TEST_FUNCTION do_test ()
  58. #include "../test-skeleton.c"