mirror of
https://github.com/danbulant/Cosmos
synced 2026-05-27 05:52:11 +00:00
commit
112a7f554d
4 changed files with 24 additions and 17 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
# Network
|
# 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().
|
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();
|
xClient.Close();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
## Utils
|
||||||
|
## Get local IP
|
||||||
|
```csharp
|
||||||
|
Console.WriteLine(NetworkConfig.CurrentConfig.Value.IPAddress.ToString());
|
||||||
|
```
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ namespace Cosmos.System.Network.IPv4.UDP.DHCP
|
||||||
// <summary>
|
// <summary>
|
||||||
/// Is DHCP ascked check variable
|
/// Is DHCP ascked check variable
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected bool asked = false;
|
private bool asked = false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the IP address of the DHCP server
|
/// 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
|
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
|
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)
|
foreach (NetworkDevice networkDevice in NetworkDevice.Devices)
|
||||||
{
|
{
|
||||||
Address source = IPConfig.FindNetwork(DHCPServerAddress(networkDevice));
|
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);
|
OutgoingBuffer.AddPacket(dhcp_release);
|
||||||
NetworkStack.Update();
|
NetworkStack.Update();
|
||||||
|
|
@ -113,7 +113,8 @@ namespace Cosmos.System.Network.IPv4.UDP.DHCP
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Send a packet to find the DHCP server and tell that we want a new IP address
|
/// Send a packet to find the DHCP server and tell that we want a new IP address
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void SendDiscoverPacket()
|
/// <returns>time value (-1 = timeout)</returns>
|
||||||
|
public int SendDiscoverPacket()
|
||||||
{
|
{
|
||||||
NetworkStack.RemoveAllConfigIP();
|
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));
|
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);
|
OutgoingBuffer.AddPacket(dhcp_discover);
|
||||||
NetworkStack.Update();
|
NetworkStack.Update();
|
||||||
|
|
||||||
asked = true;
|
asked = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Receive();
|
return Receive();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Send a request to apply the new IP configuration
|
/// Send a request to apply the new IP configuration
|
||||||
/// </summary>
|
/// </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)
|
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);
|
OutgoingBuffer.AddPacket(dhcp_request);
|
||||||
NetworkStack.Update();
|
NetworkStack.Update();
|
||||||
}
|
}
|
||||||
Receive();
|
return Receive();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ namespace Cosmos.System.Network.IPv4.UDP.DNS
|
||||||
|
|
||||||
queryurl = url;
|
queryurl = url;
|
||||||
|
|
||||||
DNSPacketAsk askpacket = new DNSPacketAsk(source, destination, url);
|
var askpacket = new DNSPacketAsk(source, destination, url);
|
||||||
|
|
||||||
OutgoingBuffer.AddPacket(askpacket);
|
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)
|
if ((ushort)(packet.DNSFlags & 0x0F) == (ushort)ReplyCode.OK)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -25,20 +25,20 @@ namespace Cosmos.System.Network.IPv4.UDP
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Local port.
|
/// Local port.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected int localPort;
|
private int localPort;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Destination address.
|
/// Destination address.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected Address destination;
|
internal Address destination;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Destination port.
|
/// Destination port.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected int destinationPort;
|
private int destinationPort;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// RX buffer queue.
|
/// RX buffer queue.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected Queue<UDPPacket> rxBuffer;
|
internal Queue<UDPPacket> rxBuffer;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Assign clients dictionary.
|
/// Assign clients dictionary.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue