Skip to content

Commit

Permalink
Initial JSON output support, MDB only
Browse files Browse the repository at this point in the history
Signed-off-by: Joachim Wiberg <[email protected]>
  • Loading branch information
troglobit committed Mar 7, 2024
1 parent 9b55f5c commit bab7e1f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 19 deletions.
64 changes: 57 additions & 7 deletions src/bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ TAILQ_HEAD(, mdb) mdb_list = TAILQ_HEAD_INITIALIZER(mdb_list);
static char *br = "br0";
static int compat = 0;
extern int detail;

extern int json;
int prefix = 0;

static struct mdb *find(char *group, int vid)
{
Expand Down Expand Up @@ -108,7 +109,7 @@ static int populate(void)
return -1;

while (fgets(buf, sizeof(buf), fp)) {
char br[IFNAMSIZ + 1], port[IFNAMSIZ + 1], group[64];
char br[IFNAMSIZ + 1], port[IFNAMSIZ + 3], group[64];
char *tok, *dst;
int vid = 0;
size_t len;
Expand Down Expand Up @@ -150,7 +151,11 @@ static int populate(void)

if (e->port[0])
strcat(e->port, ", ");
if (json)
strcat(e->port, "\"");
strcat(e->port, port);
if (json)
strcat(e->port, "\"");
strlcpy(e->br, br, sizeof(e->br));
strlcpy(e->group, group, sizeof(e->group));
e->vid = vid;
Expand Down Expand Up @@ -629,28 +634,73 @@ int show_bridge_compat(FILE *fp)
return 0;
}

static void jprint(FILE *fp, const char *key, const char *val, int *first)
{
fprintf(fp, "%s%*s\"%s\": \"%s\"", *first ? "" : ",\n", prefix, "", key, val);
*first = 0;
}

static void jprinti(FILE *fp, const char *key, int val, int *first)
{
fprintf(fp, "%s%*s\"%s\": %d", *first ? "" : ",\n", prefix, "", key, val);
*first = 0;
}

static void jprinta(FILE *fp, const char *key, const char *val, int *first)
{
fprintf(fp, "%s%*s\"%s\": [ %s ]", *first ? "" : ",\n", prefix, "", key, val);
*first = 0;
}

int show_bridge_groups(FILE *fp)
{
struct mdb *e;
int first = 1;

if (populate()) {
logit(LOG_ERR, 0, "Failed reading MDB");
return 1;
}

fprintf(fp, " VID Multicast MAC Multicast Group Ports=\n");
if (json) {
fprintf(fp, "%*s[\n", prefix, "");
prefix += 2;
}
else
fprintf(fp, " VID Multicast MAC Multicast Group Ports=\n");

TAILQ_FOREACH(e, &mdb_list, link) {
uint8_t mac[ETH_ALEN];
char ena[20], vid[11] = { 0 };
unsigned char mac[ETH_ALEN];
struct in_addr ina;
int once = 1;

inet_aton(e->group, &ina);
ETHER_MAP_IP_MULTICAST(&ina, mac);
snprintf(ena, sizeof(ena), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
if (e->vid > 0)
snprintf(vid, sizeof(vid), "%4d", e->vid);

if (json) {
fprintf(fp, "%s%*s{\n", first ? "" : ",\n", prefix, "");
prefix += 2;
first = 0;

if (e->vid > 0)
jprinti(fp, "vid", e->vid, &once);
jprint(fp, "group", e->group, &once);
jprint(fp, "mac", ena, &once);
jprinta(fp, "ports", e->port, &once);

prefix -= 2;
fprintf(fp, "\n%*s}", prefix, "");
} else
fprintf(fp, "%s %s %-20s %s\n", vid, ena, e->group, e->port);
}

fprintf(fp, "%4d %02X:%02X:%02X:%02X:%02X:%02X %-20s %s\n",
e->vid,mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
e->group, e->port);
if (json) {
prefix -= 2;
fprintf(fp, "%*s\n]\n", prefix, "");
}

drop();
Expand Down
28 changes: 16 additions & 12 deletions src/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ static struct sockaddr_un sun;
static int ipc_sockid = 0;
static int ipc_socket = -1;
int detail = 0;
int json = 0;

enum {
IPC_ERR = -1,
Expand All @@ -77,7 +78,7 @@ struct ipcmd {
} cmds[] = {
{ IPC_HELP, "help", NULL, "This help text" },
{ IPC_VERSION, "version", NULL, "Show daemon version" },
{ IPC_IGMP_GRP, "show groups", NULL, "Show IGMP/MLD group memberships" },
{ IPC_IGMP_GRP, "show groups", "[json]", "Show IGMP/MLD group memberships" },
{ IPC_IGMP_IFACE, "show interfaces", NULL, "Show IGMP/MLD interface status" },
{ IPC_STATUS, "show status", NULL, "Show daemon status (default)" },
{ IPC_IGMP, "show igmp", NULL, "Show interfaces and group memberships" },
Expand Down Expand Up @@ -146,21 +147,24 @@ static void strip(char *cmd, size_t len)
chomp(cmd);
}

static void check_detail(char *cmd, size_t len)
static void check_opts(char *cmd, size_t len)
{
const char *det = "detail";
char *ptr;

strip(cmd, len);

len = MIN(strlen(cmd), strlen(det));
if (len > 0 && !strncasecmp(cmd, det, len)) {
detail = 0;
json = 0;

while (len > 0) {
len = strcspn(cmd, " \t\n");
strip(cmd, len);
if (!strncasecmp(cmd, "detail", len))
detail = 1;
else if (!strncasecmp(cmd, "json", len))
json = 1;
else
break;

detail = 1;
} else
detail = 0;
strip(cmd, len);
}
}

static int ipc_read(int sd, char *cmd, ssize_t len)
Expand All @@ -186,7 +190,7 @@ static int ipc_read(int sd, char *cmd, ssize_t len)
size_t len = strlen(c->cmd);

if (!strncasecmp(cmd, c->cmd, len)) {
check_detail(cmd, len);
check_opts(cmd, len);
return c->op;
}
}
Expand Down

0 comments on commit bab7e1f

Please sign in to comment.