diff options
-rw-r--r-- | doc/developer/logging.rst | 6 | ||||
-rw-r--r-- | lib/prefix.c | 46 | ||||
-rw-r--r-- | tests/lib/test_printfrr.c | 4 |
3 files changed, 56 insertions, 0 deletions
diff --git a/doc/developer/logging.rst b/doc/developer/logging.rst index e60804682..a51f51553 100644 --- a/doc/developer/logging.rst +++ b/doc/developer/logging.rst @@ -123,10 +123,14 @@ Networking data types :frrfmtout:`1.2.3.4` + ``%pI4s``: :frrfmtout:`*` — print star instead of ``0.0.0.0`` (for multicast) + .. frrfmt:: %pI6 (struct in6_addr *) :frrfmtout:`fe80::1234` + ``%pI6s``: :frrfmtout:`*` — print star instead of ``::`` (for multicast) + .. frrfmt:: %pEA (struct ethaddr *) :frrfmtout:`01:23:45:67:89:ab` @@ -135,6 +139,8 @@ Networking data types :frrfmtout:`1.2.3.4` / :frrfmtout:`fe80::1234` + ``%pIAs``: — print star instead of zero address (for multicast) + .. frrfmt:: %pFX (struct prefix *) :frrfmtout:`1.2.3.0/24` / :frrfmtout:`fe80::1234/64` diff --git a/lib/prefix.c b/lib/prefix.c index d3e8a612e..4e9d41836 100644 --- a/lib/prefix.c +++ b/lib/prefix.c @@ -1374,10 +1374,36 @@ static ssize_t printfrr_ia(struct fbuf *buf, struct printfrr_eargs *ea, { const struct ipaddr *ipa = ptr; char cbuf[INET6_ADDRSTRLEN]; + bool use_star = false; + + if (ea->fmt[0] == 's') { + use_star = true; + ea->fmt++; + } if (!ipa) return bputs(buf, "(null)"); + if (use_star) { + struct in_addr zero4 = {}; + struct in6_addr zero6 = {}; + + switch (ipa->ipa_type) { + case IPADDR_V4: + if (!memcmp(&ipa->ip.addr, &zero4, sizeof(zero4))) + return bputch(buf, '*'); + break; + + case IPADDR_V6: + if (!memcmp(&ipa->ip.addr, &zero6, sizeof(zero6))) + return bputch(buf, '*'); + break; + + default: + break; + } + } + ipaddr2str(ipa, cbuf, sizeof(cbuf)); return bputs(buf, cbuf); } @@ -1387,10 +1413,20 @@ static ssize_t printfrr_i4(struct fbuf *buf, struct printfrr_eargs *ea, const void *ptr) { char cbuf[INET_ADDRSTRLEN]; + bool use_star = false; + struct in_addr zero = {}; + + if (ea->fmt[0] == 's') { + use_star = true; + ea->fmt++; + } if (!ptr) return bputs(buf, "(null)"); + if (use_star && !memcmp(ptr, &zero, sizeof(zero))) + return bputch(buf, '*'); + inet_ntop(AF_INET, ptr, cbuf, sizeof(cbuf)); return bputs(buf, cbuf); } @@ -1400,10 +1436,20 @@ static ssize_t printfrr_i6(struct fbuf *buf, struct printfrr_eargs *ea, const void *ptr) { char cbuf[INET6_ADDRSTRLEN]; + bool use_star = false; + struct in6_addr zero = {}; + + if (ea->fmt[0] == 's') { + use_star = true; + ea->fmt++; + } if (!ptr) return bputs(buf, "(null)"); + if (use_star && !memcmp(ptr, &zero, sizeof(zero))) + return bputch(buf, '*'); + inet_ntop(AF_INET6, ptr, cbuf, sizeof(cbuf)); return bputs(buf, cbuf); } diff --git a/tests/lib/test_printfrr.c b/tests/lib/test_printfrr.c index 769407757..55aa9bdef 100644 --- a/tests/lib/test_printfrr.c +++ b/tests/lib/test_printfrr.c @@ -186,6 +186,10 @@ int main(int argc, char **argv) test_va("VA [192.168.1.2 1234] --", "%pI4 %u", &ip, 1234); + inet_aton("0.0.0.0", &ip); + printchk("0.0.0.0", "%pI4", &ip); + printchk("*", "%pI4s", &ip); + snprintfrr(buf, sizeof(buf), "test%s", "#1"); csnprintfrr(buf, sizeof(buf), "test%s", "#2"); assert(strcmp(buf, "test#1test#2") == 0); |