Esempio n. 1
0
        public void Close()
        {
            //Apps.System.Debugger.debugger.Send("Closing TCP Connection...");

            IPV4.Address source = IPV4.Config.FindNetwork(this.destination);

            TCPConnection.Connection connection = new TCPConnection.Connection();

            //Apps.System.Debugger.debugger.Send("Closing " + destination.ToString() + ":" + this.destinationPort + "now!");

            connection.dest   = this.destination;
            connection.source = source;

            connection.localPort = (ushort)this.localPort;
            connection.destPort  = (ushort)this.destinationPort;

            connection.acknowledgmentnb = lastack;

            connection.WSValue = 1024;

            connection.sequencenumber = lastsn;

            connection.Checksum = 0x0000;

            connection.Flags = 0x11;

            connection.Send(false);

            readytosend = false;

            //Apps.System.Debugger.debugger.Send("Closed!");
        }
Esempio n. 2
0
        public void Send(byte[] data, IPV4.Address dest, Int32 destPort)
        {
            IPV4.Address source = IPV4.Config.FindNetwork(dest);

            //Console.WriteLine("Sending " + "TCP Packet Src=" + source + ":" + localPort + ", Dest=" + dest + ":" + destPort + ", DataLen=" + data.Length);

            TCPConnection.Connection connection = new TCPConnection.Connection();

            connection.dest   = dest;
            connection.source = source;

            connection.localPort = (ushort)localPort;
            connection.destPort  = (ushort)destPort;

            connection.acknowledgmentnb = lastack;

            connection.WSValue = 1024;

            connection.sequencenumber = lastsn;

            connection.Checksum = 0x0000;

            connection.Flags = 0x18;

            connection.data = data;

            connection.Send(true);

            readytosend = false;
        }
Esempio n. 3
0
        public void Connect(IPV4.Address dest, Int32 destPort)
        {
            this.destination     = dest;
            this.destinationPort = destPort;

            TCPConnection.Connection connection = new TCPConnection.Connection();
            Address source = Config.FindNetwork(dest);

            ulong CID = (ulong)(dest.Hash + this.localPort + destPort);

            TCPConnection.Connections.Add((uint)CID, connection);

            connection.CID = CID;

            //Apps.System.Debugger.debugger.Send(CID.ToString());

            connection.dest   = dest;
            connection.source = source;

            connection.localPort = (ushort)localPort;
            connection.destPort  = (ushort)destPort;

            connection.acknowledgmentnb = 0x0000;

            connection.WSValue = 1024;

            connection.sequencenumber = 3455719727;

            connection.Checksum = 0x0000;

            connection.Flags = 0x02;

            connection.Send(false);

            int _deltaT = 0;
            int second  = 0;

            while (!connection.IsOpen)
            {
                if (_deltaT != Cosmos.HAL.RTC.Second)
                {
                    second++;
                    _deltaT = Cosmos.HAL.RTC.Second;
                }

                if (second >= 4)
                {
                    //Apps.System.Debugger.debugger.Send("No response in 4 secondes...");
                    break;
                }
            }

            //Apps.System.Debugger.debugger.Send("Connected!!");
        }
Esempio n. 4
0
        internal static void TCPHandler(byte[] packetData)
        {
            TCPPacket tcp_packet = new TCPPacket(packetData);

            if (Firewall.TCP.TCPIncomingFilter(tcp_packet))
            {
                //"=== [FIREWALL] TCP INCOMING PACKET BLOCKED " + tcp_packet.SourceIP.ToString() + ":" + tcp_packet.SourcePort.ToString() + " ===
                return;
            }

            foreach (Drivers.NetworkDevice device in NetworkConfig.Keys)
            {
                if (device.Name == "PCNETII")
                {
                    if (packetData.Length == 64 && tcp_packet.ipLength < 50)
                    {
                        byte[] rdata = tcp_packet.mRawData;
                        int lenght = 64 - (64 - (tcp_packet.ipLength + 14));
                        tcp_packet.tcpLen = (ushort)(lenght - 34);
                        tcp_packet.mRawData = new byte[lenght];
                        for (int b = 0; b <= lenght; b++)
                        {
                            tcp_packet.mRawData[b] = rdata[b];
                        }
                    }
                }
                if (device.Name == "RTL8168")
                {
                    byte[] rdata = tcp_packet.mRawData;
                    int lenght = 64 - (64 - (tcp_packet.ipLength + 14));
                    tcp_packet.tcpLen = (ushort)(lenght - 34);
                    tcp_packet.mRawData = new byte[lenght];
                    for (int b = 0; b <= lenght; b++)
                    {
                        tcp_packet.mRawData[b] = rdata[b];
                    }
                }
            }

            //=== Received TCP packet from " + tcp_packet.SourceIP.ToString() + ":" + tcp_packet.SourcePort.ToString() + " Len:" + tcp_packet.tcpLen.ToString() + "===

            if (CheckCRC(tcp_packet))
            {

                bool SYN = (packetData[47] & (1 << 1)) != 0;
                bool ACK = (packetData[47] & (1 << 4)) != 0;
                bool FIN = (packetData[47] & (1 << 0)) != 0;
                bool PSH = (packetData[47] & (1 << 3)) != 0;
                bool RST = (packetData[47] & (1 << 2)) != 0;

                ulong CID = tcp_packet.SourceIP.Hash + tcp_packet.sourcePort + tcp_packet.destPort;

                TCPConnection.Connection connection = new TCPConnection.Connection();

                if (SYN && !ACK)
                {

                    //FLAG: SYN, New connection

                    if (TCPConnection.Connections.ContainsKey((uint)CID))
                    {
                        TCPConnection.Connections.Remove((uint)CID);
                    }
                    else
                    {
                        connection = new TCPConnection.Connection();
                    }

                    TCPConnection.Connections.Add((uint)CID, connection);

                    connection.CID = CID;

                    connection.dest = tcp_packet.sourceIP;
                    connection.source = tcp_packet.destIP;

                    connection.localPort = tcp_packet.destPort;
                    connection.destPort = tcp_packet.sourcePort;

                    connection.acknowledgmentnb = tcp_packet.sequencenumber + 1;

                    connection.WSValue = 1024;

                    connection.sequencenumber = 3455719727;

                    connection.Checksum = 0x0000;

                    connection.Flags = 0x12;

                    connection.Send(false);

                    connection.IsOpen = true;

                    return;
                }
                else if (SYN && ACK)
                {
                    connection.dest = tcp_packet.sourceIP;
                    connection.source = tcp_packet.destIP;

                    connection.localPort = tcp_packet.destPort;
                    connection.destPort = tcp_packet.sourcePort;

                    connection.acknowledgmentnb = tcp_packet.sequencenumber + 1;

                    connection.WSValue = 1024;

                    connection.sequencenumber = tcp_packet.acknowledgmentnb;

                    connection.Checksum = 0x0000;

                    connection.Flags = 0x10;

                    connection.Send(false);

                    TCPClient.lastack = tcp_packet.sequencenumber + 1;
                    TCPClient.lastsn = tcp_packet.acknowledgmentnb;

                    TCPConnection.Connections[(uint)CID].IsOpen = true;
                }
                else if ((FIN || RST) && ACK && TCPConnection.Connections[(uint)CID].IsOpen)
                {
                    //FLAG: FIN, ACK, Disconnected by host!!!

                    if (tcp_packet.sourcePort == 4224 && tcp_packet.destPort == 4224)
                    {
                        Console.WriteLine("Debugger closed by client computer!");
                        //Kernel.debugger.Stop();
                    }

                    connection.dest = tcp_packet.sourceIP;
                    connection.source = tcp_packet.destIP;

                    connection.localPort = tcp_packet.destPort;
                    connection.destPort = tcp_packet.sourcePort;

                    connection.acknowledgmentnb = tcp_packet.sequencenumber + 1;

                    connection.WSValue = 1024;

                    connection.sequencenumber = tcp_packet.acknowledgmentnb;

                    connection.Checksum = 0x0000;

                    connection.Flags = 0x10;

                    connection.Send(false);

                    connection.Flags = 0x11;

                    connection.Send(false);

                    connection.Close();

                    return;
                }
                else if (PSH && ACK && TCPConnection.Connections[(uint)CID].IsOpen)
                {
                    //FLAG: PSH, ACK, Data received!? :D
                    TCPClient receiver = TCPClient.Client(tcp_packet.DestinationPort);
                    if (receiver != null)
                    {
                        receiver.receiveData(tcp_packet);
                    }

                    connection.dest = tcp_packet.sourceIP;
                    connection.source = tcp_packet.destIP;

                    connection.localPort = tcp_packet.destPort;
                    connection.destPort = tcp_packet.sourcePort;

                    connection.acknowledgmentnb = tcp_packet.sequencenumber + (ulong)tcp_packet.TCP_Data.Length;

                    connection.WSValue = 1024;

                    connection.sequencenumber = tcp_packet.acknowledgmentnb;

                    connection.Checksum = 0x0000;

                    connection.Flags = 0x10;

                    connection.Send(false);

                    connection.IsOpen = false;

                    return;
                }
                else if (RST && ACK)
                {
                    //Apps.System.Debugger.debugger.Send("FLAG: RST, ACK, Port not listening on remote machine?");
                    return;
                }
                else if (RST)
                {
                    //Apps.System.Debugger.debugger.Send("FLAG: RST, Connection aborted by host.");
                    return;
                }
                else if (ACK && TCPConnection.Connections[(uint)CID].IsOpen)
                {
                    TCPClient.lastack = tcp_packet.sequencenumber;
                    TCPClient.lastsn = tcp_packet.acknowledgmentnb;
                    TCPClient.readytosend = true;
                    return;
                }
                else
                {
                    //Apps.System.Debugger.debugger.Send("What is going wrong? D:");
                }
            }
            else
            {
                //Apps.System.Debugger.debugger.Send("But checksum is incorrect... Packet Passed.");

                TCPConnection.Connection connection = new TCPConnection.Connection();

                connection.dest = tcp_packet.sourceIP;
                connection.source = tcp_packet.destIP;

                connection.localPort = tcp_packet.destPort;
                connection.destPort = tcp_packet.sourcePort;

                connection.acknowledgmentnb = 0x0000;

                connection.WSValue = 1024;

                connection.sequencenumber = tcp_packet.acknowledgmentnb;

                connection.Checksum = 0x0000;

                connection.Flags = 0x04;

                connection.Send(false);

                connection.Close();

                return;
            }
        }