Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > 42b045660792cbae411a02843159994c > files > 3

networkmanager-0.9.8.8-3.1.mga4.src.rpm

--- NetworkManager-0.9.3.995/src/settings/plugins/ifcfg-rh/reader.c~	2012-03-02 00:45:18.000000000 +0100
+++ NetworkManager-0.9.3.995/src/settings/plugins/ifcfg-rh/reader.c	2012-03-06 23:52:24.759497076 +0100
@@ -171,6 +171,46 @@
 }
 
 static gboolean
+discover_mac_address(shvarFile *ifcfg, GByteArray **array, GError **error)
+{
+	int fd, ret;
+	struct ifreq ifr;
+	char *device;
+
+	device = svGetValue (ifcfg, "DEVICE", FALSE);
+
+	g_return_val_if_fail (device != NULL, FALSE);
+	g_return_val_if_fail (array != NULL, FALSE);
+	g_return_val_if_fail (*array == NULL, FALSE);
+	g_return_val_if_fail (error != NULL, FALSE);
+	g_return_val_if_fail (*error == NULL, FALSE);
+
+	fd = socket(AF_INET, SOCK_DGRAM, 0);
+	if (fd < 0) {
+		g_set_error(error, IFCFG_PLUGIN_ERROR, errno,
+				"Unable to discover MAC address: socket error");
+		g_free (device);
+		return FALSE;
+	}
+
+	ifr.ifr_addr.sa_family = AF_INET;
+	strncpy(ifr.ifr_name, device, IFNAMSIZ-1);
+
+	ret = ioctl(fd, SIOCGIFHWADDR, &ifr);
+	g_free (device);
+	if (ret < 0) {
+		g_set_error(error, IFCFG_PLUGIN_ERROR, errno,
+				"Unable to discover MAC address: ioctl error");
+		return FALSE;
+	}
+	close(fd);
+
+	*array = g_byte_array_sized_new (ETH_ALEN);
+	g_byte_array_append (*array, (guint8 *) ifr.ifr_hwaddr.sa_data, ETH_ALEN);
+	return TRUE;
+}
+
+static gboolean
 read_mac_address (shvarFile *ifcfg, const char *key, int type,
                   GByteArray **array, GError **error)
 {
@@ -2887,6 +2927,10 @@
 	s_wireless = NM_SETTING_WIRELESS (nm_setting_wireless_new ());
 
 	if (read_mac_address (ifcfg, "HWADDR", ARPHRD_ETHER, &array, error)) {
+		/* if we don't have a HWADDR saved in ifcfg file, try to discover it manually */
+		if (!array) {
+			discover_mac_address(ifcfg, &array, error);
+		}
 		if (array) {
 			g_object_set (s_wireless, NM_SETTING_WIRELESS_MAC_ADDRESS, array, NULL);
 
@@ -3217,6 +3261,10 @@
 	}
 
 	if (read_mac_address (ifcfg, "HWADDR", ARPHRD_ETHER, &mac, error)) {
+		/* if we don't have a HWADDR saved in ifcfg file, try to discover it manually */
+		if (!mac) {
+			discover_mac_address(ifcfg, &mac, error);
+		}
 		if (mac) {
 			g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac, NULL);