From e76e2fdeb1ae68ec47032ac57d6604d3f9dbd95a Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Tue, 6 Jun 2023 23:25:01 +0200 Subject: WIP: use lease Duration and Instan --- .../java/com/wireguard/android/backend/Dhcp.java | 22 +++++++++++++++------- .../com/wireguard/android/backend/GoBackend.java | 6 +++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/tunnel/src/main/java/com/wireguard/android/backend/Dhcp.java b/tunnel/src/main/java/com/wireguard/android/backend/Dhcp.java index 4cb2a5a0..c4043625 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/Dhcp.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/Dhcp.java @@ -8,6 +8,8 @@ package com.wireguard.android.backend; import com.wireguard.config.InetNetwork; import com.wireguard.util.NonNullForAll; +import java.time.Duration; +import java.time.Instant; import java.util.LinkedHashSet; import java.util.Set; @@ -20,13 +22,19 @@ public class Dhcp { public class Lease { private InetNetwork address; - private long valid; - private long preferred; + private Duration validLt; + private Duration preferredLt; + private Instant validTs; + private Instant preferredTs; - public Lease(InetNetwork address, long valid, long preferred) { + public Lease(InetNetwork address, Duration validLt, Duration preferredLt) { this.address = address; - this.valid = valid; - this.preferred = preferred; + this.validLt = validLt; + this.preferredLt = preferredLt; + + Instant now = Instant.now(); + this.validTs = now.plus(validLt); + this.preferredTs = now.plus(preferredLt); } public final InetNetwork getAddress() { @@ -34,11 +42,11 @@ public class Dhcp { } public String toString() { - return address.toString() + " (valid " + this.valid + "s, preferred " + this.preferred + "s)"; + return address.toString() + " (valid " + this.validTs + "s, preferred " + this.preferredTs + "s)"; } } - public void addLease(InetNetwork address, long valid, long preferred) { + public void addLease(InetNetwork address, Duration valid, Duration preferred) { this.leases.add(new Lease(address, valid, preferred)); } diff --git a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java index 040a316d..6695c81e 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -78,6 +78,7 @@ import java.net.Inet6Address; import java.net.InetSocketAddress; import java.net.UnknownHostException; import java.net.URL; +import java.time.Duration; import java.time.Instant; import java.nio.ByteOrder; import java.util.Collections; @@ -433,7 +434,9 @@ public final class GoBackend implements Backend { try { InetAddress addr = InetAddress.getByAddress(lease.getAddress().getAddress().toByteArray()); Log.i(TAG, "Lease: " + addr + " " + lease.getValidLifetime().getSeconds() + " " + lease.getPreferredLifetime().getSeconds()); - dhcp.addLease(new InetNetwork(addr, 128), lease.getValidLifetime().getSeconds(), lease.getPreferredLifetime().getSeconds()); + dhcp.addLease(new InetNetwork(addr, 128), + Duration.ofSeconds(lease.getValidLifetime().getSeconds(), lease.getValidLifetime().getNanos()), + Duration.ofSeconds(lease.getPreferredLifetime().getSeconds(), lease.getPreferredLifetime().getNanos())); } catch (UnknownHostException ex) { // Ignore } @@ -463,6 +466,7 @@ public final class GoBackend implements Backend { // TODO fetch expiration time from Dhcp leases long delayMillis = 1000 * 3600 * 12 * 3 / 4; // Renew 3/4 * 12h + //long delayMillis = lease.getValidLifetime().getSeconds() * 1000 + lease.getValidLifetime().getNanos() / 1000; handler.sendMessageDelayed(msg, delayMillis); bgp = new Bgp(this, channel, currentTunnel, currentTunnelHandle); -- cgit v1.2.3