using System;
using sysIO = System.IO;
using Cosmos.HAL.Network;
namespace Cosmos.System.Network.ARP
{
///
/// ARPCache class.
///
internal static class ARPCache
{
///
/// Cache.
///
private static TempDictionary cache;
///
/// Ensure cache exists.
///
/// Thrown on fatal error (contact support).
private static void ensureCacheExists()
{
if (cache == null)
{
cache = new TempDictionary();
}
}
///
/// Check if ARP cache contains the given IP.
///
/// IP address to check.
/// bool value.
internal static bool Contains(IPv4.Address ipAddress)
{
ensureCacheExists();
return cache.ContainsKey(ipAddress.Hash);
}
///
/// Update ARP cache.
///
/// IP address.
/// MAC address.
/// Thrown on fatal error (contact support).
/// Thrown on IO error.
/// Thrown on fatal error (contact support).
internal static void Update(IPv4.Address ipAddress, MACAddress macAddress)
{
ensureCacheExists();
if (ipAddress == null)
{
global::System.Console.Write("");
}
UInt32 ip_hash = ipAddress.Hash;
if (ip_hash == 0)
{
return;
}
if (cache.ContainsKey(ip_hash) == false)
{
cache.Add(ip_hash, macAddress);
}
else
{
cache[ip_hash] = macAddress;
}
}
///
/// Resolve ARP cache.
///
/// IP address.
/// MAC address.
internal static MACAddress Resolve(IPv4.Address ipAddress)
{
ensureCacheExists();
if (cache.ContainsKey(ipAddress.Hash) == false)
{
return null;
}
return cache[ipAddress.Hash];
}
}
}