diff options
| author | Quentin Young <qlyoung@users.noreply.github.com> | 2020-07-29 14:29:34 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-29 14:29:34 -0400 |
| commit | 25ee44b52278c86fda27f76e932f6a12c375bf10 (patch) | |
| tree | bea9a0e54e73dafb08400f7e2e435d5be7fa1e93 /lib/sockunion.c | |
| parent | 93d08879ad18f820ba00181aaf9a320471d3f7a8 (diff) | |
| parent | 6894924238aa70b7031df3bf77548f1857342217 (diff) | |
Merge pull request #6732 from opensourcerouting/printfrr-prep
*: preparations for printfrr coccinelle run
Diffstat (limited to 'lib/sockunion.c')
| -rw-r--r-- | lib/sockunion.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/sockunion.c b/lib/sockunion.c index 0e7483bfbe..d77229797c 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") @@ -665,3 +666,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; +} |
