diff options
author | Christian Franke <chris@opensourcerouting.org> | 2017-07-22 13:57:05 +0200 |
---|---|---|
committer | Christian Franke <chris@opensourcerouting.org> | 2017-08-03 11:34:04 +0200 |
commit | 9879e291a4e50cff67d5ad1476f14b3c8e246d8d (patch) | |
tree | 7ad3b27f9d13ef82a5a6c3c0f83087cf074628aa /isisd/isis_misc.c | |
parent | lib: add sbuf (diff) | |
download | frr-9879e291a4e50cff67d5ad1476f14b3c8e246d8d.tar.xz frr-9879e291a4e50cff67d5ad1476f14b3c8e246d8d.zip |
isisd: add formatter code
Signed-off-by: Christian Franke <chris@opensourcerouting.org>
Diffstat (limited to 'isisd/isis_misc.c')
-rw-r--r-- | isisd/isis_misc.c | 209 |
1 files changed, 129 insertions, 80 deletions
diff --git a/isisd/isis_misc.c b/isisd/isis_misc.c index 16c789ff5..cf6558c13 100644 --- a/isisd/isis_misc.c +++ b/isisd/isis_misc.c @@ -28,6 +28,7 @@ #include "hash.h" #include "if.h" #include "command.h" +#include "log_int.h" #include "isisd/dict.h" #include "isisd/isis_constants.h" @@ -46,15 +47,9 @@ /* staticly assigned vars for printing purposes */ struct in_addr new_prefix; -/* len of xxxx.xxxx.xxxx + place for #0 termination */ -char sysid[15]; -/* len of xxxx.xxxx.xxxx + place for #0 termination */ -char snpa[15]; /* len of xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx */ -char isonet[51]; /* + place for #0 termination */ -/* len of xxxx.xxxx.xxxx.xx.xx + place for #0 termination */ -char lspid[21]; +char isonet[51]; /* len of xxYxxMxWxdxxhxxmxxs + place for #0 termination */ char datestring[20]; char nlpidstring[30]; @@ -179,6 +174,26 @@ int sysid2buff(u_char *buff, const char *dotted) return len; } +const char *nlpid2str(uint8_t nlpid) +{ + static char buf[4]; + switch (nlpid) { + case NLPID_IP: + return "IPv4"; + case NLPID_IPV6: + return "IPv6"; + case NLPID_SNAP: + return "SNAP"; + case NLPID_CLNP: + return "CLNP"; + case NLPID_ESIS: + return "ES-IS"; + default: + snprintf(buf, sizeof(buf), "%" PRIu8, nlpid); + return buf; + } +} + /* * converts the nlpids struct (filled by TLV #129) * into a string @@ -190,26 +205,7 @@ char *nlpid2string(struct nlpids *nlpids) int i; for (i = 0; i < nlpids->count; i++) { - switch (nlpids->nlpids[i]) { - case NLPID_IP: - pos += sprintf(pos, "IPv4"); - break; - case NLPID_IPV6: - pos += sprintf(pos, "IPv6"); - break; - case NLPID_SNAP: - pos += sprintf(pos, "SNAP"); - break; - case NLPID_CLNP: - pos += sprintf(pos, "CLNP"); - break; - case NLPID_ESIS: - pos += sprintf(pos, "ES-IS"); - break; - default: - pos += sprintf(pos, "unknown"); - break; - } + pos += sprintf(pos, "%s", nlpid2str(nlpids->nlpids[i])); if (nlpids->count - i > 1) pos += sprintf(pos, ", "); } @@ -330,71 +326,53 @@ const char *syst2string(int type) */ const char *snpa_print(const u_char *from) { - int i = 0; - u_char *pos = (u_char *)snpa; - - if (!from) - return "unknown"; - - while (i < ETH_ALEN - 1) { - if (i & 1) { - sprintf((char *)pos, "%02x.", *(from + i)); - pos += 3; - } else { - sprintf((char *)pos, "%02x", *(from + i)); - pos += 2; - } - i++; - } + return isis_format_id(from, ISIS_SYS_ID_LEN); +} - sprintf((char *)pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1))); - pos += 2; - *(pos) = '\0'; +const char *sysid_print(const u_char *from) +{ + return isis_format_id(from, ISIS_SYS_ID_LEN); +} - return snpa; +const char *rawlspid_print(const u_char *from) +{ + return isis_format_id(from, 8); } -const char *sysid_print(const u_char *from) +#define FORMAT_ID_SIZE sizeof("0000.0000.0000.00-00") +const char *isis_format_id(const uint8_t *id, size_t len) { - int i = 0; - char *pos = sysid; +#define FORMAT_BUF_COUNT 4 + static char buf_ring[FORMAT_BUF_COUNT][FORMAT_ID_SIZE]; + static size_t cur_buf = 0; - if (!from) - return "unknown"; + char *rv; - while (i < ISIS_SYS_ID_LEN - 1) { - if (i & 1) { - sprintf(pos, "%02x.", *(from + i)); - pos += 3; - } else { - sprintf(pos, "%02x", *(from + i)); - pos += 2; - } - i++; - } + cur_buf++; + if (cur_buf >= FORMAT_BUF_COUNT) + cur_buf = 0; - sprintf(pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1))); - pos += 2; - *(pos) = '\0'; + rv = buf_ring[cur_buf]; - return sysid; -} + if (!id) { + snprintf(rv, FORMAT_ID_SIZE, "unknown"); + return rv; + } -const char *rawlspid_print(const u_char *from) -{ - char *pos = lspid; - if (!from) - return "unknown"; - memcpy(pos, sysid_print(from), 15); - pos += 14; - sprintf(pos, ".%02x", LSP_PSEUDO_ID(from)); - pos += 3; - sprintf(pos, "-%02x", LSP_FRAGMENT(from)); - pos += 3; + if (len < 6) { + snprintf(rv, FORMAT_ID_SIZE, "Short ID"); + return rv; + } - *(pos) = '\0'; + snprintf(rv, FORMAT_ID_SIZE, "%02x%02x.%02x%02x.%02x%02x", id[0], id[1], + id[2], id[3], id[4], id[5]); + + if (len > 6) + snprintf(rv + 14, FORMAT_ID_SIZE - 14, ".%02x", id[6]); + if (len > 7) + snprintf(rv + 17, FORMAT_ID_SIZE - 17, "-%02x", id[7]); - return lspid; + return rv; } const char *time2string(u_int32_t time) @@ -572,3 +550,74 @@ void zlog_dump_data(void *data, int len) zlog_debug("[%8.8s] %-50.50s %s", addrstr, hexstr, charstr); return; } + +static char *qasprintf(const char *format, va_list ap) +{ + va_list aq; + va_copy(aq, ap); + + int size = 0; + char *p = NULL; + + size = vsnprintf(p, size, format, ap); + + if (size < 0) { + va_end(aq); + return NULL; + } + + size++; + p = XMALLOC(MTYPE_TMP, size); + + size = vsnprintf(p, size, format, aq); + va_end(aq); + + if (size < 0) { + XFREE(MTYPE_TMP, p); + return NULL; + } + + return p; +} + +void log_multiline(int priority, const char *prefix, const char *format, ...) +{ + va_list ap; + char *p; + + va_start(ap, format); + p = qasprintf(format, ap); + va_end(ap); + + if (!p) + return; + + char *saveptr = NULL; + for (char *line = strtok_r(p, "\n", &saveptr); line; + line = strtok_r(NULL, "\n", &saveptr)) { + zlog(priority, "%s%s", prefix, line); + } + + XFREE(MTYPE_TMP, p); +} + +void vty_multiline(struct vty *vty, const char *prefix, const char *format, ...) +{ + va_list ap; + char *p; + + va_start(ap, format); + p = qasprintf(format, ap); + va_end(ap); + + if (!p) + return; + + char *saveptr = NULL; + for (char *line = strtok_r(p, "\n", &saveptr); line; + line = strtok_r(NULL, "\n", &saveptr)) { + vty_out(vty, "%s%s\n", prefix, line); + } + + XFREE(MTYPE_TMP, p); +} |