2020-12-23 22:21:36 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2010-12-13 16:18:35 +01:00
|
|
|
/*
|
2019-10-01 09:20:35 +02:00
|
|
|
* Copyright (C) 2011 Red Hat, Inc.
|
2010-12-13 16:18:35 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
2014-09-29 10:58:16 -04:00
|
|
|
* The example shows how to list connections. Contrast this example with
|
|
|
|
|
* list-connections-gdbus.c, which is a bit lower level and talks directly to NM
|
|
|
|
|
* using GDBus.
|
2010-12-13 16:18:35 +01:00
|
|
|
*
|
|
|
|
|
* Compile with:
|
2017-09-20 14:13:18 -07:00
|
|
|
* gcc -Wall list-connections-libnm.c -o list-connections-libnm `pkg-config --cflags --libs libnm`
|
2010-12-13 16:18:35 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2014-07-06 16:53:02 -04:00
|
|
|
#include <NetworkManager.h>
|
2010-12-13 16:18:35 +01:00
|
|
|
|
|
|
|
|
/* Print details of connection */
|
|
|
|
|
static void
|
2014-10-22 12:32:46 -04:00
|
|
|
show_connection(NMConnection *connection)
|
2010-12-13 16:18:35 +01:00
|
|
|
{
|
|
|
|
|
NMSettingConnection *s_con;
|
|
|
|
|
guint64 timestamp;
|
2021-11-09 13:28:54 +01:00
|
|
|
char *timestamp_str;
|
2010-12-13 16:18:35 +01:00
|
|
|
char timestamp_real_str[64];
|
2021-11-09 13:28:54 +01:00
|
|
|
const char *val1, *val2, *val3, *val4, *val5;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2011-12-05 12:27:47 +01:00
|
|
|
s_con = nm_connection_get_setting_connection(connection);
|
2010-12-13 16:18:35 +01:00
|
|
|
if (s_con) {
|
2020-05-06 22:33:52 +02:00
|
|
|
struct tm localtime_data;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2010-12-13 16:18:35 +01:00
|
|
|
/* Get various info from NMSettingConnection and show it */
|
|
|
|
|
timestamp = nm_setting_connection_get_timestamp(s_con);
|
|
|
|
|
timestamp_str = g_strdup_printf("%" G_GUINT64_FORMAT, timestamp);
|
2020-05-06 22:33:52 +02:00
|
|
|
strftime(timestamp_real_str,
|
|
|
|
|
sizeof(timestamp_real_str),
|
|
|
|
|
"%c",
|
|
|
|
|
localtime_r((time_t *) ×tamp, &localtime_data));
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2010-12-13 16:18:35 +01:00
|
|
|
val1 = nm_setting_connection_get_id(s_con);
|
|
|
|
|
val2 = nm_setting_connection_get_uuid(s_con);
|
|
|
|
|
val3 = nm_setting_connection_get_connection_type(s_con);
|
|
|
|
|
val4 = nm_connection_get_path(connection);
|
|
|
|
|
val5 = timestamp ? timestamp_real_str : "never";
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2010-12-13 16:18:35 +01:00
|
|
|
printf("%-25s | %s | %-15s | %-43s | %s\n", val1, val2, val3, val4, val5);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2010-12-13 16:18:35 +01:00
|
|
|
g_free(timestamp_str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-20 12:55:59 -04:00
|
|
|
int
|
|
|
|
|
main(int argc, char *argv[])
|
2010-12-13 16:18:35 +01:00
|
|
|
{
|
2021-11-09 13:28:54 +01:00
|
|
|
NMClient *client;
|
|
|
|
|
GError *error = NULL;
|
2014-10-22 12:32:46 -04:00
|
|
|
const GPtrArray *connections;
|
|
|
|
|
int i;
|
2014-07-20 12:55:59 -04:00
|
|
|
|
2014-09-29 10:58:16 -04:00
|
|
|
if (!(client = nm_client_new(NULL, &error))) {
|
|
|
|
|
g_message("Error: Could not connect to NetworkManager: %s.", error->message);
|
2014-05-15 14:25:07 -04:00
|
|
|
g_error_free(error);
|
2014-07-20 12:55:59 -04:00
|
|
|
return EXIT_FAILURE;
|
2010-12-13 16:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
2014-09-29 10:58:16 -04:00
|
|
|
if (!nm_client_get_nm_running(client)) {
|
|
|
|
|
g_message("Error: Can't obtain connections: NetworkManager is not running.");
|
2014-07-20 12:55:59 -04:00
|
|
|
return EXIT_FAILURE;
|
2010-12-13 16:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 12:55:59 -04:00
|
|
|
/* Now the connections can be listed. */
|
2014-10-22 12:32:46 -04:00
|
|
|
connections = nm_client_get_connections(client);
|
2010-12-13 16:18:35 +01:00
|
|
|
|
2014-07-20 12:55:59 -04:00
|
|
|
printf("Connections:\n===================\n");
|
2010-12-13 16:18:35 +01:00
|
|
|
|
2014-10-22 12:32:46 -04:00
|
|
|
for (i = 0; i < connections->len; i++)
|
|
|
|
|
show_connection(connections->pdata[i]);
|
2010-12-13 16:18:35 +01:00
|
|
|
|
2014-09-29 10:58:16 -04:00
|
|
|
g_object_unref(client);
|
2010-12-13 16:18:35 +01:00
|
|
|
|
2014-07-20 12:55:59 -04:00
|
|
|
return EXIT_SUCCESS;
|
2010-12-13 16:18:35 +01:00
|
|
|
}
|