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