138 lines
3.2 KiB
C
138 lines
3.2 KiB
C
#include "common.h"
|
|
#include "ctl.h"
|
|
#include "str.h"
|
|
|
|
#include "../argz/argz.h"
|
|
|
|
#include <stdio.h>
|
|
#include <sys/socket.h>
|
|
#include <dirent.h>
|
|
#include <sys/un.h>
|
|
#include <arpa/inet.h>
|
|
#include <unistd.h>
|
|
|
|
static int
|
|
gt_show_dev_status(int fd, const char *dev)
|
|
{
|
|
struct ctl_msg res, req = {.type = CTL_STATUS};
|
|
|
|
if (ctl_reply(fd, &res, &req))
|
|
return -1;
|
|
|
|
char bindstr[INET6_ADDRSTRLEN];
|
|
char peerstr[INET6_ADDRSTRLEN];
|
|
|
|
gt_toaddr(bindstr, sizeof(bindstr),
|
|
(struct sockaddr *)&res.status.bind);
|
|
|
|
int server = gt_toaddr(peerstr, sizeof(peerstr),
|
|
(struct sockaddr *)&res.status.peer);
|
|
|
|
int term = isatty(1);
|
|
|
|
if (server) {
|
|
printf(term ? "server %s:\n"
|
|
" pid: %li\n"
|
|
" bind: %s port %"PRIu16"\n"
|
|
" mtu: %zu\n"
|
|
" cipher: %s\n"
|
|
: "server %s"
|
|
" %li"
|
|
" %s %"PRIu16
|
|
" %zu"
|
|
" %s"
|
|
"\n",
|
|
dev,
|
|
res.status.pid,
|
|
bindstr[0] ? bindstr : "-",
|
|
gt_get_port((struct sockaddr *)&res.status.bind),
|
|
res.status.mtu,
|
|
res.status.chacha ? "chacha20poly1305" : "aes256gcm");
|
|
} else {
|
|
printf(term ? "client %s:\n"
|
|
" pid: %li\n"
|
|
" bind: %s port %"PRIu16"\n"
|
|
" peer: %s port %"PRIu16"\n"
|
|
" mtu: %zu\n"
|
|
" cipher: %s\n"
|
|
: "client %s"
|
|
" %li"
|
|
" %s %"PRIu16
|
|
" %s %"PRIu16
|
|
" %zu"
|
|
" %s"
|
|
"\n",
|
|
dev,
|
|
res.status.pid,
|
|
bindstr[0] ? bindstr : "-",
|
|
gt_get_port((struct sockaddr *)&res.status.bind),
|
|
peerstr[0] ? peerstr : "-",
|
|
gt_get_port((struct sockaddr *)&res.status.peer),
|
|
res.status.mtu,
|
|
res.status.chacha ? "chacha20poly1305" : "aes256gcm");
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
gt_show_dev(const char *dev)
|
|
{
|
|
int fd = ctl_connect(GT_RUNDIR, dev);
|
|
|
|
if (fd < 0) {
|
|
if (fd == -1)
|
|
perror("show");
|
|
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
|
|
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)
|
|
return !!gt_show_dev(dev);
|
|
|
|
DIR *dp = opendir(GT_RUNDIR);
|
|
|
|
if (!dp) {
|
|
if (errno == ENOENT)
|
|
return 0;
|
|
perror("show");
|
|
return 1;
|
|
}
|
|
|
|
int ret = 0;
|
|
struct dirent *d = NULL;
|
|
|
|
while (d = readdir(dp), d) {
|
|
if (d->d_name[0] != '.')
|
|
ret |= !!gt_show_dev(d->d_name);
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
return ret;
|
|
}
|