Monero
i2p_address.h
Go to the documentation of this file.
1 // Copyright (c) 2019-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include <boost/utility/string_ref.hpp>
32 #include <cstdint>
33 #include <string>
34 
35 #include "common/expect.h"
36 #include "net/enums.h"
37 #include "net/error.h"
38 
39 namespace epee
40 {
41 namespace serialization
42 {
43  class portable_storage;
44  struct section;
45 }
46 }
47 
48 namespace net
49 {
52  {
53  char host_[61]; // null-terminated
54 
56  i2p_address(boost::string_ref host) noexcept;
57 
58  public:
60  static constexpr std::size_t buffer_size() noexcept { return sizeof(host_); }
61 
63  static const char* unknown_str() noexcept;
64 
66  i2p_address() noexcept;
67 
69  static i2p_address unknown() noexcept { return i2p_address{}; }
70 
76  static expect<i2p_address> make(boost::string_ref address);
77 
80 
83 
84  // Moves and copies are currently identical
85 
86  i2p_address(const i2p_address& rhs) noexcept;
87  ~i2p_address() = default;
88  i2p_address& operator=(const i2p_address& rhs) noexcept;
89 
91  bool is_unknown() const noexcept;
92 
93  bool equal(const i2p_address& rhs) const noexcept;
94  bool less(const i2p_address& rhs) const noexcept;
95 
97  bool is_same_host(const i2p_address& rhs) const noexcept;
98 
100  std::string str() const;
101 
103  const char* host_str() const noexcept { return host_; }
104 
106  std::uint16_t port() const noexcept { return 1; }
107 
108  static constexpr bool is_loopback() noexcept { return false; }
109  static constexpr bool is_local() noexcept { return false; }
110 
111  static constexpr epee::net_utils::address_type get_type_id() noexcept
112  {
114  }
115 
116  static constexpr epee::net_utils::zone get_zone() noexcept
117  {
119  }
120 
122  bool is_blockable() const noexcept { return !is_unknown(); }
123  };
124 
125  inline bool operator==(const i2p_address& lhs, const i2p_address& rhs) noexcept
126  {
127  return lhs.equal(rhs);
128  }
129 
130  inline bool operator!=(const i2p_address& lhs, const i2p_address& rhs) noexcept
131  {
132  return !lhs.equal(rhs);
133  }
134 
135  inline bool operator<(const i2p_address& lhs, const i2p_address& rhs) noexcept
136  {
137  return lhs.less(rhs);
138  }
139 } // net
Definition: binary_utils.h:36
i2p_address & operator=(const i2p_address &rhs) noexcept
Definition: i2p_address.cpp:140
bool is_blockable() const noexcept
Definition: i2p_address.h:122
static expect< i2p_address > make(boost::string_ref address)
Definition: i2p_address.cpp:105
b32 i2p address; internal format not condensed/decoded.
Definition: i2p_address.h:51
CXA_THROW_INFO_T void(* dest)(void *))
Definition: stack_trace.cpp:91
bool equal(const i2p_address &rhs) const noexcept
Definition: i2p_address.cpp:155
host
Definition: console.py:27
static const char * unknown_str() noexcept
Definition: i2p_address.cpp:93
bool _load(epee::serialization::portable_storage &src, epee::serialization::section *hparent)
Load from epee p2p format, and.
Definition: i2p_address.cpp:114
static i2p_address unknown() noexcept
Definition: i2p_address.h:69
Definition: enums.h:67
unsigned short uint16_t
Definition: stdint.h:125
Definition: portable_storage.h:45
bool is_same_host(const i2p_address &rhs) const noexcept
Definition: i2p_address.cpp:165
address_type
Definition: enums.h:39
const char * host_str() const noexcept
Definition: i2p_address.h:103
static constexpr std::size_t buffer_size() noexcept
Definition: i2p_address.h:60
Definition: net_utils_base.h:58
Definition: expect.h:71
bool operator<(const i2p_address &lhs, const i2p_address &rhs) noexcept
Definition: i2p_address.h:135
bool operator!=(const i2p_address &lhs, const i2p_address &rhs) noexcept
Definition: i2p_address.h:130
char host_[61]
Definition: i2p_address.h:53
Definition: portable_storage_base.h:168
bool is_unknown() const noexcept
Definition: i2p_address.cpp:149
bool operator==(const i2p_address &lhs, const i2p_address &rhs) noexcept
Definition: i2p_address.h:125
static constexpr bool is_local() noexcept
Definition: i2p_address.h:109
~i2p_address()=default
i2p_address() noexcept
An object with port() == 0 and host_str() == unknown_str().
Definition: i2p_address.cpp:98
TODO: (mj-xmr) This will be reduced in an another PR.
Definition: byte_slice.h:39
uint32_t address
Definition: getifaddr.c:269
std::string str() const
Definition: i2p_address.cpp:170
zone
Definition: enums.h:49
static constexpr epee::net_utils::address_type get_type_id() noexcept
Definition: i2p_address.h:111
static constexpr epee::net_utils::zone get_zone() noexcept
Definition: i2p_address.h:116
bool less(const i2p_address &rhs) const noexcept
Definition: i2p_address.cpp:160
static constexpr bool is_loopback() noexcept
Definition: i2p_address.h:108
#define const
Definition: ipfrdr.c:80
std::uint16_t port() const noexcept
Definition: i2p_address.h:106
bool store(epee::serialization::portable_storage &dest, epee::serialization::section *hparent) const
Store in epee p2p format.
Definition: i2p_address.cpp:128