From 01c11e16b118353ffbb6be7f28cae41bf5d8e200 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Mon, 5 May 2025 08:01:52 -0700 Subject: [PATCH] add SockAddr::map6to4() --- src/osiSockExt.h | 1 + src/util.cpp | 17 +++++++++++++++++ test/testsock.cpp | 16 +++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/osiSockExt.h b/src/osiSockExt.h index 4b8a615..e2f3ad7 100644 --- a/src/osiSockExt.h +++ b/src/osiSockExt.h @@ -78,6 +78,7 @@ public: bool isMCast() const noexcept; SockAddr map4to6() const; + SockAddr map6to4() const; store_t* operator->() { return &store; } const store_t* operator->() const { return &store; } diff --git a/src/util.cpp b/src/util.cpp index 6b7708b..794ee29 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -601,6 +601,23 @@ SockAddr SockAddr::map4to6() const return ret; } +SockAddr SockAddr::map6to4() const +{ + constexpr uint8_t is4[12] = {0,0,0,0, 0,0,0,0, 0,0,0xff,0xff}; + SockAddr ret; + if(family()==AF_INET6 && memcmp(store.in6.sin6_addr.s6_addr, is4, 12)==0) { + ret->in.sin_family = AF_INET; + memcpy(&ret->in.sin_addr.s_addr, + &store.in6.sin6_addr.s6_addr[12], + 4); + ret->in.sin_port = store.in6.sin6_port; + + } else { + ret = *this; + } + return ret; +} + std::string SockAddr::tostring() const { std::ostringstream strm; diff --git a/test/testsock.cpp b/test/testsock.cpp index dde60b3..bc1e3a1 100644 --- a/test/testsock.cpp +++ b/test/testsock.cpp @@ -90,6 +90,20 @@ void testEndPoint() testEq(ep.iface, "ifname"); testEq(ep.ttl, 1); } + { + SockEndpoint ep("[::ffff:192.168.1.1]"); + testEq(ep.addr.tostring(), "[::ffff:192.168.1.1]"); + testEq(ep.addr.map6to4().tostring(), "192.168.1.1"); + testEq(ep.iface, ""); + testEq(ep.ttl, -1); + } + { + SockEndpoint ep("[1234::1]"); + testEq(ep.addr.tostring(), "[1234::1]"); + testEq(ep.addr.map6to4().tostring(), "[1234::1]"); + testEq(ep.iface, ""); + testEq(ep.ttl, -1); + } std::vector bad({ "127.0.0.", @@ -542,7 +556,7 @@ MAIN(testsock) { SockAttach attach; logger_config_env(); - testPlan(101); + testPlan(109); testSetup(); testStackID(); testEndPoint();