summaryrefslogtreecommitdiffstats
path: root/src/peer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/peer.c')
-rw-r--r--src/peer.c110
1 files changed, 5 insertions, 105 deletions
diff --git a/src/peer.c b/src/peer.c
index 9e1b097..3eeb261 100644
--- a/src/peer.c
+++ b/src/peer.c
@@ -32,118 +32,18 @@
#include <arpa/inet.h>
-static void on_establish(fastd_context_t *ctx, fastd_peer_t *peer) {
+static inline void on_establish(fastd_context_t *ctx, const fastd_peer_t *peer) {
if (!ctx->conf->on_establish)
return;
- char *cwd = get_current_dir_name();
-
- if(!chdir(ctx->conf->on_establish_dir)) {
- setenv("INTERFACE", ctx->ifname, 1);
-
- char buf[INET6_ADDRSTRLEN];
-
- if (peer->config && peer->config->name)
- setenv("PEER_NAME", peer->config->name, 1);
- else
- unsetenv("PEER_NAME");
-
- switch(peer->address.sa.sa_family) {
- case AF_INET:
- inet_ntop(AF_INET, &peer->address.in.sin_addr, buf, sizeof(buf));
- setenv("PEER_ADDRESS", buf, 1);
-
- snprintf(buf, sizeof(buf), "%u", ntohs(peer->address.in.sin_port));
- setenv("PEER_PORT", buf, 1);
-
- break;
-
- case AF_INET6:
- inet_ntop(AF_INET6, &peer->address.in6.sin6_addr, buf, sizeof(buf));
- setenv("PEER_ADDRESS", buf, 1);
-
- snprintf(buf, sizeof(buf), "%u", ntohs(peer->address.in6.sin6_port));
- setenv("PEER_PORT", buf, 1);
-
- break;
-
- default:
- unsetenv("PEER_ADDRESS");
- unsetenv("PEER_PORT");
- }
-
- int ret = system(ctx->conf->on_establish);
-
- if (WIFSIGNALED(ret))
- pr_error(ctx, "on-establish command exited with signal %i", WTERMSIG(ret));
- else if(ret)
- pr_warn(ctx, "on-establish command exited with status %i", WEXITSTATUS(ret));
-
- if(chdir(cwd))
- pr_error(ctx, "can't chdir to `%s': %s", cwd, strerror(errno));
- }
- else {
- pr_error(ctx, "can't chdir to `%s': %s", ctx->conf->on_establish_dir, strerror(errno));
- }
-
- free(cwd);
+ fastd_shell_exec(ctx, peer, ctx->conf->on_establish, ctx->conf->on_establish_dir, NULL);
}
-static void on_disestablish(fastd_context_t *ctx, fastd_peer_t *peer) {
+static inline void on_disestablish(fastd_context_t *ctx, const fastd_peer_t *peer) {
if (!ctx->conf->on_disestablish)
return;
- char *cwd = get_current_dir_name();
-
- if(!chdir(ctx->conf->on_disestablish_dir)) {
- setenv("INTERFACE", ctx->ifname, 1);
-
- char buf[INET6_ADDRSTRLEN];
-
- if (peer->config && peer->config->name)
- setenv("PEER_NAME", peer->config->name, 1);
- else
- unsetenv("PEER_NAME");
-
- switch(peer->address.sa.sa_family) {
- case AF_INET:
- inet_ntop(AF_INET, &peer->address.in.sin_addr, buf, sizeof(buf));
- setenv("PEER_ADDRESS", buf, 1);
-
- snprintf(buf, sizeof(buf), "%u", ntohs(peer->address.in.sin_port));
- setenv("PEER_PORT", buf, 1);
-
- break;
-
- case AF_INET6:
- inet_ntop(AF_INET6, &peer->address.in6.sin6_addr, buf, sizeof(buf));
- setenv("PEER_ADDRESS", buf, 1);
-
- snprintf(buf, sizeof(buf), "%u", ntohs(peer->address.in6.sin6_port));
- setenv("PEER_PORT", buf, 1);
-
- break;
-
- default:
- unsetenv("PEER_ADDRESS");
- unsetenv("PEER_PORT");
- }
-
- int ret = system(ctx->conf->on_disestablish);
-
- if (WIFSIGNALED(ret))
- pr_error(ctx, "on-disestablish command exited with signal %i", WTERMSIG(ret));
- else if(ret)
- pr_warn(ctx, "on-disestablish command exited with status %i", WEXITSTATUS(ret));
-
- if(chdir(cwd))
- pr_error(ctx, "can't chdir to `%s': %s", cwd, strerror(errno));
- }
- else {
- pr_error(ctx, "can't chdir to `%s': %s", ctx->conf->on_disestablish_dir, strerror(errno));
- }
-
- free(cwd);
+ fastd_shell_exec(ctx, peer, ctx->conf->on_disestablish, ctx->conf->on_disestablish_dir, NULL);
}
static inline void free_socket(fastd_context_t *ctx, fastd_peer_t *peer) {
@@ -159,7 +59,7 @@ static inline void free_socket(fastd_context_t *ctx, fastd_peer_t *peer) {
}
}
-static bool has_group_config_constraints(const fastd_peer_group_config_t *group) {
+static inline bool has_group_config_constraints(const fastd_peer_group_config_t *group) {
for (; group; group = group->parent) {
if (group->max_connections)
return true;