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

View File

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