From 505eb464948d32f9ae5d7f8d48d89dde98972930 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 13 Dec 2012 17:51:00 +0100 Subject: Add helper function to simplify v4-mapped addresses --- src/fastd.c | 9 +-------- src/peer.c | 12 ++++++++++++ src/peer.h | 1 + 3 files changed, 14 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/fastd.c b/src/fastd.c index d751a59..9e7e2df 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -748,14 +748,7 @@ static void handle_socket(fastd_context *ctx, fastd_socket *sock) { packet_type = buffer.data; buffer.len = len; - if (recvaddr.sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&recvaddr.in6.sin6_addr)) { - struct sockaddr_in6 mapped = recvaddr.in6; - - memset(&recvaddr, 0, sizeof(recvaddr)); - recvaddr.in.sin_family = AF_INET; - recvaddr.in.sin_port = mapped.sin6_port; - memcpy(&recvaddr.in.sin_addr.s_addr, &mapped.sin6_addr.s6_addr[12], 4); - } + fastd_peer_address_simplify(&recvaddr); fastd_buffer_push_head(&buffer, 1); diff --git a/src/peer.c b/src/peer.c index 9acedcb..a9855bf 100644 --- a/src/peer.c +++ b/src/peer.c @@ -369,6 +369,18 @@ bool fastd_peer_address_equal(const fastd_peer_address *addr1, const fastd_peer_ return true; } +void fastd_peer_address_simplify(fastd_peer_address *addr) { + if (addr->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&addr->in6.sin6_addr)) { + struct sockaddr_in6 mapped = addr->in6; + + memset(addr, 0, sizeof(fastd_peer_address)); + addr->in.sin_family = AF_INET; + addr->in.sin_port = mapped.sin6_port; + memcpy(&addr->in.sin_addr.s_addr, &mapped.sin6_addr.s6_addr[12], 4); + } +} + + bool fastd_peer_claim_address(fastd_context *ctx, fastd_peer *new_peer, fastd_socket *sock, const fastd_peer_address *addr) { if (addr->sa.sa_family == AF_UNSPEC) { if (fastd_peer_is_established(new_peer)) diff --git a/src/peer.h b/src/peer.h index 62ca31d..40ff323 100644 --- a/src/peer.h +++ b/src/peer.h @@ -79,6 +79,7 @@ struct _fastd_peer_eth_addr { bool fastd_peer_address_equal(const fastd_peer_address *addr1, const fastd_peer_address *addr2); +void fastd_peer_address_simplify(fastd_peer_address *addr); fastd_peer_config* fastd_peer_config_new(fastd_context *ctx, fastd_config *conf); void fastd_peer_config_free(fastd_peer_config *peer); -- cgit v1.2.3