|
Packit Service |
87a54e |
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
Packit |
5756e2 |
/*
|
|
Packit |
5756e2 |
* Copyright (C) 2013 Red Hat, Inc.
|
|
Packit |
5756e2 |
*/
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
/**
|
|
Packit |
5756e2 |
* SECTION:nmt-route-table
|
|
Packit |
5756e2 |
* @short_description: An editable list of IP4 or IP6 routes
|
|
Packit |
5756e2 |
*
|
|
Packit |
5756e2 |
* #NmtRouteTable implements a list of #NmtRouteEntry, plus headers,
|
|
Packit |
5756e2 |
* and buttons to add and remove entries.
|
|
Packit |
5756e2 |
*/
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
#include "nm-default.h"
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
#include <arpa/inet.h>
|
|
Packit |
5756e2 |
#include <netinet/in.h>
|
|
Packit |
5756e2 |
#include <stdlib.h>
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
#include "nmt-route-table.h"
|
|
Packit |
5756e2 |
#include "nmt-route-entry.h"
|
|
Packit |
5756e2 |
#include "nmt-widget-list.h"
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
G_DEFINE_TYPE(NmtRouteTable, nmt_route_table, NMT_TYPE_NEWT_GRID)
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
#define NMT_ROUTE_TABLE_GET_PRIVATE(o) \
|
|
Packit Service |
a1bd4f |
(G_TYPE_INSTANCE_GET_PRIVATE((o), NMT_TYPE_ROUTE_TABLE, NmtRouteTablePrivate))
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
typedef struct {
|
|
Packit Service |
a1bd4f |
int family;
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
int ip_entry_width;
|
|
Packit Service |
a1bd4f |
int metric_entry_width;
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
GPtrArray * routes;
|
|
Packit Service |
a1bd4f |
NmtNewtWidget *list;
|
|
Packit |
5756e2 |
} NmtRouteTablePrivate;
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
enum {
|
|
Packit Service |
a1bd4f |
PROP_0,
|
|
Packit Service |
a1bd4f |
PROP_FAMILY,
|
|
Packit Service |
a1bd4f |
PROP_ROUTES,
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
LAST_PROP
|
|
Packit |
5756e2 |
};
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
/**
|
|
Packit |
5756e2 |
* nmt_route_table_new:
|
|
Packit |
5756e2 |
* @family: the address family, eg %AF_INET
|
|
Packit |
5756e2 |
*
|
|
Packit |
5756e2 |
* Creates a new #NmtRouteTable
|
|
Packit |
5756e2 |
*
|
|
Packit |
5756e2 |
* Returns: a new #NmtRouteTable
|
|
Packit |
5756e2 |
*/
|
|
Packit |
5756e2 |
NmtNewtWidget *
|
|
Packit Service |
a1bd4f |
nmt_route_table_new(int family)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
return g_object_new(NMT_TYPE_ROUTE_TABLE, "family", family, NULL);
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static gboolean
|
|
Packit Service |
a1bd4f |
route_list_transform_to_route(GBinding * binding,
|
|
Packit Service |
a1bd4f |
const GValue *source_value,
|
|
Packit Service |
a1bd4f |
GValue * target_value,
|
|
Packit Service |
a1bd4f |
gpointer user_data)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
NmtRouteTable * table = NMT_ROUTE_TABLE(g_binding_get_source(binding));
|
|
Packit Service |
a1bd4f |
NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE(table);
|
|
Packit Service |
a1bd4f |
int n = GPOINTER_TO_INT(user_data);
|
|
Packit Service |
a1bd4f |
NMIPRoute * route;
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
if (n >= priv->routes->len)
|
|
Packit Service |
a1bd4f |
return FALSE;
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
route = priv->routes->pdata[n];
|
|
Packit Service |
a1bd4f |
g_value_set_boxed(target_value, route);
|
|
Packit Service |
a1bd4f |
return TRUE;
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static gboolean
|
|
Packit Service |
a1bd4f |
route_list_transform_from_route(GBinding * binding,
|
|
Packit Service |
a1bd4f |
const GValue *source_value,
|
|
Packit Service |
a1bd4f |
GValue * target_value,
|
|
Packit Service |
a1bd4f |
gpointer user_data)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
NmtRouteTable * table = NMT_ROUTE_TABLE(g_binding_get_source(binding));
|
|
Packit Service |
a1bd4f |
NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE(table);
|
|
Packit Service |
a1bd4f |
int n = GPOINTER_TO_INT(user_data);
|
|
Packit Service |
a1bd4f |
GPtrArray * routes;
|
|
Packit Service |
a1bd4f |
NMIPRoute * route;
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
if (n >= priv->routes->len)
|
|
Packit Service |
a1bd4f |
return FALSE;
|
|
Packit Service |
a1bd4f |
route = priv->routes->pdata[n];
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
routes = priv->routes;
|
|
Packit Service |
a1bd4f |
priv->routes = g_ptr_array_new_with_free_func((GDestroyNotify) nm_ip_route_unref);
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
if (route)
|
|
Packit Service |
a1bd4f |
nm_ip_route_unref(route);
|
|
Packit Service |
a1bd4f |
routes->pdata[n] = g_value_dup_boxed(source_value);
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
g_value_take_boxed(target_value, routes);
|
|
Packit Service |
a1bd4f |
return TRUE;
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static NmtNewtWidget *
|
|
Packit Service |
a1bd4f |
create_route_entry(NmtWidgetList *list, int num, gpointer table)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE(table);
|
|
Packit Service |
a1bd4f |
NmtNewtWidget * entry;
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
entry = nmt_route_entry_new(priv->family, priv->ip_entry_width, priv->metric_entry_width);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
g_object_bind_property_full(table,
|
|
Packit Service |
a1bd4f |
"routes",
|
|
Packit Service |
a1bd4f |
entry,
|
|
Packit Service |
a1bd4f |
"route",
|
|
Packit Service |
a1bd4f |
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE,
|
|
Packit Service |
a1bd4f |
route_list_transform_to_route,
|
|
Packit Service |
a1bd4f |
route_list_transform_from_route,
|
|
Packit Service |
a1bd4f |
GINT_TO_POINTER(num),
|
|
Packit Service |
a1bd4f |
NULL);
|
|
Packit Service |
a1bd4f |
return entry;
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static void
|
|
Packit Service |
a1bd4f |
add_route(NmtWidgetList *list, gpointer table)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE(table);
|
|
Packit Service |
a1bd4f |
NMIPRoute * route;
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
if (priv->family == AF_INET)
|
|
Packit Service |
a1bd4f |
route = nm_ip_route_new(AF_INET, "0.0.0.0", 32, NULL, -1, NULL);
|
|
Packit Service |
a1bd4f |
else
|
|
Packit Service |
a1bd4f |
route = nm_ip_route_new(AF_INET6, "::", 128, NULL, -1, NULL);
|
|
Packit Service |
a1bd4f |
g_ptr_array_add(priv->routes, route);
|
|
Packit Service |
a1bd4f |
nmt_widget_list_set_length(list, priv->routes->len);
|
|
Packit Service |
a1bd4f |
g_object_notify(table, "routes");
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static void
|
|
Packit Service |
a1bd4f |
remove_route(NmtWidgetList *list, int num, gpointer table)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE(table);
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
if (num >= priv->routes->len)
|
|
Packit Service |
a1bd4f |
return;
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
g_ptr_array_remove_index(priv->routes, num);
|
|
Packit Service |
a1bd4f |
nmt_widget_list_set_length(list, priv->routes->len);
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
g_object_notify(table, "routes");
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static void
|
|
Packit Service |
a1bd4f |
nmt_route_table_init(NmtRouteTable *table)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE(table);
|
|
Packit Service |
a1bd4f |
NmtNewtWidget * header, *empty;
|
|
Packit Service |
a1bd4f |
NmtNewtWidget * dest_prefix_label, *next_hop_label, *metric_label;
|
|
Packit Service |
a1bd4f |
int dest_prefix_width, next_hop_width, metric_width;
|
|
Packit Service |
a1bd4f |
char * text;
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
priv->routes = g_ptr_array_new_with_free_func((GDestroyNotify) nm_ip_route_unref);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
header = nmt_newt_grid_new();
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
text = g_strdup_printf("%s/%s", _("Destination"), _("Prefix"));
|
|
Packit Service |
a1bd4f |
dest_prefix_width = nmt_newt_text_width(text);
|
|
Packit Service |
a1bd4f |
dest_prefix_label =
|
|
Packit Service |
a1bd4f |
g_object_new(NMT_TYPE_NEWT_LABEL, "text", text, "style", NMT_NEWT_LABEL_PLAIN, NULL);
|
|
Packit Service |
a1bd4f |
g_free(text);
|
|
Packit Service |
a1bd4f |
nmt_newt_grid_add(NMT_NEWT_GRID(header), dest_prefix_label, 0, 0);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
text = _("Next Hop");
|
|
Packit Service |
a1bd4f |
next_hop_label =
|
|
Packit Service |
a1bd4f |
g_object_new(NMT_TYPE_NEWT_LABEL, "text", text, "style", NMT_NEWT_LABEL_PLAIN, NULL);
|
|
Packit Service |
a1bd4f |
next_hop_width = nmt_newt_text_width(text);
|
|
Packit Service |
a1bd4f |
nmt_newt_grid_add(NMT_NEWT_GRID(header), next_hop_label, 1, 0);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
text = _("Metric");
|
|
Packit Service |
a1bd4f |
metric_label =
|
|
Packit Service |
a1bd4f |
g_object_new(NMT_TYPE_NEWT_LABEL, "text", text, "style", NMT_NEWT_LABEL_PLAIN, NULL);
|
|
Packit Service |
a1bd4f |
metric_width = nmt_newt_text_width(text);
|
|
Packit Service |
a1bd4f |
nmt_newt_grid_add(NMT_NEWT_GRID(header), metric_label, 2, 0);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
priv->ip_entry_width = MAX(20, MAX(dest_prefix_width, next_hop_width));
|
|
Packit Service |
a1bd4f |
priv->metric_entry_width = MAX(7, metric_width);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
nmt_newt_widget_set_padding(dest_prefix_label,
|
|
Packit Service |
a1bd4f |
0,
|
|
Packit Service |
a1bd4f |
0,
|
|
Packit Service |
a1bd4f |
priv->ip_entry_width - dest_prefix_width,
|
|
Packit Service |
a1bd4f |
0);
|
|
Packit Service |
a1bd4f |
nmt_newt_widget_set_padding(next_hop_label, 2, 0, priv->ip_entry_width - next_hop_width, 0);
|
|
Packit Service |
a1bd4f |
nmt_newt_widget_set_padding(metric_label, 2, 0, priv->metric_entry_width - metric_width, 0);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
nmt_newt_grid_add(NMT_NEWT_GRID(table), header, 0, 0);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
empty = nmt_newt_label_new(_("No custom routes are defined."));
|
|
Packit Service |
a1bd4f |
priv->list = nmt_widget_list_new(create_route_entry, table, NULL, empty);
|
|
Packit Service |
a1bd4f |
g_signal_connect(priv->list, "add-clicked", G_CALLBACK(add_route), table);
|
|
Packit Service |
a1bd4f |
g_signal_connect(priv->list, "remove-clicked", G_CALLBACK(remove_route), table);
|
|
Packit Service |
a1bd4f |
nmt_newt_grid_add(NMT_NEWT_GRID(table), priv->list, 0, 1);
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static void
|
|
Packit Service |
a1bd4f |
nmt_route_table_finalize(GObject *object)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE(object);
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
g_ptr_array_unref(priv->routes);
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
G_OBJECT_CLASS(nmt_route_table_parent_class)->finalize(object);
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static void
|
|
Packit Service |
a1bd4f |
nmt_route_table_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE(object);
|
|
Packit Service |
a1bd4f |
GPtrArray * array;
|
|
Packit Service |
a1bd4f |
int i;
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
switch (prop_id) {
|
|
Packit Service |
a1bd4f |
case PROP_FAMILY:
|
|
Packit Service |
a1bd4f |
priv->family = g_value_get_int(value);
|
|
Packit Service |
a1bd4f |
break;
|
|
Packit Service |
a1bd4f |
case PROP_ROUTES:
|
|
Packit Service |
a1bd4f |
array = g_value_get_boxed(value);
|
|
Packit Service |
a1bd4f |
g_ptr_array_set_size(priv->routes, 0);
|
|
Packit Service |
a1bd4f |
for (i = 0; i < array->len; i++) {
|
|
Packit Service |
a1bd4f |
nm_ip_route_ref(array->pdata[i]);
|
|
Packit Service |
a1bd4f |
g_ptr_array_add(priv->routes, array->pdata[i]);
|
|
Packit Service |
a1bd4f |
}
|
|
Packit Service |
a1bd4f |
nmt_widget_list_set_length(NMT_WIDGET_LIST(priv->list), priv->routes->len);
|
|
Packit Service |
a1bd4f |
break;
|
|
Packit Service |
a1bd4f |
default:
|
|
Packit Service |
a1bd4f |
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
|
|
Packit Service |
a1bd4f |
break;
|
|
Packit Service |
a1bd4f |
}
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static void
|
|
Packit Service |
a1bd4f |
nmt_route_table_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE(object);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
switch (prop_id) {
|
|
Packit Service |
a1bd4f |
case PROP_FAMILY:
|
|
Packit Service |
a1bd4f |
g_value_set_int(value, priv->family);
|
|
Packit Service |
a1bd4f |
break;
|
|
Packit Service |
a1bd4f |
case PROP_ROUTES:
|
|
Packit Service |
a1bd4f |
g_value_set_boxed(value, priv->routes);
|
|
Packit Service |
a1bd4f |
break;
|
|
Packit Service |
a1bd4f |
default:
|
|
Packit Service |
a1bd4f |
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
|
|
Packit Service |
a1bd4f |
break;
|
|
Packit Service |
a1bd4f |
}
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static void
|
|
Packit Service |
a1bd4f |
nmt_route_table_class_init(NmtRouteTableClass *table_class)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
GObjectClass *object_class = G_OBJECT_CLASS(table_class);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
g_type_class_add_private(table_class, sizeof(NmtRouteTablePrivate));
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
/* virtual methods */
|
|
Packit Service |
a1bd4f |
object_class->set_property = nmt_route_table_set_property;
|
|
Packit Service |
a1bd4f |
object_class->get_property = nmt_route_table_get_property;
|
|
Packit Service |
a1bd4f |
object_class->finalize = nmt_route_table_finalize;
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
/**
|
|
Packit Service |
a1bd4f |
* NmtRouteTable:family:
|
|
Packit Service |
a1bd4f |
*
|
|
Packit Service |
a1bd4f |
* The network address family of the routes, eg %AF_INET
|
|
Packit Service |
a1bd4f |
*/
|
|
Packit Service |
a1bd4f |
g_object_class_install_property(
|
|
Packit Service |
a1bd4f |
object_class,
|
|
Packit Service |
a1bd4f |
PROP_FAMILY,
|
|
Packit Service |
a1bd4f |
g_param_spec_int("family",
|
|
Packit Service |
a1bd4f |
"",
|
|
Packit Service |
a1bd4f |
"",
|
|
Packit Service |
a1bd4f |
-1,
|
|
Packit Service |
a1bd4f |
G_MAXINT,
|
|
Packit Service |
a1bd4f |
-1,
|
|
Packit Service |
a1bd4f |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
|
|
Packit Service |
a1bd4f |
/**
|
|
Packit Service |
a1bd4f |
* NmtRouteTable:routes:
|
|
Packit Service |
a1bd4f |
*
|
|
Packit Service |
a1bd4f |
* The array of routes, suitable for binding to #NMSettingIP4Config:routes
|
|
Packit Service |
a1bd4f |
* or #NMSettingIP6Config:routes.
|
|
Packit Service |
a1bd4f |
*
|
|
Packit Service |
a1bd4f |
* Element-type: NMIPRoute
|
|
Packit Service |
a1bd4f |
*/
|
|
Packit Service |
a1bd4f |
g_object_class_install_property(object_class,
|
|
Packit Service |
a1bd4f |
PROP_ROUTES,
|
|
Packit Service |
a1bd4f |
g_param_spec_boxed("routes",
|
|
Packit Service |
a1bd4f |
"",
|
|
Packit Service |
a1bd4f |
"",
|
|
Packit Service |
a1bd4f |
G_TYPE_PTR_ARRAY,
|
|
Packit Service |
a1bd4f |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
Packit |
5756e2 |
}
|