sharppcap
sharppcap copied to clipboard
Is it possible to list all of the IPs on the LAN with this library?
I saw the ArpResolve example and it seems like a way to get the MAC address of an IP (that's ARP for you)
but I would like to know if there is a class here with a function that can fetch the ARP cache table?
my main goal is to just get a list of all IPs that are in use in my LAN.
Thanks. :)
Hi @islarky88
Are you referring to the table the OS keeps from arps? If so, is there an api to get that from the OS? In our case via .net core?
If not can you explain a bit more about what you mean?
Regards, Chris
Not an issue, but if you want a subnetting algorithm. I will just leave mine here. In fact, you don't need this library to create it.
`
///
/// <summary>
/// Constructor
/// </summary>
/// <param name="localIP">Local ip.</param>
/// <param name="maskIP">Mask ip.</param>
public SubnetCalculator(IPAddress localIP, IPAddress maskIP)
{
localAddress = localIP;
localNetMask = maskIP;
Init();
}
/// <summary>
/// Initialise the object
/// </summary>
private void Init()
{
hostBytePosition = getHostBytePos(localNetMask);
localIPBits = byteTobitString(localAddress);
hostPart = localIPBits.Substring(0, hostBytePosition);
subnetPart = localIPBits.Substring(hostBytePosition);
//create max and min range
minIPrange = GetBytes(hostPart.PadRight(localIPBits.Length, '0'));
maxIPrange = GetBytes(hostPart.PadRight(localIPBits.Length, '1'));
}
/// <summary>
/// Generate the subnet
/// </summary>
/// <returns>The generate.</returns>
public virtual System.Collections.Generic.List<IPAddress> Generate()
{
//generate the ip list of the subnet
List<IPAddress> range = new List<IPAddress>();
for (int a = minIPrange[0]; a <= maxIPrange[0]; a++)
{
for (int b = minIPrange[1]; b <= maxIPrange[1]; b++)
{
for (int c = minIPrange[2]; c <= maxIPrange[2]; c++)
{
for (int d = minIPrange[3]; d <= maxIPrange[3]; d++)
{
range.Add(new IPAddress(new byte[] { minIPrange[0], minIPrange[1], minIPrange[2], minIPrange[3] }));
minIPrange[3]++;
}
minIPrange[2]++;
}
minIPrange[1]++;
}
minIPrange[0]++;
}
return range;
}
/// <summary>
/// Generate the specified and remove broadcast and first IP.
/// </summary>
/// <returns>The generate.</returns>
/// <param name="broadRm">If set to <c>true</c> broad rm.</param>
public virtual List<IPAddress> Generate(bool broadRm)
{
//generate the ip list of the subnet
List<IPAddress> range = new List<IPAddress>();
for (int a = minIPrange[0]; a <= maxIPrange[0]; a++)
{
for (int b = minIPrange[1]; b <= maxIPrange[1]; b++)
{
for (int c = minIPrange[2]; c <= maxIPrange[2]; c++)
{
for (int d = minIPrange[3]; d <= maxIPrange[3]; d++)
{
range.Add(new IPAddress(new byte[] { minIPrange[0], minIPrange[1], minIPrange[2], minIPrange[3] }));
minIPrange[3]++;
}
minIPrange[2]++;
}
minIPrange[1]++;
}
minIPrange[0]++;
}
if (broadRm)
{
range.Remove(range.First<IPAddress>());
range.Remove(range.Last<IPAddress>());
}
return range;
}
/// <summary>
/// Get the Host byte position
/// </summary>
/// <returns>The byte position.</returns>
/// <param name="maskIP">Subnet mask</param>
private byte getHostBytePos(IPAddress maskIP)
{
if (maskIP.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork && maskIP != null)
throw new TypeAccessException("Not internal network address or null");
byte x = 0;
foreach (string item in maskIP.ToString().Split('.'))
{
if (byte.Parse(item) == 255)
x += 8;
else
foreach (bool b in new BitArray(new byte[] { byte.Parse(item) }))
{
if (b == true)
x++;
}
}
return x;
}
/// <summary>
/// Gets the bytes from a ip string.
/// </summary>
/// <returns>The bytes.</returns>
/// <param name="bitString">Bit string.</param>
public byte[] GetBytes(string bitString)
{
return Enumerable.Range(0, bitString.Length / 8).
Select(pos => Convert.ToByte(
bitString.Substring(pos * 8, 8),
2)
).ToArray();
}
/// <summary>
/// Convert byte ip array to a string of bit
/// </summary>
/// <returns></returns>
public string byteTobitString(IPAddress ip)
{
//convert local Ip to bit string
string ipString = "";
foreach (var item in ip.GetAddressBytes())
ipString += Convert.ToString(item, 2).PadLeft(8, '0');
return ipString;
}
}
`
Now if you want to go along and fetch the ARP table you request from all the Ip's and create a different list with the fetched Ip's.