none
How to determine if IPHostEntry address is a VPN address? RRS feed

  • Question

  • On my PC there is a Cisco VPN client. When I run Dns.GetHostEntry the resulting AddressList of the returned IPHostEntry contains both the IP of the PC and the IP of the VPN client. I need to get the IP address of the PC so that I can use sockets to connect to another system. How can I reliably determine which address returned by Dns.GetHostEntry is the address of the PC?

    thanks,

        public static IPAddress GetLocalIpAddress()
        {
          IPAddress localAddr = null;
          string hostName = Dns.GetHostName();
          IPHostEntry he = Dns.GetHostEntry(hostName);
    
          IPAddress[] addrs = he.AddressList;
          if (addrs.Length == 0)
            throw new ApplicationException(
              "local system " + hostName + " does not have an IP address");
    
          // get the ip4 address from the address list 
          foreach (IPAddress addr in addrs)
          {
            if (addr.AddressFamily == AddressFamily.InterNetwork)
            {
              if (addr.ToString().IndexOf("192.168") != -1)
              {
                localAddr = addr;
                break;
              }
            }
          }
    
          return localAddr;
        }
    

    Sunday, June 8, 2014 6:24 PM

Answers

  • Hi,

    If you know the name of the VPN connection, maybe you can try the following code and in the following code the "VPNConnection" is the VPN connection name:

    using System.Linq;
    
    var vpn = NetworkInterface.GetAllNetworkInterfaces()
                              .First(x => x.Name == "VPNConnection");
    var ip = vpn.GetIPProperties().UnicastAddresses.First(x => x.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).Address.ToString();

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 9, 2014 7:23 AM
    Moderator
  • if (NetworkInterface.GetIsNetworkAvailable())
                {
                    NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
                    foreach (NetworkInterface Interface in interfaces)
                    {
                        if (Interface.OperationalStatus == OperationalStatus.Up)
                        {
                            if ((Interface.NetworkInterfaceType == NetworkInterfaceType.Ppp) && (Interface.NetworkInterfaceType != NetworkInterfaceType.Loopback))
                            {
                                IPv4InterfaceStatistics statistics = Interface.GetIPv4Statistics();
                                Console.WriteLine(Interface.Name + " " + Interface.NetworkInterfaceType.ToString() + " " + Interface.Description);
                            }
                            else
                            {
                                Console.WriteLine("VPN Connection is lost!");
                            }
     
                        }
                    }
                }

    Hi,

    You can use the abovepiece of code to identify the list of vpn connection


    Regards, Sakthi Ganesh

    Monday, June 9, 2014 11:02 AM

All replies

  • Hi,

    If you know the name of the VPN connection, maybe you can try the following code and in the following code the "VPNConnection" is the VPN connection name:

    using System.Linq;
    
    var vpn = NetworkInterface.GetAllNetworkInterfaces()
                              .First(x => x.Name == "VPNConnection");
    var ip = vpn.GetIPProperties().UnicastAddresses.First(x => x.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).Address.ToString();

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 9, 2014 7:23 AM
    Moderator
  • if (NetworkInterface.GetIsNetworkAvailable())
                {
                    NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
                    foreach (NetworkInterface Interface in interfaces)
                    {
                        if (Interface.OperationalStatus == OperationalStatus.Up)
                        {
                            if ((Interface.NetworkInterfaceType == NetworkInterfaceType.Ppp) && (Interface.NetworkInterfaceType != NetworkInterfaceType.Loopback))
                            {
                                IPv4InterfaceStatistics statistics = Interface.GetIPv4Statistics();
                                Console.WriteLine(Interface.Name + " " + Interface.NetworkInterfaceType.ToString() + " " + Interface.Description);
                            }
                            else
                            {
                                Console.WriteLine("VPN Connection is lost!");
                            }
     
                        }
                    }
                }

    Hi,

    You can use the abovepiece of code to identify the list of vpn connection


    Regards, Sakthi Ganesh

    Monday, June 9, 2014 11:02 AM