0

I managed to get all the NetworkInterface's in the computer, then I reject the ipv6, the loopback and then I end up having 2 interfaces, one is the real Ethernet I want to use and one is the VPN I have open to work with some remote servers that I don't want to use.

Is there any way in C# to identify them? Both answer "InterNetwork" for the AddressFamily and both say "Ethernet" for the NetworkingInterfaceType

Any clue ?

List<IPAddress> localIPs = new List<IPAddress>();

var ifaces = NetworkInterface.GetAllNetworkInterfaces();

//I'm working on unity and it does not allow me to do .ToList().. for some reason.
List<NetworkInterface> IfacesList = new List<NetworkInterface>();
foreach (var i in ifaces)
   IfacesList.Add(i);

var selected = IfacesList.FindAll(x => x.NetworkInterfaceType == NetworkInterfaceType.Ethernet);

if (selected.Count == 1)
{
    var addresses = selected[0].GetIPProperties().UnicastAddresses;

    //once again, cannot use .toList()
    List<UnicastIPAddressInformation> addressesList = new List<UnicastIPAddressInformation>();
    foreach (var address in addresses)
        addressesList.Add(address);

    var ip =  addressesList.Find(x => x.Address.AddressFamily == AddressFamily.InterNetwork).Address;
    return new IPEndPoint(ip, port);
}
else
{
    //HERE I need to differentiate my IP from VPN IP
}

those are the two interfaces I end up having:

enter image description here

javirs
  • 1,049
  • 26
  • 52

2 Answers2

4

You could find the interfaces which are attached to the PCI bus and go from there. Here's some code which only returns interfaces which are on the PCI bus.

using System.Collections.Generic;
using System.Linq;
using System.Management; // Add a reference to System.Management for the project
using System.Net.NetworkInformation;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            HashSet<string> physicalMacAddresses = new HashSet<string>(GetMacAddressesOnPciBus());

            var ifaces = NetworkInterface.GetAllNetworkInterfaces();
            var physicalAddresses = ifaces
                .Where(x => x.NetworkInterfaceType == NetworkInterfaceType.Ethernet
                && physicalMacAddresses.Contains(x.GetPhysicalAddress().ToString().Trim('{').Trim('}')));

        }

        private static IEnumerable<string> GetMacAddressesOnPciBus()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher
    ("Select MACAddress,PNPDeviceID FROM Win32_NetworkAdapter WHERE MACAddress IS NOT NULL AND PNPDeviceID IS NOT NULL");
            ManagementObjectCollection mObject = searcher.Get();

            foreach (ManagementObject obj in mObject)
            {
                string pnp = obj["PNPDeviceID"].ToString();
                if (pnp.Contains("PCI\\"))
                {
                    string mac = obj["MACAddress"].ToString();
                    mac = mac.Replace(":", string.Empty);
                    yield return mac;
                }
            }
        }
    }
}

The technique for finding the MAC addresses is from here how-to-determine-mac-address-of-the-actual-physical-network-card-not-virtual.

Community
  • 1
  • 1
Daniel James Bryars
  • 4,429
  • 3
  • 39
  • 57
  • Hi @DanielJamesBryars I need to make it work on Unity, who refuses to access ManagementObjectSearcher even when I added the dll to the Assets (because Unity run with .NETframework lower than Linq) Can you point me to a lower level access ? – javirs Oct 10 '16 at 06:11
  • Hi Jarvis, I'm not familiar with Unity - this sounds like an excuse for me to do so! What version of unity are you using, can you post a link and I'll take a look. – Daniel James Bryars Oct 10 '16 at 08:28
  • last version out there, 5.4.1 I think, there problem here is Mono acepting just some of the .NET framework, no Linq there :( – javirs Oct 10 '16 at 11:36
  • @jarvis. The technique I described calls into a Windows specific technology called WMI. WMI is an instrumentation layer which lets you query details about the Windows operating system. I needed to reach "outside" because the available Network Interface functions abstracts away these details. On Mono you'd need some platform specific way to use my "is attached to the PCI bus technique". However, another approach might be to findd a network card which has a gateway attached to it - try iface.GetIPProperties().GatewayAddresses - an iFace with a Gateway might be the one you want. – Daniel James Bryars Oct 11 '16 at 20:46
  • @javirs - just realised I spelt your name incorrectly sorry. – Daniel James Bryars Oct 11 '16 at 20:52
0
List<IPAddress> localIPs = new List<IPAddress>();

var ifaces = NetworkInterface.GetAllNetworkInterfaces();

//I'm working on unity and it does not allow me to do .ToList().. for some reason.
List<NetworkInterface> IfacesList = new List<NetworkInterface>();
foreach (var i in ifaces)
    IfacesList.Add(i);

var selected = IfacesList.FindAll(x => x.NetworkInterfaceType == NetworkInterfaceType.Ethernet);

if (selected.Count == 1)
{
    var addresses = selected[0].GetIPProperties().UnicastAddresses;

    //once again, cannot use .toList()
    List<UnicastIPAddressInformation> addressesList = new List<UnicastIPAddressInformation>();
    foreach (var address in addresses)
        addressesList.Add(address);

    var ip = addressesList.Find(x => x.Address.AddressFamily == AddressFamily.InterNetwork).Address;
    //return new IPEndPoint(ip, 1);
}
else
{
    //HERE I need to differentiate my IP from VPN IP
    var addresses = selected[1].GetIPProperties().UnicastAddresses;
    IPAddress ip = addresses[0].Address;
    var ipAddress = ip.ToString();


}
Simon Price
  • 3,011
  • 3
  • 34
  • 98
  • how do you knwo that selected[1] will be the ethernet and not the VPN? do we know the sort method for the retrieved list ? – javirs Sep 28 '16 at 14:58
  • i'll answer that when I get home from work in an hour, then I can fire up my laptop and get a vpn running and give you the answer – Simon Price Sep 28 '16 at 14:59
  • any news ?? I did not find a solution yet :( – javirs Sep 30 '16 at 08:23
  • in all honesty I forgot, but I do have my personal machine with me at work today. I will have a try now – Simon Price Sep 30 '16 at 09:21
  • in order to replicate what youre after ill have to try it tonight sorry. youre looking for wired results and im only on wifi where I am – Simon Price Sep 30 '16 at 09:32
  • once again, thank you SO much :) I will keep on trying and keep you updated if I find a solution :) – javirs Sep 30 '16 at 12:21