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;
024
025/**
026 * <p><b>InetAddress</b> validation and conversion routines (<code>java.net.InetAddress</code>).</p>
027 *
028 * <p>This class provides methods to validate a candidate IP address.
029 *
030 * <p>
031 * This class is a Singleton; you can retrieve the instance via the {@link #getInstance()} method.
032 * </p>
033 *
034 * @version $Revision: 1650785 $
035 * @since Validator 1.4
036 */
037public class InetAddressValidator implements Serializable {
038
039    private static final long serialVersionUID = -919201640201914789L;
040
041    private static final String IPV4_REGEX =
042            "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
043
044    /**
045     * Singleton instance of this class.
046     */
047    private static final InetAddressValidator VALIDATOR = new InetAddressValidator();
048
049    /** IPv4 RegexValidator */
050    private final RegexValidator ipv4Validator = new RegexValidator(IPV4_REGEX);
051
052    /**
053     * Returns the singleton instance of this validator.
054     * @return the singleton instance of this validator
055     */
056    public static InetAddressValidator getInstance() {
057        return VALIDATOR;
058    }
059
060    /**
061     * Checks if the specified string is a valid IP address.
062     * @param inetAddress the string to validate
063     * @return true if the string validates as an IP address
064     */
065    public boolean isValid(String inetAddress) {
066        return isValidInet4Address(inetAddress) || isValidInet6Address(inetAddress);
067    }
068
069    /**
070     * Validates an IPv4 address. Returns true if valid.
071     * @param inet4Address the IPv4 address to validate
072     * @return true if the argument contains a valid IPv4 address
073     */
074    public boolean isValidInet4Address(String inet4Address) {
075        // verify that address conforms to generic IPv4 format
076        String[] groups = ipv4Validator.match(inet4Address);
077
078        if (groups == null) {
079            return false;
080        }
081
082        // verify that address subgroups are legal
083        for (int i = 0; i <= 3; i++) {
084            String ipSegment = groups[i];
085            if (ipSegment == null || ipSegment.length() == 0) {
086                return false;
087            }
088
089            int iIpSegment = 0;
090
091            try {
092                iIpSegment = Integer.parseInt(ipSegment);
093            } catch(NumberFormatException e) {
094                return false;
095            }
096
097            if (iIpSegment > 255) {
098                return false;
099            }
100
101            if (ipSegment.length() > 1 && ipSegment.startsWith("0")) {
102                return false;
103            }
104
105        }
106
107        return true;
108    }
109
110    /**
111     * Validates an IPv6 address. Returns true if valid.
112     * @param inet6Address the IPv6 address to validate
113     * @return true if the argument contains a valid IPv6 address
114     * 
115     * @since 1.4.1
116     */
117    public boolean isValidInet6Address(String inet6Address) {
118        boolean containsCompressedZeroes = inet6Address.indexOf("::") > -1; // contains is Java 1.5
119        if (containsCompressedZeroes && (inet6Address.indexOf("::") != inet6Address.lastIndexOf("::"))) {
120            return false;
121        }
122        if ((inet6Address.startsWith(":") && !inet6Address.startsWith("::"))
123                || (inet6Address.endsWith(":") && !inet6Address.endsWith("::"))) {
124            return false;
125        }
126        Object[] octets = inet6Address.split(":");
127        if (containsCompressedZeroes) {
128            List octetList = new ArrayList(Arrays.asList(octets));
129            if (inet6Address.endsWith("::")) {
130                // String.split() drops ending empty segments
131                octetList.add("");
132            } else if (inet6Address.startsWith("::") && !octetList.isEmpty()) {
133                octetList.remove(0);
134            }
135            octets = octetList.toArray();
136        }
137        if (octets.length > 8) {
138            return false;
139        }
140        int validOctets = 0;
141        int emptyOctets = 0;
142        for (int index = 0; index < octets.length; index++) {
143            String octet = (String) octets[index];
144            if (octet.length() == 0) {
145                emptyOctets++;
146                if (emptyOctets > 1) {
147                    return false;
148                }
149            } else {
150                emptyOctets = 0;
151                if (octet.indexOf(".") > -1) { // contains is Java 1.5+
152                    if (!inet6Address.endsWith(octet)) {
153                        return false;
154                    }
155                    if (index > octets.length - 1 || index > 6) {
156                        // IPV4 occupies last two octets
157                        return false;
158                    }
159                    if (!isValidInet4Address(octet)) {
160                        return false;
161                    }
162                    validOctets += 2;
163                    continue;
164                }
165                if (octet.length() > 4) {
166                    return false;
167                }
168                int octetInt = 0;
169                try {
170                    octetInt = Integer.valueOf(octet, 16).intValue();
171                } catch (NumberFormatException e) {
172                    return false;
173                }
174                if (octetInt < 0 || octetInt > 0xffff) {
175                    return false;
176                }
177            }
178            validOctets++;
179        }
180        if (validOctets < 8 && !containsCompressedZeroes) {
181            return false;
182        }
183        return true;
184    }
185}