2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
15 \brief INet4Address inline non-template implementation */
18 #include <senf/Socket/Protocols/AddressExceptions.hh>
20 #define prefix_ inline
21 //-/////////////////////////////////////////////////////////////////////////////////////////////////
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 prefix_ senf::INet4Address::INet4Address()
28 std::fill(begin(), end(), 0u);
31 prefix_ senf::INet4Address::INet4Address(senf::NoInit_t)
34 prefix_ senf::INet4Address senf::INet4Address::from_inaddr(inaddr_type v)
36 return INet4Address(v,IsInAddr);
39 prefix_ senf::INet4Address::inaddr_type & senf::INet4Address::iref()
41 return *static_cast<inaddr_type *>(static_cast<void *>((&(*this)[0])));
44 prefix_ senf::INet4Address::inaddr_type senf::INet4Address::iref()
47 return *static_cast<inaddr_type const *>(static_cast<void const *>(&(*this)[0]));
50 prefix_ senf::INet4Address::inaddr_type senf::INet4Address::inaddr()
56 prefix_ senf::INet4Address::INet4Address(inaddr_type addr, InAddr_t)
61 prefix_ bool senf::INet4Address::broadcast()
64 return inaddr() == 0xFFFFFFFFu;
67 prefix_ bool senf::INet4Address::boolean_test()
73 //-/////////////////////////////////////////////////////////////////////////////////////////////////
76 //-/////////////////////////////////////////////////////////////////////////////////////////////////
79 prefix_ senf::INet4Network::INet4Network()
80 : prefix_len_(), address_()
83 prefix_ senf::INet4Network::INet4Network(INet4Address const & address, unsigned prefix_len)
84 : prefix_len_( prefix_len_checked(prefix_len)),
85 address_(INet4Address(address.address() & mask()))
88 prefix_ senf::INet4Address const & senf::INet4Network::address()
94 prefix_ unsigned senf::INet4Network::prefix_len()
100 prefix_ bool senf::INet4Network::boolean_test()
103 return address() || prefix_len();
106 prefix_ bool senf::INet4Network::operator==(INet4Network const & other)
109 return address() == other.address() && prefix_len() == other.prefix_len();
112 prefix_ bool senf::INet4Network::match(INet4Address const & addr)
115 return (addr.address() & mask()) == address_.address();
118 prefix_ bool senf::INet4Network::match(INet4Network const & net)
121 return net.prefix_len() >= prefix_len() && match(net.address());
124 prefix_ senf::INet4Address senf::INet4Network::host(boost::uint32_t number)
126 return INet4Address(address_.address() | (number & ~mask()));
129 prefix_ senf::INet4Network senf::INet4Network::subnet(boost::uint32_t net, unsigned prefix_len)
131 return INet4Network(host(net << (32-prefix_len)),prefix_len);
134 //-/////////////////////////////////////////////////////////////////////////////////////////////////
137 prefix_ unsigned senf::INet4Network::prefix_len_checked(unsigned prefix_len)
141 throw AddressSyntaxException("invalid INet4 prefix length: " +
142 boost::lexical_cast<std::string>(prefix_len));
146 prefix_ boost::uint32_t senf::INet4Network::mask()
149 // This is correct as long as the system is using 2-complement arithmetic ...
150 return (~((boost::uint32_t(1u)<<(32u-prefix_len()))-1u)) & 0xFFFFFFFFu;
153 //-/////////////////////////////////////////////////////////////////////////////////////////////////
156 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4Network const & addr)
158 os << addr.address() << '/' << addr.prefix_len();
162 //-/////////////////////////////////////////////////////////////////////////////////////////////////
169 // comment-column: 40
170 // c-file-style: "senf"
171 // indent-tabs-mode: nil
172 // ispell-local-dictionary: "american"
173 // compile-command: "scons -u test"