diff --git a/ChangeLog b/ChangeLog index 187cc53ad4..9c2b3f11b2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2004-11-05 Dan Williams + + Patch from Robert Paskowitz: + * src/backends/NetworkManagerGentoo.c + - Update static IP config code + 2004-11-05 Dan Williams * info-daemon/NetworkManagerInfoDbus.c diff --git a/src/backends/NetworkManagerGentoo.c b/src/backends/NetworkManagerGentoo.c index bfa30c1f65..1a07a0eea1 100644 --- a/src/backends/NetworkManagerGentoo.c +++ b/src/backends/NetworkManagerGentoo.c @@ -30,14 +30,6 @@ #include "NetworkManagerSystem.h" #include "NetworkManagerUtils.h" -typedef enum GENTOOConfType -{ - GENTOO_CONF_TYPE_IFCONFIG = 0, - GENTOO_CONF_TYPE_IPROUTE -} GENTOOConfType; - -static GENTOOConfType nm_system_gentoo_conf_type; - /* * nm_system_init * @@ -46,14 +38,10 @@ static GENTOOConfType nm_system_gentoo_conf_type; */ void nm_system_init (void) { -// TODO: autodetect conf type, probably by checking if /sbin/ip exists - -/* It's not safe to assume the the iproute stuff exists, but seeing as it - * is far more convinient to flush stuff with /sbin/ip with iproute - * I think it would be acceptable to introduce the sys-apps/iproute2 - * package as a dependancy for Gentoo. -*/ - nm_system_gentoo_conf_type = GENTOO_CONF_TYPE_IPROUTE; + /* While dhcpcd is the client of choice, it's not forced upon the user + * So we should probably put in a check for available clients, and + * modify our commands appropriatly. + */ } /* @@ -139,17 +127,7 @@ void nm_system_device_flush_routes (NMDevice *dev) /* Not really applicable for test devices */ if (nm_device_is_test_device (dev)) return; - - if (nm_system_gentoo_conf_type == GENTOO_CONF_TYPE_IPROUTE) { - snprintf (buf, 100, "/sbin/ip route flush dev %s", nm_device_get_iface (dev)); - } else if (nm_system_gentoo_conf_type == GENTOO_CONF_TYPE_IFCONFIG) { -// FIXME: this command still isn't right - -/* See note above */ - snprintf (buf, 100, "/sbin/route del dev %s", nm_device_get_iface (dev)); - } else { - snprintf (buf, 100, "/bin/false"); - } + snprintf (buf, 100, "/sbin/ip route flush dev %s", nm_device_get_iface (dev)); nm_spawn_process (buf); } @@ -169,16 +147,7 @@ void nm_system_device_flush_addresses (NMDevice *dev) if (nm_device_is_test_device (dev)) return; - if (nm_system_gentoo_conf_type == GENTOO_CONF_TYPE_IPROUTE) { - snprintf (buf, 100, "/sbin/ip address flush dev %s", nm_device_get_iface (dev)); - } else if (nm_system_gentoo_conf_type == GENTOO_CONF_TYPE_IFCONFIG) { -// FIXME: find the correct command - -/* See note above */ - snprintf (buf, 100, "/bin/false"); - } else { - snprintf (buf, 100, "/bin/false"); - } + snprintf (buf, 100, "/sbin/ip address flush dev %s", nm_device_get_iface (dev)); nm_spawn_process (buf); } @@ -195,6 +164,50 @@ void nm_system_device_flush_addresses (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 ip_addr, net_addr, broad_addr, gate_addr; + int i, err; + guint32 prefix = IPBITS; + char *iface; + char *buf; + char *addr, *netmask, *broadcast, *gateway; + + + /* Extract the addresses back into strings */ + + ip_addr.s_addr = nm_device_config_get_ip4_address (dev); + net_addr.s_addr = nm_device_config_get_ip4_netmask (dev); + broad_addr.s_addr = nm_device_config_get_ip4_broadcast (dev); + gate_addr.s_addr = nm_device_config_get_ip4_gateway (dev); + + addr = g_strdup (inet_ntoa (ip_addr)); + netmask = g_strdup (inet_ntoa (net_addr)); + broadcast = g_strdup (inet_ntoa (broad_addr)); + gateway = g_strdup (inet_ntoa (gate_addr)); + iface = nm_device_get_iface (dev); + + /* Flush the device and routes */ + nm_system_device_flush_addresses (dev); + nm_system_device_flush_routes (dev); + + /* Set the IP/broadcast */ + buf = g_strdup_printf("/sbin/ip addr add local %s dev %s broadcast %s %s", addr, iface, broadcast, iface); + syslog (LOG_WARNING, "Running: %s", buf); + /* Set the gateway */ + buf = g_strdup_printf("/sbin/ip route replace default dev %s via %s", iface, gateway); + syslog (LOG_WARNING, "Running: %s", buf); + + /* Inform other computers the we are on network */ + buf = g_strdup_printf ("/sbin/arping -q -A -c 1 -I %s %s", iface, addr); + syslog (LOG_WARNING, "Running: %s", buf); + buf = g_strdup_printf ("/sbin/arping -q -U -c 1 -I %s %s", iface, addr); + syslog (LOG_WARNING, "Running: %s", buf); + + return(TRUE); + +error: + g_free(buf); + return(FALSE); } @@ -206,13 +219,8 @@ gboolean nm_system_device_setup_static_ip4_config (NMDevice *dev) */ void nm_system_enable_loopback (void) { - if (nm_system_gentoo_conf_type == GENTOO_CONF_TYPE_IPROUTE) { - nm_spawn_process ("/sbin/ip link set dev lo up"); - nm_spawn_process ("/sbin/ip addr add 127.0.0.1/8 brd 127.255.255.255 dev lo label loopback"); - } else if (nm_system_gentoo_conf_type == GENTOO_CONF_TYPE_IFCONFIG) { - nm_spawn_process ("/sbin/ifconfig lo 127.0.0.1 up"); - nm_spawn_process ("/sbin/route add -net 127.0.0.0 netmask 255.0.0.0 gw 127.0.0.1 dev lo"); - } + nm_spawn_process ("/sbin/ip link set dev lo up"); + nm_spawn_process ("/sbin/ip addr add 127.0.0.1/8 brd 127.255.255.255 dev lo label loopback"); } /* @@ -223,11 +231,7 @@ void nm_system_enable_loopback (void) */ void nm_system_delete_default_route (void) { - if (nm_system_gentoo_conf_type == GENTOO_CONF_TYPE_IPROUTE) { - nm_spawn_process ("/sbin/ip route del default"); - } else if (nm_system_gentoo_conf_type == GENTOO_CONF_TYPE_IFCONFIG) { - nm_spawn_process ("/sbin/route del default"); - } + nm_spawn_process ("/sbin/ip route del default"); } /* @@ -275,65 +279,60 @@ void nm_system_load_device_modules (void) */ void nm_system_device_update_config_info (NMDevice *dev) { - char *cfg_file_path = NULL; - FILE *file = NULL; - char buffer[100]; - char confline[100], dhcpline[100], ipline[100]; - int ipa, ipb, ipc, ipd; - int nNext = 0, bNext = 0, count = 0; - char *confToken; - gboolean data_good = FALSE; - gboolean use_dhcp = TRUE; - guint32 ip4_address = 0; - guint32 ip4_netmask = 0; - guint32 ip4_gateway = 0; - guint32 ip4_broadcast = 0; + char *cfg_file_path = NULL; + FILE *file = NULL; + char buffer[100]; + char confline[100], dhcpline[100], ipline[100]; + int ipa, ipb, ipc, ipd; + int nNext = 0, bNext = 0, count = 0; + char *confToken; + gboolean data_good = FALSE; + gboolean use_dhcp = TRUE; + guint32 ip4_address = 0; + guint32 ip4_netmask = 0; + guint32 ip4_gateway = 0; - g_return_if_fail (dev != NULL); + g_return_if_fail (dev != NULL); - /* We use DHCP on an interface unless told not to */ - nm_device_config_set_use_dhcp (dev, TRUE); - nm_device_config_set_ip4_address (dev, 0); - nm_device_config_set_ip4_gateway (dev, 0); - nm_device_config_set_ip4_netmask (dev, 0); - nm_device_config_set_ip4_broadcast (dev, 0); + /* We use DHCP on an interface unless told not to */ + nm_device_config_set_use_dhcp (dev, TRUE); + nm_device_config_set_ip4_address (dev, 0); + nm_device_config_set_ip4_gateway (dev, 0); + nm_device_config_set_ip4_netmask (dev, 0); - /* Gentoo systems store this information in - * /etc/conf.d/net, this is for all interfaces. - */ + /* Gentoo systems store this information in + * /etc/conf.d/net, this is for all interfaces. + */ - cfg_file_path = g_strdup_printf ("/etc/conf.d/net"); - if (!cfg_file_path) - return; + cfg_file_path = g_strdup_printf ("/etc/conf.d/net"); + if (!cfg_file_path) + return; - if (!(file = fopen (cfg_file_path, "r"))) - { - g_free (cfg_file_path); - return; - } - sprintf(confline, "iface_%s", nm_device_get_iface (dev)); - sprintf(dhcpline, "iface_%s=\"dhcp\"", nm_device_get_iface (dev)); - while (fgets (buffer, 499, file) && !feof (file)) - { - /* Kock off newline if any */ - g_strstrip (buffer); + if (!(file = fopen (cfg_file_path, "r"))) + { + g_free (cfg_file_path); + return; + } + sprintf(confline, "iface_%s", nm_device_get_iface (dev)); + sprintf(dhcpline, "iface_%s=\"dhcp\"", nm_device_get_iface (dev)); + while (fgets (buffer, 499, file) && !feof (file)) + { + /* Kock off newline if any */ + g_strstrip (buffer); - if (strncmp (buffer, confline, strlen(confline)) == 0) - { - /* Make sure this config file is for this device */ - if (strncmp (&buffer[strlen(confline) - strlen(nm_device_get_iface (dev))], - nm_device_get_iface (dev), strlen(nm_device_get_iface (dev)) - ) != 0) - { - syslog (LOG_WARNING, "System config file '%s' does not define device '%s'\n", - cfg_file_path, nm_device_get_iface (dev)); - break; - } - else - data_good = TRUE; + if (strncmp (buffer, confline, strlen(confline)) == 0) + { + /* Make sure this config file is for this device */ + if (strncmp (&buffer[strlen(confline) - strlen(nm_device_get_iface (dev))], + nm_device_get_iface (dev), strlen(nm_device_get_iface (dev))) != 0) + { + syslog (LOG_WARNING, "System config file '%s' does not define device '%s'\n", + cfg_file_path, nm_device_get_iface (dev)); + break; + } + else + data_good = TRUE; - - if (strncmp (buffer, dhcpline, strlen(dhcpline)) == 0) { use_dhcp = TRUE; @@ -343,7 +342,7 @@ void nm_system_device_update_config_info (NMDevice *dev) use_dhcp = FALSE; confToken = strtok(&buffer[strlen(confline) + 2], " "); while (count < 3) - { + { if (nNext == 1 && bNext == 1) { ip4_address = inet_addr (confToken); @@ -360,7 +359,6 @@ void nm_system_device_update_config_info (NMDevice *dev) else if (strcmp(confToken, "broadcast") == 0) { confToken = strtok(NULL, " "); - ip4_broadcast = inet_addr (confToken); count++; bNext = 1; } @@ -369,13 +367,13 @@ void nm_system_device_update_config_info (NMDevice *dev) ip4_address = inet_addr (confToken); count++; } - confToken = strtok(NULL, " "); + confToken = strtok(NULL, " "); + } } } - } /* If we aren't using dhcp, then try to get the gateway */ if (!use_dhcp) - { + { sprintf(ipline, "gateway=\"%s/", nm_device_get_iface (dev)); if (strncmp(buffer, ipline, strlen(ipline) - 1) == 0) { @@ -385,21 +383,21 @@ void nm_system_device_update_config_info (NMDevice *dev) ip4_gateway = inet_addr (ipline); } } - } - fclose (file); - g_free (cfg_file_path); - - /* If successful, set values on the device */ - if (data_good) - { - nm_device_config_set_use_dhcp (dev, use_dhcp); - if (ip4_address) - nm_device_config_set_ip4_address (dev, ip4_address); - if (ip4_gateway) - nm_device_config_set_ip4_gateway (dev, ip4_gateway); - if (ip4_netmask) - nm_device_config_set_ip4_netmask (dev, ip4_netmask); - if (ip4_broadcast) - nm_device_config_set_ip4_broadcast (dev, ip4_broadcast); - } + } + fclose (file); + g_free (cfg_file_path); + + /* If successful, set values on the device */ + if (data_good) + { + nm_device_config_set_use_dhcp (dev, use_dhcp); + if (ip4_address) + nm_device_config_set_ip4_address (dev, ip4_address); + if (ip4_gateway) + nm_device_config_set_ip4_gateway (dev, ip4_gateway); + if (ip4_netmask) + nm_device_config_set_ip4_netmask (dev, ip4_netmask); + if (ip4_broadcast) + nm_device_config_set_ip4_broadcast (dev, ip4_broadcast); + } }