]> git.puffer.fish Git - mirror/frr.git/commitdiff
isisd: add formatter code
authorChristian Franke <chris@opensourcerouting.org>
Sat, 22 Jul 2017 11:57:05 +0000 (13:57 +0200)
committerChristian Franke <chris@opensourcerouting.org>
Thu, 3 Aug 2017 09:34:04 +0000 (11:34 +0200)
Signed-off-by: Christian Franke <chris@opensourcerouting.org>
isisd/isis_misc.c
isisd/isis_misc.h

index 16c789ff5993875dc33263803747ea1d77e9330c..cf6558c13ad6f85c4ff318106445831942d8874b 100644 (file)
@@ -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"
 
 /* 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);
+}
index 12ab0fac1ca849585f5750ab3233f28cbf2227c2..0ff33f831d8e85314965e66c5e0eec8a148e7b20 100644 (file)
@@ -44,7 +44,9 @@ const char *isonet_print(const u_char *, int len);
 const char *sysid_print(const u_char *);
 const char *snpa_print(const u_char *);
 const char *rawlspid_print(const u_char *);
+const char *isis_format_id(const uint8_t *id, size_t len);
 const char *time2string(u_int32_t);
+const char *nlpid2str(uint8_t nlpid);
 /* typedef struct nlpids nlpids; */
 char *nlpid2string(struct nlpids *);
 const char *print_sys_hostname(const u_char *sysid);
@@ -77,4 +79,10 @@ enum { ISIS_UI_LEVEL_BRIEF,
        ISIS_UI_LEVEL_EXTENSIVE,
 };
 
+#include "lib/log.h"
+void log_multiline(int priority, const char *prefix, const char *format, ...)
+       PRINTF_ATTRIBUTE(3, 4);
+struct vty;
+void vty_multiline(struct vty *vty, const char *prefix, const char *format, ...)
+       PRINTF_ATTRIBUTE(3, 4);
 #endif