2004-10-18 Dan Williams <dcbw@redhat.com>

Patches from Thom May:
	* test/nmtestdevices.c
		- Include <string.h>
	* src/backends/NetworkManagerDebian.c:
		- (nm_system_device_run_dhcp, nm_system_device_stop_dhcp)
			(nm_system_device_flush_routes, nm_system_device_flush_addresses)
			Move to using g_strdup_printf rather than arbitrary buffers
		- (nm_system_device_setup_static_ip4_config) Implement function.
		- (nm_system_kill_all_dhcp_daemons) Use killall -q rather than killall


git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@256 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
This commit is contained in:
Dan Williams
2004-10-19 02:27:04 +00:00
parent cf9eb09cc3
commit c396b6b27d
4 changed files with 130 additions and 13 deletions

View File

@@ -1,3 +1,15 @@
2004-10-18 Dan Williams <dcbw@redhat.com>
Patches from Thom May:
* test/nmtestdevices.c
- Include <string.h>
* src/backends/NetworkManagerDebian.c:
- (nm_system_device_run_dhcp, nm_system_device_stop_dhcp)
(nm_system_device_flush_routes, nm_system_device_flush_addresses)
Move to using g_strdup_printf rather than arbitrary buffers
- (nm_system_device_setup_static_ip4_config) Implement function.
- (nm_system_kill_all_dhcp_daemons) Use killall -q rather than killall
2004-10-17 Dan Williams <dcbw@redhat.com> 2004-10-17 Dan Williams <dcbw@redhat.com>
* info-daemon/NetworkManagerInfoDbus.c * info-daemon/NetworkManagerInfoDbus.c

View File

@@ -172,6 +172,7 @@ void nm_remove_device_from_list (NMData *data, const char *udi)
/* Attempt to acquire mutex for device list deletion. If acquire fails, /* Attempt to acquire mutex for device list deletion. If acquire fails,
* just ignore the device deletion entirely. * just ignore the device deletion entirely.
*/ */
fprintf (stderr, "Remove called fro device %s\n", udi);
if (nm_try_acquire_mutex (data->dev_list_mutex, __FUNCTION__)) if (nm_try_acquire_mutex (data->dev_list_mutex, __FUNCTION__))
{ {
element = data->dev_list; element = data->dev_list;

View File

@@ -25,6 +25,7 @@
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#include <signal.h> #include <signal.h>
#include <arpa/inet.h>
#include "NetworkManagerSystem.h" #include "NetworkManagerSystem.h"
#include "NetworkManagerUtils.h" #include "NetworkManagerUtils.h"
#include "NetworkManagerDevice.h" #include "NetworkManagerDevice.h"
@@ -52,7 +53,7 @@ void nm_system_init (void)
*/ */
gboolean nm_system_device_run_dhcp (NMDevice *dev) gboolean nm_system_device_run_dhcp (NMDevice *dev)
{ {
char buf [500]; char *buf;
char *iface; char *iface;
int err; int err;
@@ -69,10 +70,11 @@ gboolean nm_system_device_run_dhcp (NMDevice *dev)
* (for example, bad WEP key so it can't actually talk to the AP). * (for example, bad WEP key so it can't actually talk to the AP).
*/ */
iface = nm_device_get_iface (dev); iface = nm_device_get_iface (dev);
snprintf (buf, 500, "/sbin/dhclient -pf /var/run/dhclient-%s.pid %s\n", buf = g_strdup_printf ("/sbin/dhclient -pf /var/run/dhclient-%s.pid %s\n",
iface, iface); iface, iface);
printf("Running %s",buf); printf("Running %s",buf);
err = nm_spawn_process (buf); err = nm_spawn_process (buf);
g_free (buf);
return (err == 0); return (err == 0);
} }
@@ -88,7 +90,7 @@ gboolean nm_system_device_run_dhcp (NMDevice *dev)
void nm_system_device_stop_dhcp (NMDevice *dev) void nm_system_device_stop_dhcp (NMDevice *dev)
{ {
FILE *pidfile; FILE *pidfile;
char buf [500]; char *buf;
g_return_if_fail (dev != NULL); g_return_if_fail (dev != NULL);
@@ -97,7 +99,8 @@ void nm_system_device_stop_dhcp (NMDevice *dev)
return; return;
/* Find and kill the previous dhclient process for this device */ /* Find and kill the previous dhclient process for this device */
snprintf (buf, 500, "/var/run/dhclient-%s.pid", nm_device_get_iface (dev)); buf = g_strdup_printf ("/var/run/dhclient-%s.pid",
nm_device_get_iface (dev));
pidfile = fopen (buf, "r"); pidfile = fopen (buf, "r");
if (pidfile) if (pidfile)
{ {
@@ -107,13 +110,14 @@ void nm_system_device_stop_dhcp (NMDevice *dev)
memset (s_pid, 0, 20); memset (s_pid, 0, 20);
fgets (s_pid, 19, pidfile); fgets (s_pid, 19, pidfile);
len = strnlen (s_pid, 20); len = strlen (s_pid);
fclose (pidfile); fclose (pidfile);
n_pid = atoi (s_pid); n_pid = atoi (s_pid);
if (n_pid > 0) if (n_pid > 0)
kill (n_pid, SIGTERM); kill (n_pid, SIGTERM);
} }
g_free (buf);
} }
@@ -125,7 +129,7 @@ void nm_system_device_stop_dhcp (NMDevice *dev)
*/ */
void nm_system_device_flush_routes (NMDevice *dev) void nm_system_device_flush_routes (NMDevice *dev)
{ {
char buf [100]; char *buf;
g_return_if_fail (dev != NULL); g_return_if_fail (dev != NULL);
@@ -134,8 +138,10 @@ void nm_system_device_flush_routes (NMDevice *dev)
return; return;
/* Remove routing table entries */ /* Remove routing table entries */
snprintf (buf, 100, "/sbin/ip route flush dev %s", nm_device_get_iface (dev)); buf = g_strdup_printf ("/sbin/ip route flush dev %s",
nm_device_get_iface (dev));
nm_spawn_process (buf); nm_spawn_process (buf);
g_free (buf);
} }
@@ -147,7 +153,7 @@ void nm_system_device_flush_routes (NMDevice *dev)
*/ */
void nm_system_device_flush_addresses (NMDevice *dev) void nm_system_device_flush_addresses (NMDevice *dev)
{ {
char buf [100]; char *buf;
g_return_if_fail (dev != NULL); g_return_if_fail (dev != NULL);
@@ -156,8 +162,10 @@ void nm_system_device_flush_addresses (NMDevice *dev)
return; return;
/* Remove all IP addresses for a device */ /* Remove all IP addresses for a device */
snprintf (buf, 100, "/sbin/ip address flush dev %s", nm_device_get_iface (dev)); buf = g_strdup_printf ("/sbin/ip address flush dev %s",
nm_device_get_iface (dev));
nm_spawn_process (buf); nm_spawn_process (buf);
g_free (buf);
} }
@@ -172,7 +180,102 @@ void nm_system_device_flush_addresses (NMDevice *dev)
*/ */
gboolean nm_system_device_setup_static_ip4_config (NMDevice *dev) gboolean nm_system_device_setup_static_ip4_config (NMDevice *dev)
{ {
syslog (LOG_WARNING, "nm_system_device_setup_static_ip4_config() is not implemented yet for this distribution.\n"); #define IPBITS (sizeof (guint32) * 8)
struct in_addr temp_addr;
struct in_addr temp_addr2;
char *s_tmp;
char *s_tmp2;
int i;
guint32 addr;
guint32 netmask;
guint32 prefix = IPBITS; /* initialize with # bits in ipv4 address */
guint32 broadcast;
char *buf;
int err;
char *iface;
g_return_val_if_fail (dev != NULL, FALSE);
g_return_val_if_fail (!nm_device_config_get_use_dhcp (dev), FALSE);
addr = nm_device_config_get_ip4_address (dev);
netmask = nm_device_config_get_ip4_netmask (dev);
iface = nm_device_get_iface (dev);
broadcast = nm_device_config_get_ip4_broadcast (dev);
/* get the prefix from the netmask */
for (i = 0; i < IPBITS; i++)
{
if (!(ntohl (netmask) & ((2 << i) - 1)))
prefix--;
}
/* Calculate the broadcast address if the user didn't specify one */
if (!broadcast)
broadcast = ((addr & (int)netmask) | ~(int)netmask);
/*
* Try and work out if someone else has our IP
* using RFC 2131 Duplicate Address Detection
*/
temp_addr.s_addr = addr;
buf = g_strdup_printf ("/sbin/arping -q -D -c 1 -I %s %s",
iface, inet_ntoa (temp_addr));
if ((err = nm_spawn_process (buf)))
{
syslog (LOG_ERR, "Error: Duplicate address '%s' detected for "
"device '%s' \n", iface, inet_ntoa (temp_addr));
goto error;
}
g_free (buf);
/* set our IP address */
temp_addr.s_addr = addr;
temp_addr2.s_addr = broadcast;
s_tmp = g_strdup (inet_ntoa (temp_addr));
s_tmp2 = g_strdup (inet_ntoa (temp_addr2));
buf = g_strdup_printf ("/sbin/ip addr add %s/%d brd %s dev %s label %s",
s_tmp, prefix, s_tmp2, iface, iface);
g_free (s_tmp);
g_free (s_tmp2);
if ((err = nm_spawn_process (buf)))
{
syslog (LOG_ERR, "Error: could not set network configuration for "
"device '%s' using command:\n '%s'",
iface, buf);
goto error;
}
g_free (buf);
/* Alert other computers of our new address */
temp_addr.s_addr = addr;
buf = g_strdup_printf ("/sbin/arping -q -A -c 1 -I %s %s", iface,
inet_ntoa (temp_addr));
nm_spawn_process (buf);
g_free (buf);
g_usleep (G_USEC_PER_SEC * 2);
buf = g_strdup_printf ("/sbin/arping -q -U -c 1 -I %s %s", iface,
inet_ntoa (temp_addr));
nm_spawn_process (buf);
g_free (buf);
/* set the default route to be this device's gateway */
temp_addr.s_addr = nm_device_config_get_ip4_gateway (dev);
buf = g_strdup_printf ("/sbin/ip route replace default via %s dev %s",
inet_ntoa (temp_addr), iface);
if ((err = nm_spawn_process (buf)))
{
syslog (LOG_ERR, "Error: could not set default route using "
"command:\n '%s'", buf);
goto error;
}
g_free (buf);
return (TRUE);
error:
g_free (buf);
nm_system_device_flush_addresses (dev);
nm_system_device_flush_routes (dev);
return (FALSE);
} }
@@ -221,7 +324,7 @@ void nm_system_delete_default_route (void)
*/ */
void nm_system_kill_all_dhcp_daemons (void) void nm_system_kill_all_dhcp_daemons (void)
{ {
nm_spawn_process ("/usr/bin/killall dhclient"); nm_spawn_process ("/usr/bin/killall -q dhclient");
} }

View File

@@ -25,6 +25,7 @@
#include <dbus/dbus-glib.h> #include <dbus/dbus-glib.h>
#include <stdio.h> #include <stdio.h>
#include <getopt.h> #include <getopt.h>
#include <string.h>
/* These MUST correspond to NetworkManager device types */ /* These MUST correspond to NetworkManager device types */
typedef enum NMDeviceType typedef enum NMDeviceType