}
}
+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);
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");
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);
}
}
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);
}
}
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);
}
{
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);
}
}
{
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);
}
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);
}
{
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);
}
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);
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;
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);
}
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);
}
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);
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)
} 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)
{
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;
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);
}
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;
}
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;
}
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)
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()