static void add_route (NmtWidgetList *list, gpointer table) { NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table); NMIPRoute *route; if (priv->family == AF_INET) route = nm_ip_route_new (AF_INET, "0.0.0.0", 32, NULL, 0, NULL); else route = nm_ip_route_new (AF_INET6, "::", 128, NULL, 0, NULL); g_ptr_array_add (priv->routes, route); nmt_widget_list_set_length (list, priv->routes->len); g_object_notify (table, "routes"); }
/* * 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; }
void ip4_routes_dialog_update_setting (GtkWidget *dialog, NMSettingIPConfig *s_ip4) { GtkBuilder *builder; GtkWidget *widget; GtkTreeModel *model; GtkTreeIter tree_iter; gboolean iter_valid; g_return_if_fail (dialog != NULL); g_return_if_fail (s_ip4 != NULL); builder = g_object_get_data (G_OBJECT (dialog), "builder"); g_return_if_fail (builder != NULL); g_return_if_fail (GTK_IS_BUILDER (builder)); widget = GTK_WIDGET (gtk_builder_get_object (builder, "ip4_routes")); model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget)); iter_valid = gtk_tree_model_get_iter_first (model, &tree_iter); nm_setting_ip_config_clear_routes (s_ip4); while (iter_valid) { char *addr = NULL, *next_hop = NULL; guint32 prefix = 0; gint64 metric = -1; NMIPRoute *route; /* Address */ if (!utils_tree_model_get_address (model, &tree_iter, COL_ADDRESS, AF_INET, TRUE, &addr, NULL)) { g_warning ("%s: IPv4 address missing or invalid!", __func__); goto next; } /* Prefix */ if (!utils_tree_model_get_ip4_prefix (model, &tree_iter, COL_PREFIX, TRUE, &prefix, NULL)) { g_warning ("%s: IPv4 prefix/netmask missing or invalid!", __func__); g_free (addr); goto next; } /* Next hop (optional) */ if (!utils_tree_model_get_address (model, &tree_iter, COL_NEXT_HOP, AF_INET, FALSE, &next_hop, NULL)) { g_warning ("%s: IPv4 next hop invalid!", __func__); g_free (addr); goto next; } /* Metric (optional) */ if (!utils_tree_model_get_int64 (model, &tree_iter, COL_METRIC, 0, G_MAXUINT32, FALSE, &metric, NULL)) { g_warning ("%s: IPv4 metric invalid!", __func__); g_free (addr); g_free (next_hop); goto next; } route = nm_ip_route_new (AF_INET, addr, prefix, next_hop, metric, NULL); nm_setting_ip_config_add_route (s_ip4, route); nm_ip_route_unref (route); g_free (addr); g_free (next_hop); next: iter_valid = gtk_tree_model_iter_next (model, &tree_iter); } widget = GTK_WIDGET (gtk_builder_get_object (builder, "ip4_ignore_auto_routes")); g_object_set (s_ip4, NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)), NULL); widget = GTK_WIDGET (gtk_builder_get_object (builder, "ip4_never_default")); g_object_set (s_ip4, NM_SETTING_IP_CONFIG_NEVER_DEFAULT, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)), NULL); }
static GVariant * get_ip6_routes (void) { GVariant *value = NULL; GPtrArray *routes; char *tmp; int i; routes = g_ptr_array_new_full (256, (GDestroyNotify) nm_ip_route_unref); for (i = 1; i < 256; i++) { NMIPRoute *route; char buf[BUFLEN]; guint32 prefix; gchar **dest_prefix; GError *error = NULL; snprintf (buf, BUFLEN, "route_ipv6_network_%d", i); tmp = getenv (buf); if (!tmp || strlen (tmp) < 1) break; /* Split network string in "dest/prefix" format */ dest_prefix = g_strsplit (tmp, "/", 2); tmp = dest_prefix[1]; if (tmp) { long int tmp_prefix; errno = 0; tmp_prefix = strtol (tmp, NULL, 10); if (errno || tmp_prefix <= 0 || tmp_prefix > 128) { _LOGW ("Ignoring invalid static route prefix '%s'", tmp ? tmp : "NULL"); g_strfreev (dest_prefix); continue; } prefix = (guint32) tmp_prefix; } else { _LOGW ("Ignoring static route %d with no prefix length", i); g_strfreev (dest_prefix); continue; } snprintf (buf, BUFLEN, "route_ipv6_gateway_%d", i); tmp = getenv (buf); route = nm_ip_route_new (AF_INET6, dest_prefix[0], prefix, tmp, -1, &error); g_strfreev (dest_prefix); if (!route) { _LOGW ("Ignoring a route: %s", error->message); g_error_free (error); continue; } g_ptr_array_add (routes, route); } if (routes->len) value = nm_utils_ip6_routes_to_variant (routes); g_ptr_array_unref (routes); return value; }