summaryrefslogtreecommitdiff
path: root/lib/sockunion.c
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@diac24.net>2021-03-26 14:16:01 +0100
committerDavid Lamparter <equinox@diac24.net>2021-03-30 22:34:56 +0200
commit94f78404952795538b72b752ef08d865122ecd09 (patch)
treec369c678b187287bb967e1e38249e279e15a61cb /lib/sockunion.c
parent7798203f5cf93b35aed96925f3ffd6fa00a44790 (diff)
lib: fix & improve `%pSU` format
This wasn't quite formatting IPv6+port in a useful way (no brackets), and printing the scope ID (interface index) and unix addrs is useful too. Signed-off-by: David Lamparter <equinox@diac24.net>
Diffstat (limited to 'lib/sockunion.c')
-rw-r--r--lib/sockunion.c31
1 files changed, 28 insertions, 3 deletions
diff --git a/lib/sockunion.c b/lib/sockunion.c
index 2175ac3360..37bd3b841e 100644
--- a/lib/sockunion.c
+++ b/lib/sockunion.c
@@ -668,7 +668,7 @@ static ssize_t printfrr_psu(struct fbuf *buf, struct printfrr_eargs *ea,
const void *ptr)
{
const union sockunion *su = ptr;
- bool include_port = false;
+ bool include_port = false, include_scope = false;
bool endflags = false;
ssize_t ret = 0;
char cbuf[INET6_ADDRSTRLEN];
@@ -682,6 +682,10 @@ static ssize_t printfrr_psu(struct fbuf *buf, struct printfrr_eargs *ea,
ea->fmt++;
include_port = true;
break;
+ case 's':
+ ea->fmt++;
+ include_scope = true;
+ break;
default:
endflags = true;
break;
@@ -696,14 +700,35 @@ static ssize_t printfrr_psu(struct fbuf *buf, struct printfrr_eargs *ea,
inet_ntop(AF_INET, &su->sin.sin_addr, cbuf, sizeof(cbuf));
ret += bputs(buf, cbuf);
if (include_port)
- ret += bprintfrr(buf, ":%d", su->sin.sin_port);
+ ret += bprintfrr(buf, ":%d", ntohs(su->sin.sin_port));
break;
case AF_INET6:
+ if (include_port)
+ ret += bputch(buf, '[');
inet_ntop(AF_INET6, &su->sin6.sin6_addr, cbuf, sizeof(cbuf));
ret += bputs(buf, cbuf);
+ if (include_scope && su->sin6.sin6_scope_id)
+ ret += bprintfrr(buf, "%%%u",
+ (unsigned int)su->sin6.sin6_scope_id);
if (include_port)
- ret += bprintfrr(buf, ":%d", su->sin6.sin6_port);
+ ret += bprintfrr(buf, "]:%d",
+ ntohs(su->sin6.sin6_port));
+ break;
+ case AF_UNIX: {
+ int len;
+#ifdef __linux__
+ if (su->sun.sun_path[0] == '\0' && su->sun.sun_path[1]) {
+ len = strnlen(su->sun.sun_path + 1,
+ sizeof(su->sun.sun_path) - 1);
+ ret += bprintfrr(buf, "@%*pSE", len,
+ su->sun.sun_path + 1);
+ break;
+ }
+#endif
+ len = strnlen(su->sun.sun_path, sizeof(su->sun.sun_path));
+ ret += bprintfrr(buf, "%*pSE", len, su->sun.sun_path);
break;
+ }
default:
ret += bprintfrr(buf, "(af %d)", sockunion_family(su));
}