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-2023 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::net {
31 
32 std::ostream&
33 operator<<(std::ostream& os, AddressScope scope)
34 {
35  switch (scope) {
37  return os << "nowhere";
38  case AddressScope::HOST:
39  return os << "host";
40  case AddressScope::LINK:
41  return os << "link";
43  return os << "global";
44  }
45  return os;
46 }
47 
49  const boost::asio::ip::address& ip,
50  const boost::asio::ip::address& broadcast,
51  uint8_t prefixLength,
52  AddressScope scope,
53  uint32_t flags)
54  : m_family(family)
55  , m_ip(ip)
56  , m_broadcast(broadcast)
57  , m_prefixLength(prefixLength)
58  , m_scope(scope)
59  , m_flags(flags)
60 {
61 }
62 
63 bool
65 {
66 #ifdef __linux__
67  return m_flags & IFA_F_DEPRECATED;
68 #else
69  // should probably check for IN6_IFF_DEPRECATED on macOS and FreeBSD, but the
70  // NetworkMonitor backend for those platforms doesn't provide any address flags
71  return false;
72 #endif
73 }
74 
75 std::ostream&
76 operator<<(std::ostream& os, const NetworkAddress& addr)
77 {
78  return os << addr.getIp() << '/' << static_cast<unsigned int>(addr.getPrefixLength());
79 }
80 
81 } // namespace ndn::net
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)