]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: add `%pFXh` to print prefix w/o prefixlen
authorDavid Lamparter <equinox@opensourcerouting.org>
Fri, 11 Mar 2022 10:59:38 +0000 (11:59 +0100)
committerDavid Lamparter <equinox@opensourcerouting.org>
Fri, 11 Mar 2022 12:43:19 +0000 (13:43 +0100)
Mostly for pimd, for the time being.  May be removed again if unused.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
doc/developer/logging.rst
lib/prefix.c
tests/lib/test_printfrr.c

index 4e6fc04206e0d78c8f8415d947ed0836df07eb29..70463612045b0352ca6e4c33e7767677acc3bef5 100644 (file)
@@ -163,6 +163,10 @@ Networking data types
    - :c:union:`prefixptr` (dereference to get :c:struct:`prefix`)
    - :c:union:`prefixconstptr` (dereference to get :c:struct:`prefix`)
 
+   Options:
+
+   ``%pFXh``: (address only) :frrfmtout:`1.2.3.0` / :frrfmtout:`fe80::1234`
+
 .. frrfmt:: %pPSG4 (struct prefix_sg *)
 
    :frrfmtout:`(*,1.2.3.4)`
index 90ab48a13b0594a45d557780d24bab184846c8b7..89c5be8f381134081db4293321697407f02d6aed 100644 (file)
@@ -1071,6 +1071,26 @@ const char *prefix2str(union prefixconstptr pu, char *str, int size)
        return str;
 }
 
+static ssize_t prefixhost2str(struct fbuf *fbuf, union prefixconstptr pu)
+{
+       const struct prefix *p = pu.p;
+       char buf[PREFIX2STR_BUFFER];
+
+       switch (p->family) {
+       case AF_INET:
+       case AF_INET6:
+               inet_ntop(p->family, &p->u.prefix, buf, sizeof(buf));
+               return bputs(fbuf, buf);
+
+       case AF_ETHERNET:
+               prefix_mac2str(&p->u.prefix_eth, buf, sizeof(buf));
+               return bputs(fbuf, buf);
+
+       default:
+               return bprintfrr(fbuf, "{prefix.af=%dPF}", p->family);
+       }
+}
+
 void prefix_mcast_inet4_dump(const char *onfail, struct in_addr addr,
                char *buf, int buf_size)
 {
@@ -1458,13 +1478,24 @@ printfrr_ext_autoreg_p("FX", printfrr_pfx);
 static ssize_t printfrr_pfx(struct fbuf *buf, struct printfrr_eargs *ea,
                            const void *ptr)
 {
-       char cbuf[PREFIX_STRLEN];
+       bool host_only = false;
+
+       if (ea->fmt[0] == 'h') {
+               ea->fmt++;
+               host_only = true;
+       }
 
        if (!ptr)
                return bputs(buf, "(null)");
 
-       prefix2str(ptr, cbuf, sizeof(cbuf));
-       return bputs(buf, cbuf);
+       if (host_only)
+               return prefixhost2str(buf, (struct prefix *)ptr);
+       else {
+               char cbuf[PREFIX_STRLEN];
+
+               prefix2str(ptr, cbuf, sizeof(cbuf));
+               return bputs(buf, cbuf);
+       }
 }
 
 printfrr_ext_autoreg_p("PSG4", printfrr_psg);
index 8f9d637afd551d9de6f906d14dbc87be1db32705..59d08ae82b0407dddabeeb091acba4c08e4a5ae3 100644 (file)
@@ -207,6 +207,24 @@ int main(int argc, char **argv)
        assert(strcmp(p, "test#5") == 0);
        XFREE(MTYPE_TMP, p);
 
+       struct prefix pfx;
+
+       str2prefix("192.168.1.23/24", &pfx);
+       printchk("192.168.1.23/24", "%pFX", &pfx);
+       printchk("192.168.1.23", "%pFXh", &pfx);
+
+       str2prefix("2001:db8::1234/64", &pfx);
+       printchk("2001:db8::1234/64", "%pFX", &pfx);
+       printchk("2001:db8::1234", "%pFXh", &pfx);
+
+       pfx.family = AF_UNIX;
+       printchk("UNK prefix", "%pFX", &pfx);
+       printchk("{prefix.af=AF_UNIX}", "%pFXh", &pfx);
+
+       str2prefix_eth("02:ca:fe:f0:0d:1e/48", (struct prefix_eth *)&pfx);
+       printchk("02:ca:fe:f0:0d:1e/48", "%pFX", &pfx);
+       printchk("02:ca:fe:f0:0d:1e", "%pFXh", &pfx);
+
        struct prefix_sg sg;
        sg.src.s_addr = INADDR_ANY;
        sg.grp.s_addr = INADDR_ANY;