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 | |
| parent | 93d08879ad18f820ba00181aaf9a320471d3f7a8 (diff) | |
| parent | 6894924238aa70b7031df3bf77548f1857342217 (diff) | |
Merge pull request #6732 from opensourcerouting/printfrr-prep
*: preparations for printfrr coccinelle run
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/ipaddr.h | 18 | ||||
| -rw-r--r-- | lib/prefix.c | 21 | ||||
| -rw-r--r-- | lib/prefix.h | 2 | ||||
| -rw-r--r-- | lib/sockunion.c | 47 | ||||
| -rw-r--r-- | lib/sockunion.h | 4 |
5 files changed, 83 insertions, 9 deletions
diff --git a/lib/ipaddr.h b/lib/ipaddr.h index cd7f79a04e..a96c9788bc 100644 --- a/lib/ipaddr.h +++ b/lib/ipaddr.h @@ -33,9 +33,9 @@ extern "C" { * Generic IP address - union of IPv4 and IPv6 address. */ enum ipaddr_type_t { - IPADDR_NONE = 0, - IPADDR_V4 = 1, /* IPv4 */ - IPADDR_V6 = 2, /* IPv6 */ + IPADDR_NONE = AF_UNSPEC, + IPADDR_V4 = AF_INET, + IPADDR_V6 = AF_INET6, }; struct ipaddr { @@ -84,12 +84,8 @@ static inline int str2ipaddr(const char *str, struct ipaddr *ip) static inline char *ipaddr2str(const struct ipaddr *ip, char *buf, int size) { buf[0] = '\0'; - if (ip) { - if (IS_IPADDR_V4(ip)) - inet_ntop(AF_INET, &ip->ip.addr, buf, size); - else if (IS_IPADDR_V6(ip)) - inet_ntop(AF_INET6, &ip->ip.addr, buf, size); - } + if (ip) + inet_ntop(ip->ipa_type, &ip->ip.addr, buf, size); return buf; } @@ -128,6 +124,10 @@ static inline bool ipaddr_isset(struct ipaddr *ip) return (0 != memcmp(&a, ip, sizeof(struct ipaddr))); } +#ifdef _FRR_ATTRIBUTE_PRINTFRR +#pragma FRR printfrr_ext "%pIA" (struct ipaddr *) +#endif + #ifdef __cplusplus } #endif diff --git a/lib/prefix.c b/lib/prefix.c index 0900100be3..697e1a6239 100644 --- a/lib/prefix.c +++ b/lib/prefix.c @@ -22,6 +22,7 @@ #include <zebra.h> #include "prefix.h" +#include "ipaddr.h" #include "vty.h" #include "sockunion.h" #include "memory.h" @@ -1316,6 +1317,26 @@ char *esi_to_str(const esi_t *esi, char *buf, int size) return ptr; } +printfrr_ext_autoreg_p("EA", printfrr_ea) +static ssize_t printfrr_ea(char *buf, size_t bsz, const char *fmt, + int prec, const void *ptr) +{ + const struct ethaddr *mac = ptr; + + prefix_mac2str(mac, buf, bsz); + return 2; +} + +printfrr_ext_autoreg_p("IA", printfrr_ia) +static ssize_t printfrr_ia(char *buf, size_t bsz, const char *fmt, + int prec, const void *ptr) +{ + const struct ipaddr *ipa = ptr; + + ipaddr2str(ipa, buf, bsz); + return 2; +} + printfrr_ext_autoreg_p("I4", printfrr_i4) static ssize_t printfrr_i4(char *buf, size_t bsz, const char *fmt, int prec, const void *ptr) diff --git a/lib/prefix.h b/lib/prefix.h index 0bd457cc23..53e9dc3cb3 100644 --- a/lib/prefix.h +++ b/lib/prefix.h @@ -555,6 +555,8 @@ static inline int is_default_host_route(const struct prefix *p) } #ifdef _FRR_ATTRIBUTE_PRINTFRR +#pragma FRR printfrr_ext "%pEA" (struct ethaddr *) + #pragma FRR printfrr_ext "%pI4" (struct in_addr *) #pragma FRR printfrr_ext "%pI4" (in_addr_t *) 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; +} 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 |
