Skip to content

Commit

Permalink
✨ TCPClient connect local port generation + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinbreiz committed Dec 5, 2023
1 parent 069069f commit c6bb978
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
51 changes: 48 additions & 3 deletions source/Cosmos.System2_Plugs/System/Net/IPAddressImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,36 @@ public static void set_PrivateAddress(uint address)
PrivateAddress = address;
}

public static void CCtor(IPAddress aThis)
{
}

public static void Ctor(IPAddress aThis, long address)
{
PrivateAddress = (uint)address;
}

public static void Ctor(IPAddress aThis, ReadOnlySpan<byte> address)
{
Ctor(aThis, address.ToArray());
}

public static void Ctor(IPAddress aThis, ReadOnlySpan<byte> address, long scopeId)
{
Ctor(aThis, address.ToArray());
}

public static void Ctor(IPAddress aThis, byte[] address)
{
Cosmos.HAL.Global.debugger.Send(address[0] + "." + address[1] + "." + address[2] + "." + address[3]);

if (address.Length == IPv4AddressBytes)
{
PrivateAddress = (uint)(address[0] << 24 | address[1] << 16 | address[2] << 8 | address[3]);
PrivateAddress = (uint)((address[0] << 24) | (address[1] << 16) | (address[2] << 8) | (address[3] << 0));
}
else if (address.Length == IPv6AddressBytes)
{
Cosmos.HAL.Global.debugger.Send("IPv6 not supported yet!");
throw new NotImplementedException("IPv6 not supported yet!");
}
else
{
Expand All @@ -49,8 +64,38 @@ public static void Ctor(IPAddress aThis, ReadOnlySpan<byte> address)
}
}

public static void Ctor(IPAddress aThis, ReadOnlySpan<byte> address, long scopeId)
public static IPAddress Parse(string address)
{
string[] fragments = address.Split('.');
if (fragments.Length == 4)
{
try
{
var addressArray = new byte[4];
addressArray[0] = byte.Parse(fragments[0]);
addressArray[1] = byte.Parse(fragments[1]);
addressArray[2] = byte.Parse(fragments[2]);
addressArray[3] = byte.Parse(fragments[3]);

Cosmos.HAL.Global.debugger.Send(fragments[0] + "." + fragments[1] + "." + fragments[2] + "." + fragments[3]);
Cosmos.HAL.Global.debugger.Send(addressArray[0] + "." + addressArray[1] + "." + addressArray[2] + "." + addressArray[3]);

return new IPAddress(addressArray);
}
catch
{
return null;
}
}
else
{
return null;
}
}

public static string ToString(IPAddress aThis)
{
return (byte)(PrivateAddress >> 24) + "." + (byte)(PrivateAddress >> 16 & 0xff) + "." + (byte)(PrivateAddress >> 8 & 0xff) + "." + (byte)(PrivateAddress & 0xff);
}
}
}
31 changes: 28 additions & 3 deletions source/Cosmos.System2_Plugs/System/Net/Sockets/SocketImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net.Sockets;
using System.Text;
using Cosmos.System.Helpers;
using Cosmos.System.Network.Config;
using Cosmos.System.Network.IPv4.TCP;
using IL2CPU.API.Attribs;

Expand All @@ -19,6 +20,9 @@ public static class SocketImpl
private static EndPoint _localEndPoint;
private static EndPoint _remoteEndPoint;

private const int MinPort = 49152;
private const int MaxPort = 65535;

public static void Ctor(Socket aThis, SocketType socketType, ProtocolType protocolType)
{
CheckSocket(aThis, socketType, protocolType);
Expand Down Expand Up @@ -111,20 +115,41 @@ private static uint ConvertIPAddressToUInt32(IPAddress ip)
return (uint)(bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]);
}

public static int GetRandomPort()
{
Random random = new Random();
return random.Next(MinPort, MaxPort + 1);
}

public static void Connect(Socket aThis, IPAddress address, int port)
{
Start();

Cosmos.HAL.Global.debugger.Send("Connect - start okay");
Cosmos.HAL.Global.debugger.Send("address - " + address.ToString());
Cosmos.HAL.Global.debugger.Send("port - " + port);

if (StateMachine.Status == Status.ESTABLISHED)
{
throw new Exception("Client must be closed before setting a new connection.");
}

StateMachine.RemoteEndPoint.Address = new Cosmos.System.Network.IPv4.Address(ConvertIPAddressToUInt32(address));
StateMachine.LocalEndPoint.Address = Cosmos.System.Network.Config.IPConfig.FindNetwork(StateMachine.RemoteEndPoint.Address);
StateMachine.RemoteEndPoint.Address = Cosmos.System.Network.IPv4.Address.Parse(EndPoint.Address.ToString());
StateMachine.RemoteEndPoint.Port = (ushort)port;
StateMachine.LocalEndPoint.Address = NetworkConfiguration.CurrentAddress;
StateMachine.LocalEndPoint.Port = (ushort)GetRandomPort();

_remoteEndPoint = new IPEndPoint(StateMachine.RemoteEndPoint.Address.ToUInt32(), StateMachine.RemoteEndPoint.Port);
Cosmos.HAL.Global.debugger.Send("StateMachine.RemoteEndPoint.Address=" + StateMachine.RemoteEndPoint.Address.ToString());
Cosmos.HAL.Global.debugger.Send("StateMachine.LocalEndPoint.Address=" + StateMachine.LocalEndPoint.Address.ToString());

_remoteEndPoint = new IPEndPoint(address, StateMachine.RemoteEndPoint.Port);
_localEndPoint = new IPEndPoint(StateMachine.LocalEndPoint.Address.ToUInt32(), StateMachine.LocalEndPoint.Port);

Cosmos.HAL.Global.debugger.Send("_remoteEndPoint=" + _remoteEndPoint.ToString());
Cosmos.HAL.Global.debugger.Send("_localEndPoint=" + _localEndPoint.ToString());

Cosmos.HAL.Global.debugger.Send("Connect - endpoints okay.");

//Generate Random Sequence Number
var rnd = new Random();
var SequenceNumber = (uint)(rnd.Next(0, Int32.MaxValue) << 32) | (uint)rnd.Next(0, Int32.MaxValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ public static int get_ReceiveBufferSize(TcpClient aThis)

public static void Connect(TcpClient aThis, string hostname, int port, [FieldAccess(Name = "System.Boolean System.Net.Sockets.TcpClient._active")] ref bool _active)
{
_clientSocket.Connect(IPAddress.Parse(hostname), port);
var address = IPAddress.Parse(hostname);
var endpoint = new IPEndPoint(address, port);

Cosmos.HAL.Global.debugger.Send("address - " + address.ToString());
_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_clientSocket.Bind(endpoint);
_clientSocket.Connect(address, port);
_active = true;
}

Expand Down

0 comments on commit c6bb978

Please sign in to comment.