face: Connect faces to the parent (unicast) or associated (multicast) channels

Change-Id: Ie7b2e8fb60050a2bc6e4f5ad053c26e121f3c3d5
Refs: #4973
diff --git a/daemon/face/channel.hpp b/daemon/face/channel.hpp
index 4b4b8c6..ffd06d7 100644
--- a/daemon/face/channel.hpp
+++ b/daemon/face/channel.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2019,  Regents of the University of California,
+ * Copyright (c) 2014-2020,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -38,7 +38,7 @@
  *  A channel can listen on a local endpoint and initiate outgoing connection from a local endpoint.
  *  A channel creates Face objects and retains shared ownership of them.
  */
-class Channel : noncopyable
+class Channel : public std::enable_shared_from_this<Channel>, noncopyable
 {
 public:
   virtual
diff --git a/daemon/face/ethernet-channel.cpp b/daemon/face/ethernet-channel.cpp
index 725bbfd..512d078 100644
--- a/daemon/face/ethernet-channel.cpp
+++ b/daemon/face/ethernet-channel.cpp
@@ -209,6 +209,7 @@
   auto transport = make_unique<UnicastEthernetTransport>(*m_localEndpoint, remoteEndpoint,
                                                          params.persistency, m_idleFaceTimeout);
   auto face = make_shared<Face>(std::move(linkService), std::move(transport));
+  face->setChannel(shared_from_this()); // use weak_from_this() in C++17
 
   m_channelFaces[remoteEndpoint] = face;
   connectFaceClosedSignal(*face, [this, remoteEndpoint] {
diff --git a/daemon/face/ethernet-factory.cpp b/daemon/face/ethernet-factory.cpp
index 9659985..fec671f 100644
--- a/daemon/face/ethernet-factory.cpp
+++ b/daemon/face/ethernet-factory.cpp
@@ -257,6 +257,9 @@
   m_mcastFaces[key] = face;
   connectFaceClosedSignal(*face, [this, key] { m_mcastFaces.erase(key); });
 
+  auto channelIt = m_channels.find(netif.getName());
+  face->setChannel(channelIt != m_channels.end() ? channelIt->second : nullptr);
+
   return face;
 }
 
diff --git a/daemon/face/face.hpp b/daemon/face/face.hpp
index 4a5948b..1bb474e 100644
--- a/daemon/face/face.hpp
+++ b/daemon/face/face.hpp
@@ -34,6 +34,8 @@
 namespace nfd {
 namespace face {
 
+class Channel;
+
 /** \brief indicates the state of a face
  */
 typedef TransportState FaceState;
@@ -171,11 +173,30 @@
   const FaceCounters&
   getCounters() const;
 
+  /**
+   * \brief Get channel on which face was created (unicast) or the associated channel (multicast)
+   */
+  weak_ptr<Channel>
+  getChannel() const
+  {
+    return m_channel;
+  }
+
+  /**
+   * \brief Set channel on which face was created (unicast) or the associated channel (multicast)
+   */
+  void
+  setChannel(weak_ptr<Channel> channel)
+  {
+    m_channel = std::move(channel);
+  }
+
 private:
   FaceId m_id;
   unique_ptr<LinkService> m_service;
   unique_ptr<Transport> m_transport;
   FaceCounters m_counters;
+  weak_ptr<Channel> m_channel;
 };
 
 inline LinkService*
diff --git a/daemon/face/tcp-channel.cpp b/daemon/face/tcp-channel.cpp
index cfbaed6..6cf50c4 100644
--- a/daemon/face/tcp-channel.cpp
+++ b/daemon/face/tcp-channel.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2019,  Regents of the University of California,
+ * Copyright (c) 2014-2020,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -129,6 +129,7 @@
                                           socket.remote_endpoint().address());
     auto transport = make_unique<TcpTransport>(std::move(socket), params.persistency, faceScope);
     face = make_shared<Face>(std::move(linkService), std::move(transport));
+    face->setChannel(shared_from_this()); // use weak_from_this() in C++17
 
     m_channelFaces[remoteEndpoint] = face;
     connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); });
diff --git a/daemon/face/udp-channel.cpp b/daemon/face/udp-channel.cpp
index 4776661..f2ccb9c 100644
--- a/daemon/face/udp-channel.cpp
+++ b/daemon/face/udp-channel.cpp
@@ -188,6 +188,7 @@
   auto transport = make_unique<UnicastUdpTransport>(std::move(socket), params.persistency,
                                                     m_idleFaceTimeout);
   auto face = make_shared<Face>(std::move(linkService), std::move(transport));
+  face->setChannel(shared_from_this()); // use weak_from_this() in C++17
 
   m_channelFaces[remoteEndpoint] = face;
   connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); });
diff --git a/daemon/face/udp-factory.cpp b/daemon/face/udp-factory.cpp
index b15b762..993f61a 100644
--- a/daemon/face/udp-factory.cpp
+++ b/daemon/face/udp-factory.cpp
@@ -362,6 +362,13 @@
   m_mcastFaces[localEp] = face;
   connectFaceClosedSignal(*face, [this, localEp] { m_mcastFaces.erase(localEp); });
 
+  // Associate with the first available channel of the same protocol family
+  auto channelIt = std::find_if(m_channels.begin(), m_channels.end(),
+                                [isV4 = localEp.address().is_v4()] (const auto& it) {
+                                  return it.first.address().is_v4() == isV4;
+                                });
+  face->setChannel(channelIt != m_channels.end() ? channelIt->second : nullptr);
+
   return face;
 }
 
diff --git a/daemon/face/unix-stream-channel.cpp b/daemon/face/unix-stream-channel.cpp
index b60ba78..760a264 100644
--- a/daemon/face/unix-stream-channel.cpp
+++ b/daemon/face/unix-stream-channel.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2019,  Regents of the University of California,
+ * Copyright (c) 2014-2020,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -137,6 +137,7 @@
   auto linkService = make_unique<GenericLinkService>(options);
   auto transport = make_unique<UnixStreamTransport>(std::move(m_socket));
   auto face = make_shared<Face>(std::move(linkService), std::move(transport));
+  face->setChannel(shared_from_this()); // use weak_from_this() in C++17
 
   ++m_size;
   connectFaceClosedSignal(*face, [this] { --m_size; });
diff --git a/daemon/face/websocket-channel.cpp b/daemon/face/websocket-channel.cpp
index a2660c6..13f6f38 100644
--- a/daemon/face/websocket-channel.cpp
+++ b/daemon/face/websocket-channel.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2019,  Regents of the University of California,
+ * Copyright (c) 2014-2020,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -126,6 +126,7 @@
   auto linkService = make_unique<GenericLinkService>();
   auto transport = make_unique<WebSocketTransport>(hdl, m_server, m_pingInterval);
   auto face = make_shared<Face>(std::move(linkService), std::move(transport));
+  face->setChannel(shared_from_this()); // use weak_from_this() in C++17
 
   BOOST_ASSERT(m_channelFaces.count(hdl) == 0);
   m_channelFaces[hdl] = face;