diff options
-rw-r--r-- | src/printf.c | 12 | ||||
-rw-r--r-- | src/shell.c | 10 |
2 files changed, 19 insertions, 3 deletions
diff --git a/src/printf.c b/src/printf.c index d5f47ee..99c577b 100644 --- a/src/printf.c +++ b/src/printf.c @@ -28,6 +28,7 @@ #include "peer.h" #include <arpa/inet.h> +#include <net/if.h> static inline int snprintf_safe(char *buffer, size_t size, const char *format, ...) { @@ -60,8 +61,15 @@ static int snprint_peer_address(const fastd_context_t *ctx, char *buffer, size_t case AF_INET6: if (!bind_address && ctx->conf->hide_ip_addresses) return snprintf_safe(buffer, size, "[hidden]:%u", ntohs(address->in.sin_port)); - if (inet_ntop(AF_INET6, &address->in6.sin6_addr, addr_buf, sizeof(addr_buf))) - return snprintf_safe(buffer, size, "[%s]:%u", addr_buf, ntohs(address->in6.sin6_port)); + if (inet_ntop(AF_INET6, &address->in6.sin6_addr, addr_buf, sizeof(addr_buf))) { + if (IN6_IS_ADDR_LINKLOCAL(&address->in6.sin6_addr)) { + char ifname_buf[IF_NAMESIZE]; + return snprintf_safe(buffer, size, "[%s%%%s]:%u", addr_buf, if_indextoname(address->in6.sin6_scope_id, ifname_buf), ntohs(address->in6.sin6_port)); + } + else { + return snprintf_safe(buffer, size, "[%s]:%u", addr_buf, ntohs(address->in6.sin6_port)); + } + } else return 0; diff --git a/src/shell.c b/src/shell.c index 80f925e..c1ecfa8 100644 --- a/src/shell.c +++ b/src/shell.c @@ -29,6 +29,7 @@ #include "peer.h" #include <arpa/inet.h> +#include <net/if.h> bool fastd_shell_exec(fastd_context_t *ctx, const char *command, const char *dir, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr, int *ret) { @@ -37,7 +38,8 @@ bool fastd_shell_exec(fastd_context_t *ctx, const char *command, const char *dir char *cwd = get_current_dir_name(); if(!chdir(dir)) { - char buf[INET6_ADDRSTRLEN]; + /* both INET6_ADDRSTRLEN and IFNAMESIZE already include space for the zero termination, so there is no need to add space for the '%' here. */ + char buf[INET6_ADDRSTRLEN+IF_NAMESIZE]; snprintf(buf, sizeof(buf), "%u", (unsigned)getpid()); setenv("FASTD_PID", buf, 1); @@ -64,6 +66,12 @@ bool fastd_shell_exec(fastd_context_t *ctx, const char *command, const char *dir case AF_INET6: inet_ntop(AF_INET6, &local_addr->in6.sin6_addr, buf, sizeof(buf)); + + if (IN6_IS_ADDR_LINKLOCAL(&local_addr->in6.sin6_addr)) { + if (if_indextoname(local_addr->in6.sin6_scope_id, buf+strlen(buf)+1)) + buf[strlen(buf)] = '%'; + } + setenv("LOCAL_ADDRESS", buf, 1); snprintf(buf, sizeof(buf), "%u", ntohs(local_addr->in6.sin6_port)); |