NMCheckpointManager * nm_checkpoint_manager_new (NMManager *manager) { NMCheckpointManager *self; g_return_val_if_fail (NM_IS_MANAGER (manager), FALSE); self = g_slice_new0 (NMCheckpointManager); /* the NMCheckpointManager instance is actually owned by NMManager. * Thus, we cannot take a reference to it, and we also don't bother * taking a weak-reference. Instead let GET_MANAGER() assert that * self->_manager is alive -- which we always expect as the lifetime * of NMManager shall surpass the lifetime of the NMCheckpointManager * instance. */ self->_manager = manager; self->checkpoints = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, checkpoint_destroy); return self; }
static NMDevice * get_best_ip4_device (NMManager *manager, NMActRequest **out_req) { GSList *devices, *iter; NMDevice *best = NULL; int best_prio = G_MAXINT; g_return_val_if_fail (manager != NULL, NULL); g_return_val_if_fail (NM_IS_MANAGER (manager), NULL); g_return_val_if_fail (out_req != NULL, NULL); g_return_val_if_fail (*out_req == NULL, NULL); devices = nm_manager_get_devices (manager); for (iter = devices; iter; iter = g_slist_next (iter)) { NMDevice *dev = NM_DEVICE (iter->data); NMActRequest *req; NMConnection *connection; NMIP4Config *ip4_config; NMSettingIP4Config *s_ip4; int prio; guint i; gboolean can_default = FALSE; const char *method = NULL; if (nm_device_get_state (dev) != NM_DEVICE_STATE_ACTIVATED) continue; ip4_config = nm_device_get_ip4_config (dev); if (!ip4_config) continue; req = nm_device_get_act_request (dev); g_assert (req); connection = nm_act_request_get_connection (req); g_assert (connection); /* Never set the default route through an IPv4LL-addressed device */ s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG); if (s_ip4) method = nm_setting_ip4_config_get_method (s_ip4); if (s_ip4 && !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL)) continue; /* Make sure at least one of this device's IP addresses has a gateway */ for (i = 0; i < nm_ip4_config_get_num_addresses (ip4_config); i++) { NMIP4Address *addr; addr = nm_ip4_config_get_address (ip4_config, i); if (nm_ip4_address_get_gateway (addr)) { can_default = TRUE; break; } } if (!can_default && !NM_IS_DEVICE_MODEM (dev)) continue; /* 'never-default' devices can't ever be the default */ if ( (s_ip4 && nm_setting_ip4_config_get_never_default (s_ip4)) || nm_ip4_config_get_never_default (ip4_config)) continue; prio = nm_device_get_priority (dev); if (prio > 0 && prio < best_prio) { best = dev; best_prio = prio; *out_req = req; } } return best; }
NMPolicy * nm_policy_new (NMManager *manager, NMVPNManager *vpn_manager) { NMPolicy *policy; static gboolean initialized = FALSE; gulong id; char hostname[HOST_NAME_MAX + 2]; g_return_val_if_fail (NM_IS_MANAGER (manager), NULL); g_return_val_if_fail (initialized == FALSE, NULL); policy = g_malloc0 (sizeof (NMPolicy)); policy->manager = g_object_ref (manager); policy->update_state_id = 0; /* Grab hostname on startup and use that if nothing provides one */ memset (hostname, 0, sizeof (hostname)); if (gethostname (&hostname[0], HOST_NAME_MAX) == 0) { /* only cache it if it's a valid hostname */ if (strlen (hostname) && strcmp (hostname, "localhost") && strcmp (hostname, "localhost.localdomain")) policy->orig_hostname = g_strdup (hostname); } policy->vpn_manager = g_object_ref (vpn_manager); id = g_signal_connect (policy->vpn_manager, "connection-activated", G_CALLBACK (vpn_connection_activated), policy); policy->vpn_activated_id = id; id = g_signal_connect (policy->vpn_manager, "connection-deactivated", G_CALLBACK (vpn_connection_deactivated), policy); policy->vpn_deactivated_id = id; id = g_signal_connect (manager, "state-changed", G_CALLBACK (global_state_changed), policy); policy->signal_ids = g_slist_append (policy->signal_ids, (gpointer) id); id = g_signal_connect (manager, "notify::" NM_MANAGER_HOSTNAME, G_CALLBACK (hostname_changed), policy); policy->signal_ids = g_slist_append (policy->signal_ids, (gpointer) id); id = g_signal_connect (manager, "notify::" NM_MANAGER_SLEEPING, G_CALLBACK (sleeping_changed), policy); policy->signal_ids = g_slist_append (policy->signal_ids, (gpointer) id); id = g_signal_connect (manager, "notify::" NM_MANAGER_NETWORKING_ENABLED, G_CALLBACK (sleeping_changed), policy); policy->signal_ids = g_slist_append (policy->signal_ids, (gpointer) id); id = g_signal_connect (manager, "device-added", G_CALLBACK (device_added), policy); policy->signal_ids = g_slist_append (policy->signal_ids, (gpointer) id); id = g_signal_connect (manager, "device-removed", G_CALLBACK (device_removed), policy); policy->signal_ids = g_slist_append (policy->signal_ids, (gpointer) id); /* Large batch of connections added, manager doesn't want us to * process each one individually. */ id = g_signal_connect (manager, "connections-added", G_CALLBACK (connections_added), policy); policy->signal_ids = g_slist_append (policy->signal_ids, (gpointer) id); /* Single connection added */ id = g_signal_connect (manager, "connection-added", G_CALLBACK (connection_added), policy); policy->signal_ids = g_slist_append (policy->signal_ids, (gpointer) id); id = g_signal_connect (manager, "connection-updated", G_CALLBACK (connection_updated), policy); policy->signal_ids = g_slist_append (policy->signal_ids, (gpointer) id); id = g_signal_connect (manager, "connection-removed", G_CALLBACK (connection_removed), policy); policy->signal_ids = g_slist_append (policy->signal_ids, (gpointer) id); id = g_signal_connect (manager, "user-permissions-changed", G_CALLBACK (manager_user_permissions_changed), policy); policy->signal_ids = g_slist_append (policy->signal_ids, (gpointer) id); return policy; }