From c3bcaaa76b28e65f5eea938f4c7a0c370b969f05 Mon Sep 17 00:00:00 2001 From: genofire Date: Wed, 19 Feb 2020 16:43:28 +0100 Subject: [PATCH] reimplement dns resolver cache + add timeout for cache --- .../persistance/DatabaseBackend.java | 53 ++++++++++++++++++- .../siacs/conversations/utils/Resolver.java | 28 ++++++++++ .../conversations/xmpp/XmppConnection.java | 13 ++++- 3 files changed, 90 insertions(+), 4 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java index b07143cb9..aab2d31e5 100644 --- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -55,6 +55,7 @@ import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CursorUtils; import eu.siacs.conversations.utils.FtsUtils; import eu.siacs.conversations.utils.MimeUtils; +import eu.siacs.conversations.utils.Resolver; import eu.siacs.conversations.xmpp.InvalidJid; import eu.siacs.conversations.xmpp.mam.MamReference; import rocks.xmpp.addr.Jid; @@ -62,7 +63,7 @@ import rocks.xmpp.addr.Jid; public class DatabaseBackend extends SQLiteOpenHelper { private static final String DATABASE_NAME = "history"; - private static final int DATABASE_VERSION = 46; + private static final int DATABASE_VERSION = 47; private static DatabaseBackend instance = null; private static String CREATE_CONTATCS_STATEMENT = "create table " + Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, " @@ -149,6 +150,20 @@ public class DatabaseBackend extends SQLiteOpenHelper { + ") ON CONFLICT IGNORE" + ");"; + private static String RESOLVER_RESULTS_TABLENAME = "resolver_results"; + + private static String CREATE_RESOLVER_RESULTS_TABLE = "create table " + RESOLVER_RESULTS_TABLENAME + "(" + + Resolver.Result.DOMAIN + " TEXT," + + Resolver.Result.HOSTNAME + " TEXT," + + Resolver.Result.IP + " BLOB," + + Resolver.Result.PRIORITY + " NUMBER," + + Resolver.Result.DIRECT_TLS + " NUMBER," + + Resolver.Result.AUTHENTICATED + " NUMBER," + + Resolver.Result.PORT + " NUMBER," + + Resolver.Result.TIME_REQUESTED + " NUMBER," + + "UNIQUE(" + Resolver.Result.DOMAIN + ") ON CONFLICT REPLACE" + + ");"; + private static String CREATE_MESSAGE_TIME_INDEX = "create INDEX message_time_index ON " + Message.TABLENAME + "(" + Message.TIME_SENT + ")"; private static String CREATE_MESSAGE_CONVERSATION_INDEX = "create INDEX message_conversation_index ON " + Message.TABLENAME + "(" + Message.CONVERSATION + ")"; private static String CREATE_MESSAGE_DELETED_INDEX = "create index message_deleted_index ON " + Message.TABLENAME + "(" + Message.DELETED + ")"; @@ -243,6 +258,7 @@ public class DatabaseBackend extends SQLiteOpenHelper { db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT); db.execSQL(CREATE_IDENTITIES_STATEMENT); db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT); + db.execSQL(CREATE_RESOLVER_RESULTS_TABLE); db.execSQL(CREATE_MESSAGE_INDEX_TABLE); db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER); db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER); @@ -537,7 +553,12 @@ public class DatabaseBackend extends SQLiteOpenHelper { Log.d(Config.LOGTAG,"deleted old edit information in "+diff+"ms"); } - db.execSQL("DROP TABLE IF EXISTS resolver_results"); + if (oldVersion < 47 && newVersion >= 47) { + // values in resolver_result are cache and not worth to store + db.execSQL("DROP TABLE IF EXISTS " + RESOLVER_RESULTS_TABLENAME); + db.execSQL(CREATE_RESOLVER_RESULTS_TABLE); + } + } private void canonicalizeJids(SQLiteDatabase db) { @@ -660,6 +681,34 @@ public class DatabaseBackend extends SQLiteOpenHelper { return result; } + public void saveResolverResult(String domain, Resolver.Result result) { + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues contentValues = result.toContentValues(); + contentValues.put(Resolver.Result.DOMAIN, domain); + db.insert(RESOLVER_RESULTS_TABLENAME, null, contentValues); + } + + public synchronized Resolver.Result findResolverResult(String domain) { + SQLiteDatabase db = this.getReadableDatabase(); + String where = Resolver.Result.DOMAIN + "=?"; + String[] whereArgs = {domain}; + final Cursor cursor = db.query(RESOLVER_RESULTS_TABLENAME, null, where, whereArgs, null, null, null); + Resolver.Result result = null; + if (cursor != null) { + try { + if (cursor.moveToFirst()) { + result = Resolver.Result.fromCursor(cursor); + } + } catch (Exception e) { + Log.d(Config.LOGTAG, "unable to find cached resolver result in database " + e.getMessage()); + return null; + } finally { + cursor.close(); + } + } + return result; + } + public void insertPresenceTemplate(PresenceTemplate template) { SQLiteDatabase db = this.getWritableDatabase(); String whereToDelete = PresenceTemplate.MESSAGE + "=?"; diff --git a/src/main/java/eu/siacs/conversations/utils/Resolver.java b/src/main/java/eu/siacs/conversations/utils/Resolver.java index 4f91c130a..fe117fb55 100644 --- a/src/main/java/eu/siacs/conversations/utils/Resolver.java +++ b/src/main/java/eu/siacs/conversations/utils/Resolver.java @@ -353,12 +353,14 @@ public class Resolver { } public static class Result implements Comparable, Callable { + public static final String DOMAIN = "domain"; public static final String IP = "ip"; public static final String HOSTNAME = "hostname"; public static final String PORT = "port"; public static final String PRIORITY = "priority"; public static final String DIRECT_TLS = "directTls"; public static final String AUTHENTICATED = "authenticated"; + public static final String TIME_REQUESTED = "time_requested"; private InetAddress ip; private DNSName hostname; @@ -366,12 +368,14 @@ public class Resolver { private boolean directTls = false; private boolean authenticated = false; private int priority; + private long timeRequested; private Socket socket; private String logID = ""; static Result fromRecord(SRV srv, boolean directTls) { Result result = new Result(); + result.timeRequested = System.currentTimeMillis(); result.port = srv.port; result.hostname = srv.name; result.directTls = directTls; @@ -381,6 +385,7 @@ public class Resolver { static Result createDefault(DNSName hostname, InetAddress ip, int port) { Result result = new Result(); + result.timeRequested = System.currentTimeMillis(); result.port = port; result.hostname = hostname; result.ip = ip; @@ -425,6 +430,10 @@ public class Resolver { return authenticated; } + public boolean isOutdated() { + return (System.currentTimeMillis() - timeRequested) > 300_000; + } + public Socket getSocket() { return socket; } @@ -498,6 +507,24 @@ public class Resolver { throw new Exception("Resolver.Result was not possible to connect - should be catched by executor"); } + public static Result fromCursor(Cursor cursor) { + final Result result = new Result(); + try { + result.ip = InetAddress.getByAddress(cursor.getBlob(cursor.getColumnIndex(IP))); + } catch (UnknownHostException e) { + result.ip = null; + } + final String hostname = cursor.getString(cursor.getColumnIndex(HOSTNAME)); + result.hostname = hostname == null ? null : DNSName.from(hostname); + result.port = cursor.getInt(cursor.getColumnIndex(PORT)); + result.directTls = cursor.getInt(cursor.getColumnIndex(DIRECT_TLS)) > 0; + result.authenticated = cursor.getInt(cursor.getColumnIndex(AUTHENTICATED)) > 0; + result.priority = cursor.getInt(cursor.getColumnIndex(PRIORITY)); + result.timeRequested = cursor.getLong(cursor.getColumnIndex(TIME_REQUESTED)); + return result; + } + + public ContentValues toContentValues() { final ContentValues contentValues = new ContentValues(); contentValues.put(IP, ip == null ? null : ip.getAddress()); @@ -506,6 +533,7 @@ public class Resolver { contentValues.put(PRIORITY, priority); contentValues.put(DIRECT_TLS, directTls ? 1 : 0); contentValues.put(AUTHENTICATED, authenticated ? 1 : 0); + contentValues.put(TIME_REQUESTED, timeRequested); return contentValues; } } diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index 3cb005c9c..b9868cb7f 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -291,11 +291,17 @@ public class XmppConnection implements Runnable { } } else { final String domain = account.getJid().getDomain(); - final Resolver.Result result; + final Resolver.Result storedBackupResult = mXmppConnectionService.databaseBackend.findResolverResult(domain); + Resolver.Result result = null; final boolean hardcoded = extended && !account.getHostname().isEmpty(); if (hardcoded) { result = Resolver.fromHardCoded(account.getHostname(), account.getPort()); - } else { + } else if (storedBackupResult != null && !storedBackupResult.isOutdated()) { + storedBackupResult.connect(); + result = storedBackupResult; + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": loaded backup resolver result from db: " + storedBackupResult); + } + if (result == null || result.getSocket() == null) { result = Resolver.resolve(domain); } if (result == null) { @@ -322,6 +328,9 @@ public class XmppConnection implements Runnable { localSocket.setSoTimeout(Config.SOCKET_TIMEOUT * 1000); if (startXmpp(localSocket)) { localSocket.setSoTimeout(0); //reset to 0; once the connection is established we don’t want this + if (!hardcoded && !result.equals(storedBackupResult)) { + mXmppConnectionService.databaseBackend.saveResolverResult(domain, result); + } // successfully connected to server that speaks xmpp } else { FileBackend.close(localSocket);