static void
nm_dhcp_manager_init (NMDhcpManager *self)
{
	NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
	NMConfig *config = nm_config_get ();
	const char *client;
	GError *error = NULL;
	GSList *iter;

	for (iter = client_descs; iter; iter = iter->next) {
		ClientDesc *desc = iter->data;

		nm_log_dbg (LOGD_DHCP, "Registered DHCP client '%s' (%s)",
		            desc->name, g_type_name (desc->gtype));
	}

	/* Client-specific setup */
	client = nm_config_get_dhcp_client (config);
	if (nm_config_get_configure_and_quit (config)) {
		if (g_strcmp0 (client, "internal") != 0)
			nm_log_warn (LOGD_DHCP, "Using internal DHCP client since configure-and-quit is set.");
		client = "internal";
	}

	priv->client_type = get_client_type (client, &error);
	if (priv->client_type == G_TYPE_INVALID) {
		nm_log_warn (LOGD_DHCP, "No usable DHCP client found (%s)! DHCP configurations will fail.",
		             error->message);
	} else {
		nm_log_dbg (LOGD_DHCP, "Using DHCP client '%s'", find_client_desc (NULL, priv->client_type)->name);

	}
	g_clear_error (&error);

	priv->clients = g_hash_table_new_full (g_direct_hash, g_direct_equal,
	                                       NULL,
	                                       (GDestroyNotify) g_object_unref);
}
Example #2
0
void
get_dhcp_hostname_and_client_id (char **hostname, char **client_id)
{
	const char *dhcp_client;
	const gchar *dhcpcd_conf = SYSCONFDIR "/dhcpcd.conf";
	const gchar *dhclient_conf = SYSCONFDIR "/dhcp/dhclient.conf";
	gchar *line = NULL, *tmp = NULL, *contents = NULL, *tmp1;
	gchar **all_lines;
	guint line_num, i;
	gboolean use_dhclient = FALSE;

	*hostname = NULL;
	*client_id = NULL;
	dhcp_client = nm_config_get_dhcp_client (nm_config_get ());
	if (dhcp_client) {
		if (!strcmp (dhcp_client, "dhclient")) {
			g_file_get_contents (dhclient_conf, &contents, NULL,
					     NULL);
			use_dhclient = TRUE;
		} else if (!strcmp (dhcp_client, "dhcpcd"))
			g_file_get_contents (dhcpcd_conf, &contents, NULL,
					     NULL);
	} else {
		if (g_file_test (dhclient_conf, G_FILE_TEST_IS_REGULAR)) {
			g_file_get_contents (dhclient_conf, &contents, NULL,
					     NULL);
			use_dhclient = TRUE;
		}
		else if (g_file_test (dhcpcd_conf, G_FILE_TEST_IS_REGULAR))
			g_file_get_contents (dhcpcd_conf, &contents, NULL,
					     NULL);
	}
	if (!contents)
		return;
	all_lines = g_strsplit (contents, "\n", 0);
	line_num = g_strv_length (all_lines);
	for (i = 0; i < line_num; i++) {
		line = all_lines[i];
		g_strstrip (line);
		if (line[0] == '#' || line[0] == '\0')
			continue;
		if (!use_dhclient) {
			// dhcpcd.conf
			if ((tmp = _has_prefix (line, "hostname"))) {
				if (tmp[0] != '\0') {
					g_free (*hostname);
					*hostname = g_strdup (tmp);
				} else
					nm_log_info (LOGD_SETTINGS, "dhcpcd hostname not defined, ignoring");
			} else if ((tmp = _has_prefix (line, "clientid"))) {
				if (tmp[0] != '\0') {
					g_free (*client_id);
					*client_id = g_strdup (tmp);
				} else
					nm_log_info (LOGD_SETTINGS, "dhcpcd clientid not defined, ignoring");
			}
		} else {
			// dhclient.conf
			if ((tmp1 = _has_prefix (line, "send"))) {
				if ((tmp = _has_prefix (tmp1, "host-name"))) {
					strip_string (tmp, ';');
					strip_string (tmp, '"');
					if (tmp[0] != '\0') {
						g_free (*hostname);
						*hostname = g_strdup (tmp);
					} else
						nm_log_info (LOGD_SETTINGS, "dhclient hostname not defined, ignoring");
				} else if ((tmp = _has_prefix (tmp1, "dhcp-client-identifier"))) {
					strip_string (tmp, ';');
					if (tmp[0] != '\0') {
						g_free (*client_id);
						*client_id = g_strdup (tmp);
					} else
						nm_log_info (LOGD_SETTINGS, "dhclient clientid not defined, ignoring");
				}
			}
		}
	}
	g_strfreev (all_lines);
	g_free (contents);
}