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