mirror of
https://passt.top/passt
synced 2025-06-12 02:25:34 +02:00
Use typing to reduce chances of IPv4 endianness errors
We recently corrected some errors handling the endianness of IPv4 addresses. These are very easy errors to make since although we mostly store them in network endianness, we sometimes need to manipulate them in host endianness. To reduce the chances of making such mistakes again, change to always using a (struct in_addr) instead of a bare in_addr_t or uint32_t to store network endian addresses. This makes it harder to accidentally do arithmetic or comparisons on such addresses as if they were host endian. We introduce a number of IN4_IS_ADDR_*() helpers to make it easier to directly work with struct in_addr values. This has the additional benefit of making the IPv4 and IPv6 paths more visually similar. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
parent
dd3470d9a9
commit
7c7b68dbe0
14 changed files with 113 additions and 100 deletions
12
util.h
12
util.h
|
@ -69,8 +69,16 @@
|
|||
#define MAC_ZERO ((uint8_t [ETH_ALEN]){ 0 })
|
||||
#define MAC_IS_ZERO(addr) (!memcmp((addr), MAC_ZERO, ETH_ALEN))
|
||||
|
||||
#define IPV4_IS_LOOPBACK(addr) \
|
||||
((addr) >> IN_CLASSA_NSHIFT == IN_LOOPBACKNET)
|
||||
#define IN4_IS_ADDR_UNSPECIFIED(a) \
|
||||
((a)->s_addr == htonl(INADDR_ANY))
|
||||
#define IN4_IS_ADDR_BROADCAST(a) \
|
||||
((a)->s_addr == htonl(INADDR_BROADCAST))
|
||||
#define IN4_IS_ADDR_LOOPBACK(a) \
|
||||
(ntohl((a)->s_addr) >> IN_CLASSA_NSHIFT == IN_LOOPBACKNET)
|
||||
#define IN4_IS_ADDR_MULTICAST(a) \
|
||||
(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)
|
||||
|
||||
#define NS_FN_STACK_SIZE (RLIMIT_STACK_VAL * 1024 / 8)
|
||||
#define NS_CALL(fn, arg) \
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue