summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-07-22 11:38:14 +0200
committerThomas Haller <thaller@redhat.com>2020-07-23 15:29:23 +0200
commit56a0aa06ac005fa991cfdbe5866857fbb9c865d3 (patch)
treeb1644b47776d2f9bbc0c25ee547e65a9150e4385
parent97770bc7a834158dad449b09cc70991b7409f91f (diff)
downloadNetworkManager-56a0aa06ac005fa991cfdbe5866857fbb9c865d3.tar.gz
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".
-rw-r--r--shared/nm-glib-aux/nm-shared-utils.h36
-rw-r--r--shared/nm-glib-aux/tests/test-shared-general.c10
-rw-r--r--shared/nm-std-aux/nm-std-aux.h45
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__ */