diff --git a/source/Cosmos.System2_Plugs/System/Net/IPAddressImpl.cs b/source/Cosmos.System2_Plugs/System/Net/IPAddressImpl.cs index 8c4989a2df..7903553ec0 100644 --- a/source/Cosmos.System2_Plugs/System/Net/IPAddressImpl.cs +++ b/source/Cosmos.System2_Plugs/System/Net/IPAddressImpl.cs @@ -26,6 +26,10 @@ 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; @@ -33,14 +37,25 @@ public static void Ctor(IPAddress aThis, long address) public static void Ctor(IPAddress aThis, ReadOnlySpan address) { + Ctor(aThis, address.ToArray()); + } + + public static void Ctor(IPAddress aThis, ReadOnlySpan 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 { @@ -49,8 +64,38 @@ public static void Ctor(IPAddress aThis, ReadOnlySpan address) } } - public static void Ctor(IPAddress aThis, ReadOnlySpan 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); } } } diff --git a/source/Cosmos.System2_Plugs/System/Net/Sockets/SocketImpl.cs b/source/Cosmos.System2_Plugs/System/Net/Sockets/SocketImpl.cs index 6003a785af..18a18194a7 100644 --- a/source/Cosmos.System2_Plugs/System/Net/Sockets/SocketImpl.cs +++ b/source/Cosmos.System2_Plugs/System/Net/Sockets/SocketImpl.cs @@ -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; @@ -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); @@ -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); diff --git a/source/Cosmos.System2_Plugs/System/Net/Sockets/TcpClientImpl.cs b/source/Cosmos.System2_Plugs/System/Net/Sockets/TcpClientImpl.cs index 16b1698fec..1a37f9c9b6 100644 --- a/source/Cosmos.System2_Plugs/System/Net/Sockets/TcpClientImpl.cs +++ b/source/Cosmos.System2_Plugs/System/Net/Sockets/TcpClientImpl.cs @@ -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; }