Esempio n. 1
0
        private void ProcessConnect(SelectionKey key, List <SelectionKey> keyIterator)
        {
            TCB    tcb             = (TCB)key.Attachment();
            Packet referencePacket = tcb.referencePacket;

            try
            {
                if (tcb.channel.FinishConnect())
                {
                    if (keyIterator[0] != null)
                    {
                        keyIterator.RemoveAt(0);
                    }
                    tcb.status = TCBStatus.SYN_RECEIVED;

                    // TODO: Set MSS for receiving larger packets from the device
                    ByteBuffer responseBuffer = ByteBufferPool.acquire();
                    referencePacket.updateTCPBuffer(responseBuffer, (byte)(Packet.TCPHeader.SYN | Packet.TCPHeader.ACK),
                                                    tcb.mySequenceNum, tcb.myAcknowledgementNum, 0);
                    outputQueue.Offer(responseBuffer);

                    tcb.mySequenceNum++; // SYN counts as a byte
                    //key.InterestOps(Operations.Read);
                    key.InterestOps();
                }
            }
            catch (IOException e)
            {
                Log.Error(TAG, "Connection error: " + tcb.ipAndPort, e);
                ByteBuffer responseBuffer = ByteBufferPool.acquire();
                referencePacket.updateTCPBuffer(responseBuffer, (byte)Packet.TCPHeader.RST, 0, tcb.myAcknowledgementNum, 0);
                outputQueue.Offer(responseBuffer);
                TCB.CloseTCB(tcb);
            }
        }
Esempio n. 2
0
        private void InitializeConnection(Java.Lang.String ipAndPort, InetAddress destinationAddress, int destinationPort,
                                          Packet currentPacket, TCPHeader tcpHeader, ByteBuffer responseBuffer)

        {
            currentPacket.SwapSourceAndDestination();
            if (tcpHeader.isSYN())
            {
                SocketChannel outputChannel = SocketChannel.Open();
                outputChannel.ConfigureBlocking(false);
                vpnService.Protect(outputChannel.Socket());

                TCB tcb = new TCB(ipAndPort, random.NextInt(Short.MaxValue + 1), tcpHeader.sequenceNumber, tcpHeader.sequenceNumber + 1,
                                  tcpHeader.acknowledgementNumber, outputChannel, currentPacket);
                TCB.PutTCB(ipAndPort, tcb);

                try
                {
                    outputChannel.Connect(new InetSocketAddress(destinationAddress, destinationPort));
                    if (outputChannel.FinishConnect())
                    {
                        tcb.status = TCB.TCBStatus.SYN_RECEIVED;
                        // TODO: Set MSS for receiving larger packets from the device
                        currentPacket.updateTCPBuffer(responseBuffer, (byte)(TCPHeader.SYN | TCPHeader.ACK),
                                                      tcb.mySequenceNum, tcb.myAcknowledgementNum, 0);
                        tcb.mySequenceNum++; // SYN counts as a byte
                    }
                    else
                    {
                        tcb.status = TCB.TCBStatus.SYN_SENT;
                        selector.Wakeup();
                        tcb.selectionKey = outputChannel.Register(selector, SelectionKey.OpConnect, tcb);
                        return;
                    }
                }
                catch (IOException e)
                {
                    Log.Error(TAG, "Connection error: " + ipAndPort, e);
                    currentPacket.updateTCPBuffer(responseBuffer, (byte)TCPHeader.RST, 0, tcb.myAcknowledgementNum, 0);
                    TCB.CloseTCB(tcb);
                }
            }
            else
            {
                currentPacket.updateTCPBuffer(responseBuffer, (byte)TCPHeader.RST,
                                              0, tcpHeader.sequenceNumber + 1, 0);
            }

            outputQueue.Offer(responseBuffer);
        }
Esempio n. 3
0
        private void processInput(SelectionKey key, List <SelectionKey> keyIterator)
        {
            System.Console.WriteLine("TCP In");

            try
            {
                if (keyIterator[0] != null)
                {
                    keyIterator.RemoveAt(0);
                }

                ByteBuffer receiveBuffer = ByteBufferPool.acquire();
                // Leave space for the header
                receiveBuffer.Position(HEADER_SIZE);

                TCB tcb = (TCB)key.Attachment();
                lock (tcb)
                {
                    Packet        referencePacket = tcb.referencePacket;
                    SocketChannel inputChannel    = (SocketChannel)key.Channel();
                    int           readBytes;
                    try
                    {
                        readBytes = inputChannel.Read(receiveBuffer);
                    }
                    catch (IOException e)
                    {
                        Log.Error(TAG, "Network read error: " + tcb.ipAndPort, e);
                        referencePacket.updateTCPBuffer(receiveBuffer, (byte)Packet.TCPHeader.RST, 0, tcb.myAcknowledgementNum, 0);
                        outputQueue.Offer(receiveBuffer);
                        TCB.CloseTCB(tcb);
                        return;
                    }

                    if (readBytes == -1)
                    {
                        // End of stream, stop waiting until we push more data
                        //key.InterestOps(0);
                        key.InterestOps();
                        tcb.waitingForNetworkData = false;

                        if (tcb.status != TCBStatus.CLOSE_WAIT)
                        {
                            ByteBufferPool.Release(receiveBuffer);
                            return;
                        }

                        tcb.status = TCBStatus.LAST_ACK;
                        referencePacket.updateTCPBuffer(receiveBuffer, (byte)Packet.TCPHeader.FIN, tcb.mySequenceNum, tcb.myAcknowledgementNum, 0);
                        tcb.mySequenceNum++; // FIN counts as a byte
                    }
                    else
                    {
                        // XXX: We should ideally be splitting segments by MTU/MSS, but this seems to work without
                        referencePacket.updateTCPBuffer(receiveBuffer, (byte)(Packet.TCPHeader.PSH | Packet.TCPHeader.ACK),
                                                        tcb.mySequenceNum, tcb.myAcknowledgementNum, readBytes);
                        tcb.mySequenceNum += readBytes; // Next sequence number
                        receiveBuffer.Position(HEADER_SIZE + readBytes);
                    }
                }

                outputQueue.Offer(receiveBuffer);
            }
            catch (Java.Lang.Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }
        }
Esempio n. 4
0
 private void CloseCleanly(TCB tcb, ByteBuffer buffer)
 {
     ByteBufferPool.Release(buffer);
     TCB.CloseTCB(tcb);
 }
Esempio n. 5
0
 private void SendRST(TCB tcb, int prevPayloadSize, ByteBuffer buffer)
 {
     tcb.referencePacket.updateTCPBuffer(buffer, (byte)TCPHeader.RST, 0, tcb.myAcknowledgementNum + prevPayloadSize, 0);
     outputQueue.Offer(buffer);
     TCB.CloseTCB(tcb);
 }