View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  /*
20   * This software consists of voluntary contributions made by many
21   * individuals on behalf of the Apache Software Foundation.  For more
22   * information on the Apache Software Foundation, please see
23   * <http://www.apache.org/>.
24   *
25   */
26  
27  package org.apache.wss4j.common.util;
28  
29  import java.util.regex.Pattern;
30  
31  /**
32   * A collection of utilities relating to InetAddresses.
33   *
34   * Copied from httpclient.
35   */
36  public final class InetAddressUtils {
37  
38      private static final String IPV4_BASIC_PATTERN_STRING =
39              "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}" // initial 3 fields, 0-255 followed by .
40              + "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"; // final field, 0-255
41  
42      private static final Pattern IPV4_PATTERN =
43          Pattern.compile("^" + IPV4_BASIC_PATTERN_STRING + "$");
44  
45      private static final Pattern IPV4_MAPPED_IPV6_PATTERN = // TODO does not allow for redundant leading zeros
46              Pattern.compile("^::[fF]{4}:" + IPV4_BASIC_PATTERN_STRING + "$");
47  
48      private static final Pattern IPV6_STD_PATTERN =
49          Pattern.compile(
50                  "^[0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}$");
51  
52      private static final Pattern IPV6_HEX_COMPRESSED_PATTERN =
53          Pattern.compile(
54                  "^(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)" // 0-6 hex fields
55                  + "::"
56                  + "(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)$"); // 0-6 hex fields
57  
58      /*
59       *  The above pattern is not totally rigorous as it allows for more than 7 hex fields in total
60       */
61      private static final char COLON_CHAR = ':';
62  
63      // Must not have more than 7 colons (i.e. 8 fields)
64      private static final int MAX_COLON_COUNT = 7;
65  
66      private InetAddressUtils() {
67      }
68  
69      /**
70       * Checks whether the parameter is a valid IPv4 address
71       *
72       * @param input the address string to check for validity
73       * @return true if the input parameter is a valid IPv4 address
74       */
75      public static boolean isIPv4Address(final String input) {
76          return IPV4_PATTERN.matcher(input).matches();
77      }
78  
79      public static boolean isIPv4MappedIPv64Address(final String input) {
80          return IPV4_MAPPED_IPV6_PATTERN.matcher(input).matches();
81      }
82  
83      /**
84       * Checks whether the parameter is a valid standard (non-compressed) IPv6 address
85       *
86       * @param input the address string to check for validity
87       * @return true if the input parameter is a valid standard (non-compressed) IPv6 address
88       */
89      public static boolean isIPv6StdAddress(final String input) {
90          return IPV6_STD_PATTERN.matcher(input).matches();
91      }
92  
93      /**
94       * Checks whether the parameter is a valid compressed IPv6 address
95       *
96       * @param input the address string to check for validity
97       * @return true if the input parameter is a valid compressed IPv6 address
98       */
99      public static boolean isIPv6HexCompressedAddress(final String input) {
100         int colonCount = 0;
101         int length = input.length();
102         for (int i = 0; i < length; i++) {
103             if (input.charAt(i) == COLON_CHAR) {
104                 colonCount++;
105             }
106         }
107         return colonCount <= MAX_COLON_COUNT && IPV6_HEX_COMPRESSED_PATTERN.matcher(input).matches();
108     }
109 
110     /**
111      * Checks whether the parameter is a valid IPv6 address (including compressed).
112      *
113      * @param input the address string to check for validity
114      * @return true if the input parameter is a valid standard or compressed IPv6 address
115      */
116     public static boolean isIPv6Address(final String input) {
117         return isIPv6StdAddress(input) || isIPv6HexCompressedAddress(input);
118     }
119 
120 }