summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2013-08-12 18:54:16 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2013-08-12 18:54:16 +0200
commitd468ffff4564ac0d6fb1b13d886ee72785d329ef (patch)
tree52b54fc5f1e020b928c01228d1be413dd3aa8e74
parent78de22fd21b348fc00dde6fa0bfec73abb8fd651 (diff)
downloadfastd-d468ffff4564ac0d6fb1b13d886ee72785d329ef.tar
fastd-d468ffff4564ac0d6fb1b13d886ee72785d329ef.zip
Add pre-up and post-down handlers
-rw-r--r--src/config.c4
-rw-r--r--src/config.l2
-rw-r--r--src/config.y22
-rw-r--r--src/fastd.c22
-rw-r--r--src/fastd.h6
-rw-r--r--src/options.c16
-rw-r--r--src/options.def.h2
-rw-r--r--src/shell.c15
-rw-r--r--src/tuntap.c2
9 files changed, 86 insertions, 5 deletions
diff --git a/src/config.c b/src/config.c
index 5ed7bc3..128b114 100644
--- a/src/config.c
+++ b/src/config.c
@@ -738,10 +738,14 @@ void fastd_config_release(fastd_context_t *ctx, fastd_config_t *conf) {
free(conf->groups);
free(conf->ifname);
free(conf->secret);
+ free(conf->on_pre_up);
+ free(conf->on_pre_up_dir);
free(conf->on_up);
free(conf->on_up_dir);
free(conf->on_down);
free(conf->on_down_dir);
+ free(conf->on_post_down);
+ free(conf->on_post_down_dir);
free(conf->on_establish);
free(conf->on_establish_dir);
free(conf->on_disestablish);
diff --git a/src/config.l b/src/config.l
index c9f25d8..dee1d39 100644
--- a/src/config.l
+++ b/src/config.l
@@ -90,8 +90,10 @@ any { TOKEN(TOK_ANY); }
tap { TOKEN(TOK_TAP); }
tun { TOKEN(TOK_TUN); }
on { TOKEN(TOK_ON); }
+pre-up { TOKEN(TOK_PRE_UP); }
up { TOKEN(TOK_UP); }
down { TOKEN(TOK_DOWN); }
+post-down { TOKEN(TOK_POST_DOWN); }
establish { TOKEN(TOK_ESTABLISH); }
disestablish { TOKEN(TOK_DISESTABLISH); }
verify { TOKEN(TOK_VERIFY); }
diff --git a/src/config.y b/src/config.y
index 55d0e94..e603e7c 100644
--- a/src/config.y
+++ b/src/config.y
@@ -77,8 +77,10 @@
%token TOK_TAP
%token TOK_TUN
%token TOK_ON
+%token TOK_PRE_UP
%token TOK_UP
%token TOK_DOWN
+%token TOK_POST_DOWN
%token TOK_ESTABLISH
%token TOK_DISESTABLISH
%token TOK_VERIFY
@@ -175,8 +177,10 @@ statement: peer_group_statement
| TOK_METHOD method ';'
| TOK_CRYPTO crypto ';'
| TOK_SECRET secret ';'
+ | TOK_ON TOK_PRE_UP on_pre_up ';'
| TOK_ON TOK_UP on_up ';'
| TOK_ON TOK_DOWN on_down ';'
+ | TOK_ON TOK_POST_DOWN on_post_down ';'
| TOK_ON TOK_ESTABLISH on_establish ';'
| TOK_ON TOK_DISESTABLISH on_disestablish ';'
| TOK_ON TOK_VERIFY on_verify ';'
@@ -354,6 +358,15 @@ crypto: TOK_STRING TOK_USE TOK_STRING {
secret: TOK_STRING { free(conf->secret); conf->secret = strdup($1->str); }
;
+on_pre_up: TOK_STRING {
+ free(conf->on_pre_up);
+ free(conf->on_pre_up_dir);
+
+ conf->on_pre_up = strdup($1->str);
+ conf->on_pre_up_dir = get_current_dir_name();
+ }
+ ;
+
on_up: TOK_STRING {
free(conf->on_up);
free(conf->on_up_dir);
@@ -372,6 +385,15 @@ on_down: TOK_STRING {
}
;
+on_post_down: TOK_STRING {
+ free(conf->on_post_down);
+ free(conf->on_post_down_dir);
+
+ conf->on_post_down = strdup($1->str);
+ conf->on_post_down_dir = get_current_dir_name();
+ }
+ ;
+
on_establish: TOK_STRING {
free(conf->on_establish);
free(conf->on_establish_dir);
diff --git a/src/fastd.c b/src/fastd.c
index ed47831..f8a5a75 100644
--- a/src/fastd.c
+++ b/src/fastd.c
@@ -256,6 +256,13 @@ void fastd_handle_receive(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer
fastd_buffer_free(buffer);
}
+static inline void on_pre_up(fastd_context_t *ctx) {
+ if (!ctx->conf->on_pre_up)
+ return;
+
+ fastd_shell_exec(ctx, ctx->conf->on_pre_up, ctx->conf->on_pre_up_dir, NULL, NULL, NULL, NULL);
+}
+
static inline void on_up(fastd_context_t *ctx) {
if (!ctx->conf->on_up)
return;
@@ -270,6 +277,13 @@ static inline void on_down(fastd_context_t *ctx) {
fastd_shell_exec(ctx, ctx->conf->on_down, ctx->conf->on_down_dir, NULL, NULL, NULL, NULL);
}
+static inline void on_post_down(fastd_context_t *ctx) {
+ if (!ctx->conf->on_post_down)
+ return;
+
+ fastd_shell_exec(ctx, ctx->conf->on_post_down, ctx->conf->on_post_down_dir, NULL, NULL, NULL, NULL);
+}
+
static fastd_peer_group_t* init_peer_group(const fastd_peer_group_config_t *config, fastd_peer_group_t *parent) {
fastd_peer_group_t *ret = calloc(1, sizeof(fastd_peer_group_t));
@@ -708,8 +722,7 @@ static void drop_caps(fastd_context_t *ctx) {
}
int main(int argc, char *argv[]) {
- fastd_context_t ctx;
- memset(&ctx, 0, sizeof(ctx));
+ fastd_context_t ctx = {};
close_fds(&ctx);
@@ -752,6 +765,8 @@ int main(int argc, char *argv[]) {
if (!fastd_socket_handle_binds(&ctx))
exit_error(&ctx, "unable to bind default socket");
+ on_pre_up(&ctx);
+
fastd_tuntap_open(&ctx);
init_peer_groups(&ctx);
@@ -824,8 +839,11 @@ int main(int argc, char *argv[]) {
fastd_tuntap_close(&ctx);
close_sockets(&ctx);
+ on_post_down(&ctx);
+
free(ctx.protocol_state);
free(ctx.eth_addr);
+ free(ctx.ifname);
crypto_free(&ctx);
diff --git a/src/fastd.h b/src/fastd.h
index 8201c5c..014bb2f 100644
--- a/src/fastd.h
+++ b/src/fastd.h
@@ -230,12 +230,18 @@ struct fastd_config {
fastd_protocol_config_t *protocol_config;
+ char *on_pre_up;
+ char *on_pre_up_dir;
+
char *on_up;
char *on_up_dir;
char *on_down;
char *on_down_dir;
+ char *on_post_down;
+ char *on_post_down_dir;
+
char *on_establish;
char *on_establish_dir;
diff --git a/src/options.c b/src/options.c
index 7bab8bc..92ac46e 100644
--- a/src/options.c
+++ b/src/options.c
@@ -231,6 +231,14 @@ static void option_forward(fastd_context_t *ctx, fastd_config_t *conf) {
conf->forward = true;
}
+static void option_on_pre_up(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
+ free(conf->on_pre_up);
+ free(conf->on_pre_up_dir);
+
+ conf->on_pre_up = strdup(arg);
+ conf->on_pre_up_dir = get_current_dir_name();
+}
+
static void option_on_up(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
free(conf->on_up);
free(conf->on_up_dir);
@@ -247,6 +255,14 @@ static void option_on_down(fastd_context_t *ctx, fastd_config_t *conf, const cha
conf->on_down_dir = get_current_dir_name();
}
+static void option_on_post_down(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
+ free(conf->on_post_down);
+ free(conf->on_post_down_dir);
+
+ conf->on_post_down = strdup(arg);
+ conf->on_post_down_dir = get_current_dir_name();
+}
+
static void option_on_establish(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
free(conf->on_establish);
free(conf->on_establish_dir);
diff --git a/src/options.def.h b/src/options.def.h
index dfb4983..0b623d4 100644
--- a/src/options.def.h
+++ b/src/options.def.h
@@ -19,8 +19,10 @@ OPTION_ARG(option_bind, "--bind" OR "-b", "<address>[:<port>]", "Sets the bind a
OPTION_ARG(option_protocol, "--protocol" OR "-p", "<protocol>", "Sets the protocol");
OPTION_ARG(option_method, "--method", "<method>", "Sets the encryption method");
OPTION(option_forward, "--forward", "Enables forwarding of packets between peers; read the documentation before use!");
+OPTION_ARG(option_on_pre_up, "--on-pre-up", "<command>", "Sets a shell command to execute before interface creation");
OPTION_ARG(option_on_up, "--on-up", "<command>", "Sets a shell command to execute after interface creation");
OPTION_ARG(option_on_down, "--on-down", "<command>", "Sets a shell command to execute before interface destruction");
+OPTION_ARG(option_on_post_down, "--on-post-down", "<command>", "Sets a shell command to execute after interface destruction");
OPTION_ARG(option_on_establish, "--on-establish", "<command>", "Sets a shell command to execute when a new connection is established");
OPTION_ARG(option_on_disestablish, "--on-disestablish", "<command>", "Sets a shell command to execute when a connection is lost");
OPTION_ARG(option_on_verify, "--on-verify", "<command>", "Sets a shell command to execute to check a connection attempt by an unknown peer");
diff --git a/src/shell.c b/src/shell.c
index aa6dccf..1139276 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -43,7 +43,20 @@ bool fastd_shell_exec(fastd_context_t *ctx, const char *command, const char *dir
snprintf(buf, sizeof(buf), "%u", (unsigned)getpid());
setenv("FASTD_PID", buf, 1);
- setenv("INTERFACE", ctx->ifname, 1);
+ if (ctx->ifname) {
+ setenv("INTERFACE", ctx->ifname, 1);
+ }
+ else if (ctx->conf->ifname) {
+ char ifname[IF_NAMESIZE];
+
+ strncpy(ifname, ctx->conf->ifname, sizeof(ifname)-1);
+ ifname[sizeof(ifname)-1] = 0;
+
+ setenv("INTERFACE", ifname, 1);
+ }
+ else {
+ unsetenv("INTERFACE");
+ }
snprintf(buf, sizeof(buf), "%u", ctx->conf->mtu);
setenv("INTERFACE_MTU", buf, 1);
diff --git a/src/tuntap.c b/src/tuntap.c
index 64633a8..b9ff646 100644
--- a/src/tuntap.c
+++ b/src/tuntap.c
@@ -337,6 +337,4 @@ void fastd_tuntap_write(fastd_context_t *ctx, fastd_buffer_t buffer) {
void fastd_tuntap_close(fastd_context_t *ctx) {
if (close(ctx->tunfd))
pr_warn_errno(ctx, "closing tun/tap: close");
-
- free(ctx->ifname);
}