]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
cbor ip addresses stored with 52/54 tag, but without yang changes
authorKaterina Kubecova <katerina.kubecova@nic.cz>
Thu, 7 Dec 2023 09:28:44 +0000 (10:28 +0100)
committerKaterina Kubecova <katerina.kubecova@nic.cz>
Thu, 7 Dec 2023 09:28:44 +0000 (10:28 +0100)
nest/cbor.c
nest/cbor_cmds.c
nest/cbor_shortcuts.c
proto/ospf/ospf_for_cbor.c
yang/birdc.py

index fc813a93faa2c3f71734f7fc6e6d816fdda2258c..47d2c48fc427497fae8cace4e483ef3adcf5f332 100644 (file)
@@ -67,6 +67,59 @@ void cbor_add_int(struct cbor_writer *writer, int64_t item)
   }
 }
 
+void cbor_add_ipv4(struct cbor_writer *writer, u32 addr)
+{
+  write_item(writer, 6, 52); // 6 is TAG, 52 is tag number for ipv4
+  write_item(writer, 2, 4); // bytestring of length 4
+  for (int i = 3; i>=0; i--)
+  {
+    writer->cbor[writer->pt] = (addr>>(i*8)) & 0xff;
+    writer->pt++;
+  }
+}
+
+void cbor_add_ipv6(struct cbor_writer *writer, u64 addr)
+{
+  write_item(writer, 6, 54); // 6 is TAG, 54 is tag number for ipv6
+  write_item(writer, 2, 8); // bytestring of length 8
+  for (int i = 7; i>=0; i--)
+  {
+    writer->cbor[writer->pt] = (addr>>(i*8)) & 0xff;
+    writer->pt++;
+  }
+}
+
+void cbor_add_ipv4_prefix(struct cbor_writer *writer, u32 addr, int prefix)
+{
+  write_item(writer, 6, 52); // 6 is TAG, 52 is tag number for ipv4
+  cbor_open_block_with_length(writer, 2);
+  cbor_add_int(writer, prefix);
+  write_item(writer, 2, 4); // bytestring of length 4
+  for (int i = 3; i>=0; i--)
+  {
+    writer->cbor[writer->pt] = (addr>>(i*8)) & 0xff;
+    writer->pt++;
+  }
+}
+
+
+void cbor_add_ipv6_prefix(struct cbor_writer *writer, struct ip6_addr addr, int prefix)
+{
+  write_item(writer, 6, 54); // 6 is TAG, 54 is tag number for ipv6
+  cbor_open_block_with_length(writer, 2);
+  cbor_add_int(writer, prefix);
+  write_item(writer, 2, 8); // bytestring of length 4
+  for (int j = 0; j < 4; j++)
+  {
+    for (int i = 3; i>=0; i--)
+    {
+      writer->cbor[writer->pt] = (addr.addr[j]>>(i*8)) & 0xff;
+      writer->pt++;
+    }
+  }
+}
+
+
 void cbor_add_uint(struct cbor_writer *writer, u64 item)
 {
   write_item(writer, 0, item);
index 01a508c180444591f731995fcf2c25399086b519..858b77167428df36c6da1e22951cdd03bf671c73 100644 (file)
@@ -85,7 +85,7 @@ cmd_show_status_cbor(byte *tbuf, uint capacity, struct linpool *lp)
   cbor_string_string(w, "version", BIRD_VERSION);
   cbor_add_string(w, "body");
   cbor_open_block(w);
-  cbor_string_int(w, "router_id", config->router_id);
+  cbor_string_ipv4(w, "router_id", config->router_id);
   cbor_string_string(w, "hostname", config->hostname);
   cbor_string_int(w, "server_time", preprocess_time(current_time()));
   cbor_string_int(w, "last_reboot", preprocess_time(boot_time));
index 15adf4604873ec32b786eefa318b70ec127b22e2..db10da5e3a3f79fa0c4c646b74074cefb2b89411 100644 (file)
@@ -19,6 +19,16 @@ void cbor_string_uint(struct cbor_writer *writer, char *key, u64 value) {
   cbor_add_uint(writer, value);
 }
 
+void cbor_string_ipv4(struct cbor_writer *writer, char *key, u32 value) {
+  cbor_add_string(writer, key);
+  cbor_add_ipv4(writer, value);
+}
+
+void cbor_string_ipv6(struct cbor_writer *writer, char *key, u64 value) {
+  cbor_add_string(writer, key);
+  cbor_add_ipv6(writer, value);
+}
+
 void cbor_named_block_two_ints(struct cbor_writer *writer, char *key, char *name1, int val1, char *name2, int val2) {
   cbor_add_string(writer, key);
   cbor_open_block_with_length(writer, 2);
@@ -37,3 +47,25 @@ void cbor_write_to_file(struct cbor_writer *writer, char *filename) {
   fclose(write_ptr);
 }
 
+void cbor_add_net(struct cbor_writer *writer, const net_addr *N) {
+  // Original switch comes from lib/net.c and contains more cases.
+  net_addr_union *n = (void *) N;
+
+  switch (n->n.type)
+  {
+  case NET_IP4:
+    cbor_add_ipv4_prefix(writer, n->ip4.prefix, n->ip4.pxlen);
+    return;
+  case NET_IP6:
+    cbor_add_ipv6_prefix(writer, n->ip6.prefix, n->ip6.pxlen);
+    return;
+  case NET_VPN4:
+    cbor_add_ipv4_prefix(writer, n->vpn4.prefix, n->vpn4.pxlen);
+    return;
+  case NET_VPN6:
+    cbor_add_ipv6_prefix(writer, n->vpn6.prefix, n->vpn6.pxlen);
+    return;
+  default:
+    bug("net type unsupported by cbor (yet)."); 
+  }
+}
index 9c8d19845964d3f9a493d516ecb3e5cee304c78a..354285485414354d80abb4201b3b7ca2d50cb8c1 100644 (file)
@@ -24,7 +24,7 @@ show_lsa_router_cbor(struct cbor_writer *w, struct ospf_proto *p, struct top_has
 
   cbor_add_string(w, "lsa_router");
   cbor_open_block(w);
-  cbor_string_int(w, "router", he->lsa.rt);
+  cbor_string_ipv4(w, "router", he->lsa.rt);
   show_lsa_distance_cbor(w, he);
 
   cbor_add_string(w, "vlink");
@@ -35,7 +35,7 @@ show_lsa_router_cbor(struct cbor_writer *w, struct ospf_proto *p, struct top_has
     if (rtl.type == LSART_VLNK)
     {
       cbor_open_block_with_length(w, 2);
-      cbor_string_int(w, "vlink", rtl.id);
+      cbor_string_ipv4(w, "vlink", rtl.id);
       cbor_string_int(w, "metric", rtl.metric);
     }
   }
@@ -49,7 +49,7 @@ show_lsa_router_cbor(struct cbor_writer *w, struct ospf_proto *p, struct top_has
     if (rtl.type == LSART_PTP)
     {
       cbor_open_block_with_length(w, 2);
-      cbor_string_int(w, "router", rtl.id);
+      cbor_string_ipv4(w, "router", rtl.id);
       cbor_string_int(w, "metric", rtl.metric);
     }
   }
@@ -75,7 +75,7 @@ show_lsa_router_cbor(struct cbor_writer *w, struct ospf_proto *p, struct top_has
 
           cbor_open_block_with_length(w, 4);
           cbor_string_int(w, "dummy_yang_id", dummy_id);
-          cbor_string_int(w, "network", net_lsa->id & net_ln->optx);
+          cbor_string_ipv4(w, "network", net_lsa->id & net_ln->optx);
           cbor_string_int(w, "len", u32_masklen(net_ln->optx));
           cbor_string_int(w, "metric", rtl.metric);
        }
@@ -83,7 +83,7 @@ show_lsa_router_cbor(struct cbor_writer *w, struct ospf_proto *p, struct top_has
        {
          cbor_open_block_with_length(w, 3);
          cbor_string_int(w, "dummy_yang_id", dummy_id);
-          cbor_string_int(w, "network", rtl.id);
+          cbor_string_ipv4(w, "network", rtl.id);
           cbor_string_int(w, "metric", rtl.metric);
         }
       }
@@ -91,7 +91,7 @@ show_lsa_router_cbor(struct cbor_writer *w, struct ospf_proto *p, struct top_has
       {
         cbor_open_block_with_length(w, 4);
         cbor_string_int(w, "dummy_yang_id", dummy_id);
-        cbor_string_int(w, "network", rtl.id);
+        cbor_string_ipv4(w, "network", rtl.id);
         cbor_string_int(w, "nif", rtl.nif);
         cbor_string_int(w, "metric", rtl.metric);
       }
@@ -110,7 +110,7 @@ show_lsa_router_cbor(struct cbor_writer *w, struct ospf_proto *p, struct top_has
       if (rtl.type == LSART_STUB)
       {
         cbor_open_block_with_length(w, 3);
-        cbor_string_int(w, "stubnet", rtl.id);
+        cbor_string_ipv4(w, "stubnet", rtl.id);
         cbor_string_int(w, "len", u32_masklen(rtl.data));
         cbor_string_int(w, "metric", rtl.metric);
       }
@@ -133,15 +133,15 @@ show_lsa_network_cbor(struct cbor_writer *w, struct top_hash_entry *he, int ospf
   {
     cbor_add_string(w, "ospf2");
     cbor_open_block_with_length(w, 3);
-    cbor_string_int(w, "network", lsa->id & ln->optx);
+    cbor_string_ipv4(w, "network", lsa->id & ln->optx);
     cbor_string_int(w, "optx", u32_masklen(ln->optx));
-    cbor_string_int(w, "dr", lsa->rt);
+    cbor_string_ipv4(w, "dr", lsa->rt);
   }
   else
   {
     cbor_add_string(w, "ospf");
     cbor_open_block_with_length(w, 2);
-    cbor_string_int(w, "network", lsa->rt);
+    cbor_string_ipv4(w, "network", lsa->rt);
     cbor_string_int(w, "lsa_id", lsa->id);
   }
 
@@ -152,7 +152,7 @@ show_lsa_network_cbor(struct cbor_writer *w, struct top_hash_entry *he, int ospf
   for (i = 0; i < lsa_net_count(lsa); i++)
   {
     cbor_open_block_with_length(w, 1);
-    cbor_string_int(w, "router", ln->routers[i]);
+    cbor_string_ipv4(w, "router", ln->routers[i]);
   }
 
   cbor_close_block_or_list(w);
@@ -161,7 +161,6 @@ show_lsa_network_cbor(struct cbor_writer *w, struct top_hash_entry *he, int ospf
 static inline void
 show_lsa_sum_net_cbor(struct cbor_writer *w, struct top_hash_entry *he, int ospf2, int af)
 {
-  char str[IPA_MAX_TEXT_LENGTH + 8] = "";
   net_addr net;
   u8 pxopts;
   u32 metric;
@@ -169,8 +168,8 @@ show_lsa_sum_net_cbor(struct cbor_writer *w, struct top_hash_entry *he, int ospf
   lsa_parse_sum_net(he, ospf2, af, &net, &pxopts, &metric);
   cbor_add_string(w, "lsa_sum_net");
   cbor_open_block_with_length(w, 2);
-  bsprintf(str, "%N", &net);
-  cbor_string_string(w, "net", str);
+  cbor_add_string(w, "net");
+  cbor_add_net(w, &net);
   cbor_string_int(w, "metric", metric);
 }
 
@@ -185,7 +184,7 @@ show_lsa_sum_rt_cbor(struct cbor_writer *w, struct top_hash_entry *he, int ospf2
 
   cbor_add_string(w, "lsa_sum_rt");
   cbor_open_block_with_length(w, 2);
-  cbor_string_int(w, "router", dst_rid);
+  cbor_string_ipv4(w, "router", dst_rid);
   cbor_string_int(w, "metric", metric);
 }
 
@@ -193,7 +192,6 @@ static inline void
 show_lsa_external_cbor(struct cbor_writer *w, struct top_hash_entry *he, int ospf2, int af)
 {
   struct ospf_lsa_ext_local rt;
-  char str[IPA_MAX_TEXT_LENGTH + 8] = "";
 
   cbor_add_string(w, "lsa_external");
   cbor_open_block(w);
@@ -204,8 +202,7 @@ show_lsa_external_cbor(struct cbor_writer *w, struct top_hash_entry *he, int osp
 
   if (rt.fbit)
   {
-    bsprintf(str, "%N", rt.fwaddr);
-    cbor_string_string(w, "via", str);
+    cbor_string_ipv4(w, "via", rt.fwaddr.addr[0]);
   }
 
   if (rt.tag)
@@ -217,9 +214,8 @@ show_lsa_external_cbor(struct cbor_writer *w, struct top_hash_entry *he, int osp
   } else {
     cbor_string_string(w, "lsa_type", "external");
   }
-
-  bsprintf(str, "%N", rt.net);
-  cbor_string_string(w, "rt_net", str);
+  cbor_add_string(w, "rt_net");
+  cbor_add_net(w, &rt.net);
 
   if(rt.ebit)
   {
@@ -262,7 +258,6 @@ show_lsa_prefix_cbor(struct cbor_writer *w, struct top_hash_entry *he, struct to
 
   cbor_add_string(w, "prefixes");
   cbor_open_list(w);
-  char str[IPA_MAX_TEXT_LENGTH + 8] = "";
   for (i = 0; i < px->pxcount; i++)
   {
     net_addr net;
@@ -275,13 +270,13 @@ show_lsa_prefix_cbor(struct cbor_writer *w, struct top_hash_entry *he, struct to
 
     if (px->ref_type == LSA_T_RT)
     {
-      bsprintf(str, "%N", &net);
-      cbor_string_string(w, "stubnet", str);
+      cbor_add_string(w, "stubnet");
+      cbor_add_net(w, &net);
       cbor_string_int(w, "metric", metric);
     }
     else{
-      bsprintf(str, "%N", &net);
-      cbor_string_string(w, "stubnet", str);
+      cbor_add_string(w, "stubnet");
+      cbor_add_net(w, &net);
     }
     cbor_close_block_or_list(w);
   }
@@ -521,7 +516,7 @@ ospf_sh_state_cbor(struct cbor_writer *w, struct proto *P, int verbose, int reac
 
        if (he->domain != last_area)
        {
-         cbor_string_int(w, "area", he->domain);
+         cbor_string_ipv4(w, "area", he->domain);
          last_area = he->domain;
          ix = 0;
        }
@@ -605,7 +600,7 @@ ospf_sh_state_cbor(struct cbor_writer *w, struct proto *P, int verbose, int reac
 
       if (he->lsa.rt != last_rt)
       {
-       cbor_string_int(w, "router", he->lsa.rt);
+       cbor_string_ipv4(w, "router", he->lsa.rt);
        last_rt = he->lsa.rt;
       }
 
index 6b01d31d979b3c9986f8a94e60757a6e8fab2f58..46e7cda2910fea92b82a0c93fc082dee5d721674 100644 (file)
@@ -9,8 +9,13 @@ import datetime
 class Command:
     num = -1
     def addr_to_str(self, addr):
-        return socket.inet_ntoa(struct.pack('!L', addr))
-    
+        return '.'.join(str(c) for c in addr.value)
+
+    def prefix_to_str(self, addr):
+        str_addr = '.'.join(str(c) for c in addr.value[1])
+        str_addr = str_addr + "/" +addr.value[0]
+        return str_addr
+
     def print_answer(self, answer):
         print(answer)
 
@@ -103,21 +108,21 @@ class Ospf(Command):
             print(f"\t\trouter {self.addr_to_str(router['router'])}")
 
     def print_lsa_sum_net(self, area):
-        print(f"\t\txnetwork {area['net']} metric {area['metric']}")
+        print(f"\t\txnetwork {self.prefix_to_str(area['net'])} metric {area['metric']}")
 
     def print_lsa_sum_rt(self, area):
         print(f"\t\txrouter {self.addr_to_str(area['router'])} metric {area['metric']}")
 
     def print_lsa_external(self, area):
         if('lsa_type_num' in area.keys()):
-            print(f"\t\t{area['lsa_type']} {self.addr_to_str(area['rt_net'])} metric{area[lsa_type_num]} {area['metric']}%s%s")
+            print(f"\t\t{area['lsa_type']} {self.prefix_to_str(area['rt_net'])} metric{area[lsa_type_num]} {area['metric']}%s%s")
         else:
-            print(f"\t\t{area['lsa_type']} {self.addr_to_str(area['rt_net'])} metric {area['metric']}{area['via']}{area['tag']}")
+            print(f"\t\t{area['lsa_type']} {self.prefix_to_str(area['rt_net'])} metric {area['metric']}{area['via']}{area['tag']}")
 
     def print_lsa_prefix(self, area):
         for prefix in area['prefixes']:
             if 'metric' in prefix.keys():
-                print(f"\t\tstubnet {self.addr_to_str(prefix['stubnet'])} metric {prefix['metric']}")
+                print(f"\t\tstubnet {self.prefix_to_str(prefix['stubnet'])} metric {prefix['metric']}")
 
     def print_answer(self, answer):
         print()