summaryrefslogtreecommitdiff
blob: fdac9fa05694ed0c7046a46dee6dbee755bbaf66 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
From 01cf3d5ead78786fb05921da18feeda985a05bac Mon Sep 17 00:00:00 2001
From: Mu Qiao <qiaomuf@gentoo.org>
Date: Wed, 5 Jan 2011 13:30:08 +0800
Subject: [PATCH] Support shared and link-local method for ipv4
 Signed-off-by: Mu Qiao <qiaomuf@gentoo.org>

---
 system-settings/plugins/ifnet/connection_parser.c  |   77 +++++++++++++++-----
 system-settings/plugins/ifnet/net_parser.c         |   12 +++-
 system-settings/plugins/ifnet/net_utils.c          |    4 +
 .../plugins/ifnet/nm-ifnet-connection.c            |    8 ++-
 system-settings/plugins/ifnet/wpa_parser.c         |   17 +++--
 5 files changed, 85 insertions(+), 33 deletions(-)

diff --git a/system-settings/plugins/ifnet/connection_parser.c b/system-settings/plugins/ifnet/connection_parser.c
index f9fae51..a755ec8 100644
--- a/system-settings/plugins/ifnet/connection_parser.c
+++ b/system-settings/plugins/ifnet/connection_parser.c
@@ -557,7 +557,7 @@ make_ip4_setting (NMConnection * connection, gchar * conn_name, GError ** error)
 
 	NMSettingIP4Config *ip4_setting =
 	    NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ());
-	gchar *value;
+	gchar *value, *method = NULL;
 	gboolean is_static_block = is_static_ip4 (conn_name);
 	ip_block *iblock = NULL;
 
@@ -569,12 +569,42 @@ make_ip4_setting (NMConnection * connection, gchar * conn_name, GError ** error)
 		      && strstr (value, "nogateway") ? TRUE : FALSE, NULL);
 
 	if (!is_static_block) {
-		g_object_set (ip4_setting,
-			      NM_SETTING_IP4_CONFIG_METHOD,
-			      NM_SETTING_IP4_CONFIG_METHOD_AUTO,
-			      NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, FALSE, NULL);
-		PLUGIN_PRINT (IFNET_PLUGIN_NAME, "Using DHCP for %s",
-			      conn_name);
+		method = ifnet_get_data (conn_name, "config");
+		if (!method){
+			g_set_error (error, ifnet_plugin_error_quark (), 0,
+						 "Unknown config for %s", conn_name);
+			g_object_unref (ip4_setting);
+			return;
+		}
+		if (!strcmp (method, "dhcp"))
+			g_object_set (ip4_setting,
+						  NM_SETTING_IP4_CONFIG_METHOD,
+						  NM_SETTING_IP4_CONFIG_METHOD_AUTO,
+						  NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, FALSE, NULL);
+		else if (!strcmp (method, "autoip")){
+			g_object_set (ip4_setting,
+						  NM_SETTING_IP4_CONFIG_METHOD,
+						  NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL,
+						  NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, FALSE, NULL);
+			nm_connection_add_setting (connection, NM_SETTING (ip4_setting));
+			return;
+		}
+		else if (!strcmp (method, "shared")){
+			g_object_set (ip4_setting,
+						  NM_SETTING_IP4_CONFIG_METHOD,
+						  NM_SETTING_IP4_CONFIG_METHOD_SHARED,
+						  NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, FALSE, NULL);
+			nm_connection_add_setting (connection, NM_SETTING (ip4_setting));
+			return;
+		}
+		else {
+			g_set_error (error, ifnet_plugin_error_quark (), 0,
+						 "Unknown config for %s", conn_name);
+			g_object_unref (ip4_setting);
+			return;
+		}
+		PLUGIN_PRINT (IFNET_PLUGIN_NAME, "Using %s method for %s",
+					  method, conn_name);
 	} else {
 		iblock = convert_ip4_config_block (conn_name);
 		if (!iblock) {
@@ -625,7 +655,7 @@ make_ip4_setting (NMConnection * connection, gchar * conn_name, GError ** error)
 	}
 
 	/* add dhcp hostname and client id */
-	if (!is_static_block) {
+	if (method && !strcmp (method, "dhcp")) {
 		gchar *dhcp_hostname, *client_id;
 
 		get_dhcp_hostname_and_client_id (&dhcp_hostname, &client_id);
@@ -712,7 +742,6 @@ make_ip4_setting (NMConnection * connection, gchar * conn_name, GError ** error)
 		iblock = iblock->next;
 		destroy_ip_block (current_iblock);
 	}
-
 	/* Finally add setting to connection */
 	nm_connection_add_setting (connection, NM_SETTING (ip4_setting));
 }
@@ -1243,10 +1272,10 @@ parse_wpa_psk (gchar * psk, GError ** error)
 	 * the passphrase contains spaces.
 	 */
 
-	p = psk;
-	if (p[0] == '"' && psk[strlen (psk) - 1] == '"')
+	p = g_strdup (psk);
+	if (p[0] == '"' && p[strlen (p) - 1] == '"')
 		quoted = TRUE;
-	if (!quoted && (strlen (psk) == 64)) {
+	if (!quoted && (strlen (p) == 64)) {
 		/* Verify the hex PSK; 64 digits */
 		if (!is_hex (p)) {
 			g_set_error (error, ifnet_plugin_error_quark (),
@@ -1254,7 +1283,7 @@ parse_wpa_psk (gchar * psk, GError ** error)
 				     "Invalid WPA_PSK (contains non-hexadecimal characters)");
 			goto out;
 		}
-		hashed = g_strdup (psk);
+		hashed = g_strdup (p);
 	} else {
 		strip_string (p, '"');
 
@@ -1276,6 +1305,7 @@ parse_wpa_psk (gchar * psk, GError ** error)
 	}
 
       out:
+	g_free (p);
 	return hashed;
 }
 
@@ -2085,7 +2115,8 @@ write_wireless_security_setting (NMConnection * connection,
 	} else if (!strcmp (key_mgmt, "wpa-eap")) {
 		wpa_set_data (conn_name, "key_mgmt", "WPA-EAP");
 		wpa = TRUE;
-	}
+	} else
+		PLUGIN_WARN (IFNET_PLUGIN_NAME, "Unknown key_mgmt: %s", key_mgmt);
 
 	if (auth_alg) {
 		if (!strcmp (auth_alg, "shared"))
@@ -2180,12 +2211,14 @@ write_wireless_security_setting (NMConnection * connection,
 			g_string_append (quoted, psk);
 			g_string_append_c (quoted, '"');
 		}
-		wpa_set_data (conn_name, "psk",
-			      quoted ? quoted->str : (gchar *) psk);
+		if (psk)
+			wpa_set_data (conn_name, "psk",
+					  quoted ? quoted->str : (gchar *) psk);
+		else
+			PLUGIN_WARN (IFNET_PLUGIN_NAME, "Use WPA, but no psk received from NM");
 		if (quoted)
 			g_string_free (quoted, TRUE);
-	} else
-		wpa_set_data (conn_name, "psk", NULL);
+	}
 
 	return TRUE;
 }
@@ -2443,8 +2476,12 @@ write_ip4_setting (NMConnection * connection, gchar * conn_name,
 		}
 		ifnet_set_data (conn_name, "config", ips->str);
 		g_string_free (ips, TRUE);
-	} else
-		ifnet_set_data (conn_name, "config", "dhcp");
+	} else if (!strcmp (value, NM_SETTING_IP4_CONFIG_METHOD_SHARED))
+		ifnet_set_data (conn_name, "config", "shared");
+	else if (!strcmp (value, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL))
+		ifnet_set_data (conn_name, "config", "autoip");
+	else
+        ifnet_set_data (conn_name, "config", "dhcp");
 
 	/* DNS Servers */
 	ifnet_set_data (conn_name, "dns_servers", NULL);
diff --git a/system-settings/plugins/ifnet/net_parser.c b/system-settings/plugins/ifnet/net_parser.c
index b4a381d..5991f09 100644
--- a/system-settings/plugins/ifnet/net_parser.c
+++ b/system-settings/plugins/ifnet/net_parser.c
@@ -377,6 +377,7 @@ ifnet_set_data (gchar * conn_name, gchar * key, gchar * value)
 {
 	gpointer orin_key = NULL, orin_value = NULL;
 	GHashTable *conn = g_hash_table_lookup (conn_table, conn_name);
+	gchar *new_value = NULL;
 
 	if (!conn) {
 		PLUGIN_WARN (IFNET_PLUGIN_NAME,
@@ -384,14 +385,19 @@ ifnet_set_data (gchar * conn_name, gchar * key, gchar * value)
 		return;
 	}
 	/* Remove existing key value pair */
+	if (value){
+		new_value = g_strdup (value);
+		strip_string (new_value, '"');
+	}
 	if (g_hash_table_lookup_extended (conn, key, &orin_key, &orin_value)) {
+		if (new_value && !strcmp (orin_value, new_value))
+			return;
 		g_hash_table_remove (conn, orin_key);
 		g_free (orin_key);
 		g_free (orin_value);
 	}
-	if (value)
-		g_hash_table_insert (conn, g_strdup (key),
-				     strip_string (g_strdup (value), '"'));
+	if (new_value)
+		g_hash_table_insert (conn, g_strdup (key), new_value);
 	net_parser_data_changed = TRUE;
 }
 
diff --git a/system-settings/plugins/ifnet/net_utils.c b/system-settings/plugins/ifnet/net_utils.c
index 2dc253c..7e52f10 100644
--- a/system-settings/plugins/ifnet/net_utils.c
+++ b/system-settings/plugins/ifnet/net_utils.c
@@ -278,6 +278,10 @@ is_static_ip4 (gchar * conn_name)
 
 	if (!data)
 		return FALSE;
+	if (!strcmp (data, "shared"))
+		return FALSE;
+	if (!strcmp (data, "autoip"))
+		return FALSE;
 	dhcp6 = strstr (data, "dhcp6");
 	if (dhcp6) {
 		gchar *dhcp4;
diff --git a/system-settings/plugins/ifnet/nm-ifnet-connection.c b/system-settings/plugins/ifnet/nm-ifnet-connection.c
index e47495c..ebe2581 100644
--- a/system-settings/plugins/ifnet/nm-ifnet-connection.c
+++ b/system-settings/plugins/ifnet/nm-ifnet-connection.c
@@ -71,12 +71,14 @@ nm_ifnet_connection_new (gchar * conn_name)
 {
 	NMConnection *tmp;
 	GObject *object;
-	GError **error = NULL;
+	GError *error = NULL;
 
 	g_return_val_if_fail (conn_name != NULL, NULL);
-	tmp = ifnet_update_connection_from_config_block (conn_name, error);
-	if (!tmp)
+	tmp = ifnet_update_connection_from_config_block (conn_name, &error);
+	if (!tmp){
+		g_error_free (error);
 		return NULL;
+	}
 	object = (GObject *) g_object_new (NM_TYPE_IFNET_CONNECTION,
 					   NM_IFNET_CONNECTION_CONN_NAME,
 					   conn_name, NULL);
diff --git a/system-settings/plugins/ifnet/wpa_parser.c b/system-settings/plugins/ifnet/wpa_parser.c
index 5e94108..957aafc 100644
--- a/system-settings/plugins/ifnet/wpa_parser.c
+++ b/system-settings/plugins/ifnet/wpa_parser.c
@@ -450,26 +450,29 @@ wpa_set_data (gchar * ssid, gchar * key, gchar * value)
 {
 	gpointer orig_key = NULL, orig_value = NULL;
 	GHashTable *security = g_hash_table_lookup (wsec_table, ssid);
+	gchar *new_value = NULL;
 
 	g_return_if_fail (security != NULL);
 
 	/* Remove old key value pairs */
+	if (value){
+		new_value = g_strdup(value);
+		if (strcmp (key, "ssid") != 0 && strcmp (key, "psk") != 0
+			&& !g_str_has_prefix (key, "wep_key"))
+			strip_string (new_value, '"');
+	}
 	if (g_hash_table_lookup_extended
 	    (security, key, &orig_key, &orig_value)) {
+		if (new_value && !strcmp(orig_value, new_value))
+			return;
 		g_hash_table_remove (security, orig_key);
 		g_free (orig_key);
 		g_free (orig_value);
 	}
 
 	/* Add new key value */
-	if (value) {
-		gchar *new_value = g_strdup (value);
-
-		if (strcmp (key, "ssid") != 0 && strcmp (key, "psk") != 0
-		    && !g_str_has_prefix (key, "wep_key"))
-			strip_string (new_value, '"');
+	if (new_value)
 		g_hash_table_insert (security, g_strdup (key), new_value);
-	}
 	wpa_parser_data_changed = TRUE;
 }
 
-- 
1.7.3.4