diff --git a/ChangeLog b/ChangeLog index b6d2a56ab..05b3a6c57 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2005-12-03 Dan Williams + + * src/NetworkManagerUtils.[ch] + src/nm-ip4-config.c + - move ip4_netmask_to_prefix() to NetworkManagerUtils.c + - consolidate code into nm_utils_ip4_addr_to_nl_addr() + 2005-12-01 Robert Love * gnome/applet/main.c, gnome/vpn-properties/nm-vpn-properties.c: We diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index 83f970bef..d17392fc3 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -37,6 +37,9 @@ #include "NetworkManagerUtils.h" #include "nm-utils.h" +#include +#include + struct NMSock { @@ -661,3 +664,34 @@ gchar *nm_utils_inet_ip4_address_as_string (guint32 ip) return g_strdup (ip_string); } + +struct nl_addr * nm_utils_ip4_addr_to_nl_addr (guint32 ip4_addr) +{ + struct nl_addr * nla = NULL; + + if (!(nla = nl_addr_alloc (sizeof (in_addr_t)))) + return NULL; + nl_addr_set_family (nla, AF_INET); + nl_addr_set_binary_addr (nla, &ip4_addr, sizeof (guint32)); + + return nla; +} + +/* + * nm_utils_ip4_netmask_to_prefix + * + * Figure out the network prefix from a netmask. Netmask + * MUST be in network byte order. + * + */ +int nm_utils_ip4_netmask_to_prefix (guint32 ip4_netmask) +{ + int i = 1; + + /* Just count how many bit shifts we need */ + ip4_netmask = ntohl (ip4_netmask); + while (!(ip4_netmask & 0x1) && ++i) + ip4_netmask = ip4_netmask >> 1; + return (32 - (i-1)); +} + diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h index f9e77d260..4c5258c89 100644 --- a/src/NetworkManagerUtils.h +++ b/src/NetworkManagerUtils.h @@ -100,6 +100,10 @@ gboolean nm_completion_boolean_function2_test(int tries, nm_completion_args args); #define nm_completion_boolean_function_test nm_completion_boolean_function1_test -gchar* nm_utils_inet_ip4_address_as_string (guint32 ip); +gchar* nm_utils_inet_ip4_address_as_string (guint32 ip); + +struct nl_addr * nm_utils_ip4_addr_to_nl_addr (guint32 ip4_addr); + +int nm_utils_ip4_netmask_to_prefix (guint32 ip4_netmask); #endif diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index 5a8fccc13..bcf4ecded 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -24,6 +24,7 @@ #include #include #include "NetworkManager.h" +#include "NetworkManagerUtils.h" #include "nm-ip4-config.h" #include @@ -238,24 +239,6 @@ guint32 nm_ip4_config_get_num_domains (NMIP4Config *config) extern void rtnl_addr_set_prefixlen (struct rtnl_addr *, int); -/* - * ip4_netmask_to_prefix - * - * Figure out the network prefix from a netmask. Netmask - * MUST be in network byte order. - * - */ -static int ip4_netmask_to_prefix (guint32 ip4_netmask) -{ - int i = 1; - - /* Just count how many bit shifts we need */ - ip4_netmask = ntohl (ip4_netmask); - while (!(ip4_netmask & 0x1) && ++i) - ip4_netmask = ip4_netmask >> 1; - return (32 - (i-1)); -} - static int ip4_addr_to_rtnl_local (guint32 ip4_address, struct rtnl_addr *addr) { struct nl_addr * local = NULL; @@ -263,13 +246,10 @@ static int ip4_addr_to_rtnl_local (guint32 ip4_address, struct rtnl_addr *addr) g_return_val_if_fail (addr != NULL, -1); - local = nl_addr_alloc (sizeof (in_addr_t)); - nl_addr_set_family (local, AF_INET); - nl_addr_set_binary_addr (local, &ip4_address, sizeof (guint32)); - + local = nm_utils_ip4_addr_to_nl_addr (ip4_address); err = rtnl_addr_set_local (addr, local); - nl_addr_put (local); + return err; } @@ -280,13 +260,10 @@ static int ip4_addr_to_rtnl_peer (guint32 ip4_address, struct rtnl_addr *addr) g_return_val_if_fail (addr != NULL, -1); - peer = nl_addr_alloc (sizeof (in_addr_t)); - nl_addr_set_family (peer, AF_INET); - nl_addr_set_binary_addr (peer, &ip4_address, sizeof (guint32)); - + peer = nm_utils_ip4_addr_to_nl_addr (ip4_address); err = rtnl_addr_set_peer (addr, peer); - nl_addr_put (peer); + return err; } @@ -294,7 +271,7 @@ static void ip4_addr_to_rtnl_prefixlen (guint32 ip4_netmask, struct rtnl_addr *a { g_return_if_fail (addr != NULL); - rtnl_addr_set_prefixlen (addr,ip4_netmask_to_prefix (ip4_netmask)); + rtnl_addr_set_prefixlen (addr, nm_utils_ip4_netmask_to_prefix (ip4_netmask)); } static int ip4_addr_to_rtnl_broadcast (guint32 ip4_broadcast, struct rtnl_addr *addr) @@ -304,13 +281,10 @@ static int ip4_addr_to_rtnl_broadcast (guint32 ip4_broadcast, struct rtnl_addr * g_return_val_if_fail (addr != NULL, -1); - local = nl_addr_alloc (sizeof (in_addr_t)); - nl_addr_set_family (local, AF_INET); - nl_addr_set_binary_addr (local, &ip4_broadcast, sizeof (guint32)); - + local = nm_utils_ip4_addr_to_nl_addr (ip4_broadcast); err = rtnl_addr_set_broadcast (addr, local); - nl_addr_put (local); + return err; }