001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.net.examples.cidr;
018
019import java.util.Arrays;
020import java.util.Scanner;
021
022import org.apache.commons.net.util.SubnetUtils;
023import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
024
025/**
026 * Example class that shows how to use the {@link SubnetUtils} class.
027 *
028 */
029public class SubnetUtilsExample {
030
031    public static void main(final String[] args) {
032        final String subnet = "192.168.0.3/31";
033        final SubnetUtils utils = new SubnetUtils(subnet);
034        final SubnetInfo info = utils.getInfo();
035
036        System.out.printf("Subnet Information for %s:%n", subnet);
037        System.out.println("--------------------------------------");
038        System.out.printf("IP Address:\t\t\t%s\t[%s]%n", info.getAddress(), Integer.toBinaryString(info.asInteger(info.getAddress())));
039        System.out.printf("Netmask:\t\t\t%s\t[%s]%n", info.getNetmask(), Integer.toBinaryString(info.asInteger(info.getNetmask())));
040        System.out.printf("CIDR Representation:\t\t%s%n%n", info.getCidrSignature());
041
042        System.out.printf("Supplied IP Address:\t\t%s%n%n", info.getAddress());
043
044        System.out.printf("Network Address:\t\t%s\t[%s]%n", info.getNetworkAddress(), Integer.toBinaryString(info.asInteger(info.getNetworkAddress())));
045        System.out.printf("Broadcast Address:\t\t%s\t[%s]%n", info.getBroadcastAddress(), Integer.toBinaryString(info.asInteger(info.getBroadcastAddress())));
046        System.out.printf("Low Address:\t\t\t%s\t[%s]%n", info.getLowAddress(), Integer.toBinaryString(info.asInteger(info.getLowAddress())));
047        System.out.printf("High Address:\t\t\t%s\t[%s]%n", info.getHighAddress(), Integer.toBinaryString(info.asInteger(info.getHighAddress())));
048
049        System.out.printf("Total usable addresses: \t%d%n", Long.valueOf(info.getAddressCountLong()));
050        System.out.printf("Address List: %s%n%n", Arrays.toString(info.getAllAddresses()));
051
052        final String prompt = "Enter an IP address (e.g. 192.168.0.10):";
053        System.out.println(prompt);
054        try (final Scanner scanner = new Scanner(System.in)) {
055            while (scanner.hasNextLine()) {
056                final String address = scanner.nextLine();
057                System.out.println("The IP address [" + address + "] is " + (info.isInRange(address) ? "" : "not ") + "within the subnet [" + subnet + "]");
058                System.out.println(prompt);
059            }
060        }
061    }
062
063}