From 41c785d77affca79c7d89cd7ecd7d00ab219cc41 Mon Sep 17 00:00:00 2001 From: Simon Goldschmidt Date: Tue, 26 Jul 2011 20:55:32 +0200 Subject: [PATCH] IPv4: splitted IPv4 header fields version/len and tos, made macros depend on BYTE_ORDER to prevent unnecessary calls to htons() --- src/core/ipv4/ip4.c | 5 +++-- src/include/ipv4/lwip/ip4.h | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/core/ipv4/ip4.c b/src/core/ipv4/ip4.c index 0f7960e3..8cbaa23f 100644 --- a/src/core/ipv4/ip4.c +++ b/src/core/ipv4/ip4.c @@ -705,9 +705,10 @@ err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16; #endif /* CHECKSUM_GEN_IP_INLINE */ - IPH_VHLTOS_SET(iphdr, 4, ip_hlen / 4, tos); + IPH_VHL_SET(iphdr, 4, ip_hlen / 4); + IPH_TOS_SET(iphdr, tos); #if CHECKSUM_GEN_IP_INLINE - chk_sum += iphdr->_v_hl_tos; + chk_sum += LWIP_MAKE_U16(tos, iphdr->_v_hl); #endif /* CHECKSUM_GEN_IP_INLINE */ IPH_LEN_SET(iphdr, htons(p->tot_len)); #if CHECKSUM_GEN_IP_INLINE diff --git a/src/include/ipv4/lwip/ip4.h b/src/include/ipv4/lwip/ip4.h index 0d3c70b6..64c9b4d7 100644 --- a/src/include/ipv4/lwip/ip4.h +++ b/src/include/ipv4/lwip/ip4.h @@ -62,8 +62,10 @@ extern "C" { #endif PACK_STRUCT_BEGIN struct ip_hdr { - /* version / header length / type of service */ - PACK_STRUCT_FIELD(u16_t _v_hl_tos); + /* version / header length */ + PACK_STRUCT_FIELD(u8_t _v_hl); + /* type of service */ + PACK_STRUCT_FIELD(u8_t _tos); /* total length */ PACK_STRUCT_FIELD(u16_t _len); /* identification */ @@ -89,9 +91,14 @@ PACK_STRUCT_END # include "arch/epstruct.h" #endif -#define IPH_V(hdr) (ntohs((hdr)->_v_hl_tos) >> 12) -#define IPH_HL(hdr) ((ntohs((hdr)->_v_hl_tos) >> 8) & 0x0f) -#define IPH_TOS(hdr) (ntohs((hdr)->_v_hl_tos) & 0xff) +#if BYTE_ORDER == LITTLE_ENDIAN +#define IPH_V(hdr) ((hdr)->_v_hl >> 4) +#define IPH_HL(hdr) ((hdr)->_v_hl & 0x0f) +#else /* BYTE_ORDER == LITTLE_ENDIAN */ +#define IPH_V(hdr) ((hdr)->_v_hl & 0x0f) +#define IPH_HL(hdr) ((hdr)->_v_hl >> 4) +#endif /* BYTE_ORDER == LITTLE_ENDIAN */ +#define IPH_TOS(hdr) ((hdr)->_tos) #define IPH_LEN(hdr) ((hdr)->_len) #define IPH_ID(hdr) ((hdr)->_id) #define IPH_OFFSET(hdr) ((hdr)->_offset) @@ -99,7 +106,12 @@ PACK_STRUCT_END #define IPH_PROTO(hdr) ((hdr)->_proto) #define IPH_CHKSUM(hdr) ((hdr)->_chksum) -#define IPH_VHLTOS_SET(hdr, v, hl, tos) (hdr)->_v_hl_tos = (htons(((v) << 12) | ((hl) << 8) | (tos))) +#if BYTE_ORDER == LITTLE_ENDIAN +#define IPH_VHL_SET(hdr, v, hl) (hdr)->_v_hl = (((v) << 4) | (hl)) +#else /* BYTE_ORDER == LITTLE_ENDIAN */ +#define IPH_VHL_SET(hdr, v, hl) (hdr)->_v_hl = ((v) | ((hl) << 4)) +#endif /* BYTE_ORDER == LITTLE_ENDIAN */ +#define IPH_TOS_SET(hdr, tos) (hdr)->_tos = (tos) #define IPH_LEN_SET(hdr, len) (hdr)->_len = (len) #define IPH_ID_SET(hdr, id) (hdr)->_id = (id) #define IPH_OFFSET_SET(hdr, off) (hdr)->_offset = (off)