add SockAddr::map6to4()
This commit is contained in:
@@ -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; }
|
||||
|
||||
@@ -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
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user