LIBWIRE
Next-generation C++17 networking library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
address.hpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2018 Max Mazurov (fox.cpp) <fox.cpp [at] disroot [dot] org>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
23 #pragma once
24 
25 #include <cstdint>
26 #include <array>
27 #include <string_view>
28 #include <string>
29 #include <libwire/protocols.hpp>
30 #include <libwire/memory_view.hpp>
31 
39 /*
40  * If you had to open this file to find answer for your question - we are so
41  * sorry. Please open issue with your question so we can update documentation
42  * to answer it.
43  */
44 
45 namespace libwire {
63  struct address {
71  address() = default;
72 
77  address(const memory_view&) noexcept;
78 
83  address(uint8_t, uint8_t, uint8_t, uint8_t) noexcept;
84 
92  address(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t,
93  uint8_t, uint8_t, uint8_t, uint8_t, uint8_t) noexcept;
94 
108  address(const std::string& text_ip, ip assume_ipver = ip(0)) noexcept(!LIBWIRE_EXCEPTIONS_ENABLED_BOOL);
109 
115  std::string to_string() const noexcept;
116 
117  bool is_invalid() const noexcept;
118 
119  bool operator==(const address&) const noexcept;
120  bool operator!=(const address&) const noexcept;
121 
122  ip version = ip(0);
123  std::array<uint8_t, 16> parts{};
124 
125  static const address invalid;
126  };
127 
131  namespace ipv4 {
132  inline address any = {0, 0, 0, 0}, broadcast = {255, 255, 255, 255}, loopback = {127, 0, 0, 1};
133  } // namespace ipv4
134 
138  namespace ipv6 {
139  inline address any = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
140  broadcast = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
141  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
142  loopback = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1};
143  } // namespace ipv6
144 } // namespace libwire
145 
146 namespace std {
150  template<>
151  struct hash<libwire::address> {
152  std::size_t operator()(const libwire::address&) const noexcept;
153  };
154 } // namespace std
std::string to_string() const noexcept
Convert address object to string representation.
bool is_invalid() const noexcept
address loopback
Definition: address.hpp:132
Non-owning STL-like wrapper for raw memory.
Definition: memory_view.hpp:58
address any
Definition: address.hpp:139
address()=default
Uninitialized address value, is_invalid() = true.
IPv4/IPv6 address structure.
Definition: address.hpp:63
address any
Definition: address.hpp:132
address broadcast
Definition: address.hpp:140
#define LIBWIRE_EXCEPTIONS_ENABLED_BOOL
Defines memory_view wrapper.
Definition: memory_view.hpp:41
bool operator==(const address &) const noexcept
address broadcast
Definition: address.hpp:132
address loopback
Definition: address.hpp:142
static const address invalid
Definition: address.hpp:125
std::array< uint8_t, 16 > parts
Definition: address.hpp:123
bool operator!=(const address &) const noexcept