/* * PROJECT: Aura Operating System Development * CONTENT: ARP Cache (Contains MAC/IP) * PROGRAMMERS: Valentin Charbonnier * Port of Cosmos Code. */ using System; using System.Collections.Generic; using Cosmos.HAL.Network; namespace Cosmos.System.Network.ARP { /// /// ARPCache class. /// internal static class ARPCache { /// /// Cache. /// public static Dictionary cache; /// /// Ensure cache exists. /// /// Thrown on fatal error (contact support). private static void ensureCacheExists() { if (cache == null) { cache = new Dictionary(); } } /// /// 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(); uint 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]; } } }