summaryrefslogtreecommitdiffstats
path: root/networkmanager/discover_mac_address.patch
blob: c73921f3bb654c3ef296656d646997d92bc7eed8 (plain)
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
diff -up NetworkManager-0.8.9997/src/settings/plugins/ifcfg-rh/reader.c.hwaddr NetworkManager-0.8.9997/src/settings/plugins/ifcfg-rh/reader.c
--- NetworkManager-0.8.9997/src/settings/plugins/ifcfg-rh/reader.c.hwaddr	2011-05-27 16:38:29.000000000 +0200
+++ NetworkManager-0.8.9997/src/settings/plugins/ifcfg-rh/reader.c	2011-07-31 20:40:11.459089519 +0200
@@ -157,6 +157,46 @@ make_connection_setting (const char *fil
 }
 
 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, GByteArray **array, GError **error)
 {
 	char *value = NULL;
@@ -2750,6 +2790,10 @@ make_wireless_setting (shvarFile *ifcfg,
 	s_wireless = NM_SETTING_WIRELESS (nm_setting_wireless_new ());
 
 	if (read_mac_address (ifcfg, "HWADDR", &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);
 
@@ -3083,6 +3127,10 @@ make_wired_setting (shvarFile *ifcfg,
 	}
 
 	if (read_mac_address (ifcfg, "HWADDR", &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);