This sub-module can be used to determine the reachability of a target host. A remote host is considered reachable when a data packet, sent by an application into the network stack, can leave the local device. Reachability does not guarantee that the data packet will actually be received by the host.
It is important to remember that this module works by determining if the computer has a route for network traffic bound to a specific destination. An active internet connection provides a default route for any network that the host is not a member of, so care must be used when testing for specific VPN or local networks to avoid false positives. Some examples follow:
This is a simple watcher which will be invoked whenever the computer's active internet connection changes state:
hs.network.reachability.internet():setCallback(function(self, flags)
if (flags & hs.network.reachability.flags.reachable) > 0 then
-- a default route exists, so an active internet connection is present
else
-- no default route exists, so no active internet connection is present
end
end):start()
Note that when an active internet connection is up (reachable), any specific network test that does not include an address pair will be reachable, since internet reachability is defined as having a default route for all non-local networks.
A specific test for determining if an OpenVPN network is available. This example requires knowing what the local computer's IP address on the VPN network is (OpenVPN does not set the isDirect
flag) and has been tested with Tunnelblick.
hs.network.reachability.forAddress("10.x.y.z"):setCallback(function(self, flags)
-- note that because having an internet connection at all will show the remote network
-- as "reachable", we instead look at whether or not our specific address is "local" instead
if (flags & hs.network.reachability.flags.isLocalAddress) > 0 then
-- VPN tunnel is up
else
-- VPN tunnel is down
end
end):start()
Signature | hs.network.reachability.flags[] |
---|---|
Type | Constant |
Description | A table containing the numeric value for the possible flags returned by the hs.network.reachability:status method or in the |
Source | extensions/network/libnetwork_reachability.m line 328 |
Signature | hs.network.reachability.forAddress(address) -> reachabilityObject |
---|---|
Type | Constructor |
Description | Returns a reachability object for the specified network address. |
Parameters |
|
Returns |
|
Notes |
|
Source | extensions/network/libnetwork_reachability.m line 74 |
Signature | hs.network.reachability.forAddressPair(localAddress, remoteAddress) -> reachabilityObject |
---|---|
Type | Constructor |
Description | Returns a reachability object for the specified network address from the specified localAddress. |
Parameters |
|
Returns |
|
Notes |
|
Source | extensions/network/libnetwork_reachability.m line 105 |
Signature | hs.network.reachability.forHostName(hostName) -> reachabilityObject |
---|---|
Type | Constructor |
Description | Returns a reachability object for the specified host. |
Parameters |
|
Returns |
|
Notes |
|
Source | extensions/network/libnetwork_reachability.m line 150 |
Signature | hs.network.reachability.internet() -> reachabilityObject |
---|---|
Type | Constructor |
Description | Creates a reachability object for testing internet access |
Parameters |
|
Returns |
|
Notes |
|
Source | extensions/network/network_reachability.lua line 45 |
Signature | hs.network.reachability.linkLocal() -> reachabilityObject |
---|---|
Type | Constructor |
Description | Creates a reachability object for testing IPv4 link local networking |
Parameters |
|
Returns |
|
Notes |
|
Source | extensions/network/network_reachability.lua line 63 |
Signature | hs.network.reachability:setCallback(function) -> reachabilityObject |
---|---|
Type | Method |
Description | Set or remove the callback function for a reachability object |
Parameters |
|
Returns |
|
Notes |
|
Source | extensions/network/libnetwork_reachability.m line 237 |
Signature | hs.network.reachability:start() -> reachabilityObject |
---|---|
Type | Method |
Description | Starts watching the reachability object for changes and invokes the callback function (if any) when a change occurs. |
Parameters |
|
Returns |
|
Notes |
|
Source | extensions/network/libnetwork_reachability.m line 272 |
Signature | hs.network.reachability:status() -> number |
---|---|
Type | Method |
Description | Returns the reachability status for the object |
Parameters |
|
Returns |
|
Notes |
|
Source | extensions/network/libnetwork_reachability.m line 176 |
Signature | hs.network.reachability:statusString() -> string |
---|---|
Type | Method |
Description | Returns a string representation of the reachability status for the object |
Parameters |
|
Returns |
|
Notes |
|
Source | extensions/network/libnetwork_reachability.m line 202 |
Signature | hs.network.reachability:stop() -> reachabilityObject |
---|---|
Type | Method |
Description | Stops watching the reachability object for changes. |
Parameters |
|
Returns |
|
Source | extensions/network/libnetwork_reachability.m line 306 |