1 /* Convert struct addrinfo values to a string.
2 Copyright (C) 2016-2017 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 <http://www.gnu.org/licenses/>. */
19 #include <support/format_nss.h>
21 #include <arpa/inet.h>
24 #include <support/support.h>
25 #include <support/xmemstream.h>
28 socket_address_length (int family
)
33 return sizeof (struct sockaddr_in
);
35 return sizeof (struct sockaddr_in6
);
42 format_ai_flags (FILE *out
, struct addrinfo
*ai
, int flag
, const char *name
,
45 if ((ai
->ai_flags
& flag
) != 0)
46 fprintf (out
, " %s", name
);
47 *flags_printed
|= flag
;
51 format_ai_one (FILE *out
, struct addrinfo
*ai
, int *flags
)
54 if (ai
->ai_flags
!= *flags
)
56 fprintf (out
, "flags:");
57 int flags_printed
= 0;
58 #define FLAG(flag) format_ai_flags (out, ai, flag, #flag, &flags_printed)
61 FLAG (AI_NUMERICHOST
);
67 FLAG (AI_IDN_ALLOW_UNASSIGNED
);
68 FLAG (AI_IDN_USE_STD3_ASCII_RULES
);
69 FLAG (AI_NUMERICSERV
);
71 int remaining
= ai
->ai_flags
& ~flags_printed
;
73 fprintf (out
, " %08x", remaining
);
75 *flags
= ai
->ai_flags
;
82 const char *proto_str
;
85 switch (ai
->ai_socktype
)
97 snprintf (type_buf
, sizeof (type_buf
), "%d", ai
->ai_socktype
);
102 switch (ai
->ai_protocol
)
114 snprintf (proto_buf
, sizeof (proto_buf
), "%d", ai
->ai_protocol
);
115 proto_str
= proto_buf
;
117 fprintf (out
, "address: %s/%s", type_str
, proto_str
);
121 if (ai
->ai_addrlen
!= socket_address_length (ai
->ai_family
))
123 char *family
= support_format_address_family (ai
->ai_family
);
124 fprintf (out
, "error: invalid address length %d for %s\n",
125 ai
->ai_addrlen
, family
);
134 switch (ai
->ai_family
)
138 struct sockaddr_in
*sin
= (struct sockaddr_in
*) ai
->ai_addr
;
139 ret
= inet_ntop (AF_INET
, &sin
->sin_addr
, buf
, sizeof (buf
));
140 port
= sin
->sin_port
;
145 struct sockaddr_in6
*sin
= (struct sockaddr_in6
*) ai
->ai_addr
;
146 ret
= inet_ntop (AF_INET6
, &sin
->sin6_addr
, buf
, sizeof (buf
));
147 port
= sin
->sin6_port
;
151 errno
= EAFNOSUPPORT
;
155 fprintf (out
, "error: inet_top failed: %m\n");
157 fprintf (out
, " %s %u\n", buf
, ntohs (port
));
161 if (ai
->ai_canonname
!= NULL
)
162 fprintf (out
, "canonname: %s\n", ai
->ai_canonname
);
165 /* Format all the addresses in one address family. */
167 format_ai_family (FILE *out
, struct addrinfo
*ai
, int family
, int *flags
)
171 if (ai
->ai_family
== family
)
172 format_ai_one (out
, ai
, flags
);
178 support_format_addrinfo (struct addrinfo
*ai
, int ret
)
180 int errno_copy
= errno
;
182 struct xmemstream mem
;
183 xopen_memstream (&mem
);
186 fprintf (mem
.out
, "error: %s\n", gai_strerror (ret
));
187 if (ret
== EAI_SYSTEM
)
190 fprintf (mem
.out
, "error: %m\n");
196 format_ai_family (mem
.out
, ai
, AF_INET
, &flags
);
197 format_ai_family (mem
.out
, ai
, AF_INET6
, &flags
);
200 xfclose_memstream (&mem
);