Merge pull request #1651 from CosmosOS/networking

Network work
This commit is contained in:
valentinbreiz 2021-01-22 19:00:31 +01:00 committed by GitHub
commit 112a7f554d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 17 deletions

View file

@ -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());
```

View file

@ -21,7 +21,7 @@ namespace Cosmos.System.Network.IPv4.UDP.DHCP
// <summary>
/// Is DHCP ascked check variable
/// </summary>
protected bool asked = false;
private bool asked = false;
/// <summary>
/// Get the IP address of the DHCP server
@ -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
{
@ -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();
@ -113,7 +113,8 @@ namespace Cosmos.System.Network.IPv4.UDP.DHCP
/// <summary>
/// Send a packet to find the DHCP server and tell that we want a new IP address
/// </summary>
public void SendDiscoverPacket()
/// <returns>time value (-1 = timeout)</returns>
public int SendDiscoverPacket()
{
NetworkStack.RemoveAllConfigIP();
@ -121,28 +122,29 @@ 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();
asked = true;
}
Receive();
return Receive();
}
/// <summary>
/// Send a request to apply the new IP configuration
/// </summary>
private void SendRequestPacket(Address RequestedAddress, Address DHCPServerAddress)
/// <returns>time value (-1 = timeout)</returns>
private int SendRequestPacket(Address RequestedAddress, Address DHCPServerAddress)
{
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();
}
Receive();
return Receive();
}
/*

View file

@ -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)
{

View file

@ -25,20 +25,20 @@ namespace Cosmos.System.Network.IPv4.UDP
/// <summary>
/// Local port.
/// </summary>
protected int localPort;
private int localPort;
/// <summary>
/// Destination address.
/// </summary>
protected Address destination;
internal Address destination;
/// <summary>
/// Destination port.
/// </summary>
protected int destinationPort;
private int destinationPort;
/// <summary>
/// RX buffer queue.
/// </summary>
protected Queue<UDPPacket> rxBuffer;
internal Queue<UDPPacket> rxBuffer;
/// <summary>
/// Assign clients dictionary.