## GNetworkMonitor

GNetworkMonitor — Network status monitor

## Functions

GNetworkMonitor * | g_network_monitor_get_default ()
gboolean | g_network_monitor_get_network_available ()
gboolean | g_network_monitor_get_network_metered ()
gboolean | g_network_monitor_can_reach ()
void | g_network_monitor_can_reach_async ()
gboolean | g_network_monitor_can_reach_finish ()
GNetworkConnectivity | g_network_monitor_get_connectivity ()

## Properties

GNetworkConnectivity | connectivity | Read
gboolean | network-available | Read
gboolean | network-metered | Read

## Signals

void | network-changed | Run Last

## Types and Values

| GNetworkMonitor
struct | GNetworkMonitorInterface
#define | G_NETWORK_MONITOR_EXTENSION_POINT_NAME
enum | GNetworkConnectivity

## Object Hierarchy

        GEnum
        ╰── GNetworkConnectivity
        GInterface
        ╰── GNetworkMonitor

## Prerequisites

GNetworkMonitor requires GInitable and GObject.

## Includes

    #include <gio/gio.h>

## 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_default ()

    GNetworkMonitor *
    g_network_monitor_get_default (void);

Gets the default GNetworkMonitor for the system.

#### Returns

a GNetworkMonitor.

[transfer none]

Since: 2.32

### 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.

#### Parameters

monitor | the GNetworkMonitor |

#### 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.

#### Parameters

monitor | the GNetworkMonitor |

#### 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()`.

#### Parameters

monitor | a GNetworkMonitor |
connectable | a GSocketConnectable |
cancellable | a GCancellable, or `NULL`. | [nullable]
error | return location for a GError, or `NULL` |

#### Returns

`TRUE` if `connectable` is reachable, `FALSE` if not.

Since: 2.32

### g_network_monitor_can_reach_async ()

    void
    g_network_monitor_can_reach_async (GNetworkMonitor *monitor,
                                       GSocketConnectable *connectable,
                                       GCancellable *cancellable,
                                       GAsyncReadyCallback callback,
                                       gpointer user_data);

Asynchronously attempts to determine whether or not the host pointed to by `connectable` can be reached, without actually trying to connect to it.

For more details, see `g_network_monitor_can_reach()`.

When the operation is finished, `callback` will be called. You can then call `g_network_monitor_can_reach_finish()` to get the result of the operation.

#### Parameters

monitor | a GNetworkMonitor |
connectable | a GSocketConnectable |
cancellable | a GCancellable, or `NULL`. | [nullable]
callback | a GAsyncReadyCallback to call when the request is satisfied. | [scope async]
user_data | the data to pass to callback function. | [closure]

### g_network_monitor_can_reach_finish ()

    gboolean
    g_network_monitor_can_reach_finish (GNetworkMonitor *monitor,
                                        GAsyncResult *result,
                                        GError **error);

Finishes an async network connectivity test. See `g_network_monitor_can_reach_async()`.

#### Parameters

monitor | a GNetworkMonitor |
result | a GAsyncResult |
error | return location for errors, or `NULL` |

#### Returns

`TRUE` if network is reachable, `FALSE` if not.

### g_network_monitor_get_connectivity ()

    GNetworkConnectivity
    g_network_monitor_get_connectivity (GNetworkMonitor *monitor);

Gets a more detailed networking state than `g_network_monitor_get_network_available()`.

If “network-available” is `FALSE`, then the connectivity state will be `G_NETWORK_CONNECTIVITY_LOCAL`.

If “network-available” is `TRUE`, then the connectivity state will be `G_NETWORK_CONNECTIVITY_FULL` (if there is full Internet connectivity), `G_NETWORK_CONNECTIVITY_LIMITED` (if the host has a default route, but appears to be unable to actually reach the full Internet), or `G_NETWORK_CONNECTIVITY_PORTAL` (if the host is trapped behind a "captive portal" that requires some sort of login or acknowledgement before allowing full Internet access).

Note that in the case of `G_NETWORK_CONNECTIVITY_LIMITED` and `G_NETWORK_CONNECTIVITY_PORTAL`, it is possible that some sites are reachable but others are not. In this case, applications can attempt to connect to remote servers, but should gracefully fall back to their "offline" behavior if the connection attempt fails.

#### Parameters

monitor | the GNetworkMonitor |

#### Returns

the network connectivity state

Since: 2.44

## 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.

#### Members

`network_changed` () | the virtual function pointer for the GNetworkMonitor::network-changed signal. |
`can_reach` () | the virtual function pointer for `g_network_monitor_can_reach()` |
`can_reach_async` () | the virtual function pointer for `g_network_monitor_can_reach_async()` |
`can_reach_finish` () | the virtual function pointer for `g_network_monitor_can_reach_finish()` |

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.

#### Members

G_NETWORK_CONNECTIVITY_LOCAL | The host is not configured with a route to the Internet; it may or may not be connected to a local network. |
G_NETWORK_CONNECTIVITY_LIMITED | The host is connected to a network, but does not appear to be able to reach the full Internet, perhaps due to upstream network problems. |
G_NETWORK_CONNECTIVITY_PORTAL | The host is behind a captive portal and cannot reach the full Internet. |
G_NETWORK_CONNECTIVITY_FULL | The host is connected to a network, and appears to be able to reach the full Internet. |

Since: 2.44

## Property Details

### The `“connectivity”` property

      “connectivity”             GNetworkConnectivity

More detailed information about the host's network connectivity. See `g_network_monitor_get_connectivity()` and GNetworkConnectivity for more details.

Owner: GNetworkMonitor

Flags: Read

Default value: G_NETWORK_CONNECTIVITY_FULL

Since: 2.44

### 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

## Signal Details

### The `“network-changed”` signal

    void
    user_function (GNetworkMonitor *monitor,
                   gboolean         network_available,
                   gpointer         user_data)

Emitted when the network configuration changes.

#### Parameters

monitor | a GNetworkMonitor |
network_available | the current value of “network-available” |
user_data | user data set when the signal handler was connected. |

Flags: Run Last

Since: 2.32