2014-07-25 18:29:08 +02:00
|
|
|
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
|
2014-03-03 23:07:58 +01:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
2014-07-25 18:29:08 +02:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer in the
|
|
|
|
// documentation and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Andrey N. Sabelnikov nor the
|
|
|
|
// names of its contributors may be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
2014-07-23 15:03:52 +02:00
|
|
|
//
|
2014-07-25 18:29:08 +02:00
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
|
|
|
|
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2014-07-23 15:03:52 +02:00
|
|
|
//
|
2014-07-25 18:29:08 +02:00
|
|
|
|
|
|
|
|
2014-03-03 23:07:58 +01:00
|
|
|
|
|
|
|
#ifndef _LEVIN_BASE_H_
|
|
|
|
#define _LEVIN_BASE_H_
|
|
|
|
|
2019-05-16 22:34:22 +02:00
|
|
|
#include <cstdint>
|
|
|
|
|
2014-03-03 23:07:58 +01:00
|
|
|
#include "net_utils_base.h"
|
2019-05-16 22:34:22 +02:00
|
|
|
#include "span.h"
|
2014-03-03 23:07:58 +01:00
|
|
|
|
|
|
|
#define LEVIN_SIGNATURE 0x0101010101012101LL //Bender's nightmare
|
|
|
|
|
|
|
|
namespace epee
|
|
|
|
{
|
2020-10-13 17:15:07 +02:00
|
|
|
class byte_slice;
|
2014-03-03 23:07:58 +01:00
|
|
|
namespace levin
|
|
|
|
{
|
|
|
|
#pragma pack(push)
|
|
|
|
#pragma pack(1)
|
|
|
|
struct bucket_head
|
|
|
|
{
|
2014-03-20 12:46:11 +01:00
|
|
|
uint64_t m_signature;
|
|
|
|
uint64_t m_cb;
|
|
|
|
bool m_have_to_return_data;
|
|
|
|
uint32_t m_command;
|
|
|
|
int32_t m_return_code;
|
|
|
|
uint32_t m_reservedA; //probably some flags in future
|
|
|
|
uint32_t m_reservedB; //probably some check sum in future
|
2014-03-03 23:07:58 +01:00
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
|
|
|
|
#pragma pack(push)
|
|
|
|
#pragma pack(1)
|
|
|
|
struct bucket_head2
|
|
|
|
{
|
2014-03-20 12:46:11 +01:00
|
|
|
uint64_t m_signature;
|
|
|
|
uint64_t m_cb;
|
|
|
|
bool m_have_to_return_data;
|
|
|
|
uint32_t m_command;
|
|
|
|
int32_t m_return_code;
|
|
|
|
uint32_t m_flags;
|
|
|
|
uint32_t m_protocol_version;
|
2014-03-03 23:07:58 +01:00
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
|
|
|
|
#define LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED 0
|
2020-12-30 01:58:53 +01:00
|
|
|
#define LEVIN_INITIAL_MAX_PACKET_SIZE 256*1024 // 256 KiB before handshake
|
|
|
|
#define LEVIN_DEFAULT_MAX_PACKET_SIZE 100000000 //100MB by default after handshake
|
2014-03-03 23:07:58 +01:00
|
|
|
|
|
|
|
#define LEVIN_PACKET_REQUEST 0x00000001
|
|
|
|
#define LEVIN_PACKET_RESPONSE 0x00000002
|
2019-05-16 22:34:22 +02:00
|
|
|
#define LEVIN_PACKET_BEGIN 0x00000004
|
|
|
|
#define LEVIN_PACKET_END 0x00000008
|
2014-03-03 23:07:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
#define LEVIN_PROTOCOL_VER_0 0
|
|
|
|
#define LEVIN_PROTOCOL_VER_1 1
|
|
|
|
|
|
|
|
template<class t_connection_context = net_utils::connection_context_base>
|
|
|
|
struct levin_commands_handler
|
|
|
|
{
|
2020-10-13 17:15:07 +02:00
|
|
|
virtual int invoke(int command, const epee::span<const uint8_t> in_buff, byte_slice& buff_out, t_connection_context& context)=0;
|
2018-12-06 19:04:33 +01:00
|
|
|
virtual int notify(int command, const epee::span<const uint8_t> in_buff, t_connection_context& context)=0;
|
2014-03-03 23:07:58 +01:00
|
|
|
virtual void callback(t_connection_context& context){};
|
|
|
|
|
|
|
|
virtual void on_connection_new(t_connection_context& context){};
|
|
|
|
virtual void on_connection_close(t_connection_context& context){};
|
|
|
|
|
2017-10-09 17:46:42 +02:00
|
|
|
virtual ~levin_commands_handler(){}
|
2014-03-03 23:07:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#define LEVIN_OK 0
|
|
|
|
#define LEVIN_ERROR_CONNECTION -1
|
|
|
|
#define LEVIN_ERROR_CONNECTION_NOT_FOUND -2
|
|
|
|
#define LEVIN_ERROR_CONNECTION_DESTROYED -3
|
|
|
|
#define LEVIN_ERROR_CONNECTION_TIMEDOUT -4
|
|
|
|
#define LEVIN_ERROR_CONNECTION_NO_DUPLEX_PROTOCOL -5
|
|
|
|
#define LEVIN_ERROR_CONNECTION_HANDLER_NOT_DEFINED -6
|
|
|
|
#define LEVIN_ERROR_FORMAT -7
|
|
|
|
|
|
|
|
#define DESCRIBE_RET_CODE(code) case code: return #code;
|
|
|
|
inline
|
|
|
|
const char* get_err_descr(int err)
|
|
|
|
{
|
|
|
|
switch(err)
|
|
|
|
{
|
|
|
|
DESCRIBE_RET_CODE(LEVIN_OK);
|
|
|
|
DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION);
|
|
|
|
DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION_NOT_FOUND);
|
|
|
|
DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION_DESTROYED);
|
|
|
|
DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION_TIMEDOUT);
|
|
|
|
DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION_NO_DUPLEX_PROTOCOL);
|
|
|
|
DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION_HANDLER_NOT_DEFINED);
|
|
|
|
DESCRIBE_RET_CODE(LEVIN_ERROR_FORMAT);
|
|
|
|
default:
|
|
|
|
return "unknown code";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:34:22 +02:00
|
|
|
//! \return Intialized levin header.
|
|
|
|
bucket_head2 make_header(uint32_t command, uint64_t msg_size, uint32_t flags, bool expect_response) noexcept;
|
|
|
|
|
|
|
|
//! \return A levin notification message.
|
|
|
|
byte_slice make_notify(int command, epee::span<const std::uint8_t> payload);
|
|
|
|
|
|
|
|
/*! Generate a dummy levin message.
|
2014-03-03 23:07:58 +01:00
|
|
|
|
2019-05-16 22:34:22 +02:00
|
|
|
\param noise_bytes Total size of the returned `byte_slice`.
|
|
|
|
\return `nullptr` if `noise_size` is smaller than the levin header.
|
|
|
|
Otherwise, a dummy levin message. */
|
|
|
|
byte_slice make_noise_notify(std::size_t noise_bytes);
|
|
|
|
|
|
|
|
/*! Generate 1+ levin messages that are identical to the noise message size.
|
|
|
|
|
|
|
|
\param noise Each levin message will be identical to the size of this
|
|
|
|
message. The bytes from this message will be used for padding.
|
|
|
|
\return `nullptr` if `noise.size()` is less than the levin header size.
|
|
|
|
Otherwise, a levin notification message OR 2+ levin fragment messages.
|
|
|
|
Each message is `noise.size()` in length. */
|
|
|
|
byte_slice make_fragmented_notify(const byte_slice& noise, int command, epee::span<const std::uint8_t> payload);
|
2014-03-03 23:07:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif //_LEVIN_BASE_H_
|
2019-05-16 22:34:22 +02:00
|
|
|
|