platform: add buffer argument to platform to-string functions

Arguably, it is more convenient to use the static buffer as
it saves typing.

But having such a low-level function use a static buffer also
limits the way how to use it. As it was, you could not avoid
using the static buffer.

E.g. you cannot do:

  char buf[100];

  _LOGD ("nmp-object: %s; platform-link: %s",
         nmp_object_to_string (nmpobj, buf, sizeof(buf)),
         nm_platform_link_to_string (link));

This will fail for non-obvious reasons because both
to-string functions end up using the same static buffer.

Also change the to-string implementations to accept NULL
as valid and return it as "(null)".

https://bugzilla.gnome.org/show_bug.cgi?id=756427
This commit is contained in:
Thomas Haller 2015-10-12 10:27:33 +02:00
parent 7e2710fcd0
commit a2d59f5f10
14 changed files with 145 additions and 108 deletions

View file

@ -983,9 +983,11 @@ nmtst_platform_ip4_routes_equal (const NMPlatformIP4Route *a, const NMPlatformIP
for (i = 0; i < len; i++) {
if (nm_platform_ip4_route_cmp (&a[i], &b[i]) != 0) {
char buf[sizeof (_nm_platform_to_string_buffer)];
g_error ("Error comparing IPv4 route[%lu]: %s vs %s", (long unsigned) i,
nmtst_static_1024_01 (nm_platform_ip4_route_to_string (&a[i])),
nmtst_static_1024_02 (nm_platform_ip4_route_to_string (&b[i])));
nm_platform_ip4_route_to_string (&a[i], NULL, 0),
nm_platform_ip4_route_to_string (&b[i], buf, sizeof (buf)));
g_assert_not_reached ();
}
}
@ -1015,9 +1017,11 @@ nmtst_platform_ip6_routes_equal (const NMPlatformIP6Route *a, const NMPlatformIP
for (i = 0; i < len; i++) {
if (nm_platform_ip6_route_cmp (&a[i], &b[i]) != 0) {
char buf[sizeof (_nm_platform_to_string_buffer)];
g_error ("Error comparing IPv6 route[%lu]: %s vs %s", (long unsigned) i,
nmtst_static_1024_01 (nm_platform_ip6_route_to_string (&a[i])),
nmtst_static_1024_02 (nm_platform_ip6_route_to_string (&b[i])));
nm_platform_ip6_route_to_string (&a[i], NULL, 0),
nm_platform_ip6_route_to_string (&b[i], buf, sizeof (buf)));
g_assert_not_reached ();
}
}

View file

@ -320,7 +320,7 @@ process_classful_routes (GHashTable *options, guint32 priority, NMIP4Config *ip4
nm_ip4_config_add_route (ip4_config, &route);
nm_log_info (LOGD_DHCP, " static route %s",
nm_platform_ip4_route_to_string (&route));
nm_platform_ip4_route_to_string (&route, NULL, 0));
}
out:
@ -469,7 +469,7 @@ nm_dhcp_utils_ip4_config_from_options (int ifindex,
route.metric = priority;
nm_ip4_config_add_route (ip4_config, &route);
nm_log_dbg (LOGD_IP, "adding route for server identifier: %s",
nm_platform_ip4_route_to_string (&route));
nm_platform_ip4_route_to_string (&route, NULL, 0));
}
}
else

View file

@ -281,7 +281,7 @@ _platform_route_sync_add (const VTableIP *vtable, NMDefaultRouteManager *self, g
}
if (!success) {
_LOGW (vtable->vt->addr_family, "failed to add default route %s with effective metric %u",
vtable->vt->route_to_string (&entry->route), (guint) entry->effective_metric);
vtable->vt->route_to_string (&entry->route, NULL, 0), (guint) entry->effective_metric);
}
return TRUE;
}
@ -553,23 +553,23 @@ _resync_all (const VTableIP *vtable, NMDefaultRouteManager *self, const Entry *c
g_array_append_val (changed_metrics, expected_metric);
if (old_entry) {
_LOGD (vtable->vt->addr_family, LOG_ENTRY_FMT": sync:update %s (%u -> %u)", LOG_ENTRY_ARGS (i, entry),
vtable->vt->route_to_string (&entry->route), (guint) old_entry->effective_metric,
vtable->vt->route_to_string (&entry->route, NULL, 0), (guint) old_entry->effective_metric,
(guint) expected_metric);
} else {
_LOGD (vtable->vt->addr_family, LOG_ENTRY_FMT": sync:add %s (%u)", LOG_ENTRY_ARGS (i, entry),
vtable->vt->route_to_string (&entry->route), (guint) expected_metric);
vtable->vt->route_to_string (&entry->route, NULL, 0), (guint) expected_metric);
}
} else if (entry->effective_metric != expected_metric) {
g_array_append_val (changed_metrics, entry->effective_metric);
g_array_append_val (changed_metrics, expected_metric);
_LOGD (vtable->vt->addr_family, LOG_ENTRY_FMT": sync:metric %s (%u -> %u)", LOG_ENTRY_ARGS (i, entry),
vtable->vt->route_to_string (&entry->route), (guint) entry->effective_metric,
vtable->vt->route_to_string (&entry->route, NULL, 0), (guint) entry->effective_metric,
(guint) expected_metric);
} else {
if (!_vt_routes_has_entry (vtable, routes, entry)) {
g_array_append_val (changed_metrics, entry->effective_metric);
_LOGD (vtable->vt->addr_family, LOG_ENTRY_FMT": sync:re-add %s (%u -> %u)", LOG_ENTRY_ARGS (i, entry),
vtable->vt->route_to_string (&entry->route), (guint) entry->effective_metric,
vtable->vt->route_to_string (&entry->route, NULL, 0), (guint) entry->effective_metric,
(guint) entry->effective_metric);
}
}
@ -636,7 +636,7 @@ _entry_at_idx_update (const VTableIP *vtable, NMDefaultRouteManager *self, guint
_LOGD (vtable->vt->addr_family, LOG_ENTRY_FMT": %s %s (%"G_GUINT32_FORMAT")",
LOG_ENTRY_ARGS (entry_idx, entry),
old_entry ? "record:update" : "record:add ",
vtable->vt->route_to_string (&entry->route),
vtable->vt->route_to_string (&entry->route, NULL, 0),
entry->effective_metric);
g_ptr_array_sort_with_data (entries, _sort_entries_cmp, NULL);
@ -658,7 +658,7 @@ _entry_at_idx_remove (const VTableIP *vtable, NMDefaultRouteManager *self, guint
entry = g_ptr_array_index (entries, entry_idx);
_LOGD (vtable->vt->addr_family, LOG_ENTRY_FMT": record:remove %s (%u)", LOG_ENTRY_ARGS (entry_idx, entry),
vtable->vt->route_to_string (&entry->route), (guint) entry->effective_metric);
vtable->vt->route_to_string (&entry->route, NULL, 0), (guint) entry->effective_metric);
/* Remove the entry from the list (but don't free it yet) */
g_ptr_array_index (entries, entry_idx) = NULL;

View file

@ -1199,7 +1199,7 @@ nm_ip4_config_dump (const NMIP4Config *config, const char *detail)
/* addresses */
for (i = 0; i < nm_ip4_config_get_num_addresses (config); i++)
g_message (" a: %s", nm_platform_ip4_address_to_string (nm_ip4_config_get_address (config, i)));
g_message (" a: %s", nm_platform_ip4_address_to_string (nm_ip4_config_get_address (config, i), NULL, 0));
/* default gateway */
if (nm_ip4_config_has_gateway (config)) {
@ -1215,7 +1215,7 @@ nm_ip4_config_dump (const NMIP4Config *config, const char *detail)
/* routes */
for (i = 0; i < nm_ip4_config_get_num_routes (config); i++)
g_message (" rt: %s", nm_platform_ip4_route_to_string (nm_ip4_config_get_route (config, i)));
g_message (" rt: %s", nm_platform_ip4_route_to_string (nm_ip4_config_get_route (config, i), NULL, 0));
/* domains */
for (i = 0; i < nm_ip4_config_get_num_domains (config); i++)

View file

@ -1166,7 +1166,7 @@ nm_ip6_config_dump (const NMIP6Config *config, const char *detail)
/* addresses */
for (i = 0; i < nm_ip6_config_get_num_addresses (config); i++)
g_message (" a: %s", nm_platform_ip6_address_to_string (nm_ip6_config_get_address (config, i)));
g_message (" a: %s", nm_platform_ip6_address_to_string (nm_ip6_config_get_address (config, i), NULL, 0));
/* default gateway */
tmp = nm_ip6_config_get_gateway (config);
@ -1181,7 +1181,7 @@ nm_ip6_config_dump (const NMIP6Config *config, const char *detail)
/* routes */
for (i = 0; i < nm_ip6_config_get_num_routes (config); i++)
g_message (" rt: %s", nm_platform_ip6_route_to_string (nm_ip6_config_get_route (config, i)));
g_message (" rt: %s", nm_platform_ip6_route_to_string (nm_ip6_config_get_route (config, i), NULL, 0));
/* domains */
for (i = 0; i < nm_ip6_config_get_num_domains (config); i++)

View file

@ -467,12 +467,12 @@ _vx_route_sync (const VTableIP *vtable, NMRouteManager *self, int ifindex, const
if (_LOGT_ENABLED (vtable->vt->addr_family)) {
for (i = 0; i < known_routes_idx->len; i++) {
_LOGT (vtable->vt->addr_family, "%3d: sync new route #%u: %s",
ifindex, i, vtable->vt->route_to_string (VTABLE_ROUTE_INDEX (vtable, known_routes, i)));
ifindex, i, vtable->vt->route_to_string (VTABLE_ROUTE_INDEX (vtable, known_routes, i), NULL, 0));
}
for (i = 0; i < ipx_routes->index->len; i++)
_LOGT (vtable->vt->addr_family, "%3d: STATE: has #%u - %s (%lld)",
ifindex, i,
vtable->vt->route_to_string (ipx_routes->index->entries[i]),
vtable->vt->route_to_string (ipx_routes->index->entries[i], NULL, 0),
(long long) g_array_index (ipx_routes->effective_metrics, gint64, i));
}
@ -512,7 +512,8 @@ _vx_route_sync (const VTableIP *vtable, NMRouteManager *self, int ifindex, const
cur_ipx_route->rx.ifindex = ifindex;
cur_ipx_route->rx.metric = vtable->vt->metric_normalize (cur_ipx_route->rx.metric);
ipx_routes_changed = TRUE;
_LOGT (vtable->vt->addr_family, "%3d: STATE: update #%u - %s", ifindex, i_ipx_routes, vtable->vt->route_to_string (cur_ipx_route));
_LOGT (vtable->vt->addr_family, "%3d: STATE: update #%u - %s", ifindex, i_ipx_routes,
vtable->vt->route_to_string (cur_ipx_route, NULL, 0));
}
} else if (cur_known_route) {
g_assert (!cur_ipx_route || route_id_cmp_result > 0);
@ -570,7 +571,8 @@ _vx_route_sync (const VTableIP *vtable, NMRouteManager *self, int ifindex, const
&& cur_plat_route->rx.metric == *p_effective_metric) {
/* we are about to delete cur_ipx_route and we have a matching route
* in platform. Delete it. */
_LOGT (vtable->vt->addr_family, "%3d: platform rt-rm #%u - %s", ifindex, i_plat_routes, vtable->vt->route_to_string (cur_plat_route));
_LOGT (vtable->vt->addr_family, "%3d: platform rt-rm #%u - %s", ifindex, i_plat_routes,
vtable->vt->route_to_string (cur_plat_route, NULL, 0));
vtable->vt->route_delete (priv->platform, ifindex, cur_plat_route);
}
}
@ -582,7 +584,8 @@ _vx_route_sync (const VTableIP *vtable, NMRouteManager *self, int ifindex, const
for (i = 0; i < to_delete_indexes->len; i++) {
guint idx = g_array_index (to_delete_indexes, guint, i);
_LOGT (vtable->vt->addr_family, "%3d: STATE: delete #%u - %s", ifindex, idx, vtable->vt->route_to_string (ipx_routes->index->entries[idx]));
_LOGT (vtable->vt->addr_family, "%3d: STATE: delete #%u - %s", ifindex, idx,
vtable->vt->route_to_string (ipx_routes->index->entries[idx], NULL, 0));
g_array_index (to_delete_indexes, guint, i) = _route_index_reverse_idx (vtable, ipx_routes->index, idx, ipx_routes->entries);
}
g_array_sort (to_delete_indexes, (GCompareFunc) _sort_indexes_cmp);
@ -606,7 +609,8 @@ _vx_route_sync (const VTableIP *vtable, NMRouteManager *self, int ifindex, const
g_array_index (ipx_routes->effective_metrics_reverse, gint64, j++) = -1;
_LOGT (vtable->vt->addr_family, "%3d: STATE: added #%u - %s", ifindex, ipx_routes->entries->len - 1, vtable->vt->route_to_string (ipx_route));
_LOGT (vtable->vt->addr_family, "%3d: STATE: added #%u - %s", ifindex, ipx_routes->entries->len - 1,
vtable->vt->route_to_string (ipx_route, NULL, 0));
}
g_ptr_array_unref (to_add_routes);
}
@ -693,7 +697,7 @@ _vx_route_sync (const VTableIP *vtable, NMRouteManager *self, int ifindex, const
next:
_LOGT (vtable->vt->addr_family, "%3d: new metric #%u - %s (%lld)",
ifindex, i_ipx_routes,
vtable->vt->route_to_string (cur_ipx_route),
vtable->vt->route_to_string (cur_ipx_route, NULL, 0),
(long long) *p_effective_metric);
}
}
@ -716,7 +720,7 @@ next:
g_assert (cur_plat_route->rx.ifindex == ifindex);
_LOGT (vtable->vt->addr_family, "%3d: platform rt #%u - %s", ifindex, i_plat_routes, vtable->vt->route_to_string (cur_plat_route));
_LOGT (vtable->vt->addr_family, "%3d: platform rt #%u - %s", ifindex, i_plat_routes, vtable->vt->route_to_string (cur_plat_route, NULL, 0));
/* skip over @cur_ipx_route that are ordered before @cur_plat_route */
while ( cur_ipx_route
@ -870,7 +874,7 @@ next:
_LOGD (vtable->vt->addr_family,
"ignore error adding IPv%c route to kernel: %s",
vtable->vt->is_ip4 ? '4' : '6',
vtable->vt->route_to_string (cur_ipx_route));
vtable->vt->route_to_string (cur_ipx_route, NULL, 0));
} else {
/* Remember that there was a failure, but for now continue trying
* to sync the remaining routes. */

View file

@ -430,7 +430,7 @@ nm_platform_link_get_all (NMPlatform *self)
for (i = 0; i < links->len; i++) {
item = &g_array_index (links, NMPlatformLink, i);
_LOGT ("link-get: %3d: %s", i, nm_platform_link_to_string (item));
_LOGT ("link-get: %3d: %s", i, nm_platform_link_to_string (item, NULL, 0));
nm_assert (item->ifindex > 0 && !g_hash_table_contains (unseen, GINT_TO_POINTER (item->ifindex)));
@ -486,7 +486,7 @@ nm_platform_link_get_all (NMPlatform *self)
if (item->parent > 0 && g_hash_table_contains (unseen, GINT_TO_POINTER (item->parent)))
continue;
_LOGT ("link-get: add %3d -> %3d: %s", i, j, nm_platform_link_to_string (item));
_LOGT ("link-get: add %3d -> %3d: %s", i, j, nm_platform_link_to_string (item, NULL, 0));
g_hash_table_remove (unseen, GINT_TO_POINTER (item->ifindex));
g_array_index (result, NMPlatformLink, j++) = *item;
@ -499,7 +499,7 @@ nm_platform_link_get_all (NMPlatform *self)
* This can happen for veth pairs where each peer is parent of the other end. */
item = &g_array_index (links, NMPlatformLink, first_idx);
_LOGT ("link-get: add (loop) %3d -> %3d: %s", first_idx, j, nm_platform_link_to_string (item));
_LOGT ("link-get: add (loop) %3d -> %3d: %s", first_idx, j, nm_platform_link_to_string (item, NULL, 0));
g_hash_table_remove (unseen, GINT_TO_POINTER (item->ifindex));
g_array_index (result, NMPlatformLink, j++) = *item;
@ -1918,7 +1918,7 @@ nm_platform_ip4_address_add (NMPlatform *self,
if (label)
g_strlcpy (addr.label, label, sizeof (addr.label));
_LOGD ("address: adding or updating IPv4 address: %s", nm_platform_ip4_address_to_string (&addr));
_LOGD ("address: adding or updating IPv4 address: %s", nm_platform_ip4_address_to_string (&addr, NULL, 0));
}
return klass->ip4_address_add (self, ifindex, address, plen, peer_address, lifetime, preferred, label);
}
@ -1953,7 +1953,7 @@ nm_platform_ip6_address_add (NMPlatform *self,
addr.preferred = preferred;
addr.flags = flags;
_LOGD ("address: adding or updating IPv6 address: %s", nm_platform_ip6_address_to_string (&addr));
_LOGD ("address: adding or updating IPv6 address: %s", nm_platform_ip6_address_to_string (&addr, NULL, 0));
}
return klass->ip6_address_add (self, ifindex, address, plen, peer_address, lifetime, preferred, flags);
}
@ -2236,7 +2236,7 @@ nm_platform_ip4_route_add (NMPlatform *self,
route.mss = mss;
route.pref_src = pref_src;
_LOGD ("route: adding or updating IPv4 route: %s", nm_platform_ip4_route_to_string (&route));
_LOGD ("route: adding or updating IPv4 route: %s", nm_platform_ip4_route_to_string (&route, NULL, 0));
}
return klass->ip4_route_add (self, ifindex, source, network, plen, gateway, pref_src, metric, mss);
}
@ -2263,7 +2263,7 @@ nm_platform_ip6_route_add (NMPlatform *self,
route.metric = metric;
route.mss = mss;
_LOGD ("route: adding or updating IPv6 route: %s", nm_platform_ip6_route_to_string (&route));
_LOGD ("route: adding or updating IPv6 route: %s", nm_platform_ip6_route_to_string (&route, NULL, 0));
}
return klass->ip6_route_add (self, ifindex, source, network, plen, gateway, metric, mss);
}
@ -2368,10 +2368,34 @@ _lifetime_summary_to_string (gint32 now, guint32 timestamp, guint32 preferred, g
return buf;
}
char _nm_platform_to_string_buffer[256];
char _nm_platform_to_string_buffer[];
static gboolean
_to_string_buffer_init (gconstpointer obj, char **buf, gsize *len)
{
if (!*buf) {
*buf = _nm_platform_to_string_buffer;
*len = sizeof (_nm_platform_to_string_buffer);
}
if (!obj) {
g_strlcpy (*buf, "(null)", *len);
return FALSE;
}
return TRUE;
}
/**
* nm_platform_link_to_string:
* @route: pointer to NMPlatformLink address structure
* @buf: (allow-none): an optional buffer. If %NULL, a static buffer is used.
* @len: the size of the @buf. If @buf is %NULL, this argument is ignored.
*
* A method for converting an link struct into a string representation.
*
* Returns: a string representation of the link.
*/
const char *
nm_platform_link_to_string (const NMPlatformLink *link)
nm_platform_link_to_string (const NMPlatformLink *link, char *buf, gsize len)
{
char master[20];
char parent[20];
@ -2381,8 +2405,8 @@ nm_platform_link_to_string (const NMPlatformLink *link)
gs_free char *str_addr = NULL;
gs_free char *str_inet6_token = NULL;
if (!link)
return "(unknown link)";
if (!_to_string_buffer_init (link, &buf, &len))
return buf;
str_flags = g_string_new (NULL);
if (NM_FLAGS_HAS (link->flags, IFF_NOARP))
@ -2438,7 +2462,7 @@ nm_platform_link_to_string (const NMPlatformLink *link)
if (link->inet6_token.is_valid)
str_inet6_token = nm_utils_hwaddr_ntoa (&link->inet6_token.iid, sizeof (link->inet6_token.iid));
g_snprintf (_nm_platform_to_string_buffer, sizeof (_nm_platform_to_string_buffer),
g_snprintf (buf, len,
"%d: " /* ifindex */
"%s" /* name */
"%s" /* parent */
@ -2475,23 +2499,23 @@ nm_platform_link_to_string (const NMPlatformLink *link)
link->driver ? " driver " : "",
link->driver ? link->driver : "");
g_string_free (str_flags, TRUE);
return _nm_platform_to_string_buffer;
return buf;
}
/**
* nm_platform_ip4_address_to_string:
* @route: pointer to NMPlatformIP4Address address structure
* @buf: (allow-none): an optional buffer. If %NULL, a static buffer is used.
* @len: the size of the @buf. If @buf is %NULL, this argument is ignored.
*
* A method for converting an address struct into a string representation.
*
* Example output: ""
*
* Returns: a string representation of the address. The returned string
* is an internal buffer, so do not keep or free the returned string.
* Also, this function is not thread safe.
* Returns: a string representation of the address.
*/
const char *
nm_platform_ip4_address_to_string (const NMPlatformIP4Address *address)
nm_platform_ip4_address_to_string (const NMPlatformIP4Address *address, char *buf, gsize len)
{
char s_address[INET_ADDRSTRLEN];
char s_peer[INET_ADDRSTRLEN];
@ -2502,7 +2526,8 @@ nm_platform_ip4_address_to_string (const NMPlatformIP4Address *address)
const char *str_lft_p, *str_pref_p, *str_time_p;
gint32 now = nm_utils_get_monotonic_timestamp_s ();
g_return_val_if_fail (address, "(unknown)");
if (!_to_string_buffer_init (address, &buf, &len))
return buf;
inet_ntop (AF_INET, &address->address, s_address, sizeof (s_address));
@ -2528,14 +2553,15 @@ nm_platform_ip4_address_to_string (const NMPlatformIP4Address *address)
now, str_pref, sizeof (str_pref)) );
str_time_p = _lifetime_summary_to_string (now, address->timestamp, address->preferred, address->lifetime, str_time, sizeof (str_time));
g_snprintf (_nm_platform_to_string_buffer, sizeof (_nm_platform_to_string_buffer), "%s/%d lft %s pref %s%s%s%s%s src %s",
g_snprintf (buf, len,
"%s/%d lft %s pref %s%s%s%s%s src %s",
s_address, address->plen, str_lft_p, str_pref_p, str_time_p,
str_peer ? str_peer : "",
str_dev,
str_label,
source_to_string (address->source));
g_free (str_peer);
return _nm_platform_to_string_buffer;
return buf;
}
/**
@ -2584,17 +2610,17 @@ nm_platform_addr_flags2str (int flags, char *buf, size_t size)
/**
* nm_platform_ip6_address_to_string:
* @route: pointer to NMPlatformIP6Address address structure
* @buf: (allow-none): an optional buffer. If %NULL, a static buffer is used.
* @len: the size of the @buf. If @buf is %NULL, this argument is ignored.
*
* A method for converting an address struct into a string representation.
*
* Example output: "2001:db8:0:f101::1/64 lft 4294967295 pref 4294967295 time 16922666 on dev em1"
*
* Returns: a string representation of the address. The returned string
* is an internal buffer, so do not keep or free the returned string.
* Also, this function is not thread safe.
* Returns: a string representation of the address.
*/
const char *
nm_platform_ip6_address_to_string (const NMPlatformIP6Address *address)
nm_platform_ip6_address_to_string (const NMPlatformIP6Address *address, char *buf, gsize len)
{
#define S_FLAGS_PREFIX " flags "
char s_flags[256];
@ -2606,7 +2632,8 @@ nm_platform_ip6_address_to_string (const NMPlatformIP6Address *address)
const char *str_lft_p, *str_pref_p, *str_time_p;
gint32 now = nm_utils_get_monotonic_timestamp_s ();
g_return_val_if_fail (address, "(unknown)");
if (!_to_string_buffer_init (address, &buf, &len))
return buf;
inet_ntop (AF_INET6, &address->address, s_address, sizeof (s_address));
@ -2633,44 +2660,46 @@ nm_platform_ip6_address_to_string (const NMPlatformIP6Address *address)
now, str_pref, sizeof (str_pref)) );
str_time_p = _lifetime_summary_to_string (now, address->timestamp, address->preferred, address->lifetime, str_time, sizeof (str_time));
g_snprintf (_nm_platform_to_string_buffer, sizeof (_nm_platform_to_string_buffer), "%s/%d lft %s pref %s%s%s%s%s src %s",
g_snprintf (buf, len,
"%s/%d lft %s pref %s%s%s%s%s src %s",
s_address, address->plen, str_lft_p, str_pref_p, str_time_p,
str_peer ? str_peer : "",
str_dev,
s_flags,
source_to_string (address->source));
g_free (str_peer);
return _nm_platform_to_string_buffer;
return buf;
}
/**
* nm_platform_ip4_route_to_string:
* @route: pointer to NMPlatformIP4Route route structure
* @buf: (allow-none): an optional buffer. If %NULL, a static buffer is used.
* @len: the size of the @buf. If @buf is %NULL, this argument is ignored.
*
* A method for converting a route struct into a string representation.
*
* Example output: "192.168.1.0/24 via 0.0.0.0 dev em1 metric 0 mss 0"
*
* Returns: a string representation of the route. The returned string
* is an internal buffer, so do not keep or free the returned string.
* Also, this function is not thread safe.
* Returns: a string representation of the route.
*/
const char *
nm_platform_ip4_route_to_string (const NMPlatformIP4Route *route)
nm_platform_ip4_route_to_string (const NMPlatformIP4Route *route, char *buf, gsize len)
{
char s_network[INET_ADDRSTRLEN], s_gateway[INET_ADDRSTRLEN];
char s_pref_src[INET_ADDRSTRLEN];
char str_dev[TO_STRING_DEV_BUF_SIZE];
char str_scope[30];
g_return_val_if_fail (route, "(unknown)");
if (!_to_string_buffer_init (route, &buf, &len))
return buf;
inet_ntop (AF_INET, &route->network, s_network, sizeof(s_network));
inet_ntop (AF_INET, &route->gateway, s_gateway, sizeof(s_gateway));
_to_string_dev (NULL, route->ifindex, str_dev, sizeof (str_dev));
g_snprintf (_nm_platform_to_string_buffer, sizeof (_nm_platform_to_string_buffer),
g_snprintf (buf, len,
"%s/%d"
" via %s"
"%s"
@ -2690,35 +2719,36 @@ nm_platform_ip4_route_to_string (const NMPlatformIP4Route *route)
route->scope_inv ? (rtnl_scope2str (nm_platform_route_scope_inv (route->scope_inv), str_scope, sizeof (str_scope))) : "",
route->pref_src ? " pref-src " : "",
route->pref_src ? inet_ntop (AF_INET, &route->pref_src, s_pref_src, sizeof(s_pref_src)) : "");
return _nm_platform_to_string_buffer;
return buf;
}
/**
* nm_platform_ip6_route_to_string:
* @route: pointer to NMPlatformIP6Route route structure
* @buf: (allow-none): an optional buffer. If %NULL, a static buffer is used.
* @len: the size of the @buf. If @buf is %NULL, this argument is ignored.
*
* A method for converting a route struct into a string representation.
*
* Example output: "ff02::fb/128 via :: dev em1 metric 0"
*
* Returns: a string representation of the route. The returned string
* is an internal buffer, so do not keep or free the returned string.
* Also, this function is not thread safe.
* Returns: a string representation of the route.
*/
const char *
nm_platform_ip6_route_to_string (const NMPlatformIP6Route *route)
nm_platform_ip6_route_to_string (const NMPlatformIP6Route *route, char *buf, gsize len)
{
char s_network[INET6_ADDRSTRLEN], s_gateway[INET6_ADDRSTRLEN];
char str_dev[TO_STRING_DEV_BUF_SIZE];
g_return_val_if_fail (route, "(unknown)");
if (!_to_string_buffer_init (route, &buf, &len))
return buf;
inet_ntop (AF_INET6, &route->network, s_network, sizeof(s_network));
inet_ntop (AF_INET6, &route->gateway, s_gateway, sizeof(s_gateway));
_to_string_dev (NULL, route->ifindex, str_dev, sizeof (str_dev));
g_snprintf (_nm_platform_to_string_buffer, sizeof (_nm_platform_to_string_buffer),
g_snprintf (buf, len,
"%s/%d"
" via %s"
"%s"
@ -2732,7 +2762,7 @@ nm_platform_ip6_route_to_string (const NMPlatformIP6Route *route)
route->metric,
route->mss,
source_to_string (route->source));
return _nm_platform_to_string_buffer;
return buf;
}
#define _CMP_SELF(a, b) \
@ -2983,31 +3013,31 @@ static void
log_link (NMPlatform *self, NMPObjectType obj_type, int ifindex, NMPlatformLink *device, NMPlatformSignalChangeType change_type, gpointer user_data)
{
_LOGD ("signal: link %7s: %s", nm_platform_signal_change_type_to_string (change_type), nm_platform_link_to_string (device));
_LOGD ("signal: link %7s: %s", nm_platform_signal_change_type_to_string (change_type), nm_platform_link_to_string (device, NULL, 0));
}
static void
log_ip4_address (NMPlatform *self, NMPObjectType obj_type, int ifindex, NMPlatformIP4Address *address, NMPlatformSignalChangeType change_type, gpointer user_data)
{
_LOGD ("signal: address 4 %7s: %s", nm_platform_signal_change_type_to_string (change_type), nm_platform_ip4_address_to_string (address));
_LOGD ("signal: address 4 %7s: %s", nm_platform_signal_change_type_to_string (change_type), nm_platform_ip4_address_to_string (address, NULL, 0));
}
static void
log_ip6_address (NMPlatform *self, NMPObjectType obj_type, int ifindex, NMPlatformIP6Address *address, NMPlatformSignalChangeType change_type, gpointer user_data)
{
_LOGD ("signal: address 6 %7s: %s", nm_platform_signal_change_type_to_string (change_type), nm_platform_ip6_address_to_string (address));
_LOGD ("signal: address 6 %7s: %s", nm_platform_signal_change_type_to_string (change_type), nm_platform_ip6_address_to_string (address, NULL, 0));
}
static void
log_ip4_route (NMPlatform *self, NMPObjectType obj_type, int ifindex, NMPlatformIP4Route *route, NMPlatformSignalChangeType change_type, gpointer user_data)
{
_LOGD ("signal: route 4 %7s: %s", nm_platform_signal_change_type_to_string (change_type), nm_platform_ip4_route_to_string (route));
_LOGD ("signal: route 4 %7s: %s", nm_platform_signal_change_type_to_string (change_type), nm_platform_ip4_route_to_string (route, NULL, 0));
}
static void
log_ip6_route (NMPlatform *self, NMPObjectType obj_type, int ifindex, NMPlatformIP6Route *route, NMPlatformSignalChangeType change_type, gpointer user_data)
{
_LOGD ("signal: route 6 %7s: %s", nm_platform_signal_change_type_to_string (change_type), nm_platform_ip6_route_to_string (route));
_LOGD ("signal: route 6 %7s: %s", nm_platform_signal_change_type_to_string (change_type), nm_platform_ip6_route_to_string (route, NULL, 0));
}
/******************************************************************/
@ -3084,7 +3114,7 @@ const NMPlatformVTableRoute nm_platform_vtable_route_v4 = {
.addr_family = AF_INET,
.sizeof_route = sizeof (NMPlatformIP4Route),
.route_cmp = (int (*) (const NMPlatformIPXRoute *a, const NMPlatformIPXRoute *b)) nm_platform_ip4_route_cmp,
.route_to_string = (const char *(*) (const NMPlatformIPXRoute *route)) nm_platform_ip4_route_to_string,
.route_to_string = (const char *(*) (const NMPlatformIPXRoute *route, char *buf, gsize len)) nm_platform_ip4_route_to_string,
.route_get_all = nm_platform_ip4_route_get_all,
.route_add = _vtr_v4_route_add,
.route_delete = _vtr_v4_route_delete,
@ -3097,7 +3127,7 @@ const NMPlatformVTableRoute nm_platform_vtable_route_v6 = {
.addr_family = AF_INET6,
.sizeof_route = sizeof (NMPlatformIP6Route),
.route_cmp = (int (*) (const NMPlatformIPXRoute *a, const NMPlatformIPXRoute *b)) nm_platform_ip6_route_cmp,
.route_to_string = (const char *(*) (const NMPlatformIPXRoute *route)) nm_platform_ip6_route_to_string,
.route_to_string = (const char *(*) (const NMPlatformIPXRoute *route, char *buf, gsize len)) nm_platform_ip6_route_to_string,
.route_get_all = nm_platform_ip6_route_get_all,
.route_add = _vtr_v6_route_add,
.route_delete = _vtr_v6_route_delete,

View file

@ -313,7 +313,7 @@ typedef struct {
int addr_family;
gsize sizeof_route;
int (*route_cmp) (const NMPlatformIPXRoute *a, const NMPlatformIPXRoute *b);
const char *(*route_to_string) (const NMPlatformIPXRoute *route);
const char *(*route_to_string) (const NMPlatformIPXRoute *route, char *buf, gsize len);
GArray *(*route_get_all) (NMPlatform *self, int ifindex, NMPlatformGetRouteFlags flags);
gboolean (*route_add) (NMPlatform *self, int ifindex, const NMPlatformIPXRoute *route, gint64 metric);
gboolean (*route_delete) (NMPlatform *self, int ifindex, const NMPlatformIPXRoute *route);
@ -324,8 +324,6 @@ typedef struct {
extern const NMPlatformVTableRoute nm_platform_vtable_route_v4;
extern const NMPlatformVTableRoute nm_platform_vtable_route_v6;
extern char _nm_platform_to_string_buffer[256];
typedef struct {
int peer;
} NMPlatformVethProperties;
@ -745,11 +743,13 @@ gboolean nm_platform_ip6_route_add (NMPlatform *self, int ifindex, NMIPConfigSou
gboolean nm_platform_ip4_route_delete (NMPlatform *self, int ifindex, in_addr_t network, int plen, guint32 metric);
gboolean nm_platform_ip6_route_delete (NMPlatform *self, int ifindex, struct in6_addr network, int plen, guint32 metric);
const char *nm_platform_link_to_string (const NMPlatformLink *link);
const char *nm_platform_ip4_address_to_string (const NMPlatformIP4Address *address);
const char *nm_platform_ip6_address_to_string (const NMPlatformIP6Address *address);
const char *nm_platform_ip4_route_to_string (const NMPlatformIP4Route *route);
const char *nm_platform_ip6_route_to_string (const NMPlatformIP6Route *route);
extern char _nm_platform_to_string_buffer[1024];
const char *nm_platform_link_to_string (const NMPlatformLink *link, char *buf, gsize len);
const char *nm_platform_ip4_address_to_string (const NMPlatformIP4Address *address, char *buf, gsize len);
const char *nm_platform_ip6_address_to_string (const NMPlatformIP6Address *address, char *buf, gsize len);
const char *nm_platform_ip4_route_to_string (const NMPlatformIP4Route *route, char *buf, gsize len);
const char *nm_platform_ip6_route_to_string (const NMPlatformIP6Route *route, char *buf, gsize len);
int nm_platform_link_cmp (const NMPlatformLink *a, const NMPlatformLink *b);
int nm_platform_ip4_address_cmp (const NMPlatformIP4Address *a, const NMPlatformIP4Address *b);

View file

@ -372,7 +372,6 @@ nmp_object_to_string (const NMPObject *obj, NMPObjectToStringMode to_string_mode
const NMPClass *klass;
char buf2[sizeof (_nm_platform_to_string_buffer)];
char buf3[sizeof (_nm_platform_to_string_buffer)];
const char *str;
if (!buf) {
buf = _nm_platform_to_string_buffer;
@ -380,7 +379,7 @@ nmp_object_to_string (const NMPObject *obj, NMPObjectToStringMode to_string_mode
}
if (!obj) {
g_strlcpy (buf, "NULL", buf_size);
g_strlcpy (buf, "(null)", buf_size);
return buf;
}
@ -392,7 +391,7 @@ nmp_object_to_string (const NMPObject *obj, NMPObjectToStringMode to_string_mode
case NMP_OBJECT_TO_STRING_ID:
return klass->cmd_plobj_to_string_id (&obj->object, buf, buf_size);
case NMP_OBJECT_TO_STRING_ALL:
g_strlcpy (buf2, NMP_OBJECT_GET_CLASS (obj)->cmd_plobj_to_string (&obj->object), sizeof (buf2));
NMP_OBJECT_GET_CLASS (obj)->cmd_plobj_to_string (&obj->object, buf2, sizeof (buf2));
if (NMP_OBJECT_GET_TYPE (obj) == NMP_OBJECT_TYPE_LINK) {
g_snprintf (buf3, sizeof (buf3),
@ -411,9 +410,7 @@ nmp_object_to_string (const NMPObject *obj, NMPObjectToStringMode to_string_mode
buf3, buf2);
return buf;
case NMP_OBJECT_TO_STRING_PUBLIC:
str = NMP_OBJECT_GET_CLASS (obj)->cmd_plobj_to_string (&obj->object);
if (str != buf)
g_strlcpy (buf, str, buf_size);
NMP_OBJECT_GET_CLASS (obj)->cmd_plobj_to_string (&obj->object, buf, buf_size);
return buf;
default:
g_return_val_if_reached ("ERROR");
@ -1800,7 +1797,7 @@ const NMPClass _nmp_classes[NMP_OBJECT_TYPE_MAX] = {
.cmd_plobj_id_equal = _vt_cmd_plobj_id_equal_link,
.cmd_plobj_id_hash = _vt_cmd_plobj_id_hash_link,
.cmd_plobj_to_string_id = _vt_cmd_plobj_to_string_id_link,
.cmd_plobj_to_string = (const char *(*) (const NMPlatformObject *obj)) nm_platform_link_to_string,
.cmd_plobj_to_string = (const char *(*) (const NMPlatformObject *obj, char *buf, gsize len)) nm_platform_link_to_string,
.cmd_plobj_cmp = (int (*) (const NMPlatformObject *obj1, const NMPlatformObject *obj2)) nm_platform_link_cmp,
},
[NMP_OBJECT_TYPE_IP4_ADDRESS - 1] = {
@ -1824,7 +1821,7 @@ const NMPClass _nmp_classes[NMP_OBJECT_TYPE_MAX] = {
.cmd_plobj_id_equal = _vt_cmd_plobj_id_equal_ip4_address,
.cmd_plobj_id_hash = _vt_cmd_plobj_id_hash_ip4_address,
.cmd_plobj_to_string_id = _vt_cmd_plobj_to_string_id_ip4_address,
.cmd_plobj_to_string = (const char *(*) (const NMPlatformObject *obj)) nm_platform_ip4_address_to_string,
.cmd_plobj_to_string = (const char *(*) (const NMPlatformObject *obj, char *buf, gsize len)) nm_platform_ip4_address_to_string,
.cmd_plobj_cmp = (int (*) (const NMPlatformObject *obj1, const NMPlatformObject *obj2)) nm_platform_ip4_address_cmp,
},
[NMP_OBJECT_TYPE_IP6_ADDRESS - 1] = {
@ -1848,7 +1845,7 @@ const NMPClass _nmp_classes[NMP_OBJECT_TYPE_MAX] = {
.cmd_plobj_id_equal = _vt_cmd_plobj_id_equal_ip6_address,
.cmd_plobj_id_hash = _vt_cmd_plobj_id_hash_ip6_address,
.cmd_plobj_to_string_id = _vt_cmd_plobj_to_string_id_ip6_address,
.cmd_plobj_to_string = (const char *(*) (const NMPlatformObject *obj)) nm_platform_ip6_address_to_string,
.cmd_plobj_to_string = (const char *(*) (const NMPlatformObject *obj, char *buf, gsize len)) nm_platform_ip6_address_to_string,
.cmd_plobj_cmp = (int (*) (const NMPlatformObject *obj1, const NMPlatformObject *obj2)) nm_platform_ip6_address_cmp
},
[NMP_OBJECT_TYPE_IP4_ROUTE - 1] = {
@ -1872,7 +1869,7 @@ const NMPClass _nmp_classes[NMP_OBJECT_TYPE_MAX] = {
.cmd_plobj_id_equal = _vt_cmd_plobj_id_equal_ip4_route,
.cmd_plobj_id_hash = _vt_cmd_plobj_id_hash_ip4_route,
.cmd_plobj_to_string_id = _vt_cmd_plobj_to_string_id_ip4_route,
.cmd_plobj_to_string = (const char *(*) (const NMPlatformObject *obj)) nm_platform_ip4_route_to_string,
.cmd_plobj_to_string = (const char *(*) (const NMPlatformObject *obj, char *buf, gsize len)) nm_platform_ip4_route_to_string,
.cmd_plobj_cmp = (int (*) (const NMPlatformObject *obj1, const NMPlatformObject *obj2)) nm_platform_ip4_route_cmp,
},
[NMP_OBJECT_TYPE_IP6_ROUTE - 1] = {
@ -1896,7 +1893,7 @@ const NMPClass _nmp_classes[NMP_OBJECT_TYPE_MAX] = {
.cmd_plobj_id_equal = _vt_cmd_plobj_id_equal_ip6_route,
.cmd_plobj_id_hash = _vt_cmd_plobj_id_hash_ip6_route,
.cmd_plobj_to_string_id = _vt_cmd_plobj_to_string_id_ip6_route,
.cmd_plobj_to_string = (const char *(*) (const NMPlatformObject *obj)) nm_platform_ip6_route_to_string,
.cmd_plobj_to_string = (const char *(*) (const NMPlatformObject *obj, char *buf, gsize len)) nm_platform_ip6_route_to_string,
.cmd_plobj_cmp = (int (*) (const NMPlatformObject *obj1, const NMPlatformObject *obj2)) nm_platform_ip6_route_cmp,
},
};

View file

@ -138,7 +138,7 @@ typedef struct {
gboolean (*cmd_plobj_id_equal) (const NMPlatformObject *obj1, const NMPlatformObject *obj2);
guint (*cmd_plobj_id_hash) (const NMPlatformObject *obj);
const char *(*cmd_plobj_to_string_id) (const NMPlatformObject *obj, char *buf, gsize buf_size);
const char *(*cmd_plobj_to_string) (const NMPlatformObject *obj);
const char *(*cmd_plobj_to_string) (const NMPlatformObject *obj, char *buf, gsize len);
int (*cmd_plobj_cmp) (const NMPlatformObject *obj1, const NMPlatformObject *obj2);
} NMPClass;

View file

@ -75,12 +75,12 @@ dump_interface (NMPlatformLink *link)
for (i = 0; i < ip4_addresses->len; i++) {
ip4_address = &g_array_index (ip4_addresses, NMPlatformIP4Address, i);
printf (" ip4-address %s\n", nm_platform_ip4_address_to_string (ip4_address));
printf (" ip4-address %s\n", nm_platform_ip4_address_to_string (ip4_address, NULL, 0));
}
for (i = 0; i < ip6_addresses->len; i++) {
ip6_address = &g_array_index (ip6_addresses, NMPlatformIP6Address, i);
printf (" ip6-address %s\n", nm_platform_ip6_address_to_string (ip6_address));
printf (" ip6-address %s\n", nm_platform_ip6_address_to_string (ip6_address, NULL, 0));
}
g_array_unref (ip4_addresses);

View file

@ -58,28 +58,28 @@ test_nm_platform_ip6_address_to_string_flags (void)
{
NMPlatformIP6Address addr = { 0 };
g_assert_cmpstr (strstr (nm_platform_ip6_address_to_string (&addr), " flags "), ==, NULL);
g_assert_cmpstr (strstr (nm_platform_ip6_address_to_string (&addr, NULL, 0), " flags "), ==, NULL);
addr.flags = IFA_F_MANAGETEMPADDR;
nmtst_assert_str_has_substr (nm_platform_ip6_address_to_string (&addr), " flags mngtmpaddr ");
nmtst_assert_str_has_substr (nm_platform_ip6_address_to_string (&addr, NULL, 0), " flags mngtmpaddr ");
addr.flags = IFA_F_NOPREFIXROUTE;
nmtst_assert_str_has_substr (nm_platform_ip6_address_to_string (&addr), " flags noprefixroute ");
nmtst_assert_str_has_substr (nm_platform_ip6_address_to_string (&addr, NULL, 0), " flags noprefixroute ");
addr.flags = IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE;
nmtst_assert_str_has_substr (nm_platform_ip6_address_to_string (&addr), " flags mngtmpaddr,noprefixroute ");
nmtst_assert_str_has_substr (nm_platform_ip6_address_to_string (&addr, NULL, 0), " flags mngtmpaddr,noprefixroute ");
addr.flags = IFA_F_TENTATIVE | IFA_F_NOPREFIXROUTE;
nmtst_assert_str_has_substr (nm_platform_ip6_address_to_string (&addr), " flags tentative,noprefixroute ");
nmtst_assert_str_has_substr (nm_platform_ip6_address_to_string (&addr, NULL, 0), " flags tentative,noprefixroute ");
addr.flags = IFA_F_TENTATIVE | IFA_F_PERMANENT | IFA_F_MANAGETEMPADDR| IFA_F_NOPREFIXROUTE;
nmtst_assert_str_has_substr (nm_platform_ip6_address_to_string (&addr), " flags tentative,permanent,mngtmpaddr,noprefixroute ");
nmtst_assert_str_has_substr (nm_platform_ip6_address_to_string (&addr, NULL, 0), " flags tentative,permanent,mngtmpaddr,noprefixroute ");
addr.flags = IFA_F_TENTATIVE | IFA_F_PERMANENT | IFA_F_MANAGETEMPADDR| IFA_F_NOPREFIXROUTE | 0x8000;
nmtst_assert_str_has_substr (nm_platform_ip6_address_to_string (&addr), " flags tentative,permanent,mngtmpaddr,noprefixroute, ");
nmtst_assert_str_has_substr (nm_platform_ip6_address_to_string (&addr, NULL, 0), " flags tentative,permanent,mngtmpaddr,noprefixroute, ");
addr.flags = IFA_F_TENTATIVE | IFA_F_PERMANENT | IFA_F_MANAGETEMPADDR| IFA_F_NOPREFIXROUTE | ((G_MAXUINT - (G_MAXUINT >> 1)) >> 1);
nmtst_assert_str_has_substr (nm_platform_ip6_address_to_string (&addr), " flags tentative,permanent,mngtmpaddr,noprefixroute, ");
nmtst_assert_str_has_substr (nm_platform_ip6_address_to_string (&addr, NULL, 0), " flags tentative,permanent,mngtmpaddr,noprefixroute, ");
}
/******************************************************************/

View file

@ -264,8 +264,8 @@ test_nm_ip6_config_addresses_sort_check (NMIP6Config *config, NMSettingIP6Config
if (!nm_ip6_config_equal (copy, config)) {
g_message ("%s", "SORTING yields unexpected output:");
for (i = 0; i < addr_count; i++) {
g_message (" >> [%d] = %s", i, nm_platform_ip6_address_to_string (nm_ip6_config_get_address (config, i)));
g_message (" << [%d] = %s", i, nm_platform_ip6_address_to_string (nm_ip6_config_get_address (copy, i)));
g_message (" >> [%d] = %s", i, nm_platform_ip6_address_to_string (nm_ip6_config_get_address (config, i), NULL, 0));
g_message (" << [%d] = %s", i, nm_platform_ip6_address_to_string (nm_ip6_config_get_address (copy, i), NULL, 0));
}
g_assert_not_reached ();
}

View file

@ -797,10 +797,12 @@ _assert_route_check (const NMPlatformVTableRoute *vtable, gboolean has, const NM
if (!has) {
g_assert (!r);
} else {
char buf[sizeof (_nm_platform_to_string_buffer)];
if (!r || vtable->route_cmp (route, r) != 0)
g_error ("Invalid route. Expect %s, has %s",
nmtst_static_1024_01 (vtable->route_to_string (route)),
nmtst_static_1024_02 (vtable->route_to_string (r)));
vtable->route_to_string (route, NULL, 0),
vtable->route_to_string (r, buf, sizeof (buf)));
g_assert (r);
}
}