View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.net;
29  
30  import java.net.InetAddress;
31  import java.net.InetSocketAddress;
32  import java.net.SocketAddress;
33  import java.net.UnknownHostException;
34  import java.util.regex.Pattern;
35  
36  import org.apache.hc.core5.util.Args;
37  
38  /**
39   * A collection of utilities relating to InetAddresses.
40   *
41   * @since 4.0
42   */
43  public class InetAddressUtils {
44  
45      /**
46       * Represents the ipv4
47       *
48       * @since 5.1
49       */
50      public static final byte IPV4 = 1;
51      /**
52       * Represents the ipv6.
53       *
54       * @since 5.1
55       */
56      public static final byte IPV6 = 4;
57  
58      private InetAddressUtils() {
59      }
60  
61      private static final String IPV4_BASIC_PATTERN_STRING =
62              "(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){1}" + // initial first field, 1-255
63              "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){2}" + // following 2 fields, 0-255 followed by .
64               "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"; // final field, 0-255
65  
66      private static final Pattern IPV4_PATTERN =
67          Pattern.compile("^" + IPV4_BASIC_PATTERN_STRING + "$");
68  
69      private static final Pattern IPV4_MAPPED_IPV6_PATTERN = // TODO does not allow for redundant leading zeros
70              Pattern.compile("^::[fF]{4}:" + IPV4_BASIC_PATTERN_STRING + "$");
71  
72      private static final Pattern IPV6_STD_PATTERN =
73          Pattern.compile(
74                  "^[0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}$");
75  
76      private static final Pattern IPV6_HEX_COMPRESSED_PATTERN =
77          Pattern.compile(
78                  "^(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)" + // 0-6 hex fields
79                   "::" +
80                   "(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)$"); // 0-6 hex fields
81  
82      /**
83       * Regular expression pattern to match the scope ID in an IPv6 scoped address.
84       * The scope ID should be a non-empty string consisting of only alphanumeric characters or "-".
85       */
86      private static final Pattern SCOPE_ID_PATTERN = Pattern.compile("^[a-zA-Z0-9\\-]+$");
87  
88      /**
89       * Delimiter used to separate an IPv6 address from its scope ID.
90       */
91      private static final char SCOPE_ID_DELIMITER = '%';
92  
93  
94  
95      /*
96       *  The above pattern is not totally rigorous as it allows for more than 7 hex fields in total
97       */
98      private static final char COLON_CHAR = ':';
99  
100     // Must not have more than 7 colons (i.e. 8 fields)
101     private static final int MAX_COLON_COUNT = 7;
102 
103     /**
104      * @deprecated Use {@link #isIPv4(CharSequence)}
105      */
106     @Deprecated
107     public static boolean isIPv4Address(final String input) {
108         return isIPv4(input);
109     }
110 
111     /**
112      * Checks whether the parameter is a valid IPv4 address
113      *
114      * @param input the address character sequence to check for validity
115      * @return true if the input parameter is a valid IPv4 address
116      * @since 5.3
117      */
118     public static boolean isIPv4(final CharSequence input) {
119         return IPV4_PATTERN.matcher(input).matches();
120     }
121 
122     /**
123      * @deprecated Use {@link #isIPv4MappedIPv6(CharSequence)}
124      */
125     @Deprecated
126     public static boolean isIPv4MappedIPv64Address(final String input) {
127         return isIPv4MappedIPv6(input);
128     }
129 
130     /**
131      * Check if an IPv6 address is an IPv4-mapped IPv6 address.
132      *
133      * @param  input the IPv6 address to be checked
134      * @return true if the IPv6 address is an IPv4-mapped IPv6 address, false otherwise.
135      * @since 5.3
136      */
137     public static boolean isIPv4MappedIPv6(final CharSequence input) {
138         return IPV4_MAPPED_IPV6_PATTERN.matcher(input).matches();
139     }
140 
141     static boolean hasValidIPv6ColonCount(final CharSequence input) {
142         int colonCount = 0;
143         for (int i = 0; i < input.length(); i++) {
144             if (input.charAt(i) == COLON_CHAR) {
145                 colonCount++;
146             }
147         }
148         // IPv6 address must have at least 2 colons and not more than 7 (i.e. 8 fields)
149         return colonCount >= 2 && colonCount <= MAX_COLON_COUNT;
150     }
151 
152     /**
153      * @deprecated Use {@link #isIPv6Std(CharSequence)}
154      */
155     @Deprecated
156     public static boolean isIPv6StdAddress(final String input) {
157         return isIPv6Std(input);
158     }
159 
160     /**
161      * Checks whether the parameter is a valid standard (non-compressed) IPv6 address
162      *
163      * @param input the address character sequence to check for validity
164      * @return true if the input parameter is a valid standard (non-compressed) IPv6 address
165      * @since 5.3
166      */
167     public static boolean isIPv6Std(final CharSequence input) {
168         return hasValidIPv6ColonCount(input) && IPV6_STD_PATTERN.matcher(input).matches();
169     }
170 
171     /**
172      * @deprecated Use {@link #isIPv6HexCompressed(CharSequence)}
173      */
174     @Deprecated
175     public static boolean isIPv6HexCompressedAddress(final String input) {
176         return isIPv6HexCompressed(input);
177     }
178 
179     /**
180      * Checks whether the parameter is a valid compressed IPv6 address
181      *
182      * @param input the address character sequence to check for validity
183      * @return true if the input parameter is a valid compressed IPv6 address
184      * @since 5.3
185      */
186     public static boolean isIPv6HexCompressed(final CharSequence input) {
187         return hasValidIPv6ColonCount(input) && IPV6_HEX_COMPRESSED_PATTERN.matcher(input).matches();
188     }
189 
190     /**
191      * @deprecated Use {@link #isIPv6(CharSequence)}
192      */
193     @Deprecated
194     public static boolean isIPv6Address(final String input) {
195         return isIPv6(input);
196     }
197 
198     /**
199      * Checks whether the parameter is a valid IPv6 address (including compressed).
200      *
201      * @param input the address character sequence to check for validity
202      * @return true if the input parameter is a valid standard or compressed IPv6 address
203      * @since 5.3
204      */
205     public static boolean isIPv6(final CharSequence input) {
206         int index = -1;
207         for (int i = 0; i < input.length(); i++) {
208             if (input.charAt(i) == SCOPE_ID_DELIMITER) {
209                 index = i;
210                 break;
211             }
212         }
213         if (index == -1) {
214             return isIPv6Std(input) || isIPv6HexCompressed(input);
215         } else {
216             final CharSequence address = input.subSequence(0, index);
217             if (isIPv6Std(address) || isIPv6HexCompressed(address)) {
218                 // Check if the scope ID is valid
219                 final CharSequence scopeId = input.subSequence(index + 1, input.length());
220                 // Scope ID should be a non-empty character sequence consisting of only alphanumeric characters or "-"
221                 return scopeId.length() != 0 && SCOPE_ID_PATTERN.matcher(scopeId).matches();
222             } else {
223                 return false;
224             }
225         }
226     }
227 
228     /**
229      * @deprecated Use {@link #isIPv6URLBracketed(CharSequence)}
230      */
231     @Deprecated
232     public static boolean isIPv6URLBracketedAddress(final String input) {
233         return isIPv6URLBracketed(input);
234     }
235 
236     /**
237      * Checks whether the parameter is a valid URL formatted bracketed IPv6 address (including compressed).
238      * This matches only bracketed values e.g. {@code [::1]}.
239      *
240      * @param input the address character sequence to check for validity
241      * @return true if the input parameter is a valid URL-formatted bracketed IPv6 address
242      * @since 5.3
243      */
244     public static boolean isIPv6URLBracketed(final CharSequence input) {
245         if (input.length() == 0) {
246             return false;
247         }
248         return input.charAt(0) == '['
249                 && input.charAt(input.length() - 1) == ']'
250                 && isIPv6(input.subSequence(1, input.length() - 1));
251     }
252 
253     /**
254      * Formats {@link SocketAddress} as text.
255      *
256      * @since 5.0
257      */
258     public static void formatAddress(
259             final StringBuilder buffer,
260             final SocketAddress socketAddress) {
261         Args.notNull(buffer, "buffer");
262         if (socketAddress instanceof InetSocketAddress) {
263             final InetSocketAddress socketaddr = (InetSocketAddress) socketAddress;
264             final InetAddress inetaddr = socketaddr.getAddress();
265             if (inetaddr != null) {
266                 buffer.append(inetaddr.getHostAddress()).append(':').append(socketaddr.getPort());
267             } else {
268                 buffer.append(socketAddress);
269             }
270         } else {
271             buffer.append(socketAddress);
272         }
273     }
274 
275     /**
276      * Returns canonical name (fully qualified domain name) of the localhost.
277      *
278      * @since 5.0
279      */
280     public static String getCanonicalLocalHostName() {
281         try {
282             final InetAddress localHost = InetAddress.getLocalHost();
283             return localHost.getCanonicalHostName();
284         } catch (final UnknownHostException ex) {
285             return "localhost";
286         }
287     }
288 
289 }