Add the sync command

Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
Adrien Gallouët
2018-05-21 09:26:37 +00:00
parent a3bb488fd5
commit 2dbf5fb765
8 changed files with 81 additions and 0 deletions

View File

@@ -289,6 +289,10 @@ gt_bind(int argc, char **argv)
res.status.bind = bind_addr;
res.status.peer = peer_addr;
break;
case CTL_SYNC:
if (mud_send(mud, NULL, 0, 0) == -1)
res.ret = errno;
break;
}
if (sendto(ctl_fd, &res, sizeof(res), 0,
(const struct sockaddr *)&ss, sl) == -1)

View File

@@ -74,3 +74,4 @@ int gt_path (int, char **);
int gt_keygen (int, char **);
int gt_bench (int, char **);
int gt_set (int, char **);
int gt_sync (int, char **);

View File

@@ -14,6 +14,7 @@ enum ctl_type {
CTL_TIMEOUT,
CTL_TIMETOLERANCE,
CTL_PATH_STATUS,
CTL_SYNC,
};
struct ctl_msg {

View File

@@ -64,6 +64,7 @@ main(int argc, char **argv)
{"bench", "start a crypto bench", gt_bench},
{"bind", "start a new tunnel", gt_bind},
{"set", "change tunnel properties", gt_set},
{"sync", "re-sync tunnels", gt_sync},
{"keygen", "generate a new secret key", gt_keygen},
{"path", "manage paths", gt_path},
{"version", "show version", gt_version},

71
src/sync.c Normal file
View File

@@ -0,0 +1,71 @@
#include "common.h"
#include "ctl.h"
#include "str.h"
#include "../argz/argz.h"
#include <stdio.h>
#include <dirent.h>
static int
gt_sync_dev(const char *dev)
{
const int fd = ctl_connect(GT_RUNDIR, dev);
if (fd < 0) {
if (fd == -1)
perror("sync");
return 1;
}
struct ctl_msg res, req = {
.type = CTL_SYNC,
};
const int ret = ctl_reply(fd, &res, &req);
if (ret == -1)
perror("sync");
ctl_delete(fd);
return ret;
}
int
gt_sync(int argc, char **argv)
{
const char *dev = NULL;
struct argz syncz[] = {
{"dev", "NAME", &dev, argz_str},
{NULL}};
if (argz(syncz, argc, argv))
return 1;
if (dev) {
gt_sync_dev(dev);
return 0;
}
DIR *dp = opendir(GT_RUNDIR);
if (!dp) {
if (errno == ENOENT)
return 0;
perror("sync");
return 1;
}
struct dirent *d = NULL;
while (d = readdir(dp), d) {
if (d->d_name[0] != '.')
gt_sync_dev(d->d_name);
}
closedir(dp);
return 0;
}