From 40654139771ea8c1d984d62db70e5496dd2daa26 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Sat, 10 Sep 2022 14:44:55 -0300 Subject: [PATCH] Simplify/fix ip_header layout ip_header wasn't 20 bytes on windows compilations for some unholy reason. This restructures it to avoid the template and just use two different structs for le/be with a condition_t for the ifdef, which resolves it (and *also* apparently avoids the need for the pack). Also add a static_assert to check the size. Also do the same for ipv6. --- llarp/net/ip_packet.cpp | 16 ---------------- llarp/net/ip_packet.hpp | 32 ++++++++++++++++++-------------- 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/llarp/net/ip_packet.cpp b/llarp/net/ip_packet.cpp index 3bda060d0..c9bed51e9 100644 --- a/llarp/net/ip_packet.cpp +++ b/llarp/net/ip_packet.cpp @@ -17,22 +17,6 @@ namespace llarp::net { constexpr uint32_t ipv6_flowlabel_mask = 0b0000'0000'0000'1111'1111'1111'1111'1111; - template - struct ipv6_header_preamble - { - unsigned char pad_small : 4; - unsigned char version : 4; - uint8_t pad[3]; - }; - - template <> - struct ipv6_header_preamble - { - unsigned char version : 4; - unsigned char pad_small : 4; - uint8_t pad[3]; - }; - /// get 20 bit truncated flow label in network order llarp::nuint32_t ipv6_header::FlowLabel() const diff --git a/llarp/net/ip_packet.hpp b/llarp/net/ip_packet.hpp index 5d297ff92..3a1ed2d74 100644 --- a/llarp/net/ip_packet.hpp +++ b/llarp/net/ip_packet.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include "net.hpp" #include @@ -10,12 +11,10 @@ namespace llarp::net { -#pragma pack(push, 1) - template struct ip_header_le { - unsigned int ihl : 4; - unsigned int version : 4; + uint8_t ihl : 4; + uint8_t version : 4; uint8_t tos; uint16_t tot_len; uint16_t id; @@ -27,11 +26,10 @@ namespace llarp::net uint32_t daddr; }; - template <> - struct ip_header_le + struct ip_header_be { - unsigned int version : 4; - unsigned int ihl : 4; + uint8_t version : 4; + uint8_t ihl : 4; uint8_t tos; uint16_t tot_len; uint16_t id; @@ -42,11 +40,11 @@ namespace llarp::net uint32_t saddr; uint32_t daddr; }; -#pragma pack(pop) - using ip_header = ip_header_le; + using ip_header = std::conditional_t; + + static_assert(sizeof(ip_header) == 20); - template struct ipv6_header_preamble_le { unsigned char pad_small : 4; @@ -54,19 +52,23 @@ namespace llarp::net uint8_t pad[3]; }; - template <> - struct ipv6_header_preamble_le + struct ipv6_header_preamble_be { unsigned char version : 4; unsigned char pad_small : 4; uint8_t pad[3]; }; + using ipv6_header_preamble = + std::conditional_t; + + static_assert(sizeof(ipv6_header_preamble) == 4); + struct ipv6_header { union { - ipv6_header_preamble_le preamble; + ipv6_header_preamble preamble; uint32_t flowlabel; } preamble; @@ -83,6 +85,8 @@ namespace llarp::net FlowLabel(llarp::nuint32_t label); }; + static_assert(sizeof(ipv6_header) == 40); + /// "well known" ip protocols /// TODO: extend this to non "well known values" enum class IPProtocol : uint8_t