epee: fix network packet header field endianness

This commit is contained in:
moneromooo-monero 2018-11-18 10:18:35 +00:00
parent ec1a62b50d
commit 9c923bad9b
No known key found for this signature in database
GPG Key ID: 686F07454D6CEFC3
5 changed files with 113 additions and 57 deletions

View File

@ -80,10 +80,10 @@ int levin_client_impl::invoke(int command, const std::string& in_buff, std::stri
return -1; return -1;
bucket_head head = {0}; bucket_head head = {0};
head.m_signature = LEVIN_SIGNATURE; head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
head.m_cb = in_buff.size(); head.m_cb = SWAP64LE(in_buff.size());
head.m_have_to_return_data = true; head.m_have_to_return_data = true;
head.m_command = command; head.m_command = SWAP32LE(command);
if(!m_transport.send(&head, sizeof(head))) if(!m_transport.send(&head, sizeof(head)))
return -1; return -1;
@ -97,7 +97,7 @@ int levin_client_impl::invoke(int command, const std::string& in_buff, std::stri
head = *(bucket_head*)local_buff.data(); head = *(bucket_head*)local_buff.data();
if(head.m_signature!=LEVIN_SIGNATURE) if(head.m_signature!=SWAP64LE(LEVIN_SIGNATURE))
{ {
LOG_PRINT_L1("Signature mismatch in response"); LOG_PRINT_L1("Signature mismatch in response");
return -1; return -1;
@ -116,10 +116,10 @@ int levin_client_impl::notify(int command, const std::string& in_buff)
return -1; return -1;
bucket_head head = {0}; bucket_head head = {0};
head.m_signature = LEVIN_SIGNATURE; head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
head.m_cb = in_buff.size(); head.m_cb = SWAP64LE(in_buff.size());
head.m_have_to_return_data = false; head.m_have_to_return_data = false;
head.m_command = command; head.m_command = SWAP32LE(command);
if(!m_transport.send((const char*)&head, sizeof(head))) if(!m_transport.send((const char*)&head, sizeof(head)))
return -1; return -1;
@ -139,12 +139,13 @@ inline
return -1; return -1;
bucket_head2 head = {0}; bucket_head2 head = {0};
head.m_signature = LEVIN_SIGNATURE; head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
head.m_cb = in_buff.size(); head.m_cb = SWAP64LE(in_buff.size());
head.m_have_to_return_data = true; head.m_have_to_return_data = true;
head.m_command = command; head.m_command = SWAP32LE(command);
head.m_protocol_version = LEVIN_PROTOCOL_VER_1; head.m_return_code = SWAP32LE(0);
head.m_flags = LEVIN_PACKET_REQUEST; head.m_flags = SWAP32LE(LEVIN_PACKET_REQUEST);
head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
if(!m_transport.send(&head, sizeof(head))) if(!m_transport.send(&head, sizeof(head)))
return -1; return -1;
@ -157,14 +158,13 @@ inline
head = *(bucket_head2*)local_buff.data(); head = *(bucket_head2*)local_buff.data();
if(head.m_signature != SWAP64LE(LEVIN_SIGNATURE))
if(head.m_signature!=LEVIN_SIGNATURE)
{ {
LOG_PRINT_L1("Signature mismatch in response"); LOG_PRINT_L1("Signature mismatch in response");
return -1; return -1;
} }
if(!m_transport.recv_n(buff_out, head.m_cb)) if(!m_transport.recv_n(buff_out, SWAP64LE(head.m_cb)))
return -1; return -1;
return head.m_return_code; return head.m_return_code;
@ -177,12 +177,13 @@ inline
return -1; return -1;
bucket_head2 head = {0}; bucket_head2 head = {0};
head.m_signature = LEVIN_SIGNATURE; head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
head.m_cb = in_buff.size(); head.m_cb = SWAP64LE(in_buff.size());
head.m_have_to_return_data = false; head.m_have_to_return_data = false;
head.m_command = command; head.m_command = SWAP32LE(command);
head.m_protocol_version = LEVIN_PROTOCOL_VER_1; head.m_return_code = SWAP32LE(0);
head.m_flags = LEVIN_PACKET_REQUEST; head.m_flags = SWAP32LE(LEVIN_PACKET_REQUEST);
head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
if(!m_transport.send((const char*)&head, sizeof(head))) if(!m_transport.send((const char*)&head, sizeof(head)))
return -1; return -1;

View File

@ -30,6 +30,7 @@
#include "levin_base.h" #include "levin_base.h"
#include "serializeble_struct_helper.h" #include "serializeble_struct_helper.h"
#include "int-util.h"
#undef MONERO_DEFAULT_LOG_CATEGORY #undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "net" #define MONERO_DEFAULT_LOG_CATEGORY "net"
@ -43,11 +44,11 @@ namespace levin
{ {
buff.resize(sizeof(levin::bucket_head)); buff.resize(sizeof(levin::bucket_head));
levin::bucket_head& head = *(levin::bucket_head*)(&buff[0]); levin::bucket_head& head = *(levin::bucket_head*)(&buff[0]);
head.m_signature = LEVIN_SIGNATURE; head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
head.m_cb = 0; head.m_cb = 0;
head.m_have_to_return_data = true; head.m_have_to_return_data = true;
head.m_command = command_id; head.m_command = SWAP32LE(command_id);
head.m_return_code = 1; head.m_return_code = SWAP32LE(1);
head.m_reservedA = rand(); //probably some flags in future head.m_reservedA = rand(); //probably some flags in future
head.m_reservedB = rand(); //probably some check summ in future head.m_reservedB = rand(); //probably some check summ in future
@ -55,7 +56,7 @@ namespace levin
if(!StorageNamed::save_struct_as_storage_to_buff_t<t_struct, StorageNamed::DefaultStorageType>(t, buff_strg)) if(!StorageNamed::save_struct_as_storage_to_buff_t<t_struct, StorageNamed::DefaultStorageType>(t, buff_strg))
return false; return false;
head.m_cb = buff_strg.size(); head.m_cb = SWAP64LE(buff_strg.size());
buff.append(buff_strg); buff.append(buff_strg);
return true; return true;
} }
@ -65,15 +66,15 @@ namespace levin
{ {
buff.resize(sizeof(levin::bucket_head)); buff.resize(sizeof(levin::bucket_head));
levin::bucket_head& head = *(levin::bucket_head*)(&buff[0]); levin::bucket_head& head = *(levin::bucket_head*)(&buff[0]);
head.m_signature = LEVIN_SIGNATURE; head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
head.m_cb = 0; head.m_cb = 0;
head.m_have_to_return_data = true; head.m_have_to_return_data = true;
head.m_command = command_id; head.m_command = SWAP32LE(command_id);
head.m_return_code = 1; head.m_return_code = SWAP32LE(1);
head.m_reservedA = rand(); //probably some flags in future head.m_reservedA = rand(); //probably some flags in future
head.m_reservedB = rand(); //probably some check summ in future head.m_reservedB = rand(); //probably some check summ in future
head.m_cb = data.size(); head.m_cb = SWAP64LE(data.size());
buff.append(data); buff.append(data);
return true; return true;
} }
@ -86,7 +87,17 @@ namespace levin
return false; return false;
} }
#if BYTE_ORDER == LITTLE_ENDIAN
levin::bucket_head &head = *(levin::bucket_head*)(&buff[0]); levin::bucket_head &head = *(levin::bucket_head*)(&buff[0]);
#else
levin::bucket_head head = *(levin::bucket_head*)(&buff[0]);
head.m_signature = SWAP64LE(head.m_signature);
head.m_cb = SWAP64LE(head.m_cb);
head.m_command = SWAP32LE(head.m_command);
head.m_return_code = SWAP32LE(head.m_return_code);
head.m_reservedA = SWAP32LE(head.m_reservedA);
head.m_reservedB = SWAP32LE(head.m_reservedB);
#endif
if(head.m_signature != LEVIN_SIGNATURE) if(head.m_signature != LEVIN_SIGNATURE)
{ {
LOG_PRINT_L3("Failed to read signature in levin message, at load_struct_from_levin_message"); LOG_PRINT_L3("Failed to read signature in levin message, at load_struct_from_levin_message");
@ -113,7 +124,17 @@ namespace levin
return false; return false;
} }
#if BYTE_ORDER == LITTLE_ENDIAN
levin::bucket_head &head = *(levin::bucket_head*)(&buff[0]); levin::bucket_head &head = *(levin::bucket_head*)(&buff[0]);
#else
levin::bucket_head head = *(levin::bucket_head*)(&buff[0]);
head.m_signature = SWAP64LE(head.m_signature);
head.m_cb = SWAP64LE(head.m_cb);
head.m_command = SWAP32LE(head.m_command);
head.m_return_code = SWAP32LE(head.m_return_code);
head.m_reservedA = SWAP32LE(head.m_reservedA);
head.m_reservedB = SWAP32LE(head.m_reservedB);
#endif
if(head.m_signature != LEVIN_SIGNATURE) if(head.m_signature != LEVIN_SIGNATURE)
{ {
LOG_ERROR("Failed to read signature in levin message, at load_struct_from_levin_message"); LOG_ERROR("Failed to read signature in levin message, at load_struct_from_levin_message");

View File

@ -31,6 +31,7 @@
#include <boost/uuid/uuid_generators.hpp> #include <boost/uuid/uuid_generators.hpp>
#include "levin_base.h" #include "levin_base.h"
#include "int-util.h"
#undef MONERO_DEFAULT_LOG_CATEGORY #undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "net" #define MONERO_DEFAULT_LOG_CATEGORY "net"
@ -103,7 +104,7 @@ namespace levin
case conn_state_reading_head: case conn_state_reading_head:
if(m_cach_in_buffer.size() < sizeof(bucket_head)) if(m_cach_in_buffer.size() < sizeof(bucket_head))
{ {
if(m_cach_in_buffer.size() >= sizeof(uint64_t) && *((uint64_t*)m_cach_in_buffer.data()) != LEVIN_SIGNATURE) if(m_cach_in_buffer.size() >= sizeof(uint64_t) && *((uint64_t*)m_cach_in_buffer.data()) != SWAP64LE(LEVIN_SIGNATURE))
{ {
LOG_ERROR_CC(m_conn_context, "Signature mismatch on accepted connection"); LOG_ERROR_CC(m_conn_context, "Signature mismatch on accepted connection");
return false; return false;
@ -112,13 +113,23 @@ namespace levin
break; break;
} }
{ {
bucket_head* phead = (bucket_head*)m_cach_in_buffer.data(); #if BYTE_ORDER == LITTLE_ENDIAN
if(LEVIN_SIGNATURE != phead->m_signature) bucket_head &phead = *(bucket_head*)m_cach_in_buffer.data();
#else
bucket_head phead = *(bucket_head*)m_cach_in_buffer.data();
phead.m_signature = SWAP64LE(phead.m_signature);
phead.m_cb = SWAP64LE(phead.m_cb);
phead.m_command = SWAP32LE(phead.m_command);
phead.m_return_code = SWAP32LE(phead.m_return_code);
phead.m_reservedA = SWAP32LE(phead.m_reservedA);
phead.m_reservedB = SWAP32LE(phead.m_reservedB);
#endif
if(LEVIN_SIGNATURE != phead.m_signature)
{ {
LOG_ERROR_CC(m_conn_context, "Signature mismatch on accepted connection"); LOG_ERROR_CC(m_conn_context, "Signature mismatch on accepted connection");
return false; return false;
} }
m_current_head = *phead; m_current_head = phead;
} }
m_cach_in_buffer.erase(0, sizeof(bucket_head)); m_cach_in_buffer.erase(0, sizeof(bucket_head));
m_state = conn_state_reading_body; m_state = conn_state_reading_body;

View File

@ -37,6 +37,7 @@
#include "misc_language.h" #include "misc_language.h"
#include "syncobj.h" #include "syncobj.h"
#include "misc_os_dependent.h" #include "misc_os_dependent.h"
#include "int-util.h"
#include <random> #include <random>
#include <chrono> #include <chrono>
@ -469,7 +470,18 @@ public:
m_current_head.m_have_to_return_data = false; m_current_head.m_have_to_return_data = false;
m_current_head.m_protocol_version = LEVIN_PROTOCOL_VER_1; m_current_head.m_protocol_version = LEVIN_PROTOCOL_VER_1;
m_current_head.m_flags = LEVIN_PACKET_RESPONSE; m_current_head.m_flags = LEVIN_PACKET_RESPONSE;
#if BYTE_ORDER == LITTLE_ENDIAN
std::string send_buff((const char*)&m_current_head, sizeof(m_current_head)); std::string send_buff((const char*)&m_current_head, sizeof(m_current_head));
#else
bucket_head2 head = m_current_head;
head.m_signature = SWAP64LE(head.m_signature);
head.m_cb = SWAP64LE(head.m_cb);
head.m_command = SWAP32LE(head.m_command);
head.m_return_code = SWAP32LE(head.m_return_code);
head.m_flags = SWAP32LE(head.m_flags);
head.m_protocol_version = SWAP32LE(head.m_protocol_version);
std::string send_buff((const char*)&head, sizeof(head));
#endif
send_buff += return_buff; send_buff += return_buff;
CRITICAL_REGION_BEGIN(m_send_lock); CRITICAL_REGION_BEGIN(m_send_lock);
if(!m_pservice_endpoint->do_send(send_buff.data(), send_buff.size())) if(!m_pservice_endpoint->do_send(send_buff.data(), send_buff.size()))
@ -491,7 +503,7 @@ public:
{ {
if(m_cache_in_buffer.size() < sizeof(bucket_head2)) if(m_cache_in_buffer.size() < sizeof(bucket_head2))
{ {
if(m_cache_in_buffer.size() >= sizeof(uint64_t) && *((uint64_t*)m_cache_in_buffer.data()) != LEVIN_SIGNATURE) if(m_cache_in_buffer.size() >= sizeof(uint64_t) && *((uint64_t*)m_cache_in_buffer.data()) != SWAP64LE(LEVIN_SIGNATURE))
{ {
MWARNING(m_connection_context << "Signature mismatch, connection will be closed"); MWARNING(m_connection_context << "Signature mismatch, connection will be closed");
return false; return false;
@ -500,13 +512,23 @@ public:
break; break;
} }
bucket_head2* phead = (bucket_head2*)m_cache_in_buffer.data(); #if BYTE_ORDER == LITTLE_ENDIAN
if(LEVIN_SIGNATURE != phead->m_signature) bucket_head2& phead = *(bucket_head2*)m_cache_in_buffer.data();
#else
bucket_head2 phead = *(bucket_head2*)m_cache_in_buffer.data();
phead.m_signature = SWAP64LE(phead.m_signature);
phead.m_cb = SWAP64LE(phead.m_cb);
phead.m_command = SWAP32LE(phead.m_command);
phead.m_return_code = SWAP32LE(phead.m_return_code);
phead.m_flags = SWAP32LE(phead.m_flags);
phead.m_protocol_version = SWAP32LE(phead.m_protocol_version);
#endif
if(LEVIN_SIGNATURE != phead.m_signature)
{ {
LOG_ERROR_CC(m_connection_context, "Signature mismatch, connection will be closed"); LOG_ERROR_CC(m_connection_context, "Signature mismatch, connection will be closed");
return false; return false;
} }
m_current_head = *phead; m_current_head = phead;
m_cache_in_buffer.erase(0, sizeof(bucket_head2)); m_cache_in_buffer.erase(0, sizeof(bucket_head2));
m_state = stream_state_body; m_state = stream_state_body;
@ -566,13 +588,13 @@ public:
} }
bucket_head2 head = {0}; bucket_head2 head = {0};
head.m_signature = LEVIN_SIGNATURE; head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
head.m_cb = in_buff.size(); head.m_cb = SWAP64LE(in_buff.size());
head.m_have_to_return_data = true; head.m_have_to_return_data = true;
head.m_flags = LEVIN_PACKET_REQUEST; head.m_flags = SWAP32LE(LEVIN_PACKET_REQUEST);
head.m_command = command; head.m_command = SWAP32LE(command);
head.m_protocol_version = LEVIN_PROTOCOL_VER_1; head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
boost::interprocess::ipcdetail::atomic_write32(&m_invoke_buf_ready, 0); boost::interprocess::ipcdetail::atomic_write32(&m_invoke_buf_ready, 0);
CRITICAL_REGION_BEGIN(m_send_lock); CRITICAL_REGION_BEGIN(m_send_lock);
@ -624,13 +646,13 @@ public:
return LEVIN_ERROR_CONNECTION_DESTROYED; return LEVIN_ERROR_CONNECTION_DESTROYED;
bucket_head2 head = {0}; bucket_head2 head = {0};
head.m_signature = LEVIN_SIGNATURE; head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
head.m_cb = in_buff.size(); head.m_cb = SWAP64LE(in_buff.size());
head.m_have_to_return_data = true; head.m_have_to_return_data = true;
head.m_flags = LEVIN_PACKET_REQUEST; head.m_flags = SWAP32LE(LEVIN_PACKET_REQUEST);
head.m_command = command; head.m_command = SWAP32LE(command);
head.m_protocol_version = LEVIN_PROTOCOL_VER_1; head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
boost::interprocess::ipcdetail::atomic_write32(&m_invoke_buf_ready, 0); boost::interprocess::ipcdetail::atomic_write32(&m_invoke_buf_ready, 0);
CRITICAL_REGION_BEGIN(m_send_lock); CRITICAL_REGION_BEGIN(m_send_lock);
@ -698,13 +720,13 @@ public:
return LEVIN_ERROR_CONNECTION_DESTROYED; return LEVIN_ERROR_CONNECTION_DESTROYED;
bucket_head2 head = {0}; bucket_head2 head = {0};
head.m_signature = LEVIN_SIGNATURE; head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
head.m_have_to_return_data = false; head.m_have_to_return_data = false;
head.m_cb = in_buff.size(); head.m_cb = SWAP64LE(in_buff.size());
head.m_command = command; head.m_command = SWAP32LE(command);
head.m_protocol_version = LEVIN_PROTOCOL_VER_1; head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
head.m_flags = LEVIN_PACKET_REQUEST; head.m_flags = SWAP32LE(LEVIN_PACKET_REQUEST);
CRITICAL_REGION_BEGIN(m_send_lock); CRITICAL_REGION_BEGIN(m_send_lock);
if(!m_pservice_endpoint->do_send(&head, sizeof(head))) if(!m_pservice_endpoint->do_send(&head, sizeof(head)))
{ {

View File

@ -35,6 +35,7 @@
#include "portable_storage_to_json.h" #include "portable_storage_to_json.h"
#include "portable_storage_from_json.h" #include "portable_storage_from_json.h"
#include "portable_storage_val_converters.h" #include "portable_storage_val_converters.h"
#include "int-util.h"
namespace epee namespace epee
{ {
@ -135,8 +136,8 @@ namespace epee
TRY_ENTRY(); TRY_ENTRY();
std::stringstream ss; std::stringstream ss;
storage_block_header sbh = AUTO_VAL_INIT(sbh); storage_block_header sbh = AUTO_VAL_INIT(sbh);
sbh.m_signature_a = PORTABLE_STORAGE_SIGNATUREA; sbh.m_signature_a = SWAP32LE(PORTABLE_STORAGE_SIGNATUREA);
sbh.m_signature_b = PORTABLE_STORAGE_SIGNATUREB; sbh.m_signature_b = SWAP32LE(PORTABLE_STORAGE_SIGNATUREB);
sbh.m_ver = PORTABLE_STORAGE_FORMAT_VER; sbh.m_ver = PORTABLE_STORAGE_FORMAT_VER;
ss.write((const char*)&sbh, sizeof(storage_block_header)); ss.write((const char*)&sbh, sizeof(storage_block_header));
pack_entry_to_buff(ss, m_root); pack_entry_to_buff(ss, m_root);
@ -154,8 +155,8 @@ namespace epee
return false; return false;
} }
storage_block_header* pbuff = (storage_block_header*)source.data(); storage_block_header* pbuff = (storage_block_header*)source.data();
if(pbuff->m_signature_a != PORTABLE_STORAGE_SIGNATUREA || if(pbuff->m_signature_a != SWAP32LE(PORTABLE_STORAGE_SIGNATUREA) ||
pbuff->m_signature_b != PORTABLE_STORAGE_SIGNATUREB pbuff->m_signature_b != SWAP32LE(PORTABLE_STORAGE_SIGNATUREB)
) )
{ {
LOG_ERROR("portable_storage: wrong binary format - signature mismatch"); LOG_ERROR("portable_storage: wrong binary format - signature mismatch");