]> git.proxmox.com Git - mirror_frr.git/blobdiff - isisd/isis_misc.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / isisd / isis_misc.c
index 16c789ff5993875dc33263803747ea1d77e9330c..c24917454a8556bb7897cf87fbf15b9ebc2f4039 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"
@@ -38,7 +39,6 @@
 #include "isisd/isisd.h"
 #include "isisd/isis_misc.h"
 
-#include "isisd/isis_tlv.h"
 #include "isisd/isis_lsp.h"
 #include "isisd/isis_constants.h"
 #include "isisd/isis_adjacency.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];
@@ -62,7 +56,7 @@ char nlpidstring[30];
 /*
  * This converts the isonet to its printable format
  */
-const char *isonet_print(const u_char *from, int len)
+const char *isonet_print(const uint8_t *from, int len)
 {
        int i = 0;
        char *pos = isonet;
@@ -93,11 +87,11 @@ const char *isonet_print(const u_char *from, int len)
  * Returns 0 on error, length of buff on ok
  * extract dot from the dotted str, and insert all the number in a buff
  */
-int dotformat2buff(u_char *buff, const char *dotted)
+int dotformat2buff(uint8_t *buff, const char *dotted)
 {
        int dotlen, len = 0;
        const char *pos = dotted;
-       u_char number[3];
+       uint8_t number[3];
        int nextdotpos = 2;
 
        number[2] = '\0';
@@ -142,11 +136,11 @@ int dotformat2buff(u_char *buff, const char *dotted)
 /*
  * conversion of XXXX.XXXX.XXXX to memory
  */
-int sysid2buff(u_char *buff, const char *dotted)
+int sysid2buff(uint8_t *buff, const char *dotted)
 {
        int len = 0;
        const char *pos = dotted;
-       u_char number[3];
+       uint8_t number[3];
 
        number[2] = '\0';
        // surely not a sysid_string if not 14 length
@@ -179,6 +173,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 +204,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, ", ");
        }
@@ -219,25 +214,6 @@ char *nlpid2string(struct nlpids *nlpids)
        return nlpidstring;
 }
 
-/*
- *  supports the given af ?
- */
-int speaks(struct nlpids *nlpids, int family)
-{
-       int i, speaks = 0;
-
-       if (nlpids == (struct nlpids *)NULL)
-               return speaks;
-       for (i = 0; i < nlpids->count; i++) {
-               if (family == AF_INET && nlpids->nlpids[i] == NLPID_IP)
-                       speaks = 1;
-               if (family == AF_INET6 && nlpids->nlpids[i] == NLPID_IPV6)
-                       speaks = 1;
-       }
-
-       return speaks;
-}
-
 /*
  * Returns 0 on error, IS-IS Circuit Type on ok
  */
@@ -328,79 +304,61 @@ const char *syst2string(int type)
 /*
  * Print functions - we print to static vars
  */
-const char *snpa_print(const u_char *from)
+const char *snpa_print(const uint8_t *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 uint8_t *from)
+{
+       return isis_format_id(from, ISIS_SYS_ID_LEN);
+}
 
-       return snpa;
+const char *rawlspid_print(const uint8_t *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)
+const char *time2string(uint32_t time)
 {
        char *pos = datestring;
-       u_int32_t rest;
+       uint32_t rest;
 
        if (time == 0)
                return "-";
@@ -463,7 +421,7 @@ unsigned long isis_jitter(unsigned long timer, unsigned long jitter)
        return timer;
 }
 
-struct in_addr newprefix2inaddr(u_char *prefix_start, u_char prefix_masklen)
+struct in_addr newprefix2inaddr(uint8_t *prefix_start, uint8_t prefix_masklen)
 {
        memset(&new_prefix, 0, sizeof(new_prefix));
        memcpy(&new_prefix, prefix_start,
@@ -473,29 +431,11 @@ struct in_addr newprefix2inaddr(u_char *prefix_start, u_char prefix_masklen)
        return new_prefix;
 }
 
-/*
- * Returns host.name if any, otherwise
- * it returns the system hostname.
- */
-const char *unix_hostname(void)
-{
-       static struct utsname names;
-       const char *hostname;
-
-       hostname = host.name;
-       if (!hostname) {
-               uname(&names);
-               hostname = names.nodename;
-       }
-
-       return hostname;
-}
-
 /*
  * Returns the dynamic hostname associated with the passed system ID.
  * If no dynamic hostname found then returns formatted system ID.
  */
-const char *print_sys_hostname(const u_char *sysid)
+const char *print_sys_hostname(const uint8_t *sysid)
 {
        struct isis_dynhn *dyn;
 
@@ -504,11 +444,11 @@ const char *print_sys_hostname(const u_char *sysid)
 
        /* For our system ID return our host name */
        if (memcmp(sysid, isis->sysid, ISIS_SYS_ID_LEN) == 0)
-               return unix_hostname();
+               return cmd_hostname_get();
 
        dyn = dynhn_find_by_id(sysid);
        if (dyn)
-               return (const char *)dyn->name.name;
+               return dyn->hostname;
 
        return sysid_print(sysid);
 }
@@ -572,3 +512,93 @@ 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);
+}
+
+void vty_out_timestr(struct vty *vty, time_t uptime)
+{
+       struct tm *tm;
+       time_t difftime = time(NULL);
+       difftime -= uptime;
+       tm = gmtime(&difftime);
+
+       if (difftime < ONE_DAY_SECOND)
+               vty_out(vty, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
+                       tm->tm_sec);
+       else if (difftime < ONE_WEEK_SECOND)
+               vty_out(vty, "%dd%02dh%02dm", tm->tm_yday, tm->tm_hour,
+                       tm->tm_min);
+       else
+               vty_out(vty, "%02dw%dd%02dh", tm->tm_yday / 7,
+                       tm->tm_yday - ((tm->tm_yday / 7) * 7), tm->tm_hour);
+       vty_out(vty, " ago");
+}