1 /* Convert struct addrinfo values to a string.
2 Copyright (C) 2016-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <support/format_nss.h>
21 #include <arpa/inet.h>
25 #include <support/support.h>
26 #include <support/xmemstream.h>
29 socket_address_length (int family
)
34 return sizeof (struct sockaddr_in
);
36 return sizeof (struct sockaddr_in6
);
43 format_ai_flags_1 (FILE *out
, struct addrinfo
*ai
, int flag
, const char *name
,
46 if ((ai
->ai_flags
& flag
) != 0)
47 fprintf (out
, " %s", name
);
48 *flags_printed
|= flag
;
52 format_ai_flags (FILE *out
, struct addrinfo
*ai
)
57 if (ai
->ai_flags
!= 0)
59 fprintf (out
, "flags:");
60 int flags_printed
= 0;
61 #define FLAG(flag) format_ai_flags_1 (out, ai, flag, #flag, &flags_printed)
64 FLAG (AI_NUMERICHOST
);
70 FLAG (AI_NUMERICSERV
);
72 int remaining
= ai
->ai_flags
& ~flags_printed
;
74 fprintf (out
, " %08x", remaining
);
78 /* Report flag mismatches within the list. */
79 int flags
= ai
->ai_flags
;
84 if (ai
->ai_flags
!= flags
)
85 fprintf (out
, "error: flags at %d: 0x%x expected, 0x%x actual\n",
86 index
, flags
, ai
->ai_flags
);
93 format_ai_canonname (FILE *out
, struct addrinfo
*ai
)
97 if (ai
->ai_canonname
!= NULL
)
98 fprintf (out
, "canonname: %s\n", ai
->ai_canonname
);
100 /* Report incorrectly set ai_canonname fields on subsequent list
106 if (ai
->ai_canonname
!= NULL
)
107 fprintf (out
, "error: canonname set at %d: %s\n",
108 index
, ai
->ai_canonname
);
115 format_ai_one (FILE *out
, struct addrinfo
*ai
)
119 const char *type_str
;
121 const char *proto_str
;
124 switch (ai
->ai_socktype
)
136 snprintf (type_buf
, sizeof (type_buf
), "%d", ai
->ai_socktype
);
141 switch (ai
->ai_protocol
)
153 snprintf (proto_buf
, sizeof (proto_buf
), "%d", ai
->ai_protocol
);
154 proto_str
= proto_buf
;
156 fprintf (out
, "address: %s/%s", type_str
, proto_str
);
160 if (ai
->ai_addrlen
!= socket_address_length (ai
->ai_family
))
162 char *family
= support_format_address_family (ai
->ai_family
);
163 fprintf (out
, "error: invalid address length %d for %s\n",
164 ai
->ai_addrlen
, family
);
173 switch (ai
->ai_family
)
177 struct sockaddr_in
*sin
= (struct sockaddr_in
*) ai
->ai_addr
;
178 ret
= inet_ntop (AF_INET
, &sin
->sin_addr
, buf
, sizeof (buf
));
179 port
= sin
->sin_port
;
184 struct sockaddr_in6
*sin
= (struct sockaddr_in6
*) ai
->ai_addr
;
185 ret
= inet_ntop (AF_INET6
, &sin
->sin6_addr
, buf
, sizeof (buf
));
186 port
= sin
->sin6_port
;
190 errno
= EAFNOSUPPORT
;
194 fprintf (out
, "error: inet_top failed: %m\n");
196 fprintf (out
, " %s %u\n", buf
, ntohs (port
));
200 /* Format all the addresses in one address family. */
202 format_ai_family (FILE *out
, struct addrinfo
*ai
, int family
)
206 if (ai
->ai_family
== family
)
207 format_ai_one (out
, ai
);
213 support_format_addrinfo (struct addrinfo
*ai
, int ret
)
215 int errno_copy
= errno
;
217 struct xmemstream mem
;
218 xopen_memstream (&mem
);
221 const char *errmsg
= gai_strerror (ret
);
222 if (strcmp (errmsg
, "Unknown error") == 0)
223 fprintf (mem
.out
, "error: Unknown error %d\n", ret
);
225 fprintf (mem
.out
, "error: %s\n", errmsg
);
226 if (ret
== EAI_SYSTEM
)
229 fprintf (mem
.out
, "error: %m\n");
234 format_ai_flags (mem
.out
, ai
);
235 format_ai_canonname (mem
.out
, ai
);
236 format_ai_family (mem
.out
, ai
, AF_INET
);
237 format_ai_family (mem
.out
, ai
, AF_INET6
);
240 xfclose_memstream (&mem
);