Commit c77784b5 authored by Thomas Haller's avatar Thomas Haller

dhcp: remove unused nm_dhcp_dhclient_read_lease_ip_configs() function

parent e762eba0
......@@ -1791,10 +1791,6 @@ $(src_dhcp_tests_test_dhcp_utils_OBJECTS): $(libnm_core_lib_h_pub_mkenums)
EXTRA_DIST += \
src/dhcp/tests/test-dhclient-duid.leases \
src/dhcp/tests/test-dhclient-commented-duid.leases \
src/dhcp/tests/leases/basic.leases \
src/dhcp/tests/leases/malformed1.leases \
src/dhcp/tests/leases/malformed2.leases \
src/dhcp/tests/leases/malformed3.leases \
src/dhcp/tests/meson.build
###############################################################################
......
......@@ -643,259 +643,3 @@ nm_dhcp_dhclient_save_duid (const char *leasefile,
g_string_free (s, TRUE);
return success;
}
static void
add_lease_option (GHashTable *hash, char *line)
{
char *spc;
size_t len;
/* Find the space after "option" */
spc = strchr (line, ' ');
if (!spc)
return;
/* Find the option tag's data, which is after the second space */
if (g_str_has_prefix (line, "option ")) {
while (g_ascii_isspace (*spc))
spc++;
spc = strchr (spc + 1, ' ');
if (!spc)
return;
}
/* Split the line at the space */
*spc = '\0';
spc++;
/* Kill the ';' at the end of the line, if any */
len = strlen (spc);
if (*(spc + len - 1) == ';')
*(spc + len - 1) = '\0';
/* Strip leading quote */
while (g_ascii_isspace (*spc))
spc++;
if (*spc == '"')
spc++;
/* Strip trailing quote */
len = strlen (spc);
if (len > 0 && spc[len - 1] == '"')
spc[len - 1] = '\0';
if (spc[0])
g_hash_table_insert (hash, g_strdup (line), g_strdup (spc));
}
#define LEASE_INVALID G_MININT64
static GTimeSpan
lease_validity_span (const char *str_expire, GDateTime *now)
{
GDateTime *expire = NULL;
struct tm expire_tm;
GTimeSpan span;
g_return_val_if_fail (now != NULL, LEASE_INVALID);
g_return_val_if_fail (str_expire != NULL, LEASE_INVALID);
/* Skip initial number (day of week?) */
if (!isdigit (*str_expire++))
return LEASE_INVALID;
if (!isspace (*str_expire++))
return LEASE_INVALID;
/* Read lease expiration (in UTC) */
if (!strptime (str_expire, "%t%Y/%m/%d %H:%M:%S", &expire_tm))
return LEASE_INVALID;
expire = g_date_time_new_utc (expire_tm.tm_year + 1900,
expire_tm.tm_mon + 1,
expire_tm.tm_mday,
expire_tm.tm_hour,
expire_tm.tm_min,
expire_tm.tm_sec);
if (!expire)
return LEASE_INVALID;
span = g_date_time_difference (expire, now);
g_date_time_unref (expire);
/* GDateTime only supports a range of less then 10000 years, so span can
* not overflow or be equal to LEASE_INVALID */
return span;
}
/**
* nm_dhcp_dhclient_read_lease_ip_configs:
* @multi_idx: the multi index instance for the ip config object
* @addr_family: whether to read IPv4 or IPv6 leases
* @iface: the interface name to match leases with
* @ifindex: interface index of @iface
* @route_table: the route table for the default route.
* @route_metric: the route metric for the default route.
* @contents: the contents of a dhclient leasefile
* @now: the current UTC date/time; pass %NULL to automatically use current
* UTC time. Testcases may need a different value for 'now'
*
* Reads dhclient leases from @contents and parses them into either
* #NMIP4Config or #NMIP6Config objects depending on the value of @addr_family.
*
* Returns: a #GSList of #NMIP4Config objects (if @addr_family is %AF_INET) or a list of
* #NMIP6Config objects (if @addr_family is %AF_INET6) containing the lease data.
*/
GSList *
nm_dhcp_dhclient_read_lease_ip_configs (NMDedupMultiIndex *multi_idx,
int addr_family,
const char *iface,
int ifindex,
guint32 route_table,
guint32 route_metric,
const char *contents,
GDateTime *now)
{
GSList *parsed = NULL, *iter, *leases = NULL;
char **line, **split = NULL;
GHashTable *hash = NULL;
gint32 now_monotonic_ts;
g_return_val_if_fail (contents != NULL, NULL);
nm_assert (NM_IN_SET (addr_family, AF_INET, AF_INET6));
split = g_strsplit_set (contents, "\n\r", -1);
if (!split)
return NULL;
for (line = split; line && *line; line++) {
*line = g_strstrip (*line);
if (*line[0] == '#') {
/* Comment */
} else if (!strcmp (*line, "}")) {
/* Lease ends */
parsed = g_slist_append (parsed, hash);
hash = NULL;
} else if (!strcmp (*line, "lease {")) {
/* Beginning of a new lease */
if (hash) {
/* Ignore malformed lease that doesn't end before new one starts */
g_hash_table_destroy (hash);
}
hash = g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, g_free);
} else if (hash && strlen (*line))
add_lease_option (hash, *line);
}
g_strfreev (split);
/* Check if the last lease in the file was properly ended */
if (hash) {
/* Ignore malformed lease that doesn't end before new one starts */
g_hash_table_destroy (hash);
hash = NULL;
}
if (now)
g_date_time_ref (now);
else
now = g_date_time_new_now_utc ();
now_monotonic_ts = nm_utils_get_monotonic_timestamp_s ();
for (iter = parsed; iter; iter = g_slist_next (iter)) {
NMIP4Config *ip4;
NMPlatformIP4Address address;
const char *value;
GTimeSpan expiry;
guint32 tmp, gw = 0;
hash = iter->data;
/* Make sure this lease is for the interface we want */
value = g_hash_table_lookup (hash, "interface");
if (!value || strcmp (value, iface))
continue;
value = g_hash_table_lookup (hash, "expire");
if (!value)
continue;
expiry = lease_validity_span (value, now);
if (expiry == LEASE_INVALID)
continue;
/* scale expiry to seconds (and CLAMP into the range of guint32) */
expiry = CLAMP (expiry / G_TIME_SPAN_SECOND, 0, NM_PLATFORM_LIFETIME_PERMANENT-1);
if (expiry <= 0) {
/* the address is already expired. Don't even add it. */
continue;
}
memset (&address, 0, sizeof (address));
/* IP4 address */
value = g_hash_table_lookup (hash, "fixed-address");
if (!value)
continue;
if (!inet_pton (AF_INET, value, &address.address))
continue;
address.peer_address = address.address;
/* Gateway */
value = g_hash_table_lookup (hash, "option routers");
if (!value)
continue;
if (!inet_pton (AF_INET, value, &gw))
continue;
/* Netmask */
value = g_hash_table_lookup (hash, "option subnet-mask");
if (value && inet_pton (AF_INET, value, &tmp))
address.plen = nm_utils_ip4_netmask_to_prefix (tmp);
/* Get default netmask for the IP according to appropriate class. */
if (!address.plen)
address.plen = _nm_utils_ip4_get_default_prefix (address.address);
address.timestamp = now_monotonic_ts;
address.lifetime = address.preferred = expiry;
address.addr_source = NM_IP_CONFIG_SOURCE_DHCP;
ip4 = nm_ip4_config_new (multi_idx, ifindex);
nm_ip4_config_add_address (ip4, &address);
{
const NMPlatformIP4Route r = {
.rt_source = NM_IP_CONFIG_SOURCE_DHCP,
.gateway = gw,
.table_coerced = nm_platform_route_table_coerce (route_table),
.metric = route_metric,
};
nm_ip4_config_add_route (ip4, &r, NULL);
}
value = g_hash_table_lookup (hash, "option domain-name-servers");
if (value) {
char **dns, **dns_iter;
dns = g_strsplit_set (value, ",", -1);
for (dns_iter = dns; dns_iter && *dns_iter; dns_iter++) {
if (inet_pton (AF_INET, *dns_iter, &tmp))
nm_ip4_config_add_nameserver (ip4, tmp);
}
if (dns)
g_strfreev (dns);
}
value = g_hash_table_lookup (hash, "option domain-name");
if (value && value[0])
nm_ip4_config_add_domain (ip4, value);
/* FIXME: static routes */
leases = g_slist_append (leases, ip4);
}
g_date_time_unref (now);
g_slist_free_full (parsed, (GDestroyNotify) g_hash_table_destroy);
return leases;
}
......@@ -43,15 +43,6 @@ gboolean nm_dhcp_dhclient_save_duid (const char *leasefile,
const char *escaped_duid,
GError **error);
GSList *nm_dhcp_dhclient_read_lease_ip_configs (struct _NMDedupMultiIndex *multi_idx,
int addr_family,
const char *iface,
int ifindex,
guint32 route_table,
guint32 route_metric,
const char *contents,
GDateTime *now);
GBytes *nm_dhcp_dhclient_get_client_id_from_config_file (const char *path);
#endif /* __NETWORKMANAGER_DHCP_DHCLIENT_UTILS_H__ */
......
lease {
interface "wlan0";
fixed-address 192.168.1.180;
option subnet-mask 255.255.255.0;
option routers 192.168.1.1;
option dhcp-lease-time 600;
option dhcp-message-type 5;
option domain-name-servers 192.168.1.1;
option dhcp-server-identifier 192.168.1.1;
option broadcast-address 192.168.1.255;
renew 5 2013/11/01 19:56:15;
rebind 5 2013/11/01 20:00:44;
expire 5 2013/11/01 20:01:59;
}
lease {
interface "wlan0";
fixed-address 10.77.52.141;
option subnet-mask 255.0.0.0;
option dhcp-lease-time 1200;
option routers 10.77.52.254;
option dhcp-message-type 5;
option dhcp-server-identifier 10.77.52.254;
option domain-name-servers 8.8.8.8,8.8.4.4;
option dhcp-renewal-time 600;
option dhcp-rebinding-time 1050;
option domain-name "morriesguest.local";
renew 5 2013/11/01 20:01:08;
rebind 5 2013/11/01 20:05:00;
expire 5 2013/11/01 20:06:15;
}
# missing fixed-address option
lease {
interface "wlan0";
option subnet-mask 255.255.255.0;
option routers 192.168.1.1;
option dhcp-lease-time 600;
option dhcp-message-type 5;
option domain-name-servers 192.168.1.1;
option dhcp-server-identifier 192.168.1.1;
option broadcast-address 192.168.1.255;
renew 5 2013/11/01 19:56:15;
rebind 5 2013/11/01 20:00:44;
expire 5 2013/11/01 20:01:59;
}
# missing routers option
lease {
interface "wlan0";
fixed-address 192.168.1.180;
option subnet-mask 255.255.255.0;
option dhcp-lease-time 600;
option dhcp-message-type 5;
option domain-name-servers 192.168.1.1;
option dhcp-server-identifier 192.168.1.1;
option broadcast-address 192.168.1.255;
renew 5 2013/11/01 19:56:15;
rebind 5 2013/11/01 20:00:44;
expire 5 2013/11/01 20:01:59;
}
# missing expire time
lease {
interface "wlan0";
fixed-address 192.168.1.180;
option subnet-mask 255.255.255.0;
option routers 192.168.1.1;
option dhcp-lease-time 600;
option dhcp-message-type 5;
option domain-name-servers 192.168.1.1;
option dhcp-server-identifier 192.168.1.1;
option broadcast-address 192.168.1.255;
renew 5 2013/11/01 19:56:15;
rebind 5 2013/11/01 20:00:44;
}
......@@ -36,12 +36,6 @@
#include "nm-test-utils-core.h"
#define DEBUG 1
static const int IFINDEX = 5;
static const guint32 ROUTE_TABLE = RT_TABLE_MAIN;
static const guint32 ROUTE_METRIC = 100;
static void
test_config (const char *orig,
const char *expected,
......@@ -993,133 +987,6 @@ test_config_req_intf (void)
/*****************************************************************************/
static void
test_read_lease_ip4_config_basic (void)
{
nm_auto_unref_dedup_multi_index NMDedupMultiIndex *multi_idx = nm_dedup_multi_index_new ();
GError *error = NULL;
char *contents = NULL;
gboolean success;
const char *path = TESTDIR "/leases/basic.leases";
GSList *leases;
GDateTime *now;
NMIP4Config *config;
const NMPlatformIP4Address *addr;
guint32 expected_addr;
success = g_file_get_contents (path, &contents, NULL, &error);
g_assert_no_error (error);
g_assert (success);
/* Date from before the least expiration */
now = g_date_time_new_utc (2013, 11, 1, 19, 55, 32);
leases = nm_dhcp_dhclient_read_lease_ip_configs (multi_idx, AF_INET, "wlan0", IFINDEX, ROUTE_TABLE, ROUTE_METRIC, contents, now);
g_assert_cmpint (g_slist_length (leases), ==, 2);
/* IP4Config #1 */
config = g_slist_nth_data (leases, 0);
g_assert (NM_IS_IP4_CONFIG (config));
/* Address */
g_assert_cmpint (nm_ip4_config_get_num_addresses (config), ==, 1);
expected_addr = nmtst_inet4_from_string ("192.168.1.180");
addr = _nmtst_ip4_config_get_address (config, 0);
g_assert_cmpint (addr->address, ==, expected_addr);
g_assert_cmpint (addr->peer_address, ==, expected_addr);
g_assert_cmpint (addr->plen, ==, 24);
/* Gateway */
expected_addr = nmtst_inet4_from_string ("192.168.1.1");
g_assert_cmpint (nmtst_ip4_config_get_gateway (config), ==, expected_addr);
/* DNS */
g_assert_cmpint (nm_ip4_config_get_num_nameservers (config), ==, 1);
expected_addr = nmtst_inet4_from_string ("192.168.1.1");
g_assert_cmpint (nm_ip4_config_get_nameserver (config, 0), ==, expected_addr);
g_assert_cmpint (nm_ip4_config_get_num_domains (config), ==, 0);
/* IP4Config #2 */
config = g_slist_nth_data (leases, 1);
g_assert (NM_IS_IP4_CONFIG (config));
/* Address */
g_assert_cmpint (nm_ip4_config_get_num_addresses (config), ==, 1);
expected_addr = nmtst_inet4_from_string ("10.77.52.141");
addr = _nmtst_ip4_config_get_address (config, 0);
g_assert_cmpint (addr->address, ==, expected_addr);
g_assert_cmpint (addr->peer_address, ==, expected_addr);
g_assert_cmpint (addr->plen, ==, 8);
/* Gateway */
expected_addr = nmtst_inet4_from_string ("10.77.52.254");
g_assert_cmpint (nmtst_ip4_config_get_gateway (config), ==, expected_addr);
/* DNS */
g_assert_cmpint (nm_ip4_config_get_num_nameservers (config), ==, 2);
expected_addr = nmtst_inet4_from_string ("8.8.8.8");
g_assert_cmpint (nm_ip4_config_get_nameserver (config, 0), ==, expected_addr);
expected_addr = nmtst_inet4_from_string ("8.8.4.4");
g_assert_cmpint (nm_ip4_config_get_nameserver (config, 1), ==, expected_addr);
/* Domains */
g_assert_cmpint (nm_ip4_config_get_num_domains (config), ==, 1);
g_assert_cmpstr (nm_ip4_config_get_domain (config, 0), ==, "morriesguest.local");
g_slist_free_full (leases, g_object_unref);
g_date_time_unref (now);
g_free (contents);
}
static void
test_read_lease_ip4_config_expired (void)
{
nm_auto_unref_dedup_multi_index NMDedupMultiIndex *multi_idx = nm_dedup_multi_index_new ();
GError *error = NULL;
char *contents = NULL;
gboolean success;
const char *path = TESTDIR "/leases/basic.leases";
GSList *leases;
GDateTime *now;
success = g_file_get_contents (path, &contents, NULL, &error);
g_assert_no_error (error);
g_assert (success);
/* Date from *after* the lease expiration */
now = g_date_time_new_utc (2013, 12, 1, 19, 55, 32);
leases = nm_dhcp_dhclient_read_lease_ip_configs (multi_idx, AF_INET, "wlan0", IFINDEX, ROUTE_TABLE, ROUTE_METRIC, contents, now);
g_assert (leases == NULL);
g_date_time_unref (now);
g_free (contents);
}
static void
test_read_lease_ip4_config_expect_failure (gconstpointer user_data)
{
nm_auto_unref_dedup_multi_index NMDedupMultiIndex *multi_idx = nm_dedup_multi_index_new ();
GError *error = NULL;
char *contents = NULL;
gboolean success;
GSList *leases;
GDateTime *now;
success = g_file_get_contents ((const char *) user_data, &contents, NULL, &error);
g_assert_no_error (error);
g_assert (success);
/* Date from before the least expiration */
now = g_date_time_new_utc (2013, 11, 1, 1, 1, 1);
leases = nm_dhcp_dhclient_read_lease_ip_configs (multi_idx, AF_INET, "wlan0", IFINDEX, ROUTE_TABLE, ROUTE_METRIC, contents, now);
g_assert (leases == NULL);
g_date_time_unref (now);
g_free (contents);
}
/*****************************************************************************/
NMTST_DEFINE ();
int
......@@ -1157,18 +1024,6 @@ main (int argc, char **argv)
g_test_add_func ("/dhcp/dhclient/write_existing_duid", test_write_existing_duid);
g_test_add_func ("/dhcp/dhclient/write_existing_commented_duid", test_write_existing_commented_duid);
g_test_add_func ("/dhcp/dhclient/leases/ip4-config/basic", test_read_lease_ip4_config_basic);
g_test_add_func ("/dhcp/dhclient/leases/ip4-config/expired", test_read_lease_ip4_config_expired);
g_test_add_data_func ("/dhcp/dhclient/leases/ip4-config/missing-address",
TESTDIR "/leases/malformed1.leases",
test_read_lease_ip4_config_expect_failure);
g_test_add_data_func ("/dhcp/dhclient/leases/ip4-config/missing-gateway",
TESTDIR "/leases/malformed2.leases",
test_read_lease_ip4_config_expect_failure);
g_test_add_data_func ("/dhcp/dhclient/leases/ip4-config/missing-expire",
TESTDIR "/leases/malformed3.leases",
test_read_lease_ip4_config_expect_failure);
return g_test_run ();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment