Add option dev for show
Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
72
src/show.c
72
src/show.c
@@ -2,6 +2,8 @@
|
|||||||
#include "ctl.h"
|
#include "ctl.h"
|
||||||
#include "str.h"
|
#include "str.h"
|
||||||
|
|
||||||
|
#include "../argz/argz.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
@@ -37,28 +39,25 @@ gt_ss_addr(char *str, size_t size, struct sockaddr_storage *ss)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
gt_show_tunnel(int fd, const char *dev)
|
gt_show_dev_status(int fd, const char *dev)
|
||||||
{
|
{
|
||||||
if (ctl_connect(fd, "/run/" PACKAGE_NAME, dev) == -1) {
|
if (ctl_connect(fd, "/run/" PACKAGE_NAME, dev) == -1)
|
||||||
perror(dev);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
struct ctl_msg res, req = {.type = CTL_STATUS};
|
struct ctl_msg res, req = {.type = CTL_STATUS};
|
||||||
|
|
||||||
if (ctl_reply(fd, &res, &req)) {
|
if (ctl_reply(fd, &res, &req))
|
||||||
perror(dev);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
char bindstr[INET6_ADDRSTRLEN] = {0};
|
char bindstr[INET6_ADDRSTRLEN] = {0};
|
||||||
char peerstr[INET6_ADDRSTRLEN] = {0};
|
char peerstr[INET6_ADDRSTRLEN] = {0};
|
||||||
|
|
||||||
if (gt_ss_addr(bindstr, sizeof(bindstr), &res.status.bind) ||
|
if (gt_ss_addr(bindstr, sizeof(bindstr), &res.status.bind))
|
||||||
gt_ss_addr(peerstr, sizeof(peerstr), &res.status.peer))
|
return -2;
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (res.status.peer.ss_family == 0) {
|
int server = gt_ss_addr(peerstr, sizeof(peerstr), &res.status.peer);
|
||||||
|
|
||||||
|
if (server) {
|
||||||
printf("server %s:\n"
|
printf("server %s:\n"
|
||||||
" bind: %s port %hu\n"
|
" bind: %s port %hu\n"
|
||||||
" mtu: %zu\n"
|
" mtu: %zu\n"
|
||||||
@@ -87,9 +86,46 @@ gt_show_tunnel(int fd, const char *dev)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
gt_show_dev(const char *dev)
|
||||||
|
{
|
||||||
|
int fd = ctl_create("/run/" PACKAGE_NAME, NULL);
|
||||||
|
|
||||||
|
if (fd < 0) {
|
||||||
|
perror(dev);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ret = gt_show_dev_status(fd, dev);
|
||||||
|
|
||||||
|
if (ret == -1)
|
||||||
|
perror(dev);
|
||||||
|
|
||||||
|
if (ret == -2)
|
||||||
|
gt_log("%s: bad reply from server\n", dev);
|
||||||
|
|
||||||
|
ctl_delete(fd);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
gt_show(int argc, char **argv)
|
gt_show(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
const char *dev = NULL;
|
||||||
|
|
||||||
|
struct argz showz[] = {
|
||||||
|
{"dev", "NAME", &dev, argz_str},
|
||||||
|
{NULL}};
|
||||||
|
|
||||||
|
if (argz(showz, argc, argv))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (dev) {
|
||||||
|
gt_show_dev(dev);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
DIR *dp = opendir("/run/" PACKAGE_NAME);
|
DIR *dp = opendir("/run/" PACKAGE_NAME);
|
||||||
|
|
||||||
if (!dp) {
|
if (!dp) {
|
||||||
@@ -102,18 +138,8 @@ gt_show(int argc, char **argv)
|
|||||||
struct dirent *d = NULL;
|
struct dirent *d = NULL;
|
||||||
|
|
||||||
while (d = readdir(dp), d) {
|
while (d = readdir(dp), d) {
|
||||||
if (d->d_name[0] == '.')
|
if (d->d_name[0] != '.')
|
||||||
continue;
|
gt_show_dev(d->d_name);
|
||||||
|
|
||||||
int fd = ctl_create("/run/" PACKAGE_NAME, NULL);
|
|
||||||
|
|
||||||
if (fd == -1) {
|
|
||||||
perror("ctl_create");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
gt_show_tunnel(fd, d->d_name);
|
|
||||||
ctl_delete(fd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dp);
|
closedir(dp);
|
||||||
|
|||||||
Reference in New Issue
Block a user