diff --git a/daemon/core/face-uri.cpp b/daemon/core/face-uri.cpp index 0a3f1d43..cc6c8c8f 100644 --- a/daemon/core/face-uri.cpp +++ b/daemon/core/face-uri.cpp @@ -47,7 +47,7 @@ FaceUri::parse(const std::string& uri) boost::regex v6Exp("^\\[(([a-fA-F0-9:]+))\\](:(\\d+))?$"); // [host]:port boost::regex v4Exp("^((\\d+\\.){3}\\d+)(:(\\d+))?$"); - boost::regex hostExp("^(([^:]+))(:(\\d+))?$"); // host:port + boost::regex hostExp("^(([^:]*))(:(\\d+))?$"); // host:port boost::smatch match; m_isV6 = boost::regex_match(authority, match, v6Exp); diff --git a/daemon/face/datagram-face.hpp b/daemon/face/datagram-face.hpp index f4f9a18f..78a4a12f 100644 --- a/daemon/face/datagram-face.hpp +++ b/daemon/face/datagram-face.hpp @@ -18,7 +18,8 @@ public: typedef T protocol; explicit - DatagramFace(const shared_ptr& socket); + DatagramFace(const FaceUri& uri, + const shared_ptr& socket); virtual ~DatagramFace(); @@ -64,8 +65,10 @@ protected: template inline -DatagramFace::DatagramFace(const shared_ptr& socket) - : m_socket(socket) +DatagramFace::DatagramFace(const FaceUri& uri, + const shared_ptr& socket) + : Face(uri) + , m_socket(socket) { m_socket->async_receive(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE), 0, bind(&DatagramFace::handleReceive, this, _1, _2)); diff --git a/daemon/face/ethernet-face.cpp b/daemon/face/ethernet-face.cpp index 2d3b8e3d..f98a3da9 100644 --- a/daemon/face/ethernet-face.cpp +++ b/daemon/face/ethernet-face.cpp @@ -34,7 +34,8 @@ static const uint8_t MAX_PADDING[ethernet::MIN_DATA_LEN] = { EthernetFace::EthernetFace(const shared_ptr& socket, const ethernet::Endpoint& interface, const ethernet::Address& address) - : m_socket(socket) + : Face(FaceUri("ether://" + interface + "/" + address.toString(':'))) + , m_socket(socket) , m_interface(interface) , m_destAddress(address) { diff --git a/daemon/face/face.cpp b/daemon/face/face.cpp index 8e81217f..7268ee4d 100644 --- a/daemon/face/face.cpp +++ b/daemon/face/face.cpp @@ -11,9 +11,10 @@ namespace nfd { NFD_LOG_INIT("Face") -Face::Face(bool isLocal) +Face::Face(const FaceUri& uri, bool isLocal) : m_id(INVALID_FACEID) , m_isLocal(isLocal) + , m_uri(uri) { } diff --git a/daemon/face/face.hpp b/daemon/face/face.hpp index f20c37be..893a2536 100644 --- a/daemon/face/face.hpp +++ b/daemon/face/face.hpp @@ -37,8 +37,7 @@ public: Error(const std::string& what) : std::runtime_error(what) {} }; - explicit - Face(bool isLocal = false); + Face(const FaceUri& uri, bool isLocal = false); virtual ~Face(); @@ -104,6 +103,9 @@ public: const FaceCounters& getCounters() const; + const FaceUri& + getUri() const; + protected: // this is a non-virtual method bool @@ -121,6 +123,7 @@ private: std::string m_description; bool m_isLocal; // for scoping purposes FaceCounters m_counters; + FaceUri m_uri; // allow setting FaceId friend class FaceTable; @@ -145,6 +148,12 @@ Face::getMutableCounters() return m_counters; } +inline const FaceUri& +Face::getUri() const +{ + return m_uri; +} + } // namespace nfd #endif // NFD_FACE_FACE_HPP diff --git a/daemon/face/local-face.hpp b/daemon/face/local-face.hpp index b7d973a8..7023efbb 100644 --- a/daemon/face/local-face.hpp +++ b/daemon/face/local-face.hpp @@ -31,7 +31,8 @@ enum LocalControlHeaderFeature class LocalFace : public Face { public: - LocalFace(); + explicit + LocalFace(const FaceUri& uri); /** \brief get whether a LocalControlHeader feature is enabled * @@ -81,8 +82,8 @@ private: }; inline -LocalFace::LocalFace() - : Face(true) +LocalFace::LocalFace(const FaceUri& uri) + : Face(uri, true) , m_localControlHeaderFeatures(LOCAL_CONTROL_HEADER_FEATURE_MAX) { } diff --git a/daemon/face/multicast-udp-face.cpp b/daemon/face/multicast-udp-face.cpp index edd2ff43..e56c1478 100644 --- a/daemon/face/multicast-udp-face.cpp +++ b/daemon/face/multicast-udp-face.cpp @@ -12,7 +12,7 @@ NFD_LOG_INIT("MulticastUdpFace"); MulticastUdpFace::MulticastUdpFace(const shared_ptr& socket) - : UdpFace(socket) + : DatagramFace(FaceUri(socket->local_endpoint()), socket) { NFD_LOG_DEBUG("Face creation. Multicast group: " << m_socket->local_endpoint()); diff --git a/daemon/face/multicast-udp-face.hpp b/daemon/face/multicast-udp-face.hpp index 46404da9..2697a7f5 100644 --- a/daemon/face/multicast-udp-face.hpp +++ b/daemon/face/multicast-udp-face.hpp @@ -7,7 +7,7 @@ #ifndef NFD_FACE_MULTICAST_UDP_FACE_HPP #define NFD_FACE_MULTICAST_UDP_FACE_HPP -#include "udp-face.hpp" +#include "datagram-face.hpp" namespace nfd { @@ -16,7 +16,7 @@ namespace nfd * \brief Implementation of Face abstraction that uses * multicast UDP as underlying transport mechanism */ -class MulticastUdpFace : public UdpFace +class MulticastUdpFace : public DatagramFace { public: typedef boost::asio::ip::udp protocol; diff --git a/daemon/face/stream-face.hpp b/daemon/face/stream-face.hpp index db376602..8a0f9608 100644 --- a/daemon/face/stream-face.hpp +++ b/daemon/face/stream-face.hpp @@ -25,7 +25,8 @@ public: * \brief Create instance of StreamFace */ explicit - StreamFace(const shared_ptr& socket); + StreamFace(const FaceUri& uri, + const shared_ptr& socket); virtual ~StreamFace(); @@ -95,15 +96,17 @@ struct StreamFaceValidator }; -template +template inline -StreamFace::StreamFace(const shared_ptr& socket) - : m_socket(socket) +StreamFace::StreamFace(const FaceUri& uri, + const shared_ptr& socket) + : FaceBase(uri) + , m_socket(socket) , m_inputBufferSize(0) { - StreamFaceValidator::validateSocket(*socket); + StreamFaceValidator::validateSocket(*socket); m_socket->async_receive(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE), 0, - bind(&StreamFace::handleReceive, this, _1, _2)); + bind(&StreamFace::handleReceive, this, _1, _2)); } template diff --git a/daemon/face/tcp-face.cpp b/daemon/face/tcp-face.cpp index 4f8cbab5..6f85a213 100644 --- a/daemon/face/tcp-face.cpp +++ b/daemon/face/tcp-face.cpp @@ -17,14 +17,14 @@ NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(StreamFace, TcpLocalFace::protocol, LocalFace, "TcpLocalFace"); TcpFace::TcpFace(const shared_ptr& socket) - : StreamFace(socket) + : StreamFace(FaceUri(socket->remote_endpoint()), socket) { } // TcpLocalFace::TcpLocalFace(const shared_ptr& socket) - : StreamFace(socket) + : StreamFace(FaceUri(socket->remote_endpoint()), socket) { } diff --git a/daemon/face/udp-face.cpp b/daemon/face/udp-face.cpp index 0ee40cc7..dc516a74 100644 --- a/daemon/face/udp-face.cpp +++ b/daemon/face/udp-face.cpp @@ -11,7 +11,7 @@ namespace nfd { NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(DatagramFace, UdpFace::protocol, "UdpFace"); UdpFace::UdpFace(const shared_ptr& socket) - : DatagramFace(socket) + : DatagramFace(FaceUri(socket->remote_endpoint()), socket) { } diff --git a/daemon/face/unix-stream-face.cpp b/daemon/face/unix-stream-face.cpp index 27ba6937..43e2369b 100644 --- a/daemon/face/unix-stream-face.cpp +++ b/daemon/face/unix-stream-face.cpp @@ -16,7 +16,7 @@ NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(StreamFace, "UnixStreamFace"); UnixStreamFace::UnixStreamFace(const shared_ptr& socket) - : StreamFace(socket) + : StreamFace(FaceUri(socket->local_endpoint()), socket) { } diff --git a/daemon/mgmt/internal-face.cpp b/daemon/mgmt/internal-face.cpp index a6c116bb..f7e5018a 100644 --- a/daemon/mgmt/internal-face.cpp +++ b/daemon/mgmt/internal-face.cpp @@ -11,7 +11,7 @@ namespace nfd { NFD_LOG_INIT("InternalFace"); InternalFace::InternalFace() - : Face(true) + : Face(FaceUri("internal://"), true) { } diff --git a/tests/face/dummy-face.hpp b/tests/face/dummy-face.hpp index 969053a0..e64a1625 100644 --- a/tests/face/dummy-face.hpp +++ b/tests/face/dummy-face.hpp @@ -20,6 +20,11 @@ template class DummyFaceImpl : public FaceBase { public: + DummyFaceImpl() + : FaceBase(FaceUri("dummy://")) + { + } + virtual void sendInterest(const Interest& interest) { diff --git a/tests/face/ethernet.cpp b/tests/face/ethernet.cpp index 9322d4cc..2e731bf1 100644 --- a/tests/face/ethernet.cpp +++ b/tests/face/ethernet.cpp @@ -75,6 +75,9 @@ BOOST_AUTO_TEST_CASE(SendPacket) BOOST_REQUIRE(static_cast(face)); BOOST_CHECK_EQUAL(face->isLocal(), false); + BOOST_CHECK_EQUAL(face->getUri().toString(), + "ether://" + interfaces[0] + "/" + + ethernet::getDefaultMulticastAddress().toString(':')); Interest interest1("ndn:/TpnzGvW9R"); Data data1 ("ndn:/KfczhUqVix"); diff --git a/tests/face/tcp.cpp b/tests/face/tcp.cpp index 9223283a..47c3b5b6 100644 --- a/tests/face/tcp.cpp +++ b/tests/face/tcp.cpp @@ -162,20 +162,19 @@ public: }; -BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture) +BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture) { TcpFactory factory; shared_ptr channel1 = factory.createChannel("127.0.0.1", "20070"); - shared_ptr channel2 = factory.createChannel("127.0.0.1", "20071"); + factory.createChannel("127.0.0.1", "20071"); channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); - channel2->connect("127.0.0.1", "20070", - bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), - bind(&EndToEndFixture::channel2_onConnectFailed, this, _1), - time::seconds(4)); + factory.createFace(FaceUri("tcp://127.0.0.1:20070"), + bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), + bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, "TcpChannel error: cannot connect or cannot accept connection"); @@ -183,6 +182,74 @@ BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture) BOOST_REQUIRE(static_cast(m_face1)); BOOST_REQUIRE(static_cast(m_face2)); + BOOST_CHECK_EQUAL(m_face2->getUri().toString(), "tcp4://127.0.0.1:20070"); + // face1 has an unknown URI, since the source port is automatically chosen by OS + + BOOST_CHECK_EQUAL(m_face1->isLocal(), true); + BOOST_CHECK_EQUAL(m_face2->isLocal(), true); + + BOOST_CHECK_EQUAL(static_cast(dynamic_pointer_cast(m_face1)), true); + BOOST_CHECK_EQUAL(static_cast(dynamic_pointer_cast(m_face2)), true); + + // integrated tests needs to check that TcpFace for non-loopback fails these tests... + + Interest interest1("ndn:/TpnzGvW9R"); + Data data1 ("ndn:/KfczhUqVix"); + data1.setContent(0, 0); + Interest interest2("ndn:/QWiIMfj5sL"); + Data data2 ("ndn:/XNBV796f"); + data2.setContent(0, 0); + + ndn::SignatureSha256WithRsa fakeSignature; + fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast(0), 0)); + + // set fake signature on data1 and data2 + data1.setSignature(fakeSignature); + data2.setSignature(fakeSignature); + + m_face1->sendInterest(interest1); + m_face1->sendData (data1 ); + m_face2->sendInterest(interest2); + m_face2->sendData (data2 ); + + BOOST_CHECK_MESSAGE(m_limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS, + "TcpChannel error: cannot send or receive Interest/Data packets"); + + + BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1); + BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1); + BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1); + BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1); + + BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName()); + BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName()); + BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName()); + BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName()); +} + +BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture) +{ + TcpFactory factory; + + shared_ptr channel1 = factory.createChannel("::1", "20070"); + shared_ptr channel2 = factory.createChannel("::1", "20071"); + + channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), + bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); + + factory.createFace(FaceUri("tcp://[::1]:20070"), + bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), + bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); + + BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, + "TcpChannel error: cannot connect or cannot accept connection"); + + BOOST_REQUIRE(static_cast(m_face1)); + BOOST_REQUIRE(static_cast(m_face2)); + + BOOST_CHECK_EQUAL(m_face2->getUri().toString(), "tcp6://[::1]:20070"); + // face1 has an unknown URI, since the source port is automatically chosen by OS + BOOST_CHECK_EQUAL(m_face1->isLocal(), true); BOOST_CHECK_EQUAL(m_face2->isLocal(), true); diff --git a/tests/face/udp.cpp b/tests/face/udp.cpp index f877e2ff..3c86a6de 100644 --- a/tests/face/udp.cpp +++ b/tests/face/udp.cpp @@ -320,16 +320,11 @@ public: }; -BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture) +BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture) { UdpFactory factory; - shared_ptr channel2 = factory.createChannel("127.0.0.1", "20071"); - - - //channel2->connect("127.0.0.1", "20070", - // bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), - // bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); + factory.createChannel("127.0.0.1", "20071"); factory.createFace(FaceUri("udp4://127.0.0.1:20070"), bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), @@ -340,12 +335,14 @@ BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture) "UdpChannel error: cannot connect or cannot accept connection"); BOOST_REQUIRE(static_cast(m_face2)); + BOOST_CHECK_EQUAL(m_face2->getUri().toString(), "udp4://127.0.0.1:20070"); + BOOST_CHECK_EQUAL(m_face2->isLocal(), false); + // m_face1 is not created yet shared_ptr channel1 = factory.createChannel("127.0.0.1", "20070"); channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); - Interest interest1("ndn:/TpnzGvW9R"); Data data1 ("ndn:/KfczhUqVix"); data1.setContent(0, 0); @@ -375,6 +372,8 @@ BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture) "UdpChannel error: cannot send or receive Interest/Data packets"); BOOST_REQUIRE(static_cast(m_face1)); + BOOST_CHECK_EQUAL(m_face1->getUri().toString(), "udp4://127.0.0.1:20071"); + BOOST_CHECK_EQUAL(m_face1->isLocal(), false); m_face1->sendInterest(interest1); m_face1->sendData (data1 ); @@ -408,7 +407,95 @@ BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture) BOOST_CHECK_EQUAL(m_face1_receivedInterests[1].getName(), interest3.getName()); BOOST_CHECK_EQUAL(m_face1_receivedDatas [1].getName(), data3.getName()); +} +BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture) +{ + UdpFactory factory; + + factory.createChannel("::1", "20071"); + + factory.createFace(FaceUri("udp://[::1]:20070"), + bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), + bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); + + + BOOST_CHECK_MESSAGE(m_limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS, + "UdpChannel error: cannot connect or cannot accept connection"); + + BOOST_REQUIRE(static_cast(m_face2)); + BOOST_CHECK_EQUAL(m_face2->getUri().toString(), "udp6://[::1]:20070"); + BOOST_CHECK_EQUAL(m_face2->isLocal(), false); + // m_face1 is not created yet + + shared_ptr channel1 = factory.createChannel("::1", "20070"); + channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), + bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); + + Interest interest1("ndn:/TpnzGvW9R"); + Data data1 ("ndn:/KfczhUqVix"); + data1.setContent(0, 0); + Interest interest2("ndn:/QWiIMfj5sL"); + Data data2 ("ndn:/XNBV796f"); + data2.setContent(0, 0); + Interest interest3("ndn:/QWiIhjgkj5sL"); + Data data3 ("ndn:/XNBV794f"); + data3.setContent(0, 0); + + + ndn::SignatureSha256WithRsa fakeSignature; + fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, + reinterpret_cast(0), + 0)); + + // set fake signature on data1 and data2 + data1.setSignature(fakeSignature); + data2.setSignature(fakeSignature); + data3.setSignature(fakeSignature); + + m_face2->sendInterest(interest2); + m_face2->sendData (data2 ); + + BOOST_CHECK_MESSAGE(m_limitedIo.run(3,//2 send + 1 listen return + time::seconds(1)) == LimitedIo::EXCEED_OPS, + "UdpChannel error: cannot send or receive Interest/Data packets"); + + BOOST_REQUIRE(static_cast(m_face1)); + BOOST_CHECK_EQUAL(m_face1->getUri().toString(), "udp6://[::1]:20071"); + BOOST_CHECK_EQUAL(m_face1->isLocal(), false); + + m_face1->sendInterest(interest1); + m_face1->sendData (data1 ); + + BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, + "UdpChannel error: cannot send or receive Interest/Data packets"); + + + BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1); + BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1); + BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1); + BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1); + + BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName()); + BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName()); + BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName()); + BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName()); + + + + //checking if the connection accepting mechanism works properly. + + m_face2->sendData (data3 ); + m_face2->sendInterest(interest3); + + BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, + "UdpChannel error: cannot send or receive Interest/Data packets"); + + BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 2); + BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 2); + + BOOST_CHECK_EQUAL(m_face1_receivedInterests[1].getName(), interest3.getName()); + BOOST_CHECK_EQUAL(m_face1_receivedDatas [1].getName(), data3.getName()); } BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture) diff --git a/tests/face/unix-stream.cpp b/tests/face/unix-stream.cpp index 921c1ffa..09ee905b 100644 --- a/tests/face/unix-stream.cpp +++ b/tests/face/unix-stream.cpp @@ -148,6 +148,11 @@ BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture) BOOST_REQUIRE(static_cast(m_face1)); + std::string face1uri = m_face1->getUri().toString(); + BOOST_CHECK_EQUAL(face1uri.find("unix:///"), 0); // third '/' is the path separator + BOOST_CHECK_EQUAL(face1uri.rfind(CHANNEL_PATH1), + face1uri.size() - std::string(CHANNEL_PATH1).size()); + m_face2 = make_shared(client); m_face2->onReceiveInterest += bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);