1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2014-2018 Jan-Michael Brummer <jan.brummer@tabos.org>
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.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
34 #include "gwin32networkmonitor.h"
35 #include "ginetaddress.h"
36 #include "ginetaddressmask.h"
37 #include "ginitable.h"
38 #include "giomodule-priv.h"
40 #include "glib/gstdio.h"
41 #include "gnetworkingprivate.h"
43 #include "gnetworkmonitor.h"
46 static GInitableIface
*initable_parent_iface
;
47 static void g_win32_network_monitor_iface_init (GNetworkMonitorInterface
*iface
);
48 static void g_win32_network_monitor_initable_iface_init (GInitableIface
*iface
);
50 struct _GWin32NetworkMonitorPrivate
54 GMainContext
*main_context
;
55 GSource
*route_change_source
;
59 #define g_win32_network_monitor_get_type _g_win32_network_monitor_get_type
60 G_DEFINE_TYPE_WITH_CODE (GWin32NetworkMonitor
, g_win32_network_monitor
, G_TYPE_NETWORK_MONITOR_BASE
,
61 G_ADD_PRIVATE (GWin32NetworkMonitor
)
62 G_IMPLEMENT_INTERFACE (G_TYPE_NETWORK_MONITOR
,
63 g_win32_network_monitor_iface_init
)
64 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE
,
65 g_win32_network_monitor_initable_iface_init
)
66 _g_io_modules_ensure_extension_points_registered ();
67 g_io_extension_point_implement (G_NETWORK_MONITOR_EXTENSION_POINT_NAME
,
73 g_win32_network_monitor_init (GWin32NetworkMonitor
*win
)
75 win
->priv
= g_win32_network_monitor_get_instance_private (win
);
79 win_network_monitor_get_ip_info (IP_ADDRESS_PREFIX prefix
,
80 GSocketFamily
*family
,
84 switch (prefix
.Prefix
.si_family
)
87 /* Fall-through: AF_UNSPEC deliveres both IPV4 and IPV6 infos, let`s stick with IPV4 here */
89 *family
= G_SOCKET_FAMILY_IPV4
;
90 *dest
= (guint8
*) &prefix
.Prefix
.Ipv4
.sin_addr
;
91 *len
= prefix
.PrefixLength
;
94 *family
= G_SOCKET_FAMILY_IPV6
;
95 *dest
= (guint8
*) &prefix
.Prefix
.Ipv6
.sin6_addr
;
96 *len
= prefix
.PrefixLength
;
105 static GInetAddressMask
*
106 get_network_mask (GSocketFamily family
,
110 GInetAddressMask
*network
;
111 GInetAddress
*dest_addr
;
114 dest_addr
= g_inet_address_new_from_bytes (dest
, family
);
116 dest_addr
= g_inet_address_new_any (family
);
118 network
= g_inet_address_mask_new (dest_addr
, len
, NULL
);
119 g_object_unref (dest_addr
);
125 win_network_monitor_process_table (GWin32NetworkMonitor
*win
,
131 MIB_IPFORWARD_TABLE2
*routes
= NULL
;
132 MIB_IPFORWARD_ROW2
*route
;
134 ret
= GetIpForwardTable2 (AF_UNSPEC
, &routes
);
135 if (ret
!= ERROR_SUCCESS
)
137 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_FAILED
,
138 "GetIpForwardTable2 () failed: %ld", ret
);
143 networks
= g_ptr_array_new_full (routes
->NumEntries
, g_object_unref
);
144 for (i
= 0; i
< routes
->NumEntries
; i
++)
146 GInetAddressMask
*network
;
149 GSocketFamily family
;
151 route
= routes
->Table
+ i
;
153 if (!win_network_monitor_get_ip_info (route
->DestinationPrefix
, &family
, &dest
, &len
))
156 network
= get_network_mask (family
, dest
, len
);
160 g_ptr_array_add (networks
, network
);
163 g_network_monitor_base_set_networks (G_NETWORK_MONITOR_BASE (win
),
164 (GInetAddressMask
**) networks
->pdata
,
171 add_network (GWin32NetworkMonitor
*win
,
172 GSocketFamily family
,
176 GInetAddressMask
*network
;
178 network
= get_network_mask (family
, dest
, dest_len
);
181 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (win
), network
);
182 g_object_unref (network
);
187 remove_network (GWin32NetworkMonitor
*win
,
188 GSocketFamily family
,
192 GInetAddressMask
*network
;
194 network
= get_network_mask (family
, dest
, dest_len
);
197 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (win
), network
);
198 g_object_unref (network
);
203 PMIB_IPFORWARD_ROW2 route
;
204 MIB_NOTIFICATION_TYPE type
;
205 GWin32NetworkMonitor
*win
;
209 win_network_monitor_invoke_route_changed (gpointer user_data
)
211 GSocketFamily family
;
212 RouteData
*route_data
= user_data
;
216 switch (route_data
->type
)
218 case MibDeleteInstance
:
219 if (!win_network_monitor_get_ip_info (route_data
->route
->DestinationPrefix
, &family
, &dest
, &len
))
222 remove_network (route_data
->win
, family
, dest
, len
);
225 if (!win_network_monitor_get_ip_info (route_data
->route
->DestinationPrefix
, &family
, &dest
, &len
))
228 add_network (route_data
->win
, family
, dest
, len
);
230 case MibInitialNotification
:
235 return G_SOURCE_REMOVE
;
239 win_network_monitor_route_changed_cb (PVOID context
,
240 PMIB_IPFORWARD_ROW2 route
,
241 MIB_NOTIFICATION_TYPE type
)
243 GWin32NetworkMonitor
*win
= context
;
244 RouteData
*route_data
;
246 route_data
= g_new0 (RouteData
, 1);
247 route_data
->route
= route
;
248 route_data
->type
= type
;
249 route_data
->win
= win
;
251 win
->priv
->route_change_source
= g_idle_source_new ();
252 g_source_set_priority (win
->priv
->route_change_source
, G_PRIORITY_DEFAULT
);
253 g_source_set_callback (win
->priv
->route_change_source
,
254 win_network_monitor_invoke_route_changed
,
258 g_source_attach (win
->priv
->route_change_source
, win
->priv
->main_context
);
262 g_win32_network_monitor_initable_init (GInitable
*initable
,
263 GCancellable
*cancellable
,
266 GWin32NetworkMonitor
*win
= G_WIN32_NETWORK_MONITOR (initable
);
270 if (!win
->priv
->initialized
)
272 win
->priv
->main_context
= g_main_context_ref_thread_default ();
274 /* Read current IP routing table. */
275 read
= win_network_monitor_process_table (win
, &win
->priv
->init_error
);
278 /* Register for IPv4 and IPv6 route updates. */
279 status
= NotifyRouteChange2 (AF_UNSPEC
, (PIPFORWARD_CHANGE_CALLBACK
) win_network_monitor_route_changed_cb
, win
, FALSE
, &win
->priv
->handle
);
280 if (status
!= NO_ERROR
)
281 g_set_error (&win
->priv
->init_error
, G_IO_ERROR
, G_IO_ERROR_FAILED
,
282 "NotifyRouteChange2() error: %ld", status
);
285 win
->priv
->initialized
= TRUE
;
288 /* Forward the results. */
289 if (win
->priv
->init_error
!= NULL
)
291 g_propagate_error (error
, g_error_copy (win
->priv
->init_error
));
295 return initable_parent_iface
->init (initable
, cancellable
, error
);
299 g_win32_network_monitor_finalize (GObject
*object
)
301 GWin32NetworkMonitor
*win
= G_WIN32_NETWORK_MONITOR (object
);
303 /* Cancel notification event */
304 if (win
->priv
->handle
)
305 CancelMibChangeNotify2 (win
->priv
->handle
);
307 g_clear_error (&win
->priv
->init_error
);
309 if (win
->priv
->route_change_source
!= NULL
)
311 g_source_destroy (win
->priv
->route_change_source
);
312 g_source_unref (win
->priv
->route_change_source
);
315 g_main_context_unref (win
->priv
->main_context
);
317 G_OBJECT_CLASS (g_win32_network_monitor_parent_class
)->finalize (object
);
321 g_win32_network_monitor_class_init (GWin32NetworkMonitorClass
*win_class
)
323 GObjectClass
*gobject_class
= G_OBJECT_CLASS (win_class
);
325 gobject_class
->finalize
= g_win32_network_monitor_finalize
;
329 g_win32_network_monitor_iface_init (GNetworkMonitorInterface
*monitor_iface
)
334 g_win32_network_monitor_initable_iface_init (GInitableIface
*iface
)
336 initable_parent_iface
= g_type_interface_peek_parent (iface
);
338 iface
->init
= g_win32_network_monitor_initable_init
;