This commit is contained in:
valentinbreiz 2021-02-19 21:50:20 +01:00
parent f3c3aeb1d3
commit e25394e01d
2 changed files with 19 additions and 19 deletions

View file

@ -119,15 +119,13 @@ namespace Cosmos.System.Network.IPv4.TCP
/// </summary>
/// <param name="dest">Destination address.</param>
/// <param name="destPort">Destination port.</param>
public void Connect(Address dest, int destPort, int timeout = 5000)
public bool Connect(Address dest, int destPort, int timeout = 5000)
{
destination = dest;
destinationPort = destPort;
source = IPConfig.FindNetwork(dest);
global::System.Console.WriteLine(source.ToString());
byte[] options = new byte[]
{
0x02, 0x04, 0x05, 0xB4, 0x01, 0x03, 0x03, 0x08, 0x01, 0x01, 0x04, 0x02
@ -138,15 +136,15 @@ namespace Cosmos.System.Network.IPv4.TCP
ulong sequencenumber = (ulong)((rnd.Next(0, Int32.MaxValue)) << 32) | (ulong)(rnd.Next(0, Int32.MaxValue));
// Flags=0x02 -> Syn
var packet = new TCPPacket(source, dest, (ushort)localPort, (ushort)destPort, sequencenumber, 0, (ushort)(20 + options.Length), 0x02, 0xFAF0, 0, (ushort)options.Length, options);
var packet = new TCPPacket(source, destination, (ushort)localPort, (ushort)destPort, sequencenumber, 0, (ushort)(20 + options.Length), 0x02, 0xFAF0, 0, (ushort)options.Length, options);
OutgoingBuffer.AddPacket(packet);
NetworkStack.Update();
WaitForConnectionResponse(timeout);
return WaitForConnectionResponse(timeout);
}
private void WaitForConnectionResponse(int timeout)
private bool WaitForConnectionResponse(int timeout)
{
int second = 0;
int _deltaT = 0;
@ -155,7 +153,7 @@ namespace Cosmos.System.Network.IPv4.TCP
{
if (second > (timeout / 1000))
{
return;
return false;
}
if (_deltaT != RTC.Second)
{
@ -164,24 +162,30 @@ namespace Cosmos.System.Network.IPv4.TCP
}
}
var packet = new TCPPacket(rxBuffer.Dequeue().RawData);
var packet = rxBuffer.Dequeue();
if (packet.Flags == 0x12) // SYN/ACK
{
SendAck(packet);
Global.mDebugger.Send("TCP Connection established!");
Connected = true;
return true;
}
else
{
return false;
}
}
private void SendAck(TCPPacket responsepacket)
{
var packet = new TCPPacket(source, destination, (ushort)localPort, (ushort)localPort, responsepacket.AckNumber, responsepacket.SequenceNumber + 1, 20, 0x10, 0xFAF0, 0, 0);
var packet = new TCPPacket(source, destination, (ushort)localPort, (ushort)localPort, responsepacket.AckNumber, responsepacket.SequenceNumber + 1, 20, 0x10, 0xFAF0, 0);
OutgoingBuffer.AddPacket(packet);
NetworkStack.Update();
Global.mDebugger.Send("TCP Connection established!");
Connected = true;
}
/// <summary>

View file

@ -226,29 +226,25 @@ namespace Cosmos.System.Network.IPv4.TCP
/// <returns>byte array value.</returns>
internal byte[] MakeHeader()
{
/* Pseudo Header */
byte[] header = new byte[12 + (RawData.Length - DataOffset)];
/* Pseudo Header */
//Addresses
for (int b = 0; b < 4; b++)
{
header[0 + b] = SourceIP.address[b];
header[4 + b] = DestinationIP.address[b];
}
//Reserved
header[8] = 0x00;
//Protocol (TCP)
header[9] = 0x06;
ushort tcplen = (ushort)(RawData.Length - DataOffset);
//TCP Length
header[10] = (byte)((tcplen >> 8) & 0xFF);
header[11] = (byte)((tcplen >> 0) & 0xFF);
/** TCP Packet **/
/* TCP Packet */
for (int i = 0; i < RawData.Length - DataOffset; i++)
{
header[12 + i] = RawData[DataOffset + i];