blob: fc13c220fce57e89316104f8a4f453ec77bac9d7 [file] [log] [blame]
Davide Pesavento2bf35a62017-04-02 00:41:06 -04001/* -*- 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 Shi25467942017-06-30 02:53:14 +000024#ifndef NDN_NET_NETWORK_ADDRESS_HPP
25#define NDN_NET_NETWORK_ADDRESS_HPP
Davide Pesavento2bf35a62017-04-02 00:41:06 -040026
Junxiao Shi2dc416d2017-07-03 04:46:16 +000027#include "../common.hpp"
Davide Pesavento2bf35a62017-04-02 00:41:06 -040028#include <boost/asio/ip/address.hpp>
29
30namespace ndn {
Junxiao Shi25467942017-06-30 02:53:14 +000031namespace net {
Davide Pesavento2bf35a62017-04-02 00:41:06 -040032
33enum class AddressFamily {
34 UNSPECIFIED,
35 V4,
36 V6,
37};
38
39enum class AddressScope {
40 NOWHERE,
41 HOST,
42 LINK,
43 GLOBAL,
44};
45
46std::ostream&
47operator<<(std::ostream& os, AddressScope scope);
48
49/**
50 * @brief Stores one IP address supported by a network interface.
51 */
52class NetworkAddress
53{
Junxiao Shi2dc416d2017-07-03 04:46:16 +000054public:
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 Pesavento2bf35a62017-04-02 00:41:06 -040062 /** @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 Shi2dc416d2017-07-03 04:46:16 +000086 /** @brief Returns the prefix length
Davide Pesavento2bf35a62017-04-02 00:41:06 -040087 */
Junxiao Shi2dc416d2017-07-03 04:46:16 +000088 uint8_t
89 getPrefixLength() const
Davide Pesavento2bf35a62017-04-02 00:41:06 -040090 {
Junxiao Shi2dc416d2017-07-03 04:46:16 +000091 return m_prefixLength;
Davide Pesavento2bf35a62017-04-02 00:41:06 -040092 }
93
94 /** @brief Returns the address scope
95 */
96 AddressScope
97 getScope() const
98 {
99 return m_scope;
100 }
101
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000102 /** @brief Returns a bitset of platform-specific flags enabled on the address
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400103 */
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000104 uint32_t
105 getFlags() const
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400106 {
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000107 return m_flags;
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400108 }
109
110 friend bool
111 operator<(const NetworkAddress& a, const NetworkAddress& b)
112 {
113 return a.m_ip < b.m_ip;
114 }
115
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400116private:
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400117 AddressFamily m_family;
118 boost::asio::ip::address m_ip;
119 boost::asio::ip::address m_broadcast;
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400120 uint8_t m_prefixLength;
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000121 AddressScope m_scope;
122 uint32_t m_flags; // IFA_F_* in if_addr.h
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400123};
124
125std::ostream&
126operator<<(std::ostream& os, const NetworkAddress& address);
127
Junxiao Shi25467942017-06-30 02:53:14 +0000128} // namespace net
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400129} // namespace ndn
130
Junxiao Shi25467942017-06-30 02:53:14 +0000131#endif // NDN_NET_NETWORK_ADDRESS_HPP