From f98690cb747903a6294aab4b6cdb21a91539e194 Mon Sep 17 00:00:00 2001 From: valentinbreiz Date: Fri, 22 Jan 2021 14:25:25 +0100 Subject: [PATCH 1/3] Make UDPClient global variables internal or private + code clean --- source/Cosmos.System2/Network/IPV4/UDP/DHCP/DHCPClient.cs | 8 ++++---- source/Cosmos.System2/Network/IPV4/UDP/DNS/DNSClient.cs | 4 ++-- source/Cosmos.System2/Network/IPv4/UDP/UdpClient.cs | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/source/Cosmos.System2/Network/IPV4/UDP/DHCP/DHCPClient.cs b/source/Cosmos.System2/Network/IPV4/UDP/DHCP/DHCPClient.cs index bb5349981..94dd711ea 100644 --- a/source/Cosmos.System2/Network/IPV4/UDP/DHCP/DHCPClient.cs +++ b/source/Cosmos.System2/Network/IPV4/UDP/DHCP/DHCPClient.cs @@ -21,7 +21,7 @@ namespace Cosmos.System.Network.IPv4.UDP.DHCP // /// Is DHCP ascked check variable /// - protected bool asked = false; + private bool asked = false; /// /// Get the IP address of the DHCP server @@ -98,7 +98,7 @@ namespace Cosmos.System.Network.IPv4.UDP.DHCP foreach (NetworkDevice networkDevice in NetworkDevice.Devices) { Address source = IPConfig.FindNetwork(DHCPServerAddress(networkDevice)); - DHCPRelease dhcp_release = new DHCPRelease(source, DHCPServerAddress(networkDevice), networkDevice.MACAddress); + var dhcp_release = new DHCPRelease(source, DHCPServerAddress(networkDevice), networkDevice.MACAddress); OutgoingBuffer.AddPacket(dhcp_release); NetworkStack.Update(); @@ -121,7 +121,7 @@ namespace Cosmos.System.Network.IPv4.UDP.DHCP { IPConfig.Enable(networkDevice, new Address(0, 0, 0, 0), new Address(0, 0, 0, 0), new Address(0, 0, 0, 0)); - DHCPDiscover dhcp_discover = new DHCPDiscover(networkDevice.MACAddress); + var dhcp_discover = new DHCPDiscover(networkDevice.MACAddress); OutgoingBuffer.AddPacket(dhcp_discover); NetworkStack.Update(); @@ -138,7 +138,7 @@ namespace Cosmos.System.Network.IPv4.UDP.DHCP { foreach (NetworkDevice networkDevice in NetworkDevice.Devices) { - DHCPRequest dhcp_request = new DHCPRequest(networkDevice.MACAddress, RequestedAddress, DHCPServerAddress); + var dhcp_request = new DHCPRequest(networkDevice.MACAddress, RequestedAddress, DHCPServerAddress); OutgoingBuffer.AddPacket(dhcp_request); NetworkStack.Update(); } diff --git a/source/Cosmos.System2/Network/IPV4/UDP/DNS/DNSClient.cs b/source/Cosmos.System2/Network/IPV4/UDP/DNS/DNSClient.cs index dec08b9bc..58dca96a8 100644 --- a/source/Cosmos.System2/Network/IPV4/UDP/DNS/DNSClient.cs +++ b/source/Cosmos.System2/Network/IPV4/UDP/DNS/DNSClient.cs @@ -50,7 +50,7 @@ namespace Cosmos.System.Network.IPv4.UDP.DNS queryurl = url; - DNSPacketAsk askpacket = new DNSPacketAsk(source, destination, url); + var askpacket = new DNSPacketAsk(source, destination, url); OutgoingBuffer.AddPacket(askpacket); @@ -81,7 +81,7 @@ namespace Cosmos.System.Network.IPv4.UDP.DNS } } - DNSPacketAnswer packet = new DNSPacketAnswer(rxBuffer.Dequeue().RawData); + var packet = new DNSPacketAnswer(rxBuffer.Dequeue().RawData); if ((ushort)(packet.DNSFlags & 0x0F) == (ushort)ReplyCode.OK) { diff --git a/source/Cosmos.System2/Network/IPv4/UDP/UdpClient.cs b/source/Cosmos.System2/Network/IPv4/UDP/UdpClient.cs index 46c24ab24..8aa440df1 100644 --- a/source/Cosmos.System2/Network/IPv4/UDP/UdpClient.cs +++ b/source/Cosmos.System2/Network/IPv4/UDP/UdpClient.cs @@ -25,20 +25,20 @@ namespace Cosmos.System.Network.IPv4.UDP /// /// Local port. /// - protected int localPort; + private int localPort; /// /// Destination address. /// - protected Address destination; + internal Address destination; /// /// Destination port. /// - protected int destinationPort; + private int destinationPort; /// /// RX buffer queue. /// - protected Queue rxBuffer; + internal Queue rxBuffer; /// /// Assign clients dictionary. From 0ee21466cd9d2afb2eba47282fec470e0f999d94 Mon Sep 17 00:00:00 2001 From: valentinbreiz Date: Fri, 22 Jan 2021 14:31:15 +0100 Subject: [PATCH 2/3] Return time value for Discover packet --- .../Network/IPV4/UDP/DHCP/DHCPClient.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/source/Cosmos.System2/Network/IPV4/UDP/DHCP/DHCPClient.cs b/source/Cosmos.System2/Network/IPV4/UDP/DHCP/DHCPClient.cs index 94dd711ea..b712a28e9 100644 --- a/source/Cosmos.System2/Network/IPV4/UDP/DHCP/DHCPClient.cs +++ b/source/Cosmos.System2/Network/IPV4/UDP/DHCP/DHCPClient.cs @@ -71,7 +71,7 @@ namespace Cosmos.System.Network.IPv4.UDP.DHCP { if (packet.RawData[284] == 0x02) //Offer packet received { - SendRequestPacket(packet.Client, packet.Server); + return SendRequestPacket(packet.Client, packet.Server); } else if (packet.RawData[284] == 0x05 || packet.RawData[284] == 0x06) //ACK or NAK DHCP packet received { @@ -113,7 +113,8 @@ namespace Cosmos.System.Network.IPv4.UDP.DHCP /// /// Send a packet to find the DHCP server and tell that we want a new IP address /// - public void SendDiscoverPacket() + /// time value (-1 = timeout) + public int SendDiscoverPacket() { NetworkStack.RemoveAllConfigIP(); @@ -128,13 +129,14 @@ namespace Cosmos.System.Network.IPv4.UDP.DHCP asked = true; } - Receive(); + return Receive(); } /// /// Send a request to apply the new IP configuration /// - private void SendRequestPacket(Address RequestedAddress, Address DHCPServerAddress) + /// time value (-1 = timeout) + private int SendRequestPacket(Address RequestedAddress, Address DHCPServerAddress) { foreach (NetworkDevice networkDevice in NetworkDevice.Devices) { @@ -142,7 +144,7 @@ namespace Cosmos.System.Network.IPv4.UDP.DHCP OutgoingBuffer.AddPacket(dhcp_request); NetworkStack.Update(); } - Receive(); + return Receive(); } /* From a9a3f6401bddf043c970b42c1cbb487ae10efb31 Mon Sep 17 00:00:00 2001 From: valentinbreiz Date: Fri, 22 Jan 2021 14:49:53 +0100 Subject: [PATCH 3/3] Update Network.md --- Docs/articles/Kernel/Network.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Docs/articles/Kernel/Network.md b/Docs/articles/Kernel/Network.md index 18c9ba8f7..cb7abf44b 100644 --- a/Docs/articles/Kernel/Network.md +++ b/Docs/articles/Kernel/Network.md @@ -1,8 +1,8 @@ # Network -In this article we will discuss about Networking on Cosmos, how to use the Network Stack, send and received packets. For now, available protocols are **ARP**, **IPv4**, **UDP**, **ICMP**, **DHCP** and **DNS** but the team is also working on TCP. +In this article we will discuss about Networking on Cosmos, how to use the Network Stack, send and received packets. For now, available protocols are **ARP**, **IPv4**, **UDP**, **ICMP**, **DHCP** and **DNS** but the team is also working on TCP. Note that Cosmos devkit must be installed for this article. -All protocols here don't necessary support every feature described by their RFC and may have some bugs or architecture issues, if you find something abnormal please [submit an issue](http://https://github.com/CosmosOS/Cosmos/issues/new/choose "repository") on our repository. +All protocols here don't necessary support every feature described by their RFC and may have some bugs or architecture issues, if you find bugs or something abnormal please [submit an issue](http://https://github.com/CosmosOS/Cosmos/issues/new/choose "repository") on our repository. Each protocol has a Client class which can be used to receive and send data. If a Receive() method is blocking, the method will timeout after 5 seconds or use the value optionally set by parameter. Please note that all finished connections should be closed using Close(). @@ -82,3 +82,8 @@ using(var xClient = new DnsClient()) xClient.Close(); } ``` +## Utils +## Get local IP +```csharp +Console.WriteLine(NetworkConfig.CurrentConfig.Value.IPAddress.ToString()); +```