/* * Copyright © 2017-2023 WireGuard LLC. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ 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.time.LocalTime; import java.time.ZoneId; import java.util.LinkedHashSet; import java.util.Set; /** * Class representing DHCP info for a {@link Tunnel} instance. */ @NonNullForAll public class Dhcp { private Set leases = new LinkedHashSet<>(); public class Lease { private InetNetwork address; private Duration validLt; private Duration preferredLt; private Instant validTs; private Instant preferredTs; public Lease(InetNetwork address, Duration validLt, Duration preferredLt) { this.address = address; this.validLt = validLt; this.preferredLt = preferredLt; Instant now = Instant.now(); this.validTs = now.plus(validLt); this.preferredTs = now.plus(preferredLt); } public final InetNetwork getAddress() { return this.address; } public String toString() { ZoneId zone = ZoneId.systemDefault(); LocalTime validLocal = validTs.atZone(zone).toLocalTime().withNano(0); LocalTime preferredLocal = preferredTs.atZone(zone).toLocalTime().withNano(0); // TODO add date when needed return address.toString() + " (valid " + validLocal + ", preferred " + preferredLocal + ")"; } } public void addLease(InetNetwork address, Duration valid, Duration preferred) { this.leases.add(new Lease(address, valid, preferred)); } public final Set getLeases() { return leases; } public String toString() { return "DHCP"; } }