Debian Codegraph @

src/net/tor_address.h

raw symbols SCIP
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 {
51 //! Tor onion address; internal format not condensed/decoded.
52 class tor_address
53 {
54 std::uint16_t port_;
55 char host_[63]; // null-terminated
56
57 //! Keep in private, `host.size()` has no runtime check
58 tor_address(boost::string_ref host, std::uint16_t port) noexcept;
59
60 public:
61 //! \return Size of internal buffer for host.
62 static constexpr std::size_t buffer_size() noexcept { return sizeof(host_); }
63
64 //! \return `<unknown tor host>`.
65 static const char* unknown_str() noexcept;
66
67 //! An object with `port() == 0` and `host_str() == unknown_str()`.
68 tor_address() noexcept;
69
70 //! \return A default constructed `tor_address` object.
71 static tor_address unknown() noexcept { return tor_address{}; }
72
73 /*!
74 Parse `address` in onion v2 or v3 format with (i.e. x.onion:80)
75 with `default_port` being used iff port is not specified in
76 `address`.
77 */
78 static expect<tor_address> make(boost::string_ref address, std::uint16_t default_port = 0);
79
80 //! Load from epee p2p format, and \return false if not valid tor address
81 bool _load(epee::serialization::portable_storage& src, epee::serialization::section* hparent);
82
83 //! Store in epee p2p format
84 bool store(epee::serialization::portable_storage& dest, epee::serialization::section* hparent) const;
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
92 //! \return True if default constructed or via `unknown()`.
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
98 //! \return True if onion addresses are identical.
99 bool is_same_host(const tor_address& rhs) const noexcept;
100
101 //! \return `x.onion` or `x.onion:z` if `port() != 0`.
102 std::string str() const;
103
104 //! \return Null-terminated `x.onion` value or `unknown_str()`.
105 const char* host_str() const noexcept { return host_; }
106
107 //! \return Port value or `0` if unspecified.
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 {
115 return epee::net_utils::address_type::tor;
116 }
117
118 static constexpr epee::net_utils::zone get_zone() noexcept
119 {
120 return epee::net_utils::zone::tor;
121 }
122
123 //! \return `!is_unknown()`.
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