David A
David A

Reputation: 641

Detect a VPN connection programmatically (c++)

OK, here's the deal. I am working on a program the needs to determine if the user is connected to a particular LAN via a VPN connection or directly. If it is connected to the LAN, it needs to do something. If it is "offline" (meaning it is not connected to the LAN, but could be connected to the Internet), it needs to do something else. Any idea on how to go about doing this in c++? Is there an API call to check whether a network connection is a VPN connection? I am looking for something like "IsVPNConnection(...)".

EDIT

The OS I'm using is Windows 7. I'm looking for a solution that would be independent on the VPN type.

Upvotes: 0

Views: 5605

Answers (4)

Steffen
Steffen

Reputation: 1

If what you want to do is mostly concerned about speed you could time a ping. LANs usually respond in very few MS, whereas a VPN will probably be in at least a few ten ms.

Upvotes: 0

Robb Sadler
Robb Sadler

Reputation: 735

I was trying to determine the same thing and this is the closest I got.

We use juniper networks Network Connect and I have seen that it will shut itself down when I plug into the network directly. So if it's running there a good chance that you are not directly connected. If it is not running, then you are either directly connected, or not working with your internal servers. So using this approach to get the process list and check for your VPN process, in conjunction with the above approach to ping servers that would only be available inside the corporate network may give you solid results. I used this code to test for my VPN process. It is CLR, not sure if you would use .Net resources...

#include "stdafx.h"

#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;

int main() {
    System::String ^procName = "dsNetworkConnect";

    array<System::Diagnostics::Process^>^matches =
        System::Diagnostics::Process::GetProcessesByName(procName);

    if (matches->Length > 0)
        System::Console::WriteLine("Process " + matches[0]->ProcessName + 
           " found - do something");
    else
        System::Console::WriteLine("Process not found, do something else.");
    System::Console::ReadKey();
}

Upvotes: 0

brian beuning
brian beuning

Reputation: 2862

VPN tries to be transparent so this will be tricky.

Most machines only have one route to other machines. With VPN there may be two routes. So you might check the routing table. Some VPN clients disable the local network, so then you will be back to just one route.

If you can tell your program the IP addresses on the local subnet, then it is easy.

Upvotes: 1

syam
syam

Reputation: 15069

Depending on where you are connected (LAN / VPN) you will have access to different network resources (servers). My best bet would be to ping (ICMP echo) those servers or otherwise try to access the services they host, and guess which network you're connected to according to success/failure. How to do that is entirely dependent on your OS, as @PaulR mentioned.

Any other solution involving the enumeration of network connections, routing tables etc is very likely to be much more complicated and error prone so I'd avoid that altogether and go with the simple services detection I mentioned.

Upvotes: 1

Related Questions