Java InetAddress Check isValidCiscoWildcard(InetAddress wildcard)

Here you can find the source of isValidCiscoWildcard(InetAddress wildcard)

Description

Determines if a specified address is a valid Cisco Wildcard (cisco's representation of a netmask)

License

Open Source License

Parameter

Parameter Description
wildcard InetAddress

Return

boolean

Declaration

public static boolean isValidCiscoWildcard(InetAddress wildcard) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;

import java.net.UnknownHostException;

public class Main {
    /**//from   ww  w  .  j  a  va  2  s .c  o  m
     * Determines if a specified address is a valid Cisco Wildcard (cisco's representation of a netmask)
     *
     * @param wildcard
     *       InetAddress
     *
     * @return boolean
     */
    public static boolean isValidCiscoWildcard(InetAddress wildcard) {
        byte[] segments = wildcard.getAddress();

        for (int i = 0; i < segments.length; i++) {
            assert (((byte) ~(byte) ~segments[i]) == segments[i]);
            segments[i] = (byte) ~segments[i];
        }

        return isValidNetmask(segments);
    }

    /**
     * Validates a netmask
     *
     * @param netmask
     *       String A netmask to test
     *
     * @return boolean True if it validates, otherwise false
     */
    public static boolean isValidNetmask(String netmask) {
        try {
            return isValidNetmask(InetAddress.getByName(netmask));
        } catch (UnknownHostException e) {
            return false;
        }
    }

    /**
     * Validates a netmask
     *
     * @param netmask
     *       InetAddress A netmask to test
     *
     * @return boolean True if it validates, otherwise false
     */
    public static boolean isValidNetmask(InetAddress netmask) {

        byte[] segments = netmask.getAddress();

        return isValidNetmask(segments);
    }

    /**
     * Validates a netmask
     *
     * @param segments
     *       byte[] A signed byte array whose binary values represent the address
     *
     * @return boolean True if valid, otherwise false
     */
    public static boolean isValidNetmask(byte[] segments) {
        boolean mustBeZero = false;
        for (int i = 0; i < segments.length; i++) {

            // Valid segments: 0, 128, 192, 224, 240, 248, 252, 254, 255
            switch (segments[i]) {
            case -1:
                if (mustBeZero)
                    return false;
                break;
            case 0:
                if (mustBeZero) {
                    break;
                }
            case -2:
            case -4:
            case -8:
            case -16:
            case -32:
            case -64:
            case -128:
                if (!mustBeZero)
                    mustBeZero = true;
                else {
                    return false;
                }
                break;
            default:
                return false;
            }
        }

        return true;
    }
}

Related

  1. isUDPPortFree(int port, InetAddress addr)
  2. isUnicastAddress(@CheckForNull InetAddress address)
  3. isValidAddress(InetAddress address, int timeoutMs)
  4. isValidAddress(InetAddress i, boolean includeLocalAddressesInNoderefs)
  5. isValidAddress(InetAddress i, boolean includeLocalAddressesInNoderefs)
  6. isValidInetAddress(String address)
  7. isValidInetAddress(String IP)
  8. isValidInetAddress(String ip)
  9. isValidIntranetAddress(InetAddress address)