Java tutorial
/* * Copyright 2016 Pivotal Software, Inc.. * * 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. */ package vn.vnpttech.ssdc.nms.webapp.util; import org.apache.commons.net.util.SubnetUtils; /** * * @author KhanhMQ */ public class SubnetUtil { /** * check ip range of two cidr is overlap * * @param cidr1 * @param cidr2 * @return true if overlap, false if not */ public static boolean checkCidrOverlap(String cidr1, String cidr2) { SubnetUtils su = new SubnetUtils(cidr1); SubnetUtils su2 = new SubnetUtils(cidr2); int start1 = su.getInfo().asInteger(su.getInfo().getLowAddress()); int end1 = su.getInfo().asInteger(su.getInfo().getHighAddress()); int start2 = su2.getInfo().asInteger(su2.getInfo().getLowAddress()); int end2 = su2.getInfo().asInteger(su2.getInfo().getHighAddress()); boolean ret = false; if (!((end2 <= start1) && (end1 <= start2))) { ret = true; } else { ret = false; } return ret; } /** * Check ip range of cidr2 in ip range cidr1 * * @param cidr1 * @param cidr2 * @return true if cidr2 in cidr1, false if not */ public static boolean checkCidrInCdir(String cidr1, String cidr2) { SubnetUtils su = new SubnetUtils(cidr1); SubnetUtils su2 = new SubnetUtils(cidr2); System.out.println(su.getInfo().getCidrSignature()); System.out.println(su.getInfo().getLowAddress()); System.out.println(su.getInfo().getHighAddress()); int end1 = su.getInfo().asInteger(su.getInfo().getHighAddress()); int start1 = su.getInfo().asInteger(su.getInfo().getLowAddress()); int end2 = su2.getInfo().asInteger(su2.getInfo().getHighAddress()); int start2 = su2.getInfo().asInteger(su2.getInfo().getLowAddress()); boolean ret = false; if ((start1 <= start2) && (end2 <= end1)) { ret = true; } else { ret = false; } return ret; } }