[PATCH] Backend for Gentoo Linux with baselayout-1.12



Hi guys!
I just wrote a patch which enables NM to read configuration file for Gentoo Linux with baselayout-1.12 (which has different format than before).

Currently, my patch can parse file which are formatted as in /etc/conf.d/net.example with long options:

EXAMPLE:

config_eth0=( "dhcp" )

config_eth0=( "192.168.0.1 netmask 255.255.255.0 broadcast 192.168.0.255" )
routes_eth0=( "default via 192.168.0.254" )
dns_servers_eth0=( "192.168.0.252 192.168.0.253" )

If the broadcast is not set, it is reset assuming netmask = 255.255.255.0 and set to the IP with last number = 255.

This patch MUST be improved to support spaces in the files, and the configure script must have a test to check when apply it (or simply an use-flag in the ebuild).

Hope this help!
Bye!
	Shogun

[BEGIN PATCH]
--- src/backends/NetworkManagerGentoo.c.orig 2006-08-04 16:16:02.000000000 +0200
+++ src/backends/NetworkManagerGentoo.c	2006-08-04 16:16:31.000000000 +0200
@@ -170,7 +170,9 @@
  */
 void nm_system_enable_loopback (void)
 {
-	nm_spawn_process("/etc/init.d/net.lo start");
+  /* No need to run net.lo if it is already running */
+	if (nm_spawn_process ("/etc/init.d/net.lo status") != 0)
+		nm_spawn_process("/etc/init.d/net.lo start");
 }


@@ -304,8 +306,9 @@
 	char		*cfg_file_path = NULL;
 	FILE		*file = NULL;
 	char		 buffer[100];
-	char		 confline[100], dhcpline[100], ipline[100];
+	char		 confline[100], dhcpline[100], ipline[100], dnsline[100], brd[100];
 	int		 ipa, ipb, ipc, ipd;
+	int brd_a, brd_b, brd_c, brd_d;
  	int		 nNext =  0, bNext = 0, count = 0;
 	char		*confToken;
 	gboolean	 data_good = FALSE;
@@ -315,11 +318,18 @@
 	guint32	 ip4_netmask = 0;
 	guint32	 ip4_gateway = 0;
 	guint32	 ip4_broadcast = 0;
-
+	GSList *dns = NULL, *dns_head = NULL;
+	
 	g_return_val_if_fail (dev != NULL, NULL);

 	sys_data = g_malloc0 (sizeof (GentooSystemConfigData));
-    sys_data->config = nm_device_get_ip4_config(dev);
+   sys_data->config = nm_device_get_ip4_config(dev);
+
+   //The config is null? Create it!
+   if(sys_data->config == NULL)
+   	nm_device_set_ip4_config(dev, nm_ip4_config_new());
+	sys_data->config = nm_device_get_ip4_config(dev);
+   	
 	/* We use DHCP on an interface unless told not to */
 	sys_data->use_dhcp = TRUE;
 	nm_device_set_use_dhcp (dev, TRUE);
@@ -340,8 +350,8 @@
 		g_free (cfg_file_path);
 		return NULL;
 	}
- 	sprintf(confline, "iface_%s", nm_device_get_iface (dev));
- 	sprintf(dhcpline, "iface_%s=\"dhcp\"", nm_device_get_iface (dev));
+ 	sprintf(confline, "config_%s", nm_device_get_iface (dev));
+ 	sprintf(dhcpline, "%s=( \"dhcp\" )", confline);
 	while (fgets (buffer, 499, file) && !feof (file))
 	{
 		/* Kock off newline if any */
@@ -367,18 +377,23 @@
 			else
 			{
 				use_dhcp = FALSE;
-				confToken = strtok(&buffer[strlen(confline) + 2], " ");
+				confToken = strtok(&buffer[strlen(confline) + 4], " ");
 				while (count < 3)
 					{
+					if(strncmp(confToken, ")", 1) == 0) break;
+					
 					if (nNext == 1 && bNext == 1)
 					{
-						ip4_address = inet_addr (confToken);
-						count++;
+						nm_warning("Skipped IP, should be take later...");
+//						ip4_address = inet_addr (confToken);
+//						count++;
 						continue;
 					}
 					if (strcmp(confToken, "netmask") == 0)
 					{
 						confToken = strtok(NULL, " ");
+ if(confToken[strlen(confToken) - 1] == '\"') //remove the ending ", if present
+							confToken[strlen(confToken) - 1] = '\0';
 						ip4_netmask = inet_addr (confToken);
 						count++;
 						nNext = 1;
@@ -386,28 +401,47 @@
 					else if (strcmp(confToken, "broadcast") == 0)
 					{
 						confToken = strtok(NULL, " ");
+						ip4_broadcast = inet_addr (confToken);						
 						count++;
 						bNext = 1;
 					}
 					else
 					{
 						ip4_address = inet_addr (confToken);
+ sscanf(confToken, "%d.%d.%d.%d", &brd_a, &brd_b, &brd_c, &brd_d); //These are set for broadcast later
 						count++;
 					}
+					
 						confToken = strtok(NULL, " ");
 					}
 				}
 			}
-		/* If we aren't using dhcp, then try to get the gateway */
+			
+		/* If we aren't using dhcp, then try to get the gateway and the DNS*/
 		if (!use_dhcp)
 			{
-			sprintf(ipline, "gateway=\"%s/", nm_device_get_iface (dev));
-			if (strncmp(buffer, ipline, strlen(ipline) - 1) == 0)
+			sprintf(ipline, "routes_%s", nm_device_get_iface (dev));
+			sprintf(dnsline, "dns_servers_%s", nm_device_get_iface (dev));
+			
+			if (strncmp(buffer, ipline, strlen(ipline)) == 0)
 			{
- sprintf(ipline, "gateway=\"%s/%%d.%%d.%%d.%%d\"", nm_device_get_iface (dev) ); + sprintf(ipline, "routes_%s=( \"default via %%d.%%d.%%d.%%d\" )", nm_device_get_iface (dev) );
 				sscanf(buffer, ipline, &ipa, &ipb, &ipc, &ipd);
 				sprintf(ipline, "%d.%d.%d.%d", ipa, ipb, ipc, ipd);
 				ip4_gateway = inet_addr (ipline);
+			}else if (strncmp(buffer, dnsline, strlen(dnsline)) == 0)
+			{
+				confToken = strtok(&buffer[strlen(dnsline) + 4], " ");
+				while(confToken != NULL)
+				{
+ if(confToken[strlen(confToken) - 1] == '\"') //remove the ending ", if present
+							confToken[strlen(confToken) - 1] = '\0';
+
+						if(strncmp(confToken, ")", 1) != 0)
+							dns = g_slist_append(dns, GINT_TO_POINTER(inet_addr(confToken)));
+						
+						confToken = strtok(NULL, " ");
+				}
 			}
 		}		
 	}
@@ -417,7 +451,8 @@
 	/* If successful, set values on the device */
 	if (data_good)
 	{
-        nm_warning("data good :-)");
+      nm_warning("data good :-)");
+		sys_data->use_dhcp = use_dhcp;
 		nm_device_set_use_dhcp (dev, use_dhcp);
 		if (ip4_address)
             nm_ip4_config_set_address (sys_data->config, ip4_address);
@@ -425,8 +460,27 @@
             nm_ip4_config_set_gateway (sys_data->config, ip4_gateway);
 		if (ip4_netmask)
 			nm_ip4_config_set_netmask (sys_data->config, ip4_netmask);
+
+ if (!ip4_broadcast) // If no broadcast set, get it from the ip assuming the netmask is 255.255.255.0
+		{
+			sprintf(brd, "%d.%d.%d.%d", brd_a, brd_b, brd_c, 255);
+			nm_warning("Broadcast set to: %s", brd);
+			ip4_broadcast = inet_addr(brd);
+		}
+		
 		if (ip4_broadcast)
 			nm_ip4_config_set_broadcast (sys_data->config, ip4_broadcast);
+
+		if( (dns_head = dns) != NULL ) //One or more DNS set
+		{
+			while(dns_head != NULL)
+			{
+ nm_ip4_config_add_nameserver(sys_data->config, GPOINTER_TO_INT(dns_head->data));
+				dns_head = g_slist_next(dns_head);
+			}
+			
+			g_slist_free(dns);
+		}
 	}
 	return (void *)sys_data;
 }
[END PATCH]



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]