Skip to content

Commit

Permalink
Update Pcap4J and fixed DNS test
Browse files Browse the repository at this point in the history
  • Loading branch information
PeratX committed Sep 3, 2017
1 parent c376c61 commit d0c03df
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 35 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ dependencies {
compile 'com.android.support:percent:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'

compile 'org.pcap4j:pcap4j-core:1.7.0'
compile 'org.pcap4j:pcap4j-packetfactory-propertiesbased:1.7.0'
compile 'org.pcap4j:pcap4j-core:1.7.1'
compile 'org.pcap4j:pcap4j-packetfactory-static:1.7.1'
compile 'de.measite.minidns:minidns-core:0.2.2'
compile 'com.google.code.gson:gson:2.8.1'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ private StringBuilder testServer(DNSQuery dnsQuery, Record.TYPE type, AbstractDN
message.addQuestion(new Question(domain, type));
message.setId((new Random()).nextInt());
message.setRecursionDesired(true);
message.getEdnsBuilder().setUdpPayloadSize(1024).setDnssecOk(false);
message.setOpcode(DNSMessage.OPCODE.QUERY);
message.setResponseCode(DNSMessage.RESPONSE_CODE.NO_ERROR);
message.setQrFlag(false);

long startTime = System.currentTimeMillis();
DNSMessage response = dnsQuery.query(message.build(), InetAddress.getByName(server.getAddress()), server.getPort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ public void process() {
Log.d(TAG, "Read from device");
readPacketFromDevice(inputStream, packet);
}

checkCache();

service.providerLoopCallback();
}
} catch (Exception e) {
Expand Down
29 changes: 0 additions & 29 deletions app/src/main/java/org/itxtech/daedalus/provider/UdpProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@
import org.itxtech.daedalus.util.RuleResolver;
import org.itxtech.daedalus.util.server.DNSServerHelper;
import org.pcap4j.packet.*;
import org.pcap4j.packet.factory.PacketFactoryPropertiesLoader;
import org.pcap4j.util.PropertiesLoader;

import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.*;
import java.util.Arrays;
import java.util.Iterator;
Expand All @@ -49,11 +46,6 @@ public class UdpProvider extends Provider {
FileDescriptor mInterruptFd = null;
final Queue<byte[]> deviceWrites = new LinkedList<>();

/**
* Number of iterations since we last cleared the pcap4j cache
*/
private int pcap4jFactoryClearCacheCounter = 0;

public UdpProvider(ParcelFileDescriptor descriptor, DaedalusVpnService service) {
super(descriptor, service);
}
Expand Down Expand Up @@ -148,34 +140,13 @@ public void process() {
Log.d(TAG, "Read from device");
readPacketFromDevice(inputStream, packet);
}

checkCache();
service.providerLoopCallback();
}
} catch (Exception e) {
Logger.logException(e);
}
}

void checkCache() {
// pcap4j has some sort of properties cache in the packet factory. This cache leaks, so
// we need to clean it up.
if (++pcap4jFactoryClearCacheCounter % 1024 == 0) {
try {
PacketFactoryPropertiesLoader l = PacketFactoryPropertiesLoader.getInstance();
Field field = l.getClass().getDeclaredField("loader");
field.setAccessible(true);
PropertiesLoader loader = (PropertiesLoader) field.get(l);
Log.d(TAG, "Cleaning cache");
loader.clearCache();
} catch (NoSuchFieldException e) {
Log.e(TAG, "Cannot find declared loader field", e);
} catch (IllegalAccessException e) {
Log.e(TAG, "Cannot get declared loader field", e);
}
}
}

void writeToDevice(FileOutputStream outFd) throws DaedalusVpnService.VpnNetworkException {
try {
outFd.write(deviceWrites.poll());
Expand Down

0 comments on commit d0c03df

Please sign in to comment.