Skip to content

Commit

Permalink
Use mud_set_conf()
Browse files Browse the repository at this point in the history
Signed-off-by: Adrien Gallouët <[email protected]>
  • Loading branch information
angt committed Jan 16, 2020
1 parent a9408e7 commit b2077f5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 155 deletions.
2 changes: 1 addition & 1 deletion mud
Submodule mud updated 2 files
+42 −42 mud.c
+10 −6 mud.h
40 changes: 12 additions & 28 deletions src/bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,18 @@ gt_bind(int argc, char **argv)
req.path.fixed_rate))
res.ret = errno;
break;
case CTL_CONF:
if (mud_set_conf(mud, &req.conf))
res.ret = errno;
break;
case CTL_STATUS:
memcpy(res.status.tun_name, tun_name, sizeof(tun_name)); // XXX
res.status.pid = pid;
res.status.mtu = mtu;
res.status.chacha = chacha;
res.status.bind = bind_addr;
res.status.peer = peer_addr;
break;
case CTL_PATH_STATUS:
{
unsigned count = 0;
Expand All @@ -331,34 +343,6 @@ gt_bind(int argc, char **argv)
res.ret = 0;
}
break;
case CTL_TC:
if (mud_set_tc(mud, req.tc))
res.ret = errno;
break;
case CTL_KXTIMEOUT:
if (mud_set_keyx_timeout(mud, req.ms))
res.ret = errno;
break;
case CTL_TIMETOLERANCE:
if (mud_set_time_tolerance(mud, req.ms))
res.ret = errno;
break;
case CTL_LOSSLIMIT:
if (mud_set_loss_limit(mud, req.percent))
res.ret = errno;
break;
case CTL_KEEPALIVE:
if (mud_set_keepalive(mud, req.ms))
res.ret = errno;
break;
case CTL_STATUS:
memcpy(res.status.tun_name, tun_name, sizeof(tun_name)); // XXX
res.status.pid = pid;
res.status.mtu = mtu;
res.status.chacha = chacha;
res.status.bind = bind_addr;
res.status.peer = peer_addr;
break;
case CTL_BAD:
if (mud_get_bad(mud, &res.bad))
res.ret = errno;
Expand Down
12 changes: 3 additions & 9 deletions src/ctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@
enum ctl_type {
CTL_NONE = 0,
CTL_STATE,
CTL_CONF,
CTL_STATUS,
CTL_TC,
CTL_KXTIMEOUT,
CTL_TIMETOLERANCE,
CTL_LOSSLIMIT,
CTL_KEEPALIVE,
CTL_PATH_STATUS,
CTL_BAD,
};
Expand All @@ -32,7 +28,6 @@ struct ctl_msg {
unsigned long beat;
unsigned char fixed_rate;
} path;
struct mud_path path_status;
struct {
char tun_name[64];
long pid;
Expand All @@ -41,10 +36,9 @@ struct ctl_msg {
struct sockaddr_storage bind;
struct sockaddr_storage peer;
} status;
struct mud_conf conf;
struct mud_path path_status;
struct mud_bad bad;
int tc;
unsigned long ms;
unsigned percent;
};
};

Expand Down
131 changes: 14 additions & 117 deletions src/set.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,96 +7,6 @@

#include "../argz/argz.h"

static int
gt_set_kxtimeout(int fd, unsigned long ms)
{
struct ctl_msg res, req = {
.type = CTL_KXTIMEOUT,
.ms = ms,
};

int ret = ctl_reply(fd, &res, &req);

if (ret) {
perror("set kxtimeout");
return 1;
}

return 0;
}

static int
gt_set_timetolerance(int fd, unsigned long ms)
{
struct ctl_msg res, req = {
.type = CTL_TIMETOLERANCE,
.ms = ms,
};

int ret = ctl_reply(fd, &res, &req);

if (ret) {
perror("set timetolerance");
return 1;
}

return 0;
}

static int
gt_set_losslimit(int fd, unsigned percent)
{
struct ctl_msg res, req = {
.type = CTL_LOSSLIMIT,
.percent = percent,
};

int ret = ctl_reply(fd, &res, &req);

if (ret) {
perror("set losslimit");
return 1;
}

return 0;
}

static int
gt_set_keepalive(int fd, unsigned long ms)
{
struct ctl_msg res, req = {
.type = CTL_KEEPALIVE,
.ms = ms,
};

int ret = ctl_reply(fd, &res, &req);

if (ret) {
perror("set keepalive");
return 1;
}

return 0;
}

static int
gt_set_tc(int fd, int tc)
{
struct ctl_msg res, req = {
.type = CTL_TC,
.tc = tc,
};

int ret = ctl_reply(fd, &res, &req);

if (ret) {
perror("set tc");
return 1;
}

return 0;
}

static int
gt_argz_tc(void *data, int argc, char **argv)
{
Expand All @@ -118,7 +28,7 @@ gt_argz_tc(void *data, int argc, char **argv)
} else return -1;

if (data)
*(int *)data = val;
*(int *)data = (val << 1) | 1;

return 1;
}
Expand All @@ -127,19 +37,18 @@ int
gt_set(int argc, char **argv)
{
const char *dev = NULL;
int tc;
unsigned long kxtimeout;
unsigned long timetolerance;
unsigned losslimit;
unsigned long keepalive;

struct ctl_msg req = {
.type = CTL_CONF,
}, res = {0};

struct argz pathz[] = {
{"dev", "NAME", &dev, argz_str},
{"tc", "CS|AF|EF", &tc, gt_argz_tc},
{"kxtimeout", "SECONDS", &kxtimeout, argz_time},
{"timetolerance", "SECONDS", &timetolerance, argz_time},
{"losslimit", "PERCENT", &losslimit, argz_percent},
{"keepalive", "SECONDS", &keepalive, argz_time},
{"tc", "CS|AF|EF", &req.conf.tc, gt_argz_tc},
{"kxtimeout", "SECONDS", &req.conf.kxtimeout, argz_time},
{"timetolerance", "SECONDS", &req.conf.timetolerance, argz_time},
{"losslimit", "PERCENT", &req.conf.losslimit, argz_percent},
{"keepalive", "SECONDS", &req.conf.keepalive, argz_time},
{NULL}};

if (argz(pathz, argc, argv))
Expand All @@ -164,24 +73,12 @@ gt_set(int argc, char **argv)
return 1;
}

int ret = 0;

if (argz_is_set(pathz, "tc"))
ret |= gt_set_tc(fd, tc);

if (argz_is_set(pathz, "kxtimeout"))
ret |= gt_set_kxtimeout(fd, kxtimeout);

if (argz_is_set(pathz, "timetolerance"))
ret |= gt_set_timetolerance(fd, timetolerance);

if (argz_is_set(pathz, "losslimit"))
ret |= gt_set_losslimit(fd, losslimit);
int ret = ctl_reply(fd, &res, &req);

if (argz_is_set(pathz, "keepalive"))
ret |= gt_set_keepalive(fd, keepalive);
if (ret)
perror("set");

ctl_delete(fd);

return ret;
return !!ret;
}

0 comments on commit b2077f5

Please sign in to comment.