From d867ba05ed52443b77cc2e37f2c8331263fc6467 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 8 Nov 2020 15:26:18 +0100 Subject: [PATCH] Set socket keep alive --- app/CMakeLists.txt | 8 +++---- .../java/eu/faircode/email/CharsetHelper.java | 2 +- .../java/eu/faircode/email/EmailService.java | 14 +++++++++++- app/src/main/jni/{charset.cc => fairemail.cc} | 22 +++++++++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) rename app/src/main/jni/{charset.cc => fairemail.cc} (75%) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 818f8482cc..a61f137c12 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 3.4.1) -add_library(compact_enc_det SHARED - src/main/jni/charset.cc +add_library(fairemail SHARED + src/main/jni/fairemail.cc src/main/jni/compact_enc_det/compact_enc_det/compact_enc_det.cc src/main/jni/compact_enc_det/compact_enc_det/compact_enc_det_hint_code.cc src/main/jni/compact_enc_det/util/encodings/encodings.cc @@ -17,6 +17,6 @@ find_library(log-lib log) add_definitions(-DHAVE_MEMRCHR) -target_compile_options(compact_enc_det PRIVATE -Wno-c++11-narrowing) +target_compile_options(fairemail PRIVATE -Wno-c++11-narrowing) -target_link_libraries(compact_enc_det ${log-lib}) +target_link_libraries(fairemail ${log-lib}) diff --git a/app/src/main/java/eu/faircode/email/CharsetHelper.java b/app/src/main/java/eu/faircode/email/CharsetHelper.java index d75ad8d49c..dd56b6b93a 100644 --- a/app/src/main/java/eu/faircode/email/CharsetHelper.java +++ b/app/src/main/java/eu/faircode/email/CharsetHelper.java @@ -40,7 +40,7 @@ class CharsetHelper { )); static { - System.loadLibrary("compact_enc_det"); + System.loadLibrary("fairemail"); } private static native DetectResult jni_detect(byte[] octets); diff --git a/app/src/main/java/eu/faircode/email/EmailService.java b/app/src/main/java/eu/faircode/email/EmailService.java index 0012db432e..d89fe8123c 100644 --- a/app/src/main/java/eu/faircode/email/EmailService.java +++ b/app/src/main/java/eu/faircode/email/EmailService.java @@ -21,6 +21,7 @@ package eu.faircode.email; import android.content.Context; import android.content.SharedPreferences; +import android.os.ParcelFileDescriptor; import android.security.KeyChain; import android.text.TextUtils; @@ -144,6 +145,12 @@ public class EmailService implements AutoCloseable { // TLS_FALLBACK_SCSV https://tools.ietf.org/html/rfc7507 // TLS_EMPTY_RENEGOTIATION_INFO_SCSV https://tools.ietf.org/html/rfc5746 + static { + System.loadLibrary("fairemail"); + } + + private static native int jni_socket_keep_alive(int fd, int seconds); + private EmailService() { // Prevent instantiation } @@ -1032,13 +1039,18 @@ public class EmailService implements AutoCloseable { if (keepAlive) { Log.e("Socket keep-alive=" + keepAlive); - socket.setKeepAlive(false); + socket.setKeepAlive(false); // sets SOL_SOCKET/SO_KEEPALIVE } if (linger >= 0) { Log.e("Socket linger=" + linger); socket.setSoLinger(false, -1); } + + int fd = ParcelFileDescriptor.fromSocket(socket).getFd(); + int errno = jni_socket_keep_alive(fd, 9 * 60); + if (errno != 0) + Log.e("Socket TCP_KEEPIDLE=" + errno); } class UntrustedException extends MessagingException { diff --git a/app/src/main/jni/charset.cc b/app/src/main/jni/fairemail.cc similarity index 75% rename from app/src/main/jni/charset.cc rename to app/src/main/jni/fairemail.cc index d76ab2f190..c5868fcda0 100644 --- a/app/src/main/jni/charset.cc +++ b/app/src/main/jni/fairemail.cc @@ -1,6 +1,11 @@ #include #include #include + +#include +#include +#include + #include "compact_enc_det/compact_enc_det.h" void log_android(int prio, const char *fmt, ...) { @@ -51,3 +56,20 @@ Java_eu_faircode_email_CharsetHelper_jni_1detect(JNIEnv *env, jclass type, jbyte (jint) bytes_consumed, (jboolean) is_reliable); } + +extern "C" +JNIEXPORT jint JNICALL +Java_eu_faircode_email_EmailService_jni_1socket_1keep_1alive( + JNIEnv *env, jclass clazz, + jint fd, jint seconds) { + // https://linux.die.net/man/2/setsockopt + // https://linux.die.net/man/3/setsockopt + // https://tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO/#setsockopt + int value = seconds; + + int res = setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, (void *) &value, sizeof(value)); + if (res < 0) + res = errno; + + return res; +}