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); } }
private void Cleanup() { deviceToNetworkTCPQueue = null; deviceToNetworkUDPQueue = null; networkToDeviceQueue = null; ByteBufferPool.Clear(); CloseResources(udpSelector, tcpSelector, vpnInterface); }
public void Run() { try { Log.Info(TAG, "Started"); while (!Thread.Interrupted()) { int readyChannels = selector.Select(); if (readyChannels == 0) { Thread.Sleep(10); continue; } System.Console.WriteLine("UDP In"); var keys = selector.SelectedKeys().ToList(); foreach (var key in keys) { if (!Thread.Interrupted()) { if (key.IsValid && key.IsReadable) { selector.SelectedKeys().Remove(key); ByteBuffer receiveBuffer = ByteBufferPool.acquire(); // Leave space for the header receiveBuffer.Position(HEADER_SIZE); DatagramChannel inputChannel = (DatagramChannel)key.Channel(); // XXX: We should handle any IOExceptions here immediately, // but that probably won't happen with UDP int readBytes = inputChannel.Read(receiveBuffer); Packet referencePacket = (Packet)key.Attachment(); referencePacket.updateUDPBuffer(receiveBuffer, readBytes); receiveBuffer.Position(HEADER_SIZE + readBytes); outputQueue.Offer(receiveBuffer); } } } } } catch (InterruptedException e) { Log.Info(TAG, "Stopping"); } catch (IOException e) { Log.Warn(TAG, e.ToString(), e); } }
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); } }
public void Run() { Log.Info(TAG, "Started"); try { Thread currentThread = Thread.CurrentThread(); while (true) { Packet currentPacket; // TODO: Block when not connected do { currentPacket = (Packet)inputQueue.Poll(); if (currentPacket != null) { break; } Thread.Sleep(10); } while (!currentThread.IsInterrupted); if (currentThread.IsInterrupted) { break; } ByteBuffer payloadBuffer = currentPacket.backingBuffer; currentPacket.backingBuffer = null; ByteBuffer responseBuffer = ByteBufferPool.acquire(); InetAddress destinationAddress = currentPacket.ip4Header.destinationAddress; TCPHeader tcpHeader = currentPacket.tcpHeader; int destinationPort = tcpHeader.destinationPort; int sourcePort = tcpHeader.sourcePort; Java.Lang.String ipAndPort = new Java.Lang.String(destinationAddress.HostAddress + ":" + destinationPort + ":" + sourcePort); System.Console.WriteLine("TCP Out: " + ipAndPort); TCB tcb = TCB.GetTCB(ipAndPort); if (tcb == null) { InitializeConnection(ipAndPort, destinationAddress, destinationPort, currentPacket, tcpHeader, responseBuffer); } else if (tcpHeader.isSYN()) { processDuplicateSYN(tcb, tcpHeader, responseBuffer); } else if (tcpHeader.isRST()) { CloseCleanly(tcb, responseBuffer); } else if (tcpHeader.isFIN()) { processFIN(tcb, tcpHeader, responseBuffer); } else if (tcpHeader.isACK()) { processACK(tcb, tcpHeader, payloadBuffer, responseBuffer); } // XXX: cleanup later if (responseBuffer.Position() == 0) { ByteBufferPool.Release(responseBuffer); } ByteBufferPool.Release(payloadBuffer); } } catch (InterruptedException e) { Log.Info(TAG, "Stopping"); } catch (IOException e) { Log.Error(TAG, e.ToString(), e); } finally { TCB.CloseAll(); } }
private void CloseCleanly(TCB tcb, ByteBuffer buffer) { ByteBufferPool.Release(buffer); TCB.CloseTCB(tcb); }
public void Run() { Log.Info(TAG, "Started"); FileChannel vpnInput = new FileInputStream(vpnFileDescriptor).Channel; FileChannel vpnOutput = new FileOutputStream(vpnFileDescriptor).Channel; try { ByteBuffer bufferToNetwork = null; bool dataSent = true; bool dataReceived; while (!Thread.Interrupted()) { if (dataSent) { bufferToNetwork = ByteBufferPool.acquire(); } else { bufferToNetwork.Clear(); } // TODO: Block when not connected int readBytes = vpnInput.Read(bufferToNetwork); if (readBytes > 0) { dataSent = true; bufferToNetwork.Flip(); Packet packet = new Packet(bufferToNetwork); if (packet.IsUDP) { deviceToNetworkUDPQueue.Offer(packet); System.Console.WriteLine("-> Device (UDP) to network write"); } else if (packet.IsTCP) { deviceToNetworkTCPQueue.Offer(packet); System.Console.WriteLine("-> Device (TCP) to network write"); } else { Log.Warn(TAG, "Unknown packet type"); Log.Warn(TAG, packet.ip4Header.ToString()); dataSent = false; } } else { dataSent = false; } ByteBuffer bufferFromNetwork = (ByteBuffer)networkToDeviceQueue.Poll(); if (bufferFromNetwork != null) { bufferFromNetwork.Flip(); while (bufferFromNetwork.HasRemaining) { try { vpnOutput.Write(bufferFromNetwork); } catch (Exception ex) { bufferFromNetwork.Clear(); System.Console.WriteLine(ex.Message); } } dataReceived = true; System.Console.WriteLine("<- Network to device write"); ByteBufferPool.Release(bufferFromNetwork); } else { dataReceived = false; } // TODO: Sleep-looping is not very battery-friendly, consider blocking instead // Confirm if throughput with ConcurrentQueue is really higher compared to BlockingQueue if (!dataSent && !dataReceived) { Thread.Sleep(10); } } } catch (InterruptedException e) { Log.Info(TAG, "Stopping"); } catch (IOException e) { Log.Warn(TAG, e.ToString(), e); } finally { CloseResources(vpnInput, vpnOutput); } }
public void Run() { Log.Info(TAG, "Started"); try { Thread currentThread = Thread.CurrentThread(); while (true) { Packet currentPacket; // TODO: Block when not connected do { currentPacket = (Packet)inputQueue.Poll(); if (currentPacket != null) { break; } Thread.Sleep(10); } while (!currentThread.IsInterrupted); if (currentThread.IsInterrupted) { break; } InetAddress destinationAddress = currentPacket.ip4Header.destinationAddress; int destinationPort = currentPacket.udpHeader.destinationPort; int sourcePort = currentPacket.udpHeader.sourcePort; Java.Lang.String ipAndPort = new Java.Lang.String(destinationAddress.HostAddress + ":" + destinationPort + ":" + sourcePort); System.Console.WriteLine("UDP Out: " + ipAndPort); DatagramChannel outputChannel = (DatagramChannel)channelCache.Get(ipAndPort); if (outputChannel == null) { outputChannel = DatagramChannel.Open(); vpnService.Protect(outputChannel.Socket()); try { outputChannel.Connect(new InetSocketAddress(destinationAddress, destinationPort)); } catch (IOException e) { Log.Error(TAG, "Connection error: " + ipAndPort, e); closeChannel(outputChannel); ByteBufferPool.Release(currentPacket.backingBuffer); continue; } outputChannel.ConfigureBlocking(false); currentPacket.SwapSourceAndDestination(); selector.Wakeup(); outputChannel.Register(selector, SelectionKey.OpRead, currentPacket); channelCache.Put(ipAndPort, outputChannel); } try { ByteBuffer payloadBuffer = currentPacket.backingBuffer; while (payloadBuffer.HasRemaining) { outputChannel.Write(payloadBuffer); } } catch (IOException e) { Log.Error(TAG, "Network write error: " + ipAndPort, e); channelCache.Remove(ipAndPort); closeChannel(outputChannel); } ByteBufferPool.Release(currentPacket.backingBuffer); } } catch (InterruptedException e) { Log.Info(TAG, "Stopping"); } catch (IOException e) { Log.Info(TAG, e.ToString(), e); } finally { closeAll(); } }