Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | * |
| 21 | * @author Davide Pesavento <davide.pesavento@lip6.fr> |
| 22 | */ |
| 23 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 24 | #ifndef NDN_NET_NETWORK_ADDRESS_HPP |
| 25 | #define NDN_NET_NETWORK_ADDRESS_HPP |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 26 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 27 | #include "../common.hpp" |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 28 | #include <boost/asio/ip/address.hpp> |
| 29 | |
| 30 | namespace ndn { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 31 | namespace net { |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 32 | |
| 33 | enum class AddressFamily { |
| 34 | UNSPECIFIED, |
| 35 | V4, |
| 36 | V6, |
| 37 | }; |
| 38 | |
| 39 | enum class AddressScope { |
| 40 | NOWHERE, |
| 41 | HOST, |
| 42 | LINK, |
| 43 | GLOBAL, |
| 44 | }; |
| 45 | |
| 46 | std::ostream& |
| 47 | operator<<(std::ostream& os, AddressScope scope); |
| 48 | |
| 49 | /** |
| 50 | * @brief Stores one IP address supported by a network interface. |
| 51 | */ |
| 52 | class NetworkAddress |
| 53 | { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 54 | public: |
| 55 | NetworkAddress(AddressFamily family, |
| 56 | boost::asio::ip::address ip, |
| 57 | boost::asio::ip::address broadcast, |
| 58 | uint8_t prefixLength, |
| 59 | AddressScope scope, |
| 60 | uint32_t flags); |
| 61 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 62 | /** @brief Returns the address family |
| 63 | */ |
| 64 | AddressFamily |
| 65 | getFamily() const |
| 66 | { |
| 67 | return m_family; |
| 68 | } |
| 69 | |
| 70 | /** @brief Returns the IP address (v4 or v6) |
| 71 | */ |
| 72 | boost::asio::ip::address |
| 73 | getIp() const |
| 74 | { |
| 75 | return m_ip; |
| 76 | } |
| 77 | |
| 78 | /** @brief Returns the IP broadcast address |
| 79 | */ |
| 80 | boost::asio::ip::address |
| 81 | getBroadcast() const |
| 82 | { |
| 83 | return m_broadcast; |
| 84 | } |
| 85 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 86 | /** @brief Returns the prefix length |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 87 | */ |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 88 | uint8_t |
| 89 | getPrefixLength() const |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 90 | { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 91 | return m_prefixLength; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** @brief Returns the address scope |
| 95 | */ |
| 96 | AddressScope |
| 97 | getScope() const |
| 98 | { |
| 99 | return m_scope; |
| 100 | } |
| 101 | |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 102 | /** @brief Returns a bitset of platform-specific flags enabled on the address |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 103 | */ |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 104 | uint32_t |
| 105 | getFlags() const |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 106 | { |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 107 | return m_flags; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | friend bool |
| 111 | operator<(const NetworkAddress& a, const NetworkAddress& b) |
| 112 | { |
| 113 | return a.m_ip < b.m_ip; |
| 114 | } |
| 115 | |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 116 | private: |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 117 | AddressFamily m_family; |
| 118 | boost::asio::ip::address m_ip; |
| 119 | boost::asio::ip::address m_broadcast; |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 120 | uint8_t m_prefixLength; |
Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 121 | AddressScope m_scope; |
| 122 | uint32_t m_flags; // IFA_F_* in if_addr.h |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | std::ostream& |
| 126 | operator<<(std::ostream& os, const NetworkAddress& address); |
| 127 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 128 | } // namespace net |
Davide Pesavento | 2bf35a6 | 2017-04-02 00:41:06 -0400 | [diff] [blame] | 129 | } // namespace ndn |
| 130 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 131 | #endif // NDN_NET_NETWORK_ADDRESS_HPP |