]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: add %pSU for union sockunion *
authorDavid Lamparter <equinox@diac24.net>
Fri, 13 Dec 2019 05:20:03 +0000 (06:20 +0100)
committerDavid Lamparter <equinox@diac24.net>
Tue, 14 Jul 2020 09:15:53 +0000 (11:15 +0200)
Signed-off-by: David Lamparter <equinox@diac24.net>
lib/sockunion.c
lib/sockunion.h

index 63d8a8c69b2b7f824ec1987e4ff631249e25dad0..16df03847a65d1d54fb45d031c5b214731a8b08d 100644 (file)
@@ -27,6 +27,7 @@
 #include "log.h"
 #include "jhash.h"
 #include "lib_errors.h"
+#include "printfrr.h"
 
 DEFINE_MTYPE_STATIC(LIB, SOCKUNION, "Socket union")
 
@@ -666,3 +667,49 @@ void sockunion_init(union sockunion *su)
 {
        memset(su, 0, sizeof(union sockunion));
 }
+
+printfrr_ext_autoreg_p("SU", printfrr_psu)
+static ssize_t printfrr_psu(char *buf, size_t bsz, const char *fmt,
+                           int prec, const void *ptr)
+{
+       const union sockunion *su = ptr;
+       struct fbuf fb = { .buf = buf, .pos = buf, .len = bsz - 1 };
+       bool include_port = false;
+       bool endflags = false;
+       ssize_t consumed = 2;
+
+       while (!endflags) {
+               switch (fmt[consumed++]) {
+               case 'p':
+                       include_port = true;
+                       break;
+               default:
+                       consumed--;
+                       endflags = true;
+                       break;
+               }
+       };
+
+       switch (sockunion_family(su)) {
+       case AF_UNSPEC:
+               bprintfrr(&fb, "(unspec)");
+               break;
+       case AF_INET:
+               inet_ntop(AF_INET, &su->sin.sin_addr, buf, bsz);
+               fb.pos += strlen(fb.buf);
+               if (include_port)
+                       bprintfrr(&fb, ":%d", su->sin.sin_port);
+               break;
+       case AF_INET6:
+               inet_ntop(AF_INET6, &su->sin6.sin6_addr, buf, bsz);
+               fb.pos += strlen(fb.buf);
+               if (include_port)
+                       bprintfrr(&fb, ":%d", su->sin6.sin6_port);
+               break;
+       default:
+               bprintfrr(&fb, "(af %d)", sockunion_family(su));
+       }
+
+       fb.pos[0] = '\0';
+       return consumed;
+}
index 7091c1b5e78b5d95e03fb86d69703ef5d2d0dc05..72f12b77ca3b797710834ea95a66aa19e0e2d554 100644 (file)
@@ -103,6 +103,10 @@ extern union sockunion *sockunion_dup(const union sockunion *);
 extern void sockunion_free(union sockunion *);
 extern void sockunion_init(union sockunion *);
 
+#ifdef _FRR_ATTRIBUTE_PRINTFRR
+#pragma FRR printfrr_ext "%pSU"  (union sockunion *)
+#endif
+
 #ifdef __cplusplus
 }
 #endif