From ff5b96686632f76e5517d71cc7074a4934b9147e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Tue, 27 Feb 2018 23:12:36 +0000 Subject: [PATCH] Add option 'set tc' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Adrien Gallouët --- src/bind.c | 4 +++ src/ctl.h | 2 ++ src/set.c | 101 ++++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 87 insertions(+), 20 deletions(-) diff --git a/src/bind.c b/src/bind.c index e8e8cb8..9e3053c 100644 --- a/src/bind.c +++ b/src/bind.c @@ -315,6 +315,10 @@ gt_bind(int argc, char **argv) res.mtu = gt_setup_mtu(mud, tun_name); mtu = res.mtu; break; + case CTL_TC: + if (mud_set_tc(mud, req.tc)) + res.ret = errno; + break; case CTL_TIMEOUT: if (mud_set_send_timeout(mud, req.timeout)) res.ret = errno; diff --git a/src/ctl.h b/src/ctl.h index 7421b40..fcdcd51 100644 --- a/src/ctl.h +++ b/src/ctl.h @@ -9,6 +9,7 @@ enum ctl_type { CTL_STATE, CTL_STATUS, CTL_MTU, + CTL_TC, CTL_TIMEOUT, CTL_TIMETOLERANCE, CTL_PATH_STATUS, @@ -31,6 +32,7 @@ struct ctl_msg { struct sockaddr_storage peer; } status; int mtu; + int tc; unsigned long timeout; unsigned long timetolerance; }; diff --git a/src/set.c b/src/set.c index d409fa3..4c6d31f 100644 --- a/src/set.c +++ b/src/set.c @@ -17,10 +17,14 @@ gt_set_mtu(int fd, size_t mtu) int ret = ctl_reply(fd, &res, &req); - if (!ret) - printf("new mtu: %i\n", res.mtu); + if (ret) { + perror("set mtu"); + return 1; + } - return ret; + printf("mtu set to %i\n", res.mtu); + + return 0; } static int @@ -31,7 +35,14 @@ gt_set_timeout(int fd, unsigned long timeout) .timeout = timeout, }; - return ctl_reply(fd, &res, &req); + int ret = ctl_reply(fd, &res, &req); + + if (ret) { + perror("set timeout"); + return 1; + } + + return 0; } static int @@ -42,20 +53,73 @@ gt_set_timetolerance(int fd, unsigned long timetolerance) .timetolerance = timetolerance, }; - return ctl_reply(fd, &res, &req); + int ret = ctl_reply(fd, &res, &req); + + if (ret) { + perror("set timetolerance"); + 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) +{ + if (argc < 1 || !argv[0]) + return -1; + + int val = 0; + const char *s = argv[0]; + + if ((s[0] == 'C') && (s[1] == 'S') && + (s[2] >= '0') && (s[2] <= '7') && !s[3]) { + val = (s[2] - '0') << 3; + } else if ((s[0] == 'A') && (s[1] == 'F') && + (s[2] >= '1') && (s[2] <= '4') && + (s[3] >= '1') && (s[3] <= '3') && !s[4]) { + val = ((s[2] - '0') << 3) | ((s[3] - '0') << 1); + } else if ((s[0] == 'E') && (s[1] == 'F') && !s[2]) { + val = 46; + } else return -1; + + if (data) + *(int *)data = val; + + return 1; } int gt_set(int argc, char **argv) { const char *dev = NULL; - unsigned long timetolerance = 0; - unsigned long timeout = 0; - size_t mtu = 0; + size_t mtu; + int tc; + unsigned long timetolerance; + unsigned long timeout; struct argz pathz[] = { {"dev", "NAME", &dev, argz_str}, {"mtu", "BYTES", &mtu, argz_bytes}, + {"tc", "CS|AF|EF", &tc, gt_argz_tc}, {"timeout", "SECONDS", &timeout, argz_time}, {"timetolerance", "SECONDS", &timetolerance, argz_time}, {NULL}}; @@ -78,20 +142,17 @@ gt_set(int argc, char **argv) int ret = 0; - if (mtu && gt_set_mtu(fd, mtu)) { - perror("mtu"); - ret = 1; - } + if (argz_is_set(pathz, "mtu")) + ret |= gt_set_mtu(fd, mtu); - if (!ret && timeout && gt_set_timeout(fd, timeout)) { - perror("timeout"); - ret = 1; - } + if (argz_is_set(pathz, "tc")) + ret |= gt_set_tc(fd, tc); - if (!ret && timetolerance && gt_set_timetolerance(fd, timetolerance)) { - perror("timetolerance"); - ret = 1; - } + if (argz_is_set(pathz, "timeout")) + ret |= gt_set_timeout(fd, timeout); + + if (argz_is_set(pathz, "timetolerance")) + ret |= gt_set_timetolerance(fd, timetolerance); ctl_delete(fd);