summaryrefslogtreecommitdiffhomepage
path: root/tunnel
diff options
context:
space:
mode:
Diffstat (limited to 'tunnel')
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/Dhcp.java31
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java19
-rw-r--r--tunnel/tools/libwg-go/dhcp.go9
3 files changed, 40 insertions, 19 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 59a3e69c..4cb2a5a0 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,7 @@ package com.wireguard.android.backend;
import com.wireguard.config.InetNetwork;
import com.wireguard.util.NonNullForAll;
+import java.util.LinkedHashSet;
import java.util.Set;
/**
@@ -15,14 +16,34 @@ import java.util.Set;
*/
@NonNullForAll
public class Dhcp {
- private Set<InetNetwork> addresses;
+ private Set<Lease> leases = new LinkedHashSet<>();
- Dhcp(Set<InetNetwork> addresses) {
- this.addresses = addresses;
+ public class Lease {
+ private InetNetwork address;
+ private long valid;
+ private long preferred;
+
+ public Lease(InetNetwork address, long valid, long preferred) {
+ this.address = address;
+ this.valid = valid;
+ this.preferred = preferred;
+ }
+
+ public final InetNetwork getAddress() {
+ return this.address;
+ }
+
+ public String toString() {
+ return address.toString() + " (valid " + this.valid + "s, preferred " + this.preferred + "s)";
+ }
+ }
+
+ public void addLease(InetNetwork address, long valid, long preferred) {
+ this.leases.add(new Lease(address, valid, preferred));
}
- public Set<InetNetwork> getAddresses() {
- return addresses;
+ public final Set<Lease> getLeases() {
+ return leases;
}
public String toString() {
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 327383cd..040a316d 100644
--- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
+++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
@@ -81,7 +81,6 @@ import java.net.URL;
import java.time.Instant;
import java.nio.ByteOrder;
import java.util.Collections;
-import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
@@ -427,23 +426,22 @@ public final class GoBackend implements Backend {
DhcpResponse resp = stub.dhcp(request);
Log.i(TAG, "Dhcp: " + resp.getError().getMessage());
- Set<InetNetwork> addresses = new LinkedHashSet<>();
+ Dhcp dhcp = new Dhcp();
+
if (resp.getLeasesList() != null) {
for (final Lease lease: resp.getLeasesList()) {
try {
InetAddress addr = InetAddress.getByAddress(lease.getAddress().getAddress().toByteArray());
- Log.i(TAG, "Lease: " + addr);
- addresses.add(new InetNetwork(addr, 128));
+ Log.i(TAG, "Lease: " + addr + " " + lease.getValidLifetime().getSeconds() + " " + lease.getPreferredLifetime().getSeconds());
+ dhcp.addLease(new InetNetwork(addr, 128), lease.getValidLifetime().getSeconds(), lease.getPreferredLifetime().getSeconds());
} catch (UnknownHostException ex) {
// Ignore
}
}
}
- Dhcp dhcp = new Dhcp(addresses);
-
// Replace the vpn tunnel
- final VpnService.Builder builder = getBuilder(currentTunnel.getName(), currentConfig, service, dhcp.getAddresses());
+ final VpnService.Builder builder = getBuilder(currentTunnel.getName(), currentConfig, service, dhcp.getLeases());
Log.i(TAG, "Builder: " + builder);
@@ -555,7 +553,7 @@ public final class GoBackend implements Backend {
Log.i(TAG, "Exit streamReverse");
}
- private VpnService.Builder getBuilder(final String name, @Nullable final Config config, final VpnService service, @Nullable final Set<InetNetwork> leases) throws PackageManager.NameNotFoundException {
+ private VpnService.Builder getBuilder(final String name, @Nullable final Config config, final VpnService service, @Nullable final Set<Dhcp.Lease> leases) throws PackageManager.NameNotFoundException {
Log.i(TAG, "Builder 1");
final VpnService.Builder builder = service.getBuilder();
Log.i(TAG, "Builder 2");
@@ -571,8 +569,9 @@ public final class GoBackend implements Backend {
Log.i(TAG, "Builder 5");
if (leases != null) {
- for (final InetNetwork lease: leases) {
- builder.addAddress(lease.getAddress(), lease.getMask());
+ for (final Dhcp.Lease lease: leases) {
+ InetNetwork addr = lease.getAddress();
+ builder.addAddress(addr.getAddress(), addr.getMask());
}
}
diff --git a/tunnel/tools/libwg-go/dhcp.go b/tunnel/tools/libwg-go/dhcp.go
index b7919151..4af5b0cb 100644
--- a/tunnel/tools/libwg-go/dhcp.go
+++ b/tunnel/tools/libwg-go/dhcp.go
@@ -12,6 +12,7 @@ import (
"github.com/insomniacslk/dhcp/iana"
gen "golang.zx2c4.com/wireguard/android/gen"
+ "google.golang.org/protobuf/types/known/durationpb"
)
const (
@@ -131,13 +132,13 @@ func getAddressesFromReply(reply *dhcpv6.Message) []*gen.Lease{
ianaOpts := iana.Options.Get(dhcpv6.OptionIAAddr)
for _, opt := range ianaOpts {
- addr :=opt.(*dhcpv6.OptIAAddress).IPv6Addr
+ addrOpt := opt.(*dhcpv6.OptIAAddress)
lease := &gen.Lease{
Address: &gen.InetAddress{
- Address: addr,
+ Address: addrOpt.IPv6Addr,
},
- // PreferredLifetime: ,
- // ValidLifetime: ,
+ PreferredLifetime: durationpb.New(addrOpt.PreferredLifetime),
+ ValidLifetime: durationpb.New(addrOpt.ValidLifetime),
}
leases = append(leases, lease)
}