aboutgitcodebugslistschat
path: root/util.h
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2023-01-06 11:43:12 +1100
committerStefano Brivio <sbrivio@redhat.com>2023-01-23 18:54:42 +0100
commitf5a950115be5aca6d19ea183247bc36088c472c9 (patch)
tree7e0add34ee2462c58da9d7db588aac427ca8d0cf /util.h
parent6d011c1faab0d149bb1cbd0e922f26746e50cae6 (diff)
downloadpasst-f5a950115be5aca6d19ea183247bc36088c472c9.tar
passt-f5a950115be5aca6d19ea183247bc36088c472c9.tar.gz
passt-f5a950115be5aca6d19ea183247bc36088c472c9.tar.bz2
passt-f5a950115be5aca6d19ea183247bc36088c472c9.tar.lz
passt-f5a950115be5aca6d19ea183247bc36088c472c9.tar.xz
passt-f5a950115be5aca6d19ea183247bc36088c472c9.tar.zst
passt-f5a950115be5aca6d19ea183247bc36088c472c9.zip
util: Introduce hton*_constant() in place of #ifdefs
We have several places where we have fairly ugly #ifdefs on __BYTE_ORDER where we need network order values in a constant expression (so we can't use htons() or htonl()). We can do this more cleanly by using a single __BYTE_ORDER ifdef to define htons_constant() and htonl_constant() macros, then using those in all the other places. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'util.h')
-rw-r--r--util.h37
1 files changed, 11 insertions, 26 deletions
diff --git a/util.h b/util.h
index 3baec91..b4c139d 100644
--- a/util.h
+++ b/util.h
@@ -69,6 +69,14 @@
#define MAC_ZERO ((uint8_t [ETH_ALEN]){ 0 })
#define MAC_IS_ZERO(addr) (!memcmp((addr), MAC_ZERO, ETH_ALEN))
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define htons_constant(x) (x)
+#define htonl_constant(x) (x)
+#else
+#define htons_constant(x) (__bswap_constant_16(x))
+#define htonl_constant(x) (__bswap_constant_32(x))
+#endif
+
#define IN4_IS_ADDR_UNSPECIFIED(a) \
((a)->s_addr == htonl(INADDR_ANY))
#define IN4_IS_ADDR_BROADCAST(a) \
@@ -79,13 +87,8 @@
(IN_MULTICAST(ntohl((a)->s_addr)))
#define IN4_ARE_ADDR_EQUAL(a, b) \
(((struct in_addr *)(a))->s_addr == ((struct in_addr *)b)->s_addr)
-#if __BYTE_ORDER == __BIG_ENDIAN
-#define IN4ADDR_LOOPBACK_INIT \
- { .s_addr = INADDR_LOOPBACK }
-#else
#define IN4ADDR_LOOPBACK_INIT \
- { .s_addr = __bswap_constant_32(INADDR_LOOPBACK) }
-#endif
+ { .s_addr = htonl_constant(INADDR_LOOPBACK) }
#define NS_FN_STACK_SIZE (RLIMIT_STACK_VAL * 1024 / 8)
int do_clone(int (*fn)(void *), char *stack_area, size_t stack_size, int flags,
@@ -99,37 +102,19 @@ int do_clone(int (*fn)(void *), char *stack_area, size_t stack_size, int flags,
(void *)(arg)); \
} while (0)
-#if __BYTE_ORDER == __BIG_ENDIAN
-#define L2_BUF_ETH_IP4_INIT \
- { \
- .h_dest = { 0 }, \
- .h_source = { 0 }, \
- .h_proto = ETH_P_IP, \
- }
-#else
#define L2_BUF_ETH_IP4_INIT \
{ \
.h_dest = { 0 }, \
.h_source = { 0 }, \
- .h_proto = __bswap_constant_16(ETH_P_IP), \
+ .h_proto = htons_constant(ETH_P_IP), \
}
-#endif
-#if __BYTE_ORDER == __BIG_ENDIAN
-#define L2_BUF_ETH_IP6_INIT \
- { \
- .h_dest = { 0 }, \
- .h_source = { 0 }, \
- .h_proto = ETH_P_IPV6, \
- }
-#else
#define L2_BUF_ETH_IP6_INIT \
{ \
.h_dest = { 0 }, \
.h_source = { 0 }, \
- .h_proto = __bswap_constant_16(ETH_P_IPV6), \
+ .h_proto = htons_constant(ETH_P_IPV6), \
}
-#endif
#define L2_BUF_IP4_INIT(proto) \
{ \