From b09298e211aa92e9fb152e24284230bfdcb2f8eb Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Thu, 28 Apr 2022 12:09:51 -0300 Subject: [PATCH] Replace llarp/util/endian.hpp with oxenc/endian.h --- llarp/crypto/crypto_libsodium.cpp | 4 +- llarp/dns/message.cpp | 4 +- llarp/handlers/tun.cpp | 5 +- llarp/iwp/message_buffer.cpp | 11 +- llarp/iwp/session.cpp | 19 +- llarp/net/ip_packet.cpp | 11 +- llarp/net/ip_packet.hpp | 5 +- llarp/net/net_int.cpp | 4 +- llarp/net/net_int.hpp | 1 - llarp/net/uint128.hpp | 9 +- llarp/path/path.cpp | 5 +- llarp/path/transit_hop.cpp | 5 +- llarp/routing/transfer_traffic_message.cpp | 5 +- llarp/util/buffer.cpp | 64 +++---- llarp/util/endian.hpp | 203 --------------------- llarp/vpn/linux.hpp | 6 +- 16 files changed, 84 insertions(+), 277 deletions(-) delete mode 100644 llarp/util/endian.hpp diff --git a/llarp/crypto/crypto_libsodium.cpp b/llarp/crypto/crypto_libsodium.cpp index 60f2e5513..a5671a151 100644 --- a/llarp/crypto/crypto_libsodium.cpp +++ b/llarp/crypto/crypto_libsodium.cpp @@ -8,8 +8,8 @@ #include #include #include +#include #include -#include #include #include #include @@ -295,7 +295,7 @@ namespace llarp std::array 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; diff --git a/llarp/dns/message.cpp b/llarp/dns/message.cpp index 0d78d1fdb..65b30c78d 100644 --- a/llarp/dns/message.cpp +++ b/llarp/dns/message.cpp @@ -1,9 +1,9 @@ #include "message.hpp" +#include #include "dns.hpp" #include "srv_data.hpp" #include -#include #include #include #include @@ -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)); } diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index d4d76c647..c31a300e6 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -22,12 +22,11 @@ #include #include #include - #include -#include - #include + #include + namespace llarp { namespace handlers diff --git a/llarp/iwp/message_buffer.cpp b/llarp/iwp/message_buffer.cpp index 495fb076b..e5098f656 100644 --- a/llarp/iwp/message_buffer.cpp +++ b/llarp/iwp/message_buffer.cpp @@ -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(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; } diff --git a/llarp/iwp/session.cpp b/llarp/iwp/session.cpp index 0607ff1eb..fcd4097d3 100644 --- a/llarp/iwp/session.cpp +++ b/llarp/iwp/session.cpp @@ -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(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(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(pos); pos += sizeof(sz); - uint64_t rxid = bufbe64toh(pos); + auto rxid = oxenc::load_big_to_host(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(data.data() + CommandOverhead + PacketOverhead); + auto rxid = oxenc::load_big_to_host( + 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(data.data() + 2 + PacketOverhead); auto itr = m_TXMsgs.find(txid); if (itr == m_TXMsgs.end()) { diff --git a/llarp/net/ip_packet.cpp b/llarp/net/ip_packet.cpp index deef93102..5e287b6cd 100644 --- a/llarp/net/ip_packet.cpp +++ b/llarp/net/ip_packet.cpp @@ -2,13 +2,14 @@ #include "ip.hpp" #include -#include #include #include #ifndef _WIN32 #include #endif +#include + #include #include @@ -564,11 +565,11 @@ namespace llarp // code 'Destination host unknown error' *itr++ = 7; // checksum + unused - htobe32buf(itr, 0); + oxenc::write_host_as_big(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(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(buf.sz + 8)); + oxenc::write_host_as_big(static_cast(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); diff --git a/llarp/net/ip_packet.hpp b/llarp/net/ip_packet.hpp index 0987633aa..b9e3c4ddd 100644 --- a/llarp/net/ip_packet.hpp +++ b/llarp/net/ip_packet.hpp @@ -4,6 +4,7 @@ #include "net.hpp" #include #include +#include // 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 " #endif #if defined(__linux__) diff --git a/llarp/net/net_int.cpp b/llarp/net/net_int.cpp index 973d6ed4e..566e11b70 100644 --- a/llarp/net/net_int.cpp +++ b/llarp/net/net_int.cpp @@ -2,6 +2,8 @@ #include "ip.hpp" #include +#include + 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; } diff --git a/llarp/net/net_int.hpp b/llarp/net/net_int.hpp index 616e912d8..b5ec0e7cb 100644 --- a/llarp/net/net_int.hpp +++ b/llarp/net/net_int.hpp @@ -15,7 +15,6 @@ #include // for itoa #include -#include #include #include "uint128.hpp" diff --git a/llarp/net/uint128.hpp b/llarp/net/uint128.hpp index aa1c96d08..8340000b2 100644 --- a/llarp/net/uint128.hpp +++ b/llarp/net/uint128.hpp @@ -5,7 +5,8 @@ #include #include "../util/meta/traits.hpp" -#include "../util/endian.hpp" + +#include 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 } diff --git a/llarp/path/path.cpp b/llarp/path/path.cpp index e031b845b..c61d9098b 100644 --- a/llarp/path/path.cpp +++ b/llarp/path/path.cpp @@ -14,9 +14,10 @@ #include #include #include -#include #include +#include + #include #include @@ -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(pkt.data()); if (m_ExitTrafficHandler( self, llarp_buffer_t(pkt.data() + 8, pkt.size() - 8), counter, msg.protocol)) { diff --git a/llarp/path/transit_hop.cpp b/llarp/path/transit_hop.cpp index f2cd2d1aa..e52251996 100644 --- a/llarp/path/transit_hop.cpp +++ b/llarp/path/transit_hop.cpp @@ -14,7 +14,8 @@ #include #include #include -#include + +#include 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(pkt.data()); sent &= endpoint->QueueOutboundTraffic( info.rxID, ManagedBuffer(llarp_buffer_t(pkt.data() + 8, pkt.size() - 8)), diff --git a/llarp/routing/transfer_traffic_message.cpp b/llarp/routing/transfer_traffic_message.cpp index b3b66e207..b1a7beeba 100644 --- a/llarp/routing/transfer_traffic_message.cpp +++ b/llarp/routing/transfer_traffic_message.cpp @@ -2,7 +2,8 @@ #include "handler.hpp" #include -#include + +#include 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 diff --git a/llarp/util/buffer.cpp b/llarp/util/buffer.cpp index 2967024eb..f185e6347 100644 --- a/llarp/util/buffer.cpp +++ b/llarp/util/buffer.cpp @@ -1,5 +1,5 @@ #include "buffer.hpp" -#include "endian.hpp" +#include #include #include @@ -33,64 +33,66 @@ llarp_buffer_t::writef(const char* fmt, ...) return true; } +namespace +{ + template + 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 + bool + read(llarp_buffer_t& buf, UInt& i) + { + if (buf.size_left() < sizeof(UInt)) + return false; + i = oxenc::load_big_to_host(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 diff --git a/llarp/util/endian.hpp b/llarp/util/endian.hpp deleted file mode 100644 index eb2b534c7..000000000 --- a/llarp/util/endian.hpp +++ /dev/null @@ -1,203 +0,0 @@ -#pragma once - -// adapted from libi2pd - -#include -#include - -#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) -#include -#elif defined(__sun) -#include -#include -#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 -#elif defined(__APPLE__) && defined(__MACH__) -#include -#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 -#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 -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)); -} diff --git a/llarp/vpn/linux.hpp b/llarp/vpn/linux.hpp index 9f9ffbe0f..cad48fe91 100644 --- a/llarp/vpn/linux.hpp +++ b/llarp/vpn/linux.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include #include #include @@ -19,6 +19,8 @@ #include #include +#include + 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)