NetworkManager/examples/C/glib/get-ap-info-libnm.c
Thomas Haller 615221a99c format: reformat source tree with clang-format 13.0
We use clang-format for automatic formatting of our source files.
Since clang-format is actively maintained software, the actual
formatting depends on the used version of clang-format. That is
unfortunate and painful, but really unavoidable unless clang-format
would be strictly bug-compatible.

So the version that we must use is from the current Fedora release, which
is also tested by our gitlab-ci. Previously, we were using Fedora 34 with
clang-tools-extra-12.0.1-1.fc34.x86_64.

As Fedora 35 comes along, we need to update our formatting as Fedora 35
comes with version "13.0.0~rc1-1.fc35".
An alternative would be to freeze on version 12, but that has different
problems (like, it's cumbersome to rebuild clang 12 on Fedora 35 and it
would be cumbersome for our developers which are on Fedora 35 to use a
clang that they cannot easily install).

The (differently painful) solution is to reformat from time to time, as we
switch to a new Fedora (and thus clang) version.
Usually we would expect that such a reformatting brings minor changes.
But this time, the changes are huge. That is mentioned in the release
notes [1] as

  Makes PointerAligment: Right working with AlignConsecutiveDeclarations. (Fixes https://llvm.org/PR27353)

[1] https://releases.llvm.org/13.0.0/tools/clang/docs/ReleaseNotes.html#clang-format
2021-11-29 09:31:09 +00:00

214 lines
7 KiB
C

/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2010 Red Hat, Inc.
*/
/*
* The example shows how to get info about APs visible by Wi-Fi devices using
* libnm.
*
* Compile with:
* gcc -Wall get-ap-info-libnm.c -o get-ap-info-libnm `pkg-config --libs --cflags libnm`
*/
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <NetworkManager.h>
/* Convert flags to string */
static char *
ap_wpa_rsn_flags_to_string(guint32 flags)
{
char *flags_str[16]; /* Enough space for flags and terminating NULL */
char *ret_str;
int i = 0;
if (flags & NM_802_11_AP_SEC_PAIR_WEP40)
flags_str[i++] = g_strdup("pair_wpe40");
if (flags & NM_802_11_AP_SEC_PAIR_WEP104)
flags_str[i++] = g_strdup("pair_wpe104");
if (flags & NM_802_11_AP_SEC_PAIR_TKIP)
flags_str[i++] = g_strdup("pair_tkip");
if (flags & NM_802_11_AP_SEC_PAIR_CCMP)
flags_str[i++] = g_strdup("pair_ccmp");
if (flags & NM_802_11_AP_SEC_GROUP_WEP40)
flags_str[i++] = g_strdup("group_wpe40");
if (flags & NM_802_11_AP_SEC_GROUP_WEP104)
flags_str[i++] = g_strdup("group_wpe104");
if (flags & NM_802_11_AP_SEC_GROUP_TKIP)
flags_str[i++] = g_strdup("group_tkip");
if (flags & NM_802_11_AP_SEC_GROUP_CCMP)
flags_str[i++] = g_strdup("group_ccmp");
if (flags & NM_802_11_AP_SEC_KEY_MGMT_PSK)
flags_str[i++] = g_strdup("psk");
if (flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
flags_str[i++] = g_strdup("802.1X");
if (i == 0)
flags_str[i++] = g_strdup("none");
flags_str[i] = NULL;
ret_str = g_strjoinv(" ", flags_str);
i = 0;
while (flags_str[i])
g_free(flags_str[i++]);
return ret_str;
}
static void
show_access_point_info(NMAccessPoint *ap)
{
guint32 flags, wpa_flags, rsn_flags, freq, bitrate;
guint8 strength;
GBytes *ssid;
const char *hwaddr;
NM80211Mode mode;
char *freq_str, *ssid_str, *bitrate_str, *strength_str, *wpa_flags_str, *rsn_flags_str;
GString *security_str;
/* Get AP properties */
flags = nm_access_point_get_flags(ap);
wpa_flags = nm_access_point_get_wpa_flags(ap);
rsn_flags = nm_access_point_get_rsn_flags(ap);
ssid = nm_access_point_get_ssid(ap);
hwaddr = nm_access_point_get_bssid(ap);
freq = nm_access_point_get_frequency(ap);
mode = nm_access_point_get_mode(ap);
bitrate = nm_access_point_get_max_bitrate(ap);
strength = nm_access_point_get_strength(ap);
/* Convert to strings */
if (ssid)
ssid_str = nm_utils_ssid_to_utf8(g_bytes_get_data(ssid, NULL), g_bytes_get_size(ssid));
else
ssid_str = g_strdup("--");
freq_str = g_strdup_printf("%u MHz", freq);
bitrate_str = g_strdup_printf("%u Mbit/s", bitrate / 1000);
strength_str = g_strdup_printf("%u", strength);
wpa_flags_str = ap_wpa_rsn_flags_to_string(wpa_flags);
rsn_flags_str = ap_wpa_rsn_flags_to_string(rsn_flags);
security_str = g_string_new(NULL);
if (!(flags & NM_802_11_AP_FLAGS_PRIVACY) && (wpa_flags != NM_802_11_AP_SEC_NONE)
&& (rsn_flags != NM_802_11_AP_SEC_NONE))
g_string_append(security_str, "Encrypted: ");
if ((flags & NM_802_11_AP_FLAGS_PRIVACY) && (wpa_flags == NM_802_11_AP_SEC_NONE)
&& (rsn_flags == NM_802_11_AP_SEC_NONE))
g_string_append(security_str, "WEP ");
if (wpa_flags != NM_802_11_AP_SEC_NONE)
g_string_append(security_str, "WPA ");
if (rsn_flags != NM_802_11_AP_SEC_NONE)
g_string_append(security_str, "WPA2 ");
if ((wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
|| (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X))
g_string_append(security_str, "Enterprise ");
if (security_str->len > 0)
g_string_truncate(security_str, security_str->len - 1); /* Chop off last space */
printf("SSID: %s\n", ssid_str);
printf("BSSID: %s\n", hwaddr);
printf("Mode: %s\n",
mode == NM_802_11_MODE_ADHOC ? "Ad-Hoc"
: mode == NM_802_11_MODE_INFRA ? "Infrastructure"
: "Unknown");
printf("Freq: %s\n", freq_str);
printf("Bitrate: %s\n", bitrate_str);
printf("Strength: %s\n", strength_str);
printf("Security: %s\n", security_str->str);
printf("WPA flags: %s\n", wpa_flags_str);
printf("RSN flags: %s\n", rsn_flags_str);
printf("D-Bus path: %s\n\n", nm_object_get_path(NM_OBJECT(ap)));
g_free(ssid_str);
g_free(freq_str);
g_free(bitrate_str);
g_free(strength_str);
g_free(wpa_flags_str);
g_free(rsn_flags_str);
g_string_free(security_str, TRUE);
}
static void
show_wifi_device_info(NMDevice *device)
{
NMAccessPoint *active_ap = NULL;
const GPtrArray *aps;
const char *iface;
const char *driver;
guint32 speed;
GBytes *active_ssid;
char *active_ssid_str = NULL;
int i;
/* Get active AP */
if (nm_device_get_state(device) == NM_DEVICE_STATE_ACTIVATED) {
if ((active_ap = nm_device_wifi_get_active_access_point(NM_DEVICE_WIFI(device)))) {
active_ssid = nm_access_point_get_ssid(active_ap);
if (active_ssid)
active_ssid_str = nm_utils_ssid_to_utf8(g_bytes_get_data(active_ssid, NULL),
g_bytes_get_size(active_ssid));
else
active_ssid_str = g_strdup("--");
}
}
iface = nm_device_get_iface(device);
driver = nm_device_get_driver(device);
speed = nm_device_wifi_get_bitrate(NM_DEVICE_WIFI(device));
speed /= 1000;
printf("Device: %s ---- Driver: %s ---- Speed: %d Mbit/s ---- Active AP: %s\n",
iface,
driver,
speed,
active_ssid_str ?: "none");
printf("=================================================================================\n");
g_free(active_ssid_str);
/* Get all APs of the Wi-Fi device */
aps = nm_device_wifi_get_access_points(NM_DEVICE_WIFI(device));
/* Print AP details */
for (i = 0; i < aps->len; i++) {
NMAccessPoint *ap = g_ptr_array_index(aps, i);
show_access_point_info(ap);
}
}
int
main(int argc, char *argv[])
{
NMClient *client;
const GPtrArray *devices;
int i;
GError *error = NULL;
/* Get NMClient object */
client = nm_client_new(NULL, &error);
if (!client) {
g_message("Error: Could not create NMClient: %s.", error->message);
g_error_free(error);
return EXIT_FAILURE;
}
/* Get all devices managed by NetworkManager */
devices = nm_client_get_devices(client);
/* Go through the array and process Wi-Fi devices */
for (i = 0; i < devices->len; i++) {
NMDevice *device = g_ptr_array_index(devices, i);
if (NM_IS_DEVICE_WIFI(device))
show_wifi_device_info(device);
}
g_object_unref(client);
return EXIT_SUCCESS;
}