platform: add and use nm_sock_addr_union_to_string()

We still don't use getnameinfo(). This is used for logging,
where we want to see a string representation that is as close
as possible to the actual bytes (to spot differences). It should
not be obfuscated by a libc function out of our control.

Also fix the notation for the IPv6 scope ID to use the common '%'
character.
This commit is contained in:
Thomas Haller 2019-01-13 11:36:59 +01:00
parent 32749cea99
commit 78ce4307c0
3 changed files with 59 additions and 19 deletions

View file

@ -1997,7 +1997,7 @@ int
nm_platform_link_wireguard_change (NMPlatform *self,
int ifindex,
const NMPlatformLnkWireGuard *lnk_wireguard,
const struct _NMPWireGuardPeer *peers,
const NMPWireGuardPeer *peers,
guint peers_len)
{
_CHECK_SELF (self, klass, -NME_BUG);
@ -5604,33 +5604,24 @@ nm_platform_wireguard_peer_to_string (const NMPWireGuardPeer *peer, char *buf, g
{
char *buf0 = buf;
gs_free char *public_key_b64 = NULL;
char s_endpoint[NM_UTILS_INET_ADDRSTRLEN + 100];
char s_sockaddr[NM_UTILS_INET_ADDRSTRLEN + 100];
char s_endpoint[20 + sizeof (s_sockaddr)];
char s_addr[NM_UTILS_INET_ADDRSTRLEN];
char s_scope_id[40];
guint i;
nm_utils_to_string_buffer_init (&buf, &len);
if (peer->endpoint.sa.sa_family == AF_INET) {
public_key_b64 = g_base64_encode (peer->public_key, sizeof (peer->public_key));
if (peer->endpoint.sa.sa_family != AF_UNSPEC) {
nm_sprintf_buf (s_endpoint,
" endpoint %s:%u",
nm_utils_inet4_ntop (peer->endpoint.in.sin_addr.s_addr, s_addr),
(guint) htons (peer->endpoint.in.sin_port));
} else if (peer->endpoint.sa.sa_family == AF_INET6) {
if (peer->endpoint.in6.sin6_scope_id != 0)
nm_sprintf_buf (s_scope_id, "@%u", peer->endpoint.in6.sin6_scope_id);
else
s_scope_id[0] = '\0';
nm_sprintf_buf (s_endpoint,
" endpoint [%s]%s:%u",
nm_utils_inet6_ntop (&peer->endpoint.in6.sin6_addr, s_addr),
s_scope_id,
(guint) htons (peer->endpoint.in6.sin6_port));
" endpoint %s",
nm_sock_addr_union_to_string (&peer->endpoint,
s_sockaddr,
sizeof (s_sockaddr)));
} else
s_endpoint[0] = '\0';
public_key_b64 = g_base64_encode (peer->public_key, sizeof (peer->public_key));
nm_utils_strbuf_append (&buf, &len,
"public-key %s"
"%s" /* preshared-key */

View file

@ -228,6 +228,51 @@ nm_sock_addr_union_cpy_untrusted (NMSockAddrUnion *dst,
nm_assert (dst->sa.sa_family == sa.sa_family);
}
const char *
nm_sock_addr_union_to_string (const NMSockAddrUnion *sa,
char *buf,
gsize len)
{
char s_addr[NM_UTILS_INET_ADDRSTRLEN];
char s_scope_id[40];
if (!nm_utils_to_string_buffer_init_null (sa, &buf, &len))
return buf;
/* maybe we should use getnameinfo(), but here implement it ourself.
*
* We want to see the actual bytes for debugging (as we understand them),
* and now what getnameinfo() makes of it. Also, it's simpler this way. */
switch (sa->sa.sa_family) {
case AF_INET:
g_snprintf (buf, len,
"%s:%u",
nm_utils_inet4_ntop (sa->in.sin_addr.s_addr, s_addr),
(guint) htons (sa->in.sin_port));
break;
case AF_INET6:
g_snprintf (buf, len,
"[%s%s]:%u",
nm_utils_inet6_ntop (&sa->in6.sin6_addr, s_addr),
( sa->in6.sin6_scope_id != 0
? nm_sprintf_buf (s_scope_id, "%u", sa->in6.sin6_scope_id)
: ""),
(guint) htons (sa->in6.sin6_port));
break;
case AF_UNSPEC:
g_snprintf (buf, len, "unspec");
break;
default:
g_snprintf (buf, len,
"{addr-family:%u}",
(unsigned) sa->sa.sa_family);
break;
}
return buf;
}
/*****************************************************************************/
static const NMDedupMultiIdxTypeClass _dedup_multi_idx_type_class;

View file

@ -57,6 +57,10 @@ void nm_sock_addr_union_cpy_untrusted (NMSockAddrUnion *dst,
gconstpointer src /* unaligned (const NMSockAddrUnion *) */,
gsize src_len);
const char *nm_sock_addr_union_to_string (const NMSockAddrUnion *sa,
char *buf,
gsize len);
/*****************************************************************************/
typedef struct {