config: make on_up/down/establish/disestablish commands configurable per peer group

This commit is contained in:
Matthias Schiffer 2015-10-18 03:32:21 +02:00
parent df48485aea
commit 412e341def
8 changed files with 64 additions and 54 deletions

View file

@ -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->peer_dirs);
fastd_string_stack_free(group->methods); 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->name);
free(group); free(group);
} }
@ -691,12 +698,7 @@ void fastd_config_release(void) {
fastd_string_stack_free(conf.method_list); fastd_string_stack_free(conf.method_list);
fastd_shell_command_unset(&conf.on_pre_up); 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_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 #ifdef WITH_DYNAMIC_PEERS
fastd_shell_command_unset(&conf.on_verify); fastd_shell_command_unset(&conf.on_verify);
#endif #endif

View file

@ -196,12 +196,7 @@ statement: peer_group_statement
| TOK_PROTOCOL protocol ';' | TOK_PROTOCOL protocol ';'
| TOK_SECRET secret ';' | TOK_SECRET secret ';'
| TOK_ON TOK_PRE_UP on_pre_up ';' | 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_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_STATUS TOK_SOCKET status_socket ';'
| TOK_FORWARD forward ';' | 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_GROUP peer_group '{' peer_group_config '}' peer_group_after
| TOK_PEER TOK_LIMIT peer_limit ';' | TOK_PEER TOK_LIMIT peer_limit ';'
| TOK_METHOD method ';' | 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_ON TOK_VERIFY on_verify ';'
| TOK_INCLUDE include ';' | 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 { on_post_down: TOK_STRING {
fastd_shell_command_set(&conf.on_post_down, $1->str, true); 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 { status_socket: TOK_STRING {
#ifdef WITH_STATUS_SOCKET #ifdef WITH_STATUS_SOCKET
free(conf.status_socket); conf.status_socket = fastd_strdup($1->str); 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 { on_verify: sync TOK_STRING {
#ifdef WITH_DYNAMIC_PEERS #ifdef WITH_DYNAMIC_PEERS
fastd_shell_command_set(&conf.on_verify, $2->str, $1); fastd_shell_command_set(&conf.on_verify, $2->str, $1);

View file

@ -39,6 +39,7 @@
#include "config.h" #include "config.h"
#include "crypto.h" #include "crypto.h"
#include "peer.h" #include "peer.h"
#include "peer_group.h"
#include "peer_hashtable.h" #include "peer_hashtable.h"
#include "poll.h" #include "poll.h"
#include <fastd_version.h> #include <fastd_version.h>
@ -214,7 +215,7 @@ static inline void on_pre_up(void) {
static inline void on_up(fastd_iface_t *iface) { static inline void on_up(fastd_iface_t *iface) {
fastd_shell_env_t *env = fastd_shell_env_alloc(); fastd_shell_env_t *env = fastd_shell_env_alloc();
fastd_shell_env_set_iface(env, iface); 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); 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) { static inline void on_down(fastd_iface_t *iface) {
fastd_shell_env_t *env = fastd_shell_env_alloc(); fastd_shell_env_t *env = fastd_shell_env_alloc();
fastd_shell_env_set_iface(env, iface); 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); fastd_shell_env_free(env);
} }

View file

@ -249,12 +249,7 @@ struct fastd_config {
fastd_protocol_config_t *protocol_config; /**< The protocol-specific configuration */ 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_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_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 #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_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 */ fastd_peer_group_t *on_verify_group; /**< The peer group to put dynamic peers into */

View file

@ -50,6 +50,7 @@
#include "fastd.h" #include "fastd.h"
#include "config.h" #include "config.h"
#include "peer.h" #include "peer.h"
#include "peer_group.h"
#include <fastd_version.h> #include <fastd_version.h>
#include <arpa/inet.h> #include <arpa/inet.h>
@ -346,12 +347,12 @@ static void option_on_pre_up(const char *arg) {
/** Handles the --on-up option */ /** Handles the --on-up option */
static void option_on_up(const char *arg) { 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 */ /** Handles the --on-down option */
static void option_on_down(const char *arg) { 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 */ /** Handles the --on-post-down option */
@ -361,17 +362,17 @@ static void option_on_post_down(const char *arg) {
/** Handles the --on-connect option */ /** Handles the --on-connect option */
static void option_on_connect(const char *arg) { 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 */ /** Handles the --on-establish option */
static void option_on_establish(const char *arg) { 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 */ /** Handles the --on-disestablish option */
static void option_on_disestablish(const char *arg) { 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 #ifdef WITH_DYNAMIC_PEERS

View file

@ -126,22 +126,26 @@ void fastd_peer_exec_shell_command(const fastd_shell_command_t *command, const f
/** Calls the on-up command */ /** Calls the on-up command */
static inline void on_up(const fastd_peer_t *peer, bool sync) { 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 */ /** Calls the on-down command */
static inline void on_down(const fastd_peer_t *peer, bool sync) { 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 */ /** Executes the on-establish command for a peer */
static inline void on_establish(const fastd_peer_t *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 */ /** Executes the on-disestablish command for a peer */
static inline void on_disestablish(const fastd_peer_t *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 */ /** Compares two peers by their peer ID */

View file

@ -48,9 +48,14 @@ struct fastd_peer_group {
char *name; /**< The group's name; NULL for the root 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 */ 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 */ 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_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 */
}; };

View file

@ -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); fastd_handshake_add(&buffer, RECORD_SENDER_HANDSHAKE_KEY, PUBLICKEYBYTES, &ctx.protocol_state->handshake_key.key.public);
if (!peer || !fastd_peer_is_established(peer)) 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); 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); fastd_send_handshake(sock, local_addr, remote_addr, peer, buffer.buffer);
} }