Socket/Protocols/INet: fixed link local INet6Address construction from mac/eui64
tho [Wed, 13 Oct 2010 15:27:03 +0000 (15:27 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1735 270642c3-0616-0410-b53a-bc976706d245

senf/Socket/Protocols/INet/INet6Address.cci
senf/Socket/Protocols/INet/INet6Address.test.cc

index 9fc8dd7..290db51 100644 (file)
@@ -77,7 +77,7 @@ prefix_ senf::INet6Address senf::INet6Address::from_mac(senf::MACAddress const &
     INet6Address addr;
     addr[0] = 0xfe;
     addr[1] = 0x80;
-    addr[8] = mac[0];
+    addr[8] = mac[0] ^ 0x2;  // invert the "u" (universal/local) bit; see RFC 4291 Appx. A
     addr[9] = mac[1];
     addr[10] = mac[2];
     addr[11] = 0xff;
@@ -93,7 +93,8 @@ prefix_ senf::INet6Address senf::INet6Address::from_eui64(senf::EUI64 const & eu
     INet6Address addr;
     addr[0] = 0xfe;
     addr[1] = 0x80;
-    std::copy(eui.begin(), eui.end(), addr.begin()+8);
+    addr[8] = eui[0] ^ 0x2;  // invert the "u" (universal/local) bit; see RFC 4291 Appx. A
+    std::copy(eui.begin()+1, eui.end(), addr.begin()+9);
     return addr;
 }
 
index f854f19..07be1b8 100644 (file)
@@ -85,7 +85,6 @@ SENF_AUTO_UNIT_TEST(inet6Address)
         BOOST_CHECK_EQUAL( INet6Address::from_string("1.2.3.4", INet6Address::ResolveINet4),
                            INet6Address::from_string("::ffff:1.2.3.4") );
     }
-
     {
         INet6Address addr (INet6Address::from_string("2001:dead:beef::1002:3004"));
         BOOST_CHECK_EQUAL( addr.network(), senf::INet6Network(
@@ -133,7 +132,6 @@ SENF_AUTO_UNIT_TEST(inet6Address)
         BOOST_CHECK( INet6Address::Loopback );
         BOOST_CHECK( ! INet6Address::None );
     }
-
     {
         std::stringstream str;
         INet6Address addr;
@@ -155,7 +153,6 @@ SENF_AUTO_UNIT_TEST(inet6Address)
         BOOST_CHECK( ! str.fail());
         BOOST_CHECK_EQUAL(addr, INet6Address::from_string("2001:dead:beef::1002:3004"));
     }
-
     {
         INet6Address addr;
         addr.network(0x2000010203040506ull);
@@ -163,6 +160,15 @@ SENF_AUTO_UNIT_TEST(inet6Address)
         addr.id(1u);
         BOOST_CHECK_EQUAL( addr, INet6Address(0x2000u,0x0102u,0x0304u,0x0506u,0u,0u,0u,1u) );
     }
+    {
+        using senf::MACAddress;
+        BOOST_CHECK_EQUAL(
+                INet6Address::from_mac( MACAddress::from_string("00-0C-29-C2-52-FF")),
+                INet6Address::from_string("fe80::20c:29ff:fec2:52ff") );
+        BOOST_CHECK_EQUAL(
+                INet6Address::from_eui64( senf::EUI64::from_mac( MACAddress::from_string("a4:ba:db:fd:b8:76"))),
+                INet6Address::from_string("fe80::a6ba:dbff:fefd:b876") );
+    }
 }
 
 SENF_AUTO_UNIT_TEST(inet6Network)