From 412e341def88b0734fbac2c2f19fcecf2486ee86 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 18 Oct 2015 03:32:21 +0200 Subject: config: make on_up/down/establish/disestablish commands configurable per peer group --- src/config.c | 12 ++++--- src/config.y | 60 ++++++++++++++++---------------- src/fastd.c | 5 +-- src/fastd.h | 5 --- src/options.c | 11 +++--- src/peer.c | 12 ++++--- src/peer_group.h | 7 +++- src/protocols/ec25519_fhmqvc/handshake.c | 6 ++-- 8 files changed, 64 insertions(+), 54 deletions(-) (limited to 'src') diff --git a/src/config.c b/src/config.c index 034ad75..64ee12a 100644 --- a/src/config.c +++ b/src/config.c @@ -197,6 +197,13 @@ static void free_peer_group(fastd_peer_group_t *group) { fastd_string_stack_free(group->peer_dirs); fastd_string_stack_free(group->methods); + + fastd_shell_command_unset(&group->on_up); + fastd_shell_command_unset(&group->on_down); + fastd_shell_command_unset(&group->on_connect); + fastd_shell_command_unset(&group->on_establish); + fastd_shell_command_unset(&group->on_disestablish); + free(group->name); free(group); } @@ -691,12 +698,7 @@ void fastd_config_release(void) { fastd_string_stack_free(conf.method_list); fastd_shell_command_unset(&conf.on_pre_up); - fastd_shell_command_unset(&conf.on_up); - fastd_shell_command_unset(&conf.on_down); fastd_shell_command_unset(&conf.on_post_down); - fastd_shell_command_unset(&conf.on_connect); - fastd_shell_command_unset(&conf.on_establish); - fastd_shell_command_unset(&conf.on_disestablish); #ifdef WITH_DYNAMIC_PEERS fastd_shell_command_unset(&conf.on_verify); #endif diff --git a/src/config.y b/src/config.y index 4f55d91..8495e25 100644 --- a/src/config.y +++ b/src/config.y @@ -196,12 +196,7 @@ statement: peer_group_statement | TOK_PROTOCOL protocol ';' | 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_CONNECT on_connect ';' - | TOK_ON TOK_ESTABLISH on_establish ';' - | TOK_ON TOK_DISESTABLISH on_disestablish ';' | TOK_STATUS TOK_SOCKET status_socket ';' | TOK_FORWARD forward ';' ; @@ -211,6 +206,11 @@ peer_group_statement: | TOK_PEER TOK_GROUP peer_group '{' peer_group_config '}' peer_group_after | TOK_PEER TOK_LIMIT peer_limit ';' | TOK_METHOD method ';' + | TOK_ON TOK_UP on_up ';' + | TOK_ON TOK_DOWN on_down ';' + | TOK_ON TOK_CONNECT on_connect ';' + | TOK_ON TOK_ESTABLISH on_establish ';' + | TOK_ON TOK_DISESTABLISH on_disestablish ';' | TOK_ON TOK_VERIFY on_verify ';' | TOK_INCLUDE include ';' ; @@ -408,36 +408,11 @@ on_pre_up: TOK_STRING { } ; -on_up: sync TOK_STRING { - fastd_shell_command_set(&conf.on_up, $2->str, $1); - } - ; - -on_down: sync TOK_STRING { - fastd_shell_command_set(&conf.on_down, $2->str, $1); - } - ; - on_post_down: TOK_STRING { fastd_shell_command_set(&conf.on_post_down, $1->str, true); } ; -on_connect: sync TOK_STRING { - fastd_shell_command_set(&conf.on_connect, $2->str, $1); - } - ; - -on_establish: sync TOK_STRING { - fastd_shell_command_set(&conf.on_establish, $2->str, $1); - } - ; - -on_disestablish: sync TOK_STRING { - fastd_shell_command_set(&conf.on_disestablish, $2->str, $1); - } - ; - status_socket: TOK_STRING { #ifdef WITH_STATUS_SOCKET free(conf.status_socket); conf.status_socket = fastd_strdup($1->str); @@ -585,6 +560,31 @@ method: TOK_STRING { } ; +on_up: sync TOK_STRING { + fastd_shell_command_set(&state->peer_group->on_up, $2->str, $1); + } + ; + +on_down: sync TOK_STRING { + fastd_shell_command_set(&state->peer_group->on_down, $2->str, $1); + } + ; + +on_connect: sync TOK_STRING { + fastd_shell_command_set(&state->peer_group->on_connect, $2->str, $1); + } + ; + +on_establish: sync TOK_STRING { + fastd_shell_command_set(&state->peer_group->on_establish, $2->str, $1); + } + ; + +on_disestablish: sync TOK_STRING { + fastd_shell_command_set(&state->peer_group->on_disestablish, $2->str, $1); + } + ; + on_verify: sync TOK_STRING { #ifdef WITH_DYNAMIC_PEERS fastd_shell_command_set(&conf.on_verify, $2->str, $1); diff --git a/src/fastd.c b/src/fastd.c index 0d4f577..fbeffe5 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -39,6 +39,7 @@ #include "config.h" #include "crypto.h" #include "peer.h" +#include "peer_group.h" #include "peer_hashtable.h" #include "poll.h" #include @@ -214,7 +215,7 @@ static inline void on_pre_up(void) { static inline void on_up(fastd_iface_t *iface) { fastd_shell_env_t *env = fastd_shell_env_alloc(); fastd_shell_env_set_iface(env, iface); - fastd_shell_command_exec_sync(&conf.on_up, env, NULL); + fastd_shell_command_exec_sync(&conf.peer_group->on_up, env, NULL); fastd_shell_env_free(env); } @@ -222,7 +223,7 @@ static inline void on_up(fastd_iface_t *iface) { static inline void on_down(fastd_iface_t *iface) { fastd_shell_env_t *env = fastd_shell_env_alloc(); fastd_shell_env_set_iface(env, iface); - fastd_shell_command_exec_sync(&conf.on_down, env, NULL); + fastd_shell_command_exec_sync(&conf.peer_group->on_down, env, NULL); fastd_shell_env_free(env); } diff --git a/src/fastd.h b/src/fastd.h index cc638cb..1433509 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -249,12 +249,7 @@ struct fastd_config { fastd_protocol_config_t *protocol_config; /**< The protocol-specific configuration */ fastd_shell_command_t on_pre_up; /**< The command to execute before the initialization of the tunnel interface */ - fastd_shell_command_t on_up; /**< The command to execute after the initialization of the tunnel interface */ - fastd_shell_command_t on_down; /**< The command to execute before the destruction of the tunnel interface */ fastd_shell_command_t on_post_down; /**< The command to execute after the destruction of the tunnel interface */ - fastd_shell_command_t on_connect; /**< The command to execute before a handshake is sent to establish a new connection */ - fastd_shell_command_t on_establish; /**< The command to execute when a new connection has been established */ - fastd_shell_command_t on_disestablish; /**< The command to execute when a connection has been disestablished */ #ifdef WITH_DYNAMIC_PEERS fastd_shell_command_t on_verify; /**< The command to execute to check if a connection from an unknown peer should be allowed */ fastd_peer_group_t *on_verify_group; /**< The peer group to put dynamic peers into */ diff --git a/src/options.c b/src/options.c index 811adef..17a6abb 100644 --- a/src/options.c +++ b/src/options.c @@ -50,6 +50,7 @@ #include "fastd.h" #include "config.h" #include "peer.h" +#include "peer_group.h" #include #include @@ -346,12 +347,12 @@ static void option_on_pre_up(const char *arg) { /** Handles the --on-up option */ static void option_on_up(const char *arg) { - fastd_shell_command_set(&conf.on_up, arg, true); + fastd_shell_command_set(&conf.peer_group->on_up, arg, true); } /** Handles the --on-down option */ static void option_on_down(const char *arg) { - fastd_shell_command_set(&conf.on_down, arg, true); + fastd_shell_command_set(&conf.peer_group->on_down, arg, true); } /** Handles the --on-post-down option */ @@ -361,17 +362,17 @@ static void option_on_post_down(const char *arg) { /** Handles the --on-connect option */ static void option_on_connect(const char *arg) { - fastd_shell_command_set(&conf.on_connect, arg, false); + fastd_shell_command_set(&conf.peer_group->on_connect, arg, false); } /** Handles the --on-establish option */ static void option_on_establish(const char *arg) { - fastd_shell_command_set(&conf.on_establish, arg, false); + fastd_shell_command_set(&conf.peer_group->on_establish, arg, false); } /** Handles the --on-disestablish option */ static void option_on_disestablish(const char *arg) { - fastd_shell_command_set(&conf.on_disestablish, arg, false); + fastd_shell_command_set(&conf.peer_group->on_disestablish, arg, false); } #ifdef WITH_DYNAMIC_PEERS diff --git a/src/peer.c b/src/peer.c index de8846e..23cd485 100644 --- a/src/peer.c +++ b/src/peer.c @@ -126,22 +126,26 @@ void fastd_peer_exec_shell_command(const fastd_shell_command_t *command, const f /** Calls the on-up command */ static inline void on_up(const fastd_peer_t *peer, bool sync) { - fastd_peer_exec_shell_command(&conf.on_up, peer, NULL, NULL, sync); + const fastd_shell_command_t *on_up = container_of(fastd_peer_group_lookup_peer(peer, on_up.command), fastd_shell_command_t, command); + fastd_peer_exec_shell_command(on_up, peer, NULL, NULL, sync); } /** Calls the on-down command */ static inline void on_down(const fastd_peer_t *peer, bool sync) { - fastd_peer_exec_shell_command(&conf.on_down, peer, NULL, NULL, sync); + const fastd_shell_command_t *on_down = container_of(fastd_peer_group_lookup_peer(peer, on_down.command), fastd_shell_command_t, command); + fastd_peer_exec_shell_command(on_down, peer, NULL, NULL, sync); } /** Executes the on-establish command for a peer */ static inline void on_establish(const fastd_peer_t *peer) { - fastd_peer_exec_shell_command(&conf.on_establish, peer, &peer->local_address, &peer->address, false); + const fastd_shell_command_t *on_establish = container_of(fastd_peer_group_lookup_peer(peer, on_establish.command), fastd_shell_command_t, command); + fastd_peer_exec_shell_command(on_establish, peer, &peer->local_address, &peer->address, false); } /** Executes the on-disestablish command for a peer */ static inline void on_disestablish(const fastd_peer_t *peer) { - fastd_peer_exec_shell_command(&conf.on_disestablish, peer, &peer->local_address, &peer->address, false); + const fastd_shell_command_t *on_disestablish = container_of(fastd_peer_group_lookup_peer(peer, on_disestablish.command), fastd_shell_command_t, command); + fastd_peer_exec_shell_command(on_disestablish, peer, &peer->local_address, &peer->address, false); } /** Compares two peers by their peer ID */ diff --git a/src/peer_group.h b/src/peer_group.h index ce3a2dc..edba73e 100644 --- a/src/peer_group.h +++ b/src/peer_group.h @@ -48,9 +48,14 @@ struct fastd_peer_group { char *name; /**< The group's name; NULL for the root group */ fastd_string_stack_t *peer_dirs; /**< List of peer directories which belong to this group */ - /* constraints */ int max_connections; /**< The maximum number of connections to allow in this group; -1 for no limit */ fastd_string_stack_t *methods; /**< The list of configured method names */ + + fastd_shell_command_t on_up; /**< The command to execute after the initialization of the tunnel interface */ + fastd_shell_command_t on_down; /**< The command to execute before the destruction of the tunnel interface */ + fastd_shell_command_t on_connect; /**< The command to execute before a handshake is sent to establish a new connection */ + fastd_shell_command_t on_establish; /**< The command to execute when a new connection has been established */ + fastd_shell_command_t on_disestablish; /**< The command to execute when a connection has been disestablished */ }; diff --git a/src/protocols/ec25519_fhmqvc/handshake.c b/src/protocols/ec25519_fhmqvc/handshake.c index 80dbd5c..cebf25e 100644 --- a/src/protocols/ec25519_fhmqvc/handshake.c +++ b/src/protocols/ec25519_fhmqvc/handshake.c @@ -512,8 +512,10 @@ void fastd_protocol_ec25519_fhmqvc_handshake_init(fastd_socket_t *sock, const fa fastd_handshake_add(&buffer, RECORD_SENDER_HANDSHAKE_KEY, PUBLICKEYBYTES, &ctx.protocol_state->handshake_key.key.public); - if (!peer || !fastd_peer_is_established(peer)) - fastd_peer_exec_shell_command(&conf.on_connect, peer, (local_addr && local_addr->sa.sa_family) ? local_addr : sock->bound_addr, remote_addr, false); + if (!peer || !fastd_peer_is_established(peer)) { + const fastd_shell_command_t *on_connect = container_of(fastd_peer_group_lookup_peer(peer, on_connect.command), fastd_shell_command_t, command); + fastd_peer_exec_shell_command(on_connect, peer, (local_addr && local_addr->sa.sa_family) ? local_addr : sock->bound_addr, remote_addr, false); + } fastd_send_handshake(sock, local_addr, remote_addr, peer, buffer.buffer); } -- cgit v1.2.3