static int
route_compare (NMIPRoute *route1, NMIPRoute *route2, gint64 default_metric)
{
	gint64 r, metric1, metric2;

	r = g_strcmp0 (nm_ip_route_get_dest (route1), nm_ip_route_get_dest (route2));
	if (r)
		return r;

	r = nm_ip_route_get_prefix (route1) - nm_ip_route_get_prefix (route2);
	if (r)
		return r > 0 ? 1 : -1;

	r = g_strcmp0 (nm_ip_route_get_next_hop (route1), nm_ip_route_get_next_hop (route2));
	if (r)
		return r;

	metric1 = nm_ip_route_get_metric (route1) == -1 ? default_metric : nm_ip_route_get_metric (route1);
	metric2 = nm_ip_route_get_metric (route2) == -1 ? default_metric : nm_ip_route_get_metric (route2);

	r = metric1 - metric2;
	if (r)
		return r > 0 ? 1 : -1;

	r = nm_ip_route_get_family (route1) - nm_ip_route_get_family (route2);
	if (r)
		return r > 0 ? 1 : -1;

	return 0;
}
static gboolean
ip_route_transform_to_metric_string (GBinding     *binding,
                                     const GValue *source_value,
                                     GValue       *target_value,
                                     gpointer      user_data)
{
	NMIPRoute *route;
	char *string;

	route = g_value_get_boxed (source_value);
	if (route && nm_ip_route_get_dest (route) && nm_ip_route_get_metric (route) != -1) {
		string = g_strdup_printf ("%lu", (gulong) nm_ip_route_get_metric (route));
		g_value_take_string (target_value, string);
	} else
		g_value_set_string (target_value, "");
	return TRUE;
}
static gboolean
ip_route_transform_to_dest_string (GBinding     *binding,
                                   const GValue *source_value,
                                   GValue       *target_value,
                                   gpointer      user_data)
{
	NMIPRoute *route;
	const char *addrstr;
	char *string;

	route = g_value_get_boxed (source_value);
	if (route)
		addrstr = nm_ip_route_get_dest (route);
	else
		addrstr = NULL;

	if (addrstr) {
		string = g_strdup_printf ("%s/%d", addrstr, (int) nm_ip_route_get_prefix (route));
		g_value_take_string (target_value, string);
	} else
		g_value_set_string (target_value, "");
	return TRUE;
}
static GSList *
construct_ip6_items (GSList *items, GVariant *ip6_config, const char *prefix)
{
	GPtrArray *addresses, *routes;
	char *gateway = NULL;
	GVariant *val;
	int i;

	if (ip6_config == NULL)
		return items;

	if (prefix == NULL)
		prefix = "";

	/* IP addresses */
	val = g_variant_lookup_value (ip6_config, "addresses", G_VARIANT_TYPE ("a(ayuay)"));
	if (val) {
		addresses = nm_utils_ip6_addresses_from_variant (val, &gateway);
		if (!gateway)
			gateway = g_strdup ("::");

		for (i = 0; i < addresses->len; i++) {
			NMIPAddress *addr = addresses->pdata[i];
			char *addrtmp;

			addrtmp = g_strdup_printf ("%sIP6_ADDRESS_%d=%s/%d %s", prefix, i,
			                           nm_ip_address_get_address (addr),
			                           nm_ip_address_get_prefix (addr),
			                           gateway);
			items = g_slist_prepend (items, addrtmp);
		}
		if (addresses->len)
			items = g_slist_prepend (items, g_strdup_printf ("%sIP6_NUM_ADDRESSES=%d", prefix, addresses->len));

		/* Write gateway to a separate variable, too. */
		items = g_slist_prepend (items, g_strdup_printf ("%sIP6_GATEWAY=%s", prefix, gateway));

		g_ptr_array_unref (addresses);
		g_free (gateway);
		g_variant_unref (val);
	}

	/* DNS servers */
	val = g_variant_lookup_value (ip6_config, "nameservers", G_VARIANT_TYPE ("aay"));
	if (val) {
		items = _list_append_val_strv (items, nm_utils_ip6_dns_from_variant (val),
		                               "%sIP6_NAMESERVERS=", prefix);
		g_variant_unref (val);
	}

	/* Search domains */
	items = add_domains (items, ip6_config, prefix, '6');

	/* Static routes */
	val = g_variant_lookup_value (ip6_config, "routes", G_VARIANT_TYPE ("a(ayuayu)"));
	if (val) {
		routes = nm_utils_ip6_routes_from_variant (val);

		for (i = 0; i < routes->len; i++) {
			NMIPRoute *route = routes->pdata[i];
			const char *next_hop;
			char *routetmp;

			next_hop = nm_ip_route_get_next_hop (route);
			if (!next_hop)
				next_hop = "::";

			routetmp = g_strdup_printf ("%sIP6_ROUTE_%d=%s/%d %s %u", prefix, i,
			                            nm_ip_route_get_dest (route),
			                            nm_ip_route_get_prefix (route),
			                            next_hop,
			                            (guint32) MAX (0, nm_ip_route_get_metric (route)));
			items = g_slist_prepend (items, routetmp);
		}
		if (routes->len)
			items = g_slist_prepend (items, g_strdup_printf ("%sIP6_NUM_ROUTES=%d", prefix, routes->len));
		g_ptr_array_unref (routes);
		g_variant_unref (val);
	}

	return items;
}
GtkWidget *
ip4_routes_dialog_new (NMSettingIPConfig *s_ip4, gboolean automatic)
{
	GtkBuilder *builder;
	GtkWidget *dialog, *widget, *ok_button;
	GtkListStore *store;
	GtkTreeIter model_iter;
	GtkTreeSelection *selection;
	gint offset;
	GtkTreeViewColumn *column;
	GtkCellRenderer *renderer;
	int i;
	GSList *renderers = NULL;
	GError* error = NULL;

	/* Initialize temporary storage vars */
	g_free (last_edited);
	last_edited = NULL;
	last_path = NULL;
	g_free (last_path);
	last_column = -1;

	builder = gtk_builder_new ();

	if (!gtk_builder_add_from_file (builder, UIDIR "/ce-ip4-routes.ui", &error)) {
		g_warning ("Couldn't load builder file: %s", error->message);
		g_error_free (error);
		return NULL;
	}

	dialog = GTK_WIDGET (gtk_builder_get_object (builder, "ip4_routes_dialog"));
	if (!dialog) {
		g_warning ("%s: Couldn't load ip4 routes dialog from .ui file.", __func__);
		g_object_unref (builder);
		return NULL;
	}

	gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);

	g_object_set_data_full (G_OBJECT (dialog), "builder",
	                        builder, (GDestroyNotify) g_object_unref);

	ok_button = GTK_WIDGET (gtk_builder_get_object (builder, "ok_button"));

	store = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);

	/* Add existing routes */
	for (i = 0; i < nm_setting_ip_config_get_num_routes (s_ip4); i++) {
		NMIPRoute *route = nm_setting_ip_config_get_route (s_ip4, i);
		struct in_addr tmp_addr;
		char netmask[INET_ADDRSTRLEN], metric[32];
		gint64 metric_int;

		if (!route) {
			g_warning ("%s: empty IP4 route structure!", __func__);
			continue;
		}

		tmp_addr.s_addr = nm_utils_ip4_prefix_to_netmask (nm_ip_route_get_prefix (route));
		if (!inet_ntop (AF_INET, &tmp_addr, netmask, sizeof (netmask)))
			*netmask = '\0';

		metric_int = nm_ip_route_get_metric (route);
		if (metric_int >= 0 && metric_int <= G_MAXUINT32)
			g_snprintf (metric, sizeof (metric), "%lu", (unsigned long) metric_int);
		else {
			if (metric_int != -1)
				g_warning ("invalid metric %lld", (long long int) metric_int);
			metric[0] = 0;
		}

		gtk_list_store_append (store, &model_iter);
		gtk_list_store_set (store, &model_iter,
		                    COL_ADDRESS, nm_ip_route_get_dest (route),
		                    COL_PREFIX, netmask,
		                    COL_NEXT_HOP, nm_ip_route_get_next_hop (route),
		                    COL_METRIC, metric,
		                    -1);
	}

	widget = GTK_WIDGET (gtk_builder_get_object (builder, "ip4_routes"));
	gtk_tree_view_set_model (GTK_TREE_VIEW (widget), GTK_TREE_MODEL (store));
	g_object_unref (store);

	/* IP Address column */
	renderer = gtk_cell_renderer_text_new ();
	g_object_set (renderer, "editable", TRUE, NULL);
	g_signal_connect (renderer, "edited", G_CALLBACK (cell_edited), builder);
	g_object_set_data (G_OBJECT (renderer), "column", GUINT_TO_POINTER (COL_ADDRESS));
	g_signal_connect (renderer, "editing-started", G_CALLBACK (ip4_cell_editing_started), ok_button);
	g_signal_connect (renderer, "editing-canceled", G_CALLBACK (cell_editing_canceled), builder);
	renderers = g_slist_append (renderers, renderer);

	offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (widget),
	                                                      -1, _("Address"), renderer,
	                                                      "text", COL_ADDRESS,
	                                                      NULL);
	column = gtk_tree_view_get_column (GTK_TREE_VIEW (widget), offset - 1);
	gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
	gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
	gtk_tree_view_column_set_cell_data_func (column, renderer, cell_error_data_func,
	                                         GUINT_TO_POINTER (COL_ADDRESS), NULL);

	/* Prefix column */
	renderer = gtk_cell_renderer_text_new ();
	g_object_set (renderer, "editable", TRUE, NULL);
	g_signal_connect (renderer, "edited", G_CALLBACK (cell_edited), builder);
	g_object_set_data (G_OBJECT (renderer), "column", GUINT_TO_POINTER (COL_PREFIX));
	g_signal_connect (renderer, "editing-started", G_CALLBACK (ip4_cell_editing_started), ok_button);
	g_signal_connect (renderer, "editing-canceled", G_CALLBACK (cell_editing_canceled), builder);
	renderers = g_slist_append (renderers, renderer);

	offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (widget),
	                                                      -1, _("Netmask"), renderer,
	                                                      "text", COL_PREFIX,
	                                                      NULL);
	column = gtk_tree_view_get_column (GTK_TREE_VIEW (widget), offset - 1);
	gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
	gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
	gtk_tree_view_column_set_cell_data_func (column, renderer, cell_error_data_func,
	                                         GUINT_TO_POINTER (COL_PREFIX), NULL);

	/* Gateway column */
	renderer = gtk_cell_renderer_text_new ();
	g_object_set (renderer, "editable", TRUE, NULL);
	g_signal_connect (renderer, "edited", G_CALLBACK (cell_edited), builder);
	g_object_set_data (G_OBJECT (renderer), "column", GUINT_TO_POINTER (COL_NEXT_HOP));
	g_signal_connect (renderer, "editing-started", G_CALLBACK (ip4_cell_editing_started), ok_button);
	g_signal_connect (renderer, "editing-canceled", G_CALLBACK (cell_editing_canceled), builder);
	renderers = g_slist_append (renderers, renderer);

	offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (widget),
	                                                      -1, _("Gateway"), renderer,
	                                                      "text", COL_NEXT_HOP,
	                                                      NULL);
	column = gtk_tree_view_get_column (GTK_TREE_VIEW (widget), offset - 1);
	gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
	gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
	gtk_tree_view_column_set_cell_data_func (column, renderer, cell_error_data_func,
	                                         GUINT_TO_POINTER (COL_NEXT_HOP), NULL);

	/* Metric column */
	renderer = gtk_cell_renderer_text_new ();
	g_object_set (renderer, "editable", TRUE, NULL);
	g_signal_connect (renderer, "edited", G_CALLBACK (cell_edited), builder);
	g_object_set_data (G_OBJECT (renderer), "column", GUINT_TO_POINTER (COL_METRIC));
	g_signal_connect (renderer, "editing-started", G_CALLBACK (uint_cell_editing_started), ok_button);
	g_signal_connect (renderer, "editing-canceled", G_CALLBACK (cell_editing_canceled), builder);
	renderers = g_slist_append (renderers, renderer);

	offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (widget),
	                                                      -1, _("Metric"), renderer,
	                                                      "text", COL_METRIC,
	                                                      NULL);
	column = gtk_tree_view_get_column (GTK_TREE_VIEW (widget), offset - 1);
	gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
	gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
	gtk_tree_view_column_set_cell_data_func (column, renderer, cell_error_data_func,
	                                         GUINT_TO_POINTER (COL_METRIC), NULL);

	g_object_set_data_full (G_OBJECT (dialog), "renderers", renderers, (GDestroyNotify) g_slist_free);

	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
	g_signal_connect (selection, "changed",
	                  G_CALLBACK (list_selection_changed),
	                  GTK_WIDGET (gtk_builder_get_object (builder, "ip4_route_delete_button")));
	g_signal_connect (widget, "button-press-event", G_CALLBACK (tree_view_button_pressed_cb), builder);

	widget = GTK_WIDGET (gtk_builder_get_object (builder, "ip4_route_add_button"));
	gtk_widget_set_sensitive (widget, TRUE);
	g_signal_connect (widget, "clicked", G_CALLBACK (route_add_clicked), builder);

	widget = GTK_WIDGET (gtk_builder_get_object (builder, "ip4_route_delete_button"));
	gtk_widget_set_sensitive (widget, FALSE);
	g_signal_connect (widget, "clicked", G_CALLBACK (route_delete_clicked), builder);

	widget = GTK_WIDGET (gtk_builder_get_object (builder, "ip4_ignore_auto_routes"));
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget),
	                              nm_setting_ip_config_get_ignore_auto_routes (s_ip4));
	gtk_widget_set_sensitive (widget, automatic);

	widget = GTK_WIDGET (gtk_builder_get_object (builder, "ip4_never_default"));
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget),
	                              nm_setting_ip_config_get_never_default (s_ip4));

	/* Update initial validity */
	validate (dialog);

	return dialog;
}
Exemplo n.º 6
0
gboolean
print_ip4_config (NMIPConfig *cfg4,
                  NmCli *nmc,
                  const char *group_prefix,
                  const char *one_field)
{
	GPtrArray *ptr_array;
	char **addr_arr = NULL;
	char **route_arr = NULL;
	char **dns_arr = NULL;
	char **domain_arr = NULL;
	char **wins_arr = NULL;
	int i = 0;
	NmcOutputField *tmpl, *arr;
	size_t tmpl_len;

	if (cfg4 == NULL)
		return FALSE;

	tmpl = nmc_fields_ip4_config;
	tmpl_len = sizeof (nmc_fields_ip4_config);
	nmc->print_fields.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_IP4_CONFIG_ALL,
	                                                 tmpl, FALSE, NULL, NULL);
	arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES);
	g_ptr_array_add (nmc->output_data, arr);

	/* addresses */
	ptr_array = nm_ip_config_get_addresses (cfg4);
	if (ptr_array) {
		addr_arr = g_new (char *, ptr_array->len + 1);
		for (i = 0; i < ptr_array->len; i++) {
			NMIPAddress *addr = (NMIPAddress *) g_ptr_array_index (ptr_array, i);

			addr_arr[i] = g_strdup_printf ("%s/%u",
			                               nm_ip_address_get_address (addr),
			                               nm_ip_address_get_prefix (addr));
		}
		addr_arr[i] = NULL;
	}

	/* routes */
	ptr_array = nm_ip_config_get_routes (cfg4);
	if (ptr_array) {
		route_arr = g_new (char *, ptr_array->len + 1);
		for (i = 0; i < ptr_array->len; i++) {
			NMIPRoute *route = (NMIPRoute *) g_ptr_array_index (ptr_array, i);
			const char *next_hop;

			next_hop = nm_ip_route_get_next_hop (route);
			if (!next_hop)
				next_hop = "0.0.0.0";

			route_arr[i] = g_strdup_printf ("dst = %s/%u, nh = %s%c mt = %u",
			                                nm_ip_route_get_dest (route),
			                                nm_ip_route_get_prefix (route),
			                                next_hop,
			                                nm_ip_route_get_metric (route) == -1 ? '\0' : ',',
			                                (guint32) nm_ip_route_get_metric (route));
		}
		route_arr[i] = NULL;
	}

	/* DNS */
	dns_arr = g_strdupv ((char **) nm_ip_config_get_nameservers (cfg4));

	/* domains */
	domain_arr = g_strdupv ((char **) nm_ip_config_get_domains (cfg4));

	/* WINS */
	wins_arr = g_strdupv ((char **) nm_ip_config_get_wins_servers (cfg4));

	arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX);
	set_val_strc (arr, 0, group_prefix);
	set_val_arr  (arr, 1, addr_arr);
	set_val_strc (arr, 2, nm_ip_config_get_gateway (cfg4));
	set_val_arr  (arr, 3, route_arr);
	set_val_arr  (arr, 4, dns_arr);
	set_val_arr  (arr, 5, domain_arr);
	set_val_arr  (arr, 6, wins_arr);
	g_ptr_array_add (nmc->output_data, arr);

	print_data (nmc); /* Print all data */

	/* Remove any previous data */
	nmc_empty_output_fields (nmc);

	return TRUE;
}
Exemplo n.º 7
0
/*
 * nmc_parse_and_build_route:
 * @family: AF_INET or AF_INET6
 * @first: the route destination in the form of "address/prefix"
     (/prefix is optional)
 * @second: (allow-none): next hop address, if third is not NULL. Otherwise it could be
     either next hop address or metric. (It can be NULL when @third is NULL).
 * @third: (allow-none): route metric
 * @error: location to store GError
 *
 * Parse route from strings and return an #NMIPRoute
 *
 * Returns: %TRUE on success, %FALSE on failure
 */
NMIPRoute *
nmc_parse_and_build_route (int family,
                           const char *first,
                           const char *second,
                           const char *third,
                           GError **error)
{
	int max_prefix = (family == AF_INET) ? 32 : 128;
	char *dest = NULL, *plen = NULL;
	const char *next_hop = NULL;
	const char *canon_dest;
	long int prefix = max_prefix, metric = -1;
	NMIPRoute *route = NULL;
	gboolean success = FALSE;
	GError *local = NULL;

	g_return_val_if_fail (family == AF_INET || family == AF_INET6, FALSE);
	g_return_val_if_fail (first != NULL, FALSE);
	g_return_val_if_fail (second || !third, FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	dest = g_strdup (first);
	plen = strchr (dest, '/');  /* prefix delimiter */
	if (plen)
		*plen++ = '\0';

	if (plen) {
		if (!nmc_string_to_int (plen, TRUE, 1, max_prefix, &prefix)) {
			g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT,
			             _("invalid prefix '%s'; <1-%d> allowed"),
			             plen, max_prefix);
			goto finish;
		}
	}

	if (second) {
		if (third || nm_utils_ipaddr_valid (family, second))
			next_hop = second;
		else {
			/* 'second' can be a metric */
			if (!nmc_string_to_int (second, TRUE, 0, G_MAXUINT32, &metric)) {
				g_set_error (error, 1, 0, _("the second component of route ('%s') is neither "
				                            "a next hop address nor a metric"), second);
				goto finish;
			}
		}
	}

	if (third) {
		if (!nmc_string_to_int (third, TRUE, 0, G_MAXUINT32, &metric)) {
			g_set_error (error, 1, 0, _("invalid metric '%s'"), third);
			goto finish;
		}
	}

	route = nm_ip_route_new (family, dest, prefix, next_hop, metric, &local);
	if (!route) {
		g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT,
		             _("invalid route: %s"), local->message);
		g_clear_error (&local);
		goto finish;
	}

	/* We don't accept default routes as NetworkManager handles it
	 * itself. But we have to check this after @route has normalized the
	 * dest string.
	 */
	canon_dest = nm_ip_route_get_dest (route);
	if (!strcmp (canon_dest, "0.0.0.0") || !strcmp (canon_dest, "::")) {
		g_set_error_literal (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT,
		                     _("default route cannot be added (NetworkManager handles it by itself)"));
		g_clear_pointer (&route, nm_ip_route_unref);
		goto finish;
	}

	success = TRUE;

finish:
	g_free (dest);
	return route;
}