blob: 227ccfc8042beadf991c842847fede73027394bb [file] [log] [blame]
Alexander Afanasyev689f0e92014-11-09 12:09:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04002/*
Davide Pesavento191a7a22023-05-17 22:40:43 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
susmit91e1d7c2016-10-03 16:16:57 -06004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 **/
25
26#ifndef NFD_CORE_NETWORK_HPP
27#define NFD_CORE_NETWORK_HPP
28
Davide Pesaventoa9b09b62022-06-04 14:07:25 -040029#include <boost/asio/ip/address.hpp>
30#include <string_view>
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080031
32namespace nfd {
33
34class Network
35{
36public:
Davide Pesavento89567d32016-11-19 16:39:45 +010037 Network();
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080038
39 Network(const boost::asio::ip::address& minAddress,
Davide Pesavento89567d32016-11-19 16:39:45 +010040 const boost::asio::ip::address& maxAddress);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080041
42 bool
Davide Pesavento191a7a22023-05-17 22:40:43 -040043 doesContain(const boost::asio::ip::address& address) const noexcept
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080044 {
Davide Pesavento89567d32016-11-19 16:39:45 +010045 return m_minAddress <= address && address <= m_maxAddress;
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080046 }
47
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080048 static const Network&
49 getMaxRangeV4();
50
51 static const Network&
52 getMaxRangeV6();
53
Davide Pesavento89567d32016-11-19 16:39:45 +010054 static bool
Davide Pesavento191a7a22023-05-17 22:40:43 -040055 isValidCidr(std::string_view cidr) noexcept;
Davide Pesavento89567d32016-11-19 16:39:45 +010056
Davide Pesavento191a7a22023-05-17 22:40:43 -040057private: // non-member operators
58 friend bool
59 operator==(const Network& lhs, const Network& rhs) noexcept
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080060 {
Davide Pesavento191a7a22023-05-17 22:40:43 -040061 return lhs.m_minAddress == rhs.m_minAddress &&
62 lhs.m_maxAddress == rhs.m_maxAddress;
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080063 }
64
Davide Pesavento191a7a22023-05-17 22:40:43 -040065 friend bool
66 operator!=(const Network& lhs, const Network& rhs) noexcept
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080067 {
Davide Pesavento191a7a22023-05-17 22:40:43 -040068 return !(lhs == rhs);
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080069 }
70
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080071 friend std::ostream&
72 operator<<(std::ostream& os, const Network& network);
Davide Pesavento89567d32016-11-19 16:39:45 +010073
74 friend std::istream&
75 operator>>(std::istream& is, Network& network);
Davide Pesavento191a7a22023-05-17 22:40:43 -040076
77private:
78 boost::asio::ip::address m_minAddress;
79 boost::asio::ip::address m_maxAddress;
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080080};
81
82std::ostream&
83operator<<(std::ostream& os, const Network& network);
84
85std::istream&
86operator>>(std::istream& is, Network& network);
87
88} // namespace nfd
89
90#endif // NFD_CORE_NETWORK_HPP