Merge pull request #4866

9c923bad epee: fix network packet header field endianness (moneromooo-monero)
ec1a62b5 move int-util.h to epee (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2018-12-04 17:30:11 +02:00
commit e799fc87ae
No known key found for this signature in database
GPG Key ID: 55432DF31CCD4FCD
21 changed files with 127 additions and 72 deletions

View File

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

View File

@ -30,6 +30,7 @@
#include "levin_base.h"
#include "serializeble_struct_helper.h"
#include "int-util.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "net"
@ -43,11 +44,11 @@ namespace levin
{
buff.resize(sizeof(levin::bucket_head));
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_have_to_return_data = true;
head.m_command = command_id;
head.m_return_code = 1;
head.m_command = SWAP32LE(command_id);
head.m_return_code = SWAP32LE(1);
head.m_reservedA = rand(); //probably some flags 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))
return false;
head.m_cb = buff_strg.size();
head.m_cb = SWAP64LE(buff_strg.size());
buff.append(buff_strg);
return true;
}
@ -65,15 +66,15 @@ namespace levin
{
buff.resize(sizeof(levin::bucket_head));
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_have_to_return_data = true;
head.m_command = command_id;
head.m_return_code = 1;
head.m_command = SWAP32LE(command_id);
head.m_return_code = SWAP32LE(1);
head.m_reservedA = rand(); //probably some flags 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);
return true;
}
@ -86,7 +87,17 @@ namespace levin
return false;
}
levin::bucket_head& head = *(levin::bucket_head*)(&buff[0]);
#if BYTE_ORDER == LITTLE_ENDIAN
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)
{
LOG_PRINT_L3("Failed to read signature in levin message, at load_struct_from_levin_message");
@ -113,7 +124,17 @@ namespace levin
return false;
}
levin::bucket_head& head = *(levin::bucket_head*)(&buff[0]);
#if BYTE_ORDER == LITTLE_ENDIAN
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)
{
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 "levin_base.h"
#include "int-util.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "net"
@ -103,7 +104,7 @@ namespace levin
case conn_state_reading_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");
return false;
@ -112,13 +113,23 @@ namespace levin
break;
}
{
bucket_head* phead = (bucket_head*)m_cach_in_buffer.data();
if(LEVIN_SIGNATURE != phead->m_signature)
#if BYTE_ORDER == LITTLE_ENDIAN
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");
return false;
}
m_current_head = *phead;
m_current_head = phead;
}
m_cach_in_buffer.erase(0, sizeof(bucket_head));
m_state = conn_state_reading_body;

View File

@ -37,6 +37,7 @@
#include "misc_language.h"
#include "syncobj.h"
#include "misc_os_dependent.h"
#include "int-util.h"
#include <random>
#include <chrono>
@ -469,7 +470,18 @@ public:
m_current_head.m_have_to_return_data = false;
m_current_head.m_protocol_version = LEVIN_PROTOCOL_VER_1;
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));
#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;
CRITICAL_REGION_BEGIN(m_send_lock);
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(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");
return false;
@ -500,13 +512,23 @@ public:
break;
}
bucket_head2* phead = (bucket_head2*)m_cache_in_buffer.data();
if(LEVIN_SIGNATURE != phead->m_signature)
#if BYTE_ORDER == LITTLE_ENDIAN
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");
return false;
}
m_current_head = *phead;
m_current_head = phead;
m_cache_in_buffer.erase(0, sizeof(bucket_head2));
m_state = stream_state_body;
@ -566,13 +588,13 @@ public:
}
bucket_head2 head = {0};
head.m_signature = LEVIN_SIGNATURE;
head.m_cb = in_buff.size();
head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
head.m_cb = SWAP64LE(in_buff.size());
head.m_have_to_return_data = true;
head.m_flags = LEVIN_PACKET_REQUEST;
head.m_command = command;
head.m_protocol_version = LEVIN_PROTOCOL_VER_1;
head.m_flags = SWAP32LE(LEVIN_PACKET_REQUEST);
head.m_command = SWAP32LE(command);
head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
boost::interprocess::ipcdetail::atomic_write32(&m_invoke_buf_ready, 0);
CRITICAL_REGION_BEGIN(m_send_lock);
@ -624,13 +646,13 @@ public:
return LEVIN_ERROR_CONNECTION_DESTROYED;
bucket_head2 head = {0};
head.m_signature = LEVIN_SIGNATURE;
head.m_cb = in_buff.size();
head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
head.m_cb = SWAP64LE(in_buff.size());
head.m_have_to_return_data = true;
head.m_flags = LEVIN_PACKET_REQUEST;
head.m_command = command;
head.m_protocol_version = LEVIN_PROTOCOL_VER_1;
head.m_flags = SWAP32LE(LEVIN_PACKET_REQUEST);
head.m_command = SWAP32LE(command);
head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
boost::interprocess::ipcdetail::atomic_write32(&m_invoke_buf_ready, 0);
CRITICAL_REGION_BEGIN(m_send_lock);
@ -698,13 +720,13 @@ public:
return LEVIN_ERROR_CONNECTION_DESTROYED;
bucket_head2 head = {0};
head.m_signature = LEVIN_SIGNATURE;
head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
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_protocol_version = LEVIN_PROTOCOL_VER_1;
head.m_flags = LEVIN_PACKET_REQUEST;
head.m_command = SWAP32LE(command);
head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
head.m_flags = SWAP32LE(LEVIN_PACKET_REQUEST);
CRITICAL_REGION_BEGIN(m_send_lock);
if(!m_pservice_endpoint->do_send(&head, sizeof(head)))
{

View File

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

View File

@ -63,7 +63,6 @@ set(common_private_headers
error.h
expect.h
http_connection.h
int-util.h
notify.h
pod-class.h
rpc_client.h

View File

@ -19,7 +19,7 @@ Issue Date: 20/12/2007
*/
#include <stdint.h>
#include "common/int-util.h"
#include "int-util.h"
#if defined(__cplusplus)
extern "C"

View File

@ -11,7 +11,7 @@ Public domain.
#endif
#include "chacha.h"
#include "common/int-util.h"
#include "int-util.h"
#include "warnings.h"
/*

View File

@ -29,7 +29,7 @@
#ifndef __tables_h
#define __tables_h
#include "common/int-util.h"
#include "int-util.h"
#if BYTE_ORDER == LITTLE_ENDIAN

View File

@ -37,7 +37,7 @@
#include <stddef.h>
#include <stdint.h>
#include "common/int-util.h"
#include "int-util.h"
#include "warnings.h"
static inline void *padd(void *p, size_t i) {

View File

@ -5,7 +5,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "common/int-util.h"
#include "int-util.h"
#include "hash-ops.h"
#include "keccak.h"

View File

@ -114,7 +114,7 @@ typedef uint64_t u64b_t; /* 64-bit unsigned integer */
#ifndef SKEIN_NEED_SWAP /* compile-time "override" for endianness? */
#include "common/int-util.h"
#include "int-util.h"
#define IS_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */
#define IS_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */

View File

@ -35,7 +35,7 @@
#include <stdio.h>
#include <unistd.h>
#include "common/int-util.h"
#include "int-util.h"
#include "hash-ops.h"
#include "oaes_lib.h"
#include "variant2_int_sqrt.h"

View File

@ -40,7 +40,7 @@ using namespace epee;
#include "misc_language.h"
#include "common/base58.h"
#include "crypto/hash.h"
#include "common/int-util.h"
#include "int-util.h"
#include "common/dns_utils.h"
#undef MONERO_DEFAULT_LOG_CATEGORY

View File

@ -34,7 +34,7 @@
#include <cstdint>
#include <vector>
#include "common/int-util.h"
#include "int-util.h"
#include "crypto/hash.h"
#include "cryptonote_config.h"
#include "difficulty.h"

View File

@ -44,7 +44,7 @@
#include "misc_language.h"
#include "profile_tools.h"
#include "file_io_utils.h"
#include "common/int-util.h"
#include "int-util.h"
#include "common/threadpool.h"
#include "common/boost_serialization_helper.h"
#include "warnings.h"

View File

@ -40,7 +40,7 @@
#include "blockchain.h"
#include "blockchain_db/blockchain_db.h"
#include "common/boost_serialization_helper.h"
#include "common/int-util.h"
#include "int-util.h"
#include "misc_language.h"
#include "warnings.h"
#include "common/perf_timer.h"

View File

@ -31,7 +31,7 @@
#include "device_default.hpp"
#include "common/int-util.h"
#include "int-util.h"
#include "cryptonote_basic/account.h"
#include "cryptonote_basic/subaddress_index.h"
#include "ringct/rctOps.h"

View File

@ -42,7 +42,7 @@
#include <unordered_map>
#include "wipeable_string.h"
#include "misc_language.h"
#include "common/int-util.h"
#include "int-util.h"
#include "mnemonics/electrum-words.h"
#include <boost/crc.hpp>

View File

@ -30,7 +30,7 @@
#include "gtest/gtest.h"
#include "common/int-util.h"
#include "int-util.h"
namespace
{