Skip to content

Commit

Permalink
🐛 Fix DHCP
Browse files Browse the repository at this point in the history
Now send DHCP request + receive ACK to apply net conf
  • Loading branch information
valentinbreiz committed Oct 31, 2023
1 parent bca86ac commit 52068b9
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 106 deletions.
4 changes: 2 additions & 2 deletions Tests/Kernels/NetworkTest/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
9 changes: 8 additions & 1 deletion source/Cosmos.HAL2/Network/NetworkInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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++;
Expand Down Expand Up @@ -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++;
Expand Down
57 changes: 0 additions & 57 deletions source/Cosmos.System2/Network/IPv4/UDP/DHCP/DHCPAck.cs

This file was deleted.

101 changes: 58 additions & 43 deletions source/Cosmos.System2/Network/IPv4/UDP/DHCP/DHCPClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
using System;
/*
* PROJECT: Aura Operating System Development
* CONTENT: DHCP - DHCP Core
* PROGRAMMER(S): Alexy DA CRUZ <[email protected]>
* Valentin CHARBONNIER <[email protected]>
*/

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
Expand All @@ -9,7 +19,10 @@ namespace Cosmos.System.Network.IPv4.UDP.DHCP
/// </summary>
public class DHCPClient : UdpClient
{
private bool asked = false;
/// <summary>
/// Is DHCP ascked check variable
/// </summary>
private bool applied = false;

/// <summary>
/// Gets the IP address of the DHCP server.
Expand All @@ -28,6 +41,12 @@ public DHCPClient() : base(68)
{
}

/// <summary>
/// Receive data
/// </summary>
/// <param name="timeout">timeout value, default 5000ms</param>
/// <returns>time value (-1 = timeout)</returns>
/// <exception cref="InvalidOperationException">Thrown on fatal error (contact support).</exception>
private int Receive(int timeout = 5000)
{
int second = 0;
Expand All @@ -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();
}
}
}
Expand Down Expand Up @@ -110,7 +126,7 @@ public int SendDiscoverPacket()
OutgoingBuffer.AddPacket(dhcpDiscover);
NetworkStack.Update();

asked = true;
applied = false;
}

return Receive();
Expand Down Expand Up @@ -139,45 +155,44 @@ private int SendRequestPacket(Address requestedAddress)
/// </summary>
/// <param name="message">Enable/Disable the displaying of messages about DHCP applying and conf. Disabled by default.
/// </param>
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.");
}
}
}
}
}
57 changes: 54 additions & 3 deletions source/Cosmos.System2/Network/IPv4/UDP/DHCP/DHCPPacket.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
using Cosmos.HAL.Network;
/*
* PROJECT: Aura Operating System Development
* CONTENT: DHCP Packet
* PROGRAMMERS: Alexy DA CRUZ <[email protected]>
* Valentin CHARBONNIER <[email protected]>
*/

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
{
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -134,11 +146,19 @@ internal DHCPPacket(Address client, Address server, MACAddress sourceMAC, ushort
InitializeFields();
}

/// <summary>
/// Init DHCPPacket fields.
/// </summary>
/// <exception cref="ArgumentException">Thrown if RawData is invalid or null.</exception>
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)
{
Expand All @@ -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);
}
}
}
}

Expand All @@ -175,5 +211,20 @@ protected override void InitializeFields()
/// Gets the DHCP options.
/// </summary>
internal List<DHCPOption> Options { get; private set; }

/// <summary>
/// Get Subnet IPv4 Address
/// </summary>
internal Address Subnet { get; private set; }

/// <summary>
/// Get DNS IPv4 Address
/// </summary>
internal Address DNS { get; private set; }

/// <summary>
/// Get DHCP Server IPv4 Address
/// </summary>
internal Address Server { get; private set; }
}
}
}

0 comments on commit 52068b9

Please sign in to comment.