add SockAddr::map6to4()

This commit is contained in:
Michael Davidsaver
2025-05-05 08:01:52 -07:00
parent bd50b9156f
commit 01c11e16b1
3 changed files with 33 additions and 1 deletions
+1
View File
@@ -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; }
+17
View File
@@ -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;
+15 -1
View File
@@ -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<std::string> bad({
"127.0.0.",
@@ -542,7 +556,7 @@ MAIN(testsock)
{
SockAttach attach;
logger_config_env();
testPlan(101);
testPlan(109);
testSetup();
testStackID();
testEndPoint();