1 /* Test the getaddrinfo module.
3 Copyright (C) 2006-2020 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Simon Josefsson. */
24 #include "signature.h"
25 SIGNATURE_CHECK (gai_strerror
, char const *, (int));
26 /* On native Windows, these two functions may have the __stdcall calling
27 convention. But the SIGNATURE_CHECK works only for functions with __cdecl
28 calling convention. */
29 #if !(defined _WIN32 && !defined __CYGWIN__)
30 SIGNATURE_CHECK (freeaddrinfo
, void, (struct addrinfo
*));
31 SIGNATURE_CHECK (getaddrinfo
, int, (char const *, char const *,
32 struct addrinfo
const *,
36 #include <arpa/inet.h>
38 #include <netinet/in.h>
44 /* Whether to print debugging messages. */
45 #define ENABLE_DEBUGGING 0
48 # define dbgprintf printf
50 # define dbgprintf if (0) printf
53 /* BeOS does not have AF_UNSPEC. */
59 # define EAI_SERVICE 0
63 simple (char const *host
, char const *service
)
67 struct addrinfo hints
;
68 struct addrinfo
*ai0
, *ai
;
72 /* Once we skipped the test, do not try anything else */
76 dbgprintf ("Finding %s service %s...\n", host
, service
);
78 /* This initializes "hints" but does not use it. Is there a reason
79 for this? If so, please fix this comment. */
80 memset (&hints
, 0, sizeof (hints
));
81 hints
.ai_flags
= AI_CANONNAME
;
82 hints
.ai_family
= AF_UNSPEC
;
83 hints
.ai_socktype
= SOCK_STREAM
;
85 res
= getaddrinfo (host
, service
, 0, &ai0
);
88 dbgprintf ("res %d: %s\n", res
, gai_strerror (res
));
92 /* EAI_AGAIN is returned if no network is available. Don't fail
93 the test merely because someone is down the country on their
98 fprintf (stderr
, "skipping getaddrinfo test: no network?\n");
101 /* IRIX reports EAI_NONAME for "https". Don't fail the test
102 merely because of this. */
103 if (res
== EAI_NONAME
)
105 /* Solaris reports EAI_SERVICE for "http" and "https". Don't
106 fail the test merely because of this. */
107 if (res
== EAI_SERVICE
)
110 /* AIX reports EAI_NODATA for "https". Don't fail the test
111 merely because of this. */
112 if (res
== EAI_NODATA
)
115 /* Provide details if errno was set. */
116 if (res
== EAI_SYSTEM
)
117 fprintf (stderr
, "system error: %s\n", strerror (err
));
122 for (ai
= ai0
; ai
; ai
= ai
->ai_next
)
124 void *ai_addr
= ai
->ai_addr
;
125 struct sockaddr_in
*sock_addr
= ai_addr
;
126 dbgprintf ("\tflags %x\n", ai
->ai_flags
+ 0u);
127 dbgprintf ("\tfamily %x\n", ai
->ai_family
+ 0u);
128 dbgprintf ("\tsocktype %x\n", ai
->ai_socktype
+ 0u);
129 dbgprintf ("\tprotocol %x\n", ai
->ai_protocol
+ 0u);
130 dbgprintf ("\taddrlen %lu: ", (unsigned long) ai
->ai_addrlen
);
131 dbgprintf ("\tFound %s\n",
132 inet_ntop (ai
->ai_family
,
133 &sock_addr
->sin_addr
,
134 buf
, sizeof (buf
) - 1));
135 if (ai
->ai_canonname
)
136 dbgprintf ("\tFound %s...\n", ai
->ai_canonname
);
140 char portbuf
[BUFSIZ
];
142 res
= getnameinfo (ai
->ai_addr
, ai
->ai_addrlen
,
143 ipbuf
, sizeof (ipbuf
) - 1,
144 portbuf
, sizeof (portbuf
) - 1,
145 NI_NUMERICHOST
|NI_NUMERICSERV
);
146 dbgprintf ("\t\tgetnameinfo %d: %s\n", res
, gai_strerror (res
));
149 dbgprintf ("\t\tip %s\n", ipbuf
);
150 dbgprintf ("\t\tport %s\n", portbuf
);
161 #define HOST1 "www.gnu.org"
163 #define HOST2 "www.ibm.com"
164 #define SERV2 "https"
165 #define HOST3 "microsoft.com"
167 #define HOST4 "google.org"
172 (void) gl_sockets_startup (SOCKETS_1_1
);
174 return simple (HOST1
, SERV1
)
175 + simple (HOST2
, SERV2
)
176 + simple (HOST3
, SERV3
)
177 + simple (HOST4
, SERV4
);