From f51b1da9d5e8786e4eebdc6e217f047e5679485d Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Sun, 10 Nov 2024 21:39:15 +0100 Subject: [PATCH] connection: calculate IPv6 address for peer --- impl/src/layer2/connection.c | 21 +++++++++++++++++++++ impl/src/layer2/connection.h | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/impl/src/layer2/connection.c b/impl/src/layer2/connection.c index 069b444..cadd193 100644 --- a/impl/src/layer2/connection.c +++ b/impl/src/layer2/connection.c @@ -41,6 +41,27 @@ result_t connection_init( ctx->user_context = user_ctx; + // calculate IPv6 address + struct in6_addr net_addr; + inet_pton(AF_INET6, IPV6_NET, &net_addr); + + memset(ctx->peer_ipv6_addr.s6_addr, 0, sizeof(ctx->peer_ipv6_addr)); + memcpy(ctx->peer_ipv6_addr.s6_addr, net_addr.s6_addr, 8); // copy the network part + + // fill the host part from the peer’s ham64 address. The bytes are filled in + // reverse order to make the „readable“ IPv6 address as short as possible. + for(uint8_t i = 0; i < peer_addr->length; i++) { + ctx->peer_ipv6_addr.s6_addr[15 - 2*i] = (peer_addr->addr[i] >> 8) & 0xFF; + ctx->peer_ipv6_addr.s6_addr[14 - 2*i] = (peer_addr->addr[i] >> 0) & 0xFF; + } + + // print the address for debugging + char ipv6_str[INET6_ADDRSTRLEN]; + char ham64_str[HAM64_FMT_MAX_LEN]; + ham64_format(peer_addr, ham64_str); + inet_ntop(AF_INET6, &ctx->peer_ipv6_addr, ipv6_str, sizeof(ipv6_str)); + LOG(LVL_DEBUG, "IPv6 address assigned to %s is %s.", ham64_str, ipv6_str); + ctx->conn_state = CONN_STATE_INITIALIZED; return OK; diff --git a/impl/src/layer2/connection.h b/impl/src/layer2/connection.h index 46979be..4e27370 100644 --- a/impl/src/layer2/connection.h +++ b/impl/src/layer2/connection.h @@ -14,6 +14,8 @@ #include "packet_queue.h" +#include + struct connection_ctx_s; typedef enum { @@ -44,6 +46,8 @@ typedef struct connection_ctx_s { ham64_t my_addr; //!< The local link layer address. ham64_t peer_addr; //!< The link layer address of the peer. + struct in6_addr peer_ipv6_addr; //!< The peer’s IPv6 address (generated from LL address) + uint8_t last_acked_seq; //!< Next sequence number expected by the peer (from last Ack). uint8_t next_expected_seq; //!< Next sequence number expected by us.