core: determine classful IPv4 prefix if no DHCP netmask is provided (bgo #603098)

This commit is contained in:
Jirka Klimes 2010-02-25 16:52:10 -08:00 committed by Dan Williams
parent 2281b87cc4
commit 0f0daf7852
6 changed files with 39 additions and 18 deletions

View file

@ -59,7 +59,7 @@ libnm_util_la_SOURCES= \
libnm_util_la_LIBADD = $(GLIB_LIBS) $(DBUS_LIBS) $(UUID_LIBS)
libnm_util_la_LDFLAGS = -Wl,--version-script=$(srcdir)/libnm-util.ver \
-version-info "2:0:1"
-version-info "3:0:2"
if WITH_GNUTLS
libnm_util_la_SOURCES += crypto_gnutls.c

View file

@ -345,6 +345,7 @@ global:
nm_utils_ip4_addresses_to_gvalue;
nm_utils_ip4_netmask_to_prefix;
nm_utils_ip4_prefix_to_netmask;
nm_utils_ip4_get_default_prefix;
nm_utils_ip4_routes_from_gvalue;
nm_utils_ip4_routes_to_gvalue;
nm_utils_ip6_addresses_from_gvalue;

View file

@ -21,7 +21,7 @@
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* (C) Copyright 2005 - 2009 Red Hat, Inc.
* (C) Copyright 2005 - 2010 Red Hat, Inc.
*/
#include <string.h>
@ -1209,6 +1209,29 @@ nm_utils_ip4_prefix_to_netmask (guint32 prefix)
}
/**
* nm_utils_ip4_get_default_prefix:
* @ip: an IPv4 address (in network byte order)
*
* When the Internet was originally set up, various ranges of IP addresses were
* segmented into three network classes: A, B, and C. This function will return
* a prefix that is associated with the IP address specified defining where it
* falls in the predefined classes.
*
* Returns: the default class prefix for the given IP
**/
/* The function is originally from ipcalc.c of Red Hat's initscripts. */
guint32
nm_utils_ip4_get_default_prefix (guint32 ip)
{
if (((ntohl (ip) & 0xFF000000) >> 24) <= 127)
return 8; /* Class A - 255.0.0.0 */
else if (((ntohl (ip) & 0xFF000000) >> 24) <= 191)
return 16; /* Class B - 255.255.0.0 */
return 24; /* Class C - 255.255.255.0 */
}
GSList *
nm_utils_ip6_addresses_from_gvalue (const GValue *value)
{

View file

@ -20,7 +20,7 @@
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* (C) Copyright 2005 - 2008 Red Hat, Inc.
* (C) Copyright 2005 - 2010 Red Hat, Inc.
*/
#ifndef NM_UTILS_H
@ -192,6 +192,7 @@ void nm_utils_ip4_routes_to_gvalue (GSList *list, GValue *value);
guint32 nm_utils_ip4_netmask_to_prefix (guint32 netmask);
guint32 nm_utils_ip4_prefix_to_netmask (guint32 prefix);
guint32 nm_utils_ip4_get_default_prefix (guint32 ip);
GSList *nm_utils_ip6_addresses_from_gvalue (const GValue *value);
void nm_utils_ip6_addresses_to_gvalue (GSList *list, GValue *value);

View file

@ -237,13 +237,16 @@ nm_dhcp_dhclient_get_lease_config (const char *iface, const char *uuid)
/* Netmask */
data = g_hash_table_lookup (hash, "option subnet-mask");
if (!data)
data = "255.255.255.0"; /* FIXME: assume class C? */
if (!inet_pton (AF_INET, data, &tmp)) {
g_warning ("%s: couldn't parse IP4 subnet mask '%s'", __func__, data);
goto error;
if (data) {
if (!inet_pton (AF_INET, data, &tmp)) {
g_warning ("%s: couldn't parse IP4 subnet mask '%s'", __func__, data);
goto error;
}
prefix = nm_utils_ip4_netmask_to_prefix (tmp.s_addr);
} else {
/* Get default netmask for the IP according to appropriate class. */
prefix = nm_utils_ip4_get_default_prefix (nm_ip4_address_get_address (addr));
}
prefix = nm_utils_ip4_netmask_to_prefix (tmp.s_addr);
nm_ip4_address_set_prefix (addr, prefix);
/* Gateway */

View file

@ -606,16 +606,9 @@ read_full_ip4_address (shvarFile *ifcfg,
/* Try to autodetermine the prefix for the address' class */
if (!nm_ip4_address_get_prefix (addr)) {
guint32 tmp_addr, prefix = 0;
tmp_addr = nm_ip4_address_get_address (addr);
if (((ntohl(tmp_addr) & 0xFF000000) >> 24) <= 127)
prefix = 8;
else if (((ntohl(tmp_addr) & 0xFF000000) >> 24) <= 191)
prefix = 16;
else
prefix = 24;
guint32 prefix = 0;
prefix = nm_utils_ip4_get_default_prefix (nm_ip4_address_get_address (addr));
nm_ip4_address_set_prefix (addr, prefix);
value = svGetValue (ifcfg, ip_tag, FALSE);