Skip to content

Commit

Permalink
Add RemoteEndPoint and LocalEndPoint properties to TcpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinbreiz committed Aug 19, 2021
1 parent 813062f commit 426ef62
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 53 deletions.
20 changes: 11 additions & 9 deletions source/Cosmos.System2/Network/IPv4/EndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ public class EndPoint : IComparable
/// <summary>
/// Address.
/// </summary>
public Address address;
public Address Address;
/// <summary>
/// Port.
/// </summary>
public UInt16 port;
public ushort Port;

/// <summary>
/// Create new instance of the <see cref="EndPoint"/> class.
/// </summary>
/// <param name="addr">Adress.</param>
/// <param name="port">Port.</param>
public EndPoint(Address addr, UInt16 port)
public EndPoint(Address addr, ushort port)
{
this.address = addr;
this.port = port;
Address = addr;
Port = port;
}

/// <summary>
Expand All @@ -40,7 +40,7 @@ public EndPoint(Address addr, UInt16 port)
/// <returns>string value.</returns>
public override string ToString()
{
return this.address.ToString() + ":" + this.port.ToString();
return Address.ToString() + ":" + Port.ToString();
}

/// <summary>
Expand All @@ -53,17 +53,19 @@ public int CompareTo(object obj)
{
if (obj is EndPoint)
{
EndPoint other = (EndPoint)obj;
if ((other.address.CompareTo(this.address) != 0) ||
(other.port != this.port))
var other = (EndPoint)obj;

if ((other.Address.CompareTo(Address) != 0) || (other.Port != Port))
{
return -1;
}

return 0;
}
else
{
throw new ArgumentException("obj is not a IPv4EndPoint", "obj");
}
}
}
}
2 changes: 1 addition & 1 deletion source/Cosmos.System2/Network/IPv4/ICMPClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public int Receive(ref EndPoint source, int timeout = 5000)
}

ICMPEchoReply packet = new ICMPEchoReply(rxBuffer.Dequeue().RawData);
source.address = packet.SourceIP;
source.Address = packet.SourceIP;

return second;
}
Expand Down
50 changes: 37 additions & 13 deletions source/Cosmos.System2/Network/IPv4/TCP/TCPClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public TcpClient(int localPort)
public TcpClient(Address dest, int destPort)
: this(0)
{
StateMachine.RemoteAddress = dest;
StateMachine.RemotePort = (ushort)destPort;
StateMachine.RemoteEndPoint.Address = dest;
StateMachine.RemoteEndPoint.Port = (ushort)destPort;
}

/// <summary>
Expand All @@ -78,9 +78,9 @@ public void Connect(Address dest, int destPort, int timeout = 5000)
throw new Exception("Client must be closed before setting a new connection.");
}

StateMachine.RemoteAddress = dest;
StateMachine.LocalAddress = IPConfig.FindNetwork(dest);
StateMachine.RemotePort = (ushort)destPort;
StateMachine.RemoteEndPoint.Address = dest;
StateMachine.LocalEndPoint.Address = IPConfig.FindNetwork(dest);
StateMachine.RemoteEndPoint.Port = (ushort)destPort;

//Generate Random Sequence Number
var rnd = new Random();
Expand Down Expand Up @@ -135,7 +135,7 @@ public void Close()

for (int i = 0; i < Tcp.Connections.Count; i++)
{
if (Tcp.Connections[i].Equals(StateMachine.LocalPort, StateMachine.RemotePort, StateMachine.LocalAddress, StateMachine.RemoteAddress))
if (Tcp.Connections[i].Equals(StateMachine.LocalEndPoint.Port, StateMachine.RemoteEndPoint.Port, StateMachine.LocalEndPoint.Address, StateMachine.RemoteEndPoint.Address))
{
Tcp.Connections.RemoveAt(i);
return;
Expand All @@ -154,7 +154,7 @@ public void Close()
/// <exception cref="Exception">Thrown if TCP Status is not ESTABLISHED.</exception>
public void Send(byte[] data)
{
if ((StateMachine.RemoteAddress == null) || (StateMachine.RemotePort == 0))
if ((StateMachine.RemoteEndPoint.Address == null) || (StateMachine.RemoteEndPoint.Port == 0))
{
throw new InvalidOperationException("Must establish a default remote host by calling Connect() before using this Send() overload");
}
Expand All @@ -168,7 +168,7 @@ public void Send(byte[] data)

for (int i = 0; i < chunks.Length; i++)
{
var packet = new TCPPacket(StateMachine.LocalAddress, StateMachine.RemoteAddress, StateMachine.LocalPort, StateMachine.RemotePort, StateMachine.TCB.SndNxt, StateMachine.TCB.RcvNxt, 20, i == chunks.Length - 2 ? (byte)(Flags.PSH | Flags.ACK) : (byte)(Flags.ACK), StateMachine.TCB.SndWnd, 0, chunks[i]);
var packet = new TCPPacket(StateMachine.LocalEndPoint.Address, StateMachine.RemoteEndPoint.Address, StateMachine.LocalEndPoint.Port, StateMachine.RemoteEndPoint.Port, StateMachine.TCB.SndNxt, StateMachine.TCB.RcvNxt, 20, i == chunks.Length - 2 ? (byte)(Flags.PSH | Flags.ACK) : (byte)(Flags.ACK), StateMachine.TCB.SndWnd, 0, chunks[i]);
OutgoingBuffer.AddPacket(packet);
NetworkStack.Update();

Expand All @@ -177,7 +177,7 @@ public void Send(byte[] data)
}
else
{
var packet = new TCPPacket(StateMachine.LocalAddress, StateMachine.RemoteAddress, StateMachine.LocalPort, StateMachine.RemotePort, StateMachine.TCB.SndNxt, StateMachine.TCB.RcvNxt, 20, (byte)(Flags.PSH | Flags.ACK), StateMachine.TCB.SndWnd, 0, data);
var packet = new TCPPacket(StateMachine.LocalEndPoint.Address, StateMachine.RemoteEndPoint.Address, StateMachine.LocalEndPoint.Port, StateMachine.RemoteEndPoint.Port, StateMachine.TCB.SndNxt, StateMachine.TCB.RcvNxt, 20, (byte)(Flags.PSH | Flags.ACK), StateMachine.TCB.SndWnd, 0, data);
OutgoingBuffer.AddPacket(packet);
NetworkStack.Update();

Expand All @@ -204,8 +204,8 @@ public byte[] NonBlockingReceive(ref EndPoint source)
}

var packet = StateMachine.rxBuffer.Dequeue();
source.address = packet.SourceIP;
source.port = packet.SourcePort;
source.Address = packet.SourceIP;
source.Port = packet.SourcePort;

var tmp = StateMachine.Data;
StateMachine.Data = null;
Expand All @@ -230,14 +230,38 @@ public byte[] Receive(ref EndPoint source)
}

var packet = StateMachine.rxBuffer.Dequeue();
source.address = packet.SourceIP;
source.port = packet.SourcePort;
source.Address = packet.SourceIP;
source.Port = packet.SourcePort;

var tmp = StateMachine.Data;
StateMachine.Data = null;
return tmp;
}

/// <summary>
/// Get distant computer EndPoint (IP adress and port).
/// </summary>
/// <returns>Remote EndPoint.</returns>
public EndPoint RemoteEndPoint
{
get
{
return StateMachine.RemoteEndPoint;
}
}

/// <summary>
/// Get local computer EndPoint (IP adress and port).
/// </summary>
/// <returns>Remote EndPoint.</returns>
public EndPoint LocalEndPoint
{
get
{
return StateMachine.LocalEndPoint;
}
}

/// <summary>
/// Is TCP Connected.
/// </summary>
Expand Down
39 changes: 13 additions & 26 deletions source/Cosmos.System2/Network/IPv4/TCP/Tcp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ internal static Tcp GetConnection(ushort localPort, ushort remotePort, Address l
{
return con;
}
else if (con.LocalPort.Equals(localPort) && con.Status == Status.LISTEN)
else if (con.LocalEndPoint.Port.Equals(localPort) && con.Status == Status.LISTEN)
{
return con;
}
Expand All @@ -208,24 +208,14 @@ internal static Tcp GetConnection(ushort localPort, ushort remotePort, Address l
#region TCB

/// <summary>
/// Local port.
/// Local EndPoint.
/// </summary>
internal ushort LocalPort;
internal EndPoint LocalEndPoint;

/// <summary>
/// Destination port.
/// Remote EndPoint.
/// </summary>
internal ushort RemotePort;

/// <summary>
/// Source address.
/// </summary>
internal Address LocalAddress;

/// <summary>
/// Destination address.
/// </summary>
internal Address RemoteAddress;
internal EndPoint RemoteEndPoint;

/// <summary>
/// Connection Transmission Control Block.
Expand All @@ -251,11 +241,8 @@ internal static Tcp GetConnection(ushort localPort, ushort remotePort, Address l

public Tcp(ushort localPort, ushort remotePort, Address localIp, Address remoteIp)
{
LocalPort = localPort;
RemotePort = remotePort;
LocalAddress = localIp;
RemoteAddress = remoteIp;

LocalEndPoint = new EndPoint(localIp, localPort);
RemoteEndPoint = new EndPoint(remoteIp, remotePort);
TCB = new TransmissionControlBlock();
}

Expand Down Expand Up @@ -357,9 +344,9 @@ public void ProcessListen(TCPPacket packet)
}
else if (packet.SYN)
{
LocalAddress = IPConfig.FindNetwork(packet.SourceIP);
RemoteAddress = packet.SourceIP;
RemotePort = packet.SourcePort;
LocalEndPoint.Address = IPConfig.FindNetwork(packet.SourceIP);
RemoteEndPoint.Address = packet.SourceIP;
RemoteEndPoint.Port = packet.SourcePort;

var rnd = new Random();
var sequenceNumber = (uint)((rnd.Next(0, Int32.MaxValue)) << 32) | (uint)(rnd.Next(0, Int32.MaxValue));
Expand Down Expand Up @@ -683,15 +670,15 @@ internal bool WaitStatus(Status status)
/// </summary>
internal void SendEmptyPacket(Flags flag)
{
SendPacket(new TCPPacket(LocalAddress, RemoteAddress, LocalPort, RemotePort, TCB.SndNxt, TCB.RcvNxt, 20, (byte)flag, TCB.SndWnd, 0));
SendPacket(new TCPPacket(LocalEndPoint.Address, RemoteEndPoint.Address, LocalEndPoint.Port, RemoteEndPoint.Port, TCB.SndNxt, TCB.RcvNxt, 20, (byte)flag, TCB.SndWnd, 0));
}

/// <summary>
/// Send empty packet.
/// </summary>
internal void SendEmptyPacket(Flags flag, uint sequenceNumber)
{
SendPacket(new TCPPacket(LocalAddress, RemoteAddress, LocalPort, RemotePort, sequenceNumber, TCB.RcvNxt, 20, (byte)flag, TCB.SndWnd, 0));
SendPacket(new TCPPacket(LocalEndPoint.Address, RemoteEndPoint.Address, LocalEndPoint.Port, RemoteEndPoint.Port, sequenceNumber, TCB.RcvNxt, 20, (byte)flag, TCB.SndWnd, 0));
}

/// <summary>
Expand All @@ -713,7 +700,7 @@ private void SendPacket(TCPPacket packet)
/// </summary>
internal bool Equals(ushort localPort, ushort remotePort, Address localIp, Address remoteIp)
{
return LocalPort.Equals(localPort) && RemotePort.Equals(remotePort) && LocalAddress.Hash.Equals(localIp.Hash) && RemoteAddress.Hash.Equals(remoteIp.Hash);
return LocalEndPoint.Port.Equals(localPort) && RemoteEndPoint.Port.Equals(remotePort) && LocalEndPoint.Address.Hash.Equals(localIp.Hash) && RemoteEndPoint.Address.Hash.Equals(remoteIp.Hash);
}

#endregion
Expand Down
8 changes: 4 additions & 4 deletions source/Cosmos.System2/Network/IPv4/UDP/UdpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ public byte[] NonBlockingReceive(ref EndPoint source)
}

var packet = new UDPPacket(rxBuffer.Dequeue().RawData);
source.address = packet.SourceIP;
source.port = packet.SourcePort;
source.Address = packet.SourceIP;
source.Port = packet.SourcePort;

return packet.UDP_Data;
}
Expand All @@ -197,8 +197,8 @@ public byte[] Receive(ref EndPoint source)
while (rxBuffer.Count < 1) ;

var packet = new UDPPacket(rxBuffer.Dequeue().RawData);
source.address = packet.SourceIP;
source.port = packet.SourcePort;
source.Address = packet.SourceIP;
source.Port = packet.SourcePort;

return packet.UDP_Data;
}
Expand Down

0 comments on commit 426ef62

Please sign in to comment.