Skip to content

Commit

Permalink
Merge pull request #2769 from zarlo/netstack
Browse files Browse the repository at this point in the history
start opening up the netstack
  • Loading branch information
zarlo authored Oct 26, 2023
2 parents 2d59be2 + a0ad912 commit 16d769b
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 14 deletions.
45 changes: 45 additions & 0 deletions source/Cosmos.System2/Network/IPv4/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class Address : IComparable
/// </summary>
internal byte[] Parts = new byte[4];

public bool IsIpv4 => Parts.Length == 4;
public bool IsIpv6 => !IsIpv4;

/// <summary>
/// The <c>0.0.0.0</c> IP address.
/// </summary>
Expand Down Expand Up @@ -195,5 +198,47 @@ public int CompareTo(object obj)
throw new ArgumentException("obj is not a IPv4Address", nameof(obj));
}
}

public override bool Equals(object obj)
{

if (obj == null && this == null)
{
return true;
}

if (obj == null)
{
return false;
}

if (this == null)
{
return false;
}

if (obj is Address address)
{
if (IsIpv4 != address.IsIpv4) // not same ip type so is false
{
return false;
}

for (int i = 0; i < Parts.Length; i++)
{
if (Parts[i] != address.Parts[i])
{
return false; // ips dont match
}
}

return true; // ip type and value match

}

return false; // obj is not an Address

}

}
}
3 changes: 2 additions & 1 deletion source/Cosmos.System2/Network/IPv4/IPPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ internal static void IPv4Handler(byte[] packetData)
ARPCache.Update(ipPacket.SourceIP, ipPacket.SourceMAC);

if (NetworkStack.AddressMap.ContainsKey(ipPacket.DestinationIP.Hash) == true ||
ipPacket.DestinationIP.Parts[3] == 255)
ipPacket.DestinationIP.Parts[3] == 255 // this is wrong x.x.x.255 is not always broadcast
)
{
switch (ipPacket.Protocol)
{
Expand Down
19 changes: 7 additions & 12 deletions source/Cosmos.System2/Network/IPv4/OutgoingBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
namespace Cosmos.System.Network.IPv4
{
/// <summary>
/// Represents an outgoing IPv4 buffer.
/// Represents an outgoing IPv4 buffer. for use by drivers
/// </summary>
internal static class OutgoingBuffer
public static class OutgoingBuffer
{
private class BufferEntry
{
Expand Down Expand Up @@ -67,23 +67,18 @@ private static void EnsureQueueExists()
}

/// <summary>
/// Adds a packet to the buffer.
/// Adds a packet to the buffer. for use by drivers
/// </summary>
/// <param name="packet">The IP packet.</param>
internal static void AddPacket(IPPacket packet)
{
EnsureQueueExists();
NetworkDevice nic = IPConfig.FindInterface(packet.SourceIP);
packet.SourceMAC = nic.MACAddress;
queue.Add(new BufferEntry(nic, packet));
}
public static void AddPacket(IPPacket packet) =>
AddPacket(packet, IPConfig.FindInterface(packet.SourceIP));

/// <summary>
/// Adds a packet to the buffer.
/// Adds a packet to the buffer. for use by drivers
/// </summary>
/// <param name="packet">The IP packet.</param>
/// <param name="device">The Network Interface Controller.</param>
internal static void AddPacket(IPPacket packet, NetworkDevice device)
public static void AddPacket(IPPacket packet, NetworkDevice device)
{
EnsureQueueExists();
packet.SourceMAC = device.MACAddress;
Expand Down
34 changes: 34 additions & 0 deletions source/Cosmos.System2/Network/IPv4/TCP/Tcp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,40 @@ public class TransmissionControlBlock
/// </remarks>
public class Tcp
{

public static ushort DynamicPortStart = 49152;

private static Random dynamicPortStartRandom = new Random();

/// <summary>
/// gets a random port
/// </summary>
/// <param name="tries"></param>
/// <returns></returns>
public static ushort GetDynamicPort(int tries = 10)
{
for (int i = 0; i < tries; i++)
{

var port = (ushort)dynamicPortStartRandom.Next(DynamicPortStart, ushort.MaxValue);
var portInUse = false;
foreach (var connection in Connections)
{
if (connection.LocalEndPoint.Port == port)
{
portInUse = true;
}
}

if (!portInUse)
{
return port;
}
}

return 0;
}

/// <summary>
/// The TCP window size.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions source/Cosmos.System2/Network/IPv4/UDP/UdpClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Cosmos.System.Network.Config;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Cosmos.System.Network.IPv4.UDP
{
Expand All @@ -9,6 +10,27 @@ namespace Cosmos.System.Network.IPv4.UDP
/// </summary>
public class UdpClient : IDisposable
{
public static ushort DynamicPortStart = 49152;

private static Random dynamicPortStartRandom = new Random();

/// <summary>
/// gets a random port
/// </summary>
/// <param name="tries"></param>
/// <returns></returns>
public static ushort GetDynamicPort(int tries = 10)
{
for (int i = 0; i < tries; i++)
{

var port = (ushort)dynamicPortStartRandom.Next(DynamicPortStart, ushort.MaxValue);
if (!clients.ContainsKey(port)) return port;

}

return 0;
}
private readonly static Dictionary<uint, UdpClient> clients;
private readonly int localPort;
private int destinationPort;
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.System2/Network/NetworkStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static void RemoveIPConfig(NetworkDevice nic)
/// <exception cref="global::System.IO.IOException">Thrown on IO error.</exception>
/// <exception cref="ArgumentException">Thrown on fatal error.</exception>
/// <exception cref="OverflowException">Thrown on fatal error.</exception>
internal static void HandlePacket(byte[] packetData)
public static void HandlePacket(byte[] packetData)
{
if (packetData == null)
{
Expand Down

0 comments on commit 16d769b

Please sign in to comment.