import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
public class ipDivide1 {
public static void main(String[] args) {
System.err.println(subnetDivide("121.79.128.0/18",10));
}
public static boolean isPowerOfTwo(int n) {
String str = Integer.toBinaryString(n);
if(n < 1) {
return false;
} else if(str.lastIndexOf("1") == 0) {
return true;
} else {
return false;
}
}
public static int roundup_power_of_2(int v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
private static int calculate2Num(int n) {
if (n == 0) {
return 1;
}
return 2 * calculate2Num(n - 1);
}
public static List<String> divideByPow(String ips,int pow){
List<String> ipList = new ArrayList<String>();
try {
if(ips ==null || "".equals(ips.trim()) || "null".equals(ips.trim())){
return ipList;
}
ips = ips.trim();
if(ips.indexOf("/")>0) {
String temp[] = ips.split("/");
if(temp.length == 2 && isIP(temp[0])){
String ip=temp[0];
String subnet=temp[1];
String ipBitStr = makeBinaryString( getIpFromString(ip).intValue());
int leg = ipBitStr.length();
int bitNum = Integer.parseInt(subnet);
String str0 = "00000000000000000000000000000000";
String str1 = "11111111111111111111111111111111";
if(bitNum <= 32){
String startIPBit = ipBitStr.substring(0, leg-(32-bitNum))+str0.substring(0, 32-bitNum);
String endIPBit = ipBitStr.substring(0, leg-(32-bitNum))+str1.substring(0, 32-bitNum);
Long startIPLong = getIpFromString(getIpStringFrom10bit(startIPBit));
Long endIPLong = getIpFromString(getIpStringFrom10bit(endIPBit));
Integer midNum=Integer.valueOf(1+str0.substring(0, 32-(bitNum+pow)), 2);
int subnetT=bitNum+pow;
for (int i = 0; i < calculate2Num(pow); i++) {
ipList.add(getIpFromLong(startIPLong)+"/"+subnetT);
startIPLong+=midNum;
}
}
}
}
return ipList;
} catch (Exception e) {
return ipList;
}
}
public static int judge(int subNum) {
int pow=0;
if(isPowerOfTwo(subNum)) {
pow=(int) (Math.log(subNum)/Math.log(2));
}else {
pow=roundup_power_of_2(subNum)/2;
pow=(int) (Math.log(pow)/Math.log(2));
}
return pow;
}
public static List<String> subnetDivide(String ips,int subNum) {
List<String> ipList = new ArrayList<String>();
if(subNum>1){
int pow =judge(subNum);
int difference=subNum-calculate2Num(pow);
ipList =divideByPow(ips,pow);
if(difference!=0) {
while (difference>0) {
if(difference==1) {
difference--;
ipList.addAll(divideByPow(ipList.get(0),1));
ipList.remove(ipList.get(0));
}else {
pow =judge(difference);
difference=difference-calculate2Num(pow);
for (int i = 0; i < calculate2Num(pow); i++) {
ipList.addAll(divideByPow(ipList.get(0),pow));
ipList.remove(ipList.get(0));
}
}
}
}
}else {
ipList.add(ips);
}
return ipList;
}
public static String getIpFromLong(Long ip)
{
String s1 = String.valueOf((ip & 4278190080L) / 16777216L);
String s2 = String.valueOf((ip & 16711680L) / 65536L);
String s3 = String.valueOf((ip & 65280L) / 256L);
String s4 = String.valueOf(ip & 255L);
return s1 + "." + s2 + "." + s3 + "." + s4;
}
public static Long getIpFromString(String ip)
{
Long ipLong = 0L;
String ipTemp = ip;
ipLong = ipLong * 256
+ Long.parseLong(ipTemp.substring(0, ipTemp.indexOf('.')));
ipTemp = ipTemp.substring(ipTemp.indexOf('.') + 1, ipTemp.length());
ipLong = ipLong * 256
+ Long.parseLong(ipTemp.substring(0, ipTemp.indexOf('.')));
ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length());
ipLong = ipLong * 256
+ Long.parseLong(ipTemp.substring(0, ipTemp.indexOf('.')));
ipTemp = ipTemp.substring(ipTemp.indexOf('.') + 1, ipTemp.length());
ipLong = ipLong * 256 + Long.parseLong(ipTemp);
return ipLong;
}
public static String getMaskByMaskBit(String maskBit)
{
return "".equals(maskBit) ? "error, maskBit is null !" : getMaskMap(maskBit);
}
public static String getBeginIpStr(String ip, String maskBit)
{
return getIpFromLong(getBeginIpLong(ip, maskBit));
}
public static Long getBeginIpLong(String ip, String maskBit)
{
return getIpFromString(ip) & getIpFromString(getMaskByMaskBit(maskBit));
}
public static String getEndIpStr(String ip, String maskBit)
{
return getIpFromLong(getEndIpLong(ip, maskBit));
}
public static Long getEndIpLong(String ip, String maskBit)
{
return getBeginIpLong(ip, maskBit)
+ ~getIpFromString(getMaskByMaskBit(maskBit));
}
public static String getMaskMap(String maskBit) {
switch (maskBit){
case "1" : return "128.0.0.0";
case "2" : return "192.0.0.0";
case "3" : return "224.0.0.0";
case "4" : return "240.0.0.0";
case "5" : return "248.0.0.0";
case "6" : return "252.0.0.0";
case "7" : return "254.0.0.0";
case "8" : return "255.0.0.0";
case "9" : return "255.128.0.0";
case "10" : return "255.192.0.0";
case "11" : return "255.224.0.0";
case "12" : return "255.240.0.0";
case "13" : return "255.248.0.0";
case "14" : return "255.252.0.0";
case "15" : return "255.254.0.0";
case "16" : return "255.255.0.0";
case "17" : return "255.255.128.0";
case "18" : return "255.255.192.0";
case "19" : return "255.255.224.0";
case "20" : return "255.255.240.0";
case "21" : return "255.255.248.0";
case "22" : return "255.255.252.0";
case "23" : return "255.255.254.0";
case "24" : return "255.255.255.0";
case "25" : return "255.255.255.128";
case "26" : return "255.255.255.192";
case "27" : return "255.255.255.224";
case "28" : return "255.255.255.240";
case "29" : return "255.255.255.248";
case "30" : return "255.255.255.252";
case "31" : return "255.255.255.254";
case "32" : return "255.255.255.255";
default: return "-1";
}
}
public static String makeBinaryString(int i){
String binary = "";
do{
int Bit = (i & 1);
String newDigit = ((Bit == 0) ? "0" : "1");
binary = newDigit + binary;
i >>>= 1;
}while (i != 0);
return binary;
}
public static String getIpStringFrom10bit(String ss)
{
while(ss.length() <32 ){
ss = "0"+ss ;
}
String[] ssArray =new String [8];
ssArray[0]=ss.substring(0, 8);
ssArray[1]=ss.substring(8, 16);
ssArray[2]=ss.substring(16, 24);
ssArray[3]=ss.substring(24, 32);
String sttttt=
Integer.valueOf(ssArray[0],2).toString()
+'.'+Integer.valueOf(ssArray[1],2).toString()
+'.'+Integer.valueOf(ssArray[2],2).toString()
+'.'+Integer.valueOf(ssArray[3],2).toString();
return sttttt;
}
public static boolean isIP(String str) {
String regex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
Pattern pattern = Pattern.compile(regex);
return pattern.matcher(str).matches();
}
}