Debian Codegraph @

contrib/epee/include/net/levin_base.h

raw symbols SCIP
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26
27
28
29 #ifndef _LEVIN_BASE_H_
30 #define _LEVIN_BASE_H_
31
32 #include <cstdint>
33
34 #include "byte_stream.h"
35 #include "net_utils_base.h"
36 #include "span.h"
37
38 #define LEVIN_SIGNATURE 0x0101010101012101LL //Bender's nightmare
39
40 namespace epee
41 {
42 class byte_slice;
43 namespace levin
44 {
45 #pragma pack(push)
46 #pragma pack(1)
47 struct bucket_head
48 {
49 uint64_t m_signature;
50 uint64_t m_cb;
51 uint8_t m_have_to_return_data;
52 uint32_t m_command;
53 int32_t m_return_code;
54 uint32_t m_reservedA; //probably some flags in future
55 uint32_t m_reservedB; //probably some check sum in future
56 };
57 #pragma pack(pop)
58
59
60 #pragma pack(push)
61 #pragma pack(1)
62 struct bucket_head2
63 {
64 uint64_t m_signature;
65 uint64_t m_cb;
66 uint8_t m_have_to_return_data;
67 uint32_t m_command;
68 int32_t m_return_code;
69 uint32_t m_flags;
70 uint32_t m_protocol_version;
71 };
72 #pragma pack(pop)
73
74
75 #define LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED 0
76 #define LEVIN_INITIAL_MAX_PACKET_SIZE 256*1024 // 256 KiB before handshake
77 #define LEVIN_DEFAULT_MAX_PACKET_SIZE 100000000 //100MB by default after handshake
78
79 #define LEVIN_PACKET_REQUEST 0x00000001
80 #define LEVIN_PACKET_RESPONSE 0x00000002
81 #define LEVIN_PACKET_BEGIN 0x00000004
82 #define LEVIN_PACKET_END 0x00000008
83
84
85 #define LEVIN_PROTOCOL_VER_0 0
86 #define LEVIN_PROTOCOL_VER_1 1
87
88
89 template<class t_connection_context = net_utils::connection_context_base>
90 struct levin_commands_handler
91 {
92 virtual int invoke(int command, const epee::span<const uint8_t> in_buff, byte_stream& buff_out, t_connection_context& context)=0;
93 virtual int notify(int command, const epee::span<const uint8_t> in_buff, t_connection_context& context)=0;
94 virtual void callback(t_connection_context& context){};
95
96 virtual void on_connection_new(t_connection_context& context){};
97 virtual void on_connection_close(t_connection_context& context){};
98
99 virtual ~levin_commands_handler(){}
100 };
101
102 #define LEVIN_OK 0
103 #define LEVIN_ERROR_CONNECTION -1
104 #define LEVIN_ERROR_CONNECTION_NOT_FOUND -2
105 #define LEVIN_ERROR_CONNECTION_DESTROYED -3
106 #define LEVIN_ERROR_CONNECTION_TIMEDOUT -4
107 #define LEVIN_ERROR_CONNECTION_NO_DUPLEX_PROTOCOL -5
108 #define LEVIN_ERROR_CONNECTION_HANDLER_NOT_DEFINED -6
109 #define LEVIN_ERROR_FORMAT -7
110
111 #define DESCRIBE_RET_CODE(code) case code: return #code;
112 inline
113 const char* get_err_descr(int err)
114 {
115 switch(err)
116 {
117 DESCRIBE_RET_CODE(LEVIN_OK);
118 DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION);
119 DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION_NOT_FOUND);
120 DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION_DESTROYED);
121 DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION_TIMEDOUT);
122 DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION_NO_DUPLEX_PROTOCOL);
123 DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION_HANDLER_NOT_DEFINED);
124 DESCRIBE_RET_CODE(LEVIN_ERROR_FORMAT);
125 default:
126 return "unknown code";
127 }
128 }
129
130 //! Provides space for levin (p2p) header, so that payload can be sent without copy
131 class message_writer
132 {
133 byte_slice finalize(uint32_t command, uint32_t flags, uint32_t return_code, bool expect_response);
134 public:
135 using header = bucket_head2;
136
137 explicit message_writer(std::size_t reserve = 8192);
138
139 message_writer(const message_writer&) = delete;
140 message_writer(message_writer&&) = default;
141 ~message_writer() = default;
142 message_writer& operator=(const message_writer&) = delete;
143 message_writer& operator=(message_writer&&) = default;
144
145 //! \return Size of payload (excludes header size).
146 std::size_t payload_size() const noexcept
147 {
148 return buffer.size() < sizeof(header) ? 0 : buffer.size() - sizeof(header);
149 }
150
151 byte_slice finalize_invoke(uint32_t command) { return finalize(command, LEVIN_PACKET_REQUEST, 0, true); }
152 byte_slice finalize_notify(uint32_t command) { return finalize(command, LEVIN_PACKET_REQUEST, 0, false); }
153 byte_slice finalize_response(uint32_t command, uint32_t return_code)
154 {
155 return finalize(command, LEVIN_PACKET_RESPONSE, return_code, false);
156 }
157
158 //! Has space for levin header until a finalize method is used
159 byte_stream buffer;
160 };
161
162 //! \return Intialized levin header.
163 bucket_head2 make_header(uint32_t command, uint64_t msg_size, uint32_t flags, bool expect_response) noexcept;
164
165 /*! Generate a dummy levin message.
166
167 \param noise_bytes Total size of the returned `byte_slice`.
168 \return `nullptr` if `noise_size` is smaller than the levin header.
169 Otherwise, a dummy levin message. */
170 byte_slice make_noise_notify(std::size_t noise_bytes);
171
172 /*! Generate 1+ levin messages that are identical to the noise message size.
173
174 \param noise_size Each levin message will be identical to this value.
175 \return `nullptr` if `noise.size()` is less than the levin header size.
176 Otherwise, a levin notification message OR 2+ levin fragment messages.
177 Each message is `noise.size()` in length. */
178 byte_slice make_fragmented_notify(const std::size_t noise_size, int command, message_writer message);
179 }
180 }
181
182
183 #endif //_LEVIN_BASE_H_
184