mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-05 07:38:20 +02:00
std-aux/glib-aux: minor cleanup of nm_ip_addr_*()/nm_utils_addr_family_*()
- let nm_ip_addr_is_null() work for unaligned addresses. - drop redundant nm_assert_addr_family(). - move code around.
This commit is contained in:
parent
26090bafc9
commit
c6559b04c2
2 changed files with 26 additions and 21 deletions
|
|
@ -247,7 +247,6 @@ extern const NMIPAddr nm_ip_addr_zero;
|
|||
static inline int
|
||||
nm_ip_addr_cmp(int addr_family, gconstpointer a, gconstpointer b)
|
||||
{
|
||||
nm_assert_addr_family(addr_family);
|
||||
nm_assert(a);
|
||||
nm_assert(b);
|
||||
|
||||
|
|
@ -264,20 +263,27 @@ static inline gboolean
|
|||
nm_ip_addr_is_null(int addr_family, gconstpointer addr)
|
||||
{
|
||||
nm_assert(addr);
|
||||
if (addr_family == AF_INET6)
|
||||
return IN6_IS_ADDR_UNSPECIFIED((const struct in6_addr *) addr);
|
||||
nm_assert(addr_family == AF_INET);
|
||||
return ((const struct in_addr *) addr)->s_addr == 0;
|
||||
|
||||
if (NM_IS_IPv4(addr_family)) {
|
||||
in_addr_t t;
|
||||
|
||||
/* also for in_addr_t type (AF_INET), we accept that the pointer might
|
||||
* be unaligned. */
|
||||
memcpy(&t, addr, sizeof(t));
|
||||
return t == 0;
|
||||
}
|
||||
|
||||
return IN6_IS_ADDR_UNSPECIFIED((const struct in6_addr *) addr);
|
||||
}
|
||||
|
||||
static inline void
|
||||
nm_ip_addr_set(int addr_family, gpointer dst, gconstpointer src)
|
||||
{
|
||||
nm_assert_addr_family(addr_family);
|
||||
nm_assert(dst);
|
||||
nm_assert(src);
|
||||
|
||||
memcpy(dst, src, NM_IS_IPv4(addr_family) ? sizeof(in_addr_t) : sizeof(struct in6_addr));
|
||||
/* this MUST use memcpy() (or similar means) to support unaligned src/dst pointers. */
|
||||
memcpy(dst, src, nm_utils_addr_family_to_size(addr_family));
|
||||
}
|
||||
|
||||
static inline NMIPAddr
|
||||
|
|
|
|||
|
|
@ -1060,6 +1060,18 @@ nm_utils_addr_family_to_char(int addr_family)
|
|||
return '?';
|
||||
}
|
||||
|
||||
#define nm_assert_addr_family(addr_family) \
|
||||
nm_assert(NM_IN_SET((addr_family), NM_AF_INET, NM_AF_INET6))
|
||||
|
||||
#define NM_IS_IPv4(addr_family) \
|
||||
({ \
|
||||
const int _addr_family = (addr_family); \
|
||||
\
|
||||
nm_assert_addr_family(_addr_family); \
|
||||
\
|
||||
(_addr_family == NM_AF_INET); \
|
||||
})
|
||||
|
||||
static inline size_t
|
||||
nm_utils_addr_family_to_size(int addr_family)
|
||||
{
|
||||
|
|
@ -1069,8 +1081,7 @@ nm_utils_addr_family_to_size(int addr_family)
|
|||
case NM_AF_INET6:
|
||||
return NM_AF_INET6_SIZE;
|
||||
}
|
||||
nm_assert_not_reached();
|
||||
return 0;
|
||||
return nm_assert_unreachable_val(0);
|
||||
}
|
||||
|
||||
static inline int
|
||||
|
|
@ -1085,16 +1096,4 @@ nm_utils_addr_family_from_size(size_t len)
|
|||
return NM_AF_UNSPEC;
|
||||
}
|
||||
|
||||
#define nm_assert_addr_family(addr_family) \
|
||||
nm_assert(NM_IN_SET((addr_family), NM_AF_INET, NM_AF_INET6))
|
||||
|
||||
#define NM_IS_IPv4(addr_family) \
|
||||
({ \
|
||||
const int _addr_family = (addr_family); \
|
||||
\
|
||||
nm_assert_addr_family(_addr_family); \
|
||||
\
|
||||
(_addr_family == NM_AF_INET); \
|
||||
})
|
||||
|
||||
#endif /* __NM_STD_AUX_H__ */
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue