/* * PROJECT: Aura Operating System Development * CONTENT: List of all IPs / Utils * PROGRAMMERS: Valentin Charbonnier * Alexy DA CRUZ * Port of Cosmos Code. */ using System.Collections.Generic; using System; using Cosmos.System.Network.IPv4; using Cosmos.HAL; namespace Cosmos.System.Network.Config { /// /// Contains a IPv4 configuration /// public class IPConfig { /// /// IPv4 configurations list. /// private static readonly List ipConfigs = new List(); /// /// Add IPv4 configuration. /// /// internal static void Add(IPConfig config) { ipConfigs.Add(config); } /// /// Remove IPv4 configuration. /// /// internal static void Remove(IPConfig config) { int counter = 0; foreach (var ipconfig in ipConfigs) { if (ipconfig == config) { ipConfigs.RemoveAt(counter); return; } counter++; } } /// /// Remove All IPv4 configuration. /// internal static void RemoveAll() { ipConfigs.Clear(); } /// /// Find network. /// /// Destination IP address. /// Address value. /// Thrown on fatal error (contact support). public static Address FindNetwork(Address destIP) { Address default_gw = null; foreach (IPConfig ipConfig in ipConfigs) { if ((ipConfig.IPAddress.Hash & ipConfig.SubnetMask.Hash) == (destIP.Hash & ipConfig.SubnetMask.Hash)) { return ipConfig.IPAddress; } if ((default_gw == null) && (ipConfig.DefaultGateway.CompareTo(Address.Zero) != 0)) { default_gw = ipConfig.IPAddress; } if (!IsLocalAddress(destIP)) { return ipConfig.IPAddress; } } return default_gw; } public static bool Enable(NetworkDevice device, Address ip, Address subnet, Address gw) { if (device != null) { var config = new IPConfig(ip, subnet, gw); NetworkStack.ConfigIP(device, config); Global.mDebugger.Send("Config OK."); return true; } return false; } /// /// Check if address is local address. /// /// Address to check. /// bool value. internal static bool IsLocalAddress(Address destIP) { for (int c = 0; c < ipConfigs.Count; c++) { if ((ipConfigs[c].IPAddress.Hash & ipConfigs[c].SubnetMask.Hash) == (destIP.Hash & ipConfigs[c].SubnetMask.Hash)) { return true; } } return false; } /// /// Find interface. /// /// Source IP. /// NetworkDevice value. internal static NetworkDevice FindInterface(Address sourceIP) { return NetworkStack.AddressMap[sourceIP.Hash]; } /// /// Find route to address. /// /// Destination IP. /// Address value. /// Thrown on fatal error (contact support). internal static Address FindRoute(Address destIP) { for (int c = 0; c < ipConfigs.Count; c++) { //if (ipConfigs[c].DefaultGateway.CompareTo(Address.Zero) != 0) //{ return ipConfigs[c].DefaultGateway; //} } return null; } /// /// Create a IPv4 Configuration with no default gateway /// /// IP Address /// Subnet Mask public IPConfig(Address ip, Address subnet) : this(ip, subnet, Address.Zero) { } /// /// Create a IPv4 Configuration /// /// IP Address /// Subnet Mask /// Default gateway public IPConfig(Address ip, Address subnet, Address gw) { IPAddress = ip; SubnetMask = subnet; DefaultGateway = gw; } /// /// Get IP address. /// public Address IPAddress { get; } /// /// Get subnet mask. /// public Address SubnetMask { get; } /// /// Get default gateway. /// public Address DefaultGateway { get; } } }