    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 *      https://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><strong>InetAddress</strong> validation and conversion routines ({@code java.net.InetAddress}).</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 = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
    052
    053    // Max number of hex groups (separated by :) in an IPV6 address
    054    private static final int IPV6_MAX_HEX_GROUPS = 8;
    055
    056    // Max hex digits in each IPv6 group
    057    private static final int IPV6_MAX_HEX_DIGITS_PER_GROUP = 4;
    058
    059    /**
    060     * Singleton instance of this class.
    061     */
    062    private static final InetAddressValidator VALIDATOR = new InetAddressValidator();
    063
    064    private static final Pattern DIGITS_PATTERN = Pattern.compile("\\d{1,3}");
    065
    066    private static final Pattern ID_CHECK_PATTERN = Pattern.compile("[^\\s/%]+");
    067
    068    /** IPv4 RegexValidator */
    069    private static final RegexValidator IPV4_VALIDATOR = new RegexValidator(IPV4_REGEX);
    070
    071    /**
    072     * Returns the singleton instance of this validator.
    073     *
    074     * @return the singleton instance of this validator
    075     */
    076    public static InetAddressValidator getInstance() {
    077        return VALIDATOR;
    078    }
    079
    080    /**
    081     * Constructs a new instance.
    082     */
    083    public InetAddressValidator() {
    084        // empty
    085    }
    086
    087    /**
    088     * Checks if the specified string is a valid IPv4 or IPv6 address.
    089     *
    090     * @param inetAddress the string to validate
    091     * @return true if the string validates as an IP address
    092     */
    093    public boolean isValid(final String inetAddress) {
    094        return isValidInet4Address(inetAddress) || isValidInet6Address(inetAddress);
    095    }
    096
    097    /**
    098     * Validates an IPv4 address. Returns true if valid.
    099     *
    100     * @param inet4Address the IPv4 address to validate
    101     * @return true if the argument contains a valid IPv4 address
    102     */
    103    public boolean isValidInet4Address(final String inet4Address) {
    104        // verify that address conforms to generic IPv4 format
    105        final String[] groups = IPV4_VALIDATOR.match(inet4Address);
    106        if (groups == null) {
    107            return false;
    108        }
    109        // verify that address subgroups are legal
    110        for (final String ipSegment : groups) {
    111            if (GenericValidator.isBlankOrNull(ipSegment)) {
    112                return false;
    113            }
    114            int iIpSegment = 0;
    115            try {
    116                iIpSegment = Integer.parseInt(ipSegment);
    117            } catch (final NumberFormatException e) {
    118                return false;
    119            }
    120            if (iIpSegment > IPV4_MAX_OCTET_VALUE || ipSegment.length() > 1 && ipSegment.startsWith("0")) {
    121                return false;
    122            }
    123        }
    124        return true;
    125    }
    126
    127    /**
    128     * Validates an IPv6 address. Returns true if valid.
    129     *
    130     * @param inet6Address the IPv6 address to validate
    131     * @return true if the argument contains a valid IPv6 address
    132     * @since 1.4.1
    133     */
    134    public boolean isValidInet6Address(String inet6Address) {
    135        String[] parts;
    136        // remove prefix size. This will appear after the zone id (if any)
    137        parts = inet6Address.split("/", -1);
    138        if (parts.length > 2) {
    139            return false; // can only have one prefix specifier
    140        }
    141        if (parts.length == 2) {
    142            if (!DIGITS_PATTERN.matcher(parts[1]).matches()) {
    143                return false; // not a valid number
    144            }
    145            final int bits = Integer.parseInt(parts[1]); // cannot fail because of RE check
    146            if (bits < 0 || bits > MAX_BYTE) {
    147                return false; // out of range
    148            }
    149        }
    150        // remove zone-id
    151        parts = parts[0].split("%", -1);
    152        // The id syntax is implementation independent, but it presumably cannot allow:
    153        // whitespace, '/' or '%'
    154        if (parts.length > 2 || parts.length == 2 && !ID_CHECK_PATTERN.matcher(parts[1]).matches()) {
    155            return false; // invalid id
    156        }
    157        inet6Address = parts[0];
    158        final boolean containsCompressedZeroes = inet6Address.contains("::");
    159        if (containsCompressedZeroes && inet6Address.indexOf("::") != inet6Address.lastIndexOf("::")) {
    160            return false;
    161        }
    162        final boolean startsWithCompressed = inet6Address.startsWith("::");
    163        final boolean endsWithCompressed = inet6Address.endsWith("::");
    164        final boolean endsWithSep = inet6Address.endsWith(":");
    165        if (inet6Address.startsWith(":") && !startsWithCompressed || endsWithSep && !endsWithCompressed) {
    166            return false;
    167        }
    168        String[] octets = inet6Address.split(":");
    169        if (containsCompressedZeroes) {
    170            final List<String> octetList = new ArrayList<>(Arrays.asList(octets));
    171            if (endsWithCompressed) {
    172                // String.split() drops ending empty segments
    173                octetList.add("");
    174            } else if (startsWithCompressed && !octetList.isEmpty()) {
    175                octetList.remove(0);
    176            }
    177            octets = octetList.toArray(new String[0]);
    178        }
    179        if (octets.length > IPV6_MAX_HEX_GROUPS) {
    180            return false;
    181        }
    182        int validOctets = 0;
    183        int emptyOctets = 0; // consecutive empty chunks
    184        for (int index = 0; index < octets.length; index++) {
    185            final String octet = octets[index];
    186            if (GenericValidator.isBlankOrNull(octet)) {
    187                emptyOctets++;
    188                if (emptyOctets > 1) {
    189                    return false;
    190                }
    191            } else {
    192                emptyOctets = 0;
    193                // Is last chunk an IPv4 address?
    194                if (index == octets.length - 1 && octet.contains(".")) {
    195                    if (!isValidInet4Address(octet)) {
    196                        return false;
    197                    }
    198                    validOctets += 2;
    199                    continue;
    200                }
    201                if (octet.length() > IPV6_MAX_HEX_DIGITS_PER_GROUP) {
    202                    return false;
    203                }
    204                int octetInt = 0;
    205                try {
    206                    octetInt = Integer.parseInt(octet, BASE_16);
    207                } catch (final NumberFormatException e) {
    208                    return false;
    209                }
    210                if (octetInt < 0 || octetInt > MAX_UNSIGNED_SHORT) {
    211                    return false;
    212                }
    213            }
    214            validOctets++;
    215        }
    216        if (validOctets > IPV6_MAX_HEX_GROUPS || validOctets < IPV6_MAX_HEX_GROUPS && !containsCompressedZeroes) {
    217            return false;
    218        }
    219        return true;
    220    }
    221}