Files
NetworkManager/test/nmtestdevices.c
Dan Williams 4de66efbd4 2004-08-31 Dan Williams <dcbw@redhat.com>
* Remove 'debug' extern global from all files since we now
		use syslog()

	* src/NetworkManager.[ch]
		- Break out routine that get the net.interface property from HAL,
			removing that logic from nm_create_device_and_add_to_list()
		- (nm_create_device_and_add_to_list): make this a bit more general so
			it doesn't do the talking to HAL.  Also add arguments to facilitate
			the create of test devices.
		- (nm_data_mark_state_changed): rename from nm_data_set_state_modified()
		- (nm_data_new, main, nm_print_usage): add new argument "--enable-test-devices"
			which makes NetworkManager listen for dbus commands to create test
			devices, which have no backing hardware.  Use when you're on a plane
			for example, and/or forgot your wireless card at home.  Test devices
			_cannot_ be created unless NM is started with --enable-test-devices.

	* src/NetworkManagerDbus.[ch]
		- New "getLinkActive" method for devices
		- New "setLinkActive" method for devices (only works on test devices)
		- New "createTestDevice" method on NetworkManager object to create a test
			device of a specified type (ie wired, wireless).  UDI is created from
			scratch, as is the interface name.  Only works when NM is started with
			--enable-test-devices switch.
		- New "removeTestDevice" method on NetworkManager object which removes a
			test device.  Only works when NM is started with --enable-test-devices

	* src/NetworkManagerDevice.[ch]
		- Logic to facilitate test devices.  Add variables to NMDevice struct to indicate
			whether a device is a test device or not, and what its link status is.
		- Deal with test devices in most functions.  For those that work directly on hardware
			special-case test devices.
		- (nm_device_new): don't create a test device if test devices weren't enabled on the
			command-line.
		- (nm_device_update_link_active): split out logic for wired and wireless device link
			checking to separate functions to facilitate test device link checking.
		- (nm_device_set_enc_key): Since some drivers for wireless cards are daft and
			don't make a distinction between System Authentication and Encryption
			(namely Cisco aironet), we use Open System auth when setting a WEP key
			on the card.  We don't deal with Shared Key auth yet.
		- (nm_device_activation_worker): split the activation cancel check logic out into
			a separate routine nm_device_activation_cancel_if_needed()
		- (nm_device_activation_signal_cancel): rename from nm_device_activation_cancel()
		- (nm_device_fake_ap_list): Test wireless devices obviously cannot scan, so create
			a list of fake access points that they can "see"
		- (nm_device_is_test_device): return whether or not a device is a test device

	* src/NetworkManagerPolicy.c
		- (nm_policy_get_best_device): attempt to deal with wireless network selection,
			previously if you "locked"/forced NM to use a wireless device but then
			selected a wireless network for NM to use, it would switch to a wired device.
			So, if the active device is wireless and it has a "forced" best AP, use it
			if the "forced" best AP is still valid
		- (nm_state_modification_monitor): deal with NULL best devices, for example
			there were no usable network devices, or the last one was removed

	* src/backends/NetworkManager*.c
		- Deal with test devices, mostly just return success for operations like getting
			a DHCP address

	* test/nmtestdevices.c
		- Test tool to create/remove/link-switch test devices


git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@112 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
2004-08-31 16:09:15 +00:00

298 lines
7.9 KiB
C

/* nmtestdevices - Tool to create/delete/modify test devices for NetworkManager
* (use when you are on a plane, don't have a wireless card, etc)
*
* 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 2004 Red Hat, Inc.
*/
#include <glib.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <stdio.h>
#include <getopt.h>
/* These MUST correspond to NetworkManager device types */
typedef enum NMDeviceType
{
DEVICE_TYPE_DONT_KNOW = 0,
DEVICE_TYPE_WIRED_ETHERNET,
DEVICE_TYPE_WIRELESS_ETHERNET
} NMDeviceType;
#define NM_DBUS_SERVICE "org.freedesktop.NetworkManager"
#define NM_DBUS_PATH "/org/freedesktop/NetworkManager"
#define NM_DBUS_INTERFACE "org.freedesktop.NetworkManager"
#define NM_DBUS_INTERFACE_DEVICES "org.freedesktop.NetworkManager.Devices"
void create_device (DBusConnection *connection, NMDeviceType type)
{
DBusMessage *message;
DBusMessage *reply;
DBusMessageIter iter;
DBusError error;
g_return_if_fail (connection != NULL);
g_return_if_fail (((type == DEVICE_TYPE_WIRED_ETHERNET) || (type == DEVICE_TYPE_WIRELESS_ETHERNET)));
message = dbus_message_new_method_call (NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, "createTestDevice");
if (message == NULL)
{
fprintf (stderr, "Couldn't allocate the dbus message\n");
return;
}
dbus_error_init (&error);
dbus_message_append_args (message, DBUS_TYPE_INT32, type, DBUS_TYPE_INVALID);
reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "%s raised:\n %s\n\n", error.name, error.message);
dbus_error_free (&error);
dbus_message_unref (message);
return;
}
if (reply == NULL)
{
fprintf( stderr, "dbus reply message was NULL\n" );
dbus_message_unref (message);
return;
}
/* now analyze reply */
dbus_message_iter_init (reply, &iter);
char *string;
string = dbus_message_iter_get_string (&iter);
if (!string)
{
fprintf (stderr, "NetworkManager returned a NULL test device ID, test device could not be created." );
return;
}
fprintf (stderr, "New test device ID: '%s'\n", string );
dbus_free (string);
dbus_message_unref (reply);
dbus_message_unref (message);
}
void remove_device (DBusConnection *connection, char *dev)
{
DBusMessage *message;
DBusMessage *reply;
DBusMessageIter iter;
DBusError error;
g_return_if_fail (connection != NULL);
g_return_if_fail (dev != NULL);
message = dbus_message_new_method_call (NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, "removeTestDevice");
if (message == NULL)
{
fprintf (stderr, "Couldn't allocate the dbus message\n");
return;
}
dbus_error_init (&error);
dbus_message_append_args (message, DBUS_TYPE_STRING, dev, DBUS_TYPE_INVALID);
reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "%s raised:\n %s\n\n", error.name, error.message);
dbus_error_free (&error);
dbus_message_unref (message);
return;
}
if (reply == NULL)
{
fprintf( stderr, "dbus reply message was NULL\n" );
dbus_message_unref (message);
return;
}
dbus_message_unref (message);
dbus_message_unref (reply);
}
void set_link_active (DBusConnection *connection, char *dev, gboolean active)
{
DBusMessage *message;
DBusMessage *reply;
DBusMessageIter iter;
DBusError error;
g_return_if_fail (connection != NULL);
g_return_if_fail (dev != NULL);
message = dbus_message_new_method_call (NM_DBUS_SERVICE, dev, NM_DBUS_INTERFACE_DEVICES, "setLinkActive");
if (message == NULL)
{
fprintf (stderr, "Couldn't allocate the dbus message\n");
return;
}
dbus_message_append_args (message, DBUS_TYPE_BOOLEAN, active, DBUS_TYPE_INVALID);
dbus_error_init (&error);
reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "%s raised:\n %s\n\n", error.name, error.message);
dbus_error_free (&error);
dbus_message_unref (message);
return;
}
if (reply == NULL)
{
fprintf( stderr, "dbus reply message was NULL\n" );
dbus_message_unref (message);
return;
}
dbus_message_unref (message);
dbus_message_unref (reply);
}
static void print_usage (void)
{
fprintf (stderr, "\n" "usage : nmtestdevices [options] [--help]\n");
fprintf (stderr,
"\n"
" --create-device <wired | wireless> Creates a test device, returns the new device ID\n"
" --remove-device <ID> Remove a test device (cannot remove real devices)\n"
" --make-link-active <ID> Switch a test device's link ON\n"
" --make-link-inactive <ID> Switch a test device's link OFF\n"
" --help Show this information and exit\n"
"\n"
"This tool allows you to tell NetworkManager to create and manipulate fake 'test' devices. This\n"
"is useful in sitation where you may not have a particular device but still want to test\n"
"NetworkManager out with it (For example, you forgot your wireless card at home and now you're\n"
"taking a trip and want to hack on NM, and you're on a plane so you could use the wireless\n"
"card anyway).\n"
"\n");
}
int main( int argc, char *argv[] )
{
DBusConnection *connection;
DBusError error;
char *dev = NULL;
gboolean create = FALSE;
gboolean remove = FALSE;
gboolean make_link_active = FALSE;
gboolean make_link_inactive = FALSE;
NMDeviceType dev_type = DEVICE_TYPE_DONT_KNOW;
/* Parse options */
while (1)
{
int c;
int option_index = 0;
const char *opt;
static struct option options[] = {
{"create-device", 1, NULL, 0},
{"remove-device", 1, NULL, 0},
{"make-link-active", 1, NULL, 0},
{"make-link-inactive", 1, NULL, 0},
{"help", 0, NULL, 0},
{NULL, 0, NULL, 0}
};
c = getopt_long (argc, argv, "", options, &option_index);
if (c == -1)
break;
switch (c)
{
case 0:
opt = options[option_index].name;
if (strcmp (opt, "help") == 0)
{
print_usage ();
exit (0);
}
else if (strcmp (opt, "create-device") == 0)
{
create = TRUE;
if (optarg)
{
if (strcmp (optarg, "wired") == 0)
dev_type = DEVICE_TYPE_WIRED_ETHERNET;
else if (strcmp (optarg, "wireless") == 0)
dev_type = DEVICE_TYPE_WIRELESS_ETHERNET;
}
}
else if (strcmp (opt, "remove-device") == 0)
{
remove = TRUE;
if (optarg)
dev = g_strdup (optarg);
}
else if (strcmp (opt, "make-link-active") == 0)
{
make_link_active = TRUE;
if (optarg)
dev = g_strdup (optarg);
}
else if (strcmp (opt, "make-link-inactive") == 0)
{
make_link_inactive = TRUE;
if (optarg)
dev = g_strdup (optarg);
}
break;
default:
print_usage ();
exit (1);
break;
}
}
g_type_init ();
dbus_error_init (&error);
connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
if (connection == NULL)
{
fprintf (stderr, "Error connecting to system bus: %s\n", error.message);
dbus_error_free (&error);
return 1;
}
if (create)
create_device (connection, dev_type);
else if (remove)
remove_device (connection, dev);
else if (make_link_active)
set_link_active (connection, dev, TRUE);
else if (make_link_inactive)
set_link_active (connection, dev, FALSE);
return 0;
}