face: reimplement EndpointId with std::variant

Refs: #5041
Change-Id: Ib8aced49a7aa14b137fb06de4a0ae8b979f07587
diff --git a/daemon/face/datagram-transport.hpp b/daemon/face/datagram-transport.hpp
index e053af2..51f06a0 100644
--- a/daemon/face/datagram-transport.hpp
+++ b/daemon/face/datagram-transport.hpp
@@ -37,15 +37,18 @@
 struct Unicast {};
 struct Multicast {};
 
-/** \brief Implements Transport for datagram-based protocols.
+/**
+ * \brief Implements Transport for datagram-based protocols.
  *
- *  \tparam Protocol a datagram-based protocol in Boost.Asio
+ * \tparam Protocol A datagram-based protocol in Boost.Asio
+ * \tparam Addressing The addressing mode, either Unicast or Multicast
  */
-template<class Protocol, class Addressing = Unicast>
+template<class Protocol, class Addressing>
 class DatagramTransport : public Transport
 {
 public:
   using protocol = Protocol;
+  using addressing = Addressing;
 
   /** \brief Construct datagram transport.
    *
@@ -85,9 +88,6 @@
   void
   resetRecentlyReceived();
 
-  static EndpointId
-  makeEndpointId(const typename protocol::endpoint& ep);
-
 protected:
   typename protocol::socket m_socket;
   typename protocol::endpoint m_sender;
@@ -190,7 +190,10 @@
   }
   m_hasRecentlyReceived = true;
 
-  this->receive(element, makeEndpointId(m_sender));
+  if constexpr (std::is_same_v<addressing, Multicast>)
+    this->receive(element, m_sender);
+  else
+    this->receive(element);
 }
 
 template<class T, class U>
@@ -254,13 +257,6 @@
   m_hasRecentlyReceived = false;
 }
 
-template<class T, class U>
-EndpointId
-DatagramTransport<T, U>::makeEndpointId(const typename protocol::endpoint&)
-{
-  return 0;
-}
-
 } // namespace nfd::face
 
 #endif // NFD_DAEMON_FACE_DATAGRAM_TRANSPORT_HPP
diff --git a/daemon/face/ethernet-transport.cpp b/daemon/face/ethernet-transport.cpp
index 53de0b7..c122a80 100644
--- a/daemon/face/ethernet-transport.cpp
+++ b/daemon/face/ethernet-transport.cpp
@@ -29,8 +29,6 @@
 
 #include <pcap/pcap.h>
 
-#include <cstring> // for memcpy()
-
 #include <boost/endian/conversion.hpp>
 
 namespace nfd::face {
@@ -207,13 +205,7 @@
   }
   m_hasRecentlyReceived = true;
 
-  static_assert(sizeof(EndpointId) >= ethernet::ADDR_LEN, "EndpointId is too small");
-  EndpointId endpoint = 0;
-  if (m_destAddress.isMulticast()) {
-    std::memcpy(&endpoint, sender.data(), sender.size());
-  }
-
-  this->receive(element, endpoint);
+  this->receive(element, m_destAddress.isMulticast() ? sender : EndpointId{});
 }
 
 void
diff --git a/daemon/face/face-common.hpp b/daemon/face/face-common.hpp
index 039d2fe..996d7b6 100644
--- a/daemon/face/face-common.hpp
+++ b/daemon/face/face-common.hpp
@@ -26,12 +26,14 @@
 #ifndef NFD_DAEMON_FACE_FACE_COMMON_HPP
 #define NFD_DAEMON_FACE_FACE_COMMON_HPP
 
-#include "core/common.hpp"
+#include "ethernet-protocol.hpp"
+#include "udp-protocol.hpp"
 #include "common/logger.hpp"
 
 #include <ndn-cxx/encoding/nfd-constants.hpp>
 
 #include <boost/logic/tribool.hpp>
+#include <variant>
 
 namespace nfd {
 namespace face {
@@ -62,15 +64,17 @@
  */
 constexpr ssize_t MIN_MTU = 64;
 
-/** \brief Identifies a remote endpoint on the link.
+/**
+ * \brief Identifies a remote endpoint on the link.
  *
- *  This ID is only meaningful in the context of the same Transport.
- *  Incoming packets from the same remote endpoint have the same EndpointId,
- *  and incoming packets from different remote endpoints have different EndpointIds.
+ * This ID is only meaningful in the context of the same Transport.
+ * Incoming packets from the same remote endpoint have the same EndpointId,
+ * and incoming packets from different remote endpoints have different EndpointIds.
  *
- *  Typically, a point-to-point Transport has only one meaningful EndpointId (usually 0).
+ * Typically, a point-to-point Transport has only one meaningful EndpointId,
+ * represented by `std::monostate`.
  */
-using EndpointId = uint64_t;
+using EndpointId = std::variant<std::monostate, ethernet::Address, udp::Endpoint>;
 
 /** \brief Parameters used to set Transport properties or LinkService options on a newly created face.
  *
diff --git a/daemon/face/face-endpoint.cpp b/daemon/face/face-endpoint.cpp
new file mode 100644
index 0000000..9871b2e
--- /dev/null
+++ b/daemon/face/face-endpoint.cpp
@@ -0,0 +1,53 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2022,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "face-endpoint.hpp"
+
+#include <boost/hana/functional/overload.hpp>
+
+namespace nfd {
+
+FaceEndpoint::FaceEndpoint(Face& face, const EndpointId& endpoint)
+  : face(face)
+  , endpoint(endpoint)
+{
+}
+
+void
+FaceEndpoint::print(std::ostream& os) const
+{
+  std::visit(boost::hana::overload(
+    [&] (std::monostate) {
+      os << face.getId();
+    },
+    [&] (const ethernet::Address& ep) {
+      os << '(' << face.getId() << ", " << ep << ')';
+    },
+    [&] (const udp::Endpoint& ep) {
+      os << '(' << face.getId() << ", " << ep << ')';
+    }), endpoint);
+}
+
+} // namespace nfd
diff --git a/daemon/face/face-endpoint.hpp b/daemon/face/face-endpoint.hpp
index 366c743..c9badef 100644
--- a/daemon/face/face-endpoint.hpp
+++ b/daemon/face/face-endpoint.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2021,  Regents of the University of California,
+ * Copyright (c) 2014-2022,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -30,16 +30,25 @@
 
 namespace nfd {
 
-/** \brief Represents a face-endpoint pair in the forwarder.
- *  \sa face::Face, face::EndpointId
+/**
+ * \brief Represents a face-endpoint pair in the forwarder.
+ * \sa face::Face, face::EndpointId
  */
 class FaceEndpoint
 {
 public:
-  FaceEndpoint(Face& face, EndpointId endpoint)
-    : face(face)
-    , endpoint(endpoint)
+  explicit
+  FaceEndpoint(Face& face, const EndpointId& endpoint = {});
+
+private:
+  void
+  print(std::ostream& os) const;
+
+  friend std::ostream&
+  operator<<(std::ostream& os, const FaceEndpoint& fe)
   {
+    fe.print(os);
+    return os;
   }
 
 public:
@@ -47,12 +56,6 @@
   const EndpointId endpoint;
 };
 
-inline std::ostream&
-operator<<(std::ostream& os, const FaceEndpoint& fe)
-{
-  return os << '(' << fe.face.getId() << ',' << fe.endpoint << ')';
-}
-
 } // namespace nfd
 
 #endif // NFD_DAEMON_FACE_FACE_ENDPOINT_HPP
diff --git a/daemon/face/lp-reassembler.cpp b/daemon/face/lp-reassembler.cpp
index 6c27d80..7f101bc 100644
--- a/daemon/face/lp-reassembler.cpp
+++ b/daemon/face/lp-reassembler.cpp
@@ -40,7 +40,7 @@
 }
 
 std::tuple<bool, Block, lp::Packet>
-LpReassembler::receiveFragment(EndpointId remoteEndpoint, const lp::Packet& packet)
+LpReassembler::receiveFragment(const EndpointId& remoteEndpoint, const lp::Packet& packet)
 {
   BOOST_ASSERT(packet.has<lp::FragmentField>());
 
diff --git a/daemon/face/lp-reassembler.hpp b/daemon/face/lp-reassembler.hpp
index 8009c67..30fec04 100644
--- a/daemon/face/lp-reassembler.hpp
+++ b/daemon/face/lp-reassembler.hpp
@@ -79,19 +79,20 @@
    *  \throw tlv::Error packet is malformed
    */
   std::tuple<bool, Block, lp::Packet>
-  receiveFragment(EndpointId remoteEndpoint, const lp::Packet& packet);
+  receiveFragment(const EndpointId& remoteEndpoint, const lp::Packet& packet);
 
   /** \brief count of partial packets
    */
   size_t
   size() const;
 
-  /** \brief signals before a partial packet is dropped due to timeout
+  /**
+   * \brief Notifies before a partial packet is dropped due to timeout.
    *
-   *  If a partial packet is incomplete and no new fragment is received
-   *  within Options::reassemblyTimeout, it would be dropped due to timeout.
-   *  Before it's erased, this signal is emitted with the remote endpoint,
-   *  and the number of fragments being dropped.
+   * If a partial packet is incomplete and no new fragments are received within
+   * Options::reassemblyTimeout, the partial packet is dropped due to timeout.
+   * Before dropping the packet, this signal is emitted with the remote endpoint
+   * and the number of fragments being dropped.
    */
   signal::Signal<LpReassembler, EndpointId, size_t> beforeTimeout;
 
diff --git a/daemon/face/multicast-udp-transport.cpp b/daemon/face/multicast-udp-transport.cpp
index e0b7ac5..bcb799e 100644
--- a/daemon/face/multicast-udp-transport.cpp
+++ b/daemon/face/multicast-udp-transport.cpp
@@ -31,7 +31,6 @@
 
 #include <boost/asio/ip/multicast.hpp>
 #include <boost/asio/ip/v6_only.hpp>
-#include <boost/functional/hash.hpp>
 
 #ifdef __linux__
 #include <cerrno>       // for errno
@@ -191,21 +190,4 @@
   }
 }
 
-template<>
-EndpointId
-DatagramTransport<boost::asio::ip::udp, Multicast>::makeEndpointId(const protocol::endpoint& ep)
-{
-  if (ep.address().is_v4()) {
-    return (static_cast<uint64_t>(ep.port()) << 32) |
-            static_cast<uint64_t>(ep.address().to_v4().to_ulong());
-  }
-  else {
-    size_t seed = 0;
-    const auto& addrBytes = ep.address().to_v6().to_bytes();
-    boost::hash_range(seed, addrBytes.begin(), addrBytes.end());
-    boost::hash_combine(seed, ep.port());
-    return seed;
-  }
-}
-
 } // namespace nfd::face
diff --git a/daemon/face/multicast-udp-transport.hpp b/daemon/face/multicast-udp-transport.hpp
index 5a19a0c..23c1e1d 100644
--- a/daemon/face/multicast-udp-transport.hpp
+++ b/daemon/face/multicast-udp-transport.hpp
@@ -35,14 +35,6 @@
 
 NFD_LOG_MEMBER_DECL_SPECIALIZED((DatagramTransport<boost::asio::ip::udp, Multicast>));
 
-// Explicit specialization of makeEndpointId for the UDP multicast case.
-// Note that this "shall be declared before the first use of the specialization
-// that would cause an implicit instantiation to take place, in every translation
-// unit in which such a use occurs".
-template<>
-EndpointId
-DatagramTransport<boost::asio::ip::udp, Multicast>::makeEndpointId(const protocol::endpoint& ep);
-
 /**
  * \brief A Transport that communicates on a UDP multicast group
  */
diff --git a/daemon/face/transport.hpp b/daemon/face/transport.hpp
index ed91cbc..1c24c57 100644
--- a/daemon/face/transport.hpp
+++ b/daemon/face/transport.hpp
@@ -31,7 +31,8 @@
 
 namespace nfd::face {
 
-/** \brief Indicates the state of a transport.
+/**
+ * \brief Indicates the state of a transport.
  */
 enum class TransportState {
   NONE,
@@ -263,13 +264,14 @@
   }
 
 protected: // upper interface to be invoked by subclass
-  /** \brief Pass a received link-layer packet to the upper layer for further processing
-   *  \param packet the received packet, must be a valid and well-formed TLV block
-   *  \param endpoint the source endpoint
-   *  \warning Behavior is undefined if packet size exceeds the MTU limit
+  /**
+   * \brief Pass a received link-layer packet to the upper layer for further processing.
+   * \param packet The received packet, must be a valid and well-formed TLV block
+   * \param endpoint The source endpoint, optional for unicast transports
+   * \warning Behavior is undefined if packet size exceeds the MTU limit.
    */
   void
-  receive(const Block& packet, const EndpointId& endpoint = 0);
+  receive(const Block& packet, const EndpointId& endpoint = {});
 
 protected: // properties to be set by subclass
   void
diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp
index d984f0c..f4f5928 100644
--- a/daemon/fw/forwarder.cpp
+++ b/daemon/fw/forwarder.cpp
@@ -221,7 +221,7 @@
 
   // dispatch to strategy: after receive Interest
   m_strategyChoice.findEffectiveStrategy(*pitEntry)
-    .afterReceiveInterest(interest, FaceEndpoint(ingress.face, 0), pitEntry);
+    .afterReceiveInterest(interest, FaceEndpoint(ingress.face), pitEntry);
 }
 
 void