3

I am using a WinPCapDevice and have already initialized it. I just want to be able to get the IP from that device and I cannot find anywhere how to extract the IP address of the Device. If there isnt a way to do it then is there another way to get the IP address of a WinPCapDevice so that I can check it against a list of IPAddresses?

Here is the small chunk of code that I am talking about.

        IPHostEntry host;
        host = Dns.GetHostEntry(Dns.GetHostName());

        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily.ToString() == "InterNetwork")
            {

                localIPAddress = ip.ToString();
                //Want to check if my WinPCapDevice device's IP is equal to ip
            }
        }
Brandon Stout
  • 359
  • 10
  • 22

1 Answers1

5

The WinPcapDevice class contains a property called Addresses. This property holds all addresses (IP) associated with the device:

string localIPAddress = "...";

WinPcapDeviceList devices = WinPcapDeviceList.Instance;

foreach(WinPcapDevice dev in devices)
{
  Console.Out.WriteLine("{0}", dev.Description);

  foreach(PcapAddress addr in dev.Addresses)
  {
    if(addr.Addr != null && addr.Addr.ipAddress != null)
    {
      Console.Out.WriteLine(addr.Addr.ipAddress);

      if(localIPAddress == addr.Addr.ipAddress.ToString())
      {
        Console.Out.WriteLine("Capture device found");           
      }
    }
  }
}

Of course, you could also use the CaptureDeviceList class to get a list of specific devices. Every device in this list implements ICaptureDevice. You then have to cast to a WinPcapDevice, a LibPcapLiveDevice or a AirPcapDevice in order to access the Address property.

Hope, this helps.

Hans
  • 12,902
  • 2
  • 57
  • 60
  • Thank you! that was pretty much exactly what my problem was, I noticed the dev.Addresses but I could not figure out how to use them for the life of me. so thank you again! – Brandon Stout Nov 04 '11 at 00:25
  • For some reason I can't get the member dev.Addresses. any idea? – Luis D Urraca Mar 26 '12 at 19:30
  • @LuisDUrraca: What do you mean by "can't get"? Is the addresses collection empty? – Hans Mar 26 '12 at 19:44
  • Actually, I was doing it wrong. I was trying to look for Address in the LibPcapdevice instead of LibPcabdevice[i].PcapAddress[i].Addr.ipAddress. It's all good now. Thanks your answer was very helpful. Thing is I'm getting a weird list of IP address but that gotta be another question. – Luis D Urraca Mar 26 '12 at 21:42