INet4Address.hh
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 
17 #ifndef HH_SENF_Socket_Protocols_INet_INet4Address_
18 #define HH_SENF_Socket_Protocols_INet_INet4Address_ 1
19 
20 // Custom includes
21 #include <string>
22 #include <boost/cstdint.hpp>
23 #include <boost/array.hpp>
24 #include <boost/operators.hpp>
25 #include <senf/Utils/safe_bool.hh>
26 #include <senf/Utils/Tags.hh>
27 
28 //#include "INet4Address.mpp"
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 
31 namespace senf {
32 
79  : public boost::array<boost::uint8_t,4>,
80  public comparable_safe_bool<INet4Address>
81  {
82  public:
83  //-////////////////////////////////////////////////////////////////////////
84  // Types
85 
86  typedef uint32_t address_type;
87  typedef uint32_t inaddr_type;
88 
89  static INet4Address const None;
90  static INet4Address const Loopback;
91  static INet4Address const Broadcast;
92 
93  //-////////////////////////////////////////////////////////////////////////
95  //\{
96 
97  INet4Address();
98  explicit INet4Address(senf::NoInit_t);
99  explicit INet4Address(address_type value);
101 
102  static INet4Address from_string(std::string const & s);
104 
116  template <class InputIterator>
117  static INet4Address from_data(InputIterator i);
119 
122  static INet4Address from_inaddr(inaddr_type v);
124 
128  //\}
129  //-////////////////////////////////////////////////////////////////////////
131  //\{
132 
133  bool local() const;
134 
136  bool loopback() const;
137 
139  bool multicast() const;
140 
143  bool broadcast() const;
144  bool boolean_test() const;
145 
146  inaddr_type inaddr() const;
147 
149  address_type address() const;
150 
153  //-/////////////////////////////////////////////////////////////////////////////////////////
154 
155  private:
156  enum InAddr_t { IsInAddr };
157  INet4Address(inaddr_type addr, InAddr_t);
158  inaddr_type & iref();
159  inaddr_type iref() const;
160  };
161 
165  std::ostream & operator<<(std::ostream & os, INet4Address const & addr);
166 
172  std::istream & operator>>(std::istream & os, INet4Address & addr);
173 
197  template <boost::uint32_t address, unsigned prefix_len>
199  {
200  public:
201  static bool match(INet4Address const & addr);
202  };
203 
209  : public boost::equality_comparable<INet4Network>,
210  public comparable_safe_bool<INet4Network>
211  {
212  public:
213  //-////////////////////////////////////////////////////////////////////////
215  //\{
216 
217  INet4Network();
218  INet4Network(INet4Address const & address, unsigned prefix_len);
220  explicit INet4Network(std::string const & s);
221 
222  //\}
223  //-////////////////////////////////////////////////////////////////////////
224 
225  INet4Address const & address() const;
226  unsigned prefix_len() const;
227 
228  bool boolean_test() const;
229  bool operator==(INet4Network const & other) const;
231 
232  bool match(INet4Address const & addr) const;
233  bool match(INet4Network const & net) const;
234 
237  INet4Address host(boost::uint32_t number);
238 
245  INet4Network subnet(boost::uint32_t net, unsigned prefix_len);
247 
257  protected:
258 
259  private:
260  boost::uint32_t mask() const;
261  unsigned prefix_len_checked(unsigned prefix_len) const;
262 
263  unsigned prefix_len_;
264  INet4Address address_;
265  };
266 
270  std::ostream & operator<<(std::ostream & os, INet4Network const & addr);
271 
278  std::istream & operator>>(std::istream & is, INet4Network & addr);
279 
280 }
281 
282 //-/////////////////////////////////////////////////////////////////////////////////////////////////
283 #include "INet4Address.cci"
284 #include "INet4Address.ct"
285 #include "INet4Address.cti"
286 #endif
287 
288 
289 // Local Variables:
290 // mode: c++
291 // fill-column: 100
292 // comment-column: 40
293 // c-file-style: "senf"
294 // indent-tabs-mode: nil
295 // ispell-local-dictionary: "american"
296 // compile-command: "scons -u test"
297 // End:
static INet4Address const Broadcast
Definition: INet4Address.hh:91
address_type address() const
Return address represented as integer number.
Check INet4Address against a fixed network prefix.
bool local() const
true, if address is locally administered
Definition: INet4Address.cc:80
__u32 mask
std::ostream & operator<<(std::ostream &os, INet4Address const &addr)
Output INet4Address instance as it&#39;s string representation.
NoInit_t
static INet4Address const None
The empty (0) address.
Definition: INet4Address.hh:89
static INet4Address from_inaddr(inaddr_type v)
Construct address from integer in network byte order.
bool operator==(EUI64 const &eui64, MACAddress const &mac)
bool broadcast() const
true, if address is 255.255.255.255
bool multicast() const
true, if address is a multicast address
Definition: INet4Address.cc:97
static INet4Address from_string(std::string const &s)
Convert string to address.
Definition: INet4Address.cc:41
static INet4Address from_data(InputIterator i)
Construct address from 4 bytes of raw data.
uint32_t inaddr_type
Legacy address representation in network byte order.
Definition: INet4Address.hh:87
std::istream & operator>>(std::istream &os, INet4Address &addr)
Initialize INet4Address instance from a string representation sets std::ios::failbit on the stream if...
bool loopback() const
true, if address is within the loopback network
Definition: INet4Address.cc:91
uint32_t address_type
Address representation as number in host byte order.
Definition: INet4Address.hh:86
bool boolean_test() const
true, if address is non-empty (!= 0.0.0.0)
inaddr_type inaddr() const
Return the raw network byte order address.
static INet4Address const Loopback
The loopback (127.0.0.1) address.
Definition: INet4Address.hh:90
IPv4 Internet address.
Definition: INet4Address.hh:78
IPv4 network prefix.
INet4Address()
Construct an empty address.