summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2023-06-06 23:25:01 +0200
committerMikael Magnusson <mikma@users.sourceforge.net>2023-07-05 21:41:01 +0200
commitb6557cc57a97cf543c69251e00fe82794b93dcff (patch)
tree1fa218ed473a0d865c5deeedabaa35ef3e64531e
parent56825456261abc1c321ee1aa68e5e9389487edf3 (diff)
WIP: use lease Duration and Instan
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/Dhcp.java22
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java6
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);