From 56a0aa06ac005fa991cfdbe5866857fbb9c865d3 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 22 Jul 2020 11:38:14 +0200 Subject: [PATCH] shared: move addr-family helpers to "nm-std-aux.h" Handling address families is something we do all over the place. Move some simple helper code to "nm-std-aux.h". --- shared/nm-glib-aux/nm-shared-utils.h | 36 --------------- .../nm-glib-aux/tests/test-shared-general.c | 10 +++++ shared/nm-std-aux/nm-std-aux.h | 45 +++++++++++++++++++ 3 files changed, 55 insertions(+), 36 deletions(-) diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h index 0f7c36e609..889b5f517a 100644 --- a/shared/nm-glib-aux/nm-shared-utils.h +++ b/shared/nm-glib-aux/nm-shared-utils.h @@ -68,42 +68,6 @@ G_STATIC_ASSERT (sizeof (int) == sizeof (gint32)); /*****************************************************************************/ -static inline char -nm_utils_addr_family_to_char (int addr_family) -{ - switch (addr_family) { - case AF_UNSPEC: return 'X'; - case AF_INET: return '4'; - case AF_INET6: return '6'; - } - g_return_val_if_reached ('?'); -} - -static inline gsize -nm_utils_addr_family_to_size (int addr_family) -{ - switch (addr_family) { - case AF_INET: return sizeof (in_addr_t); - case AF_INET6: return sizeof (struct in6_addr); - } - g_return_val_if_reached (0); -} - -static inline int -nm_utils_addr_family_from_size (gsize len) -{ - switch (len) { - case sizeof (in_addr_t): return AF_INET; - case sizeof (struct in6_addr): return AF_INET6; - } - return AF_UNSPEC; -} - -#define nm_assert_addr_family(addr_family) \ - nm_assert (NM_IN_SET ((addr_family), AF_INET, AF_INET6)) - -/*****************************************************************************/ - typedef struct { union { guint8 addr_ptr[1]; diff --git a/shared/nm-glib-aux/tests/test-shared-general.c b/shared/nm-glib-aux/tests/test-shared-general.c index f389770a7a..02b2f4e1ea 100644 --- a/shared/nm-glib-aux/tests/test-shared-general.c +++ b/shared/nm-glib-aux/tests/test-shared-general.c @@ -17,6 +17,16 @@ /*****************************************************************************/ +G_STATIC_ASSERT (NM_AF_UNSPEC == AF_UNSPEC); +G_STATIC_ASSERT (NM_AF_INET == AF_INET); +G_STATIC_ASSERT (NM_AF_INET6 == AF_INET6); + +G_STATIC_ASSERT (NM_AF_INET_SIZE == sizeof (in_addr_t)); +G_STATIC_ASSERT (NM_AF_INET_SIZE == sizeof (struct in_addr)); +G_STATIC_ASSERT (NM_AF_INET6_SIZE == sizeof (struct in6_addr)); + +/*****************************************************************************/ + static void test_gpid (void) { diff --git a/shared/nm-std-aux/nm-std-aux.h b/shared/nm-std-aux/nm-std-aux.h index 3262ef4c9d..ea43c96965 100644 --- a/shared/nm-std-aux/nm-std-aux.h +++ b/shared/nm-std-aux/nm-std-aux.h @@ -622,4 +622,49 @@ nm_steal_fd (int *p_fd) return -1; } +/*****************************************************************************/ + +#define NM_AF_UNSPEC 0 /* AF_UNSPEC */ +#define NM_AF_INET 2 /* AF_INET */ +#define NM_AF_INET6 10 /* AF_INET6 */ + +#define NM_AF_INET_SIZE 4 /* sizeof (in_addr_t) */ +#define NM_AF_INET6_SIZE 16 /* sizeof (stuct in6_addr) */ + +static inline char +nm_utils_addr_family_to_char (int addr_family) +{ + switch (addr_family) { + case NM_AF_UNSPEC: return 'X'; + case NM_AF_INET: return '4'; + case NM_AF_INET6: return '6'; + } + nm_assert_not_reached (); + return '?'; +} + +static inline size_t +nm_utils_addr_family_to_size (int addr_family) +{ + switch (addr_family) { + case NM_AF_INET: return NM_AF_INET_SIZE; + case NM_AF_INET6: return NM_AF_INET6_SIZE; + } + nm_assert_not_reached (); + return 0; +} + +static inline int +nm_utils_addr_family_from_size (size_t len) +{ + switch (len) { + case NM_AF_INET_SIZE: return NM_AF_INET; + case NM_AF_INET6_SIZE: return NM_AF_INET6; + } + return NM_AF_UNSPEC; +} + +#define nm_assert_addr_family(addr_family) \ + nm_assert (NM_IN_SET ((addr_family), NM_AF_INET, NM_AF_INET6)) + #endif /* __NM_STD_AUX_H__ */