connection: calculate IPv6 address for peer

This commit is contained in:
Thomas Kolb 2024-11-10 21:39:15 +01:00
parent e44cfb722d
commit 4862c09bdd
2 changed files with 25 additions and 0 deletions

View file

@ -41,6 +41,27 @@ result_t connection_init(
ctx->user_context = user_ctx; 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 peers 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; ctx->conn_state = CONN_STATE_INITIALIZED;
return OK; return OK;

View file

@ -14,6 +14,8 @@
#include "packet_queue.h" #include "packet_queue.h"
#include <arpa/inet.h>
struct connection_ctx_s; struct connection_ctx_s;
typedef enum { typedef enum {
@ -44,6 +46,8 @@ typedef struct connection_ctx_s {
ham64_t my_addr; //!< The local link layer address. ham64_t my_addr; //!< The local link layer address.
ham64_t peer_addr; //!< The link layer address of the peer. ham64_t peer_addr; //!< The link layer address of the peer.
struct in6_addr peer_ipv6_addr; //!< The peers IPv6 address (generated from LL address)
uint8_t last_acked_seq; //!< Next sequence number expected by the peer (from last Ack). 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. uint8_t next_expected_seq; //!< Next sequence number expected by us.