1 /*
2  * Copyright (C) 2018 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 package libcore.net;
17 
18 import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
19 
20 import android.annotation.SystemApi;
21 
22 import android.system.GaiException;
23 import android.system.StructAddrinfo;
24 import java.net.Inet4Address;
25 import java.net.Inet6Address;
26 import java.net.InetAddress;
27 import libcore.io.Libcore;
28 
29 import static android.system.OsConstants.AF_INET;
30 import static android.system.OsConstants.AI_NUMERICHOST;
31 
32 /**
33  * Android specific utility methods for {@link InetAddress} instances.
34  *
35  * @hide
36  */
37 @SystemApi(client = MODULE_LIBRARIES)
38 public class InetAddressUtils {
39 
40     private static final int NETID_UNSET = 0;
41 
InetAddressUtils()42     private InetAddressUtils() {
43     }
44 
45     /**
46      * Checks to see if the {@code address} is a numeric address (such as {@code "192.0.2.1"} or
47      * {@code "2001:db8::1:2"}).
48      *
49      * <p>A numeric address is either an IPv4 address containing exactly 4 decimal numbers or an
50      * IPv6 numeric address. IPv4 addresses that consist of either hexadecimal or octal digits or
51      * do not have exactly 4 numbers are not treated as numeric.
52      *
53      * <p>This method will never do a DNS lookup.
54      *
55      * @param address the address to parse.
56      * @return true if the supplied address is numeric, false otherwise.
57      *
58      * @hide
59      */
60     @SystemApi(client = MODULE_LIBRARIES)
isNumericAddress(String address)61     public static boolean isNumericAddress(String address) {
62         return parseNumericAddressNoThrow(address) != null;
63     }
64 
65     /**
66      * Returns an InetAddress corresponding to the given numeric address (such
67      * as {@code "192.168.0.1"} or {@code "2001:4860:800d::68"}).
68      *
69      * <p>See {@link #isNumericAddress(String)} for a definition as to what constitutes a numeric
70      * address.
71      *
72      * <p>This method will never do a DNS lookup.
73      *
74      * @param address the address to parse, must be numeric.
75      * @return an {@link InetAddress} instance corresponding to the address.
76      * @throws IllegalArgumentException if {@code address} is not a numeric address.
77      *
78      * @hide
79      */
80     @SystemApi(client = MODULE_LIBRARIES)
parseNumericAddress(String address)81     public static InetAddress parseNumericAddress(String address) {
82         InetAddress result = parseNumericAddressNoThrow(address);
83         if (result == null) {
84             throw new IllegalArgumentException("Not a numeric address: " + address);
85         }
86         return result;
87     }
88 
89     /**
90      * @hide
91      */
parseNumericAddressNoThrow(String address)92     public static InetAddress parseNumericAddressNoThrow(String address) {
93         StructAddrinfo hints = new StructAddrinfo();
94         hints.ai_flags = AI_NUMERICHOST;
95         InetAddress[] addresses = null;
96         try {
97             addresses = Libcore.os.android_getaddrinfo(address, hints, NETID_UNSET);
98         } catch (GaiException ignored) {
99         }
100         if (addresses == null) {
101             return null;
102         }
103         return addresses[0];
104     }
105 
106     /**
107      * Like {@link #parseNumericAddressNoThrow(String)}}, but strips optional []
108      * around a numeric IPv6 address.
109      *
110      * @hide
111      */
parseNumericAddressNoThrowStripOptionalBrackets(String address)112     public static InetAddress parseNumericAddressNoThrowStripOptionalBrackets(String address) {
113         // Accept IPv6 addresses (only) in square brackets for compatibility.
114         if (address.startsWith("[") && address.endsWith("]") && address.indexOf(':') != -1) {
115             address = address.substring(1, address.length() - 1);
116         }
117         return InetAddressUtils.parseNumericAddressNoThrow(address);
118     }
119 }
120