| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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; |
| 55 |
uint32_t m_reservedB; |
| 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 |
|
| 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 |
|
| 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 |
|
| 159 |
byte_stream buffer; |
| 160 |
}; |
| 161 |
|
| 162 |
|
| 163 |
bucket_head2 make_header(uint32_t command, uint64_t msg_size, uint32_t flags, bool expect_response) noexcept; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
byte_slice make_noise_notify(std::size_t noise_bytes); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
byte_slice make_fragmented_notify(const std::size_t noise_size, int command, message_writer message); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
#endif |
| 184 |
|