Permalink
Browse files

upgraded SharpPcap 4.0.0.0

  • Loading branch information...
1 parent 71ae210 commit 289abae21f5029d72d56f70b6710fc71b6ef9d27 seuffert committed Nov 17, 2011
View
@@ -29,57 +29,15 @@ namespace SharpPcap
/// </summary>
public class ARP
{
- private string _deviceName;
+ private LibPcap.LibPcapLiveDevice _device;
/// <summary>
/// Constructs a new ARP Resolver
/// </summary>
- public ARP()
+ /// <param name="device">The network device on which this resolver sends its ARP packets</param>
+ public ARP(LibPcap.LibPcapLiveDevice device)
{
- }
-
- /// <summary>
- /// Constructs a new ARP Resolver
- /// </summary>
- /// <param name="deviceName">The name of the network device on which this resolver sends its ARP packets</param>
- public ARP(string deviceName)
- {
- DeviceName = deviceName;
- }
-
- /// <summary>
- /// The source MAC address to be used for ARP requests.
- /// If null, the local device MAC address is used
- /// </summary>
- public PhysicalAddress LocalMAC
- {
- get;
- set;
- }
-
- /// <summary>
- /// The source IP address to be used for ARP requests.
- /// If null, the local device IP address is used
- /// </summary>
- public System.Net.IPAddress LocalIP
- {
- get;
- set;
- }
-
- /// <summary>
- /// The default device name on which to send ARP requests
- /// </summary>
- public string DeviceName
- {
- get
- {
- return _deviceName;
- }
- set
- {
- _deviceName = value;
- }
+ _device = device;
}
private TimeSpan timeout = new TimeSpan(0, 0, 1);
@@ -108,54 +66,42 @@ public TimeSpan Timeout
/// <returns>The MAC address that matches to the given IP address</returns>
public PhysicalAddress Resolve(System.Net.IPAddress destIP)
{
- if(DeviceName==null)
- throw new Exception("Can't resolve host: A network device must be specified");
-
- return Resolve(destIP, DeviceName);
- }
-
- /// <summary>
- /// Resolves the MAC address of the specified IP address.
- /// </summary>
- /// <param name="destIP">The IP address to resolve</param>
- /// <param name="deviceName">The local network device name on which to send the ARP request</param>
- /// <param name="srcIP">The local IP address from which to send the ARP request</param>
- /// <returns>The MAC address that matches to the given IP address or
- /// null if there was a timeout</returns>
- public PhysicalAddress Resolve(System.Net.IPAddress destIP, string deviceName, System.Net.IPAddress srcIP)
- {
- DeviceName = deviceName;
- LocalIP = srcIP;
- return Resolve(destIP, DeviceName);
+ return Resolve(destIP, null, null);
}
/// <summary>
/// Resolves the MAC address of the specified IP address
/// </summary>
/// <param name="destIP">The IP address to resolve</param>
- /// <param name="deviceName">The local network device name on which to send the ARP request</param>
+ /// <param name="localIP">The local IP address from which to send the ARP request, if null the local address will be discovered</param>
+ /// <param name="localMAC">The localMAC address to use, if null the local mac will be discovered</param>
/// <returns>The MAC address that matches to the given IP address or
/// null if there was a timeout</returns>
- public PhysicalAddress Resolve(System.Net.IPAddress destIP, string deviceName)
+ public PhysicalAddress Resolve(System.Net.IPAddress destIP,
+ System.Net.IPAddress localIP,
+ PhysicalAddress localMAC)
{
- PhysicalAddress localMAC = LocalMAC;
- System.Net.IPAddress localIP = LocalIP;
- var device = LibPcap.LibPcapLiveDeviceList.Instance[DeviceName];
-
// if no local ip address is specified attempt to find one from the adapter
if (localIP == null)
{
- if (device.Addresses.Count > 0)
+ if (_device.Addresses.Count > 0)
{
- foreach(var address in device.Addresses)
+ // attempt to find an ipv4 address.
+ // ARP is ipv4, NDP is used for ipv6
+ foreach(var address in _device.Addresses)
{
- if(address.Addr.type == LibPcap.Sockaddr.Type.AF_INET_AF_INET6)
+ if(address.Addr.type == LibPcap.Sockaddr.AddressTypes.AF_INET_AF_INET6)
{
- localIP = address.Addr.ipAddress;
- break; // break out of the foreach
+ // make sure the address is ipv4
+ if (address.Addr.ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
+ {
+ localIP = address.Addr.ipAddress;
+ break; // break out of the foreach
+ }
}
}
+ // if we can't find either an ipv6 or an ipv4 address use the localhost address
if(localIP == null)
{
localIP = System.Net.IPAddress.Parse("127.0.0.1");
@@ -166,9 +112,9 @@ public PhysicalAddress Resolve(System.Net.IPAddress destIP, string deviceName)
// if no local mac address is specified attempt to find one from the device
if(localMAC == null)
{
- foreach(var address in device.Addresses)
+ foreach(var address in _device.Addresses)
{
- if(address.Addr.type == LibPcap.Sockaddr.Type.HARDWARE)
+ if(address.Addr.type == LibPcap.Sockaddr.AddressTypes.HARDWARE)
{
localMAC = address.Addr.hardwareAddress;
}
@@ -192,10 +138,10 @@ public PhysicalAddress Resolve(System.Net.IPAddress destIP, string deviceName)
String arpFilter = "arp and ether dst " + localMAC.ToString();
//open the device with 20ms timeout
- device.Open(DeviceMode.Promiscuous, 20);
+ _device.Open(DeviceMode.Promiscuous, 20);
//set the filter
- device.Filter = arpFilter;
+ _device.Filter = arpFilter;
// set a last request time that will trigger sending the
// arp request immediately
@@ -212,12 +158,12 @@ public PhysicalAddress Resolve(System.Net.IPAddress destIP, string deviceName)
if(requestInterval < (DateTime.Now - lastRequestTime))
{
// inject the packet to the wire
- device.SendPacket(request);
+ _device.SendPacket(request);
lastRequestTime = DateTime.Now;
}
//read the next packet from the network
- var reply = device.GetNextPacket();
+ var reply = _device.GetNextPacket();
if(reply == null)
{
continue;
@@ -241,7 +187,7 @@ public PhysicalAddress Resolve(System.Net.IPAddress destIP, string deviceName)
}
// free the device
- device.Close();
+ _device.Close();
// the timeout happened
if(DateTime.Now >= timeoutDateTime)
@@ -25,7 +25,7 @@
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
-[assembly: AssemblyVersion("3.6.0")]
+[assembly: AssemblyVersion("4.0.0")]
//
// In order to sign your assembly you must specify a key to use. Refer to the
@@ -34,6 +34,9 @@ public class CaptureDeviceList : ReadOnlyCollection<ICaptureDevice>
{
private static CaptureDeviceList instance;
+ private WinPcap.WinPcapDeviceList winPcapDeviceList;
+ private LibPcap.LibPcapLiveDeviceList libPcapDeviceList;
+
/// <summary>
/// Method to retrieve this classes singleton instance
/// </summary>
@@ -61,7 +64,24 @@ public static CaptureDeviceList Instance
/// </returns>
public static CaptureDeviceList New()
{
- return new CaptureDeviceList();
+ var newCaptureDevice = new CaptureDeviceList();
+
+ // windows
+ if ((Environment.OSVersion.Platform == PlatformID.Win32NT) ||
+ (Environment.OSVersion.Platform == PlatformID.Win32Windows))
+ {
+ newCaptureDevice.winPcapDeviceList = WinPcap.WinPcapDeviceList.New();
+ }
+ else // not windows
+ {
+ newCaptureDevice.libPcapDeviceList = LibPcap.LibPcapLiveDeviceList.New();
+ }
+
+ // refresh the device list to flush the original devices and pull the
+ // new ones into the newCaptureDevice
+ newCaptureDevice.Refresh();
+
+ return newCaptureDevice;
}
/// <summary>
@@ -70,6 +90,17 @@ public static CaptureDeviceList New()
private CaptureDeviceList()
: base(new List<ICaptureDevice>())
{
+ // windows
+ if ((Environment.OSVersion.Platform == PlatformID.Win32NT) ||
+ (Environment.OSVersion.Platform == PlatformID.Win32Windows))
+ {
+ winPcapDeviceList = WinPcap.WinPcapDeviceList.Instance;
+ }
+ else // not windows
+ {
+ libPcapDeviceList = LibPcap.LibPcapLiveDeviceList.Instance;
+ }
+
Refresh();
}
@@ -79,23 +110,23 @@ private CaptureDeviceList()
/// <returns>
/// A <see cref="List&lt;ICaptureDevice&gt;"/>
/// </returns>
- private static List<ICaptureDevice> GetDevices()
+ private List<ICaptureDevice> GetDevices()
{
List<ICaptureDevice> deviceList = new List<ICaptureDevice>();
// windows
if ((Environment.OSVersion.Platform == PlatformID.Win32NT) ||
(Environment.OSVersion.Platform == PlatformID.Win32Windows))
{
- var dl = WinPcap.WinPcapDeviceList.Instance;
+ var dl = winPcapDeviceList;
foreach (var c in dl)
{
deviceList.Add(c);
}
}
else // not windows
{
- var dl = LibPcap.LibPcapLiveDeviceList.Instance;
+ var dl = libPcapDeviceList;
foreach (var c in dl)
{
deviceList.Add(c);
@@ -112,71 +143,29 @@ public void Refresh()
{
lock (this)
{
- // retrieve the current device list
- var newDeviceList = GetDevices();
+ // clear out any items we might have
+ base.Items.Clear();
- // update existing devices with values in the new list
- foreach (var newItem in newDeviceList)
+ // windows
+ if ((Environment.OSVersion.Platform == PlatformID.Win32NT) ||
+ (Environment.OSVersion.Platform == PlatformID.Win32Windows))
{
- foreach (var existingItem in base.Items)
- {
- if (newItem.Name == existingItem.Name)
- {
- // TODO: copy things from the new item to the existing item
-
- break; // break out of the foreach(existingItem)
- }
- }
- }
+ winPcapDeviceList.Refresh();
- // find items the current list is missing
- foreach (var newItem in newDeviceList)
- {
- bool found = false;
- foreach (var existingItem in base.Items)
+ foreach(var i in winPcapDeviceList)
{
- if (existingItem.Name == newItem.Name)
- {
- found = true;
- break;
- }
- }
-
- // add items that we were missing
- if (!found)
- {
- base.Items.Add(newItem);
+ base.Items.Add(i);
}
}
-
- // find items that we have that the current list is missing
- var itemsToRemove = new List<ICaptureDevice>();
- foreach (var existingItem in base.Items)
+ else // not windows
{
- bool found = false;
+ libPcapDeviceList.Refresh();
- foreach (var newItem in newDeviceList)
+ foreach(var i in libPcapDeviceList)
{
- if (existingItem.Name == newItem.Name)
- {
- found = true;
- break;
- }
- }
-
- // add the device we didn't see in the new list
- if (!found)
- {
- itemsToRemove.Add(existingItem);
+ base.Items.Add(i);
}
}
-
- // remove the items outside of the foreach() to avoid
- // enumeration errors
- foreach (var itemToRemove in itemsToRemove)
- {
- base.Items.Remove(itemToRemove);
- }
}
}
@@ -190,16 +179,20 @@ public void Refresh()
// with other methods
lock (this)
{
- var devices = (List<ICaptureDevice>)base.Items;
- var dev = devices.Find(delegate(ICaptureDevice i) { return i.Name == Name; });
- var result = dev ?? devices.Find(delegate(ICaptureDevice i) { return i.Description == Name; });
-
- if (result == null)
- throw new IndexOutOfRangeException();
- return result;
+ // windows
+ if ((Environment.OSVersion.Platform == PlatformID.Win32NT) ||
+ (Environment.OSVersion.Platform == PlatformID.Win32Windows))
+ {
+ return winPcapDeviceList[Name];
+ }
+ else // not windows
+ {
+ return libPcapDeviceList[Name];
+ }
}
}
}
+
#endregion
}
}
Oops, something went wrong.

0 comments on commit 289abae

Please sign in to comment.