diff options
author | Thomas Haller <thaller@redhat.com> | 2014-06-11 08:35:23 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2014-06-11 10:54:18 +0200 |
commit | 3a5f95f446650a2a622d85aa3705d57bcd844799 (patch) | |
tree | a755cdd55d0463e04193eb80645cacf4efdd410e | |
parent | e70d5579af2269d5b034f0edc7c8459aed904bf2 (diff) | |
download | NetworkManager-3a5f95f446650a2a622d85aa3705d57bcd844799.tar.gz |
platform: add struct NMPlatformHwAddress
Signed-off-by: Thomas Haller <thaller@redhat.com>
-rw-r--r-- | src/platform/nm-platform.c | 126 | ||||
-rw-r--r-- | src/platform/nm-platform.h | 17 |
2 files changed, 143 insertions, 0 deletions
diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 6a3bbef6b1..2182793894 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -311,6 +311,132 @@ nm_platform_sysctl_get_int_checked (const char *path, guint base, gint64 min, gi /******************************************************************/ +gboolean +nm_platform_hw_address_is_zero (const NMPlatformHwAddress *addr) +{ + guint8 i; + + g_return_val_if_fail (addr, FALSE); + + for (i = 0; i < addr->len; i++) { + if (addr->hw_addr[i]) + return FALSE; + } + return TRUE; +} + +gboolean +nm_platform_hw_address_is_equal (const NMPlatformHwAddress *addr, const void *data, int len) +{ + g_return_val_if_fail (addr, FALSE); + g_return_val_if_fail (len >= 0 && len <= G_N_ELEMENTS (addr->hw_addr), FALSE); + g_return_val_if_fail (data || len == 0, FALSE); + + return addr->len == len && + (len == 0 || memcmp (addr->hw_addr, data, len) == 0); +} + +int +nm_platform_hw_address_cmp (const NMPlatformHwAddress *a, const NMPlatformHwAddress *b) +{ + int c; + + if (a == b) + return 0; + if (!a) + return -1; + if (!b) + return 1; + if (a->len < b->len) + return -1; + if (a->len > b->len) + return 1; + c = memcmp (a->hw_addr, b->hw_addr, a->len); + return CLAMP (c, -1, 1); +} + +void +nm_platform_hw_address_clear (NMPlatformHwAddress *addr, int len) +{ + g_return_if_fail (addr); + g_return_if_fail (len >= 0 && len <= G_N_ELEMENTS (addr->hw_addr)); + + memset (addr->hw_addr, 0, sizeof (addr->hw_addr)); + addr->len = len; +} + +void +nm_platform_hw_address_cpy (NMPlatformHwAddress *addr, const NMPlatformHwAddress *source) +{ + g_return_if_fail (addr); + g_return_if_fail (!source || source->len <= G_N_ELEMENTS (addr->hw_addr)); + + if (source) + memcpy (addr, source, sizeof (*addr)); + else + memset (addr, 0, sizeof (*addr)); +} + +void +nm_platform_hw_address_set (NMPlatformHwAddress *addr, const void *data, int len) +{ + g_return_if_fail (addr); + g_return_if_fail (len >= 0 && len <= G_N_ELEMENTS (addr->hw_addr)); + g_return_if_fail (data || len == 0); + + if (len <= 0) + nm_platform_hw_address_clear (addr, 0); + else { + memcpy (addr, data, len); + if (len < G_N_ELEMENTS (addr->hw_addr)) + memset (&addr[len], 0, G_N_ELEMENTS (addr->hw_addr) - len); + addr->len = len; + } +} + +void +nm_platform_hw_address_set_byte_array (NMPlatformHwAddress *addr, const GByteArray *data) +{ + if (!data) + nm_platform_hw_address_clear (addr, 0); + else + nm_platform_hw_address_set (addr, data->data, data->len); +} + +gboolean +nm_platform_hw_address_set_from_string (NMPlatformHwAddress *addr, + const char *str, int expected_len) +{ + guint8 buf[sizeof (addr->hw_addr)]; + + g_return_val_if_fail (addr, FALSE); + g_return_val_if_fail (expected_len > 0 || expected_len <= G_N_ELEMENTS (addr->hw_addr), FALSE); + g_return_val_if_fail (str && *str, FALSE); + + if (nm_utils_hwaddr_aton_len (str, buf, expected_len)) { + nm_platform_hw_address_set (addr, buf, expected_len); + return TRUE; + } + return FALSE; +} + +GByteArray * +nm_platform_hw_address_to_byte_array (const NMPlatformHwAddress *addr) +{ + GByteArray *ba; + + g_return_val_if_fail (addr, NULL); + + if (!addr->len) + return NULL; + + ba = g_byte_array_sized_new (addr->len); + g_byte_array_append (ba, addr->hw_addr, addr->len); + + return ba; +} + +/******************************************************************/ /** * nm_platform_query_devices: * diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index f7b14b7b9b..fc04a12072 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -29,6 +29,7 @@ #include <linux/if_addr.h> #include <NetworkManager.h> +#include <nm-utils.h> #define NM_TYPE_PLATFORM (nm_platform_get_type ()) #define NM_PLATFORM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_PLATFORM, NMPlatform)) @@ -156,6 +157,12 @@ typedef enum { NM_PLATFORM_SOURCE_USER, } NMPlatformSource; +typedef struct { + /* must be the very first element, so that the address of NMPlatformHwAddress + * is the same as &hw_addr[0]. */ + guint8 hw_addr[NM_UTILS_HWADDR_LEN_MAX]; + guint8 len; +} NMPlatformHwAddress; typedef struct { __NMPlatformObject_COMMON; @@ -605,6 +612,16 @@ int nm_platform_ip6_address_cmp (const NMPlatformIP6Address *a, const NMPlatform int nm_platform_ip4_route_cmp (const NMPlatformIP4Route *a, const NMPlatformIP4Route *b); int nm_platform_ip6_route_cmp (const NMPlatformIP6Route *a, const NMPlatformIP6Route *b); +gboolean nm_platform_hw_address_is_zero (const NMPlatformHwAddress *addr); +gboolean nm_platform_hw_address_is_equal (const NMPlatformHwAddress *addr, const void *data, int len); +int nm_platform_hw_address_cmp (const NMPlatformHwAddress *a, const NMPlatformHwAddress *b); +void nm_platform_hw_address_clear (NMPlatformHwAddress *addr, int len); +void nm_platform_hw_address_cpy (NMPlatformHwAddress *addr, const NMPlatformHwAddress *source); +void nm_platform_hw_address_set (NMPlatformHwAddress *addr, const void *data, int len); +void nm_platform_hw_address_set_byte_array (NMPlatformHwAddress *addr, const GByteArray *data); +gboolean nm_platform_hw_address_set_from_string (NMPlatformHwAddress *addr, const char *str, int expected_len); +GByteArray *nm_platform_hw_address_to_byte_array (const NMPlatformHwAddress *addr); + gboolean nm_platform_check_support_libnl_extended_ifa_flags (void); gboolean nm_platform_check_support_kernel_extended_ifa_flags (void); |