network-address.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2021 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 
25 
26 #ifdef __linux__
27 #include <linux/if_addr.h>
28 #endif
29 
30 namespace ndn {
31 namespace net {
32 
33 std::ostream&
34 operator<<(std::ostream& os, AddressScope scope)
35 {
36  switch (scope) {
38  return os << "nowhere";
39  case AddressScope::HOST:
40  return os << "host";
41  case AddressScope::LINK:
42  return os << "link";
44  return os << "global";
45  }
46  return os;
47 }
48 
50  const boost::asio::ip::address& ip,
51  const boost::asio::ip::address& broadcast,
52  uint8_t prefixLength,
53  AddressScope scope,
54  uint32_t flags)
55  : m_family(family)
56  , m_ip(ip)
57  , m_broadcast(broadcast)
58  , m_prefixLength(prefixLength)
59  , m_scope(scope)
60  , m_flags(flags)
61 {
62 }
63 
64 bool
66 {
67 #ifdef __linux__
68  return m_flags & IFA_F_DEPRECATED;
69 #else
70  // should probably check for IN6_IFF_DEPRECATED on macOS and FreeBSD, but the
71  // NetworkMonitor backend for those platforms doesn't provide any address flags
72  return false;
73 #endif
74 }
75 
76 std::ostream&
77 operator<<(std::ostream& os, const NetworkAddress& addr)
78 {
79  return os << addr.getIp() << '/' << static_cast<unsigned int>(addr.getPrefixLength());
80 }
81 
82 } // namespace net
83 } // namespace ndn
Stores one IP address supported by a network interface.
NetworkAddress(AddressFamily family, const boost::asio::ip::address &ip, const boost::asio::ip::address &broadcast, uint8_t prefixLength, AddressScope scope, uint32_t flags)
boost::asio::ip::address getIp() const
Returns the IP address (v4 or v6)
bool isDeprecated() const
Returns true if the address is deprecated.
uint8_t getPrefixLength() const
Returns the prefix length.
std::ostream & operator<<(std::ostream &os, AddressScope scope)
Definition: data.cpp:25