Monero
Loading...
Searching...
No Matches
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
40namespace epee
41{
42namespace serialization
43{
44 class portable_storage;
45 struct section;
46}
47}
48
49namespace net
50{
53 {
54 std::uint16_t port_;
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 portable_storage.h:46
Definition expect.h:134
Tor onion address; internal format not condensed/decoded.
Definition tor_address.h:53
~tor_address()=default
bool _load(epee::serialization::portable_storage &src, epee::serialization::section *hparent)
Load from epee p2p format, and.
Definition tor_address.cpp:126
static expect< tor_address > make(boost::string_ref address, std::uint16_t default_port=0)
Definition tor_address.cpp:109
static constexpr std::size_t buffer_size() noexcept
Definition tor_address.h:62
static constexpr bool is_local() noexcept
Definition tor_address.h:111
bool less(const tor_address &rhs) const noexcept
Definition tor_address.cpp:175
static const char * unknown_str() noexcept
Definition tor_address.cpp:96
bool equal(const tor_address &rhs) const noexcept
Definition tor_address.cpp:170
std::string str() const
Definition tor_address.cpp:187
static constexpr epee::net_utils::zone get_zone() noexcept
Definition tor_address.h:118
static constexpr epee::net_utils::address_type get_type_id() noexcept
Definition tor_address.h:113
char host_[63]
Definition tor_address.h:55
std::uint16_t port_
Definition tor_address.h:54
static constexpr bool is_loopback() noexcept
Definition tor_address.h:110
bool store(epee::serialization::portable_storage &dest, epee::serialization::section *hparent) const
Store in epee p2p format.
Definition tor_address.cpp:142
tor_address & operator=(const tor_address &rhs) noexcept
Definition tor_address.cpp:154
bool is_unknown() const noexcept
Definition tor_address.cpp:164
const char * host_str() const noexcept
Definition tor_address.h:105
std::uint16_t port() const noexcept
Definition tor_address.h:108
tor_address() noexcept
An object with port() == 0 and host_str() == unknown_str().
Definition tor_address.cpp:101
bool is_blockable() const noexcept
Definition tor_address.h:124
static tor_address unknown() noexcept
Definition tor_address.h:71
bool is_same_host(const tor_address &rhs) const noexcept
Definition tor_address.cpp:181
tor_address(boost::string_ref host, std::uint16_t port) noexcept
Keep in private, host.size() has no runtime check.
Definition tor_address.cpp:85
#define const
Definition ipfrdr.c:80
uint32_t address
Definition getifaddr.c:269
address_type
Definition enums.h:40
@ tor
Definition enums.h:46
zone
Definition enums.h:50
@ tor
Definition enums.h:54
Definition keyvalue_serialization_overloads.h:49
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
Definition net_utils_base.h:59
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
bool operator==(const i2p_address &lhs, const i2p_address &rhs) noexcept
Definition i2p_address.h:125
Definition enums.h:68
CXA_THROW_INFO_T void(* dest)(void *))
Definition stack_trace.cpp:91
Definition portable_storage_base.h:169