-
Notifications
You must be signed in to change notification settings - Fork 551
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now send DHCP request + receive ACK to apply net conf
- Loading branch information
1 parent
bca86ac
commit 52068b9
Showing
5 changed files
with
122 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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. | ||
|
@@ -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; | ||
|
@@ -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) | |
/// </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."); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
@@ -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(); | ||
} | ||
|
||
/// <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) | ||
{ | ||
|
@@ -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. | ||
/// </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; } | ||
} | ||
} | ||
} |