summaryrefslogtreecommitdiffhomepage
path: root/tunnel/src/main/java/com
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2023-06-19 22:07:07 +0200
committerMikael Magnusson <mikma@users.sourceforge.net>2023-08-19 00:30:43 +0200
commit4e2e3f2ad3e9c5c7c1d61963e1cb9e98ff01be2d (patch)
treed02c3ac1666b96729ff4b50735fe88d85d175020 /tunnel/src/main/java/com
parent89536bcad847604ba6d68c0200a2a215e6e98665 (diff)
WIP: Delay DHCP renew until VPN network available
Diffstat (limited to 'tunnel/src/main/java/com')
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java42
1 files changed, 33 insertions, 9 deletions
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 11928d46..79b724e8 100644
--- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
+++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
@@ -434,6 +434,7 @@ public final class GoBackend implements Backend {
Log.i(TAG, "Dhcp: " + resp.getError().getMessage());
Dhcp dhcp = new Dhcp();
+ long delayMillis = 1000 * 3600 * 12; // Max renew 12h
if (resp.getLeasesList() != null) {
for (final Lease lease: resp.getLeasesList()) {
@@ -443,6 +444,8 @@ public final class GoBackend implements Backend {
dhcp.addLease(new InetNetwork(addr, 128),
Duration.ofSeconds(lease.getValidLifetime().getSeconds(), lease.getValidLifetime().getNanos()),
Duration.ofSeconds(lease.getPreferredLifetime().getSeconds(), lease.getPreferredLifetime().getNanos()));
+ long leaseDelayMillis = lease.getValidLifetime().getSeconds() * 1000 + lease.getValidLifetime().getNanos() / 1000;
+ delayMillis = Math.min(delayMillis, leaseDelayMillis);
} catch (UnknownHostException ex) {
// Ignore
}
@@ -468,11 +471,8 @@ public final class GoBackend implements Backend {
service.protect(wgGetSocketV6(currentTunnelHandle));
Log.i(TAG, "Dhcp done");
- // TODO fetch expiration time from Dhcp leases
- long delayMillis = 1000 * 3600 * 12; // Renew 3/4 * 12h
- //long delayMillis = lease.getValidLifetime().getSeconds() * 1000 + lease.getValidLifetime().getNanos() / 1000;
-
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
+ am.cancel(alarmListener);
am.setWindow(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + delayMillis * 3 / 4, delayMillis / 4, null, alarmListener, handler);
if (bgp != null) {
@@ -682,7 +682,11 @@ public final class GoBackend implements Backend {
activeNetwork = connectivityManager.getActiveNetwork();
- if (!connectivityManager.getNetworkCapabilities(activeNetwork).hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)) {
+ if (activeNetwork == null) {
+ Log.w(TAG, "Null activeNetwork");
+ activeNetwork = null;
+ }
+ else if (!connectivityManager.getNetworkCapabilities(activeNetwork).hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)) {
Log.w(TAG, "VPN network is active, null activeNetwork");
activeNetwork = null;
}
@@ -737,10 +741,16 @@ public final class GoBackend implements Backend {
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_DHCP_EXPIRE:
+ Log.w(TAG, "DHCP expire: " + ((activeNetwork != null) ? activeNetwork : "disabled"));
try {
- Log.w(TAG, "Before Dhcp");
- Dhcp(service); // Renew addresses
- Log.w(TAG, "After Dhcp");
+ if (activeNetwork != null) {
+ Log.w(TAG, "DHCP before");
+ Dhcp(service); // Renew addresses
+ Log.w(TAG, "DHCP after");
+ } else {
+ Log.w(TAG, "DHCP delay obtain lease");
+ obtainDhcpLease = true;
+ }
} catch (Exception ex) {
Log.e(TAG, "DHCP failed: " + ex);
}
@@ -907,7 +917,7 @@ public final class GoBackend implements Backend {
@Override
public void onAvailable(Network network) {
Log.w(TAG, "VPN onAvailable: " + network);
- if (obtainDhcpLease) {
+ if (obtainDhcpLease && activeNetwork != null) {
Log.w(TAG, "Obtaindhcplease");
try {
Log.w(TAG, "Before Dhcp");
@@ -928,6 +938,20 @@ public final class GoBackend implements Backend {
}
@Override
+ public void onLosing(Network network, int maxMsToLive) {
+ Log.w(TAG, "onLosing: " + network + " maxMsToLive: " + maxMsToLive);
+ // TODO release DHCP?
+ }
+
+ @Override
+ public void onLost(Network network) {
+ Log.w(TAG, "onLost: " + network + " (" + activeNetwork + ")");
+ if (network.equals(activeNetwork)) {
+ activeNetwork = null;
+ }
+ }
+
+ @Override
public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {
Log.w(TAG, "onLinkPropertiesChanged: " + network + " is default:" + (network.equals(activeNetwork)));
if (network.equals(activeNetwork) && currentConfig != null && currentTunnelHandle > -1) {