Improved PCAP file init

Refs #259
This commit is contained in:
M66B 2016-01-31 18:46:44 +01:00
parent e6f47cd400
commit 31403f73bc
3 changed files with 17 additions and 15 deletions

View File

@ -346,7 +346,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, !pcap_file.exists());
SinkholeService.setPcap(item.isChecked() ? pcap_file : null);
return true;
case R.id.menu_pcap_export:
@ -359,12 +359,13 @@ public class ActivityLog extends AppCompatActivity implements SharedPreferences.
protected Object doInBackground(Object... objects) {
dh.clearLog();
if (prefs.getBoolean("pcap", false)) {
SinkholeService.setPcap(null, false);
pcap_file.delete();
SinkholeService.setPcap(pcap_file, true);
SinkholeService.setPcap(null);
if (pcap_file.exists() && !pcap_file.delete())
Log.w(TAG, "Delete PCAP failed");
SinkholeService.setPcap(pcap_file);
} else {
if (pcap_file.exists())
pcap_file.delete();
if (pcap_file.exists() && !pcap_file.delete())
Log.w(TAG, "Delete PCAP failed");
}
return null;
}
@ -428,7 +429,7 @@ public class ActivityLog extends AppCompatActivity implements SharedPreferences.
FileInputStream in = null;
try {
// Stop capture
SinkholeService.setPcap(null, false);
SinkholeService.setPcap(null);
Uri target = data.getData();
if (data.hasExtra("org.openintents.extra.DIR_PATH"))
@ -471,7 +472,7 @@ public class ActivityLog extends AppCompatActivity implements SharedPreferences.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ActivityLog.this);
if (prefs.getBoolean("pcap", false)) {
File pcap_file = new File(getCacheDir(), "netguard.pcap");
SinkholeService.setPcap(pcap_file, false);
SinkholeService.setPcap(pcap_file);
}
}
}

View File

@ -146,10 +146,10 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
private native void jni_done();
private static native void jni_pcap(String name, boolean init);
private static native void jni_pcap(String name);
public static void setPcap(File pcap, boolean init) {
jni_pcap(pcap == null ? null : pcap.getAbsolutePath(), init);
public static void setPcap(File pcap) {
jni_pcap(pcap == null ? null : pcap.getAbsolutePath());
}
static {
@ -1261,7 +1261,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, false);
setPcap(pcap ? new File(getCacheDir(), "netguard.pcap") : null);
prefs.registerOnSharedPreferenceChangeListener(this);

View File

@ -175,8 +175,7 @@ Java_eu_faircode_netguard_SinkholeService_jni_1done(JNIEnv *env, jobject instanc
}
JNIEXPORT void JNICALL
Java_eu_faircode_netguard_SinkholeService_jni_1pcap(JNIEnv *env, jclass type,
jstring name_, jboolean init) {
Java_eu_faircode_netguard_SinkholeService_jni_1pcap(JNIEnv *env, jclass type, jstring name_) {
if (pthread_mutex_lock(&lock))
log_android(ANDROID_LOG_ERROR, "pthread_mutex_lock failed");
@ -210,8 +209,10 @@ Java_eu_faircode_netguard_SinkholeService_jni_1pcap(JNIEnv *env, jclass type,
log_android(ANDROID_LOG_ERROR, "PCAP fcntl O_NONBLOCK error %d: %s",
errno, strerror(errno));
if (init)
if (ftell(pcap_file) == 0) {
log_android(ANDROID_LOG_INFO, "Initializing PCAP");
write_pcap_hdr();
}
}
(*env)->ReleaseStringUTFChars(env, name_, name);