001/*
002 * JPPF.
003 * Copyright (C) 2005-2016 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.net;
020
021import java.net.*;
022
023
024/**
025 * Represents a pattern used for IP addresses include or exclude lists.<br/>
026 * A pattern represents a single value or a range of values for each component of an IP address.<br/>
027 * <p>Examples:
028 * <ul>
029 * <li>192.168.1.10 represents a single IP address</li>
030 * <li>192.168.1 represents all IP addresses in the range 192.168.1.0 - 192.168.1.255</li>
031 * <li>192.168 represents all IP addresses in the range 192.168.0.0 - 192.168.255.255</li>
032 * <li>192.168-170.10 represents all IP addresses where the first component is equal to 192, the second in the range 168 - 170, the third equal to 10, and the fourth in the range 0 - 255
033 * (equivalent to 192.168-170.10.0-255)</li>
034 * <li>192.168-170.10.1-32 represents all IP addresses where the first component is equal to 192, the second in the range 12 - 120, the third equal to 10, and the fourth in the range 1 - 32</li>
035 * </ul>
036 * <p>Syntax rules:
037 * <p>1. An empty component is considered as a 0-255 range. Examples:
038 * <ul>
039 * <li>.2.3.4 is equivalent to 0-255.2.3.4</li>
040 * <li>1.2..4 is equivalent to 1.2.0-255.4</li>
041 * <li>1.2.3. is equivalent to 1.2.3 and to 1.2.3.0-255</li>
042 * </ul>
043 * <p>2. Ranges with missing bounds but still including the &quot;-&quot; sign are interpreted as a range with the lower bound
044 * equal to zero for a missing lower bound, and an upper bound equal to 255 if the upper bound is missing. Examples:
045 * <ul>
046 * <li>-128 is equivalent to 0-128</li>
047 * <li>12- is equivalent to 12-255</li>
048 * <li>- is equivalent to an empty value and to 0-255</li>
049 * </ul>
050 * <p>3. Valid values for range bounds and single values are positive integers in the range 0 ... 255. A pattern containing any invalid value will be ignored.
051 * <p>4. A pattern describing more than 4 components or containing characters other than decimal digits, '-', '.' or spaces will be ignored.
052 * @author Laurent Cohen
053 */
054public class IPv4AddressPattern extends AbstractIPAddressPattern
055{
056  /**
057   * Initialize this object with the specified string pattern.
058   * @param source the source pattern as a string.
059   * @throws IllegalArgumentException if the pattern is null or invalid.
060   */
061  public IPv4AddressPattern(final String source) throws IllegalArgumentException
062  {
063    super(source, PatternConfiguration.IPV4_CONFIGURATION);
064  }
065
066  /**
067   * {@inheritDoc}
068   */
069  @Override
070  public boolean matches(final InetAddress ip)
071  {
072    if (!(ip instanceof Inet4Address)) return false;
073    return super.matches(ip);
074  }
075
076  /**
077   * Main method.
078   * @param args not used.
079   */
080  public static void main(final String[] args)
081  {
082    System.out.println("***** IP v4 *****");
083    String[] ipv4patterns = {
084        "192.168.1.10", "192.168.1.11", "192.168.1", "192.168", "192.168-170.1", "192.168-170.1.1-32",
085        ".2.3.4", "1.2..4", "1.2.3.", "1.2.3.4-", "1.2.3.-4", " 1. 2 .  3. 4 - 8 ", "1.-.3.", "1.2.3.-",
086    };
087    //String[] patterns = { " 1. 2 .  3. 4 - 8 " };
088    String ip = "192.168.1.11";
089    for (int i=0; i<ipv4patterns.length; i++)
090    {
091      try
092      {
093        IPv4AddressPattern p = new IPv4AddressPattern(ipv4patterns[i]);
094        InetAddress addr = InetAddress.getByName(ip);
095        System.out.println("pattern " + i + " for source '" + ipv4patterns[i] + "' = '" + p + "', ip match = " + p.matches(addr));
096      }
097      catch (Exception e)
098      {
099        System.out.println("#" + i + " : " + e.getMessage());
100      }
101    }
102  }
103}