mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-01-06 14:20:16 +01:00
dhcp: actually use runtime-selected DHCP client
This commit is contained in:
parent
702836b42f
commit
81f23ea383
4 changed files with 86 additions and 41 deletions
|
|
@ -226,6 +226,21 @@ out:
|
|||
g_free (reason);
|
||||
}
|
||||
|
||||
static GType
|
||||
get_client_type (const char *client, GError **error)
|
||||
{
|
||||
g_return_val_if_fail (client != NULL, 0);
|
||||
|
||||
if (!strcmp (client, "dhclient") && strlen (DHCLIENT_PATH))
|
||||
return NM_TYPE_DHCP_DHCLIENT;
|
||||
else if (!strcmp (client, "dhcpcd") && strlen (DHCPCD_PATH))
|
||||
return NM_TYPE_DHCP_DHCPCD;
|
||||
else
|
||||
g_set_error (error, 0, 0, "unknown or missing DHCP client '%s'", client);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
NMDHCPManager *
|
||||
nm_dhcp_manager_new (const char *client, GError **error)
|
||||
{
|
||||
|
|
@ -239,16 +254,16 @@ nm_dhcp_manager_new (const char *client, GError **error)
|
|||
priv = NM_DHCP_MANAGER_GET_PRIVATE (singleton);
|
||||
|
||||
/* Figure out which DHCP client to use */
|
||||
if (!strcmp (client, "dhclient") && strlen (DHCLIENT_PATH)) {
|
||||
priv->client_type = NM_TYPE_DHCP_DHCLIENT;
|
||||
priv->get_lease_config_func = nm_dhcp_dhclient_get_lease_config;
|
||||
} else if (!strcmp (client, "dhcpcd") && strlen (DHCPCD_PATH)) {
|
||||
priv->client_type = NM_TYPE_DHCP_DHCPCD;
|
||||
priv->get_lease_config_func = nm_dhcp_dhcpcd_get_lease_config;
|
||||
} else {
|
||||
g_set_error (error, 0, 0, "unknown or missing DHCP client '%s'", client);
|
||||
priv->client_type = get_client_type (client, error);
|
||||
if (!priv->client_type)
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (priv->client_type == NM_TYPE_DHCP_DHCLIENT)
|
||||
priv->get_lease_config_func = nm_dhcp_dhclient_get_lease_config;
|
||||
else if (priv->client_type == NM_TYPE_DHCP_DHCPCD)
|
||||
priv->get_lease_config_func = nm_dhcp_dhcpcd_get_lease_config;
|
||||
else
|
||||
g_assert_not_reached ();
|
||||
|
||||
priv->clients = g_hash_table_new_full (g_direct_hash, g_direct_equal,
|
||||
NULL,
|
||||
|
|
@ -368,7 +383,7 @@ nm_dhcp_manager_start_client (NMDHCPManager *self,
|
|||
}
|
||||
|
||||
/* And make a new one */
|
||||
client = g_object_new (NM_TYPE_DHCP_DHCLIENT,
|
||||
client = g_object_new (priv->client_type,
|
||||
NM_DHCP_CLIENT_INTERFACE, iface,
|
||||
NULL);
|
||||
g_return_val_if_fail (client != NULL, NULL);
|
||||
|
|
@ -444,14 +459,24 @@ nm_dhcp_manager_get_lease_config (NMDHCPManager *self,
|
|||
}
|
||||
|
||||
NMIP4Config *
|
||||
nm_dhcp_manager_test_ip4_options_to_config (const char *iface,
|
||||
nm_dhcp_manager_test_ip4_options_to_config (const char *dhcp_client,
|
||||
const char *iface,
|
||||
GHashTable *options,
|
||||
const char *reason)
|
||||
{
|
||||
NMDHCPClient *client;
|
||||
NMIP4Config *config;
|
||||
GType client_type;
|
||||
GError *error = NULL;
|
||||
|
||||
client = (NMDHCPClient *) g_object_new (NM_TYPE_DHCP_DHCLIENT,
|
||||
client_type = get_client_type (dhcp_client, &error);
|
||||
if (!client_type) {
|
||||
g_warning ("Error: %s", error ? error->message : "(unknown)");
|
||||
g_clear_error (&error);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
client = (NMDHCPClient *) g_object_new (client_type,
|
||||
NM_DHCP_CLIENT_INTERFACE, iface,
|
||||
NULL);
|
||||
g_return_val_if_fail (client != NULL, NULL);
|
||||
|
|
|
|||
|
|
@ -66,7 +66,8 @@ GSList * nm_dhcp_manager_get_lease_config (NMDHCPManager *self,
|
|||
const char *uuid);
|
||||
|
||||
/* For testing only */
|
||||
NMIP4Config *nm_dhcp_manager_test_ip4_options_to_config (const char *iface,
|
||||
NMIP4Config *nm_dhcp_manager_test_ip4_options_to_config (const char *dhcp_client,
|
||||
const char *iface,
|
||||
GHashTable *options,
|
||||
const char *reason);
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,9 @@ test_dhcp_options_SOURCES = \
|
|||
|
||||
test_dhcp_options_CPPFLAGS = \
|
||||
$(GLIB_CFLAGS) \
|
||||
$(DBUS_CFLAGS)
|
||||
$(DBUS_CFLAGS) \
|
||||
-DDHCLIENT_PATH=\"$(DHCLIENT_PATH)\" \
|
||||
-DDHCPCD_PATH=\"$(DHCPCD_PATH)\"
|
||||
|
||||
test_dhcp_options_LDADD = \
|
||||
$(top_builddir)/libnm-util/libnm-util.la \
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ static Option generic_options[] = {
|
|||
};
|
||||
|
||||
static void
|
||||
test_generic_options (void)
|
||||
test_generic_options (const char *client)
|
||||
{
|
||||
GHashTable *options;
|
||||
NMIP4Config *ip4_config;
|
||||
|
|
@ -116,7 +116,7 @@ test_generic_options (void)
|
|||
const char *expected_route2_gw = "10.1.1.1";
|
||||
|
||||
options = fill_table (generic_options, NULL);
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config (client, "eth0", options, "rebind");
|
||||
ASSERT (ip4_config != NULL,
|
||||
"dhcp-generic", "failed to parse DHCP4 options");
|
||||
|
||||
|
|
@ -215,7 +215,7 @@ static Option wins_options[] = {
|
|||
};
|
||||
|
||||
static void
|
||||
test_wins_options (void)
|
||||
test_wins_options (const char *client)
|
||||
{
|
||||
GHashTable *options;
|
||||
NMIP4Config *ip4_config;
|
||||
|
|
@ -227,7 +227,7 @@ test_wins_options (void)
|
|||
options = fill_table (generic_options, NULL);
|
||||
options = fill_table (wins_options, options);
|
||||
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config (client, "eth0", options, "rebind");
|
||||
ASSERT (ip4_config != NULL,
|
||||
"dhcp-wins", "failed to parse DHCP4 options");
|
||||
|
||||
|
|
@ -259,7 +259,7 @@ static Option classless_routes_options[] = {
|
|||
};
|
||||
|
||||
static void
|
||||
test_classless_static_routes (void)
|
||||
test_classless_static_routes (const char *client)
|
||||
{
|
||||
GHashTable *options;
|
||||
NMIP4Config *ip4_config;
|
||||
|
|
@ -273,7 +273,7 @@ test_classless_static_routes (void)
|
|||
options = fill_table (generic_options, NULL);
|
||||
options = fill_table (classless_routes_options, options);
|
||||
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config (client, "eth0", options, "rebind");
|
||||
ASSERT (ip4_config != NULL,
|
||||
"dhcp-rfc3442", "failed to parse DHCP4 options");
|
||||
|
||||
|
|
@ -327,7 +327,7 @@ static Option invalid_classless_routes1[] = {
|
|||
};
|
||||
|
||||
static void
|
||||
test_invalid_classless_routes1 (void)
|
||||
test_invalid_classless_routes1 (const char *client)
|
||||
{
|
||||
GHashTable *options;
|
||||
NMIP4Config *ip4_config;
|
||||
|
|
@ -339,7 +339,7 @@ test_invalid_classless_routes1 (void)
|
|||
options = fill_table (generic_options, NULL);
|
||||
options = fill_table (invalid_classless_routes1, options);
|
||||
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config (client, "eth0", options, "rebind");
|
||||
ASSERT (ip4_config != NULL,
|
||||
"dhcp-rfc3442-invalid-1", "failed to parse DHCP4 options");
|
||||
|
||||
|
|
@ -376,7 +376,7 @@ static Option invalid_classless_routes2[] = {
|
|||
};
|
||||
|
||||
static void
|
||||
test_invalid_classless_routes2 (void)
|
||||
test_invalid_classless_routes2 (const char *client)
|
||||
{
|
||||
GHashTable *options;
|
||||
NMIP4Config *ip4_config;
|
||||
|
|
@ -390,7 +390,7 @@ test_invalid_classless_routes2 (void)
|
|||
options = fill_table (generic_options, NULL);
|
||||
options = fill_table (invalid_classless_routes2, options);
|
||||
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config (client, "eth0", options, "rebind");
|
||||
ASSERT (ip4_config != NULL,
|
||||
"dhcp-rfc3442-invalid-2", "failed to parse DHCP4 options");
|
||||
|
||||
|
|
@ -448,7 +448,7 @@ static Option invalid_classless_routes3[] = {
|
|||
};
|
||||
|
||||
static void
|
||||
test_invalid_classless_routes3 (void)
|
||||
test_invalid_classless_routes3 (const char *client)
|
||||
{
|
||||
GHashTable *options;
|
||||
NMIP4Config *ip4_config;
|
||||
|
|
@ -460,7 +460,7 @@ test_invalid_classless_routes3 (void)
|
|||
options = fill_table (generic_options, NULL);
|
||||
options = fill_table (invalid_classless_routes3, options);
|
||||
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config (client, "eth0", options, "rebind");
|
||||
ASSERT (ip4_config != NULL,
|
||||
"dhcp-rfc3442-invalid-3", "failed to parse DHCP4 options");
|
||||
|
||||
|
|
@ -497,7 +497,7 @@ static Option gw_in_classless_routes[] = {
|
|||
};
|
||||
|
||||
static void
|
||||
test_gateway_in_classless_routes (void)
|
||||
test_gateway_in_classless_routes (const char *client)
|
||||
{
|
||||
GHashTable *options;
|
||||
NMIP4Config *ip4_config;
|
||||
|
|
@ -511,7 +511,7 @@ test_gateway_in_classless_routes (void)
|
|||
options = fill_table (generic_options, NULL);
|
||||
options = fill_table (gw_in_classless_routes, options);
|
||||
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config (client, "eth0", options, "rebind");
|
||||
ASSERT (ip4_config != NULL,
|
||||
"dhcp-rfc3442-gateway", "failed to parse DHCP4 options");
|
||||
|
||||
|
|
@ -554,7 +554,7 @@ static Option escaped_searches_options[] = {
|
|||
};
|
||||
|
||||
static void
|
||||
test_escaped_domain_searches (void)
|
||||
test_escaped_domain_searches (const char *client)
|
||||
{
|
||||
GHashTable *options;
|
||||
NMIP4Config *ip4_config;
|
||||
|
|
@ -565,7 +565,7 @@ test_escaped_domain_searches (void)
|
|||
options = fill_table (generic_options, NULL);
|
||||
options = fill_table (escaped_searches_options, options);
|
||||
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config (client, "eth0", options, "rebind");
|
||||
ASSERT (ip4_config != NULL,
|
||||
"dhcp-escaped-domain-searches", "failed to parse DHCP4 options");
|
||||
|
||||
|
|
@ -588,7 +588,7 @@ static Option invalid_escaped_searches_options[] = {
|
|||
};
|
||||
|
||||
static void
|
||||
test_invalid_escaped_domain_searches (void)
|
||||
test_invalid_escaped_domain_searches (const char *client)
|
||||
{
|
||||
GHashTable *options;
|
||||
NMIP4Config *ip4_config;
|
||||
|
|
@ -596,7 +596,7 @@ test_invalid_escaped_domain_searches (void)
|
|||
options = fill_table (generic_options, NULL);
|
||||
options = fill_table (invalid_escaped_searches_options, options);
|
||||
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
|
||||
ip4_config = nm_dhcp_manager_test_ip4_options_to_config (client, "eth0", options, "rebind");
|
||||
ASSERT (ip4_config != NULL,
|
||||
"dhcp-invalid-escaped-domain-searches", "failed to parse DHCP4 options");
|
||||
|
||||
|
|
@ -607,6 +607,9 @@ test_invalid_escaped_domain_searches (void)
|
|||
g_hash_table_destroy (options);
|
||||
}
|
||||
|
||||
#define DHCLIENT "dhclient"
|
||||
#define DHCPCD "dhcpcd"
|
||||
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
GError *error = NULL;
|
||||
|
|
@ -620,15 +623,29 @@ int main (int argc, char **argv)
|
|||
FAIL ("nm-utils-init", "failed to initialize libnm-util: %s", error->message);
|
||||
|
||||
/* The tests */
|
||||
test_generic_options ();
|
||||
test_wins_options ();
|
||||
test_classless_static_routes ();
|
||||
test_invalid_classless_routes1 ();
|
||||
test_invalid_classless_routes2 ();
|
||||
test_invalid_classless_routes3 ();
|
||||
test_gateway_in_classless_routes ();
|
||||
test_escaped_domain_searches ();
|
||||
test_invalid_escaped_domain_searches ();
|
||||
if (strlen (DHCLIENT_PATH)) {
|
||||
test_generic_options (DHCLIENT);
|
||||
test_wins_options (DHCLIENT);
|
||||
test_classless_static_routes (DHCLIENT);
|
||||
test_invalid_classless_routes1 (DHCLIENT);
|
||||
test_invalid_classless_routes2 (DHCLIENT);
|
||||
test_invalid_classless_routes3 (DHCLIENT);
|
||||
test_gateway_in_classless_routes (DHCLIENT);
|
||||
test_escaped_domain_searches (DHCLIENT);
|
||||
test_invalid_escaped_domain_searches (DHCLIENT);
|
||||
}
|
||||
|
||||
if (strlen (DHCPCD_PATH)) {
|
||||
test_generic_options (DHCPCD);
|
||||
test_wins_options (DHCPCD);
|
||||
test_classless_static_routes (DHCPCD);
|
||||
test_invalid_classless_routes1 (DHCPCD);
|
||||
test_invalid_classless_routes2 (DHCPCD);
|
||||
test_invalid_classless_routes3 (DHCPCD);
|
||||
test_gateway_in_classless_routes (DHCPCD);
|
||||
test_escaped_domain_searches (DHCPCD);
|
||||
test_invalid_escaped_domain_searches (DHCPCD);
|
||||
}
|
||||
|
||||
base = g_path_get_basename (argv[0]);
|
||||
fprintf (stdout, "%s: SUCCESS\n", base);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue