001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.validator.routines; 019 020import java.io.Serializable; 021import java.util.ArrayList; 022import java.util.Arrays; 023import java.util.List; 024import java.util.regex.Pattern; 025 026import org.apache.commons.validator.GenericValidator; 027 028/** 029 * <p><b>InetAddress</b> validation and conversion routines (<code>java.net.InetAddress</code>).</p> 030 * 031 * <p>This class provides methods to validate a candidate IP address. 032 * 033 * <p> 034 * This class is a Singleton; you can retrieve the instance via the {@link #getInstance()} method. 035 * </p> 036 * 037 * @since 1.4 038 */ 039public class InetAddressValidator implements Serializable { 040 041 private static final int MAX_BYTE = 128; 042 043 private static final int IPV4_MAX_OCTET_VALUE = 255; 044 045 private static final int MAX_UNSIGNED_SHORT = 0xffff; 046 047 private static final int BASE_16 = 16; 048 049 private static final long serialVersionUID = -919201640201914789L; 050 051 private static final String IPV4_REGEX = 052 "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"; 053 054 // Max number of hex groups (separated by :) in an IPV6 address 055 private static final int IPV6_MAX_HEX_GROUPS = 8; 056 057 // Max hex digits in each IPv6 group 058 private static final int IPV6_MAX_HEX_DIGITS_PER_GROUP = 4; 059 060 /** 061 * Singleton instance of this class. 062 */ 063 private static final InetAddressValidator VALIDATOR = new InetAddressValidator(); 064 065 private static final Pattern DIGITS_PATTERN = Pattern.compile("\\d{1,3}"); 066 067 private static final Pattern ID_CHECK_PATTERN = Pattern.compile("[^\\s/%]+"); 068 /** 069 * Returns the singleton instance of this validator. 070 * @return the singleton instance of this validator 071 */ 072 public static InetAddressValidator getInstance() { 073 return VALIDATOR; 074 } 075 076 /** IPv4 RegexValidator */ 077 private final RegexValidator ipv4Validator = new RegexValidator(IPV4_REGEX); 078 079 /** 080 * Checks if the specified string is a valid IPv4 or IPv6 address. 081 * @param inetAddress the string to validate 082 * @return true if the string validates as an IP address 083 */ 084 public boolean isValid(final String inetAddress) { 085 return isValidInet4Address(inetAddress) || isValidInet6Address(inetAddress); 086 } 087 088 /** 089 * Validates an IPv4 address. Returns true if valid. 090 * @param inet4Address the IPv4 address to validate 091 * @return true if the argument contains a valid IPv4 address 092 */ 093 public boolean isValidInet4Address(final String inet4Address) { 094 // verify that address conforms to generic IPv4 format 095 final String[] groups = ipv4Validator.match(inet4Address); 096 if (groups == null) { 097 return false; 098 } 099 // verify that address subgroups are legal 100 for (final String ipSegment : groups) { 101 if (GenericValidator.isBlankOrNull(ipSegment)) { 102 return false; 103 } 104 int iIpSegment = 0; 105 try { 106 iIpSegment = Integer.parseInt(ipSegment); 107 } catch (final NumberFormatException e) { 108 return false; 109 } 110 if (iIpSegment > IPV4_MAX_OCTET_VALUE) { 111 return false; 112 } 113 if (ipSegment.length() > 1 && ipSegment.startsWith("0")) { 114 return false; 115 } 116 } 117 return true; 118 } 119 120 /** 121 * Validates an IPv6 address. Returns true if valid. 122 * @param inet6Address the IPv6 address to validate 123 * @return true if the argument contains a valid IPv6 address 124 * 125 * @since 1.4.1 126 */ 127 public boolean isValidInet6Address(String inet6Address) { 128 String[] parts; 129 // remove prefix size. This will appear after the zone id (if any) 130 parts = inet6Address.split("/", -1); 131 if (parts.length > 2) { 132 return false; // can only have one prefix specifier 133 } 134 if (parts.length == 2) { 135 if (!DIGITS_PATTERN.matcher(parts[1]).matches()) { 136 return false; // not a valid number 137 } 138 final int bits = Integer.parseInt(parts[1]); // cannot fail because of RE check 139 if (bits < 0 || bits > MAX_BYTE) { 140 return false; // out of range 141 } 142 } 143 // remove zone-id 144 parts = parts[0].split("%", -1); 145 if (parts.length > 2) { 146 return false; 147 } 148 // The id syntax is implementation independent, but it presumably cannot allow: 149 // whitespace, '/' or '%' 150 if (parts.length == 2 && !ID_CHECK_PATTERN.matcher(parts[1]).matches()) { 151 return false; // invalid id 152 } 153 inet6Address = parts[0]; 154 final boolean containsCompressedZeroes = inet6Address.contains("::"); 155 if (containsCompressedZeroes && inet6Address.indexOf("::") != inet6Address.lastIndexOf("::")) { 156 return false; 157 } 158 if (inet6Address.startsWith(":") && !inet6Address.startsWith("::") 159 || inet6Address.endsWith(":") && !inet6Address.endsWith("::")) { 160 return false; 161 } 162 String[] octets = inet6Address.split(":"); 163 if (containsCompressedZeroes) { 164 final List<String> octetList = new ArrayList<>(Arrays.asList(octets)); 165 if (inet6Address.endsWith("::")) { 166 // String.split() drops ending empty segments 167 octetList.add(""); 168 } else if (inet6Address.startsWith("::") && !octetList.isEmpty()) { 169 octetList.remove(0); 170 } 171 octets = octetList.toArray(new String[0]); 172 } 173 if (octets.length > IPV6_MAX_HEX_GROUPS) { 174 return false; 175 } 176 int validOctets = 0; 177 int emptyOctets = 0; // consecutive empty chunks 178 for (int index = 0; index < octets.length; index++) { 179 final String octet = octets[index]; 180 if (GenericValidator.isBlankOrNull(octet)) { 181 emptyOctets++; 182 if (emptyOctets > 1) { 183 return false; 184 } 185 } else { 186 emptyOctets = 0; 187 // Is last chunk an IPv4 address? 188 if (index == octets.length - 1 && octet.contains(".")) { 189 if (!isValidInet4Address(octet)) { 190 return false; 191 } 192 validOctets += 2; 193 continue; 194 } 195 if (octet.length() > IPV6_MAX_HEX_DIGITS_PER_GROUP) { 196 return false; 197 } 198 int octetInt = 0; 199 try { 200 octetInt = Integer.parseInt(octet, BASE_16); 201 } catch (final NumberFormatException e) { 202 return false; 203 } 204 if (octetInt < 0 || octetInt > MAX_UNSIGNED_SHORT) { 205 return false; 206 } 207 } 208 validOctets++; 209 } 210 if (validOctets > IPV6_MAX_HEX_GROUPS || validOctets < IPV6_MAX_HEX_GROUPS && !containsCompressedZeroes) { 211 return false; 212 } 213 return true; 214 } 215}