diff options
author | Dan Williams <dcbw@redhat.com> | 2008-07-07 18:57:37 +0000 |
---|---|---|
committer | Dan Williams <dcbw@redhat.com> | 2008-07-07 18:57:37 +0000 |
commit | e1e4100f769cb6839e0a11e1439c1f6c66328653 (patch) | |
tree | e69d6b5cc7a9a557ee66eb24cc416db925a09b35 /libnm-util | |
parent | a2f98c6c59f41c4f160ac732a79201d426f29401 (diff) | |
download | NetworkManager-e1e4100f769cb6839e0a11e1439c1f6c66328653.tar.gz |
2008-07-07 Dan Williams <dcbw@redhat.com>
Convert to using IPv4 prefixes instead of netmasks.
git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@3812 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
Diffstat (limited to 'libnm-util')
-rw-r--r-- | libnm-util/nm-setting-ip4-config.c | 18 | ||||
-rw-r--r-- | libnm-util/nm-setting-ip4-config.h | 8 | ||||
-rw-r--r-- | libnm-util/nm-utils.c | 58 | ||||
-rw-r--r-- | libnm-util/nm-utils.h | 3 |
4 files changed, 81 insertions, 6 deletions
diff --git a/libnm-util/nm-setting-ip4-config.c b/libnm-util/nm-setting-ip4-config.c index a04b70320d..b3f5e6a893 100644 --- a/libnm-util/nm-setting-ip4-config.c +++ b/libnm-util/nm-setting-ip4-config.c @@ -70,6 +70,8 @@ static gboolean verify (NMSetting *setting, GSList *all_settings, GError **error) { NMSettingIP4Config *self = NM_SETTING_IP4_CONFIG (setting); + GSList *iter; + int i; if (!self->method) { g_set_error (error, @@ -132,6 +134,22 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } + /* Validate addresses */ + for (iter = self->addresses, i = 0; iter; iter = g_slist_next (iter), i++) { + NMSettingIP4Address *addr = (NMSettingIP4Address *) iter->data; + + if (!addr->address) { + g_warning ("invalid IP4 address #%d", i); + return FALSE; + } + + if (!addr->prefix || addr->prefix > 32) { + g_warning ("invalid IP4 address prefix %d for address #%d", + addr->prefix, i); + return FALSE; + } + } + return TRUE; } diff --git a/libnm-util/nm-setting-ip4-config.h b/libnm-util/nm-setting-ip4-config.h index ec73d869d3..340662d12b 100644 --- a/libnm-util/nm-setting-ip4-config.h +++ b/libnm-util/nm-setting-ip4-config.h @@ -45,16 +45,16 @@ GQuark nm_setting_ip4_config_error_quark (void); #define NM_SETTING_IP4_CONFIG_METHOD_SHARED "shared" typedef struct { - guint32 address; - guint32 netmask; - guint32 gateway; + guint32 address; /* network byte order */ + guint32 prefix; + guint32 gateway; /* network byte order */ } NMSettingIP4Address; typedef struct { NMSetting parent; char *method; - GArray *dns; /* array of guint32 */ + GArray *dns; /* array of guint32; elements in network byte order */ GSList *dns_search; /* list of strings */ GSList *addresses; /* array of NMSettingIP4Address */ GSList *routes; /* array of NMSettingIP4Address */ diff --git a/libnm-util/nm-utils.c b/libnm-util/nm-utils.c index e23ba361bc..013328d578 100644 --- a/libnm-util/nm-utils.c +++ b/libnm-util/nm-utils.c @@ -815,7 +815,7 @@ nm_utils_ip4_addresses_from_gvalue (const GValue *value) addr = g_malloc0 (sizeof (NMSettingIP4Address)); addr->address = g_array_index (array, guint32, 0); - addr->netmask = g_array_index (array, guint32, 1); + addr->prefix = g_array_index (array, guint32, 1); addr->gateway = g_array_index (array, guint32, 2); list = g_slist_prepend (list, addr); } @@ -838,7 +838,7 @@ nm_utils_ip4_addresses_to_gvalue (GSList *list, GValue *value) array = g_array_sized_new (FALSE, TRUE, sizeof (guint32), 3); g_array_append_val (array, addr->address); - g_array_append_val (array, addr->netmask); + g_array_append_val (array, addr->prefix); g_array_append_val (array, addr->gateway); g_ptr_array_add (addresses, array); } @@ -846,6 +846,60 @@ nm_utils_ip4_addresses_to_gvalue (GSList *list, GValue *value) g_value_take_boxed (value, addresses); } +/* + * nm_utils_ip4_netmask_to_prefix + * + * Figure out the network prefix from a netmask. Netmask + * MUST be in network byte order. + * + */ +guint32 +nm_utils_ip4_netmask_to_prefix (guint32 netmask) +{ + guchar *p, *end; + guint32 prefix = 0; + + p = (guchar *) &netmask; + end = p + sizeof (guint32); + + while ((*p == 0xFF) && p < end) { + prefix += 8; + p++; + } + + if (p < end) { + guchar v = *p; + + while (v) { + prefix++; + v <<= 1; + } + } + + return prefix; +} + +/* + * nm_utils_ip4_prefix_to_netmask + * + * Figure out the netmask from a prefix. + * + */ +guint32 +nm_utils_ip4_prefix_to_netmask (guint32 prefix) +{ + guint32 msk = 0x80000000; + guint32 netmask = 0; + + while (prefix > 0) { + netmask |= msk; + msk >>= 1; + prefix--; + } + + return (guint32) htonl (netmask); +} + GSList * nm_utils_ip6_addresses_from_gvalue (const GValue *value) { diff --git a/libnm-util/nm-utils.h b/libnm-util/nm-utils.h index f9694b4ec1..e14b616c85 100644 --- a/libnm-util/nm-utils.h +++ b/libnm-util/nm-utils.h @@ -180,6 +180,9 @@ gboolean nm_utils_security_valid (NMUtilsSecurityType type, GSList *nm_utils_ip4_addresses_from_gvalue (const GValue *value); void nm_utils_ip4_addresses_to_gvalue (GSList *list, GValue *value); +guint32 nm_utils_ip4_netmask_to_prefix (guint32 ip4_netmask); +guint32 nm_utils_ip4_prefix_to_netmask (guint32 ip4_prefix); + GSList *nm_utils_ip6_addresses_from_gvalue (const GValue *value); void nm_utils_ip6_addresses_to_gvalue (GSList *list, GValue *value); |