/* * PROJECT: Aura Operating System Development * CONTENT: ICMP Client * PROGRAMMERS: Valentin Charbonnier * Port of Cosmos Code. */ using Cosmos.System.Network.Config; using Cosmos.HAL; using System; using System.Collections.Generic; using System.Text; namespace Cosmos.System.Network.IPv4 { /// /// ICMPClient class. Used to manage the ICMP connection to a client. /// public class ICMPClient : IDisposable { /// /// Clients dictionary. /// private static Dictionary clients; /// /// Destination address. /// protected Address destination; /// /// RX buffer queue. /// protected Queue rxBuffer; /// /// Assign clients dictionary. /// /// Thrown on fatal error (contact support). static ICMPClient() { clients = new Dictionary(); } /// /// Get client. /// /// IP Hash. /// ICMPClient internal static ICMPClient GetClient(uint iphash) { if (clients.ContainsKey(iphash) == true) { return clients[iphash]; } return null; } /// /// Create new instance of the class. /// public ICMPClient() { rxBuffer = new Queue(8); } /// /// Connect to client. /// /// Destination address. public void Connect(Address dest) { destination = dest; clients.Add(dest.Hash, this); } /// /// Close connection. /// /// Thrown on fatal error (contact support). public void Close() { if (clients.ContainsKey(destination.Hash) == true) { clients.Remove(destination.Hash); } } /// /// Send ICMP Echo /// public void SendEcho() { Address source = IPConfig.FindNetwork(destination); ICMPEchoRequest request = new ICMPEchoRequest(source, destination, 0x0001, 0x50); //this is working OutgoingBuffer.AddPacket(request); //Aura doesn't work when this is called. NetworkStack.Update(); } /// /// Receive data /// /// Source end point. /// timeout value, default 5000ms /// Address from Domain Name /// Thrown on fatal error (contact support). public int Receive(ref EndPoint source, int timeout = 5000) { int second = 0; int _deltaT = 0; while (rxBuffer.Count < 1) { if (second > (timeout / 1000)) { return -1; } if (_deltaT != RTC.Second) { second++; _deltaT = RTC.Second; } } ICMPEchoReply packet = new ICMPEchoReply(rxBuffer.Dequeue().RawData); source.address = packet.SourceIP; return second; } /// /// Receive data from packet. /// /// Packet to receive. /// Thrown on fatal error (contact support). /// Thrown on IO error. public void ReceiveData(ICMPPacket packet) { rxBuffer.Enqueue(packet); } /// /// Close Client /// public void Dispose() { Close(); } } }