From 2f376480f7a6fc8b5391f96b5b232f6a0489e88a Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 7 Jan 2016 09:04:42 +0100 Subject: [PATCH] Decode IPv4 UDP packets --- .../main/java/eu/faircode/netguard/Packet.java | 17 ++++++++++++++--- .../eu/faircode/netguard/SinkholeService.java | 3 +-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/Packet.java b/app/src/main/java/eu/faircode/netguard/Packet.java index 4fdf8abc..61b191f0 100644 --- a/app/src/main/java/eu/faircode/netguard/Packet.java +++ b/app/src/main/java/eu/faircode/netguard/Packet.java @@ -273,7 +273,18 @@ public class Packet { // https://en.wikipedia.org/wiki/User_Datagram_Protocol public class UDPHeader { + public int sourcePort; + public int destinationPort; + public int length; + public int checksum; + public UDPHeader(ByteBuffer buffer) { + int pos = buffer.position(); + + this.sourcePort = buffer.getShort() & 0xFFFF; + this.destinationPort = buffer.getShort() & 0xFFFF; + this.length = buffer.getShort() & 0xFFFF; + this.checksum = buffer.getShort() & 0xFFFF; } public void validate() throws IOException { @@ -492,15 +503,15 @@ public class Packet { } } - public int getUid4() { - if (this.TCP == null) + public int getUid() { + if (this.TCP == null && this.UDP == null) return -1; StringBuilder addr = new StringBuilder(); byte[] b = this.IPv4.sourceAddress.getAddress(); for (int i = b.length - 1; i >= 0; i--) addr.append(String.format("%02X", b[i])); - addr.append(':').append(String.format("%04X", this.TCP.sourcePort)); + addr.append(':').append(String.format("%04X", this.TCP == null ? this.UDP.sourcePort : this.TCP.sourcePort)); int uid = scanUid("0000000000000000FFFF0000" + addr.toString(), "/proc/net/tcp6"); if (uid < 0) diff --git a/app/src/main/java/eu/faircode/netguard/SinkholeService.java b/app/src/main/java/eu/faircode/netguard/SinkholeService.java index 1dcda19f..fca7d363 100644 --- a/app/src/main/java/eu/faircode/netguard/SinkholeService.java +++ b/app/src/main/java/eu/faircode/netguard/SinkholeService.java @@ -63,7 +63,6 @@ import android.widget.RemoteViews; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; -import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.nio.ByteBuffer; @@ -708,7 +707,7 @@ public class SinkholeService extends VpnService { version, pkt.IPv4.destinationAddress.toString(), pkt.IPv4.protocol, - pkt.getUid4()).close(); + pkt.getUid()).close(); } else if (version == 6) { buffer.position(24);