Description
GNetworkMonitor provides an easy-to-use cross-platform API
for monitoring network connectivity. On Linux, the available
implementations are based on the kernel's netlink interface and
on NetworkManager.
There is also an implementation for use inside Flatpak sandboxes.
Functions
g_network_monitor_get_network_available ()
gboolean
g_network_monitor_get_network_available
(GNetworkMonitor *monitor);
Checks if the network is available. "Available" here means that the
system has a default route available for at least one of IPv4 or
IPv6. It does not necessarily imply that the public Internet is
reachable. See “network-available” for more details.
Returns
whether the network is available
Since: 2.32
g_network_monitor_get_network_metered ()
gboolean
g_network_monitor_get_network_metered (GNetworkMonitor *monitor);
Checks if the network is metered.
See “network-metered” for more details.
Returns
whether the connection is metered
Since: 2.46
g_network_monitor_can_reach ()
gboolean
g_network_monitor_can_reach (GNetworkMonitor *monitor,
GSocketConnectable *connectable,
GCancellable *cancellable,
GError **error);
Attempts to determine whether or not the host pointed to by
connectable
can be reached, without actually trying to connect to
it.
This may return TRUE even when “network-available”
is FALSE, if, for example, monitor
can determine that
connectable
refers to a host on a local network.
If monitor
believes that an attempt to connect to connectable
will succeed, it will return TRUE. Otherwise, it will return
FALSE and set error
to an appropriate error (such as
G_IO_ERROR_HOST_UNREACHABLE).
Note that although this does not attempt to connect to
connectable
, it may still block for a brief period of time (eg,
trying to do multicast DNS on the local network), so if you do not
want to block, you should use g_network_monitor_can_reach_async().
Returns
TRUE if connectable
is reachable, FALSE if not.
Since: 2.32
Types and Values
GNetworkMonitor
typedef struct _GNetworkMonitor GNetworkMonitor;
GNetworkMonitor monitors the status of network connections and
indicates when a possibly-user-visible change has occurred.
Since: 2.32
struct GNetworkMonitorInterface
struct GNetworkMonitorInterface {
GTypeInterface g_iface;
void (*network_changed) (GNetworkMonitor *monitor,
gboolean network_available);
gboolean (*can_reach) (GNetworkMonitor *monitor,
GSocketConnectable *connectable,
GCancellable *cancellable,
GError **error);
void (*can_reach_async) (GNetworkMonitor *monitor,
GSocketConnectable *connectable,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
gboolean (*can_reach_finish) (GNetworkMonitor *monitor,
GAsyncResult *result,
GError **error);
};
The virtual function table for GNetworkMonitor.
Since: 2.32
G_NETWORK_MONITOR_EXTENSION_POINT_NAME
#define G_NETWORK_MONITOR_EXTENSION_POINT_NAME "gio-network-monitor"
Extension point for network status monitoring functionality.
See Extending GIO.
Since: 2.30
enum GNetworkConnectivity
The host's network connectivity state, as reported by GNetworkMonitor.
Since: 2.44
Property Details
The “network-available” property
“network-available” gboolean
Whether the network is considered available. That is, whether the
system has a default route for at least one of IPv4 or IPv6.
Real-world networks are of course much more complicated than
this; the machine may be connected to a wifi hotspot that
requires payment before allowing traffic through, or may be
connected to a functioning router that has lost its own upstream
connectivity. Some hosts might only be accessible when a VPN is
active. Other hosts might only be accessible when the VPN is
not active. Thus, it is best to use g_network_monitor_can_reach()
or g_network_monitor_can_reach_async() to test for reachability
on a host-by-host basis. (On the other hand, when the property is
FALSE, the application can reasonably expect that no remote
hosts at all are reachable, and should indicate this to the user
in its UI.)
See also “network-changed”.
Owner: GNetworkMonitor
Flags: Read
Default value: FALSE
Since: 2.32
The “network-metered” property
“network-metered” gboolean
Whether the network is considered metered. That is, whether the
system has traffic flowing through the default connection that is
subject to limitations set by service providers. For example, traffic
might be billed by the amount of data transmitted, or there might be a
quota on the amount of traffic per month. This is typical with tethered
connections (3G and 4G) and in such situations, bandwidth intensive
applications may wish to avoid network activity where possible if it will
cost the user money or use up their limited quota.
If more information is required about specific devices then the
system network management API should be used instead (for example,
NetworkManager or ConnMan).
If this information is not available then no networks will be
marked as metered.
See also “network-available”.
Owner: GNetworkMonitor
Flags: Read
Default value: FALSE
Since: 2.46