From: David Lamparter Date: Fri, 13 Dec 2019 05:20:03 +0000 (+0100) Subject: lib: add %pSU for union sockunion * X-Git-Tag: base_7.5~144^2~5 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=02f686fff8820c4a0a91665c5909ba33570bf716;p=matthieu%2Ffrr.git lib: add %pSU for union sockunion * Signed-off-by: David Lamparter --- diff --git a/lib/sockunion.c b/lib/sockunion.c index 63d8a8c69b..16df03847a 100644 --- a/lib/sockunion.c +++ b/lib/sockunion.c @@ -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; +} diff --git a/lib/sockunion.h b/lib/sockunion.h index 7091c1b5e7..72f12b77ca 100644 --- a/lib/sockunion.h +++ b/lib/sockunion.h @@ -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