Add methods to get the minimal preferred/valid lifetime of delegated prefix.

Bug: 260934173
Test: m
Change-Id: I4ec75b8bdff1722317692cbd4966800724c8ba09
diff --git a/src/android/net/dhcp6/Dhcp6Client.java b/src/android/net/dhcp6/Dhcp6Client.java
index 8c869ed..990f026 100644
--- a/src/android/net/dhcp6/Dhcp6Client.java
+++ b/src/android/net/dhcp6/Dhcp6Client.java
@@ -380,18 +380,18 @@
         // prevent packet storms due to low timeouts.
         int renewTimeout = mReply.t1;
         int rebindTimeout = mReply.t2;
-        final IaPrefixOption ipo = mReply.ipos.get(0);
-        final long expirationTimeout = ipo.valid;
+        final long preferredTimeout = mReply.getMinimalPreferredLifetime();
+        final long expirationTimeout = mReply.getMinimalValidLifetime();
 
         // rfc8415#section-14.2: if t1 and / or t2 are 0, the client chooses an appropriate value.
         // rfc8415#section-21.21: Recommended values for T1 and T2 are 0.5 and 0.8 times the
         // shortest preferred lifetime of the prefixes in the IA_PD that the server is willing to
         // extend, respectively.
         if (renewTimeout == 0) {
-            renewTimeout = (int) (ipo.preferred * 0.5);
+            renewTimeout = (int) (preferredTimeout * 0.5);
         }
         if (rebindTimeout == 0) {
-            rebindTimeout = (int) (ipo.preferred * 0.8);
+            rebindTimeout = (int) (preferredTimeout * 0.8);
         }
 
         // Note: message validation asserts that the received t1 <= t2 if both t1 > 0 and t2 > 0.
diff --git a/src/android/net/dhcp6/Dhcp6Packet.java b/src/android/net/dhcp6/Dhcp6Packet.java
index 54c005b..594863c 100644
--- a/src/android/net/dhcp6/Dhcp6Packet.java
+++ b/src/android/net/dhcp6/Dhcp6Packet.java
@@ -35,6 +35,7 @@
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 import java.util.OptionalInt;
@@ -293,6 +294,27 @@
             return "Prefix Delegation: iaid " + iaid + ", t1 " + t1 + ", t2 " + t2
                     + ", IA prefix options: " + ipos;
         }
+
+        /**
+         * Compare the preferred lifetime in the IA prefix optin list and return the minimum one.
+         * TODO: exclude 0 preferred lifetime.
+         */
+        public long getMinimalPreferredLifetime() {
+            final IaPrefixOption ipo = Collections.min(ipos,
+                    (IaPrefixOption lhs, IaPrefixOption rhs) -> Long.compare(lhs.preferred,
+                            rhs.preferred));
+            return ipo.preferred;
+        }
+
+        /**
+         * Compare the valid lifetime in the IA prefix optin list and return the minimum one.
+         * TODO: exclude 0 valid lifetime.
+         */
+        public long getMinimalValidLifetime() {
+            final IaPrefixOption ipo = Collections.min(ipos,
+                    (IaPrefixOption lhs, IaPrefixOption rhs) -> Long.compare(lhs.valid, rhs.valid));
+            return ipo.valid;
+        }
     }
 
     /**