1
0
Fork 0
mirror of https://github.com/M66B/NetGuard.git synced 2025-01-01 12:54:07 +00:00

Native allow settings PCAP record/file size from Java

This commit is contained in:
M66B 2016-02-13 12:28:59 +01:00
parent 6b7d4ed017
commit f83cc0e82d
5 changed files with 33 additions and 24 deletions

View file

@ -380,8 +380,7 @@ public class ActivityLog extends AppCompatActivity implements SharedPreferences.
case R.id.menu_pcap_enabled:
item.setChecked(!item.isChecked());
prefs.edit().putBoolean("pcap", item.isChecked()).apply();
SinkholeService.setPcap(item.isChecked() ? pcap_file : null);
SinkholeService.setPcap(item.isChecked(), ActivityLog.this);
return true;
case R.id.menu_pcap_export:
@ -394,10 +393,10 @@ public class ActivityLog extends AppCompatActivity implements SharedPreferences.
protected Object doInBackground(Object... objects) {
DatabaseHelper.getInstance(ActivityLog.this).clearLog();
if (prefs.getBoolean("pcap", false)) {
SinkholeService.setPcap(null);
SinkholeService.setPcap(false, ActivityLog.this);
if (pcap_file.exists() && !pcap_file.delete())
Log.w(TAG, "Delete PCAP failed");
SinkholeService.setPcap(pcap_file);
SinkholeService.setPcap(true, ActivityLog.this);
} else {
if (pcap_file.exists() && !pcap_file.delete())
Log.w(TAG, "Delete PCAP failed");
@ -481,7 +480,7 @@ public class ActivityLog extends AppCompatActivity implements SharedPreferences.
FileInputStream in = null;
try {
// Stop capture
SinkholeService.setPcap(null);
SinkholeService.setPcap(false, ActivityLog.this);
Uri target = data.getData();
if (data.hasExtra("org.openintents.extra.DIR_PATH"))
@ -522,10 +521,8 @@ public class ActivityLog extends AppCompatActivity implements SharedPreferences.
// Resume capture
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ActivityLog.this);
if (prefs.getBoolean("pcap", false)) {
File pcap_file = new File(getCacheDir(), "netguard.pcap");
SinkholeService.setPcap(pcap_file);
}
if (prefs.getBoolean("pcap", false))
SinkholeService.setPcap(true, ActivityLog.this);
}
}

View file

@ -153,12 +153,13 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
private native int[] jni_get_stats();
private static native void jni_pcap(String name);
private static native void jni_pcap(String name, int record_size, int file_size);
private native void jni_done();
public static void setPcap(File pcap) {
jni_pcap(pcap == null ? null : pcap.getAbsolutePath());
public static void setPcap(boolean enabled, Context context) {
File pcap = (enabled ? new File(context.getCacheDir(), "netguard.pcap") : null);
jni_pcap(pcap == null ? null : pcap.getAbsolutePath(), 64, 2 * 1024 * 1024);
}
synchronized private static PowerManager.WakeLock getLock(Context context) {
@ -1410,7 +1411,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
// Native init
jni_init();
boolean pcap = prefs.getBoolean("pcap", false);
setPcap(pcap ? new File(getCacheDir(), "netguard.pcap") : null);
setPcap(pcap, this);
prefs.registerOnSharedPreferenceChangeListener(this);

View file

@ -34,6 +34,8 @@ int loglevel = ANDROID_LOG_WARN;
extern int max_tun_msg;
extern FILE *pcap_file;
extern int pcap_record_size;
extern long pcap_file_size;
// JNI
@ -210,7 +212,13 @@ Java_eu_faircode_netguard_SinkholeService_jni_1get_1stats(JNIEnv *env, jobject i
}
JNIEXPORT void JNICALL
Java_eu_faircode_netguard_SinkholeService_jni_1pcap(JNIEnv *env, jclass type, jstring name_) {
Java_eu_faircode_netguard_SinkholeService_jni_1pcap(
JNIEnv *env, jclass type,
jstring name_, jint record_size, jint file_size) {
pcap_record_size = record_size;
pcap_file_size = file_size;
if (pthread_mutex_lock(&lock))
log_android(ANDROID_LOG_ERROR, "pthread_mutex_lock failed");
@ -229,11 +237,12 @@ Java_eu_faircode_netguard_SinkholeService_jni_1pcap(JNIEnv *env, jclass type, js
pcap_file = NULL;
}
log_android(ANDROID_LOG_INFO, "PCAP disabled");
log_android(ANDROID_LOG_WARN, "PCAP disabled");
}
else {
const char *name = (*env)->GetStringUTFChars(env, name_, 0);
log_android(ANDROID_LOG_INFO, "PCAP file %s", name);
log_android(ANDROID_LOG_WARN, "PCAP file %s record size %d truncate @%ld",
name, pcap_record_size, pcap_file_size);
pcap_file = fopen(name, "ab+");
if (pcap_file == NULL)
@ -244,10 +253,13 @@ Java_eu_faircode_netguard_SinkholeService_jni_1pcap(JNIEnv *env, jclass type, js
log_android(ANDROID_LOG_ERROR, "PCAP fcntl O_NONBLOCK error %d: %s",
errno, strerror(errno));
if (ftell(pcap_file) == 0) {
log_android(ANDROID_LOG_INFO, "Initializing PCAP");
long size = ftell(pcap_file);
if (size == 0) {
log_android(ANDROID_LOG_WARN, "PCAP initialize");
write_pcap_hdr();
}
else
log_android(ANDROID_LOG_WARN, "PCAP current size %ld", size);
}
(*env)->ReleaseStringUTFChars(env, name_, name);

View file

@ -56,9 +56,6 @@
#define UID_DELAYTRY 10 // milliseconds
#define UID_MAXTRY 3
#define MAX_PCAP_FILE (1024 * 1024) // bytes
#define MAX_PCAP_RECORD 128 // bytes
#define RTLD_NOLOAD 4
struct arguments {

View file

@ -20,6 +20,8 @@
#include "netguard.h"
FILE *pcap_file = NULL;
int pcap_record_size = 64;
long pcap_file_size = 2 * 1024 * 1024;
void write_pcap_hdr() {
struct pcap_hdr_s pcap_hdr;
@ -28,7 +30,7 @@ void write_pcap_hdr() {
pcap_hdr.version_minor = 4;
pcap_hdr.thiszone = 0;
pcap_hdr.sigfigs = 0;
pcap_hdr.snaplen = MAX_PCAP_RECORD;
pcap_hdr.snaplen = pcap_record_size;
pcap_hdr.network = LINKTYPE_RAW;
write_pcap(&pcap_hdr, sizeof(struct pcap_hdr_s));
}
@ -38,7 +40,7 @@ void write_pcap_rec(const uint8_t *buffer, size_t length) {
if (clock_gettime(CLOCK_REALTIME, &ts))
log_android(ANDROID_LOG_ERROR, "clock_gettime error %d: %s", errno, strerror(errno));
size_t plen = (length < MAX_PCAP_RECORD ? length : MAX_PCAP_RECORD);
size_t plen = (length < pcap_record_size ? length : pcap_record_size);
struct pcaprec_hdr_s pcap_rec;
pcap_rec.ts_sec = (guint32_t) ts.tv_sec;
@ -57,7 +59,7 @@ void write_pcap(const void *ptr, size_t len) {
long fsize = ftell(pcap_file);
log_android(ANDROID_LOG_VERBOSE, "PCAP wrote %d @%ld", len, fsize);
if (fsize > MAX_PCAP_FILE) {
if (fsize > pcap_file_size) {
log_android(ANDROID_LOG_WARN, "PCAP truncate @%ld", fsize);
if (ftruncate(fileno(pcap_file), sizeof(struct pcap_hdr_s)))
log_android(ANDROID_LOG_ERROR, "PCAP ftruncate error %d: %s",