2006-10-14 Dan Williams <dcbw@redhat.com>

* src/dhcp-manager/nm-dhcp-manager.c
		- (get_ip4_string, get_ip4_uint32s): have the caller pass
		the dbus connection and the device object path rather than
		constructing it inside both functions.  Saves a bit of memory
		and clarifies a failure path.
		- (nm_dhcp_manager_get_ip4_config): grab the dbus connection
		and allocate device path here rather than each of the two
		functions above.


git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@2065 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
This commit is contained in:
Dan Williams
2006-10-14 13:56:38 +00:00
parent 5270567ae6
commit a476cd8efd
2 changed files with 101 additions and 97 deletions

View File

@@ -1,3 +1,14 @@
2006-10-14 Dan Williams <dcbw@redhat.com>
* src/dhcp-manager/nm-dhcp-manager.c
- (get_ip4_string, get_ip4_uint32s): have the caller pass
the dbus connection and the device object path rather than
constructing it inside both functions. Saves a bit of memory
and clarifies a failure path.
- (nm_dhcp_manager_get_ip4_config): grab the dbus connection
and allocate device path here rather than each of the two
functions above.
2006-10-13 Dan Williams <dcbw@redhat.com> 2006-10-13 Dan Williams <dcbw@redhat.com>
* src/NetworkManager.c * src/NetworkManager.c

View File

@@ -52,8 +52,7 @@ static void nm_dhcp_manager_dbus_connection_changed (NMDBusManager *dbus_mgr,
DBusConnection *dbus_connection, DBusConnection *dbus_connection,
gpointer user_data); gpointer user_data);
struct NMDHCPManager struct NMDHCPManager {
{
NMData * data; NMData * data;
GMainContext * main_ctx; GMainContext * main_ctx;
gboolean running; gboolean running;
@@ -200,7 +199,8 @@ out:
* Called after timeout of a DHCP transaction to notify device of the failure. * Called after timeout of a DHCP transaction to notify device of the failure.
* *
*/ */
static gboolean nm_dhcp_manager_handle_timeout (NMActRequest *req) static gboolean
nm_dhcp_manager_handle_timeout (NMActRequest *req)
{ {
NMData * data; NMData * data;
NMDevice * dev; NMDevice * dev;
@@ -213,11 +213,12 @@ static gboolean nm_dhcp_manager_handle_timeout (NMActRequest *req)
dev = nm_act_request_get_dev (req); dev = nm_act_request_get_dev (req);
g_assert (dev); g_assert (dev);
nm_info ("Device '%s' DHCP transaction took too long (>%ds), stopping it.", nm_info ("Device '%s' DHCP transaction took too long (>%ds), stopping"
nm_device_get_iface (dev), NM_DHCP_TIMEOUT); " it.",
nm_device_get_iface (dev),
NM_DHCP_TIMEOUT);
if (nm_act_request_get_stage (req) == NM_ACT_STAGE_IP_CONFIG_START) if (nm_act_request_get_stage (req) == NM_ACT_STAGE_IP_CONFIG_START) {
{
nm_act_request_set_dhcp_timeout (req, 0); nm_act_request_set_dhcp_timeout (req, 0);
nm_dhcp_manager_cancel_transaction (data->dhcp_manager, req); nm_dhcp_manager_cancel_transaction (data->dhcp_manager, req);
nm_device_activate_schedule_stage4_ip_config_timeout (req); nm_device_activate_schedule_stage4_ip_config_timeout (req);
@@ -288,8 +289,8 @@ nm_dhcp_manager_begin_transaction (NMDHCPManager *manager,
dbus_message_unref (reply); dbus_message_unref (reply);
dbus_message_unref (message); dbus_message_unref (message);
if (dbus_error_is_set (&error)) { if (dbus_error_is_set (&error)) {
nm_info ("Couldn't send DHCP 'up' message because: name '%s', message " nm_info ("Couldn't send DHCP 'up' message because: name '%s', "
" '%s'.", "message '%s'.",
error.name, error.name,
error.message); error.message);
dbus_error_free (&error); dbus_error_free (&error);
@@ -311,7 +312,8 @@ out:
} }
static void remove_timeout (NMDHCPManager *manager, NMActRequest *req) static void
remove_timeout (NMDHCPManager *manager, NMActRequest *req)
{ {
guint id; guint id;
@@ -319,8 +321,7 @@ static void remove_timeout (NMDHCPManager *manager, NMActRequest *req)
g_return_if_fail (req != NULL); g_return_if_fail (req != NULL);
/* Remove any pending timeouts on the request */ /* Remove any pending timeouts on the request */
if ((id = nm_act_request_get_dhcp_timeout (req)) > 0) if ((id = nm_act_request_get_dhcp_timeout (req)) > 0) {
{
GSource * source = g_main_context_find_source_by_id (manager->main_ctx, id); GSource * source = g_main_context_find_source_by_id (manager->main_ctx, id);
nm_act_request_set_dhcp_timeout (req, 0); nm_act_request_set_dhcp_timeout (req, 0);
g_source_destroy (source); g_source_destroy (source);
@@ -386,8 +387,8 @@ out:
static gboolean static gboolean
get_ip4_uint32s (NMDHCPManager *manager, get_ip4_uint32s (DBusConnection *dbus_connection,
NMDevice *dev, const char *path,
const char *item, const char *item,
guint32 **ip4_uint32, guint32 **ip4_uint32,
guint32 *num_items, guint32 *num_items,
@@ -395,33 +396,22 @@ get_ip4_uint32s (NMDHCPManager *manager,
{ {
DBusMessage * message = NULL; DBusMessage * message = NULL;
DBusMessage * reply = NULL; DBusMessage * reply = NULL;
char * path;
gboolean success = FALSE; gboolean success = FALSE;
DBusConnection *dbus_connection;
DBusError error; DBusError error;
GArray * buffer; GArray * buffer;
DBusMessageIter iter; DBusMessageIter iter;
g_return_val_if_fail (dbus_connection != NULL, FALSE);
g_return_val_if_fail (manager != NULL, FALSE); g_return_val_if_fail (path != NULL, FALSE);
g_return_val_if_fail (dev != NULL, FALSE);
g_return_val_if_fail (ip4_uint32 != NULL, FALSE); g_return_val_if_fail (ip4_uint32 != NULL, FALSE);
g_return_val_if_fail (num_items != NULL, FALSE); g_return_val_if_fail (num_items != NULL, FALSE);
dbus_connection = nm_dbus_manager_get_dbus_connection (manager->dbus_mgr);
if (!dbus_connection) {
nm_warning ("could not get dbus connection.");
goto out;
}
*ip4_uint32 = NULL; *ip4_uint32 = NULL;
*num_items = 0; *num_items = 0;
path = g_strdup_printf (DHCP_OBJECT_PATH"/%s", nm_device_get_iface (dev));
message = dbus_message_new_method_call (DHCP_SERVICE_NAME, message = dbus_message_new_method_call (DHCP_SERVICE_NAME,
path, path,
DHCP_SERVICE_NAME".dbus.get", DHCP_SERVICE_NAME".dbus.get",
item); item);
g_free (path);
if (!message) { if (!message) {
nm_warning ("could not allocate dbus message."); nm_warning ("could not allocate dbus message.");
goto out; goto out;
@@ -435,8 +425,8 @@ get_ip4_uint32s (NMDHCPManager *manager,
dbus_message_unref (message); dbus_message_unref (message);
if (dbus_error_is_set (&error)) { if (dbus_error_is_set (&error)) {
if (!ignore_error) { if (!ignore_error) {
nm_warning ("error calling '%s', DHCP daemon returned error '%s', " nm_warning ("error calling '%s', DHCP daemon returned "
"message '%s'.", "error '%s', message '%s'.",
item, item,
error.name, error.name,
error.message); error.message);
@@ -446,7 +436,8 @@ get_ip4_uint32s (NMDHCPManager *manager,
} }
if (!reply) { if (!reply) {
nm_warning ("error calling '%s', DHCP daemon did not respond.", item); nm_warning ("error calling '%s', DHCP daemon did not respond.",
item);
goto out; goto out;
} }
@@ -475,37 +466,27 @@ out:
static gboolean static gboolean
get_ip4_string (NMDHCPManager *manager, get_ip4_string (DBusConnection *dbus_connection,
NMDevice *dev, const char *path,
const char *item, const char *item,
char **string, char **string,
gboolean ignore_error) gboolean ignore_error)
{ {
DBusMessage * message = NULL; DBusMessage * message = NULL;
DBusMessage * reply = NULL; DBusMessage * reply = NULL;
char * path;
gboolean success = FALSE; gboolean success = FALSE;
DBusError error; DBusError error;
DBusConnection *dbus_connection;
DBusMessageIter iter; DBusMessageIter iter;
g_return_val_if_fail (manager != NULL, FALSE); g_return_val_if_fail (dbus_connection != NULL, FALSE);
g_return_val_if_fail (dev != NULL, FALSE); g_return_val_if_fail (path != NULL, FALSE);
g_return_val_if_fail (string != NULL, FALSE); g_return_val_if_fail (string != NULL, FALSE);
dbus_connection = nm_dbus_manager_get_dbus_connection (manager->dbus_mgr);
if (!dbus_connection) {
nm_warning ("could not get dbus connection.");
goto out;
}
*string = NULL; *string = NULL;
path = g_strdup_printf (DHCP_OBJECT_PATH"/%s", nm_device_get_iface (dev));
message = dbus_message_new_method_call (DHCP_SERVICE_NAME, message = dbus_message_new_method_call (DHCP_SERVICE_NAME,
path, path,
DHCP_SERVICE_NAME".dbus.get", DHCP_SERVICE_NAME".dbus.get",
item); item);
g_free (path);
if (!message) { if (!message) {
nm_warning ("could not allocate dbus message."); nm_warning ("could not allocate dbus message.");
goto out; goto out;
@@ -519,8 +500,8 @@ get_ip4_string (NMDHCPManager *manager,
dbus_message_unref (message); dbus_message_unref (message);
if (dbus_error_is_set (&error)) { if (dbus_error_is_set (&error)) {
if (!ignore_error) { if (!ignore_error) {
nm_warning ("error calling '%s', DHCP daemon returned error '%s', " nm_warning ("error calling '%s', DHCP daemon returned "
"message '%s'.", "error '%s', message '%s'.",
item, item,
error.name, error.name,
error.message); error.message);
@@ -531,7 +512,8 @@ get_ip4_string (NMDHCPManager *manager,
} }
if (!reply) { if (!reply) {
nm_warning ("error calling '%s', DHCP daemon did not respond.", item); nm_warning ("error calling '%s', DHCP daemon did not respond.",
item);
goto out; goto out;
} }
@@ -567,7 +549,8 @@ out:
} }
static gboolean nm_completion_dhcp_bound_test(int tries, static gboolean
nm_completion_dhcp_bound_test (int tries,
nm_completion_args args) nm_completion_args args)
{ {
NMActRequest * req = args[0]; NMActRequest * req = args[0];
@@ -586,7 +569,9 @@ static gboolean nm_completion_dhcp_bound_test(int tries,
* Get IP4 configuration values from the DHCP daemon * Get IP4 configuration values from the DHCP daemon
* *
*/ */
NMIP4Config * nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager, NMActRequest *req) NMIP4Config *
nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager,
NMActRequest *req)
{ {
NMDevice * dev; NMDevice * dev;
NMIP4Config * ip4_config = NULL; NMIP4Config * ip4_config = NULL;
@@ -605,6 +590,8 @@ NMIP4Config * nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager, NMActReque
guint32 * ip4_nis_servers = NULL; guint32 * ip4_nis_servers = NULL;
struct in_addr temp_addr; struct in_addr temp_addr;
nm_completion_args args; nm_completion_args args;
DBusConnection * dbus_connection;
char * path = NULL;
g_return_val_if_fail (manager != NULL, NULL); g_return_val_if_fail (manager != NULL, NULL);
g_return_val_if_fail (req != NULL, NULL); g_return_val_if_fail (req != NULL, NULL);
@@ -612,6 +599,12 @@ NMIP4Config * nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager, NMActReque
if (!manager->running) if (!manager->running)
return NULL; return NULL;
dbus_connection = nm_dbus_manager_get_dbus_connection (manager->dbus_mgr);
if (!dbus_connection) {
nm_warning ("could not get dbus connection.");
return NULL;
}
dev = nm_act_request_get_dev (req); dev = nm_act_request_get_dev (req);
g_assert (dev); g_assert (dev);
@@ -622,33 +615,38 @@ NMIP4Config * nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager, NMActReque
if (nm_device_activation_should_cancel (dev)) if (nm_device_activation_should_cancel (dev))
return NULL; return NULL;
if (!state_is_bound (nm_act_request_get_dhcp_state (req))) if (!state_is_bound (nm_act_request_get_dhcp_state (req))) {
{ nm_warning ("Tried to get IP4 Config for a device when dhcdbd "
nm_warning ("Tried to get IP4 Config for a device when dhcdbd wasn't in a BOUND state!"); "wasn't in a BOUND state!");
return NULL; return NULL;
} }
if (!get_ip4_uint32s (manager, dev, "ip_address", &ip4_address, &count, FALSE) || !count) path = g_strdup_printf (DHCP_OBJECT_PATH"/%s", nm_device_get_iface (dev));
goto out; if (!path) {
nm_warning ("could not allocate device path.");
if (!get_ip4_uint32s (manager, dev, "subnet_mask", &ip4_netmask, &count, FALSE) || !count)
goto out;
if (!get_ip4_uint32s (manager, dev, "broadcast_address", &ip4_broadcast, &count, FALSE) || !count)
goto out;
if (!get_ip4_uint32s (manager, dev, "routers", &ip4_gateway, &count, TRUE) || !count)
{
/* If DHCP doesn't have a 'routers', just use the DHCP server's address as our gateway for now */
if (!get_ip4_uint32s (manager, dev, "dhcp_server_identifier", &ip4_gateway, &count, FALSE) || !count)
goto out; goto out;
} }
get_ip4_string (manager, dev, "host_name", &hostname, TRUE); if (!get_ip4_uint32s (dbus_connection, path, "ip_address", &ip4_address, &count, FALSE) || !count)
get_ip4_uint32s (manager, dev, "domain_name_servers", &ip4_nameservers, &num_ip4_nameservers, FALSE); goto out;
get_ip4_string (manager, dev, "domain_name", &domain_names, TRUE);
get_ip4_string (manager, dev, "nis_domain", &nis_domain, TRUE); if (!get_ip4_uint32s (dbus_connection, path, "subnet_mask", &ip4_netmask, &count, FALSE) || !count)
get_ip4_uint32s (manager, dev, "nis_servers", &ip4_nis_servers, &num_ip4_nis_servers, TRUE); goto out;
if (!get_ip4_uint32s (dbus_connection, path, "broadcast_address", &ip4_broadcast, &count, FALSE) || !count)
goto out;
if (!get_ip4_uint32s (dbus_connection, path, "routers", &ip4_gateway, &count, TRUE) || !count) {
/* If DHCP doesn't have a 'routers', just use the DHCP server's address as our gateway for now */
if (!get_ip4_uint32s (dbus_connection, path, "dhcp_server_identifier", &ip4_gateway, &count, FALSE) || !count)
goto out;
}
get_ip4_string (dbus_connection, path, "host_name", &hostname, TRUE);
get_ip4_uint32s (dbus_connection, path, "domain_name_servers", &ip4_nameservers, &num_ip4_nameservers, FALSE);
get_ip4_string (dbus_connection, path, "domain_name", &domain_names, TRUE);
get_ip4_string (dbus_connection, path, "nis_domain", &nis_domain, TRUE);
get_ip4_uint32s (dbus_connection, path, "nis_servers", &ip4_nis_servers, &num_ip4_nis_servers, TRUE);
nm_info ("Retrieved the following IP4 configuration from the DHCP daemon:"); nm_info ("Retrieved the following IP4 configuration from the DHCP daemon:");
@@ -669,52 +667,47 @@ NMIP4Config * nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager, NMActReque
temp_addr.s_addr = ip4_gateway[0]; temp_addr.s_addr = ip4_gateway[0];
nm_info (" gateway %s", inet_ntoa (temp_addr)); nm_info (" gateway %s", inet_ntoa (temp_addr));
for (i = 0; i < num_ip4_nameservers; i++) for (i = 0; i < num_ip4_nameservers; i++) {
{
nm_ip4_config_add_nameserver (ip4_config, ip4_nameservers[i]); nm_ip4_config_add_nameserver (ip4_config, ip4_nameservers[i]);
temp_addr.s_addr = ip4_nameservers[i]; temp_addr.s_addr = ip4_nameservers[i];
nm_info (" nameserver %s", inet_ntoa (temp_addr)); nm_info (" nameserver %s", inet_ntoa (temp_addr));
} }
if (hostname) if (hostname) {
{
nm_ip4_config_set_hostname (ip4_config, hostname); nm_ip4_config_set_hostname (ip4_config, hostname);
nm_info (" hostname '%s'", hostname); nm_info (" hostname '%s'", hostname);
} }
if (domain_names) if (domain_names) {
{
char **searches = g_strsplit (domain_names, " ", 0); char **searches = g_strsplit (domain_names, " ", 0);
char **s; char **s;
for (s = searches; *s; s++) for (s = searches; *s; s++) {
{
nm_info (" domain name '%s'", *s); nm_info (" domain name '%s'", *s);
nm_ip4_config_add_domain (ip4_config, *s); nm_ip4_config_add_domain (ip4_config, *s);
} }
g_strfreev (searches); g_strfreev (searches);
} }
if (nis_domain) if (nis_domain) {
{
nm_ip4_config_set_nis_domain (ip4_config, nis_domain); nm_ip4_config_set_nis_domain (ip4_config, nis_domain);
nm_info (" nis domain '%s'", nis_domain); nm_info (" nis domain '%s'", nis_domain);
} }
for (i = 0; i < num_ip4_nis_servers; i++) for (i = 0; i < num_ip4_nis_servers; i++) {
{
nm_ip4_config_add_nis_server (ip4_config, ip4_nis_servers[i]); nm_ip4_config_add_nis_server (ip4_config, ip4_nis_servers[i]);
temp_addr.s_addr = ip4_nis_servers[i]; temp_addr.s_addr = ip4_nis_servers[i];
nm_info (" nis server %s", inet_ntoa (temp_addr)); nm_info (" nis server %s", inet_ntoa (temp_addr));
} }
/* /*
* Grab the MTU from the backend. If DHCP servers can send recommended MTU's, * Grab the MTU from the backend. If DHCP servers can send recommended
* should set that here if the backend returns zero. * MTU's, should set that here if the backend returns zero.
*/ */
nm_ip4_config_set_mtu (ip4_config, nm_system_get_mtu (dev)); nm_ip4_config_set_mtu (ip4_config, nm_system_get_mtu (dev));
out: out:
g_free (path);
return ip4_config; return ip4_config;
} }