001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 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 * Represents a pattern used for IP addresses include or exclude lists.<br/>
025 * A pattern represents a single value or a range of values for each component of an IP address.<br/>
026 * <p>Examples:
027 * <ul>
028 * <li>192.168.1.10 represents a single IP address</li>
029 * <li>192.168.1 represents all IP addresses in the range 192.168.1.0 - 192.168.1.255</li>
030 * <li>192.168 represents all IP addresses in the range 192.168.0.0 - 192.168.255.255</li>
031 * <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
032 * (equivalent to 192.168-170.10.0-255)</li>
033 * <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>
034 * </ul>
035 * <p>Syntax rules:
036 * <p>1. An empty component is considered as a 0-255 range. Examples:
037 * <ul>
038 * <li>.2.3.4 is equivalent to 0-255.2.3.4</li>
039 * <li>1.2..4 is equivalent to 1.2.0-255.4</li>
040 * <li>1.2.3. is equivalent to 1.2.3 and to 1.2.3.0-255</li>
041 * </ul>
042 * <p>2. Ranges with missing bounds but still including the &quot;-&quot; sign are interpreted as a range with the lower bound
043 * equal to zero for a missing lower bound, and an upper bound equal to 255 if the upper bound is missing. Examples:
044 * <ul>
045 * <li>-128 is equivalent to 0-128</li>
046 * <li>12- is equivalent to 12-255</li>
047 * <li>- is equivalent to an empty value and to 0-255</li>
048 * </ul>
049 * <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.
050 * <p>4. A pattern describing more than 4 components or containing characters other than decimal digits, '-', '.' or spaces will be ignored.
051 * @author Laurent Cohen
052 */
053public class IPv4AddressPattern extends AbstractIPAddressPattern {
054  /**
055   * Initialize this object with the specified string pattern.
056   * @param source the source pattern as a string.
057   * @throws IllegalArgumentException if the pattern is null or invalid.
058   */
059  public IPv4AddressPattern(final String source) throws IllegalArgumentException {
060    super(source, PatternConfiguration.IPV4_CONFIGURATION);
061  }
062
063  /**
064   * {@inheritDoc}
065   */
066  @Override
067  public boolean matches(final InetAddress ip) {
068    if (!(ip instanceof Inet4Address)) return false;
069    return super.matches(ip);
070  }
071
072  /**
073   * Main method.
074   * @param args not used.
075   */
076  public static void main(final String[] args) {
077    System.out.println("***** IP v4 *****");
078    final String[] ipv4patterns = { "192.168.1.10", "192.168.1.11", "192.168.1", "192.168", "192.168-170.1", "192.168-170.1.1-32", ".2.3.4", "1.2..4", "1.2.3.", "1.2.3.4-", "1.2.3.-4", " 1. 2 .  3. 4 - 8 ",
079      "1.-.3.", "1.2.3.-", };
080    //String[] patterns = { " 1. 2 .  3. 4 - 8 " };
081    final String ip = "192.168.1.11";
082    for (int i = 0; i < ipv4patterns.length; i++) {
083      try {
084        final IPv4AddressPattern p = new IPv4AddressPattern(ipv4patterns[i]);
085        final InetAddress addr = InetAddress.getByName(ip);
086        System.out.println("pattern " + i + " for source '" + ipv4patterns[i] + "' = '" + p + "', ip match = " + p.matches(addr));
087      } catch (final Exception e) {
088        System.out.println("#" + i + " : " + e.getMessage());
089      }
090    }
091  }
092}