Add ctl for mud_add_path() and mud_del_path()

Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
Adrien Gallouët
2018-01-26 19:52:16 +00:00
parent 0169a09469
commit 567e5d0193
5 changed files with 68 additions and 18 deletions

View File

@@ -2,8 +2,10 @@
enum ctl_type {
CTL_UNKNOWN,
CTL_PATH_ADD,
CTL_PATH_DEL,
CTL_PING,
CTL_PONG,
CTL_REPLY,
};
struct ctl_msg {
@@ -12,6 +14,12 @@ struct ctl_msg {
struct {
enum ctl_type type;
} unknown;
struct {
struct {
char addr[256];
} add, del;
} path;
int reply;
};
};

View File

@@ -269,7 +269,7 @@ main(int argc, char **argv)
int icmp_fd = -1;
if (gt.ipv4 && gt.mtu_auto) {
if (gt.ipv4 && gt.mtu_auto && gt.host) {
icmp_fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (icmp_fd == -1)
@@ -389,8 +389,7 @@ main(int argc, char **argv)
}
if (FD_ISSET(ctl_fd, &rfds)) {
struct ctl_msg msg;
struct ctl_msg reply;
struct ctl_msg msg, reply = {.type = CTL_REPLY};
struct sockaddr_storage ss;
socklen_t sl = sizeof(ss);
@@ -400,10 +399,23 @@ main(int argc, char **argv)
if (r == (ssize_t)sizeof(msg)) {
switch (msg.type) {
case CTL_PATH_ADD:
gt_log("[ctl path add] addr=%s\n",
&msg.path.add.addr[0]);
if (mud_add_path(mud, &msg.path.add.addr[0])) {
reply.reply = errno;
perror("mud_add_path");
}
break;
case CTL_PATH_DEL:
gt_log("[ctl path del] addr=%s\n",
&msg.path.del.addr[0]);
if (mud_del_path(mud, &msg.path.del.addr[0])) {
reply.reply = errno;
perror("mud_del_path");
}
break;
case CTL_PING:
reply = (struct ctl_msg){
.type = CTL_PONG,
};
break;
default:
reply = (struct ctl_msg){

View File

@@ -14,6 +14,12 @@
static struct {
char *dev;
int version;
struct {
struct {
int set;
const char *addr;
} add, del;
} path;
} gt = {
.dev = "tun0",
};
@@ -23,10 +29,18 @@ gt_setup_option(int argc, char **argv)
{
// clang-format off
struct option path_opts[] = {
{ "add", &gt.path.add.addr, option_str },
{ "del", &gt.path.del.addr, option_str },
{ NULL },
};
struct option opts[] = {
{ "dev", &gt.dev, option_str },
{ "version", NULL, option_option },
{ NULL },
{ "dev", &gt.dev, option_str },
{ "path", &path_opts, option_option },
{ "version", NULL, option_option },
{ NULL },
};
// clang-format on
@@ -34,6 +48,8 @@ gt_setup_option(int argc, char **argv)
if (option(opts, argc, argv))
return 1;
gt.path.add.set = option_is_set(path_opts, "add");
gt.path.del.set = option_is_set(path_opts, "del");
gt.version = option_is_set(opts, "version");
return 0;
@@ -62,9 +78,19 @@ main(int argc, char **argv)
return 1;
}
struct ctl_msg msg = {
.type = CTL_PING,
};
struct ctl_msg msg;
if (gt.path.add.set) {
msg = (struct ctl_msg){
.type = CTL_PATH_ADD,
};
str_cpy(msg.path.add.addr, sizeof(msg.path.add.addr) - 1, gt.path.add.addr);
} else if (gt.path.del.set) {
msg = (struct ctl_msg){
.type = CTL_PATH_DEL,
};
str_cpy(msg.path.del.addr, sizeof(msg.path.del.addr) - 1, gt.path.del.addr);
}
if (send(ctl_fd, &msg, sizeof(msg), 0) == -1) {
perror("send");
@@ -79,8 +105,11 @@ main(int argc, char **argv)
}
switch (reply.type) {
case CTL_PONG:
gt_print("PONG!\n");
case CTL_REPLY:
if (reply.reply) {
errno = reply.reply;
perror("error");
}
break;
case CTL_UNKNOWN:
gt_print("unknown command: %i\n", reply.unknown.type);