using System;
using Sys = System;
using System.Collections.Generic;
using Cosmos.HAL;
using Cosmos.System.Network.ARP;
using Cosmos.System.Network.IPv4;
using Cosmos.Debug.Kernel;
namespace Cosmos.System.Network
{
///
/// Implement a Network Stack for all network devices and protocols
///
public static class NetworkStack
{
///
/// Debugger inctanse of the "System" ring, with the "NetworkStack" tag.
///
public static Debugger debugger = new Debugger("System", "NetworkStack");
///
/// Get address dictionary.
///
internal static TempDictionary AddressMap { get; private set; }
///
/// Initialize the Network Stack to prepare it for operation.
///
/// Thrown on fatal error (contact support).
public static void Init()
{
AddressMap = new TempDictionary();
// VMT Scanner issue workaround
ARPPacket.VMTInclude();
ARPPacket_Ethernet.VMTInclude();
ARPReply_Ethernet.VMTInclude();
ARPRequest_Ethernet.VMTInclude();
ICMPPacket.VMTInclude();
ICMPEchoReply.VMTInclude();
ICMPEchoRequest.VMTInclude();
UDPPacket.VMTInclude();
//TCPPacket.VMTInclude();
}
///
/// 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, Config config)
{
AddressMap.Add(config.IPAddress.Hash, nic);
Config.Add(config);
nic.DataReceived = HandlePacket;
}
///
/// 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)
{
debugger.Send("Packet Received Length=" + packetData.Length.ToString());
if (packetData == null)
{
debugger.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();
}
}
}