1 /* Test getaddrinfo return value, [BZ #15339].
2 Copyright (C) 2013-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <string.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include <netdb.h>
23
24 static int
try(const char * service,int family,int flags)25 try (const char *service, int family, int flags)
26 {
27 struct addrinfo hints, *h, *ai;
28 int res;
29
30 memset (&hints, 0, sizeof hints);
31 hints.ai_family = family;
32 hints.ai_flags = flags;
33
34 errno = 0;
35 h = (family || flags) ? &hints : NULL;
36 res = getaddrinfo ("example.net", service, h, &ai);
37 switch (res)
38 {
39 case 0:
40 case EAI_AGAIN:
41 case EAI_NONAME:
42 printf ("SUCCESS getaddrinfo(service=%s, family=%d, flags=%d): %s: %m\n",
43 service ?: "NULL", family, flags, gai_strerror (res));
44 return 0;
45 }
46 printf ("FAIL getaddrinfo(service=%s, family=%d, flags=%d): %s: %m\n",
47 service ?: "NULL", family, flags, gai_strerror (res));
48 return 1;
49 }
50
51 static int
do_test(void)52 do_test (void)
53 {
54 int err = 0;
55 err |= try (NULL, 0, 0);
56 err |= try (NULL, AF_UNSPEC, AI_ADDRCONFIG);
57 err |= try (NULL, AF_INET, 0);
58 err |= try (NULL, AF_INET6, 0);
59 err |= try ("http", 0, 0);
60 err |= try ("http", AF_UNSPEC, AI_ADDRCONFIG);
61 err |= try ("http", AF_INET, 0);
62 err |= try ("http", AF_INET6, 0);
63 return err;
64 }
65
66 #define TEST_FUNCTION do_test ()
67 #include "../test-skeleton.c"
68