Decode IPv4 UDP packets

This commit is contained in:
M66B 2016-01-07 09:04:42 +01:00
parent ac8409328e
commit 2f376480f7
2 changed files with 15 additions and 5 deletions

View File

@ -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)

View File

@ -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);