1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/* 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 <stdlib.h>
#include <getopt.h>
#include <string.h>
#include "NetworkManager.h"
static void create_device (DBusConnection *connection, NMDeviceType type)
{
DBusMessage *message;
DBusMessage *reply;
DBusError error;
char *string;
g_return_if_fail (connection != NULL);
g_return_if_fail (((type == DEVICE_TYPE_802_3_ETHERNET) || (type == DEVICE_TYPE_802_11_WIRELESS)));
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);
dbus_message_unref (message);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "%s raised:\n %s\n\n", error.name, error.message);
dbus_error_free (&error);
return;
}
if (reply == NULL)
{
fprintf( stderr, "dbus reply message was NULL\n" );
return;
}
dbus_error_init (&error);
if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &string, DBUS_TYPE_INVALID) || !string)
{
fprintf (stderr, "NetworkManager returned a NULL test device ID, test device could not be created." );
dbus_message_unref (reply);
if (dbus_error_is_set (&error))
dbus_error_free (&error);
return;
}
fprintf (stderr, "New test device ID: '%s'\n", string );
dbus_message_unref (reply);
dbus_free (string);
}
static void destroy_device (DBusConnection *connection, char *dev)
{
DBusMessage *message;
DBusMessage *reply;
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);
}
static void set_link_active (DBusConnection *connection, char *dev, gboolean active)
{
DBusMessage *message;
DBusMessage *reply;
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 couldn't 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 destroy = FALSE;
gboolean make_link_active = FALSE;
gboolean make_link_inactive = FALSE;
NMDeviceType dev_type = DEVICE_TYPE_UNKNOWN;
if (argc < 2) {
print_usage ();
exit (0);
}
/* 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_802_3_ETHERNET;
else if (strcmp (optarg, "wireless") == 0)
dev_type = DEVICE_TYPE_802_11_WIRELESS;
}
}
else if (strcmp (opt, "remove-device") == 0)
{
destroy = 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 (destroy)
destroy_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;
}
|