diff options
| author | Etan Cohen <etancohen@google.com> | 2018-11-02 15:07:20 -0700 |
|---|---|---|
| committer | Etan Cohen <etancohen@google.com> | 2018-11-16 14:00:22 -0800 |
| commit | a4824cf4016736c6cc6d153b6465f6a8140218c7 (patch) | |
| tree | 1a7e208980bd7d3cb5dcf97c90410fee954697ee /core/java/android/net/MacAddress.java | |
| parent | 2938bf7f773f982fceee54a17a3e933a8ffe8cf5 (diff) | |
Add IPv6 link-local address generation from EUI-48
Add a utility method to convert an EUI-48 to an IPv6 link-local
address based on RFC 4291 (EUI-64 generation) followed by RFC 4862.
Bug: 117605977
Test: atest MacAddressTest
Change-Id: I80b683e69da6beff3b37fc345fc15aa9610d09b7
Diffstat (limited to 'core/java/android/net/MacAddress.java')
| -rw-r--r-- | core/java/android/net/MacAddress.java | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/core/java/android/net/MacAddress.java b/core/java/android/net/MacAddress.java index 4cd000113b7e..4e551756198e 100644 --- a/core/java/android/net/MacAddress.java +++ b/core/java/android/net/MacAddress.java @@ -18,6 +18,7 @@ package android.net; import android.annotation.IntDef; import android.annotation.NonNull; +import android.annotation.Nullable; import android.annotation.UnsupportedAppUsage; import android.os.Parcel; import android.os.Parcelable; @@ -27,6 +28,8 @@ import com.android.internal.util.Preconditions; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.net.Inet6Address; +import java.net.UnknownHostException; import java.security.SecureRandom; import java.util.Arrays; import java.util.Random; @@ -408,4 +411,34 @@ public final class MacAddress implements Parcelable { Preconditions.checkNotNull(mask); return (mAddr & mask.mAddr) == (baseAddress.mAddr & mask.mAddr); } + + /** + * Create a link-local Inet6Address from the MAC address. The EUI-48 MAC address is converted + * to an EUI-64 MAC address per RFC 4291. The resulting EUI-64 is used to construct a link-local + * IPv6 address per RFC 4862. + * + * @return A link-local Inet6Address constructed from the MAC address. + * @hide + */ + public @Nullable Inet6Address getLinkLocalIpv6FromEui48Mac() { + byte[] macEui48Bytes = toByteArray(); + byte[] addr = new byte[16]; + + addr[0] = (byte) 0xfe; + addr[1] = (byte) 0x80; + addr[8] = (byte) (macEui48Bytes[0] ^ (byte) 0x02); // flip the link-local bit + addr[9] = macEui48Bytes[1]; + addr[10] = macEui48Bytes[2]; + addr[11] = (byte) 0xff; + addr[12] = (byte) 0xfe; + addr[13] = macEui48Bytes[3]; + addr[14] = macEui48Bytes[4]; + addr[15] = macEui48Bytes[5]; + + try { + return Inet6Address.getByAddress(null, addr, 0); + } catch (UnknownHostException e) { + return null; + } + } } |
