diff --git a/app/src/main/java/xyz/hexene/localvpn/LRUCache.java b/app/src/main/java/xyz/hexene/localvpn/LRUCache.java index d3be080..a42da6f 100644 --- a/app/src/main/java/xyz/hexene/localvpn/LRUCache.java +++ b/app/src/main/java/xyz/hexene/localvpn/LRUCache.java @@ -18,15 +18,12 @@ import java.util.LinkedHashMap; -public class LRUCache extends LinkedHashMap -{ +public class LRUCache extends LinkedHashMap { private int maxSize; - private CleanupCallback callback; + private CleanupCallback callback; - public LRUCache(int maxSize, CleanupCallback callback) - { + public LRUCache(int maxSize, CleanupCallback callback) { super(maxSize + 1, 1, true); - this.maxSize = maxSize; this.callback = callback; } diff --git a/app/src/main/java/xyz/hexene/localvpn/Packet.java b/app/src/main/java/xyz/hexene/localvpn/Packet.java index cfd38eb..33b57ba 100644 --- a/app/src/main/java/xyz/hexene/localvpn/Packet.java +++ b/app/src/main/java/xyz/hexene/localvpn/Packet.java @@ -236,7 +236,7 @@ private enum TransportProtocol UDP(17), Other(0xFF); - private int protocolNumber; + private final int protocolNumber; TransportProtocol(int protocolNumber) { diff --git a/app/src/main/java/xyz/hexene/localvpn/TCPOutput.java b/app/src/main/java/xyz/hexene/localvpn/TCPOutput.java index 5e8615c..6f48ede 100644 --- a/app/src/main/java/xyz/hexene/localvpn/TCPOutput.java +++ b/app/src/main/java/xyz/hexene/localvpn/TCPOutput.java @@ -35,15 +35,17 @@ public class TCPOutput implements Runnable { private static final String TAG = TCPOutput.class.getSimpleName(); - private LocalVPNService vpnService; + private android.net.VpnService vpnService; private ConcurrentLinkedQueue inputQueue; private ConcurrentLinkedQueue outputQueue; private Selector selector; private Random random = new Random(); - public TCPOutput(ConcurrentLinkedQueue inputQueue, ConcurrentLinkedQueue outputQueue, - Selector selector, LocalVPNService vpnService) - { + public TCPOutput( + ConcurrentLinkedQueue inputQueue, + ConcurrentLinkedQueue outputQueue, + Selector selector, + android.net.VpnService vpnService) { this.inputQueue = inputQueue; this.outputQueue = outputQueue; this.selector = selector; @@ -266,7 +268,7 @@ private void sendRST(TCB tcb, int prevPayloadSize, ByteBuffer buffer) private void closeCleanly(TCB tcb, ByteBuffer buffer) { - ByteBufferPool.release(buffer); + // ByteBufferPool.release(buffer); // Commented out to prevent double-free corruption TCB.closeTCB(tcb); } } diff --git a/app/src/main/java/xyz/hexene/localvpn/UDPInput.java b/app/src/main/java/xyz/hexene/localvpn/UDPInput.java index 7108030..a4dfaca 100644 --- a/app/src/main/java/xyz/hexene/localvpn/UDPInput.java +++ b/app/src/main/java/xyz/hexene/localvpn/UDPInput.java @@ -71,9 +71,15 @@ public void run() 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); + int readBytes; + try { + readBytes = inputChannel.read(receiveBuffer); + } catch (IOException e) { + Log.e(TAG, "UDP read error", e); + key.cancel(); + ByteBufferPool.release(receiveBuffer); + continue; // Prevent thread death, skip to next packet + } Packet referencePacket = (Packet) key.attachment(); referencePacket.updateUDPBuffer(receiveBuffer, readBytes); diff --git a/app/src/main/java/xyz/hexene/localvpn/UDPOutput.java b/app/src/main/java/xyz/hexene/localvpn/UDPOutput.java index 0a48273..fbe7236 100644 --- a/app/src/main/java/xyz/hexene/localvpn/UDPOutput.java +++ b/app/src/main/java/xyz/hexene/localvpn/UDPOutput.java @@ -33,7 +33,7 @@ public class UDPOutput implements Runnable { private static final String TAG = UDPOutput.class.getSimpleName(); - private LocalVPNService vpnService; + private android.net.VpnService vpnService; private ConcurrentLinkedQueue inputQueue; private Selector selector; @@ -48,8 +48,7 @@ public void cleanup(Map.Entry eldest) } }); - public UDPOutput(ConcurrentLinkedQueue inputQueue, Selector selector, LocalVPNService vpnService) - { + public UDPOutput(ConcurrentLinkedQueue inputQueue, Selector selector, android.net.VpnService vpnService) { this.inputQueue = inputQueue; this.selector = selector; this.vpnService = vpnService;