INet4Address.cci
Go to the documentation of this file.
1 //
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
6 //
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
11 //
12 
13 
14 /** \file
15  \brief INet4Address inline non-template implementation */
16 
17 // Custom includes
18 #include <senf/Socket/Protocols/AddressExceptions.hh>
19 
20 #define prefix_ inline
21 //-/////////////////////////////////////////////////////////////////////////////////////////////////
22 
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
24 // senf::INet4Address
25 
26 prefix_ senf::INet4Address::INet4Address()
27 {
28  std::fill(begin(), end(), 0u);
29 }
30 
31 prefix_ senf::INet4Address::INet4Address(senf::NoInit_t)
32 {}
33 
34 prefix_ senf::INet4Address senf::INet4Address::from_inaddr(inaddr_type v)
35 {
36  return INet4Address(v,IsInAddr);
37 }
38 
39 prefix_ senf::INet4Address::inaddr_type & senf::INet4Address::iref()
40 {
41  return *static_cast<inaddr_type *>(static_cast<void *>((&(*this)[0])));
42 }
43 
44 prefix_ senf::INet4Address::inaddr_type senf::INet4Address::iref()
45  const
46 {
47  return *static_cast<inaddr_type const *>(static_cast<void const *>(&(*this)[0]));
48 }
49 
50 prefix_ senf::INet4Address::inaddr_type senf::INet4Address::inaddr()
51  const
52 {
53  return iref();
54 }
55 
56 prefix_ senf::INet4Address::INet4Address(inaddr_type addr, InAddr_t)
57 {
58  iref() = addr;
59 }
60 
61 prefix_ bool senf::INet4Address::broadcast()
62  const
63 {
64  return inaddr() == 0xFFFFFFFFu;
65 }
66 
67 prefix_ bool senf::INet4Address::boolean_test()
68  const
69 {
70  return inaddr();
71 }
72 
73 //-/////////////////////////////////////////////////////////////////////////////////////////////////
74 // senf::INet4Network
75 
76 //-/////////////////////////////////////////////////////////////////////////////////////////////////
77 // public members
78 
79 prefix_ senf::INet4Network::INet4Network()
80  : prefix_len_(), address_()
81 {}
82 
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()))
86 {}
87 
88 prefix_ senf::INet4Address const & senf::INet4Network::address()
89  const
90 {
91  return address_;
92 }
93 
94 prefix_ unsigned senf::INet4Network::prefix_len()
95  const
96 {
97  return prefix_len_;
98 }
99 
100 prefix_ bool senf::INet4Network::boolean_test()
101  const
102 {
103  return address() || prefix_len();
104 }
105 
106 prefix_ bool senf::INet4Network::operator==(INet4Network const & other)
107  const
108 {
109  return address() == other.address() && prefix_len() == other.prefix_len();
110 }
111 
112 prefix_ bool senf::INet4Network::match(INet4Address const & addr)
113  const
114 {
115  return (addr.address() & mask()) == address_.address();
116 }
117 
118 prefix_ bool senf::INet4Network::match(INet4Network const & net)
119  const
120 {
121  return net.prefix_len() >= prefix_len() && match(net.address());
122 }
123 
124 prefix_ senf::INet4Address senf::INet4Network::host(boost::uint32_t number)
125 {
126  return INet4Address(address_.address() | (number & ~mask()));
127 }
128 
129 prefix_ senf::INet4Network senf::INet4Network::subnet(boost::uint32_t net, unsigned prefix_len)
130 {
131  return INet4Network(host(net << (32-prefix_len)),prefix_len);
132 }
133 
134 //-/////////////////////////////////////////////////////////////////////////////////////////////////
135 // private members
136 
137 prefix_ unsigned senf::INet4Network::prefix_len_checked(unsigned prefix_len)
138  const
139 {
140  if (prefix_len > 32)
141  throw AddressSyntaxException("invalid INet4 prefix length: " +
142  boost::lexical_cast<std::string>(prefix_len));
143  return prefix_len;
144 }
145 
146 prefix_ boost::uint32_t senf::INet4Network::mask()
147  const
148 {
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;
151 }
152 
153 //-/////////////////////////////////////////////////////////////////////////////////////////////////
154 // namespace members
155 
156 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4Network const & addr)
157 {
158  os << addr.address() << '/' << addr.prefix_len();
159  return os;
160 }
161 
162 //-/////////////////////////////////////////////////////////////////////////////////////////////////
163 #undef prefix_
164 
165 
166 // Local Variables:
167 // mode: c++
168 // fill-column: 100
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"
174 // End: