/* * PROJECT: Aura Operating System Development * CONTENT: Network Intialization + Packet Handler * PROGRAMMERS: Valentin Charbonnier * Port of Cosmos Code. */ #define COSMOSDEBUG using System; using System.Collections.Generic; using Cosmos.Debug.Kernel; using Cosmos.HAL; using Cosmos.System.Network.ARP; using Cosmos.System.Network.Config; using Cosmos.System.Network.IPv4; using Cosmos.System.Network.IPv4.UDP; namespace Cosmos.System.Network { /// /// Implement a Network Stack for all network devices and protocols /// public static class NetworkStack { /// /// Debugger instance of the "System" ring, with the "NetworkStack" tag. /// public static Debugger debugger = new Debugger("System", "NetworkStack"); /// /// Get address dictionary. /// internal static Dictionary AddressMap { get; private set; } /// /// Get address dictionary. /// internal static Dictionary MACMap { get; private set; } /// /// Initialize the Network Stack to prepare it for operation. /// /// Thrown on fatal error (contact support). public static void Init() { AddressMap = new Dictionary(); MACMap = new Dictionary(); } /// /// Set ConfigIP for NetworkDevice /// /// Network device. /// /// IP Config private static void SetConfigIP(NetworkDevice nic, IPConfig config) { NetworkConfig.Add(nic, config); AddressMap.Add(config.IPAddress.Hash, nic); MACMap.Add(nic.MACAddress.Hash, nic); IPConfig.Add(config); nic.DataReceived = HandlePacket; } /// /// Configure a IP configuration on the given network device. /// Multiple IP Configurations can be made, like *nix environments /// /// that will have the assigned configuration /// instance that defines the IP Address, Subnet /// Mask and Default Gateway for the device /// /// /// Thrown if configuration with the given config.IPAddress.Hash already exists. /// Thrown on fatal error (contact support). /// /// /// Thrown on fatal error (contact support). /// Thrown on IO error. /// Thrown on fatal error (contact support). public static void ConfigIP(NetworkDevice nic, IPConfig config) { if (NetworkConfig.ContainsKey(nic)) { RemoveIPConfig(nic); SetConfigIP(nic, config); } else { SetConfigIP(nic, config); } NetworkConfig.CurrentConfig = new KeyValuePair(nic, config); } /// /// Check if Config is empty /// bool value. /// public static bool ConfigEmpty() { if (NetworkConfig.Keys.Count == 0) { return true; } else { return false; } } /// /// Remove All IPConfig /// public static void RemoveAllConfigIP() { AddressMap.Clear(); MACMap.Clear(); IPConfig.RemoveAll(); NetworkConfig.Clear(); } /// /// Remove IPConfig /// /// Network device. public static void RemoveIPConfig(NetworkDevice nic) { IPConfig config = NetworkConfig.Get(nic); AddressMap.Remove(config.IPAddress.Hash); MACMap.Remove(nic.MACAddress.Hash); IPConfig.Remove(config); NetworkConfig.Remove(nic); } /// /// Handle packet. /// /// Packet data array. /// Thrown on fatal error (contact support). /// Thrown on IO error. /// Thrown on fatal error (contact support). /// Thrown on fatal error (contact support). internal static void HandlePacket(byte[] packetData) { if (packetData == null) { Global.mDebugger.Send("Error packet data null"); return; } UInt16 etherType = (UInt16)((packetData[12] << 8) | packetData[13]); switch (etherType) { case 0x0806: ARPPacket.ARPHandler(packetData); break; case 0x0800: IPPacket.IPv4Handler(packetData); break; } } /// /// Called continously to keep the Network Stack going. /// /// Thrown on fatal error (contact support). /// Thrown on memory error. /// Thrown if data length of any packet in the queue is bigger than Int32.MaxValue. public static void Update() { OutgoingBuffer.Send(); } } }