diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-06-06 23:25:01 +0200 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-08-19 00:30:43 +0200 |
commit | 3b67da728e82b5cc236adaf5939be2f03788b5a9 (patch) | |
tree | e3ae663b17c7f5abbd324e55f3f30d6acdbed12f /tunnel/src/main | |
parent | 4910edc398a9ad74414d5b198286055f182dea97 (diff) |
WIP: use lease Duration and Instan
Diffstat (limited to 'tunnel/src/main')
-rw-r--r-- | tunnel/src/main/java/com/wireguard/android/backend/Dhcp.java | 22 | ||||
-rw-r--r-- | tunnel/src/main/java/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); |