nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* GIO - GLib Input, Output and Streaming Library |
2 | * |
||
3 | * Copyright 2014 Red Hat, Inc. |
||
4 | * |
||
5 | * This library is free software; you can redistribute it and/or |
||
6 | * modify it under the terms of the GNU Lesser General Public |
||
7 | * License as published by the Free Software Foundation; either |
||
8 | * version 2 of the License, or (at your option) any later version. |
||
9 | * |
||
10 | * This library is distributed in the hope that it will be useful, |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||
13 | * Lesser General Public License for more details. |
||
14 | * |
||
15 | * You should have received a copy of the GNU Lesser General |
||
16 | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
||
17 | */ |
||
18 | |||
19 | #include "config.h" |
||
20 | |||
21 | #include <errno.h> |
||
22 | #include <string.h> |
||
23 | #include <unistd.h> |
||
24 | |||
25 | #include "gnetworkmonitornm.h" |
||
26 | #include "gioerror.h" |
||
27 | #include "ginitable.h" |
||
28 | #include "giomodule-priv.h" |
||
29 | #include "glibintl.h" |
||
30 | #include "glib/gstdio.h" |
||
31 | #include "gnetworkingprivate.h" |
||
32 | #include "gnetworkmonitor.h" |
||
33 | #include "gdbusproxy.h" |
||
34 | |||
35 | static void g_network_monitor_nm_iface_init (GNetworkMonitorInterface *iface); |
||
36 | static void g_network_monitor_nm_initable_iface_init (GInitableIface *iface); |
||
37 | |||
38 | enum |
||
39 | { |
||
40 | PROP_0, |
||
41 | |||
42 | PROP_NETWORK_AVAILABLE, |
||
43 | PROP_NETWORK_METERED, |
||
44 | PROP_CONNECTIVITY |
||
45 | }; |
||
46 | |||
47 | typedef enum { |
||
48 | NM_CONNECTIVITY_UNKNOWN, |
||
49 | NM_CONNECTIVITY_NONE, |
||
50 | NM_CONNECTIVITY_PORTAL, |
||
51 | NM_CONNECTIVITY_LIMITED, |
||
52 | NM_CONNECTIVITY_FULL |
||
53 | } NMConnectivityState; |
||
54 | |||
55 | struct _GNetworkMonitorNMPrivate |
||
56 | { |
||
57 | GDBusProxy *proxy; |
||
58 | |||
59 | GNetworkConnectivity connectivity; |
||
60 | gboolean network_available; |
||
61 | gboolean network_metered; |
||
62 | }; |
||
63 | |||
64 | #define g_network_monitor_nm_get_type _g_network_monitor_nm_get_type |
||
65 | G_DEFINE_TYPE_WITH_CODE (GNetworkMonitorNM, g_network_monitor_nm, G_TYPE_NETWORK_MONITOR_NETLINK, |
||
66 | G_ADD_PRIVATE (GNetworkMonitorNM) |
||
67 | G_IMPLEMENT_INTERFACE (G_TYPE_NETWORK_MONITOR, |
||
68 | g_network_monitor_nm_iface_init) |
||
69 | G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, |
||
70 | g_network_monitor_nm_initable_iface_init) |
||
71 | _g_io_modules_ensure_extension_points_registered (); |
||
72 | g_io_extension_point_implement (G_NETWORK_MONITOR_EXTENSION_POINT_NAME, |
||
73 | g_define_type_id, |
||
74 | "networkmanager", |
||
75 | 30)) |
||
76 | |||
77 | static void |
||
78 | g_network_monitor_nm_init (GNetworkMonitorNM *nm) |
||
79 | { |
||
80 | nm->priv = g_network_monitor_nm_get_instance_private (nm); |
||
81 | } |
||
82 | |||
83 | static void |
||
84 | g_network_monitor_nm_get_property (GObject *object, |
||
85 | guint prop_id, |
||
86 | GValue *value, |
||
87 | GParamSpec *pspec) |
||
88 | { |
||
89 | GNetworkMonitorNM *nm = G_NETWORK_MONITOR_NM (object); |
||
90 | |||
91 | switch (prop_id) |
||
92 | { |
||
93 | case PROP_NETWORK_AVAILABLE: |
||
94 | g_value_set_boolean (value, nm->priv->network_available); |
||
95 | break; |
||
96 | |||
97 | case PROP_NETWORK_METERED: |
||
98 | g_value_set_boolean (value, nm->priv->network_metered); |
||
99 | break; |
||
100 | |||
101 | case PROP_CONNECTIVITY: |
||
102 | g_value_set_enum (value, nm->priv->connectivity); |
||
103 | break; |
||
104 | |||
105 | default: |
||
106 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
||
107 | break; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | static GNetworkConnectivity |
||
112 | nm_conn_to_g_conn (int nm_state) |
||
113 | { |
||
114 | switch (nm_state) |
||
115 | { |
||
116 | case NM_CONNECTIVITY_UNKNOWN: |
||
117 | return G_NETWORK_CONNECTIVITY_LOCAL; |
||
118 | case NM_CONNECTIVITY_NONE: |
||
119 | return G_NETWORK_CONNECTIVITY_LOCAL; |
||
120 | case NM_CONNECTIVITY_PORTAL: |
||
121 | return G_NETWORK_CONNECTIVITY_PORTAL; |
||
122 | case NM_CONNECTIVITY_LIMITED: |
||
123 | return G_NETWORK_CONNECTIVITY_LIMITED; |
||
124 | case NM_CONNECTIVITY_FULL: |
||
125 | return G_NETWORK_CONNECTIVITY_FULL; |
||
126 | default: |
||
127 | g_warning ("Unknown NM connectivity state %d", nm_state); |
||
128 | return G_NETWORK_CONNECTIVITY_LOCAL; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | static gboolean |
||
133 | nm_metered_to_bool (guint nm_metered) |
||
134 | { |
||
135 | switch (nm_metered) |
||
136 | { |
||
137 | case 1: /* yes */ |
||
138 | case 3: /* guess-yes */ |
||
139 | return TRUE; |
||
140 | case 0: /* unknown */ |
||
141 | /* We default to FALSE in the unknown-because-you're-not-running-NM |
||
142 | * case, so we should return FALSE in the |
||
143 | * unknown-when-you-are-running-NM case too. */ |
||
144 | case 2: /* no */ |
||
145 | case 4: /* guess-no */ |
||
146 | return FALSE; |
||
147 | default: |
||
148 | g_warning ("Unknown NM metered state %d", nm_metered); |
||
149 | return FALSE; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | static void |
||
154 | sync_properties (GNetworkMonitorNM *nm, |
||
155 | gboolean emit_signals) |
||
156 | { |
||
157 | GVariant *v; |
||
158 | NMConnectivityState nm_connectivity; |
||
159 | gboolean new_network_available; |
||
160 | gboolean new_network_metered; |
||
161 | GNetworkConnectivity new_connectivity; |
||
162 | |||
163 | v = g_dbus_proxy_get_cached_property (nm->priv->proxy, "Connectivity"); |
||
164 | nm_connectivity = g_variant_get_uint32 (v); |
||
165 | g_variant_unref (v); |
||
166 | |||
167 | if (nm_connectivity == NM_CONNECTIVITY_NONE) |
||
168 | { |
||
169 | new_network_available = FALSE; |
||
170 | new_network_metered = FALSE; |
||
171 | new_connectivity = G_NETWORK_CONNECTIVITY_LOCAL; |
||
172 | } |
||
173 | else |
||
174 | { |
||
175 | |||
176 | /* this is only available post NM 1.0 */ |
||
177 | v = g_dbus_proxy_get_cached_property (nm->priv->proxy, "Metered"); |
||
178 | if (v == NULL) |
||
179 | { |
||
180 | new_network_metered = FALSE; |
||
181 | } |
||
182 | else |
||
183 | { |
||
184 | new_network_metered = nm_metered_to_bool (g_variant_get_uint32 (v)); |
||
185 | g_variant_unref (v); |
||
186 | } |
||
187 | |||
188 | new_network_available = TRUE; |
||
189 | new_connectivity = nm_conn_to_g_conn (nm_connectivity); |
||
190 | } |
||
191 | |||
192 | if (!emit_signals) |
||
193 | { |
||
194 | nm->priv->network_metered = new_network_metered; |
||
195 | nm->priv->network_available = new_network_available; |
||
196 | nm->priv->connectivity = new_connectivity; |
||
197 | return; |
||
198 | } |
||
199 | |||
200 | if (new_network_available != nm->priv->network_available) |
||
201 | { |
||
202 | nm->priv->network_available = new_network_available; |
||
203 | g_object_notify (G_OBJECT (nm), "network-available"); |
||
204 | } |
||
205 | if (new_network_metered != nm->priv->network_metered) |
||
206 | { |
||
207 | nm->priv->network_metered = new_network_metered; |
||
208 | g_object_notify (G_OBJECT (nm), "network-metered"); |
||
209 | } |
||
210 | if (new_connectivity != nm->priv->connectivity) |
||
211 | { |
||
212 | nm->priv->connectivity = new_connectivity; |
||
213 | g_object_notify (G_OBJECT (nm), "connectivity"); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | static void |
||
218 | update_cached_property (GDBusProxy *proxy, |
||
219 | const char *property_name, |
||
220 | GVariantDict *dict) |
||
221 | { |
||
222 | GVariant *v; |
||
223 | |||
224 | v = g_variant_dict_lookup_value (dict, property_name, NULL); |
||
225 | if (!v) |
||
226 | return; |
||
227 | g_dbus_proxy_set_cached_property (proxy, property_name, v); |
||
228 | } |
||
229 | |||
230 | static void |
||
231 | proxy_signal_cb (GDBusProxy *proxy, |
||
232 | gchar *sender_name, |
||
233 | gchar *signal_name, |
||
234 | GVariant *parameters, |
||
235 | GNetworkMonitorNM *nm) |
||
236 | { |
||
237 | GVariant *asv; |
||
238 | GVariantDict *dict; |
||
239 | |||
240 | if (g_strcmp0 (signal_name, "PropertiesChanged") != 0) |
||
241 | return; |
||
242 | |||
243 | g_variant_get (parameters, "(@a{sv})", &asv); |
||
244 | if (!asv) |
||
245 | return; |
||
246 | |||
247 | dict = g_variant_dict_new (asv); |
||
248 | if (!dict) |
||
249 | { |
||
250 | g_warning ("Failed to handle PropertiesChanged signal from NetworkManager"); |
||
251 | return; |
||
252 | } |
||
253 | |||
254 | update_cached_property (nm->priv->proxy, "Connectivity", dict); |
||
255 | |||
256 | g_variant_dict_unref (dict); |
||
257 | |||
258 | sync_properties (nm, TRUE); |
||
259 | } |
||
260 | |||
261 | static gboolean |
||
262 | has_property (GDBusProxy *proxy, |
||
263 | const char *property_name) |
||
264 | { |
||
265 | char **props; |
||
266 | guint i; |
||
267 | gboolean prop_found = FALSE; |
||
268 | |||
269 | props = g_dbus_proxy_get_cached_property_names (proxy); |
||
270 | |||
271 | if (!props) |
||
272 | return FALSE; |
||
273 | |||
274 | for (i = 0; props[i] != NULL; i++) |
||
275 | { |
||
276 | if (g_str_equal (props[i], property_name)) |
||
277 | { |
||
278 | prop_found = TRUE; |
||
279 | break; |
||
280 | } |
||
281 | } |
||
282 | |||
283 | g_strfreev (props); |
||
284 | return prop_found; |
||
285 | } |
||
286 | |||
287 | static gboolean |
||
288 | g_network_monitor_nm_initable_init (GInitable *initable, |
||
289 | GCancellable *cancellable, |
||
290 | GError **error) |
||
291 | { |
||
292 | GNetworkMonitorNM *nm = G_NETWORK_MONITOR_NM (initable); |
||
293 | GDBusProxy *proxy; |
||
294 | GInitableIface *parent_iface; |
||
295 | gchar *name_owner = NULL; |
||
296 | |||
297 | parent_iface = g_type_interface_peek_parent (G_NETWORK_MONITOR_NM_GET_INITABLE_IFACE (initable)); |
||
298 | if (!parent_iface->init (initable, cancellable, error)) |
||
299 | return FALSE; |
||
300 | |||
301 | proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, |
||
302 | G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START | G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES, |
||
303 | NULL, |
||
304 | "org.freedesktop.NetworkManager", |
||
305 | "/org/freedesktop/NetworkManager", |
||
306 | "org.freedesktop.NetworkManager", |
||
307 | cancellable, |
||
308 | error); |
||
309 | if (!proxy) |
||
310 | return FALSE; |
||
311 | |||
312 | name_owner = g_dbus_proxy_get_name_owner (proxy); |
||
313 | |||
314 | if (!name_owner) |
||
315 | { |
||
316 | g_object_unref (proxy); |
||
317 | return FALSE; |
||
318 | } |
||
319 | |||
320 | g_free (name_owner); |
||
321 | |||
322 | /* Verify it has the PrimaryConnection and Connectivity properties */ |
||
323 | if (!has_property (proxy, "Connectivity")) |
||
324 | { |
||
325 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
||
326 | _("NetworkManager version too old")); |
||
327 | g_object_unref (proxy); |
||
328 | return FALSE; |
||
329 | } |
||
330 | |||
331 | g_signal_connect (G_OBJECT (proxy), "g-signal", |
||
332 | G_CALLBACK (proxy_signal_cb), nm); |
||
333 | nm->priv->proxy = proxy; |
||
334 | sync_properties (nm, FALSE); |
||
335 | |||
336 | return TRUE; |
||
337 | } |
||
338 | |||
339 | static void |
||
340 | g_network_monitor_nm_finalize (GObject *object) |
||
341 | { |
||
342 | GNetworkMonitorNM *nm = G_NETWORK_MONITOR_NM (object); |
||
343 | |||
344 | g_clear_object (&nm->priv->proxy); |
||
345 | |||
346 | G_OBJECT_CLASS (g_network_monitor_nm_parent_class)->finalize (object); |
||
347 | } |
||
348 | |||
349 | static void |
||
350 | g_network_monitor_nm_class_init (GNetworkMonitorNMClass *nl_class) |
||
351 | { |
||
352 | GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class); |
||
353 | |||
354 | gobject_class->finalize = g_network_monitor_nm_finalize; |
||
355 | gobject_class->get_property = g_network_monitor_nm_get_property; |
||
356 | |||
357 | g_object_class_override_property (gobject_class, PROP_NETWORK_AVAILABLE, "network-available"); |
||
358 | g_object_class_override_property (gobject_class, PROP_NETWORK_METERED, "network-metered"); |
||
359 | g_object_class_override_property (gobject_class, PROP_CONNECTIVITY, "connectivity"); |
||
360 | } |
||
361 | |||
362 | static void |
||
363 | g_network_monitor_nm_iface_init (GNetworkMonitorInterface *monitor_iface) |
||
364 | { |
||
365 | } |
||
366 | |||
367 | static void |
||
368 | g_network_monitor_nm_initable_iface_init (GInitableIface *iface) |
||
369 | { |
||
370 | iface->init = g_network_monitor_nm_initable_init; |
||
371 | } |