Detect Windows Network Type - winapi

Does anyone know if there is any convinient Windows API function that can be used to determine the active Network Type. By network type I mean:
Home
Work
Public
I would like to be able to inform the users of my software if they are on a public network, as it may cause connection issues in my software.

To determine if you are on a public network, you can check for the NLM_NETWORK_CATEGORY_PUBLIC value (NLM_NETWORK_CATEGORY enumeration) using the INetwork interface (which is used to determine the properties of a network) and the INetwork::GetCategory method.
For more info about this topic try the Windows 7 Network Location doc.

Related

How to get the underlying network interface used by a VPN connection in Windows

The closest thing to a solution I've found is using Get-NetConnectionProfile to return all active interfaces, which works fine when there's only an active physical interface and the VPN itself. However, this would not work if the user's machine has 2 active physical interfaces (e.g Wi-Fi + Ethernet) along with the VPN.
Ideally, I'd like a solution that works similarly to "ifconfig -v" in MacOS, which tells you the effective interface for a virtual interface:
Unfortunately it seems there is no sure-fire way to get the underlying physical adapter for a VPN using a Windows API. Short of involving a packet sniffer such as Wireshark, the best solution I found involves parsing the output of two PowerShell commands: Get-NetAdapter and Get-NetRoute.
With the information from these commands, I can know which interfaces are virtual and which ones are physical, and I can rank the physical interfaces by 3 different criteria (in case of tie, we move on to the next criteria):
Sorting the physical interfaces by the interface metric + the route metric to the default gateway (0.0.0.0).
Wired connections over wireless ones (PhysicalMediaType=802.3).
Prioritizing faster adapters.
With this logic all the VPNs I tested appear to reflect the expected network interface, although some VPNs let you force traffic through a particular physical adapter in which case obviously this all goes out the window.
First,
You can install wireshark or some other traffic monitoring tool and capture the relevant packets (filtering using openvVPN protocol or port etc.)
Second,
As far as I know there is no hard linking between the virtual network interface and the regular (ethernet, WIFI etc.) interface, at least not in OpenVPN (there are diffrent VPN protocols). The openVPN packets will be routed to the remote server using you OS routing table.
This way if your ethernet interface is your primary default gateway, and it gets unplugged, your VPN service will be able to recover, since it will have a route to your remote VPN-server address using your WIFI interface.

Can i use two networks at the same time?

I am developing a program that makes a request to a private IP address, to fetch data, which I can access only by being connected to that network.
And I have a database that is behind a vpn (other private IP), and I need to send that previous data to that database.
My computer is windows 10 and has two network cards, I can successfully connect to each network independently.
But the question is how can I make my computer stay connected to both networks at the same time and the described process works.
I appreciate if anyone can provide me with information on whether this is possible at the operating system configuration level. Or should it be something software development as such?

How to detect Network type on Win Mobile

I spend lot of time searching how to determine the current network type such as WIFI, 3G, Ethernet.
But I cannot find any information online in windows platform.
I want to detect the Ethernet network type, but I cannot find any API that can retrieve the network type.
I've find Connection Manager for keyword but there's less information.
Did anyone have any idea?
Please help me...I was blocked by this for 2 weeks.
Thanks.
Look in the registry key HKLM\system\state\connections, there is are entries for each of the possible network types, for instance:
When connected to a wireless network:
[HKLM\system\state\Connections\Network]
Adapters="BCMSDDHD1"
Count=dword:00000001
Descriptions="Business World"
when connected to activesync:
[HKLM\system\state\Connections\Desktop]
Adapters=""
Count=dword:00000001
Descriptions="CurrentDTPTNetwork"
The Count value under HKLM\system\state\connections gives you the total number of active connections.
In HKLM\system\state\Hardware you can find the current power state of the various devices.
There is an API on Windows Mobile that makes it easier to access these values vs. going directly to the registry. See the SystemState class documentation, specifically the ConnectionsNetworkCount and ConnectionsCount properties.

Network control on Windows

How do I control the network interface on Windows? Actually, the idea is quiet simple.
Establish connect to a certain adapter on a computer.
Direct whole traffic of the adapter to this program.
Give the rights to this program what must be transfer to the adapter and what should not.
I think the program has to implement any windows network adapter interface and be registered as an adapter driver. Thus, when it wants to transfer data to real adapter, the program has to call adapter driver methods. In their turn, the methods implement the same windows network interface (or that is called in other way don't really know, I hope you get the sense), don't they?
As a result, we have a kind of inheritance here.
Before use our new driver:
windows application and windows itself
________________________ windows network interface
DRIVER of Adapter1
________________________ end of OS boundaries
Adapter 1
Use our new driver:
windows application and windows itself
________________________ windows network interface
our DRIVER
________________________ windows network interface
DRIVER of Adapter1
________________________ end of OS boundaries
Adapter 1
I believe that kind of interface exists and I hope it is realized very easy.
You will likely need to hook Windows API calls to really do this. It is possible... Start by Googling hook API and Winternals - and see if you can find their sample code.
Before Winternals was bought by Microsoft they published their code. http://technet.microsoft.com/en-us/sysinternals/bb545021
The idea is this:
You write a user-mode program which controls your hooking device
driver.
Your hooking device driver determines if network traffic on
given API calls is ok.
Driver allows normal API calls to succeed if they are.

How do you determine if an interface is a physical LAN port on a Windows PC?

I'm looking for a method to acurately determine if an interface is the physical 802.3 ethernet port on a pc in windows.
Using ipconfig /all I can list all the interfaces, and when I do this on my pc several entries can be listed here including VPN, Bluetooth, Wifi and the physical ethernet interface.
I'm looking for something like, "isPhysical(interface)".
(It's ok to have multiple physical lan ports, I just want to know if it is a physical port or not).
UPDATE:
Jay and Chris thanks!
(Not enough space in the comments so I'll post here)
I'm currently looking at using WMI Win32_NetworkAdapter.
However, it shows 4 interfaces with AdapterType="Ethernet 802.3".
I only have 1 phyical port on my pc the others have the Name, "Virtual Machine Network Services Driver". (I assume these are installed by my company for some nefarious reason)
I found the attribute I need in the interface, but it's not available in winXP!!!
PhysicalAdapter Data type: boolean
Access type: Read-only
Indicates whether the adapter is a
physical or a logical adapter. If
True, the adapter is physical. Windows
Server 2003, Windows XP, Windows 2000,
and Windows NT 4.0: This property is
not available.
Is there any way to determine if it's a physical port, other than doing a text filter for the word "Virtual" in the name?
UPDATE 12/08
Looks like the virtual interfaces are added when you have a VM installed.
Here's some details about the virtual adapter:
http://blogs.msdn.com/virtual_pc_guy/archive/2005/04/01/404816.aspx
Found an issue where if the user doesn't have admin rights the WMI interface doesn't return the data needed. So, now looking at the GetAdapterInfo method. However, it seems to add 'virtual machine services driver" to the actual adapter's description, making the 'virtual' text check invalid.
And the structure returned by GetAdapterInfo:
http://msdn.microsoft.com/en-us/library/aa366062(VS.85).aspx
Call GetIfEntry and look for a dwType of IF_TYPE_ETHERNET_CSMACD in the MIB_IFROW structure returned.
Or in .NET, look for a System.Net.NetworkInformation.NetworkInterface with a NetworkInterfaceType of Ethernet.
The best way I've seen so far with any system Windows 2000 SP2 and up is using WMI.
http://msdn.microsoft.com/en-us/library/aa394582(VS.85).aspx
Win32_NetworkAdapter
http://msdn.microsoft.com/en-us/library/aa394216(VS.85).aspx
And you can use the namespace System.Net.NetworkInformation Jay mentioned. Here is a link about detecting what network cards are connected or disconnected:
http://felizk.dk/?p=43
If a machine has two NICs, how will you pick which one is the right one? The only way I've seen this done is giving the user a dropdown combo with each interface's IP address. See Wireshark for an example -
If there are other solutions, I'd like to hear them -
Check interface via:
Call GetIfEntry and look for a dwType of IF_TYPE_ETHERNET_CSMACD in the MIB_IFROW structure returned.
Or in .NET, look for a System.Net.NetworkInformation.NetworkInterface with a NetworkInterfaceType of Ethernet.
And check if the name contains the text, "Virtual".
---This seems like a lame way to check, but so far it's the only method I see to ignore those virtual 802.3 adapters.
I'm not sure of the answer - but be aware that it is possible to have multiple ethernet interfaces.

Resources