Skip to content

Commit

Permalink
Merge pull request #6 from FredSkar/main
Browse files Browse the repository at this point in the history
Fixed issue of not showing Router Ports and fixed nicer output
  • Loading branch information
FredSkar authored Nov 21, 2022
2 parents 8b4f3ff + c6e6f80 commit 3616444
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 8 deletions.
111 changes: 107 additions & 4 deletions src/bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@

#define SYSFS_PATH_ "/sys/class/net/"

struct port_name {
TAILQ_ENTRY(port_name) link;
char ifname[IFNAMSIZ];
};
TAILQ_HEAD(port_name_list, port_name);
struct port_name_list pnl = TAILQ_HEAD_INITIALIZER(pnl);

struct mdb {
TAILQ_ENTRY(mdb) link;

Expand All @@ -36,6 +43,7 @@ struct mdb {
int vid;
};


TAILQ_HEAD(, mdb) mdb_list = TAILQ_HEAD_INITIALIZER(mdb_list);
static char *br = "br0";
static int compat = 0;
Expand Down Expand Up @@ -209,17 +217,108 @@ static char *got(char *prop, int setval, int first)
dir = NULL;

return NULL;
}

static int cmpstringp(const void *p1, const void *p2)
{

const char *str1 = *(const char **)p1;
const char *str2 = *(const char **)p2;

int length = 0;
int index = 0;
int str1_len = strlen(str1);
int str2_len = strlen(str2);

if (str1_len == str2_len) {
length = str1_len;
return strncmp(str1, str2, length);
}
else if (str1_len > str2_len)
length = str1_len;
else
length = str2_len;

for (index=0; index<=length; index++) {
if (*str1 != *str2)
break;

str1++;
str2++;
}

if (index <= 2)
return strncmp(str1, str2, length);
else if (str1_len < str2_len)
return -1;
else
return 1;


}

void bridge_prop(FILE *fp, char *prop, int setval)
{
char *ifname;
char **array = NULL;
int num = 0;
int x = 0;
struct port_name *name = NULL, *next = NULL;

while ((ifname = got(prop, setval, !num))) {
fprintf(fp, "%s%s", num ? ", " : "", ifname);

name = malloc(sizeof(struct port_name));
if (!name)
goto out;
strncpy(name->ifname, ifname, IFNAMSIZ);
logit(LOG_DEBUG, 0, "Loop, name %s", name->ifname);

TAILQ_INSERT_TAIL(&pnl, name, link);
num++;
name = NULL;
}

array = malloc(num * sizeof(char*));
if (!array)
goto out;

for(int j=0; j<num; j++) {
array[j] = malloc(IFNAMSIZ * sizeof(char));
if (!array[j])
goto out;
}

TAILQ_FOREACH(name, &pnl, link) {
logit(LOG_DEBUG, 0, "Foreach, port name: %s", name->ifname);
strncpy(array[x], name->ifname, IFNAMSIZ);
x++;
}

qsort(array, num, sizeof(char *), cmpstringp);

for (int i=0; i<num; i++) {
logit(LOG_DEBUG, 0, "Array val: %s, index: %d", array[i], i);
fprintf(fp, "%s%s", i ? ", " : "", array[i]);
}

out:
/* Cleaning up */
if (array) {
for (int j=0; j<num; j++)
{
if (array[j])
free(array[j]);
}

free(array);
}

for (name = TAILQ_FIRST(&pnl); name; name = next) {
next = TAILQ_NEXT(name, link);
TAILQ_REMOVE(&pnl, name, link);
free(name);
}

if (!num && compat)
fprintf(fp, "---");
fprintf(fp, "\n");
Expand Down Expand Up @@ -261,19 +360,23 @@ void bridge_prop(FILE *fp, char *prop, int setval)
void bridge_router_ports(FILE *fp)
{
static const char *bridge_args = "-json -s vlan global show dev";
static const char *jq_filter = ".[].vlans[].router_ports[] | " \
".port + \" \" + .timer + \" \" + .type";
static const char *jq_filter = ".[].vlans[] | " \
"if has(\"router_ports\") == true then .router_ports[].port + \" \" + " \
".router_ports[].timer + \" \" + .router_ports[].type else \"false\" end";
char prev_ifname[20] = { 0 };
char cmd[256], buf[80];
char cmd[300], buf[80];
int num = 0;
FILE *rfp;
int ret;

ret = snprintf(cmd, sizeof(cmd), "bridge %s %s | jq -r '%s' | sort",
bridge_args, br, jq_filter);

if (ret < 0 || ret >= (int)sizeof(cmd))
goto fail;

rfp = popen(cmd, "r");

if (!rfp)
goto fail;

Expand Down
40 changes: 36 additions & 4 deletions src/querierctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,22 +265,31 @@ static int ipc_ping(void)

static int get_width(void)
{
int ret = 79;
/* Max width for highlighted lines. */
int max_width = 78;
int ret = max_width;
#ifdef HAVE_TERMIOS_H
struct pollfd fd = { STDIN_FILENO, POLLIN, 0 };
struct termios tc, saved;
struct winsize ws;
char buf[42];

if (!ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws)) {
return ws.ws_col;
if (ws.ws_col > 0 && ws.ws_col <= max_width)
return ws.ws_col;
else if (ws.ws_col > max_width)
return max_width;

} else if (!isatty(STDOUT_FILENO)) {
char *columns;

/* we may be running under watch(1) */
columns = getenv("COLUMNS");
if (columns)
return atoi(columns);
ret = atoi(columns);

if (ret > max_width)
ret = max_width;

return ret;
}
Expand All @@ -303,6 +312,10 @@ static int get_width(void)
fprintf(stderr, "\e8");
tcsetattr(STDERR_FILENO, TCSANOW, &saved);
#endif

if (ret > max_width)
ret = max_width;

return ret;
}

Expand All @@ -326,6 +339,8 @@ static void print(char *line, int indent)
{
int type = 0;
int i, len;
int index = 0;
int str_len;

chomp(line);

Expand Down Expand Up @@ -380,7 +395,24 @@ static void print(char *line, int indent)
break;

default:
puts(line);
str_len = strlen(line);
len = 79;

if (str_len > len) {
for (int x=0; x <= str_len; x++) {
fputc(line[0], stdout);

if (index >= len && line[0] == ' ') {
index = 0;
fprintf(stdout, "\n\t\t\t");
index = 24;
}
line++;
index++;
}
}
else
puts(line);
break;
}
}
Expand Down

0 comments on commit 3616444

Please sign in to comment.