Skip to content

Commit

Permalink
Add the sync command
Browse files Browse the repository at this point in the history
Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
  • Loading branch information
angt committed May 21, 2018
1 parent a3bb488 commit 2dbf5fb
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Expand Up @@ -24,6 +24,7 @@ glorytun_SOURCES = \
src/set.c \
src/show.c \
src/str.h \
src/sync.c \
src/tun.c \
src/tun.h

Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -59,6 +59,7 @@ available commands:
bench start a crypto bench
bind start a new tunnel
set change tunnel properties
sync re-sync tunnels
keygen generate a new secret key
path manage paths
version show version
Expand Down
1 change: 1 addition & 0 deletions meson.build
Expand Up @@ -33,6 +33,7 @@ executable('glorytun', install: true,
'src/path.c',
'src/set.c',
'src/show.c',
'src/sync.c',
'src/tun.c',
],
dependencies: [
Expand Down
4 changes: 4 additions & 0 deletions src/bind.c
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/common.h
Expand Up @@ -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 **);
1 change: 1 addition & 0 deletions src/ctl.h
Expand Up @@ -14,6 +14,7 @@ enum ctl_type {
CTL_TIMEOUT,
CTL_TIMETOLERANCE,
CTL_PATH_STATUS,
CTL_SYNC,
};

struct ctl_msg {
Expand Down
1 change: 1 addition & 0 deletions src/main.c
Expand Up @@ -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},
Expand Down
71 changes: 71 additions & 0 deletions src/sync.c
@@ -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;
}

0 comments on commit 2dbf5fb

Please sign in to comment.