Cleanup byte order handling a bit.

- Create LWIP_MAKEU32(a,b,c,d) to create an U32 value from bytes
- Use PP_HTONL() in some macros to emphasize network byte order conversion
This commit is contained in:
Dirk Ziegelmeier 2016-12-07 20:18:58 +01:00
parent ff3656f4f5
commit 0ca82df062
4 changed files with 12 additions and 35 deletions

View File

@ -57,6 +57,12 @@ extern "C" {
/* Get the number of entries in an array ('x' must NOT be a pointer!) */
#define LWIP_ARRAYSIZE(x) (sizeof(x)/sizeof((x)[0]))
/** Create u32_t value from bytes */
#define LWIP_MAKEU32(a,b,c,d) (((u32_t)((a) & 0xff) << 24) | \
((u32_t)((b) & 0xff) << 16) | \
((u32_t)((c) & 0xff) << 8) | \
(u32_t)((d) & 0xff))
#ifndef NULL
#ifdef __cplusplus
#define NULL 0

View File

@ -116,24 +116,8 @@ struct netif;
#define IP_LOOPBACKNET 127 /* official! */
#if BYTE_ORDER == BIG_ENDIAN
/** Convert IP address given by the four byte-parts to an u32_t. */
#define IP4_ADDR_MAKEU32(a,b,c,d) (((u32_t)((a) & 0xff) << 24) | \
((u32_t)((b) & 0xff) << 16) | \
((u32_t)((c) & 0xff) << 8) | \
(u32_t)((d) & 0xff))
#else
/** Convert IP address given by the four byte-parts to an u32_t.
Little-endian version that prevents the use of lwip_htonl. */
#define IP4_ADDR_MAKEU32(a,b,c,d) (((u32_t)((d) & 0xff) << 24) | \
((u32_t)((c) & 0xff) << 16) | \
((u32_t)((b) & 0xff) << 8) | \
(u32_t)((a) & 0xff))
#endif
/** Set an IP address given by the four byte-parts */
#define IP4_ADDR(ipaddr, a,b,c,d) (ipaddr)->addr = IP4_ADDR_MAKEU32(a,b,c,d)
#define IP4_ADDR(ipaddr, a,b,c,d) (ipaddr)->addr = PP_HTONL(LWIP_MAKEU32(a,b,c,d))
/** MEMCPY-like copying of IP addresses where addresses are known to be
* 16-bit-aligned if the port is correctly configured (so a port could define

View File

@ -43,6 +43,7 @@
#define LWIP_HDR_IP6_ADDR_H
#include "lwip/opt.h"
#include "def.h"
#if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */
@ -61,23 +62,9 @@ struct ip6_addr {
/** IPv6 address */
typedef struct ip6_addr ip6_addr_t;
#if BYTE_ORDER == BIG_ENDIAN
/** Set an IPv6 partial address given by byte-parts. */
/** Set an IPv6 partial address given by byte-parts */
#define IP6_ADDR_PART(ip6addr, index, a,b,c,d) \
(ip6addr)->addr[index] = ((u32_t)((a) & 0xff) << 24) | \
((u32_t)((b) & 0xff) << 16) | \
((u32_t)((c) & 0xff) << 8) | \
(u32_t)((d) & 0xff)
#else
/** Set an IPv6 partial address given by byte-parts.
Little-endian version, stored in network order (no lwip_htonl). */
#define IP6_ADDR_PART(ip6addr, index, a,b,c,d) \
(ip6addr)->addr[index] = ((u32_t)((d) & 0xff) << 24) | \
((u32_t)((c) & 0xff) << 16) | \
((u32_t)((b) & 0xff) << 8) | \
(u32_t)((a) & 0xff)
#endif
(ip6addr)->addr[index] = PP_HTONL(LWIP_MAKEU32(a,b,c,d))
/** Set a full IPv6 address by passing the 4 u32_t indices in network byte order
(use PP_HTONL() for constants) */

View File

@ -80,7 +80,7 @@ extern const ip_addr_t ip_addr_any_type;
/** @ingroup ip4addr */
#define IPADDR4_INIT(u32val) { { { { u32val, 0ul, 0ul, 0ul } } }, IPADDR_TYPE_V4 }
/** @ingroup ip4addr */
#define IPADDR4_INIT_BYTES(a,b,c,d) IPADDR4_INIT(IP4_ADDR_MAKEU32(a,b,c,d))
#define IPADDR4_INIT_BYTES(a,b,c,d) IPADDR4_INIT(PP_HTONL(LWIP_MAKEU32(a,b,c,d)))
/** @ingroup ip6addr */
#define IPADDR6_INIT(a, b, c, d) { { { { a, b, c, d } } }, IPADDR_TYPE_V6 }
/** @ingroup ip6addr */
@ -241,7 +241,7 @@ int ipaddr_aton(const char *cp, ip_addr_t *addr);
typedef ip4_addr_t ip_addr_t;
#define IPADDR4_INIT(u32val) { u32val }
#define IPADDR4_INIT_BYTES(a,b,c,d) IPADDR4_INIT(IP4_ADDR_MAKEU32(a,b,c,d))
#define IPADDR4_INIT_BYTES(a,b,c,d) IPADDR4_INIT(PP_HTONL(LWIP_MAKEU32(a,b,c,d)))
#define IP_IS_V4_VAL(ipaddr) 1
#define IP_IS_V6_VAL(ipaddr) 0
#define IP_IS_V4(ipaddr) 1