• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net.util;
18 
19 import android.net.IpPrefix;
20 import android.net.LinkAddress;
21 import android.net.LinkProperties;
22 
23 import java.net.Inet4Address;
24 import java.net.InetAddress;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.Set;
28 
29 
30 /**
31  * @hide
32  */
33 public class PrefixUtils {
34     private static final IpPrefix[] MIN_NON_FORWARDABLE_PREFIXES = {
35             pfx("127.0.0.0/8"),     // IPv4 loopback
36             pfx("169.254.0.0/16"),  // IPv4 link-local, RFC3927#section-8
37             pfx("::/3"),
38             pfx("fe80::/64"),       // IPv6 link-local
39             pfx("fc00::/7"),        // IPv6 ULA
40             pfx("ff02::/8"),        // IPv6 link-local multicast
41     };
42 
43     public static final IpPrefix DEFAULT_WIFI_P2P_PREFIX = pfx("192.168.49.0/24");
44 
getNonForwardablePrefixes()45     public static Set<IpPrefix> getNonForwardablePrefixes() {
46         final HashSet<IpPrefix> prefixes = new HashSet<>();
47         addNonForwardablePrefixes(prefixes);
48         return prefixes;
49     }
50 
addNonForwardablePrefixes(Set<IpPrefix> prefixes)51     public static void addNonForwardablePrefixes(Set<IpPrefix> prefixes) {
52         Collections.addAll(prefixes, MIN_NON_FORWARDABLE_PREFIXES);
53     }
54 
localPrefixesFrom(LinkProperties lp)55     public static Set<IpPrefix> localPrefixesFrom(LinkProperties lp) {
56         final HashSet<IpPrefix> localPrefixes = new HashSet<>();
57         if (lp == null) return localPrefixes;
58 
59         for (LinkAddress addr : lp.getAllLinkAddresses()) {
60             if (addr.getAddress().isLinkLocalAddress()) continue;
61             localPrefixes.add(asIpPrefix(addr));
62         }
63         // TODO: Add directly-connected routes as well (ones from which we did
64         // not also form a LinkAddress)?
65 
66         return localPrefixes;
67     }
68 
asIpPrefix(LinkAddress addr)69     public static IpPrefix asIpPrefix(LinkAddress addr) {
70         return new IpPrefix(addr.getAddress(), addr.getPrefixLength());
71     }
72 
ipAddressAsPrefix(InetAddress ip)73     public static IpPrefix ipAddressAsPrefix(InetAddress ip) {
74         final int bitLength = (ip instanceof Inet4Address)
75                 ? NetworkConstants.IPV4_ADDR_BITS
76                 : NetworkConstants.IPV6_ADDR_BITS;
77         return new IpPrefix(ip, bitLength);
78     }
79 
pfx(String prefixStr)80     private static IpPrefix pfx(String prefixStr) {
81         return new IpPrefix(prefixStr);
82     }
83 }
84