diff --git a/src/dhcp-manager/nm-dhcp-dhclient.c b/src/dhcp-manager/nm-dhcp-dhclient.c index 478ac298f0..e0deabb4a8 100644 --- a/src/dhcp-manager/nm-dhcp-dhclient.c +++ b/src/dhcp-manager/nm-dhcp-dhclient.c @@ -15,7 +15,7 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * Copyright (C) 2005 - 2008 Red Hat, Inc. + * Copyright (C) 2005 - 2010 Red Hat, Inc. */ #define _XOPEN_SOURCE @@ -119,7 +119,7 @@ add_lease_option (GHashTable *hash, char *line) } GSList * -nm_dhcp_client_get_lease_ip4_config (const char *iface, const char *uuid) +nm_dhcp4_client_get_lease_config (const char *iface, const char *uuid) { GSList *parsed = NULL, *iter, *leases = NULL; char *contents = NULL; @@ -288,7 +288,7 @@ out: #define DHCP_HOSTNAME_FORMAT DHCP_HOSTNAME_TAG " \"%s\"; # added by NetworkManager" static gboolean -merge_dhclient_config (NMDHCPDevice *device, +merge_dhclient_config (NMDHCPClient *client, NMSettingIP4Config *s_ip4, guint8 *anycast_addr, const char *contents, @@ -298,8 +298,8 @@ merge_dhclient_config (NMDHCPDevice *device, GString *new_contents; gboolean success = FALSE; - g_return_val_if_fail (device != NULL, FALSE); - g_return_val_if_fail (device->iface != NULL, FALSE); + g_return_val_if_fail (client != NULL, FALSE); + g_return_val_if_fail (client->iface != NULL, FALSE); new_contents = g_string_new (_("# Created by NetworkManager\n")); @@ -374,13 +374,13 @@ merge_dhclient_config (NMDHCPDevice *device, " initial-interval 1; \n" " anycast-mac ethernet %02x:%02x:%02x:%02x:%02x:%02x;\n" "}\n", - device->iface, + client->iface, anycast_addr[0], anycast_addr[1], anycast_addr[2], anycast_addr[3], anycast_addr[4], anycast_addr[5]); } - if (g_file_set_contents (device->conf_file, new_contents->str, -1, error)) + if (g_file_set_contents (client->conf_file, new_contents->str, -1, error)) success = TRUE; g_string_free (new_contents, TRUE); @@ -394,7 +394,7 @@ merge_dhclient_config (NMDHCPDevice *device, * config file along with the NM options. */ static gboolean -create_dhclient_config (NMDHCPDevice *device, +create_dhclient_config (NMDHCPClient *client, NMSettingIP4Config *s_ip4, guint8 *dhcp_anycast_addr) { @@ -403,7 +403,8 @@ create_dhclient_config (NMDHCPDevice *device, gboolean success = FALSE; char *tmp; - g_return_val_if_fail (device != NULL, FALSE); + g_return_val_if_fail (client != NULL, FALSE); + g_return_val_if_fail (client->iface != NULL, FALSE); #if defined(TARGET_SUSE) orig = g_strdup (SYSCONFDIR "/dhclient.conf"); @@ -412,16 +413,16 @@ create_dhclient_config (NMDHCPDevice *device, #elif defined(TARGET_GENTOO) orig = g_strdup (SYSCONFDIR "/dhcp/dhclient.conf"); #else - orig = g_strdup_printf (SYSCONFDIR "/dhclient-%s.conf", device->iface); + orig = g_strdup_printf (SYSCONFDIR "/dhclient-%s.conf", client->iface); #endif if (!orig) { - nm_warning ("%s: not enough memory for dhclient options.", device->iface); + nm_warning ("%s: not enough memory for dhclient options.", client->iface); return FALSE; } - tmp = g_strdup_printf ("nm-dhclient-%s.conf", device->iface); - device->conf_file = g_build_filename ("/var", "run", tmp, NULL); + tmp = g_strdup_printf ("nm-dhclient-%s.conf", client->iface); + client->conf_file = g_build_filename ("/var", "run", tmp, NULL); g_free (tmp); if (!g_file_test (orig, G_FILE_TEST_EXISTS)) @@ -429,18 +430,18 @@ create_dhclient_config (NMDHCPDevice *device, if (!g_file_get_contents (orig, &contents, NULL, &error)) { nm_warning ("%s: error reading dhclient configuration %s: %s", - device->iface, orig, error->message); + client->iface, orig, error->message); g_error_free (error); goto out; } out: error = NULL; - if (merge_dhclient_config (device, s_ip4, dhcp_anycast_addr, contents, orig, &error)) + if (merge_dhclient_config (client, s_ip4, dhcp_anycast_addr, contents, orig, &error)) success = TRUE; else { nm_warning ("%s: error creating dhclient configuration: %s", - device->iface, error->message); + client->iface, error->message); g_error_free (error); } @@ -460,12 +461,12 @@ dhclient_child_setup (gpointer user_data G_GNUC_UNUSED) GPid -nm_dhcp_client_start (NMDHCPDevice *device, - const char *uuid, - NMSettingIP4Config *s_ip4, - guint8 *dhcp_anycast_addr) +nm_dhcp4_client_start (NMDHCPClient *client, + const char *uuid, + NMSettingIP4Config *s_ip4, + guint8 *dhcp_anycast_addr) { - GPtrArray *dhclient_argv = NULL; + GPtrArray *argv = NULL; GPid pid = 0; GError *error = NULL; char *pid_contents = NULL; @@ -475,51 +476,51 @@ nm_dhcp_client_start (NMDHCPDevice *device, goto out; } - device->pid_file = get_pidfile_for_iface (device->iface); - if (!device->pid_file) { - nm_warning ("%s: not enough memory for dhclient options.", device->iface); + client->pid_file = get_pidfile_for_iface (client->iface); + if (!client->pid_file) { + nm_warning ("%s: not enough memory for dhclient options.", client->iface); goto out; } - device->lease_file = get_leasefile_for_iface (device->iface, uuid); - if (!device->lease_file) { - nm_warning ("%s: not enough memory for dhclient options.", device->iface); + client->lease_file = get_leasefile_for_iface (client->iface, uuid); + if (!client->lease_file) { + nm_warning ("%s: not enough memory for dhclient options.", client->iface); goto out; } - if (!create_dhclient_config (device, s_ip4, dhcp_anycast_addr)) + if (!create_dhclient_config (client, s_ip4, dhcp_anycast_addr)) goto out; /* Kill any existing dhclient bound to this interface */ - if (g_file_get_contents (device->pid_file, &pid_contents, NULL, NULL)) { + if (g_file_get_contents (client->pid_file, &pid_contents, NULL, NULL)) { unsigned long int tmp = strtoul (pid_contents, NULL, 10); if (!((tmp == ULONG_MAX) && (errno == ERANGE))) - nm_dhcp_client_stop (device, (pid_t) tmp); - remove (device->pid_file); + nm_dhcp_client_stop (client, (pid_t) tmp); + remove (client->pid_file); } - dhclient_argv = g_ptr_array_new (); - g_ptr_array_add (dhclient_argv, (gpointer) DHCP_CLIENT_PATH); + argv = g_ptr_array_new (); + g_ptr_array_add (argv, (gpointer) DHCP_CLIENT_PATH); - g_ptr_array_add (dhclient_argv, (gpointer) "-d"); + g_ptr_array_add (argv, (gpointer) "-d"); - g_ptr_array_add (dhclient_argv, (gpointer) "-sf"); /* Set script file */ - g_ptr_array_add (dhclient_argv, (gpointer) ACTION_SCRIPT_PATH ); + g_ptr_array_add (argv, (gpointer) "-sf"); /* Set script file */ + g_ptr_array_add (argv, (gpointer) ACTION_SCRIPT_PATH ); - g_ptr_array_add (dhclient_argv, (gpointer) "-pf"); /* Set pid file */ - g_ptr_array_add (dhclient_argv, (gpointer) device->pid_file); + g_ptr_array_add (argv, (gpointer) "-pf"); /* Set pid file */ + g_ptr_array_add (argv, (gpointer) client->pid_file); - g_ptr_array_add (dhclient_argv, (gpointer) "-lf"); /* Set lease file */ - g_ptr_array_add (dhclient_argv, (gpointer) device->lease_file); + g_ptr_array_add (argv, (gpointer) "-lf"); /* Set lease file */ + g_ptr_array_add (argv, (gpointer) client->lease_file); - g_ptr_array_add (dhclient_argv, (gpointer) "-cf"); /* Set interface config file */ - g_ptr_array_add (dhclient_argv, (gpointer) device->conf_file); + g_ptr_array_add (argv, (gpointer) "-cf"); /* Set interface config file */ + g_ptr_array_add (argv, (gpointer) client->conf_file); - g_ptr_array_add (dhclient_argv, (gpointer) device->iface); - g_ptr_array_add (dhclient_argv, NULL); + g_ptr_array_add (argv, (gpointer) client->iface); + g_ptr_array_add (argv, NULL); - if (!g_spawn_async (NULL, (char **) dhclient_argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD, + if (!g_spawn_async (NULL, (char **) argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD, &dhclient_child_setup, NULL, &pid, &error)) { nm_warning ("dhclient failed to start. error: '%s'", error->message); g_error_free (error); @@ -530,7 +531,7 @@ nm_dhcp_client_start (NMDHCPDevice *device, out: g_free (pid_contents); - g_ptr_array_free (dhclient_argv, TRUE); + g_ptr_array_free (argv, TRUE); return pid; } @@ -596,9 +597,9 @@ error: } gboolean -nm_dhcp_client_process_classless_routes (GHashTable *options, - NMIP4Config *ip4_config, - guint32 *gwaddr) +nm_dhcp4_client_process_classless_routes (GHashTable *options, + NMIP4Config *ip4_config, + guint32 *gwaddr) { const char *str; char **octets, **o; diff --git a/src/dhcp-manager/nm-dhcp-dhcpcd.c b/src/dhcp-manager/nm-dhcp-dhcpcd.c index a8d929a528..6ba3f0a91c 100644 --- a/src/dhcp-manager/nm-dhcp-dhcpcd.c +++ b/src/dhcp-manager/nm-dhcp-dhcpcd.c @@ -50,7 +50,7 @@ get_pidfile_for_iface (const char * iface) } GSList * -nm_dhcp_client_get_lease_ip4_config (const char *iface, const char *uuid) +nm_dhcp4_client_get_lease_config (const char *iface, const char *uuid) { return NULL; } @@ -65,10 +65,10 @@ dhcpcd_child_setup (gpointer user_data G_GNUC_UNUSED) GPid -nm_dhcp_client_start (NMDHCPDevice *device, - const char *uuid, - NMSettingIP4Config *s_ip4, - guint8 *dhcp_anycast_addr) +nm_dhcp4_client_start (NMDHCPClient *client, + const char *uuid, + NMSettingIP4Config *s_ip4, + guint8 *dhcp_anycast_addr) { GPtrArray *argv = NULL; GPid pid = 0; @@ -80,19 +80,19 @@ nm_dhcp_client_start (NMDHCPDevice *device, goto out; } - device->pid_file = get_pidfile_for_iface (device->iface); - if (!device->pid_file) { - nm_warning ("%s: not enough memory for dhcpcd options.", device->iface); + client->pid_file = get_pidfile_for_iface (client->iface); + if (!client->pid_file) { + nm_warning ("%s: not enough memory for dhcpcd options.", client->iface); goto out; } /* Kill any existing dhcpcd bound to this interface */ - if (g_file_get_contents (device->pid_file, &pid_contents, NULL, NULL)) { + if (g_file_get_contents (client->pid_file, &pid_contents, NULL, NULL)) { unsigned long int tmp = strtoul (pid_contents, NULL, 10); if (!((tmp == ULONG_MAX) && (errno == ERANGE))) - nm_dhcp_client_stop (device, (pid_t) tmp); - remove (device->pid_file); + nm_dhcp_client_stop (client, (pid_t) tmp); + remove (client->pid_file); } argv = g_ptr_array_new (); @@ -107,7 +107,7 @@ nm_dhcp_client_start (NMDHCPDevice *device, g_ptr_array_add (argv, (gpointer) "-c"); /* Set script file */ g_ptr_array_add (argv, (gpointer) ACTION_SCRIPT_PATH ); - g_ptr_array_add (argv, (gpointer) device->iface); + g_ptr_array_add (argv, (gpointer) client->iface); g_ptr_array_add (argv, NULL); if (!g_spawn_async (NULL, (char **) argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD, @@ -126,9 +126,9 @@ out: } gboolean -nm_dhcp_client_process_classless_routes (GHashTable *options, - NMIP4Config *ip4_config, - guint32 *gwaddr) +nm_dhcp4_client_process_classless_routes (GHashTable *options, + NMIP4Config *ip4_config, + guint32 *gwaddr) { const char *str; char **routes, **r; diff --git a/src/dhcp-manager/nm-dhcp-manager.c b/src/dhcp-manager/nm-dhcp-manager.c index 8942b01d2b..2b48a2651d 100644 --- a/src/dhcp-manager/nm-dhcp-manager.c +++ b/src/dhcp-manager/nm-dhcp-manager.c @@ -15,7 +15,7 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * Copyright (C) 2005 - 2008 Red Hat, Inc. + * Copyright (C) 2005 - 2010 Red Hat, Inc. * Copyright (C) 2006 - 2008 Novell, Inc. * */ @@ -50,9 +50,10 @@ #define NM_DHCP_TIMEOUT 45 /* DHCP timeout, in seconds */ typedef struct { + guint32 next_id; NMDBusManager * dbus_mgr; - GHashTable * devices; - DBusGProxy * proxy; + GHashTable * clients; + DBusGProxy * proxy; NMHostnameProvider *hostname_provider; } NMDHCPManagerPrivate; @@ -69,82 +70,9 @@ enum { static guint signals[LAST_SIGNAL] = { 0 }; -static NMDHCPManager *nm_dhcp_manager_new (void); +static void nm_dhcp_manager_cancel_transaction_real (NMDHCPClient *client); -static void nm_dhcp_manager_cancel_transaction_real (NMDHCPDevice *device); - -static void hostname_provider_destroyed (gpointer data, GObject *destroyed_object); - -NMDHCPManager * -nm_dhcp_manager_get (void) -{ - static NMDHCPManager *singleton = NULL; - - if (!singleton) - singleton = nm_dhcp_manager_new (); - else - g_object_ref (singleton); - - g_assert (singleton); - return singleton; -} - -static void -nm_dhcp_manager_init (NMDHCPManager *manager) -{ -} - -static void -finalize (GObject *object) -{ - NMDHCPManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (object); - - if (priv->hostname_provider) { - g_object_weak_unref (G_OBJECT (priv->hostname_provider), hostname_provider_destroyed, object); - priv->hostname_provider = NULL; - } - - g_hash_table_destroy (priv->devices); - g_object_unref (priv->proxy); - g_object_unref (priv->dbus_mgr); - - G_OBJECT_CLASS (nm_dhcp_manager_parent_class)->finalize (object); -} - -static void -nm_dhcp_manager_class_init (NMDHCPManagerClass *manager_class) -{ - GObjectClass *object_class = G_OBJECT_CLASS (manager_class); - - g_type_class_add_private (manager_class, sizeof (NMDHCPManagerPrivate)); - - /* virtual methods */ - object_class->finalize = finalize; - - /* signals */ - signals[STATE_CHANGED] = - g_signal_new ("state-changed", - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET (NMDHCPManagerClass, state_changed), - NULL, NULL, - _nm_marshal_VOID__STRING_UCHAR, - G_TYPE_NONE, 2, - G_TYPE_STRING, - G_TYPE_UCHAR); - - signals[TIMEOUT] = - g_signal_new ("timeout", - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET (NMDHCPManagerClass, timeout), - NULL, NULL, - g_cclosure_marshal_VOID__STRING, - G_TYPE_NONE, 1, - G_TYPE_STRING); -} - -static gboolean state_is_bound (guint8 state) +static gboolean state_is_bound (guint32 state) { if ( (state == DHC_BOUND4) || (state == DHC_BOUND6) @@ -161,46 +89,46 @@ static gboolean state_is_bound (guint8 state) static void -nm_dhcp_device_timeout_cleanup (NMDHCPDevice * device) +nm_dhcp_client_timeout_cleanup (NMDHCPClient *client) { - if (device->timeout_id) { - g_source_remove (device->timeout_id); - device->timeout_id = 0; + if (client->timeout_id) { + g_source_remove (client->timeout_id); + client->timeout_id = 0; } } static void -nm_dhcp_device_watch_cleanup (NMDHCPDevice * device) +nm_dhcp_client_watch_cleanup (NMDHCPClient *client) { - if (device->watch_id) { - g_source_remove (device->watch_id); - device->watch_id = 0; + if (client->watch_id) { + g_source_remove (client->watch_id); + client->watch_id = 0; } } static void -nm_dhcp_device_destroy (NMDHCPDevice *device) +nm_dhcp_client_destroy (NMDHCPClient *client) { int ignored; - nm_dhcp_device_timeout_cleanup (device); + nm_dhcp_client_timeout_cleanup (client); - if (device->pid) - nm_dhcp_client_stop (device, device->pid); + if (client->pid) + nm_dhcp_client_stop (client, client->pid); - if (device->options) - g_hash_table_destroy (device->options); + if (client->options) + g_hash_table_destroy (client->options); - if (device->conf_file) { - ignored = unlink (device->conf_file); - g_free (device->conf_file); + if (client->conf_file) { + ignored = unlink (client->conf_file); + g_free (client->conf_file); } - g_free (device->pid_file); - g_free (device->lease_file); - g_free (device->iface); + g_free (client->pid_file); + g_free (client->lease_file); + g_free (client->iface); - g_slice_free (NMDHCPDevice, device); + g_slice_free (NMDHCPClient, client); } typedef struct { @@ -316,7 +244,7 @@ copy_option (gpointer key, gpointer value, gpointer user_data) { - NMDHCPDevice * device = (NMDHCPDevice *) user_data; + NMDHCPClient *client = user_data; const char *str_key = (const char *) key; char *str_value = NULL; @@ -329,21 +257,21 @@ copy_option (gpointer key, str_value = garray_to_string ((GArray *) g_value_get_boxed (value), str_key); if (str_value) - g_hash_table_insert (device->options, g_strdup (str_key), str_value); + g_hash_table_insert (client->options, g_strdup (str_key), str_value); } static void -handle_options (NMDHCPManager * manager, - NMDHCPDevice * device, - GHashTable * options, - const char * reason) +handle_options (NMDHCPManager *manager, + NMDHCPClient *client, + GHashTable *options, + const char *reason) { - guint32 old_state = device->state; + guint32 old_state = client->state; guint32 new_state = string_to_state (reason); /* Clear old and save new DHCP options */ - g_hash_table_remove_all (device->options); - g_hash_table_foreach (options, copy_option, device); + g_hash_table_remove_all (client->options); + g_hash_table_foreach (options, copy_option, client); if (old_state == new_state) return; @@ -351,20 +279,78 @@ handle_options (NMDHCPManager * manager, /* Handle changed device state */ if (state_is_bound (new_state)) { /* Cancel the timeout if the DHCP client is now bound */ - nm_dhcp_device_timeout_cleanup (device); + nm_dhcp_client_timeout_cleanup (client); } - device->state = new_state; + client->state = new_state; nm_info ("DHCP: device %s state changed %s -> %s", - device->iface, + client->iface, state_to_string (old_state), - state_to_string (device->state)); + state_to_string (client->state)); - g_signal_emit (G_OBJECT (device->manager), + g_signal_emit (G_OBJECT (client->manager), signals[STATE_CHANGED], 0, - device->iface, - device->state); + client->id, + client->state); +} + +static NMDHCPClient * +get_client_for_pid (NMDHCPManager *manager, GPid pid) +{ + NMDHCPManagerPrivate *priv; + GHashTableIter iter; + gpointer value; + + g_return_val_if_fail (manager != NULL, NULL); + g_return_val_if_fail (NM_IS_DHCP_MANAGER (manager), NULL); + + priv = NM_DHCP_MANAGER_GET_PRIVATE (manager); + + g_hash_table_iter_init (&iter, priv->clients); + while (g_hash_table_iter_next (&iter, NULL, &value)) { + NMDHCPClient *candidate = value; + + if (candidate->pid == pid) + return candidate; + } + + return NULL; +} + +static NMDHCPClient * +get_client_for_iface (NMDHCPManager *manager, const char *iface) +{ + NMDHCPManagerPrivate *priv; + GHashTableIter iter; + gpointer value; + + g_return_val_if_fail (manager != NULL, NULL); + g_return_val_if_fail (NM_IS_DHCP_MANAGER (manager), NULL); + g_return_val_if_fail (iface, NULL); + + priv = NM_DHCP_MANAGER_GET_PRIVATE (manager); + + g_hash_table_iter_init (&iter, priv->clients); + while (g_hash_table_iter_next (&iter, NULL, &value)) { + NMDHCPClient *candidate = value; + + if (!strcmp (iface, candidate->iface)) + return candidate; + } + + return NULL; +} + +static NMDHCPClient * +get_client_for_id (NMDHCPManager *manager, guint32 id) +{ + g_return_val_if_fail (manager != NULL, NULL); + g_return_val_if_fail (NM_IS_DHCP_MANAGER (manager), NULL); + g_return_val_if_fail (id > 0, NULL); + + return g_hash_table_lookup (NM_DHCP_MANAGER_GET_PRIVATE (manager)->clients, + GUINT_TO_POINTER (id)); } static void @@ -372,14 +358,13 @@ nm_dhcp_manager_handle_event (DBusGProxy *proxy, GHashTable *options, gpointer user_data) { - NMDHCPManager * manager; - NMDHCPManagerPrivate * priv; - NMDHCPDevice * device; - char * iface = NULL; - char * pid_str = NULL; - char * reason = NULL; + NMDHCPManager *manager; + NMDHCPManagerPrivate *priv; + NMDHCPClient *client; + char *iface = NULL; + char *pid_str = NULL; + char *reason = NULL; unsigned long temp; - pid_t pid; manager = NM_DHCP_MANAGER (user_data); priv = NM_DHCP_MANAGER_GET_PRIVATE (manager); @@ -390,29 +375,27 @@ nm_dhcp_manager_handle_event (DBusGProxy *proxy, goto out; } - device = (NMDHCPDevice *) g_hash_table_lookup (priv->devices, iface); - if (device == NULL) { - nm_warning ("Unhandled DHCP event for interface %s", iface); - goto out; - } - pid_str = get_option (options, "pid"); if (pid_str == NULL) { nm_warning ("DHCP event didn't have associated PID."); goto out; } - temp = strtoul(pid_str, NULL, 10); + temp = strtoul (pid_str, NULL, 10); if ((temp == ULONG_MAX) && (errno == ERANGE)) { nm_warning ("Couldn't convert PID"); goto out; } - pid = (pid_t) temp; - if (pid != device->pid) { - nm_warning ("Received DHCP event from unexpected PID %u (expected %u)", - pid, - device->pid); + client = get_client_for_pid (manager, (GPid) temp); + if (client == NULL) { + nm_warning ("Unhandled DHCP event for interface %s", iface); + goto out; + } + + if (strcmp (iface, client->iface)) { + nm_warning ("Received DHCP event from unexpected interface '%s' (expected '%s')", + iface, client->iface); goto out; } @@ -422,7 +405,7 @@ nm_dhcp_manager_handle_event (DBusGProxy *proxy, goto out; } - handle_options (manager, device, options, reason); + handle_options (manager, client, options, reason); out: g_free (iface); @@ -435,20 +418,18 @@ nm_dhcp_manager_new (void) { NMDHCPManager *manager; NMDHCPManagerPrivate *priv; - DBusGConnection * g_connection; + DBusGConnection *g_connection; manager = g_object_new (NM_TYPE_DHCP_MANAGER, NULL); priv = NM_DHCP_MANAGER_GET_PRIVATE (manager); - priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal, - NULL, - (GDestroyNotify) nm_dhcp_device_destroy); - if (!priv->devices) { - nm_warning ("Error: not enough memory to initialize DHCP manager " - "tables"); + priv->clients = g_hash_table_new_full (g_direct_hash, g_direct_equal, + NULL, + (GDestroyNotify) nm_dhcp_client_destroy); + if (!priv->clients) { + nm_warning ("Error: not enough memory to initialize DHCP manager tables"); g_object_unref (manager); - manager = NULL; - goto out; + return NULL; } priv->dbus_mgr = nm_dbus_manager_get (); @@ -460,6 +441,7 @@ nm_dhcp_manager_new (void) if (!priv->proxy) { nm_warning ("Error: could not init DHCP manager proxy"); g_object_unref (manager); + return NULL; } dbus_g_proxy_add_signal (priv->proxy, @@ -472,7 +454,6 @@ nm_dhcp_manager_new (void) manager, NULL); -out: return manager; } @@ -486,56 +467,54 @@ out: static gboolean nm_dhcp_manager_handle_timeout (gpointer user_data) { - NMDHCPDevice *device = (NMDHCPDevice *) user_data; + NMDHCPClient *client = user_data; - nm_info ("(%s): DHCP transaction took too long, stopping it.", device->iface); + nm_info ("(%s): DHCP transaction took too long, stopping it.", client->iface); - nm_dhcp_manager_cancel_transaction (device->manager, device->iface); + nm_dhcp_manager_cancel_transaction (client->manager, client->id); - g_signal_emit (G_OBJECT (device->manager), signals[TIMEOUT], 0, device->iface); + g_signal_emit (G_OBJECT (client->manager), signals[TIMEOUT], 0, client->id); return FALSE; } -static NMDHCPDevice * -nm_dhcp_device_new (NMDHCPManager *manager, const char *iface) +static NMDHCPClient * +nm_dhcp_client_new (NMDHCPManager *manager, const char *iface) { - NMDHCPDevice *device; - GHashTable * hash = NM_DHCP_MANAGER_GET_PRIVATE (manager)->devices; + NMDHCPManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (manager); + NMDHCPClient *client; - device = g_slice_new0 (NMDHCPDevice); - if (!device) { + client = g_slice_new0 (NMDHCPClient); + if (!client) { nm_warning ("%s: Out of memory creating DHCP transaction object.", iface); return NULL; } - device->iface = g_strdup (iface); - if (!device) { + client->id = priv->next_id++; + client->iface = g_strdup (iface); + if (!client) { nm_warning ("%s: Out of memory creating DHCP transaction object " "property 'iface'.", iface); goto error; } - device->manager = manager; + client->manager = manager; /* Do this after the transaction cancel since that clears options out */ - device->options = g_hash_table_new_full (g_str_hash, - g_str_equal, - g_free, - g_free); - if (!device->options) { + client->options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + if (!client->options) { nm_warning ("%s: Out of memory creating DHCP transaction object " "property 'options'.", iface); goto error; } - g_hash_table_insert (hash, device->iface, device); - return device; + g_hash_table_insert (priv->clients, GUINT_TO_POINTER (client->id), client); + return client; error: - nm_dhcp_device_destroy (device); + nm_dhcp_client_destroy (client); return NULL; } @@ -548,21 +527,21 @@ error: */ static void dhcp_watch_cb (GPid pid, gint status, gpointer user_data) { - NMDHCPDevice *device = (NMDHCPDevice *)user_data; + NMDHCPClient *client = user_data; if (!WIFEXITED (status)) { - device->state = DHC_ABEND; + client->state = DHC_ABEND; nm_warning ("dhcp client died abnormally"); } - device->pid = 0; + client->pid = 0; - nm_dhcp_device_watch_cleanup (device); - nm_dhcp_device_timeout_cleanup (device); + nm_dhcp_client_watch_cleanup (client); + nm_dhcp_client_timeout_cleanup (client); - g_signal_emit (G_OBJECT (device->manager), signals[STATE_CHANGED], 0, device->iface, device->state); + g_signal_emit (G_OBJECT (client->manager), signals[STATE_CHANGED], 0, client->id, client->state); } -gboolean +guint32 nm_dhcp_manager_begin_transaction (NMDHCPManager *manager, const char *iface, const char *uuid, @@ -571,21 +550,22 @@ nm_dhcp_manager_begin_transaction (NMDHCPManager *manager, guint8 *dhcp_anycast_addr) { NMDHCPManagerPrivate *priv; - NMDHCPDevice *device; + NMDHCPClient *client; NMSettingIP4Config *setting; - g_return_val_if_fail (NM_IS_DHCP_MANAGER (manager), FALSE); - g_return_val_if_fail (iface != NULL, FALSE); + g_return_val_if_fail (manager, 0); + g_return_val_if_fail (NM_IS_DHCP_MANAGER (manager), 0); + g_return_val_if_fail (iface != NULL, 0); priv = NM_DHCP_MANAGER_GET_PRIVATE (manager); - device = (NMDHCPDevice *) g_hash_table_lookup (priv->devices, iface); - if (!device) - device = nm_dhcp_device_new (manager, iface); + client = get_client_for_iface (manager, iface); + if (!client) + client = nm_dhcp_client_new (manager, iface); - if (device->pid && (device->state < DHC_ABEND)) { + if (client->pid && (client->state < DHC_ABEND)) { /* Cancel any DHCP transaction already in progress */ - nm_dhcp_manager_cancel_transaction_real (device); + nm_dhcp_manager_cancel_transaction_real (client); } if (s_ip4 && @@ -601,44 +581,42 @@ nm_dhcp_manager_begin_transaction (NMDHCPManager *manager, NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME, nm_hostname_provider_get_hostname (priv->hostname_provider), NULL); - } else { + } else setting = s_ip4 ? g_object_ref (s_ip4) : NULL; - } if (timeout == 0) timeout = NM_DHCP_TIMEOUT; nm_info ("Activation (%s) Beginning DHCP transaction (timeout in %d seconds)", iface, timeout); - device->pid = nm_dhcp_client_start (device, uuid, setting, dhcp_anycast_addr); + client->pid = nm_dhcp4_client_start (client, uuid, setting, dhcp_anycast_addr); if (setting) g_object_unref (setting); - if (device->pid == 0) + if (client->pid == 0) return FALSE; /* Set up a timeout on the transaction to kill it after the timeout */ - device->timeout_id = g_timeout_add_seconds (timeout, + client->timeout_id = g_timeout_add_seconds (timeout, nm_dhcp_manager_handle_timeout, - device); - device->watch_id = g_child_watch_add (device->pid, - (GChildWatchFunc) dhcp_watch_cb, - device); - return TRUE; + client); + client->watch_id = g_child_watch_add (client->pid, + (GChildWatchFunc) dhcp_watch_cb, + client); + return client->id; } void -nm_dhcp_client_stop (NMDHCPDevice *device, pid_t pid) +nm_dhcp_client_stop (NMDHCPClient *client, GPid pid) { int i = 15; /* 3 seconds */ + g_return_if_fail (client != NULL); g_return_if_fail (pid > 0); - /* Clean up the watch handler since we're explicitly killing - * the daemon - */ - nm_dhcp_device_watch_cleanup (device); + /* Clean up the watch handler since we're explicitly killing the daemon */ + nm_dhcp_client_watch_cleanup (client); /* Tell it to quit; maybe it wants to send out a RELEASE message */ kill (pid, SIGTERM); @@ -663,7 +641,7 @@ nm_dhcp_client_stop (NMDHCPDevice *device, pid_t pid) } if (i <= 0) { - nm_warning ("%s: dhcp client pid %d didn't exit, will kill it.", device->iface, pid); + nm_warning ("%s: dhcp client pid %d didn't exit, will kill it.", client->iface, pid); kill (pid, SIGKILL); nm_debug ("waiting for dhcp client pid %d to exit", pid); @@ -673,41 +651,42 @@ nm_dhcp_client_stop (NMDHCPDevice *device, pid_t pid) } static void -nm_dhcp_manager_cancel_transaction_real (NMDHCPDevice *device) +nm_dhcp_manager_cancel_transaction_real (NMDHCPClient *client) { - g_return_if_fail (device->pid > 0); + g_return_if_fail (client != NULL); + g_return_if_fail (client->pid > 0); - nm_dhcp_client_stop (device, device->pid); + nm_dhcp_client_stop (client, client->pid); nm_info ("(%s): canceled DHCP transaction, dhcp client pid %d", - device->iface, - device->pid); + client->iface, + client->pid); - device->pid = 0; - device->state = DHC_END; + client->pid = 0; + client->state = DHC_END; /* Clean up the pidfile if it got left around */ - if (device->pid_file) { - remove (device->pid_file); - g_free (device->pid_file); - device->pid_file = NULL; + if (client->pid_file) { + remove (client->pid_file); + g_free (client->pid_file); + client->pid_file = NULL; } /* Free leasefile (but don't delete) */ - if (device->lease_file) { - g_free (device->lease_file); - device->lease_file = NULL; + if (client->lease_file) { + g_free (client->lease_file); + client->lease_file = NULL; } /* Clean up config file if it got left around */ - if (device->conf_file) { - remove (device->conf_file); - g_free (device->conf_file); - device->conf_file = NULL; + if (client->conf_file) { + remove (client->conf_file); + g_free (client->conf_file); + client->conf_file = NULL; } - nm_dhcp_device_timeout_cleanup (device); - g_hash_table_remove_all (device->options); + nm_dhcp_client_timeout_cleanup (client); + g_hash_table_remove_all (client->options); } @@ -718,23 +697,20 @@ nm_dhcp_manager_cancel_transaction_real (NMDHCPDevice *device) * */ void -nm_dhcp_manager_cancel_transaction (NMDHCPManager *manager, - const char *iface) +nm_dhcp_manager_cancel_transaction (NMDHCPManager *manager, guint32 id) { - NMDHCPDevice *device; + NMDHCPClient *client; NMDHCPManagerPrivate *priv; + g_return_if_fail (manager != NULL); g_return_if_fail (NM_IS_DHCP_MANAGER (manager)); - g_return_if_fail (iface != NULL); + g_return_if_fail (id > 0); priv = NM_DHCP_MANAGER_GET_PRIVATE (manager); - device = (NMDHCPDevice *) g_hash_table_lookup (priv->devices, iface); - - if (!device || !device->pid) - return; - - nm_dhcp_manager_cancel_transaction_real (device); + client = get_client_for_id (manager, id); + if (client && client->pid) + nm_dhcp_manager_cancel_transaction_real (client); } static void @@ -823,7 +799,7 @@ out: /* Given a table of DHCP options from the client, convert into an IP4Config */ NMIP4Config * -nm_dhcp_manager_options_to_ip4_config (const char *iface, GHashTable *options) +nm_dhcp4_manager_options_to_config (const char *iface, GHashTable *options) { NMIP4Config *ip4_config = NULL; struct in_addr tmp_addr; @@ -863,7 +839,7 @@ nm_dhcp_manager_options_to_ip4_config (const char *iface, GHashTable *options) /* Routes: if the server returns classless static routes, we MUST ignore * the 'static_routes' option. */ - have_classless = nm_dhcp_client_process_classless_routes (options, ip4_config, &gwaddr); + have_classless = nm_dhcp4_client_process_classless_routes (options, ip4_config, &gwaddr); if (!have_classless) { gwaddr = 0; /* Ensure client code doesn't lie */ process_classful_routes (options, ip4_config); @@ -979,29 +955,29 @@ error: * */ NMIP4Config * -nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager, - const char *iface) +nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager, guint32 id) { NMDHCPManagerPrivate *priv; - NMDHCPDevice *device; + NMDHCPClient *client; + g_return_val_if_fail (manager != NULL, NULL); g_return_val_if_fail (NM_IS_DHCP_MANAGER (manager), NULL); - g_return_val_if_fail (iface != NULL, NULL); + g_return_val_if_fail (id > 0, NULL); priv = NM_DHCP_MANAGER_GET_PRIVATE (manager); - device = (NMDHCPDevice *) g_hash_table_lookup (priv->devices, iface); - if (!device) { - nm_warning ("Device '%s' transaction not started.", iface); + client = get_client_for_id (manager, id); + if (!client) { + nm_warning ("Device DHCP transaction %d not started.", id); return NULL; } - if (!state_is_bound (device->state)) { - nm_warning ("%s: dhcp client didn't bind to a lease.", device->iface); + if (!state_is_bound (client->state)) { + nm_warning ("%s: dhcp client didn't bind to a lease.", client->iface); return NULL; } - return nm_dhcp_manager_options_to_ip4_config (iface, device->options); + return nm_dhcp4_manager_options_to_config (client->iface, client->options); } #define NEW_TAG "new_" @@ -1044,34 +1020,35 @@ iterate_dhcp4_config_option (gpointer key, gboolean nm_dhcp_manager_foreach_dhcp4_option (NMDHCPManager *self, - const char *iface, + guint32 id, GHFunc func, gpointer user_data) { NMDHCPManagerPrivate *priv; - NMDHCPDevice *device; + NMDHCPClient *client; Dhcp4ForeachInfo info = { NULL, NULL }; + g_return_val_if_fail (self != NULL, FALSE); g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), FALSE); - g_return_val_if_fail (iface != NULL, FALSE); + g_return_val_if_fail (id > 0, FALSE); g_return_val_if_fail (func != NULL, FALSE); priv = NM_DHCP_MANAGER_GET_PRIVATE (self); - device = (NMDHCPDevice *) g_hash_table_lookup (priv->devices, iface); - if (!device) { - nm_warning ("Device '%s' transaction not started.", iface); + client = get_client_for_id (self, id); + if (!client) { + nm_warning ("Device DHCP transaction %d not started.", id); return FALSE; } - if (!state_is_bound (device->state)) { - nm_warning ("%s: dhclient didn't bind to a lease.", device->iface); + if (!state_is_bound (client->state)) { + nm_warning ("%s: dhclient didn't bind to a lease.", client->iface); return FALSE; } info.func = func; info.user_data = user_data; - g_hash_table_foreach (device->options, iterate_dhcp4_config_option, &info); + g_hash_table_foreach (client->options, iterate_dhcp4_config_option, &info); return TRUE; } @@ -1103,14 +1080,86 @@ nm_dhcp_manager_set_hostname_provider (NMDHCPManager *manager, } GSList * -nm_dhcp_manager_get_lease_ip4_config (NMDHCPManager *self, - const char *iface, - const char *uuid) +nm_dhcp4_manager_get_lease_config (NMDHCPManager *self, + const char *iface, + const char *uuid) { + g_return_val_if_fail (self != NULL, NULL); g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL); g_return_val_if_fail (iface != NULL, NULL); g_return_val_if_fail (uuid != NULL, NULL); - return nm_dhcp_client_get_lease_ip4_config (iface, uuid); + return nm_dhcp4_client_get_lease_config (iface, uuid); +} + +/***************************************************/ + +NMDHCPManager * +nm_dhcp_manager_get (void) +{ + static NMDHCPManager *singleton = NULL; + + if (!singleton) + singleton = nm_dhcp_manager_new (); + else + g_object_ref (singleton); + + g_assert (singleton); + return singleton; +} + +static void +nm_dhcp_manager_init (NMDHCPManager *manager) +{ + NMDHCPManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (manager); + + priv->next_id = 1; +} + +static void +finalize (GObject *object) +{ + NMDHCPManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (object); + + if (priv->hostname_provider) { + g_object_weak_unref (G_OBJECT (priv->hostname_provider), hostname_provider_destroyed, object); + priv->hostname_provider = NULL; + } + + g_hash_table_destroy (priv->clients); + g_object_unref (priv->proxy); + g_object_unref (priv->dbus_mgr); + + G_OBJECT_CLASS (nm_dhcp_manager_parent_class)->finalize (object); +} + +static void +nm_dhcp_manager_class_init (NMDHCPManagerClass *manager_class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (manager_class); + + g_type_class_add_private (manager_class, sizeof (NMDHCPManagerPrivate)); + + /* virtual methods */ + object_class->finalize = finalize; + + /* signals */ + signals[STATE_CHANGED] = + g_signal_new ("state-changed", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (NMDHCPManagerClass, state_changed), + NULL, NULL, + _nm_marshal_VOID__UINT_UINT, + G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT); + + signals[TIMEOUT] = + g_signal_new ("timeout", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (NMDHCPManagerClass, timeout), + NULL, NULL, + g_cclosure_marshal_VOID__UINT, + G_TYPE_NONE, 1, G_TYPE_UINT); } diff --git a/src/dhcp-manager/nm-dhcp-manager.h b/src/dhcp-manager/nm-dhcp-manager.h index 46d0ea4bc3..3b8a6a410e 100644 --- a/src/dhcp-manager/nm-dhcp-manager.h +++ b/src/dhcp-manager/nm-dhcp-manager.h @@ -15,7 +15,7 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * Copyright (C) 2005 - 2008 Red Hat, Inc. + * Copyright (C) 2005 - 2010 Red Hat, Inc. * Copyright (C) 2006 - 2008 Novell, Inc. */ @@ -51,6 +51,7 @@ typedef enum { DHC_REBOOT, /* have valid lease, but now obtained a different one */ DHC_REBIND4, /* IPv4 new/different lease */ DHC_REBIND6, /* IPv6 new/different lease */ + DHC_DEPREF6, /* IPv6 lease depreferred */ DHC_STOP, /* remove old lease */ DHC_MEDIUM, /* media selection begun */ DHC_TIMEOUT, /* timed out contacting DHCP server */ @@ -60,7 +61,6 @@ typedef enum { DHC_START, /* sent when dhclient started OK */ DHC_ABEND, /* dhclient exited abnormally */ DHC_END, /* dhclient exited normally */ - DHC_DEPREF6, /* IPv6 lease depreferred */ DHC_END_OPTIONS, /* last option in subscription sent */ } NMDHCPState; @@ -72,22 +72,23 @@ typedef struct { GObjectClass parent; /* Signals */ - void (*state_changed) (NMDHCPManager *manager, char *iface, NMDHCPState state); - void (*timeout) (NMDHCPManager *manager, char *iface); + void (*state_changed) (NMDHCPManager *manager, guint32 id, NMDHCPState state); + void (*timeout) (NMDHCPManager *manager, guint32 id); } NMDHCPManagerClass; typedef struct { - char * iface; - guchar state; - GPid pid; - char * pid_file; - char * conf_file; - char * lease_file; - guint timeout_id; - guint watch_id; - NMDHCPManager * manager; - GHashTable * options; -} NMDHCPDevice; + GPid id; + char * iface; + guchar state; + GPid pid; + char * pid_file; + char * conf_file; + char * lease_file; + guint timeout_id; + guint watch_id; + NMDHCPManager * manager; + GHashTable * options; +} NMDHCPClient; GType nm_dhcp_manager_get_type (void); @@ -95,42 +96,42 @@ NMDHCPManager *nm_dhcp_manager_get (void); void nm_dhcp_manager_set_hostname_provider(NMDHCPManager *manager, NMHostnameProvider *provider); -gboolean nm_dhcp_manager_begin_transaction (NMDHCPManager *manager, +guint32 nm_dhcp_manager_begin_transaction (NMDHCPManager *manager, const char *iface, const char *uuid, NMSettingIP4Config *s_ip4, guint32 timeout, guint8 *dhcp_anycast_addr); void nm_dhcp_manager_cancel_transaction (NMDHCPManager *manager, - const char *iface); -NMIP4Config * nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager, const char *iface); -NMDHCPState nm_dhcp_manager_get_state_for_device (NMDHCPManager *manager, const char *iface); + guint32 id); +NMIP4Config * nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager, guint32 id); +NMDHCPState nm_dhcp_manager_get_client_state (NMDHCPManager *manager, guint32 id); gboolean nm_dhcp_manager_foreach_dhcp4_option (NMDHCPManager *self, - const char *iface, + guint32 id, GHFunc func, gpointer user_data); -GSList * nm_dhcp_manager_get_lease_ip4_config (NMDHCPManager *self, +GSList * nm_dhcp4_manager_get_lease_config (NMDHCPManager *self, const char *iface, const char *uuid); /* The following are implemented by the DHCP client backends */ -GPid nm_dhcp_client_start (NMDHCPDevice *device, - const char *uuid, - NMSettingIP4Config *s_ip4, - guint8 *anycast_addr); -void nm_dhcp_client_stop (NMDHCPDevice *device, pid_t pid); +GPid nm_dhcp4_client_start (NMDHCPClient *client, + const char *uuid, + NMSettingIP4Config *s_ip4, + guint8 *anycast_addr); +void nm_dhcp_client_stop (NMDHCPClient *client, pid_t pid); -gboolean nm_dhcp_client_process_classless_routes (GHashTable *options, - NMIP4Config *ip4_config, - guint32 *gwaddr); +gboolean nm_dhcp4_client_process_classless_routes (GHashTable *options, + NMIP4Config *ip4_config, + guint32 *gwaddr); -GSList * nm_dhcp_client_get_lease_ip4_config (const char *iface, - const char *uuid); +GSList * nm_dhcp4_client_get_lease_config (const char *iface, + const char *uuid); /* Test functions */ -NMIP4Config *nm_dhcp_manager_options_to_ip4_config (const char *iface, - GHashTable *options); +NMIP4Config *nm_dhcp4_manager_options_to_config (const char *iface, + GHashTable *options); #endif /* NM_DHCP_MANAGER_H */ diff --git a/src/nm-device-ethernet.c b/src/nm-device-ethernet.c index 037d603380..72073e4150 100644 --- a/src/nm-device-ethernet.c +++ b/src/nm-device-ethernet.c @@ -15,7 +15,7 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * Copyright (C) 2005 - 2008 Red Hat, Inc. + * Copyright (C) 2005 - 2010 Red Hat, Inc. * Copyright (C) 2006 - 2008 Novell, Inc. */ @@ -1608,9 +1608,9 @@ ip4_match_config (NMDevice *self, NMConnection *connection) /* Get any saved leases that apply to this connection */ dhcp_mgr = nm_dhcp_manager_get (); - leases = nm_dhcp_manager_get_lease_ip4_config (dhcp_mgr, - nm_device_get_iface (self), - nm_setting_connection_get_uuid (s_con)); + leases = nm_dhcp4_manager_get_lease_config (dhcp_mgr, + nm_device_get_iface (self), + nm_setting_connection_get_uuid (s_con)); g_object_unref (dhcp_mgr); method = nm_setting_ip4_config_get_method (s_ip4); diff --git a/src/nm-device.c b/src/nm-device.c index 05c9598254..4be3fc23e3 100644 --- a/src/nm-device.c +++ b/src/nm-device.c @@ -15,7 +15,7 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * Copyright (C) 2005 - 2008 Red Hat, Inc. + * Copyright (C) 2005 - 2010 Red Hat, Inc. * Copyright (C) 2006 - 2008 Novell, Inc. */ @@ -105,6 +105,7 @@ typedef struct { /* IP4 configuration info */ NMIP4Config * ip4_config; /* Config from DHCP, PPP, or system config files */ NMDHCPManager * dhcp_manager; + guint32 dhcp4_id; guint32 dhcp_timeout; gulong dhcp_state_sigid; gulong dhcp_timeout_sigid; @@ -1090,7 +1091,6 @@ real_act_stage3_ip4_config_start (NMDevice *self, NMDeviceStateReason *reason) if (!s_ip4 || !method || !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); - gboolean success; guint8 *anycast = NULL; if (priv->dhcp_anycast_address) @@ -1102,10 +1102,15 @@ real_act_stage3_ip4_config_start (NMDevice *self, NMDeviceStateReason *reason) /* DHCP manager will cancel any transaction already in progress and we do not want to cancel this activation if we get "down" state from that. */ g_signal_handler_block (priv->dhcp_manager, priv->dhcp_state_sigid); - success = nm_dhcp_manager_begin_transaction (priv->dhcp_manager, ip_iface, uuid, s_ip4, priv->dhcp_timeout, anycast); + priv->dhcp4_id = nm_dhcp_manager_begin_transaction (priv->dhcp_manager, + ip_iface, + uuid, + s_ip4, + priv->dhcp_timeout, + anycast); g_signal_handler_unblock (priv->dhcp_manager, priv->dhcp_state_sigid); - if (success) { + if (priv->dhcp4_id) { /* DHCP devices will be notified by the DHCP manager when * stuff happens. */ @@ -1315,14 +1320,14 @@ real_act_stage4_get_ip4_config (NMDevice *self, s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG); if (nm_device_get_use_dhcp (self)) { - *config = nm_dhcp_manager_get_ip4_config (priv->dhcp_manager, ip_iface); + *config = nm_dhcp_manager_get_ip4_config (priv->dhcp_manager, priv->dhcp4_id); if (*config) { /* Merge user-defined overrides into the IP4Config to be applied */ nm_utils_merge_ip4_config (*config, s_ip4); nm_dhcp4_config_reset (priv->dhcp4_config); nm_dhcp_manager_foreach_dhcp4_option (priv->dhcp_manager, - ip_iface, + priv->dhcp4_id, dhcp4_add_option_cb, priv->dhcp4_config); @@ -2013,7 +2018,7 @@ nm_device_deactivate_quickly (NMDevice *self) /* Stop any ongoing DHCP transaction on this device */ if (nm_device_get_act_request (self)) { if (nm_device_get_use_dhcp (self)) { - nm_dhcp_manager_cancel_transaction (priv->dhcp_manager, nm_device_get_ip_iface (self)); + nm_dhcp_manager_cancel_transaction (priv->dhcp_manager, priv->dhcp4_id); nm_device_set_use_dhcp (self, FALSE); /* Notify of invalid DHCP4 config */ g_object_notify (G_OBJECT (self), NM_DEVICE_INTERFACE_DHCP4_CONFIG); @@ -2027,6 +2032,8 @@ nm_device_deactivate_quickly (NMDevice *self) g_object_unref (priv->dnsmasq_manager); priv->dnsmasq_manager = NULL; } + + priv->dhcp4_id = 0; } aipd_cleanup (self); @@ -2255,7 +2262,6 @@ handle_dhcp_lease_change (NMDevice *device) NMConnection *connection; NMActRequest *req; NMDeviceStateReason reason = NM_DEVICE_STATE_REASON_NONE; - const char *ip_iface; gboolean assumed; if (!nm_device_get_use_dhcp (device)) { @@ -2263,9 +2269,7 @@ handle_dhcp_lease_change (NMDevice *device) return; } - ip_iface = nm_device_get_ip_iface (device); - - config = nm_dhcp_manager_get_ip4_config (priv->dhcp_manager, ip_iface); + config = nm_dhcp_manager_get_ip4_config (priv->dhcp_manager, priv->dhcp4_id); if (!config) { nm_warning ("failed to get DHCP config for rebind"); nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_IP_CONFIG_EXPIRED); @@ -2286,7 +2290,7 @@ handle_dhcp_lease_change (NMDevice *device) if (nm_device_set_ip4_config (device, config, assumed, &reason)) { nm_dhcp4_config_reset (priv->dhcp4_config); nm_dhcp_manager_foreach_dhcp4_option (priv->dhcp_manager, - ip_iface, + priv->dhcp4_id, dhcp4_add_option_cb, priv->dhcp4_config); } else { @@ -2297,7 +2301,7 @@ handle_dhcp_lease_change (NMDevice *device) static void dhcp_state_changed (NMDHCPManager *dhcp_manager, - const char *iface, + guint32 id, NMDHCPState state, gpointer user_data) { @@ -2305,10 +2309,8 @@ dhcp_state_changed (NMDHCPManager *dhcp_manager, NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (device); NMDeviceState dev_state; - if (strcmp (nm_device_get_ip_iface (device), iface) != 0) - return; - - if (!nm_device_get_act_request (device)) + if ( !nm_device_get_act_request (device) + || (priv->dhcp4_id != id)) return; dev_state = nm_device_get_state (device); @@ -2350,12 +2352,14 @@ dhcp_state_changed (NMDHCPManager *dhcp_manager, static void dhcp_timeout (NMDHCPManager *dhcp_manager, - const char *iface, + guint32 id, gpointer user_data) { - NMDevice * device = NM_DEVICE (user_data); + NMDevice *device = NM_DEVICE (user_data); + NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (device); - if (strcmp (nm_device_get_ip_iface (device), iface) != 0) + if ( !nm_device_get_act_request (device) + || (priv->dhcp4_id != id)) return; if (nm_device_get_state (device) == NM_DEVICE_STATE_IP_CONFIG) diff --git a/src/tests/test-dhcp-options.c b/src/tests/test-dhcp-options.c index 90828d9dd8..a99feea9db 100644 --- a/src/tests/test-dhcp-options.c +++ b/src/tests/test-dhcp-options.c @@ -15,7 +15,7 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * Copyright (C) 2008 Red Hat, Inc. + * Copyright (C) 2008 - 2010 Red Hat, Inc. * */ @@ -88,7 +88,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_options_to_ip4_config ("eth0", options); + ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options); ASSERT (ip4_config != NULL, "dhcp-generic", "failed to parse DHCP4 options"); @@ -199,7 +199,7 @@ test_wins_options (void) options = fill_table (generic_options, NULL); options = fill_table (wins_options, options); - ip4_config = nm_dhcp_manager_options_to_ip4_config ("eth0", options); + ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options); ASSERT (ip4_config != NULL, "dhcp-wins", "failed to parse DHCP4 options"); @@ -245,7 +245,7 @@ test_classless_static_routes (void) options = fill_table (generic_options, NULL); options = fill_table (classless_routes_options, options); - ip4_config = nm_dhcp_manager_options_to_ip4_config ("eth0", options); + ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options); ASSERT (ip4_config != NULL, "dhcp-rfc3442", "failed to parse DHCP4 options"); @@ -311,7 +311,7 @@ test_invalid_classless_routes1 (void) options = fill_table (generic_options, NULL); options = fill_table (invalid_classless_routes1, options); - ip4_config = nm_dhcp_manager_options_to_ip4_config ("eth0", options); + ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options); ASSERT (ip4_config != NULL, "dhcp-rfc3442-invalid-1", "failed to parse DHCP4 options"); @@ -362,7 +362,7 @@ test_invalid_classless_routes2 (void) options = fill_table (generic_options, NULL); options = fill_table (invalid_classless_routes2, options); - ip4_config = nm_dhcp_manager_options_to_ip4_config ("eth0", options); + ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options); ASSERT (ip4_config != NULL, "dhcp-rfc3442-invalid-2", "failed to parse DHCP4 options"); @@ -432,7 +432,7 @@ test_invalid_classless_routes3 (void) options = fill_table (generic_options, NULL); options = fill_table (invalid_classless_routes3, options); - ip4_config = nm_dhcp_manager_options_to_ip4_config ("eth0", options); + ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options); ASSERT (ip4_config != NULL, "dhcp-rfc3442-invalid-3", "failed to parse DHCP4 options"); @@ -483,7 +483,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_options_to_ip4_config ("eth0", options); + ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options); ASSERT (ip4_config != NULL, "dhcp-rfc3442-gateway", "failed to parse DHCP4 options"); @@ -537,7 +537,7 @@ test_escaped_domain_searches (void) options = fill_table (generic_options, NULL); options = fill_table (escaped_searches_options, options); - ip4_config = nm_dhcp_manager_options_to_ip4_config ("eth0", options); + ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options); ASSERT (ip4_config != NULL, "dhcp-escaped-domain-searches", "failed to parse DHCP4 options"); @@ -568,7 +568,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_options_to_ip4_config ("eth0", options); + ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options); ASSERT (ip4_config != NULL, "dhcp-invalid-escaped-domain-searches", "failed to parse DHCP4 options");