From c822b12cf17253cd59efdda0a21cd2f802c16a06 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 26 Aug 2013 22:10:11 +0200 Subject: [PATCH] core: add nm_platform_ip4_route_to_string for debugging Add convenience function to convert an IPv4 route to string. Signed-off-by: Thomas Haller --- src/platform/nm-platform.c | 29 +++++++++++++++++++++++++++++ src/platform/nm-platform.h | 2 ++ 2 files changed, 31 insertions(+) diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 86993db33f..4eeaaf92e6 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -1568,6 +1568,35 @@ nm_platform_route_flush (int ifindex) /******************************************************************/ +/** + * nm_platform_ip4_route_to_string: + * @route: pointer to NMPlatformIP4Route route structure + * + * A method for converting a route struct into a string representation. + * This is only useful for printf debugging, if you want to log what's + * currently happening. + * + * Returns: a string with the content of the route. Must be freed by g_free. + */ +char * +nm_platform_ip4_route_to_string (const NMPlatformIP4Route *route) +{ + char s_network[INET_ADDRSTRLEN], s_gateway[INET_ADDRSTRLEN], s_dev[16]; + if (!route) { + return g_strdup (""); + } + if (route->ifindex > 0) { + g_snprintf (s_dev, sizeof(s_dev), " dev [%d]", route->ifindex); + } else { + s_dev[0] = 0; + } + inet_ntop (AF_INET, &route->network, s_network, sizeof(s_network)); + inet_ntop (AF_INET, &route->gateway, s_gateway, sizeof(s_gateway)); + return g_strdup_printf("%s/%d via %s%s metric %d mss %d", + s_network, route->plen, s_gateway, s_dev, + route->metric, route->mss); +} + static void log_link (NMPlatformLink *device, const char *change_type) { diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index 6f72e5f53f..a09d1b3c09 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -420,6 +420,8 @@ gboolean nm_platform_ip4_route_sync (int ifindex, const GArray *known_routes); gboolean nm_platform_ip6_route_sync (int ifindex, const GArray *known_routes); gboolean nm_platform_route_flush (int ifindex); +char *nm_platform_ip4_route_to_string (const NMPlatformIP4Route *route); + #define auto_g_free __attribute__((cleanup(put_g_free))) static void __attribute__((unused)) put_g_free (void *ptr)