Пример #1
0
        /// <summary>
        /// 是否有效的状态值
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        internal static bool InvalidValue(this TcpConnectionState state)
        {
#if NET20 || NET30 || NET35
            foreach (var item in ConnectionStateValues)
            {
                if (item == state)
                {
                    return(true);
                }
            }

            return(false);
#else
            return(ConnectionStateValues.Any(p => p == state));
#endif
        }
Пример #2
0
        private void EndAccept(IAsyncResult ar)
        {
            var client = m_listener.EndAcceptSocket(ar);
            var state  = new TcpConnectionState
            {
                Client    = client,
                Scheduler = Scheduler,
                Stream    = this,
                Buffer    = new byte[4],
                ToRead    = 4,
                Offset    = 0
            };

            // start the io completion listening

            BeginReadHeader(state);
        }
Пример #3
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void sendTcpData(TcpConnectionState tcpConnectionState, byte[] data) throws java.io.EOFException
        private void sendTcpData(TcpConnectionState tcpConnectionState, sbyte[] data)
        {
            EtherFrame answerFrame = new EtherFrame();

            answerFrame.srcMac = tcpConnectionState.destinationMacAddress;
            answerFrame.dstMac = tcpConnectionState.sourceMacAddress;
            answerFrame.type   = ETHER_TYPE_IPv4;

            IPv4 answerIPv4 = new IPv4();

            answerIPv4.protocol             = IPv4_PROTOCOL_TCP;
            answerIPv4.sourceIPAddress      = tcpConnectionState.destinationIPAddress;
            answerIPv4.destinationIPAddress = tcpConnectionState.sourceIPAddress;

            TCP answerTcp = new TCP();

            answerTcp.sourcePort           = tcpConnectionState.destinationPort;
            answerTcp.destinationPort      = tcpConnectionState.sourcePort;
            answerTcp.sequenceNumber       = tcpConnectionState.destinationSequenceNumber;
            answerTcp.acknowledgmentNumber = tcpConnectionState.sourceSequenceNumber;
            answerTcp.flagACK = true;
            answerTcp.flagPSH = true;
            tcpConnectionState.destinationSequenceNumber += data.Length;
            answerTcp.data = data;

            // Update lengths and checksums
            answerTcp.computeChecksum(answerIPv4);
            answerIPv4.totalLength = answerIPv4.sizeOf() + answerTcp.sizeOf();
            answerIPv4.computeChecksum();

            // Write the different headers in sequence
            NetPacket answerPacket = new NetPacket(BUFFER_SIZE);

            answerFrame.write(answerPacket);
            answerIPv4.write(answerPacket);
            answerTcp.write(answerPacket);

            //if (log.DebugEnabled)
            {
                Console.WriteLine(string.Format("sendTcpData frame={0}", answerFrame));
                Console.WriteLine(string.Format("sendTcpData IPv4={0}", answerIPv4));
                Console.WriteLine(string.Format("sendTcpData TCP={0}", answerTcp));
            }

            sendPacket(answerPacket);
        }
Пример #4
0
 private static void BeginReadHeader(TcpConnectionState state)
 {
     state.Client.BeginReceive(state.Buffer, state.Offset, state.ToRead, SocketFlags.None, EndReadHeader, state);
 }
Пример #5
0
        public void ProcessPacket(TcpHeaderWithOptions header, ReadOnlySpan <byte> data)
        {
            // If there is backpressure just drop the packet
            if (!_flushTask.IsCompleted)
            {
                return;
            }

            _echoTimestamp = header.TimeStamp;
            switch (_state)
            {
            case TcpConnectionState.Listen:
                _maxSegmentSize        = header.MaximumSegmentSize <= 50 || header.MaximumSegmentSize > 1460 ? 1460 : header.MaximumSegmentSize;
                _sendSequenceNumber    = GetRandomSequenceStart();
                _receiveSequenceNumber = header.Header.SequenceNumber;
                // We know we checked for syn in the upper layer so we can ignore that for now
                _partialTcpHeader = Network.Header.Tcp.Create((ushort)LocalPort, (ushort)RemotePort);
                WriteSyncAckPacket();
                _state = TcpConnectionState.Syn_Rcvd;
                break;

            case TcpConnectionState.Syn_Rcvd:
                if (header.Header.SYN)
                {
                    //Console.WriteLine("Another Syn made");
                    return;
                }
                _connectionDispatcher.OnConnection(this);
                var ignore = TransmitLoop();
                _state = TcpConnectionState.Established;
                break;

            case TcpConnectionState.Established:
                if (header.Header.FIN)
                {
                    _receiveSequenceNumber++;
                    _state = TcpConnectionState.Fin_Wait_1;
                    return;
                }
                if (header.Header.RST)
                {
                    //Console.WriteLine("Got RST Should shut down!");
                    return;
                }

                // First lets update our acked squence number;
                _sendAckSequenceNumber = header.Header.AcknowledgmentNumber;

                if (_receiveSequenceNumber != header.Header.SequenceNumber)
                {
                    // We are just going to drop this and wait for a resend
                    return;
                }
                unchecked { _receiveSequenceNumber += (uint)data.Length; }
                var output = Input.GetMemory(data.Length);
                data.CopyTo(output.Span);
                Input.Advance(data.Length);
                // need to do something with the task and make sure we don't overlap the writes
                var task = Input.FlushAsync();
                if (!task.IsCompleted)
                {
                    _flushTask = task.AsTask();
                }
                PendingAck          = true;
                _totalBytesWritten += data.Length;
                break;

            case TcpConnectionState.Fin_Wait_1:
            case TcpConnectionState.Fin_Wait_2:
                break;

            default:
                throw new NotImplementedException($"Unknown tcp state?? {_state}");
            }
        }
Пример #6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void processMessageTCP(pspsharp.network.protocols.NetPacket packet, pspsharp.network.protocols.EtherFrame frame, pspsharp.network.protocols.IPv4 ipv4) throws java.io.EOFException
        private void processMessageTCP(NetPacket packet, EtherFrame frame, IPv4 ipv4)
        {
            TCP tcp = new TCP();

            tcp.read(packet);

            //if (log.DebugEnabled)
            {
                Console.WriteLine(string.Format("processMessageTCP {0}", tcp));
            }

            TcpConnectionState tcpConnectionState = getTcpConnectionState(ipv4, tcp);

            if (tcp.flagSYN)
            {
                if (tcpConnectionState != null)
                {
                    if (!tcpConnectionState.pendingConnection)
                    {
                        Console.WriteLine(string.Format("processMessageTCP SYN received but connection already exists: {0}", tcpConnectionState));
                        return;
                    }

                    //if (log.DebugEnabled)
                    {
                        Console.WriteLine(string.Format("processMessageTCP SYN received for a connection still pending ({0}), retrying the connection", tcpConnectionState));
                    }

                    try
                    {
                        tcpConnectionState.close();
                    }
                    catch (IOException e)
                    {
                        //if (log.DebugEnabled)
                        {
                            Console.WriteLine("error while closing connection", e);
                        }
                    }
                    tcpConnectionStates.Remove(tcpConnectionState);
                }

                tcpConnectionState = new TcpConnectionState();
                tcpConnectionState.sourceMacAddress          = frame.srcMac;
                tcpConnectionState.destinationMacAddress     = frame.dstMac;
                tcpConnectionState.sourceIPAddress           = ipv4.sourceIPAddress;
                tcpConnectionState.destinationIPAddress      = ipv4.destinationIPAddress;
                tcpConnectionState.sourcePort                = tcp.sourcePort;
                tcpConnectionState.destinationPort           = tcp.destinationPort;
                tcpConnectionState.sourceSequenceNumber      = tcp.sequenceNumber + tcp.data.Length;
                tcpConnectionState.destinationSequenceNumber = random.Next();
                tcpConnectionState.pendingConnection         = true;
                tcpConnectionStates.Add(tcpConnectionState);
            }
            else if (tcp.flagACK)
            {
                if (tcpConnectionState == null)
                {
                    // Acknowledge to an unknown connection, ignore
                    //if (log.DebugEnabled)
                    {
                        Console.WriteLine(string.Format("processMessageTCP ACK received for unknown connection: {0}", tcp));
                    }
                    return;
                }

                try
                {
                    if (tcp.flagFIN)
                    {
                        tcpConnectionState.sourceSequenceNumber += tcp.data.Length;
                        tcpConnectionState.sourceSequenceNumber++;
                        sendAcknowledgeTCP(tcpConnectionState, false);
                    }
                    else if (tcp.flagPSH)
                    {
                        // Acknowledge the reception of the data
                        tcpConnectionState.sourceSequenceNumber += tcp.data.Length;
                        sendAcknowledgeTCP(tcpConnectionState, false);

                        // Queue the received data for the destination
                        tcpConnectionState.addPendingWriteData(tcp.data);
                    }
                }
                catch (IOException e)
                {
                    Console.WriteLine("processMessageTCP", e);
                }
            }
        }