Replace llarp/util/endian.hpp with oxenc/endian.h

pull/1903/head
Jason Rhinelander 2 years ago
parent 64d6ba8a53
commit b09298e211
No known key found for this signature in database
GPG Key ID: C4992CE7A88D4262

@ -8,8 +8,8 @@
#include <sodium/crypto_aead_xchacha20poly1305.h>
#include <sodium/randombytes.h>
#include <sodium/utils.h>
#include <oxenc/endian.h>
#include <llarp/util/mem.hpp>
#include <llarp/util/endian.hpp>
#include <llarp/util/str.hpp>
#include <cassert>
#include <cstring>
@ -295,7 +295,7 @@ namespace llarp
std::array<byte_t, 160 + K::SIZE + sizeof(uint64_t)> buf;
std::copy(derived_key_hash_str, derived_key_hash_str + 160, buf.begin());
std::copy(k.begin(), k.end(), buf.begin() + 160);
htole64buf(buf.data() + 160 + K::SIZE, i);
oxenc::write_host_as_little(i, buf.data() + 160 + K::SIZE);
// n = H(b)
// h = make_point(n)
ShortHash n;

@ -1,9 +1,9 @@
#include "message.hpp"
#include <oxenc/endian.h>
#include "dns.hpp"
#include "srv_data.hpp"
#include <llarp/util/buffer.hpp>
#include <llarp/util/endian.hpp>
#include <llarp/util/logging/logger.hpp>
#include <llarp/util/printer.hpp>
#include <llarp/net/ip.hpp>
@ -197,7 +197,7 @@ namespace llarp
const auto addr = net::TruncateV6(ip);
rec.rr_type = qTypeA;
rec.rData.resize(4);
htobe32buf(rec.rData.data(), addr.h);
oxenc::write_host_as_big(addr.h, rec.rData.data());
}
answers.emplace_back(std::move(rec));
}

@ -22,12 +22,11 @@
#include <llarp/nodedb.hpp>
#include <llarp/quic/tunnel.hpp>
#include <llarp/rpc/endpoint_rpc.hpp>
#include <llarp/util/str.hpp>
#include <llarp/util/endian.hpp>
#include <llarp/dns/srv_data.hpp>
#include <oxenc/bt.h>
namespace llarp
{
namespace handlers

@ -27,8 +27,9 @@ namespace llarp
{
size_t extra = std::min(m_Data.size(), FragmentSize);
auto xmit = CreatePacket(Command::eXMIT, 10 + 32 + extra, 0, 0);
htobe16buf(xmit.data() + CommandOverhead + PacketOverhead, m_Data.size());
htobe64buf(xmit.data() + 2 + CommandOverhead + PacketOverhead, m_MsgID);
oxenc::write_host_as_big(
static_cast<uint16_t>(m_Data.size()), xmit.data() + CommandOverhead + PacketOverhead);
oxenc::write_host_as_big(m_MsgID, xmit.data() + 2 + CommandOverhead + PacketOverhead);
std::copy_n(
m_Digest.begin(), m_Digest.size(), xmit.data() + 10 + CommandOverhead + PacketOverhead);
std::copy_n(m_Data.data(), extra, xmit.data() + 10 + CommandOverhead + PacketOverhead + 32);
@ -71,8 +72,8 @@ namespace llarp
{
const size_t fragsz = idx + FragmentSize < datasz ? FragmentSize : datasz - idx;
auto frag = CreatePacket(Command::eDATA, fragsz + Overhead, 0, 0);
htobe16buf(frag.data() + 2 + PacketOverhead, idx);
htobe64buf(frag.data() + 4 + PacketOverhead, m_MsgID);
oxenc::write_host_as_big(idx, frag.data() + 2 + PacketOverhead);
oxenc::write_host_as_big(m_MsgID, frag.data() + 4 + PacketOverhead);
std::copy(
m_Data.begin() + idx,
m_Data.begin() + idx + fragsz,
@ -136,7 +137,7 @@ namespace llarp
InboundMessage::ACKS() const
{
auto acks = CreatePacket(Command::eACKS, 9);
htobe64buf(acks.data() + CommandOverhead + PacketOverhead, m_MsgID);
oxenc::write_host_as_big(m_MsgID, acks.data() + CommandOverhead + PacketOverhead);
acks[PacketOverhead + 10] = AcksBitmask();
return acks;
}

@ -223,7 +223,7 @@ namespace llarp
const auto& itr = m_SendMACKs.top();
while (numAcks > 0)
{
htobe64buf(ptr, itr);
oxenc::write_host_as_big(itr, ptr);
m_SendMACKs.pop();
numAcks--;
ptr += sizeof(uint64_t);
@ -716,7 +716,7 @@ namespace llarp
byte_t* ptr = data.data() + CommandOverhead + PacketOverhead + 1;
while (numAcks > 0)
{
uint64_t acked = bufbe64toh(ptr);
auto acked = oxenc::load_big_to_host<uint64_t>(ptr);
LogTrace("mack containing txid=", acked, " from ", m_RemoteAddr);
auto itr = m_TXMsgs.find(acked);
if (itr != m_TXMsgs.end())
@ -743,7 +743,7 @@ namespace llarp
LogError("short nack from ", m_RemoteAddr);
return;
}
uint64_t txid = bufbe64toh(data.data() + CommandOverhead + PacketOverhead);
auto txid = oxenc::load_big_to_host<uint64_t>(data.data() + CommandOverhead + PacketOverhead);
LogTrace("got nack on ", txid, " from ", m_RemoteAddr);
auto itr = m_TXMsgs.find(txid);
if (itr != m_TXMsgs.end())
@ -765,9 +765,9 @@ namespace llarp
return;
}
auto* pos = data.data() + CommandOverhead + PacketOverhead;
uint16_t sz = bufbe16toh(pos);
auto sz = oxenc::load_big_to_host<uint16_t>(pos);
pos += sizeof(sz);
uint64_t rxid = bufbe64toh(pos);
auto rxid = oxenc::load_big_to_host<uint64_t>(pos);
pos += sizeof(rxid);
auto p2 = pos + ShortHash::SIZE;
assert(p2 == data.data() + XMITOverhead);
@ -826,8 +826,9 @@ namespace llarp
return;
}
m_LastRX = m_Parent->Now();
uint16_t sz = bufbe16toh(data.data() + CommandOverhead + PacketOverhead);
uint64_t rxid = bufbe64toh(data.data() + CommandOverhead + sizeof(uint16_t) + PacketOverhead);
auto sz = oxenc::load_big_to_host<uint16_t>(data.data() + CommandOverhead + PacketOverhead);
auto rxid = oxenc::load_big_to_host<uint64_t>(
data.data() + CommandOverhead + sizeof(uint16_t) + PacketOverhead);
auto itr = m_RXMsgs.find(rxid);
if (itr == m_RXMsgs.end())
{
@ -835,7 +836,7 @@ namespace llarp
{
LogTrace("no rxid=", rxid, " for ", m_RemoteAddr);
auto nack = CreatePacket(Command::eNACK, 8);
htobe64buf(nack.data() + PacketOverhead + CommandOverhead, rxid);
oxenc::write_host_as_big(rxid, nack.data() + PacketOverhead + CommandOverhead);
EncryptAndSend(std::move(nack));
}
else
@ -888,7 +889,7 @@ namespace llarp
}
const auto now = m_Parent->Now();
m_LastRX = now;
uint64_t txid = bufbe64toh(data.data() + 2 + PacketOverhead);
auto txid = oxenc::load_big_to_host<uint64_t>(data.data() + 2 + PacketOverhead);
auto itr = m_TXMsgs.find(txid);
if (itr == m_TXMsgs.end())
{

@ -2,13 +2,14 @@
#include "ip.hpp"
#include <llarp/util/buffer.hpp>
#include <llarp/util/endian.hpp>
#include <llarp/util/mem.hpp>
#include <llarp/util/str.hpp>
#ifndef _WIN32
#include <netinet/in.h>
#endif
#include <oxenc/endian.h>
#include <algorithm>
#include <map>
@ -564,11 +565,11 @@ namespace llarp
// code 'Destination host unknown error'
*itr++ = 7;
// checksum + unused
htobe32buf(itr, 0);
oxenc::write_host_as_big<uint32_t>(0, itr);
checksum = (uint16_t*)itr;
itr += 4;
// next hop mtu is ignored but let's put something here anyways just in case tm
htobe16buf(itr, 1500);
oxenc::write_host_as_big<uint16_t>(1500, itr);
itr += 2;
// copy ip header and first 8 bytes of datagram for icmp rject
std::copy_n(buf, l4_PacketSize + l3_HeaderSize, itr);
@ -637,9 +638,9 @@ namespace llarp
ptr += 2;
std::memcpy(ptr, &dstport.n, 2);
ptr += 2;
htobe16buf(ptr, static_cast<uint16_t>(buf.sz + 8));
oxenc::write_host_as_big(static_cast<uint16_t>(buf.sz + 8), ptr);
ptr += 2;
htobe16buf(ptr, uint16_t{0}); // checksum
oxenc::write_host_as_big(uint16_t{0}, ptr); // checksum
ptr += 2;
std::copy_n(buf.base, buf.sz, ptr);

@ -4,6 +4,7 @@
#include "net.hpp"
#include <llarp/util/buffer.hpp>
#include <llarp/util/time.hpp>
#include <oxenc/endian.h> // Guarantees __{LITTLE,BIG}_ENDIAN__ defines
#ifndef _WIN32
// unix, linux
@ -16,11 +17,9 @@ struct ip_header
#ifdef __LITTLE_ENDIAN__
unsigned int ihl : 4;
unsigned int version : 4;
#elif defined(__BIG_ENDIAN__)
#else
unsigned int version : 4;
unsigned int ihl : 4;
#else
#error "Please fix <bits/endian.h>"
#endif
#if defined(__linux__)

@ -2,6 +2,8 @@
#include "ip.hpp"
#include <string>
#include <oxenc/endian.h>
namespace llarp
{
huint16_t
@ -46,7 +48,7 @@ namespace llarp
{
c.resize(16);
std::fill(c.begin(), c.end(), 0);
htobe32buf(c.data() + 12, h);
oxenc::write_host_as_big(h, c.data() + 12);
c[11] = 0xff;
c[10] = 0xff;
}

@ -15,7 +15,6 @@
#include <cstdlib> // for itoa
#include <iostream>
#include <llarp/util/endian.hpp>
#include <vector>
#include "uint128.hpp"

@ -5,7 +5,8 @@
#include <functional>
#include "../util/meta/traits.hpp"
#include "../util/endian.hpp"
#include <oxenc/endian.h>
namespace llarp
{
@ -309,9 +310,9 @@ ntoh128(llarp::uint128_t i)
#ifdef __BIG_ENDIAN__
return i;
#else
const auto loSwapped = htobe64(i.lower);
const auto hiSwapped = htobe64(i.upper);
return {loSwapped, hiSwapped};
const auto loSwapped = oxenc::big_to_host(i.lower);
const auto hiSwapped = oxenc::big_to_host(i.upper);
return {/*upper=*/loSwapped, /*lower=*/hiSwapped};
#endif
}

@ -14,9 +14,10 @@
#include <llarp/routing/path_latency_message.hpp>
#include <llarp/routing/transfer_traffic_message.hpp>
#include <llarp/util/buffer.hpp>
#include <llarp/util/endian.hpp>
#include <llarp/tooling/path_event.hpp>
#include <oxenc/endian.h>
#include <deque>
#include <queue>
@ -894,7 +895,7 @@ namespace llarp
{
if (pkt.size() <= 8)
return false;
uint64_t counter = bufbe64toh(pkt.data());
auto counter = oxenc::load_big_to_host<uint64_t>(pkt.data());
if (m_ExitTrafficHandler(
self, llarp_buffer_t(pkt.data() + 8, pkt.size() - 8), counter, msg.protocol))
{

@ -14,7 +14,8 @@
#include <llarp/routing/path_transfer_message.hpp>
#include <llarp/routing/handler.hpp>
#include <llarp/util/buffer.hpp>
#include <llarp/util/endian.hpp>
#include <oxenc/endian.h>
namespace llarp
{
@ -396,7 +397,7 @@ namespace llarp
// check short packet buffer
if (pkt.size() <= 8)
continue;
uint64_t counter = bufbe64toh(pkt.data());
auto counter = oxenc::load_big_to_host<uint64_t>(pkt.data());
sent &= endpoint->QueueOutboundTraffic(
info.rxID,
ManagedBuffer(llarp_buffer_t(pkt.data() + 8, pkt.size() - 8)),

@ -2,7 +2,8 @@
#include "handler.hpp"
#include <llarp/util/bencode.hpp>
#include <llarp/util/endian.hpp>
#include <oxenc/endian.h>
namespace llarp
{
@ -15,7 +16,7 @@ namespace llarp
return false;
X.emplace_back(buf.sz + 8);
byte_t* ptr = X.back().data();
htobe64buf(ptr, counter);
oxenc::write_host_as_big(counter, ptr);
ptr += 8;
memcpy(ptr, buf.base, buf.sz);
// 8 bytes encoding overhead and 8 bytes counter

@ -1,5 +1,5 @@
#include "buffer.hpp"
#include "endian.hpp"
#include <oxenc/endian.h>
#include <cstdarg>
#include <cstdio>
@ -33,64 +33,66 @@ llarp_buffer_t::writef(const char* fmt, ...)
return true;
}
namespace
{
template <typename UInt>
bool
put(llarp_buffer_t& buf, UInt i)
{
if (buf.size_left() < sizeof(UInt))
return false;
oxenc::write_host_as_big(i, buf.cur);
buf.cur += sizeof(UInt);
return true;
}
template <typename UInt>
bool
read(llarp_buffer_t& buf, UInt& i)
{
if (buf.size_left() < sizeof(UInt))
return false;
i = oxenc::load_big_to_host<UInt>(buf.cur);
buf.cur += sizeof(UInt);
return true;
}
} // namespace
bool
llarp_buffer_t::put_uint16(uint16_t i)
{
if (size_left() < sizeof(uint16_t))
return false;
htobe16buf(cur, i);
cur += sizeof(uint16_t);
return true;
return put(*this, i);
}
bool
llarp_buffer_t::put_uint64(uint64_t i)
{
if (size_left() < sizeof(uint64_t))
return false;
htobe64buf(cur, i);
cur += sizeof(uint64_t);
return true;
return put(*this, i);
}
bool
llarp_buffer_t::put_uint32(uint32_t i)
{
if (size_left() < sizeof(uint32_t))
return false;
htobe32buf(cur, i);
cur += sizeof(uint32_t);
return true;
return put(*this, i);
}
bool
llarp_buffer_t::read_uint16(uint16_t& i)
{
if (size_left() < sizeof(uint16_t))
return false;
i = bufbe16toh(cur);
cur += sizeof(uint16_t);
return true;
return read(*this, i);
}
bool
llarp_buffer_t::read_uint32(uint32_t& i)
{
if (size_left() < sizeof(uint32_t))
return false;
i = bufbe32toh(cur);
cur += sizeof(uint32_t);
return true;
return read(*this, i);
}
bool
llarp_buffer_t::read_uint64(uint64_t& i)
{
if (size_left() < sizeof(uint64_t))
return false;
i = bufbe64toh(cur);
cur += sizeof(uint64_t);
return true;
return read(*this, i);
}
size_t

@ -1,203 +0,0 @@
#pragma once
// adapted from libi2pd
#include <cinttypes>
#include <cstring>
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
#include <sys/endian.h>
#elif defined(__sun)
#include <sys/byteorder.h>
#include <endian.h>
#define htobe16(x) htons(x)
#define htole16(x) (x)
#define be16toh(x) ntohs(x)
#define le16toh(x) (x)
#define htobe32(x) htonl(x)
#define htole32(x) (x)
#define be32toh(x) ntohl(x)
#define le32toh(x) (x)
#define htobe64(x) \
(((uint64_t)htonl(((uint32_t)(((uint64_t)(x)) >> 32)))) \
| (((uint64_t)htonl(((uint32_t)(x)))) << 32))
#define htole64(x) (x)
#define be64toh(x) \
(((uint64_t)ntohl(((uint32_t)(((uint64_t)(x)) >> 32)))) \
| (((uint64_t)ntohl(((uint32_t)(x)))) << 32))
#define le64toh(x) (x)
#elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__GLIBC__)
#include <endian.h>
#elif defined(__APPLE__) && defined(__MACH__)
#include <libkern/OSByteOrder.h>
#define htobe16(x) OSSwapHostToBigInt16(x)
#define htole16(x) OSSwapHostToLittleInt16(x)
#define be16toh(x) OSSwapBigToHostInt16(x)
#define le16toh(x) OSSwapLittleToHostInt16(x)
#define htobe32(x) OSSwapHostToBigInt32(x)
#define htole32(x) OSSwapHostToLittleInt32(x)
#define be32toh(x) OSSwapBigToHostInt32(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)
#define htobe64(x) OSSwapHostToBigInt64(x)
#define htole64(x) OSSwapHostToLittleInt64(x)
#define be64toh(x) OSSwapBigToHostInt64(x)
#define le64toh(x) OSSwapLittleToHostInt64(x)
#elif defined(_WIN32)
#include <winsock2.h>
#ifndef __LITTLE_ENDIAN__
#define __LITTLE_ENDIAN__
#endif
#define htobe16(x) htons(x)
#define htole16(x) (x)
#define be16toh(x) ntohs(x)
#define le16toh(x) (x)
#define htobe32(x) htonl(x)
#define htole32(x) (x)
#define be32toh(x) ntohl(x)
#define le32toh(x) (x)
#define htobe64(x) \
(((uint64_t)htonl(((uint32_t)(((uint64_t)(x)) >> 32)))) \
| (((uint64_t)htonl(((uint32_t)(x)))) << 32))
#define htole64(x) (x)
#define be64toh(x) \
(((uint64_t)ntohl(((uint32_t)(((uint64_t)(x)) >> 32)))) \
| (((uint64_t)ntohl(((uint32_t)(x)))) << 32))
#define le64toh(x) (x)
#else
#define NEEDS_LOCAL_ENDIAN
#include <cstdint>
uint16_t
htobe16(uint16_t int16);
uint32_t
htobe32(uint32_t int32);
uint64_t
htobe64(uint64_t int64);
uint16_t
be16toh(uint16_t big16);
uint32_t
be32toh(uint32_t big32);
uint64_t
be64toh(uint64_t big64);
// assume LittleEndine
#define htole16
#define htole32
#define htole64
#define le16toh
#define le32toh
#define le64toh
#endif
#if !defined(__LITTLE_ENDIAN__) && defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) \
&& __BYTE_ORDER == __LITTLE_ENDIAN
#define __LITTLE_ENDIAN__
#elif !defined(__BIG_ENDIAN__) && defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) \
&& __BYTE_ORDER == __BIG_ENDIAN
#define __BIG_ENDIAN__
#elif !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
#error "Error: don't know which endian this is"
#endif
inline uint16_t
buf16toh(const void* buf)
{
uint16_t b16;
memcpy(&b16, buf, sizeof(uint16_t));
return b16;
}
inline uint32_t
buf32toh(const void* buf)
{
uint32_t b32;
memcpy(&b32, buf, sizeof(uint32_t));
return b32;
}
inline uint64_t
buf64toh(const void* buf)
{
uint64_t b64;
memcpy(&b64, buf, sizeof(uint64_t));
return b64;
}
inline uint16_t
bufbe16toh(const void* buf)
{
return be16toh(buf16toh(buf));
}
inline uint32_t
bufbe32toh(const void* buf)
{
return be32toh(buf32toh(buf));
}
inline uint64_t
bufbe64toh(const void* buf)
{
return be64toh(buf64toh(buf));
}
inline void
htobuf16(void* buf, uint16_t b16)
{
memcpy(buf, &b16, sizeof(uint16_t));
}
inline void
htobuf32(void* buf, uint32_t b32)
{
memcpy(buf, &b32, sizeof(uint32_t));
}
inline void
htobuf64(void* buf, uint64_t b64)
{
memcpy(buf, &b64, sizeof(uint64_t));
}
inline void
htobe16buf(void* buf, uint16_t big16)
{
htobuf16(buf, htobe16(big16));
}
inline void
htobe32buf(void* buf, uint32_t big32)
{
htobuf32(buf, htobe32(big32));
}
inline void
htobe64buf(void* buf, uint64_t big64)
{
htobuf64(buf, htobe64(big64));
}
inline void
htole16buf(void* buf, uint16_t big16)
{
htobuf16(buf, htole16(big16));
}
inline void
htole32buf(void* buf, uint32_t big32)
{
htobuf32(buf, htole32(big32));
}
inline void
htole64buf(void* buf, uint64_t big64)
{
htobuf64(buf, htole64(big64));
}

@ -9,7 +9,7 @@
#include <net/if.h>
#include <linux/if_tun.h>
#include <string.h>
#include <cstring>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
@ -19,6 +19,8 @@
#include <llarp/util/str.hpp>
#include <exception>
#include <oxenc/endian.h>
namespace llarp::vpn
{
struct in6_ifreq
@ -181,7 +183,7 @@ namespace llarp::vpn
{
family = AF_INET;
bitlen = bits;
htobe32buf(data, addr.h);
oxenc::write_host_as_big(addr.h, data);
}
_inet_addr(huint128_t addr, size_t bits = 128)

Loading…
Cancel
Save