Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2021-06

How to check if a network is available?

12 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bo Berglund

I would like to know how I can check if a remote network is available, i.e. if the VPN system has succeeded to connect the remote network.

I need this in a class that connects an OpenVPN tunnel on demand and takes it down after use. Unfortunately openvpn-gui does not have an API call to do this...
It provides an API for connect, disconnect, reconnect etc but not for returning the state of a connection for example.
https://github.com/OpenVPN/openvpn-gui#send-commands-to-a-running-instance-of-openvpn-gui

Any suggestions for Windows?
I just want to know if a call to connect succeeded.

Bo Berglund Developer in Sweden

Reply by email

Jean SUZINEAU
to Bo Berglund: I would like to know how I can check if a remote network is available, i.e. if the VPN…

May be you can get some information with the "route" command .

If you run "route print" in cmd command prompt, you can get information on the different networks available.

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/route_ws2008

(I couldn't find the doc for Windows 10, but "route print" works on Windows 10 Pro)

May be you can run the command "route print 0.0.0.0 " with TProcess and parse the output ?

Reply by email

Tony Whyman
to James Richters: Do a Ping to an address on the network to see if it's connected?

Yep, that's the golden rule in networking. The only way that you know that a bi-directional path is open is an end-to-end ping. Even then, you only know that it's open at the time the ping completed.

If you are using TCP then you can always enable keepalive packets that effectively do the same thing while the TCP connection is open. The IPsec Internet Key Exchange Protocol also has the same capability for Dead Peer Detection (DPD) which works between the two end points of a VPN tunnel.

James Richters wrote 17 lines
Do a Ping to an address on the network to see if it's connected? _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal I would like to know how I can check if a remote network is available, i.e. if the VPN system has succeeded to connect the remote network. I need this in a class that connects an OpenVPN tunnel on demand and takes it down after use. Unfortunately openvpn-gui does not have an API call to do this... It provides an API for connect, disconnect, reconnect etc but not for returning the state of a connection for example. https://github.com/OpenVPN/openvpn-gui#send-commands-to-a-running-instance-of-openvpn-gui Any suggestions for Windows? I just want to know if a call to connect succeeded.

Reply by email

Bo Berglund
to Jean SUZINEAU: May be you can get some information with the "route" command . If you run "route print"…
Jean SUZINEAU wrote 13 lines
May be you can get some information with the "route" command . If you run "route print" in cmd command prompt, you can get information on the different networks available. https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/route_ws2008 (I couldn't find the doc for Windows 10, but "route print" works on Windows 10 Pro) May be you can run the command "route print 0.0.0.0 " with TProcess and parse the output ?

On command line (grep is available from a delphi):

C:\>route print | grep 10.117
File STDIN:
10.117.3.0 255.255.255.0 On-link 10.117.3.2 281 10.117.3.2 255.255.255.255 On-link 10.117.3.2 281 10.117.3.255 255.255.255.255 On-link 10.117.3.2 281 224.0.0.0 240.0.0.0 On-link 10.117.3.2 281 255.255.255.255 255.255.255.255 On-link 10.117.3.2 281

If I know beforehand which will be the IP address of the VPN network I could use this...

Bo Berglund Developer in Sweden

Reply by email

Jean SUZINEAU
to Bo Berglund

I've never used open vpn, may be the 10.117 is defined in your configuration file ?

For the vpns I've used, the ip adressed were fixed.

( for route, you can type too something like "route print 10.117.*" )

Reply by email

Bo Berglund
to Jean SUZINEAU
Jean SUZINEAU wrote
I've never used open vpn, may be the 10.117 is defined in your configuration file ? For the vpns I've used, the ip adressed were fixed.

The tunnel IP address is defined server side and the route is pushed to the client on connection. A common VPN address in all tutorials starts with 10.8.0 but this is too common so I have set ours as 10.117.xx

( for route, you can type too something like "route print 10.117.*" )

C:\>route print 10.117.*
=========================================================================== Interface List
9...c4 65 16 9d 7e 8e ......Intel(R) Ethernet Connection (7) I219-LM 16...98 fc 84 ed a3 ea ......Realtek USB GbE Family Controller 7...00 ff 2d dd 8d be ......TAP-Windows Adapter V9 21...00 ff 94 a3 1f d4 ......TAP-Windows Adapter V9 #2 14...d0 c6 37 ea 58 3f ......Intel(R) Wireless-AC 9560 160MHz 26...d0 c6 37 ea 58 40 ......Microsoft Wi-Fi Direct Virtual Adapter #3 11...d2 c6 37 ea 58 3f ......Microsoft Wi-Fi Direct Virtual Adapter #4 10...00 50 56 c0 00 01 ......VMware Virtual Ethernet Adapter for VMnet1 13...00 50 56 c0 00 08 ......VMware Virtual Ethernet Adapter for VMnet8 15...d0 c6 37 ea 58 43 ......Bluetooth Device (Personal Area Network) 1...........................Software Loopback Interface 1 ===========================================================================

IPv4 Route Table
=========================================================================== Active Routes:
Network Destination Netmask Gateway Interface Metric 10.117.3.0 255.255.255.0 On-link 10.117.3.2 281 10.117.3.2 255.255.255.255 On-link 10.117.3.2 281 10.117.3.255 255.255.255.255 On-link 10.117.3.2 281 =========================================================================== Persistent Routes:
None

IPv6 Route Table
=========================================================================== Active Routes:
None
Persistent Routes:
None

I will need to check this in my connector class, the address to look for has to be a config item in my application so it can be modified if need be without rebuilding the app.
Have to figure out how to:
- Retrieve the output of the TProcess execution
- Parse the output for the expected network address.

Bo Berglund Developer in Sweden

Reply by email

Jean SUZINEAU
to Bo Berglund

Le 18/06/2021 à 17:07, Bo Berglund via fpc-pascal a écrit :

Show quoted text 5 lines
I will need to check this in my connector class, the address to look for has to be a config item in my application so it can be modified if need be without rebuilding the app. Have to figure out how to: - Retrieve the output of the TProcess execution

There are many ways to cook TProcess ...
I think you'll find what you need at :

https://wiki.freepascal.org/Executing_External_Programs

- Parse the output for the expected network address.

My idea would be to store the output in a Stringlist as shown in the examples,  then with IndexOf you can locate the position of  lines like "IPv4 Route Table" and "Persistent Routes:" and make a more detailed parsing between the two.

Reply by email

Bo Berglund
to James Richters: Do a Ping to an address on the network to see if it's connected?
James Richters wrote
Do a Ping to an address on the network to see if it's connected?

I would need a ping with only one execution so I can put it into a loop and break after a successful ping or break after some timeout value.

Seems like the ping syntax differs between Linux and Windows for some strange reason...

On Windows something like this then (10.117.3.1 being the IP address inside the tunnel of the OVPN server itself):

When connected:
---------------
ping -n 1 -w 500 10.117.3.1

Pinging 10.117.3.1 with 32 bytes of data:
Reply from 10.117.3.1: bytes=32 time=2ms TTL=64

Ping statistics for 10.117.3.1:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss), Approximate round trip times in milli-seconds:
Minimum = 2ms, Maximum = 2ms, Average = 2ms

Before connection:
------------------
ping -n 1 -w 500 10.117.3.1

Pinging 10.117.3.1 with 32 bytes of data:
Request timed out.

Ping statistics for 10.117.3.1:
Packets: Sent = 1, Received = 0, Lost = 1 (100% loss)

So looking for % loss in the response would be a way. 0 means no connection yet 100 means we are connected.

Bo Berglund Developer in Sweden

Reply by email

Luca Olivetti
to Jean SUZINEAU

El 20/6/21 a les 19:38, Jean SUZINEAU via fpc-pascal ha escrit:

Show quoted text 6 lines
Ping uses ICMP protocol. I don't have currently Indy installed on my machines, but I think you can find pascal components for ICMP client and even ICMP server. I think that this way you can do a single "ping", a single ICMP request. It seems that Synapse has ping  support too: https://wiki.freepascal.org/Networking_libraries

Beware though: under raw socket access (needed for ICMP) is a privileged operation.
In older systems the ping executable was setuid root, nowadays it has the cap_net_raw capability.

Bye

Luca

Reply by email

Marc Weustink
to Bo Berglund: I would like to know how I can check if a remote network is available, i.e. if the VPN…
Bo Berglund wrote 12 lines
I would like to know how I can check if a remote network is available, i.e. if the VPN system has succeeded to connect the remote network. I need this in a class that connects an OpenVPN tunnel on demand and takes it down after use. Unfortunately openvpn-gui does not have an API call to do this... It provides an API for connect, disconnect, reconnect etc but not for returning the state of a connection for example. https://github.com/OpenVPN/openvpn-gui#send-commands-to-a-running-instance-of-openvpn-gui Any suggestions for Windows? I just want to know if a call to connect succeeded.

You can use the management interface
https://openvpn.net/community-resources/management-interface/

But since we are developers you can lookup this info with the windows function GetIpAddrTable

https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getipaddrtable

Marc

Reply by email