diff --git a/Tests/Kernels/NetworkTest/Kernel.cs b/Tests/Kernels/NetworkTest/Kernel.cs index 215dc44bc3..f013389439 100644 --- a/Tests/Kernels/NetworkTest/Kernel.cs +++ b/Tests/Kernels/NetworkTest/Kernel.cs @@ -143,10 +143,10 @@ private void TestDhcpConnection() xClient.SendDiscoverPacket(); var ip = NetworkConfiguration.CurrentAddress.ToString(); + Global.debugger.Send("IP: " + ip); + Assert.IsTrue(ip != null, "Received IP is valid."); Assert.IsFalse(NetworkConfiguration.CurrentAddress.Equals(Address.Zero), "Received IP is not ZERO, DHCP works"); - - Global.debugger.Send("IP: " + NetworkConfiguration.CurrentAddress.ToString()); } } diff --git a/source/Cosmos.HAL2/Network/NetworkInit.cs b/source/Cosmos.HAL2/Network/NetworkInit.cs index 5fad2b9dbc..51f2d4e82a 100644 --- a/source/Cosmos.HAL2/Network/NetworkInit.cs +++ b/source/Cosmos.HAL2/Network/NetworkInit.cs @@ -23,7 +23,6 @@ public static void Init() if (device.VendorID == (ushort)VendorID.AMD && device.DeviceID == (ushort)DeviceID.PCNETII) { - Console.WriteLine("NIC IRQ: " + device.InterruptLine); var AMDPCNetIIDevice = new AMDPCNetII(device); @@ -42,10 +41,14 @@ public static void Init() if (device.VendorID == 0x10EC && device.DeviceID == 0x8139) { + Console.WriteLine("NIC IRQ: " + device.InterruptLine); + var RTL8139Device = new RTL8139(device); RTL8139Device.NameID = "eth" + NetworkDeviceID; + Console.WriteLine("Registered at " + RTL8139Device.NameID + " (" + RTL8139Device.MACAddress.ToString() + ")"); + RTL8139Device.Enable(); NetworkDeviceID++; @@ -153,10 +156,14 @@ public static void Init() device.DeviceID == (ushort)E1000DeviceID.IntelI219LM_2 ) { + Console.WriteLine("NIC IRQ: " + device.InterruptLine); + var E1000Device = new E1000(device); E1000Device.NameID = ("eth" + NetworkDeviceID); + Console.WriteLine("Registered at " + E1000Device.NameID + " (" + E1000Device.MACAddress.ToString() + ")"); + E1000Device.Enable(); NetworkDeviceID++; diff --git a/source/Cosmos.System2/Network/IPv4/UDP/DHCP/DHCPAck.cs b/source/Cosmos.System2/Network/IPv4/UDP/DHCP/DHCPAck.cs deleted file mode 100644 index d8741e61a6..0000000000 --- a/source/Cosmos.System2/Network/IPv4/UDP/DHCP/DHCPAck.cs +++ /dev/null @@ -1,57 +0,0 @@ -namespace Cosmos.System.Network.IPv4.UDP.DHCP -{ - /// - /// Represents a DHCP acknowledge (ACK) packet. - /// - internal class DHCPAck : DHCPPacket - { - /// - /// Initializes a new instance of the class. - /// - internal DHCPAck() : base() - { } - - /// - /// Initializes a new instance of the class. - /// - /// Raw data. - internal DHCPAck(byte[] rawData) : base(rawData) - { } - - protected override void InitializeFields() - { - base.InitializeFields(); - - foreach (var option in Options) - { - if (option.Type == 1) // Mask - { - Subnet = new Address(option.Data, 0); - } - else if (option.Type == 3) // Router - { - Server = new Address(option.Data, 0); - } - else if (option.Type == 6) // DNS - { - DNS = new Address(option.Data, 0); - } - } - } - - /// - /// Gets the subnet IPv4 address. - /// - internal Address Subnet { get; private set; } - - /// - /// Gets the DNS IPv4 address. - /// - internal Address DNS { get; private set; } - - /// - /// Gets the DHCP server IPv4 address. - /// - internal Address Server { get; private set; } - } -} diff --git a/source/Cosmos.System2/Network/IPv4/UDP/DHCP/DHCPClient.cs b/source/Cosmos.System2/Network/IPv4/UDP/DHCP/DHCPClient.cs index 9a9fd9d523..a4ff6d2ec8 100644 --- a/source/Cosmos.System2/Network/IPv4/UDP/DHCP/DHCPClient.cs +++ b/source/Cosmos.System2/Network/IPv4/UDP/DHCP/DHCPClient.cs @@ -1,5 +1,15 @@ -using System; +/* +* PROJECT: Aura Operating System Development +* CONTENT: DHCP - DHCP Core +* PROGRAMMER(S): Alexy DA CRUZ +* Valentin CHARBONNIER +*/ + +using Cosmos.System.Network.IPv4; +using System; +using Cosmos.System.Network.IPv4.UDP.DNS; using Cosmos.System.Network.Config; +using System.Collections.Generic; using Cosmos.HAL; namespace Cosmos.System.Network.IPv4.UDP.DHCP @@ -9,7 +19,10 @@ namespace Cosmos.System.Network.IPv4.UDP.DHCP /// public class DHCPClient : UdpClient { - private bool asked = false; + /// + /// Is DHCP ascked check variable + /// + private bool applied = false; /// /// Gets the IP address of the DHCP server. @@ -28,6 +41,12 @@ public DHCPClient() : base(68) { } + /// + /// Receive data + /// + /// timeout value, default 5000ms + /// time value (-1 = timeout) + /// Thrown on fatal error (contact support). private int Receive(int timeout = 5000) { int second = 0; @@ -48,23 +67,20 @@ private int Receive(int timeout = 5000) var packet = new DHCPPacket(rxBuffer.Dequeue().RawData); - if (packet.MessageType == 2) // Boot Reply + if (packet.MessageType == 2) //Boot Reply { - if (packet.RawData[284] == 0x02) // Offer packet received + if (packet.RawData[284] == 0x02) //Offer packet received { NetworkStack.Debugger.Send("Offer received."); return SendRequestPacket(packet.Client); } - else if (packet.RawData[284] is 0x05 or 0x06) // ACK or NAK DHCP packet received + else if (packet.RawData[284] == 0x05 || packet.RawData[284] == 0x06) //ACK or NAK DHCP packet received { - var ack = new DHCPAck(packet.RawData); - if (asked) - { - Apply(ack, true); - } - else + if (applied == false) { - Apply(ack); + Apply(packet, true); + + Close(); } } } @@ -110,7 +126,7 @@ public int SendDiscoverPacket() OutgoingBuffer.AddPacket(dhcpDiscover); NetworkStack.Update(); - asked = true; + applied = false; } return Receive(); @@ -139,45 +155,44 @@ private int SendRequestPacket(Address requestedAddress) /// /// Enable/Disable the displaying of messages about DHCP applying and conf. Disabled by default. /// - private void Apply(DHCPAck packet, bool message = false) + private void Apply(DHCPPacket packet, bool message = false) { - NetworkStack.RemoveAllConfigIP(); - - //cf. Roadmap. (have to change this, because some network interfaces are not configured in dhcp mode) [have to be done in 0.5.x] - foreach (NetworkDevice networkDevice in NetworkDevice.Devices) + if (applied == false) { - // NOTE: @ascpixi: Why are we checking if ToString() returns null - // *four* times...? Can this be removed? - if (packet.Client.ToString() == null || - packet.Client.ToString() == null || - packet.Client.ToString() == null || - packet.Client.ToString() == null) - { - throw new Exception("Parsing DHCP ACK Packet failed, can't apply network configuration."); - } - else + NetworkStack.RemoveAllConfigIP(); + + //cf. Roadmap. (have to change this, because some network interfaces are not configured in dhcp mode) [have to be done in 0.5.x] + foreach (NetworkDevice networkDevice in NetworkDevice.Devices) { - if (message) + if (packet.Client.ToString() == null) { - NetworkStack.Debugger.Send("[DHCP ACK][" + networkDevice.Name + "] Packet received, applying IP configuration..."); - NetworkStack.Debugger.Send(" IP Address : " + packet.Client.ToString()); - NetworkStack.Debugger.Send(" Subnet mask : " + packet.Subnet.ToString()); - NetworkStack.Debugger.Send(" Gateway : " + packet.Server.ToString()); - NetworkStack.Debugger.Send(" DNS server : " + packet.DNS.ToString()); + throw new Exception("Parsing DHCP ACK Packet failed, can't apply network configuration."); } + else + { + HAL.Global.debugger.Send("[DHCP ACK][" + networkDevice.Name + "] Packet received, applying IP configuration..."); + HAL.Global.debugger.Send(" IP Address : " + packet.Client.ToString()); + HAL.Global.debugger.Send(" Subnet mask : " + packet.Subnet.ToString()); + HAL.Global.debugger.Send(" Gateway : " + packet.Server.ToString()); + HAL.Global.debugger.Send(" DNS server : " + packet.DNS.ToString()); - IPConfig.Enable(networkDevice, packet.Client, packet.Subnet, packet.Server); - DNSConfig.Add(packet.DNS); + IPConfig.Enable(networkDevice, packet.Client, packet.Subnet, packet.Server); + DNSConfig.Add(packet.DNS); - if (message) - { - NetworkStack.Debugger.Send("[DHCP CONFIG][" + networkDevice.Name + "] IP configuration applied."); - asked = false; + HAL.Global.debugger.Send("[DHCP CONFIG][" + networkDevice.Name + "] IP configuration applied."); + + applied = true; + + return; } } - } - Close(); + HAL.Global.debugger.Send("[DHCP CONFIG] No DHCP Config applied!"); + } + else + { + HAL.Global.debugger.Send("[DHCP CONFIG] DHCP already applied."); + } } } -} +} \ No newline at end of file diff --git a/source/Cosmos.System2/Network/IPv4/UDP/DHCP/DHCPPacket.cs b/source/Cosmos.System2/Network/IPv4/UDP/DHCP/DHCPPacket.cs index c54d57e572..b27ac7cf30 100644 --- a/source/Cosmos.System2/Network/IPv4/UDP/DHCP/DHCPPacket.cs +++ b/source/Cosmos.System2/Network/IPv4/UDP/DHCP/DHCPPacket.cs @@ -1,6 +1,17 @@ -using Cosmos.HAL.Network; +/* +* PROJECT: Aura Operating System Development +* CONTENT: DHCP Packet +* PROGRAMMERS: Alexy DA CRUZ +* Valentin CHARBONNIER +*/ + +using Cosmos.HAL; +using Cosmos.HAL.Network; +using Cosmos.System.Network.IPv4; +using Cosmos.System.Network.IPv4.UDP; using System; using System.Collections.Generic; +using System.Text; namespace Cosmos.System.Network.IPv4.UDP.DHCP { @@ -41,6 +52,7 @@ public class DHCPPacket : UDPPacket public static void DHCPHandler(byte[] packetData) { var dhcpPacket = new DHCPPacket(packetData); + var receiver = UdpClient.GetClient(dhcpPacket.DestinationPort); receiver?.ReceiveData(dhcpPacket); } @@ -134,11 +146,19 @@ internal DHCPPacket(Address client, Address server, MACAddress sourceMAC, ushort InitializeFields(); } + /// + /// Init DHCPPacket fields. + /// + /// Thrown if RawData is invalid or null. protected override void InitializeFields() { base.InitializeFields(); MessageType = RawData[42]; - Client = new Address(RawData, 58); + + if (RawData[58] != 0) + { + Client = new Address(RawData, 58); + } if (RawData[282] != 0) { @@ -158,6 +178,22 @@ protected override void InitializeFields() i += option.Length; } + + foreach (var option in Options) + { + if (option.Type == 1) //Mask + { + Subnet = new Address(option.Data, 0); + } + else if (option.Type == 3) //Router + { + Server = new Address(option.Data, 0); + } + else if (option.Type == 6) //DNS + { + DNS = new Address(option.Data, 0); + } + } } } @@ -175,5 +211,20 @@ protected override void InitializeFields() /// Gets the DHCP options. /// internal List Options { get; private set; } + + /// + /// Get Subnet IPv4 Address + /// + internal Address Subnet { get; private set; } + + /// + /// Get DNS IPv4 Address + /// + internal Address DNS { get; private set; } + + /// + /// Get DHCP Server IPv4 Address + /// + internal Address Server { get; private set; } } -} +} \ No newline at end of file