Merge pull request #9361 from qlyoung/upstream-8857

ospf6d: JSON output for database dump show command
This commit is contained in:
Igor Ryzhov 2021-08-13 20:10:02 +03:00 committed by GitHub
commit cb5eb14c52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 20 deletions

View File

@ -18,9 +18,7 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "zebra.h"
#include <string.h>
#include <ctype.h>
@ -217,3 +215,21 @@ int all_digit(const char *str)
return 0;
return 1;
}
char *frrstr_hex(char *buff, size_t bufsiz, const uint8_t *str, size_t num)
{
if (bufsiz == 0)
return buff;
char tmp[3];
buff[0] = '\0';
for (size_t i = 0; i < num; i++) {
snprintf(tmp, sizeof(tmp), "%02x", (unsigned char)str[i]);
strlcat(buff, tmp, bufsiz);
}
return buff;
}

View File

@ -154,6 +154,26 @@ bool frrstr_endswith(const char *str, const char *suffix);
*/
int all_digit(const char *str);
/*
* Copy the hexadecimal representation of the string to a buffer.
*
* buff
* Buffer to copy result into with size of at least (2 * num) + 1.
*
* bufsiz
* Size of destination buffer.
*
* str
* String to represent as hexadecimal.
*
* num
* Number of characters to copy.
*
* Returns:
* Pointer to buffer containing resulting hexadecimal representation.
*/
char *frrstr_hex(char *buff, size_t bufsiz, const uint8_t *str, size_t num);
#ifdef __cplusplus
}
#endif

View File

@ -29,6 +29,7 @@
#include "memory.h"
#include "thread.h"
#include "checksum.h"
#include "frrstr.h"
#include "ospf6_proto.h"
#include "ospf6_lsa.h"
@ -80,7 +81,6 @@ static int ospf6_unknown_lsa_show(struct vty *vty, struct ospf6_lsa *lsa,
json_object *json_obj, bool use_json)
{
uint8_t *start, *end, *current;
char byte[4];
start = (uint8_t *)lsa->header + sizeof(struct ospf6_lsa_header);
end = (uint8_t *)lsa->header + ntohs(lsa->header->length);
@ -95,8 +95,7 @@ static int ospf6_unknown_lsa_show(struct vty *vty, struct ospf6_lsa *lsa,
else if ((current - start) % 4 == 0)
vty_out(vty, " ");
snprintf(byte, sizeof(byte), "%02x", *current);
vty_out(vty, "%s", byte);
vty_out(vty, "%02x", *current);
}
vty_out(vty, "\n\n");
@ -553,30 +552,52 @@ void ospf6_lsa_show_summary(struct vty *vty, struct ospf6_lsa *lsa,
void ospf6_lsa_show_dump(struct vty *vty, struct ospf6_lsa *lsa,
json_object *json_array, bool use_json)
{
uint8_t *start, *end, *current;
uint8_t *start = NULL;
uint8_t *end = NULL;
uint8_t *current = NULL;
char byte[4];
char *header_str = NULL;
char adv_router[INET6_ADDRSTRLEN];
char id[INET6_ADDRSTRLEN];
json_object *json = NULL;
start = (uint8_t *)lsa->header;
end = (uint8_t *)lsa->header + ntohs(lsa->header->length);
if (use_json)
return;
if (use_json) {
json = json_object_new_object();
size_t header_str_sz = (2 * (end - start)) + 1;
vty_out(vty, "\n");
vty_out(vty, "%s:\n", lsa->name);
header_str = XMALLOC(MTYPE_TMP, header_str_sz);
for (current = start; current < end; current++) {
if ((current - start) % 16 == 0)
vty_out(vty, "\n ");
else if ((current - start) % 4 == 0)
vty_out(vty, " ");
inet_ntop(AF_INET, &lsa->header->id, id, sizeof(id));
inet_ntop(AF_INET, &lsa->header->adv_router, adv_router,
sizeof(adv_router));
snprintf(byte, sizeof(byte), "%02x", *current);
vty_out(vty, "%s", byte);
frrstr_hex(header_str, header_str_sz, start, end - start);
json_object_string_add(json, "linkStateId", id);
json_object_string_add(json, "advertisingRouter", adv_router);
json_object_string_add(json, "header", header_str);
json_object_array_add(json_array, json);
XFREE(MTYPE_TMP, header_str);
} else {
vty_out(vty, "\n%s:\n", lsa->name);
for (current = start; current < end; current++) {
if ((current - start) % 16 == 0)
vty_out(vty, "\n ");
else if ((current - start) % 4 == 0)
vty_out(vty, " ");
snprintf(byte, sizeof(byte), "%02x", *current);
vty_out(vty, "%s", byte);
}
vty_out(vty, "\n\n");
}
vty_out(vty, "\n\n");
return;
}