Set socket keep alive

pull/187/head
M66B 4 years ago
parent dad1977eac
commit d867ba05ed

@ -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})

@ -40,7 +40,7 @@ class CharsetHelper {
));
static {
System.loadLibrary("compact_enc_det");
System.loadLibrary("fairemail");
}
private static native DetectResult jni_detect(byte[] octets);

@ -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 {

@ -1,6 +1,11 @@
#include <jni.h>
#include <android/log.h>
#include <cstdio>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#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;
}
Loading…
Cancel
Save