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