Java Network Interface Check isCIDR(String network)

Here you can find the source of isCIDR(String network)

Description

Verify if CIDR string is a valid CIDR address.

License

Open Source License

Parameter

Parameter Description
network CIDR to verify

Exception

Parameter Description
IllegalArgumentException an exception

Declaration

public static void isCIDR(String network) throws IllegalArgumentException 

Method Source Code


//package com.java2s;
/*/* w  ww.j  a  va  2  s.co m*/
 * Copyright (c) 2015-2016 VMware, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, without warranties or
 * conditions of any kind, EITHER EXPRESS OR IMPLIED.  See the License for the
 * specific language governing permissions and limitations under the License.
 */

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {
    /**
     * Verify if CIDR string is a valid CIDR address.
     *
     * @param network CIDR to verify
     * @throws IllegalArgumentException
     */
    public static void isCIDR(String network) throws IllegalArgumentException {
        String[] hostMask = network.split("/");
        if (hostMask.length != 2) {
            throw new IllegalArgumentException("subnetAddress is not a CIDR");
        }

        isValidInetAddress(hostMask[0]);

        // Mask must be < 32
        if (Integer.parseUnsignedInt(hostMask[1]) > 32) {
            throw new IllegalArgumentException("CIDR mask may not be larger than 32");
        }
    }

    /**
     * Verify if IP string is an IPv4 address.
     *
     * @param IP IP to verify
     * @throws IllegalArgumentException
     */
    public static void isValidInetAddress(String IP) throws IllegalArgumentException {

        // Opened issue #84 to track proper validation
        if (IP == null || IP.isEmpty()) {
            throw new IllegalArgumentException("IP is missing or empty");
        }

        if (IP.contains(":")) {
            // implement IPv6 validation
        } else {
            String[] segments = IP.split("\\.");
            if (segments.length != 4) {
                throw new IllegalArgumentException("IP does not appear valid:" + IP);
            }
            // it appears to be literal IP, its safe to use the getByName method
            try {
                InetAddress.getByName(IP);
            } catch (UnknownHostException e) {
                throw new IllegalArgumentException(e);
            }
        }
    }
}

Related

  1. checkMethod(NetworkInterface iface, Method toCheck)
  2. cloneInterfaces( List interfaces)
  3. describeInterface(NetworkInterface subIf, boolean subinterface)
  4. dumpLocalNetworkInfo()
  5. hasNetworkAccess()
  6. isHostInNetworkCard(String host)
  7. isInterfaceLoopback(NetworkInterface iface)
  8. isInterfaceUp(NetworkInterface iface)
  9. isLoopbackNetworkInterface( NetworkInterface anInterface)