shell: pass ifname and mtu to fastd_shell_env_set_iface() individually

Make the function reusable for non-fastd_iface_t interfaces.
This commit is contained in:
Matthias Schiffer 2021-03-01 21:06:43 +01:00
parent cc98d2fc97
commit f6fc1a39dd
4 changed files with 18 additions and 12 deletions

View file

@ -182,7 +182,7 @@ static inline void on_pre_up(void) {
/** Calls the on-up command */ /** Calls the on-up command */
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->name, iface->mtu);
fastd_shell_command_exec_sync(&conf.peer_group->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);
} }
@ -190,7 +190,7 @@ static inline void on_up(fastd_iface_t *iface) {
/** Calls the on-down command */ /** Calls the on-down command */
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->name, iface->mtu);
fastd_shell_command_exec_sync(&conf.peer_group->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

@ -64,7 +64,15 @@ void fastd_peer_set_shell_env(
fastd_shell_env_set(env, "PEER_NAME", peer ? peer->name : NULL); fastd_shell_env_set(env, "PEER_NAME", peer ? peer->name : NULL);
fastd_shell_env_set_iface(env, peer ? peer->iface : NULL); const char *ifname = NULL;
uint16_t mtu = 0;
if (peer) {
if (peer->iface) {
ifname = peer->iface->name;
mtu = peer->iface->mtu;
}
}
fastd_shell_env_set_iface(env, ifname, mtu);
fastd_peer_set_shell_env_addr(env, local_addr, "LOCAL_ADDRESS", "LOCAL_PORT"); fastd_peer_set_shell_env_addr(env, local_addr, "LOCAL_ADDRESS", "LOCAL_PORT");
fastd_peer_set_shell_env_addr(env, peer_addr, "PEER_ADDRESS", "PEER_PORT"); fastd_peer_set_shell_env_addr(env, peer_addr, "PEER_ADDRESS", "PEER_PORT");

View file

@ -55,17 +55,15 @@ void fastd_shell_env_free(fastd_shell_env_t *env) {
free(env); free(env);
} }
/** Adds an interface name to a shell environment */ /** Adds an interface name and MTU to a shell environment */
void fastd_shell_env_set_iface(fastd_shell_env_t *env, const fastd_iface_t *iface) { void fastd_shell_env_set_iface(fastd_shell_env_t *env, const char *ifname, uint16_t mtu) {
if (iface) { fastd_shell_env_set(env, "INTERFACE", ifname);
if (mtu) {
char buf[6]; char buf[6];
snprintf(buf, sizeof(buf), "%u", mtu);
fastd_shell_env_set(env, "INTERFACE", iface->name);
snprintf(buf, sizeof(buf), "%u", iface->mtu);
fastd_shell_env_set(env, "INTERFACE_MTU", buf); fastd_shell_env_set(env, "INTERFACE_MTU", buf);
} else { } else {
fastd_shell_env_set(env, "INTERFACE", NULL);
fastd_shell_env_set(env, "INTERFACE_MTU", NULL); fastd_shell_env_set(env, "INTERFACE_MTU", NULL);
} }
} }

View file

@ -52,7 +52,7 @@ static inline bool fastd_shell_command_isset(const fastd_shell_command_t *comman
fastd_shell_env_t *fastd_shell_env_alloc(void); fastd_shell_env_t *fastd_shell_env_alloc(void);
void fastd_shell_env_set(fastd_shell_env_t *env, const char *key, const char *value); void fastd_shell_env_set(fastd_shell_env_t *env, const char *key, const char *value);
void fastd_shell_env_set_iface(fastd_shell_env_t *env, const fastd_iface_t *iface); void fastd_shell_env_set_iface(fastd_shell_env_t *env, const char *ifname, uint16_t mtu);
void fastd_shell_env_free(fastd_shell_env_t *env); void fastd_shell_env_free(fastd_shell_env_t *env);
bool fastd_shell_command_exec_sync(const fastd_shell_command_t *command, const fastd_shell_env_t *env, int *ret); bool fastd_shell_command_exec_sync(const fastd_shell_command_t *command, const fastd_shell_env_t *env, int *ret);