Java InetAddress doMatch(InetAddress inetAddress, String toMatchParam)

Here you can find the source of doMatch(InetAddress inetAddress, String toMatchParam)

Description

Checks a string macthes with a IP address.

License

Open Source License

Parameter

Parameter Description
inetAddress ont of addresses of the machine
toMatch can be either an IP, an IP with wild card "*" on the last octet or an IP with a range on last octet (e.g. 196.168.35.10-100)

Return

true if the toMatch string matches the hostAddress from the InetAddress

Declaration

private static boolean doMatch(InetAddress inetAddress, String toMatchParam) 

Method Source Code

//package com.java2s;
/**// www  .  j a  v a 2s.c o  m
JEM, the BEE - Job Entry Manager, the Batch Execution Environment
Copyright (C) 2012-2015   Simone "Busy" Businaro
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.net.InetAddress;

public class Main {
    /**
     * Checks a string macthes with a IP address.
     * <br>
     * String is the NIC interface defined inside of Hazecast configuration.
     * <br>
     * INetaddress is one of addresses of the machine.
     * 
     * @param inetAddress ont of addresses of the machine
     * @param toMatch can be either an IP, an IP with wild card "*" on the last
     *            octet or an IP with a range on last octet (e.g. 196.168.35.10-100)
     * @return true if the toMatch string matches the hostAddress from the
     *         InetAddress
     */
    private static boolean doMatch(InetAddress inetAddress, String toMatchParam) {
        // gets host ip address
        String hostAddres = inetAddress.getHostAddress();
        // if HC string contains "-", 
        // means contains more than 1 ip address (at last OCTECT)
        if (toMatchParam.contains("-")) {
            // if is ipv6 address do not consider
            if (hostAddres.contains(":")) {
                return false;
            }
            // splits the ip HC string address 
            String[] octects = toMatchParam.split("\\.");
            // splits the local IP address
            String[] octectsIA = hostAddres.split("\\.");
            // composes again strings which represent
            // the two ip addresses
            String first3 = octects[0] + "." + octects[1] + "." + octects[2];
            String firstIA = octectsIA[0] + "." + octectsIA[1] + "." + octectsIA[2];
            // compares the ip addresses
            if (!first3.equals(firstIA)) {
                return false;
            }
            // gets last octect
            String lastOctect = octects[3];
            // gets the range of IP address
            String[] ranges = lastOctect.split("-");
            // min and max of ip address
            int min = Integer.parseInt(ranges[0]);
            int max = Integer.parseInt(ranges[1]);
            // parses the last octect as integer
            int inetAddressLastOctect = Integer.parseInt(octectsIA[3]);
            // checks if the last octect is inside
            // of the range of HC config
            return inetAddressLastOctect >= min && inetAddressLastOctect <= max;
        } else {
            // checks the 2 ip addresses by regex
            String toMatch = toMatchParam.replace(".", "\\.").replace("*", "\\w*");
            return hostAddres.matches(toMatch);
        }
    }
}

Related

  1. createAbsoluteURL(InetAddress address, int localStreamPort, URI relativeOrNot)
  2. createDatagramSocket(InetAddress addr, int port)
  3. createInetSocketAddress(final InetAddress address)
  4. createResolved(InetAddress address, int port)
  5. createSocket(InetAddress address, int port, boolean fixedPort)
  6. findFreePort(InetAddress address, int start, int end)
  7. findUnusedPort(InetAddress address, int from, int to)
  8. findUnusedPort(InetAddress address, int low, int high)
  9. formatAddress(InetAddress address, int port, Context cx, Scriptable scope)