ospfd: Replace inet_ntop to %pI4/6 for JSON outputs

Signed-off-by: Donatas Abraitis <donatas.abraitis@gmail.com>
This commit is contained in:
Donatas Abraitis 2021-11-18 10:57:40 +02:00
parent e46a4ac958
commit 20308be317
2 changed files with 86 additions and 155 deletions

View File

@ -2674,10 +2674,8 @@ static void show_sr_prefix(struct sbuf *sbuf, struct json_object *json,
srp->nhlfe.label_out); srp->nhlfe.label_out);
json_object_string_add(json_obj, "interface", json_object_string_add(json_obj, "interface",
itf ? itf->name : "-"); itf ? itf->name : "-");
json_object_string_add( json_object_string_addf(json_obj, "nexthop", "%pI4",
json_obj, "nexthop", &srp->nhlfe.nexthop);
inet_ntop(AF_INET, &srp->nhlfe.nexthop,
buf, sizeof(buf)));
json_object_array_add(json_route, json_obj); json_object_array_add(json_route, json_obj);
} else { } else {
sbuf_push(sbuf, 0, "%20s %9s %15s\n", sbuf_push(sbuf, 0, "%20s %9s %15s\n",
@ -2712,10 +2710,8 @@ static void show_sr_prefix(struct sbuf *sbuf, struct json_object *json,
path->srni.label_out); path->srni.label_out);
json_object_string_add(json_obj, "interface", json_object_string_add(json_obj, "interface",
itf ? itf->name : "-"); itf ? itf->name : "-");
json_object_string_add( json_object_string_addf(json_obj, "nexthop", "%pI4",
json_obj, "nexthop", &path->nexthop);
inet_ntop(AF_INET, &path->nexthop,
buf, sizeof(buf)));
json_object_array_add(json_route, json_obj); json_object_array_add(json_route, json_obj);
} else { } else {
sbuf_push(sbuf, indent, "%20s %9s %15s\n", sbuf_push(sbuf, indent, "%20s %9s %15s\n",
@ -2755,9 +2751,8 @@ static void show_sr_node(struct vty *vty, struct json_object *json,
if (json) { if (json) {
json_node = json_object_new_object(); json_node = json_object_new_object();
json_object_string_add(json_node, "routerID", json_object_string_addf(json_node, "routerID", "%pI4",
inet_ntop(AF_INET, &srn->adv_router, &srn->adv_router);
buf, sizeof(buf)));
json_object_int_add(json_node, "srgbSize", json_object_int_add(json_node, "srgbSize",
srn->srgb.range_size); srn->srgb.range_size);
json_object_int_add(json_node, "srgbLabel", json_object_int_add(json_node, "srgbLabel",
@ -2846,10 +2841,8 @@ static void show_sr_node(struct vty *vty, struct json_object *json,
srl->nhlfe[0].label_out); srl->nhlfe[0].label_out);
json_object_string_add(json_obj, "interface", json_object_string_add(json_obj, "interface",
itf ? itf->name : "-"); itf ? itf->name : "-");
json_object_string_add( json_object_string_addf(json_obj, "nexthop", "%pI4",
json_obj, "nexthop", &srl->nhlfe[0].nexthop);
inet_ntop(AF_INET, &srl->nhlfe[0].nexthop,
buf, sizeof(buf)));
json_object_array_add(json_link, json_obj); json_object_array_add(json_link, json_obj);
/* Backup Link */ /* Backup Link */
json_obj = json_object_new_object(); json_obj = json_object_new_object();
@ -2862,10 +2855,8 @@ static void show_sr_node(struct vty *vty, struct json_object *json,
srl->nhlfe[1].label_out); srl->nhlfe[1].label_out);
json_object_string_add(json_obj, "interface", json_object_string_add(json_obj, "interface",
itf ? itf->name : "-"); itf ? itf->name : "-");
json_object_string_add( json_object_string_addf(json_obj, "nexthop", "%pI4",
json_obj, "nexthop", &srl->nhlfe[1].nexthop);
inet_ntop(AF_INET, &srl->nhlfe[1].nexthop,
buf, sizeof(buf)));
json_object_array_add(json_link, json_obj); json_object_array_add(json_link, json_obj);
} else { } else {
sbuf_push(&sbuf, 0, "%18s %21s %20s %9s %15s\n", sbuf_push(&sbuf, 0, "%18s %21s %20s %9s %15s\n",
@ -2937,10 +2928,8 @@ DEFUN (show_ip_opsf_srdb,
if (uj) { if (uj) {
json = json_object_new_object(); json = json_object_new_object();
json_node_array = json_object_new_array(); json_node_array = json_object_new_array();
json_object_string_add( json_object_string_addf(json, "srdbID", "%pI4",
json, "srdbID", &OspfSR.self->adv_router);
inet_ntop(AF_INET, &OspfSR.self->adv_router,
buf, sizeof(buf)));
json_object_object_add(json, "srNodes", json_node_array); json_object_object_add(json, "srNodes", json_node_array);
} else { } else {
vty_out(vty, vty_out(vty,

View File

@ -3068,9 +3068,8 @@ static int show_ip_ospf_common(struct vty *vty, struct ospf *ospf,
/* Show Router ID. */ /* Show Router ID. */
if (json) { if (json) {
json_object_string_add(json_vrf, "routerId", json_object_string_addf(json_vrf, "routerId", "%pI4",
inet_ntop(AF_INET, &ospf->router_id, &ospf->router_id);
buf, sizeof(buf)));
} else { } else {
vty_out(vty, " OSPF Routing Process, Router ID: %pI4\n", vty_out(vty, " OSPF Routing Process, Router ID: %pI4\n",
&ospf->router_id); &ospf->router_id);
@ -3603,11 +3602,9 @@ static void show_ip_ospf_interface_sub(struct vty *vty, struct ospf *ospf,
/* Show OSPF interface information. */ /* Show OSPF interface information. */
if (use_json) { if (use_json) {
json_object_string_add( json_object_string_addf(
json_interface_sub, "ipAddress", json_interface_sub, "ipAddress", "%pI4",
inet_ntop(AF_INET, &oi->address->u.prefix4);
&oi->address->u.prefix4,
buf, sizeof(buf)));
json_object_int_add(json_interface_sub, json_object_int_add(json_interface_sub,
"ipAddressPrefixlen", "ipAddressPrefixlen",
oi->address->prefixlen); oi->address->prefixlen);
@ -3637,17 +3634,13 @@ static void show_ip_ospf_interface_sub(struct vty *vty, struct ospf *ospf,
json_interface_sub, json_interface_sub,
"ospfIfType", dstr); "ospfIfType", dstr);
if (oi->type == OSPF_IFTYPE_VIRTUALLINK) if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
json_object_string_add( json_object_string_addf(
json_interface_sub, json_interface_sub, "vlinkPeer",
"vlinkPeer", "%pI4", &dest);
inet_ntop(AF_INET, &dest,
buf, sizeof(buf)));
else else
json_object_string_add( json_object_string_addf(
json_interface_sub, json_interface_sub,
"localIfUsed", "localIfUsed", "%pI4", &dest);
inet_ntop(AF_INET, &dest,
buf, sizeof(buf)));
} else } else
vty_out(vty, " %s %pI4,", dstr, vty_out(vty, " %s %pI4,", dstr,
&dest); &dest);
@ -3659,10 +3652,8 @@ static void show_ip_ospf_interface_sub(struct vty *vty, struct ospf *ospf,
json_object_boolean_true_add( json_object_boolean_true_add(
json_interface_sub, json_interface_sub,
"mtuMismatchDetect"); "mtuMismatchDetect");
json_object_string_add( json_object_string_addf(json_interface_sub, "routerId",
json_interface_sub, "routerId", "%pI4", &ospf->router_id);
inet_ntop(AF_INET, &ospf->router_id,
buf, sizeof(buf)));
json_object_string_add(json_interface_sub, json_object_string_add(json_interface_sub,
"networkType", "networkType",
ospf_network_type_str[oi->type]); ospf_network_type_str[oi->type]);
@ -3706,17 +3697,13 @@ static void show_ip_ospf_interface_sub(struct vty *vty, struct ospf *ospf,
nbr = ospf_nbr_lookup_by_addr(oi->nbrs, &DR(oi)); nbr = ospf_nbr_lookup_by_addr(oi->nbrs, &DR(oi));
if (nbr) { if (nbr) {
if (use_json) { if (use_json) {
json_object_string_add( json_object_string_addf(
json_interface_sub, "drId", json_interface_sub, "drId",
inet_ntop(AF_INET, "%pI4", &nbr->router_id);
&nbr->router_id, buf, json_object_string_addf(
sizeof(buf)));
json_object_string_add(
json_interface_sub, "drAddress", json_interface_sub, "drAddress",
inet_ntop( "%pI4",
AF_INET, &nbr->address.u.prefix4);
&nbr->address.u.prefix4,
buf, sizeof(buf)));
} else { } else {
vty_out(vty, vty_out(vty,
" Designated Router (ID) %pI4", " Designated Router (ID) %pI4",
@ -3735,18 +3722,13 @@ static void show_ip_ospf_interface_sub(struct vty *vty, struct ospf *ospf,
" No backup designated router on this network\n"); " No backup designated router on this network\n");
} else { } else {
if (use_json) { if (use_json) {
json_object_string_add( json_object_string_addf(
json_interface_sub, "bdrId", json_interface_sub, "bdrId",
inet_ntop(AF_INET, "%pI4", &nbr->router_id);
&nbr->router_id, json_object_string_addf(
buf, sizeof(buf)));
json_object_string_add(
json_interface_sub, json_interface_sub,
"bdrAddress", "bdrAddress", "%pI4",
inet_ntop(AF_INET, &nbr->address.u.prefix4);
&nbr->address.u
.prefix4,
buf, sizeof(buf)));
} else { } else {
vty_out(vty, vty_out(vty,
" Backup Designated Router (ID) %pI4,", " Backup Designated Router (ID) %pI4,",
@ -4447,10 +4429,9 @@ static void show_ip_ospf_neighbor_sub(struct vty *vty,
"deadTimeMsecs", "deadTimeMsecs",
"inactive"); "inactive");
} }
json_object_string_add( json_object_string_addf(json_neighbor,
json_neighbor, "address", "address", "%pI4",
inet_ntop(AF_INET, &nbr->src, &nbr->src);
buf, sizeof(buf)));
json_object_string_add(json_neighbor, json_object_string_add(json_neighbor,
"ifaceName", "ifaceName",
IF_NAME(oi)); IF_NAME(oi));
@ -5038,9 +5019,8 @@ static void show_ip_ospf_nbr_nbma_detail_sub(struct vty *vty,
/* Show interface address. */ /* Show interface address. */
if (use_json) if (use_json)
json_object_string_add(json_sub, "ifaceAddress", json_object_string_addf(json_sub, "ifaceAddress", "%pI4",
inet_ntop(AF_INET, &nbr_nbma->addr, &nbr_nbma->addr);
buf, sizeof(buf)));
else else
vty_out(vty, " interface address %pI4\n", vty_out(vty, " interface address %pI4\n",
&nbr_nbma->addr); &nbr_nbma->addr);
@ -5150,10 +5130,8 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
/* Show interface address. */ /* Show interface address. */
if (use_json) if (use_json)
json_object_string_add(json_neigh, "ifaceAddress", json_object_string_addf(json_neigh, "ifaceAddress", "%pI4",
inet_ntop(AF_INET, &nbr->address.u.prefix4);
&nbr->address.u.prefix4,
buf, sizeof(buf)));
else else
vty_out(vty, " interface address %pI4\n", vty_out(vty, " interface address %pI4\n",
&nbr->address.u.prefix4); &nbr->address.u.prefix4);
@ -5230,17 +5208,15 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
/* Show Designated Rotuer ID. */ /* Show Designated Rotuer ID. */
if (use_json) if (use_json)
json_object_string_add(json_neigh, "routerDesignatedId", json_object_string_addf(json_neigh, "routerDesignatedId",
inet_ntop(AF_INET, &nbr->d_router, "%pI4", &nbr->d_router);
buf, sizeof(buf)));
else else
vty_out(vty, " DR is %pI4,", &nbr->d_router); vty_out(vty, " DR is %pI4,", &nbr->d_router);
/* Show Backup Designated Rotuer ID. */ /* Show Backup Designated Rotuer ID. */
if (use_json) if (use_json)
json_object_string_add(json_neigh, "routerDesignatedBackupId", json_object_string_addf(json_neigh, "routerDesignatedBackupId",
inet_ntop(AF_INET, &nbr->bd_router, "%pI4", &nbr->bd_router);
buf, sizeof(buf)));
else else
vty_out(vty, " BDR is %pI4\n", &nbr->bd_router); vty_out(vty, " BDR is %pI4\n", &nbr->bd_router);
@ -6018,15 +5994,11 @@ static int show_lsa_summary(struct vty *vty, struct ospf_lsa *lsa, int self,
ntohl(lsa->data->ls_seqnum)); ntohl(lsa->data->ls_seqnum));
snprintf(checksum, sizeof(checksum), "%x", snprintf(checksum, sizeof(checksum), "%x",
ntohs(lsa->data->checksum)); ntohs(lsa->data->checksum));
json_object_string_add( json_object_string_addf(json_lsa, "lsId",
json_lsa, "lsId", "%pI4", &lsa->data->id);
inet_ntop(AF_INET, &lsa->data->id, json_object_string_addf(
buf, sizeof(buf))); json_lsa, "advertisedRouter", "%pI4",
json_object_string_add( &lsa->data->adv_router);
json_lsa, "advertisedRouter",
inet_ntop(AF_INET,
&lsa->data->adv_router,
buf, sizeof(buf)));
json_object_int_add(json_lsa, "lsaAge", json_object_int_add(json_lsa, "lsaAge",
LS_AGE(lsa)); LS_AGE(lsa));
json_object_string_add( json_object_string_add(
@ -6243,13 +6215,10 @@ static void show_ip_ospf_database_header(struct vty *vty, struct ospf_lsa *lsa,
json_object_string_add( json_object_string_add(
json, "lsaType", json, "lsaType",
lookup_msg(ospf_lsa_type_msg, lsa->data->type, NULL)); lookup_msg(ospf_lsa_type_msg, lsa->data->type, NULL));
json_object_string_add(json, "linkStateId", json_object_string_addf(json, "linkStateId", "%pI4",
inet_ntop(AF_INET, &lsa->data->id, &lsa->data->id);
buf, sizeof(buf))); json_object_string_addf(json, "advertisingRouter", "%pI4",
json_object_string_add(json, "advertisingRouter", &lsa->data->adv_router);
inet_ntop(AF_INET,
&lsa->data->adv_router,
buf, sizeof(buf)));
json_object_string_add(json, "lsaSeqNumber", seqnum); json_object_string_add(json, "lsaSeqNumber", seqnum);
json_object_string_add(json, "checksum", checksum); json_object_string_add(json, "checksum", checksum);
json_object_int_add(json, "length", ntohs(lsa->data->length)); json_object_int_add(json, "length", ntohs(lsa->data->length));
@ -6513,10 +6482,8 @@ static int show_as_external_lsa_detail(struct vty *vty, struct ospf_lsa *lsa,
json_object_int_add(json, "tos", tos); json_object_int_add(json, "tos", tos);
json_object_int_add(json, "metric", json_object_int_add(json, "metric",
GET_METRIC(al->e[0].metric)); GET_METRIC(al->e[0].metric));
json_object_string_add(json, "forwardAddress", json_object_string_addf(json, "forwardAddress", "%pI4",
inet_ntop(AF_INET, &(al->e[0].fwd_addr));
&(al->e[0].fwd_addr),
buf, sizeof(buf)));
json_object_int_add( json_object_int_add(
json, "externalRouteTag", json, "externalRouteTag",
(route_tag_t)ntohl(al->e[0].route_tag)); (route_tag_t)ntohl(al->e[0].route_tag));
@ -6566,10 +6533,8 @@ static int show_as_nssa_lsa_detail(struct vty *vty, struct ospf_lsa *lsa,
json_object_int_add(json, "tos", tos); json_object_int_add(json, "tos", tos);
json_object_int_add(json, "metric", json_object_int_add(json, "metric",
GET_METRIC(al->e[0].metric)); GET_METRIC(al->e[0].metric));
json_object_string_add(json, "nssaForwardAddress", json_object_string_addf(json, "nssaForwardAddress",
inet_ntop(AF_INET, "%pI4", &al->e[0].fwd_addr);
&al->e[0].fwd_addr,
buf, sizeof(buf)));
json_object_int_add( json_object_int_add(
json, "externalRouteTag", json, "externalRouteTag",
(route_tag_t)ntohl(al->e[0].route_tag)); (route_tag_t)ntohl(al->e[0].route_tag));
@ -6961,15 +6926,11 @@ static void show_ip_ospf_database_maxage(struct vty *vty, struct ospf *ospf,
json_lsa = json_object_new_object(); json_lsa = json_object_new_object();
json_object_int_add(json_lsa, "linkType", json_object_int_add(json_lsa, "linkType",
lsa->data->type); lsa->data->type);
json_object_string_add( json_object_string_addf(json_lsa, "linkStateId",
json_lsa, "linkStateId", "%pI4", &lsa->data->id);
inet_ntop(AF_INET, &lsa->data->id, json_object_string_addf(
buf, sizeof(buf))); json_lsa, "advertisingRouter", "%pI4",
json_object_string_add( &lsa->data->adv_router);
json_lsa, "advertisingRouter",
inet_ntop(AF_INET,
&lsa->data->adv_router,
buf, sizeof(buf)));
json_object_int_add(json_lsa, "lsaLockCount", json_object_int_add(json_lsa, "lsaLockCount",
lsa->lock); lsa->lock);
json_object_object_add( json_object_object_add(
@ -7033,9 +6994,8 @@ static int show_ip_ospf_database_common(struct vty *vty, struct ospf *ospf,
/* Show Router ID. */ /* Show Router ID. */
if (uj) { if (uj) {
json_object_string_add(json_vrf, "routerId", json_object_string_addf(json_vrf, "routerId", "%pI4",
inet_ntop(AF_INET, &ospf->router_id, &ospf->router_id);
buf, sizeof(buf)));
} else { } else {
vty_out(vty, "\n OSPF Router with ID (%pI4)\n\n", vty_out(vty, "\n OSPF Router with ID (%pI4)\n\n",
&ospf->router_id); &ospf->router_id);
@ -7307,9 +7267,8 @@ static int show_ip_ospf_database_type_adv_router_common(struct vty *vty,
/* Show Router ID. */ /* Show Router ID. */
if (uj) { if (uj) {
json_object_string_add(json_vrf, "routerId", json_object_string_addf(json_vrf, "routerId", "%pI4",
inet_ntop(AF_INET, &ospf->router_id, &ospf->router_id);
buf, sizeof(buf)));
} else { } else {
vty_out(vty, "\n OSPF Router with ID (%pI4)\n\n", vty_out(vty, "\n OSPF Router with ID (%pI4)\n\n",
&ospf->router_id); &ospf->router_id);
@ -10112,11 +10071,9 @@ static void show_ip_ospf_route_network(struct vty *vty, struct ospf *ospf,
"N IA"); "N IA");
json_object_int_add(json_route, "cost", json_object_int_add(json_route, "cost",
or->cost); or->cost);
json_object_string_add( json_object_string_addf(
json_route, "area", json_route, "area", "%pI4",
inet_ntop(AF_INET, &or->u.std.area_id);
&or->u.std.area_id,
buf1, sizeof(buf1)));
} else { } else {
vty_out(vty, vty_out(vty,
"N IA %-18s [%d] area: %pI4\n", "N IA %-18s [%d] area: %pI4\n",
@ -10141,10 +10098,9 @@ static void show_ip_ospf_route_network(struct vty *vty, struct ospf *ospf,
"N"); "N");
json_object_int_add(json_route, "cost", json_object_int_add(json_route, "cost",
or->cost); or->cost);
json_object_string_add( json_object_string_addf(json_route, "area",
json_route, "area", "%pI4",
inet_ntop(AF_INET, &or->u.std.area_id, &or->u.std.area_id);
buf1, sizeof(buf1)));
} else { } else {
vty_out(vty, "N %-18s [%d] area: %pI4\n", vty_out(vty, "N %-18s [%d] area: %pI4\n",
buf1, or->cost, buf1, or->cost,
@ -10195,14 +10151,10 @@ static void show_ip_ospf_route_network(struct vty *vty, struct ospf *ospf,
} }
} else { } else {
if (json) { if (json) {
json_object_string_add( json_object_string_addf(
json_nexthop, json_nexthop,
"ip", "ip", "%pI4",
inet_ntop( &path->nexthop);
AF_INET,
&path->nexthop,
buf,
sizeof(buf)));
json_object_string_add( json_object_string_add(
json_nexthop, json_nexthop,
"via", "via",
@ -10271,10 +10223,9 @@ static void show_ip_ospf_route_router(struct vty *vty, struct ospf *ospf,
if (json) { if (json) {
json_object_int_add(json_route, "cost", json_object_int_add(json_route, "cost",
or->cost); or->cost);
json_object_string_add( json_object_string_addf(json_route, "area",
json_route, "area", "%pI4",
inet_ntop(AF_INET, &or->u.std.area_id, &or->u.std.area_id);
buf, sizeof(buf)));
if (or->path_type == OSPF_PATH_INTER_AREA) if (or->path_type == OSPF_PATH_INTER_AREA)
json_object_boolean_true_add(json_route, json_object_boolean_true_add(json_route,
"IA"); "IA");
@ -10339,13 +10290,10 @@ static void show_ip_ospf_route_router(struct vty *vty, struct ospf *ospf,
} }
} else { } else {
if (json) { if (json) {
json_object_string_add( json_object_string_addf(
json_nexthop, json_nexthop,
"ip", "ip", "%pI4",
inet_ntop( &path->nexthop);
AF_INET,
&path->nexthop,
buf, sizeof(buf)));
json_object_string_add( json_object_string_add(
json_nexthop, json_nexthop,
"via", "via",
@ -10469,13 +10417,9 @@ static void show_ip_ospf_route_external(struct vty *vty, struct ospf *ospf,
} }
} else { } else {
if (json) { if (json) {
json_object_string_add( json_object_string_addf(
json_nexthop, "ip", json_nexthop, "ip",
inet_ntop( "%pI4", &path->nexthop);
AF_INET,
&path->nexthop,
buf,
sizeof(buf)));
json_object_string_add( json_object_string_add(
json_nexthop, "via", json_nexthop, "via",
ifindex2ifname( ifindex2ifname(
@ -10825,10 +10769,8 @@ DEFUN (show_ip_ospf_vrfs,
if (uj) { if (uj) {
json_object_int_add(json_vrf, "vrfId", vrf_id_ui); json_object_int_add(json_vrf, "vrfId", vrf_id_ui);
json_object_string_add( json_object_string_addf(json_vrf, "routerId", "%pI4",
json_vrf, "routerId", &ospf->router_id);
inet_ntop(AF_INET, &ospf->router_id,
buf, sizeof(buf)));
json_object_object_add(json_vrfs, name, json_vrf); json_object_object_add(json_vrfs, name, json_vrf);