tools+core: Moving Network class implementation to core
Change-Id: I312b827215c8ec3d13aff61584f162125fc48d2a
Refs: #2153
diff --git a/tools/network.hpp b/core/network.cpp
similarity index 63%
rename from tools/network.hpp
rename to core/network.cpp
index 044b915..b9a6c36 100644
--- a/tools/network.hpp
+++ b/core/network.cpp
@@ -1,11 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014 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
+ * Copyright (c) 2014, 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.
@@ -22,74 +23,45 @@
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
**/
-#ifndef NFD_TOOLS_NETWORK_HPP
-#define NFD_TOOLS_NETWORK_HPP
+#include "network.hpp"
-#include <boost/asio.hpp>
+namespace nfd {
-class Network
+void
+Network::print(std::ostream& os) const
{
-public:
- Network()
- {
- }
+ os << m_minAddress << " <-> " << m_maxAddress;
+}
- Network(const boost::asio::ip::address& minAddress,
- const boost::asio::ip::address& maxAddress)
- : m_minAddress(minAddress)
- , m_maxAddress(maxAddress)
- {
- }
+const Network&
+Network::getMaxRangeV4()
+{
+ using boost::asio::ip::address_v4;
+ static Network range = Network(address_v4(0), address_v4(0xFFFFFFFF));
+ return range;
+}
- void
- print(std::ostream& os) const
- {
- os << m_minAddress << " <-> " << m_maxAddress;
- }
+const Network&
+Network::getMaxRangeV6()
+{
+ using boost::asio::ip::address_v6;
+ static address_v6::bytes_type maxV6 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
+ static Network range = Network(address_v6(), address_v6(maxV6));
+ return range;
+}
- bool
- doesContain(const boost::asio::ip::address& address) const
- {
- return (m_minAddress <= address && address <= m_maxAddress);
- }
+//////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////
- static const Network&
- getMaxRangeV4()
- {
- using boost::asio::ip::address_v4;
- static Network range = Network(address_v4(0), address_v4(0xFFFFFFFF));
- return range;
- }
-
- static const Network&
- getMaxRangeV6()
- {
- using boost::asio::ip::address_v6;
- static address_v6::bytes_type maxV6 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
- static Network range = Network(address_v6(), address_v6(maxV6));
- return range;
- }
-
-private:
- boost::asio::ip::address m_minAddress;
- boost::asio::ip::address m_maxAddress;
-
- friend std::istream&
- operator>>(std::istream& is, Network& network);
-
- friend std::ostream&
- operator<<(std::ostream& os, const Network& network);
-};
-
-inline std::ostream&
+std::ostream&
operator<<(std::ostream& os, const Network& network)
{
network.print(os);
return os;
}
-inline std::istream&
+std::istream&
operator>>(std::istream& is, Network& network)
{
using namespace boost::asio;
@@ -158,4 +130,4 @@
return is;
}
-#endif // NFD_TOOLS_NETWORK_HPP
+} // namespace nfd
diff --git a/core/network.hpp b/core/network.hpp
new file mode 100644
index 0000000..1c0f6d3
--- /dev/null
+++ b/core/network.hpp
@@ -0,0 +1,95 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, 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/>.
+ **/
+
+#ifndef NFD_CORE_NETWORK_HPP
+#define NFD_CORE_NETWORK_HPP
+
+#include <boost/asio.hpp>
+#include <boost/utility/value_init.hpp>
+#include <boost/lexical_cast.hpp>
+
+namespace nfd {
+
+class Network
+{
+public:
+ Network()
+ {
+ }
+
+ Network(const boost::asio::ip::address& minAddress,
+ const boost::asio::ip::address& maxAddress)
+ : m_minAddress(minAddress)
+ , m_maxAddress(maxAddress)
+ {
+ }
+
+ void
+ print(std::ostream& os) const;
+
+ bool
+ doesContain(const boost::asio::ip::address& address) const
+ {
+ return (m_minAddress <= address && address <= m_maxAddress);
+ }
+
+ static const Network&
+ getMaxRangeV4();
+
+ static const Network&
+ getMaxRangeV6();
+
+ bool
+ operator==(const Network& rhs) const
+ {
+ return m_minAddress == rhs.m_minAddress && m_maxAddress == rhs.m_maxAddress;
+ }
+
+ bool
+ operator!=(const Network& rhs) const
+ {
+ return !(*this == rhs);
+ }
+
+private:
+ boost::asio::ip::address m_minAddress;
+ boost::asio::ip::address m_maxAddress;
+
+ friend std::istream&
+ operator>>(std::istream& is, Network& network);
+
+ friend std::ostream&
+ operator<<(std::ostream& os, const Network& network);
+};
+
+std::ostream&
+operator<<(std::ostream& os, const Network& network);
+
+std::istream&
+operator>>(std::istream& is, Network& network);
+
+} // namespace nfd
+
+#endif // NFD_CORE_NETWORK_HPP
diff --git a/tests/core/network.cpp b/tests/core/network.cpp
new file mode 100644
index 0000000..866d0b7
--- /dev/null
+++ b/tests/core/network.cpp
@@ -0,0 +1,150 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, 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 "core/network.hpp"
+
+#include "tests/test-common.hpp"
+
+namespace nfd {
+namespace tests {
+
+BOOST_FIXTURE_TEST_SUITE(CoreNetwork, BaseFixture)
+
+using boost::asio::ip::address;
+
+BOOST_AUTO_TEST_CASE(Empty)
+{
+ Network n;
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), false);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3")),
+ false);
+
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false);
+
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
+ false);
+}
+
+BOOST_AUTO_TEST_CASE(MaxRangeV4)
+{
+ Network n = Network::getMaxRangeV4();
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), true);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")),
+ false);
+
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), true);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), true);
+
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
+ false);
+}
+
+BOOST_AUTO_TEST_CASE(RangeV4)
+{
+ Network n = boost::lexical_cast<Network>("192.0.2.0/24");
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(n), "192.0.2.0 <-> 192.0.2.255");
+
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), true);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.254")), true);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.1.255")), false);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.3.0")), false);
+
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")),
+ false);
+
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false);
+
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
+ false);
+}
+
+BOOST_AUTO_TEST_CASE(MaxRangeV6)
+{
+ Network n = Network::getMaxRangeV6();
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), false);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")),
+ true);
+
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false);
+
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), true);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
+ true);
+}
+
+BOOST_AUTO_TEST_CASE(RangeV6)
+{
+ Network n = boost::lexical_cast<Network>("2001:db8:3f9:1::/64");
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(n),
+ "2001:db8:3f9:1:: <-> 2001:db8:3f9:1:ffff:ffff:ffff:ffff");
+
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("192.0.2.1")), false);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:3025:ccc5:eeeb:86d3")),
+ true);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1::")),
+ true);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:1:ffff:ffff:ffff:ffff")),
+ true);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:0:ffff:ffff:ffff:ffff")),
+ false);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("2001:db8:3f9:2::")),
+ false);
+
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("0.0.0.1")), false);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("255.255.255.255")), false);
+
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("::")), false);
+ BOOST_CHECK_EQUAL(n.doesContain(address::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")),
+ false);
+}
+
+BOOST_AUTO_TEST_CASE(Comparisons)
+{
+ BOOST_CHECK_EQUAL(boost::lexical_cast<Network>("192.0.2.0/24"),
+ boost::lexical_cast<Network>("192.0.2.127/24"));
+
+ BOOST_CHECK_EQUAL(boost::lexical_cast<Network>("2001:db8:3f9:0::/64"),
+ boost::lexical_cast<Network>("2001:db8:3f9:0:ffff::/64"));
+
+ BOOST_CHECK_NE(boost::lexical_cast<Network>("192.0.2.0/24"),
+ boost::lexical_cast<Network>("192.0.3.127/24"));
+
+ BOOST_CHECK_NE(boost::lexical_cast<Network>("2001:db8:3f9:0::/64"),
+ boost::lexical_cast<Network>("2001:db8:3f9:1::/64"));
+
+ BOOST_CHECK_NE(boost::lexical_cast<Network>("192.0.2.0/24"),
+ boost::lexical_cast<Network>("2001:db8:3f9:0::/64"));
+}
+
+BOOST_AUTO_TEST_SUITE_END() // CoreNetwork
+
+} // namespace tests
+} // namespace nfd
diff --git a/tools/nfd-autoreg.cpp b/tools/nfd-autoreg.cpp
index 0b6f7a5..dc013ea 100644
--- a/tools/nfd-autoreg.cpp
+++ b/tools/nfd-autoreg.cpp
@@ -37,7 +37,7 @@
#include "version.hpp"
#include "core/face-uri.hpp"
-#include "network.hpp"
+#include "core/network.hpp"
namespace po = boost::program_options;