NetworkManager/test/nm-tool.c
Tambet Ingo 24adbe3fa4 2007-10-09 Tambet Ingo <tambet@gmail.com>
* src/NetworkManagerUtils.c
        (nm_utils_is_empty_ssid):
        (nm_utils_escape_ssid):
        (nm_utils_same_ssid): Remove. These functions are copied and
pasted in a 
        lot of places, so they belong to libnm-utils instead.

        Now with 100% less compiler warnings:

        * libnm-util/nm-utils.c (nm_dbus_escape_object_path): Remove,
        * unused.
        (nm_dbus_unescape_object_path): Ditto.
        (nm_utils_ssid_to_utf8): Ditto.
        (nm_utils_is_empty_ssid): Move here from
src/NetworkManagerUtils.c
        (nm_utils_escape_ssid): Ditto.
        (nm_utils_same_ssid): Ditto.

        * src/nm-manager.c: Include 'netinet/ether.h' for ether_aton_r.
        (add_one_connection_element): Remove an unused variable.
        (impl_manager_get_active_connections): Ditto.

        * src/NetworkManagerPolicy.c (get_device_connection): Remove an
        * unused
        variable.

        * src/nm-dbus-manager.c (nm_dbus_manager_start_service): Remove
        * a leftover
        from the previous commit.

        * src/nm-device-802-11-wireless.c (set_current_ap): Remove
        * unused variable.
        (real_act_stage1_prepare): Ditto.
        (activation_success_handler): Ditto.
        (get_property): Ditto.

        * src/nm-device-802-3-ethernet.c (real_get_best_connection):
        * Remove unused
        variable.

        * src/ppp-manager/nm-pppd-plugin.c (nm_ip_up): Remove the check
        * for 'ifname',
        it's always set.

        * src/supplicant-manager/nm-supplicant-config.c 
        (nm_supplicant_config_add_setting_wireless): Cast the
GByteArray's 'guint8 *'
        to expected "char *".
        (nm_supplicant_config_add_setting_wireless): Ditto.
        (nm_supplicant_config_remove_option): Remove, not used.

        * libnm-glib/libnm-glib-test.c (dump_access_point): Frequency is
        * a guint32,
        not double.
        (test_wireless_enabled): Ifdef out unused function.
        (device_deactivate): Ditto.
        (device_state_changed): Ditto.
        (nm_utils_is_empty_ssid): Remove, it's now in libnm-utils.
        (nm_utils_escape_ssid): Ditto.

        * test/nm-tool.c (nm_utils_escape_ssid): Remove, it's now in
        * libnm-utils.
        (nm_utils_is_empty_ssid): Ditto.

        * libnm-glib/nm-client.c
        * (nm_client_free_active_connection_element): Remove
        unused variable.

        * libnm-util/nm-setting.c (setting_wireless_destroy): Remove
        * unused variable.
        (setting_vpn_properties_update_secrets): Ditto.
        (int_to_gvalue): Ifdef out for now, not used.
        (byte_to_gvalue): Ditto.

        * libnm-util/dbus-dict-helpers.c
        * (_nmu_dbus_add_dict_entry_string_array): 
        Unused, remove.




git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@2960 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
2007-10-09 08:57:35 +00:00

356 lines
8.9 KiB
C

/* nm-tool - information tool for NetworkManager
*
* Dan Williams <dcbw@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* (C) Copyright 2005 Red Hat, Inc.
*/
#include <glib.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iwlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <nm-client.h>
#include <nm-device.h>
#include <nm-device-802-3-ethernet.h>
#include <nm-device-802-11-wireless.h>
#include <nm-utils.h>
static gboolean
get_nm_state (NMClient *client)
{
NMState state;
char *state_string;
gboolean success = TRUE;
state = nm_client_get_state (client);
switch (state) {
case NM_STATE_ASLEEP:
state_string = "asleep";
break;
case NM_STATE_CONNECTING:
state_string = "connecting";
break;
case NM_STATE_CONNECTED:
state_string = "connected";
break;
case NM_STATE_DISCONNECTED:
state_string = "disconnected";
break;
case NM_STATE_UNKNOWN:
default:
state_string = "unknown";
success = FALSE;
break;
}
printf ("State: %s\n\n", state_string);
return success;
}
static void
print_string (const char *label, const char *data)
{
#define SPACING 18
int label_len = 0;
char spaces[50];
int i;
g_return_if_fail (label != NULL);
g_return_if_fail (data != NULL);
label_len = strlen (label);
if (label_len > SPACING)
label_len = SPACING - 1;
for (i = 0; i < (SPACING - label_len); i++)
spaces[i] = 0x20;
spaces[i] = 0x00;
printf (" %s:%s%s\n", label, &spaces[0], data);
}
static void
detail_access_point (gpointer data, gpointer user_data)
{
NMAccessPoint *ap = NM_ACCESS_POINT (data);
const char *active_bssid = (const char *) user_data;
GString *str;
gboolean active = FALSE;
guint32 flags, wpa_flags, rsn_flags;
const GByteArray * ssid;
char *tmp;
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);
if (active_bssid) {
const char *current_bssid = nm_access_point_get_hw_address (ap);
if (current_bssid && !strcmp (current_bssid, active_bssid))
active = TRUE;
}
str = g_string_new (NULL);
g_string_append_printf (str,
"%s Mode, Freq %d MHz, Rate %d Mb/s, Strength %d",
(nm_access_point_get_mode (ap) == IW_MODE_INFRA) ? "Infrastructure" : "Ad-Hoc",
nm_access_point_get_frequency (ap),
nm_access_point_get_rate (ap) / 1024,
nm_access_point_get_strength (ap));
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 (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 (str, " WEP");
if (wpa_flags != NM_802_11_AP_SEC_NONE)
g_string_append (str, " WPA");
if (rsn_flags != NM_802_11_AP_SEC_NONE)
g_string_append (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 (str, " Enterprise");
/* FIXME: broadcast/hidden */
ssid = nm_access_point_get_ssid (ap);
tmp = g_strdup_printf (" %s%s", active ? "*" : "",
ssid ? nm_utils_escape_ssid (ssid->data, ssid->len) : "(none)");
print_string (tmp, str->str);
g_string_free (str, TRUE);
g_free (tmp);
}
static gchar *
ip4_address_as_string (guint32 ip)
{
struct in_addr tmp_addr;
gchar *ip_string;
tmp_addr.s_addr = ip;
ip_string = inet_ntoa (tmp_addr);
return g_strdup (ip_string);
}
static void
detail_device (gpointer data, gpointer user_data)
{
NMDevice *device = NM_DEVICE (data);
char *tmp;
NMDeviceState state;
int caps;
int speed;
GArray *array;
state = nm_device_get_state (device);
tmp = nm_device_get_iface (device);
printf ("- Device: %s ----------------------------------------------------------------\n", tmp);
g_free (tmp);
/* General information */
if (NM_IS_DEVICE_802_3_ETHERNET (device))
print_string ("Type", "Wired");
else if (NM_IS_DEVICE_802_11_WIRELESS (device))
print_string ("Type", "802.11 Wireless");
tmp = nm_device_get_driver (device);
if (tmp) {
print_string ("Driver", tmp);
g_free (tmp);
} else
print_string ("Driver", "(unknown)");
if (state == NM_DEVICE_STATE_ACTIVATED)
print_string ("Active", "yes");
else
print_string ("Active", "no");
tmp = NULL;
if (NM_IS_DEVICE_802_3_ETHERNET (device))
tmp = nm_device_802_3_ethernet_get_hw_address (NM_DEVICE_802_3_ETHERNET (device));
else if (NM_IS_DEVICE_802_11_WIRELESS (device))
tmp = nm_device_802_11_wireless_get_hw_address (NM_DEVICE_802_11_WIRELESS (device));
if (tmp) {
print_string ("HW Address", tmp);
g_free (tmp);
}
/* Capabilities */
caps = nm_device_get_capabilities (device);
printf ("\n Capabilities:\n");
if (caps & NM_DEVICE_CAP_NM_SUPPORTED)
print_string (" Supported", "yes");
else
print_string (" Supported", "no");
if (caps & NM_DEVICE_CAP_CARRIER_DETECT)
print_string (" Carrier Detect", "yes");
speed = 0;
if (NM_IS_DEVICE_802_3_ETHERNET (device))
speed = nm_device_802_3_ethernet_get_speed (NM_DEVICE_802_3_ETHERNET (device));
else if (NM_IS_DEVICE_802_11_WIRELESS (device))
speed = nm_device_802_11_wireless_get_bitrate (NM_DEVICE_802_11_WIRELESS (device));
if (speed) {
char *speed_string;
speed_string = g_strdup_printf ("%d Mb/s", speed);
print_string (" Speed", speed_string);
g_free (speed_string);
}
/* Wireless specific information */
if ((NM_IS_DEVICE_802_11_WIRELESS (device))) {
guint32 wcaps;
NMAccessPoint *active_ap = NULL;
const char *active_bssid = NULL;
GSList *aps;
printf ("\n Wireless Settings\n");
wcaps = nm_device_802_11_wireless_get_capabilities (NM_DEVICE_802_11_WIRELESS (device));
if (wcaps & (NM_802_11_DEVICE_CAP_CIPHER_WEP40 | NM_802_11_DEVICE_CAP_CIPHER_WEP104))
print_string (" WEP Encryption", "yes");
if (wcaps & NM_802_11_DEVICE_CAP_WPA)
print_string (" WPA Encryption", "yes");
if (wcaps & NM_802_11_DEVICE_CAP_RSN)
print_string (" WPA2 Encryption", "yes");
if (nm_device_get_state (device) == NM_DEVICE_STATE_ACTIVATED) {
active_ap = nm_device_802_11_wireless_get_active_access_point (NM_DEVICE_802_11_WIRELESS (device));
active_bssid = active_ap ? nm_access_point_get_hw_address (active_ap) : NULL;
}
printf ("\n Wireless Access Points%s\n", active_ap ? "(* = Current AP)" : "");
aps = nm_device_802_11_wireless_get_access_points (NM_DEVICE_802_11_WIRELESS (device));
g_slist_foreach (aps, detail_access_point, (gpointer) active_bssid);
g_slist_free (aps);
} else if (NM_IS_DEVICE_802_3_ETHERNET (device)) {
printf ("\n Wired Settings\n");
/* FIXME */
#if 0
if (link_active)
print_string (" Hardware Link", "yes");
else
print_string (" Hardware Link", "no");
#endif
}
/* IP Setup info */
if (state == NM_DEVICE_STATE_ACTIVATED) {
NMIP4Config *cfg = nm_device_get_ip4_config (device);
printf ("\n IP Settings:\n");
tmp = ip4_address_as_string (nm_ip4_config_get_address (cfg));
print_string (" IP Address", tmp);
g_free (tmp);
tmp = ip4_address_as_string (nm_ip4_config_get_netmask (cfg));
print_string (" Subnet Mask", tmp);
g_free (tmp);
tmp = ip4_address_as_string (nm_ip4_config_get_broadcast (cfg));
print_string (" Broadcast", tmp);
g_free (tmp);
tmp = ip4_address_as_string (nm_ip4_config_get_gateway (cfg));
print_string (" Gateway", tmp);
g_free (tmp);
array = nm_ip4_config_get_nameservers (cfg);
if (array) {
int i;
for (i = 0; i < array->len; i++) {
tmp = ip4_address_as_string (g_array_index (array, guint32, i));
print_string (" DNS", tmp);
g_free (tmp);
}
g_array_free (array, TRUE);
}
g_object_unref (cfg);
}
printf ("\n\n");
}
static void
print_devices (NMClient *client)
{
GSList *devices;
devices = nm_client_get_devices (client);
g_slist_foreach (devices, detail_device, NULL);
g_slist_free (devices);
}
int
main (int argc, char *argv[])
{
NMClient *client;
g_type_init ();
client = nm_client_new ();
if (!client) {
exit (1);
}
printf ("\nNetworkManager Tool\n\n");
if (!get_nm_state (client)) {
fprintf (stderr, "\n\nNetworkManager appears not to be running (could not get its state).\n");
exit (1);
}
print_devices (client);
g_object_unref (client);
return 0;
}