IPv4地址段、地址掩碼、可用地址等常用方法

靠譜的提莫發表於2020-08-05
package com.xxx.iptools;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IPv4Util {
     public static void main(String[] args){
         String ipAddr="15.10.44.60";
         System.out.println("判斷字串是否為IPv4地址:"+isIPv4("24"));
         System.out.println("IPv4地址轉換為數字:"+ipToInt(ipAddr));
         System.out.println("數字轉換為IP地址:"+intToIP(252324924));
         System.out.println("數字串轉換為IP地址:"+intToIP("252324928"));
         System.out.println("將掩碼位轉換為地址:"+maskToIP("8"));
         System.out.println("將掩碼轉換為掩碼位:"+maskToBit("255.0.0.0"));
         System.out.println("根據IP和掩碼計算首地址:"+IPmap("192.168.1.2/24"));
         System.out.println("根據IP和掩碼計算所有IP地址:"+IPAll("192.168.1.15/28"));
         System.out.println("根據IP和掩碼計算所有IP十進位制值:"+IPnums("192.168.1.15/32"));
         System.out.println("兩組IP判斷包含(bIsContA:B包含A;aIsContB:A包含B)、交集(retainAll)、並集(all)、去重並集(disAll)、差集(diffAll,A有B沒有):"+IPColl("192.168.1.1/30","192.168.1.1/31"));
     }

    /**
     * 根據IP地址和掩碼,獲取全量IP列表
     * @param ipAddra,ipAddrb
     * @return map
     */
    public static Map IPColl (String ipAddra,String ipAddrb){
        //根據地址資訊獲取全量十進位制IP
        List lista=IPnums(ipAddra);
        List listb=IPnums(ipAddrb);
        Map map=new HashMap();
        List list = new ArrayList();
        //包含關係判斷
        Boolean aIsContB=lista.containsAll(listb);
        map.put("aIsContB",aIsContB);
        Boolean bIsContA=listb.containsAll(lista);
        map.put("bIsContA",bIsContA);
        //並集,包含重複資料
        list = lista;
        list.addAll(listb);
        map.put("all",listNuToIP(list));
        list=null;
        //差集,lista對listb的差集,a有b沒有的意思
        list=lista;
        list.removeAll(listb);
        map.put("diffAll",listNuToIP(list));
        //去重並集:根據上一步的差集,合併第二集合,得到去重並集
        list.addAll(listb);
        map.put("disAll",listNuToIP(list));
        list=null;
        //交集:兩個list集合的交集
        list=lista;
        list.retainAll(listb);
        map.put("retainAll",listNuToIP(list));
        list=null;
        return map;
    }

    /**
     * 根據十進位制IP list列表,獲取點分制IP地址
     * @param listint
     * @return list
     */
    public static List listNuToIP (List listint){
        List list=new ArrayList();
        for (Object ip:listint){
            String ips = intToIP(Long.parseLong(ip.toString()));
            list.add(ips);
        }
        return list;
    }

    /**
     * 根據IP地址和掩碼,獲取全量IP列表
     * @param ipAddr
     * @return list
     */
    public static List IPnums (String ipAddr){
        List list=new ArrayList();
        if (!ipAddr.contains("/")){
            list.add(ipToInt(ipAddr));
            return list;
        }
        if (ipAddr.split("/")[1].equals("32") || ipAddr.split("/")[1].equals("255.255.255.255")){
            list.add(ipToInt(ipAddr.split("/")[0]));
            return list;
        }
        Map mapIP=IPmap(ipAddr);
        Long startIP=ipToInt(mapIP.get("netIP").toString());
        Long endIP=ipToInt(mapIP.get("broadIP").toString());
        for (Long i=startIP;i<=endIP;i++){
            list.add(i);
        }
        return list;
    }

    /**
     * 根據IP地址和掩碼,獲取全量IP列表
     * @param ipAddr
     * @return list
     */
    public static List IPAll (String ipAddr){
        List list=new ArrayList();
        if (!ipAddr.contains("/")){
            list.add(ipAddr);
            return list;
        }
        if (ipAddr.split("/")[1].equals("32") || ipAddr.split("/")[1].equals("255.255.255.255")){
            list.add(ipAddr.split("/")[0]);
            return list;
        }
        Map mapIP=IPmap(ipAddr);
        Long startIP=ipToInt(mapIP.get("netIP").toString());
        Long endIP=ipToInt(mapIP.get("broadIP").toString());
        for (Long i=startIP;i<=endIP;i++){
            list.add(intToIP(i));
        }
        return list;
    }

    /**
     * 根據IP地址和掩碼計算掩碼、網路、廣播、起始IP地址
     * @param ipAddr
     * @return map
     */
    public static Map<String,String> IPmap(String ipAddr){
        Map<String,String> map=new HashMap<>();
        if (!ipAddr.contains("/")){
            map.put("startIP",ipAddr);
            map.put("maskBit","32");
            map.put("maskIP","255.255.255.255");
            return map;
        }
        String ip=ipAddr.split("/")[0];
        String maskIP=ipAddr.split("/")[1];
        String maskBit="";
        if(isIPv4(maskIP)){
            maskBit=maskToBit(maskIP);
            if (maskBit.equals("32")){
                map.put("startIP",ip);
                map.put("maskBit","32");
                map.put("maskIP","255.255.255.255");
                return map;
            }
        }else{
            maskBit=maskIP;
            if (maskBit.equals("32")){
                map.put("startIP",ip);
                return map;
            }
            maskIP=maskToIP(maskBit);
        }
        String ip1st = "";
        int ipArray[] = new int[4];
        int netMaskArray[] = new int[4];
        for (int i = 0; i <4; i++) {
            ipArray[i] = Integer.parseInt(ip.split("\\.")[i]);
            netMaskArray[i] = Integer.parseInt(maskIP.split("\\.")[i]);
            ipArray[i] = ipArray[i]&netMaskArray[i];
        }
        for (int i = 0; i < 4; i++){
            if(i == 3){
                ipArray[i] = ipArray[i];
            }
            if ("" == ip1st){
                ip1st +=ipArray[i];
            } else{
                ip1st += "." + ipArray[i];
            }
        }
        map.put("maskBit",maskBit);
        map.put("maskIP",maskIP);
        map.put("netIP",ip1st);
        String broadIP=intToIP(ipToInt(ip1st)+(int)Math.pow(2,32-Integer.parseInt(maskBit))-1);
        String end=intToIP(ipToInt(ip1st)+(int)Math.pow(2,32-Integer.parseInt(maskBit))-2);
        map.put("broadIP",broadIP);
        map.put("endIP",broadIP);
        ip1st="";
        for (int i = 0; i < 4; i++){
            if(i == 3){
                ipArray[i] = ipArray[i]+1;
            }
            if ("" == ip1st){
                ip1st +=ipArray[i];
            } else{
                ip1st += "." + ipArray[i];
            }
        }
        map.put("startIP",ip1st);
        return map;
    }

    /**
     * 將掩碼位轉換為地址,入參為字串
     * @param maskIP
     * @return String
     */
    public static String maskToBit(String maskIP) {
        String bitMask="-1";
        switch (maskIP){
            case "128.0.0.0" : bitMask = "1" ; break;
            case "192.0.0.0" : bitMask = "2" ; break;
            case "224.0.0.0" : bitMask = "3" ; break;
            case "240.0.0.0" : bitMask = "4" ; break;
            case "248.0.0.0" : bitMask = "5" ; break;
            case "252.0.0.0" : bitMask = "6" ; break;
            case "254.0.0.0" : bitMask = "7" ; break;
            case "255.0.0.0" : bitMask = "8" ; break;
            case "255.128.0.0" : bitMask = "9" ; break;
            case "255.192.0.0" : bitMask ="10" ; break;
            case "255.224.0.0" : bitMask ="11" ; break;
            case "255.240.0.0" : bitMask ="12" ; break;
            case "255.248.0.0" : bitMask ="13" ; break;
            case "255.252.0.0" : bitMask ="14" ; break;
            case "255.254.0.0" : bitMask ="15" ; break;
            case "255.255.0.0" : bitMask ="16" ; break;
            case "255.255.128.0" : bitMask ="17" ; break;
            case "255.255.192.0" : bitMask ="18" ; break;
            case "255.255.224.0" : bitMask ="19" ; break;
            case "255.255.240.0" : bitMask ="20" ; break;
            case "255.255.248.0" : bitMask ="21" ; break;
            case "255.255.252.0" : bitMask ="22" ; break;
            case "255.255.254.0" : bitMask ="23" ; break;
            case "255.255.255.0" : bitMask ="24" ; break;
            case "255.255.255.128" : bitMask ="25" ; break;
            case "255.255.255.192" : bitMask ="26" ; break;
            case "255.255.255.224" : bitMask ="27" ; break;
            case "255.255.255.240" : bitMask ="28" ; break;
            case "255.255.255.248" : bitMask ="29" ; break;
            case "255.255.255.252" : bitMask ="30" ; break;
            case "255.255.255.254" : bitMask ="31" ; break;
            case "255.255.255.255" : bitMask ="32" ; break;
        }
        return bitMask;
    }

    /**
     * 將掩碼位轉換為地址,入參為字串
     * @param maskBit
     * @return String
     */
    public static String maskToIP(String maskBit) {
        String ipMask="-1";
        switch (maskBit){
            case "1" : ipMask = "128.0.0.0"; break;
            case "2" : ipMask = "192.0.0.0"; break;
            case "3" : ipMask = "224.0.0.0"; break;
            case "4" : ipMask = "240.0.0.0"; break;
            case "5" : ipMask = "248.0.0.0"; break;
            case "6" : ipMask = "252.0.0.0"; break;
            case "7" : ipMask = "254.0.0.0"; break;
            case "8" : ipMask = "255.0.0.0"; break;
            case "9" : ipMask = "255.128.0.0"; break;
            case "10" : ipMask = "255.192.0.0"; break;
            case "11" : ipMask = "255.224.0.0"; break;
            case "12" : ipMask = "255.240.0.0"; break;
            case "13" : ipMask = "255.248.0.0"; break;
            case "14" : ipMask = "255.252.0.0"; break;
            case "15" : ipMask = "255.254.0.0"; break;
            case "16" : ipMask = "255.255.0.0"; break;
            case "17" : ipMask = "255.255.128.0"; break;
            case "18" : ipMask = "255.255.192.0"; break;
            case "19" : ipMask = "255.255.224.0"; break;
            case "20" : ipMask = "255.255.240.0"; break;
            case "21" : ipMask = "255.255.248.0"; break;
            case "22" : ipMask = "255.255.252.0"; break;
            case "23" : ipMask = "255.255.254.0"; break;
            case "24" : ipMask = "255.255.255.0"; break;
            case "25" : ipMask = "255.255.255.128"; break;
            case "26" : ipMask = "255.255.255.192"; break;
            case "27" : ipMask = "255.255.255.224"; break;
            case "28" : ipMask = "255.255.255.240"; break;
            case "29" : ipMask = "255.255.255.248"; break;
            case "30" : ipMask = "255.255.255.252"; break;
            case "31" : ipMask = "255.255.255.254"; break;
            case "32" : ipMask = "255.255.255.255"; break;
        }
        return ipMask;
    }

    /**
     * 將掩碼位轉換為地址,入參為數字
     * @param maskBit
     * @return String
     */
    public static String maskToIP(int maskBit) {
        String ipMask="-1";
        switch (maskBit){
            case 1 : ipMask = "128.0.0.0"; break;
            case 2 : ipMask = "192.0.0.0"; break;
            case 3 : ipMask = "224.0.0.0"; break;
            case 4 : ipMask = "240.0.0.0"; break;
            case 5 : ipMask = "248.0.0.0"; break;
            case 6 : ipMask = "252.0.0.0"; break;
            case 7 : ipMask = "254.0.0.0"; break;
            case 8 : ipMask = "255.0.0.0"; break;
            case 9 : ipMask = "255.128.0.0"; break;
            case 10 : ipMask = "255.192.0.0"; break;
            case 11 : ipMask = "255.224.0.0"; break;
            case 12 : ipMask = "255.240.0.0"; break;
            case 13 : ipMask = "255.248.0.0"; break;
            case 14 : ipMask = "255.252.0.0"; break;
            case 15 : ipMask = "255.254.0.0"; break;
            case 16 : ipMask = "255.255.0.0"; break;
            case 17 : ipMask = "255.255.128.0"; break;
            case 18 : ipMask = "255.255.192.0"; break;
            case 19 : ipMask = "255.255.224.0"; break;
            case 20 : ipMask = "255.255.240.0"; break;
            case 21 : ipMask = "255.255.248.0"; break;
            case 22 : ipMask = "255.255.252.0"; break;
            case 23 : ipMask = "255.255.254.0"; break;
            case 24 : ipMask = "255.255.255.0"; break;
            case 25 : ipMask = "255.255.255.128"; break;
            case 26 : ipMask = "255.255.255.192"; break;
            case 27 : ipMask = "255.255.255.224"; break;
            case 28 : ipMask = "255.255.255.240"; break;
            case 29 : ipMask = "255.255.255.248"; break;
            case 30 : ipMask = "255.255.255.252"; break;
            case 31 : ipMask = "255.255.255.254"; break;
            case 32 : ipMask = "255.255.255.255"; break;
        }
        return ipMask;
    }

    /**
     * 將數字轉換為IPv4地址
     * @param intIPv4
     * @return String
     */
    public static String intToIP(long intIPv4) {
        return ((intIPv4 >> 24) & 0xFF) +
                "." + ((intIPv4 >> 16) & 0xFF) +
                "." + ((intIPv4 >> 8) & 0xFF) +
                "." + (intIPv4 & 0xFF);
    }

    /**
     * 將數字轉換為IPv4地址
     * @param intIPv4
     * @return String
     */
    public static String intToIP(String  intIPv4) {
        Long ip=Long.parseLong(intIPv4);
        return ((ip >> 24) & 0xFF) +
                "." + ((ip >> 16) & 0xFF) +
                "." + ((ip >> 8) & 0xFF) +
                "." + (ip & 0xFF);
    }

    /**
     * 將IPv4地址轉換為數字
     * @param ipAddr
     * @return long
     */
    public static Long ipToInt(String ipAddr){
        long ipInt = 0;
        String[] ipAddrArray = ipAddr.split("\\.");
        for (int i = 3;i>=0;i--){
            long ip=Long.parseLong(ipAddrArray[3-i]);
            ipInt |=ip<<(i*8);
        }
        return ipInt;
    }

    /**
     * 判斷IPv4地址是否合法
     * @param ipAddr
     * @return boolean
     */
    public static boolean isIPv4(String ipAddr){
        boolean isIP=ipAddr.matches("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}");
        return isIP;

    }

    /**
     * 判斷IPv4掩碼是否合法
     * @param mask
     * @return boolean
     */
    public static boolean isMask(String mask){
        String isMask=maskToBit(mask);
        if ("-1".equals(isMask)){
            return false;
        }
        return true;
    }
}

 

相關文章