NetworkManager/test/nm-tool.c
Dan Williams 54aac18883 2008-04-07 Dan Williams <dcbw@redhat.com>
* include/NetworkManager.h
		- Remove the DOWN and CANCELLED device states
		- Add UNMANAGED and UNAVAILABLE device states
		- Document the device states

	* introspection/nm-device.xml
	  src/nm-device-interface.c
	  src/nm-device-interface.h
		- Add the 'managed' property

	* test/nm-tool.c
		- (detail_device): print out device state

	* src/NetworkManagerSystem.h
	  src/backends/NetworkManagerArch.c
	  src/backends/NetworkManagerDebian.c
	  src/backends/NetworkManagerFrugalware.c
	  src/backends/NetworkManagerGentoo.c
	  src/backends/NetworkManagerMandriva.c
	  src/backends/NetworkManagerPaldo.c
	  src/backends/NetworkManagerRedHat.c
	  src/backends/NetworkManagerSlackware.c
	  src/backends/NetworkManagerSuSE.c
		- (nm_system_device_get_system_config, nm_system_device_get_disabled
		   nm_system_device_free_system_config): remove; they were unused and
			their functionality should be re-implemented in each distro's
			system settings service plugin 

	* src/nm-gsm-device.c
	  src/nm-gsm-device.h
	  src/nm-cdma-device.c
	  src/nm-cdma-device.h
		- (*_new): take the 'managed' argument

	* src/nm-device.c
		- (nm_device_set_address): remove, fold into nm_device_bring_up()
		- (nm_device_init): start in unmanaged state, not disconnected
		- (constructor): don't start device until the system settings service
			has had a chance to figure out if the device is managed or not
		- (nm_device_deactivate, nm_device_bring_up, nm_device_bring_down):
			don't set device state here, let callers handle that as appropriate
		- (nm_device_dispose): don't touch the device if it's not managed
		- (set_property, get_property, nm_device_class_init): implement the
			'managed' property
		- (nm_device_state_changed): bring the device up if its now managed,
			and deactivate it if it used to be active
		- (nm_device_get_managed, nm_device_set_managed): do the right thing
			with the managed state

	* src/nm-hal-manager.c
		- (wired_device_creator, wireless_device_creator, modem_device_creator):
			take initial managed state and pass it along to device constructors
		- (create_device_and_add_to_list): get managed state and pass to
			type creators

	* src/nm-device-802-11-wireless.c
		- (real_can_activate): fold in most of
			nm_device_802_11_wireless_can_activate()
		- (can_scan): can't scan in UNAVAILABLE or UNMANAGED
		- (link_timeout_cb): instead of deactivating, change device state and
			let the device state handler to it
		- (real_update_hw_address): clean up
		- (state_changed_cb): when entering UNAVAILABLE state, schedule an idle
			handler to transition to DISCONNECTED if the device isn't rfkilled

	* src/nm-device-802-3-ethernet.c
		- (set_carrier): move above callers and get rid of prototype
		- (device_state_changed): when entering UNAVAILABLE state, schedule an
			idle handler to transition to DISCONNECTED if the device has a
			carrier
		- (real_update_hw_address): clean up
		- (link_timeout_cb, ppp_state_changed): change state instead of calling
			deactivation directly as deactivation doesn't change state anymore

	* src/NetworkManagerPolicy.c
		- (schedule_activate_check): yay, remove wireless_enabled hack since
			the NMManager and wireless devices work that out themselves now
		- (device_state_changed): change to a switch and update for new device
			states
		- (device_carrier_changed): remove; device handles this now through
			state changes
		- (device_added): don't care about carrier any more; the initial
			activation check will happen when the device transitions to
			DISCONNECTED

	* src/nm-manager.c
		- (dispose): clear unmanaged devices
		- (handle_unmanaged_devices): update unmanaged device list and toggle
			the managed property on each device when needed
		- (system_settings_properties_changed_cb): handle signals from the
			system settings service
		- (system_settings_get_unmanaged_devices_cb): handle callback from
			getting the unmanaged device list method call
		- (query_unmanaged_devices): ask the system settings service for its
			list of unmanaged devices
		- (nm_manager_name_owner_changed, initial_get_connections): get unmanaged
			devices
		- (manager_set_wireless_enabled): push rfkill state down to wireless
			devices directly and let them handle the necessary state transitions
		- (manager_device_state_changed): update for new device states
		- (nm_manager_add_device): set initial rfkill state on wireless devices
		- (nm_manager_remove_device): don't touch the device if it's unmanaged
		- (nm_manager_activate_connection): return error if the device is
			unmanaged
		- (nm_manager_sleep): handle new device states correctly; don't change
			the state of unavailable/unmanaged devices

	* libnm-glib/nm-device-802-11-wireless.c
		- (state_changed_cb): update for new device states



git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@3540 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
2008-04-08 02:58:02 +00:00

363 lines
9.6 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 "wireless-helper.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, %s, Freq %d MHz, Rate %d Mb/s, Strength %d",
(nm_access_point_get_mode (ap) == IW_MODE_INFRA) ? "Infra" : "Ad-Hoc",
nm_access_point_get_hw_address (ap),
nm_access_point_get_frequency (ap),
nm_access_point_get_max_bitrate (ap) / 1000,
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 const char *
get_dev_state_string (NMDeviceState state)
{
if (state == NM_DEVICE_STATE_UNMANAGED)
return "unmanaged";
else if (state == NM_DEVICE_STATE_UNAVAILABLE)
return "unavailable";
else if (state == NM_DEVICE_STATE_DISCONNECTED)
return "disconnected";
else if (state == NM_DEVICE_STATE_PREPARE)
return "connecting (prepare)";
else if (state == NM_DEVICE_STATE_CONFIG)
return "connecting (configuring)";
else if (state == NM_DEVICE_STATE_NEED_AUTH)
return "connecting (need authentication)";
else if (state == NM_DEVICE_STATE_IP_CONFIG)
return "connecting (getting IP configuration)";
else if (state == NM_DEVICE_STATE_ACTIVATED)
return "connected";
else if (state == NM_DEVICE_STATE_FAILED)
return "connection failed";
return "unknown";
}
static void
detail_device (gpointer data, gpointer user_data)
{
NMDevice *device = NM_DEVICE (data);
char *tmp;
NMDeviceState state;
guint32 caps;
guint32 speed;
const GArray *array;
state = nm_device_get_state (device);
printf ("- Device: %s ----------------------------------------------------------------\n",
nm_device_get_iface (device));
/* 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");
print_string ("Driver", nm_device_get_driver (device) ? nm_device_get_driver (device) : "(unknown)");
print_string ("State", get_dev_state_string (state));
tmp = NULL;
if (NM_IS_DEVICE_802_3_ETHERNET (device))
tmp = g_strdup (nm_device_802_3_ethernet_get_hw_address (NM_DEVICE_802_3_ETHERNET (device)));
else if (NM_IS_DEVICE_802_11_WIRELESS (device))
tmp = g_strdup (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 in Mb/s */
speed = nm_device_802_3_ethernet_get_speed (NM_DEVICE_802_3_ETHERNET (device));
} else if (NM_IS_DEVICE_802_11_WIRELESS (device)) {
/* Speed in b/s */
speed = nm_device_802_11_wireless_get_bitrate (NM_DEVICE_802_11_WIRELESS (device));
speed /= 1000000;
}
if (speed) {
char *speed_string;
speed_string = g_strdup_printf ("%u 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;
const GPtrArray *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));
if (aps && aps->len)
g_ptr_array_foreach ((GPtrArray *) aps, detail_access_point, (gpointer) active_bssid);
} 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);
}
}
}
printf ("\n\n");
}
int
main (int argc, char *argv[])
{
NMClient *client;
const GPtrArray *devices;
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);
}
devices = nm_client_get_devices (client);
g_ptr_array_foreach ((GPtrArray *) devices, detail_device, NULL);
g_object_unref (client);
return 0;
}