face: Set FaceScope to local for IPv4-mapped IPv6 loopback addresses

refs #3682

Change-Id: I33c22d3585c8fee081b1317cdd6587ec0f0e4ef5
diff --git a/daemon/face/websocket-transport.cpp b/daemon/face/websocket-transport.cpp
index 6423330..2fc331c 100644
--- a/daemon/face/websocket-transport.cpp
+++ b/daemon/face/websocket-transport.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2015,  Regents of the University of California,
+ * Copyright (c) 2014-2016,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -30,6 +30,24 @@
 
 NFD_LOG_INIT("WebSocketTransport");
 
+static bool
+isLoopback(const boost::asio::ip::address& addr)
+{
+  if (addr.is_loopback()) {
+    return true;
+  }
+  // Workaround for loopback IPv4-mapped IPv6 addresses
+  // see https://svn.boost.org/trac/boost/ticket/9084
+  else if (addr.is_v6()) {
+    auto addr6 = addr.to_v6();
+    if (addr6.is_v4_mapped()) {
+      return addr6.to_v4().is_loopback();
+    }
+  }
+
+  return false;
+}
+
 WebSocketTransport::WebSocketTransport(websocketpp::connection_hdl hdl,
                                        websocket::Server& server,
                                        time::milliseconds pingInterval)
@@ -41,11 +59,13 @@
   this->setLocalUri(FaceUri(sock.local_endpoint(), "ws"));
   this->setRemoteUri(FaceUri(sock.remote_endpoint(), "wsclient"));
 
-  if (sock.local_endpoint().address().is_loopback() &&
-      sock.remote_endpoint().address().is_loopback())
+  if (isLoopback(sock.local_endpoint().address()) &&
+      isLoopback(sock.remote_endpoint().address())) {
     this->setScope(ndn::nfd::FACE_SCOPE_LOCAL);
-  else
+  }
+  else {
     this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
+  }
 
   this->setPersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
   this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT);
diff --git a/tests/daemon/face/websocket-transport.t.cpp b/tests/daemon/face/websocket-transport.t.cpp
index 8dbd717..993ebc2 100644
--- a/tests/daemon/face/websocket-transport.t.cpp
+++ b/tests/daemon/face/websocket-transport.t.cpp
@@ -115,7 +115,13 @@
                      const time::milliseconds& pongTimeout = time::seconds(1))
   {
     this->serverListen(ep, pongTimeout);
-    std::string uri = "ws://" + ep.address().to_string() + ":" + to_string(ep.port());
+    std::string uri;
+    if (ep.address().is_v6()) {
+      uri = "ws://[" + ep.address().to_string() + "]:" + to_string(ep.port());
+    }
+    else {
+      uri = "ws://" + ep.address().to_string() + ":" + to_string(ep.port());
+    }
     this->clientConnect(uri);
     BOOST_REQUIRE_EQUAL(limitedIo.run(2, // serverHandleOpen, clientHandleOpen
                         time::seconds(1)), LimitedIo::EXCEED_OPS);
@@ -248,6 +254,29 @@
   BOOST_CHECK_EQUAL(transport->getMtu(), MTU_UNLIMITED);
 }
 
+BOOST_AUTO_TEST_CASE(StaticPropertiesLocalIpv4MappedIpv6)
+{
+  auto address4 = getTestIp<ip::address_v4>(LoopbackAddress::Yes);
+  SKIP_IF_IP_UNAVAILABLE(address4);
+  auto address6 = ip::address_v6::v4_mapped(address4);
+  BOOST_REQUIRE(address6.is_v4_mapped());
+  this->endToEndInitialize(ip::tcp::endpoint(address6, 20070));
+
+  checkStaticPropertiesInitialized(*transport);
+
+  BOOST_CHECK_EQUAL(transport->getLocalUri().getScheme(), "ws");
+  BOOST_CHECK_EQUAL(transport->getLocalUri().getHost(), "::ffff:127.0.0.1");
+  BOOST_CHECK_EQUAL(transport->getLocalUri().getPort(), "20070");
+  BOOST_CHECK_EQUAL(transport->getLocalUri().getPath(), "");
+  BOOST_CHECK_EQUAL(transport->getRemoteUri().getScheme(), "wsclient");
+  BOOST_CHECK_EQUAL(transport->getRemoteUri().getHost(), "::ffff:127.0.0.1");
+  BOOST_CHECK_EQUAL(transport->getRemoteUri().getPath(), "");
+  BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_LOCAL);
+  BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
+  BOOST_CHECK_EQUAL(transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT);
+  BOOST_CHECK_EQUAL(transport->getMtu(), MTU_UNLIMITED);
+}
+
 BOOST_AUTO_TEST_CASE(PingPong)
 {
   auto address = getTestIp<ip::address_v4>();