技术分享 其他 查看内容

java实现 IP/掩码位 转换 ip段范围

admin | 发布于 2022-09-08 13:17| 浏览()| 评论() | 收藏() | 点赞() | 打印

摘要: 今天接到一个需求,需要判断用户的请求ip是否在办公网网段,如果是,那么就要做出相应的提示。一开始我以为,办公网段就只需要截取前三位就行,比如:22.3.4.6 所在网段就是22.3.4就可以。

大学学的计算机网络已经忘了一大半,今天接到一个需求,需要判断用户的请求ip是否在办公网网段,如果是,那么就要做出相应的提示。

一开始我以为,办公网段就只需要截取前三位就行,比如:22.3.4.6 所在网段就是22.3.4就可以。

可是安全管理员给我发的却不是这样。而是以下这样的。

192.168.1.64/26
192.168.0.0/23
192.168.0.0/24
192.168.0.0/32

这些是什么意思呢?

前面的192.168.1.64表示ip后面的26表示掩码,就是表示192.168.1.64的前26位不能动,ip分为四部分,每一辈分占8位,一共32位,也就是说32位中有26位是不能动的,前面的192.168.1占8*3=24位不能动。后面的64是0100 0000 前面已经有24位不能动了,所以后面还有2位不能动就是01不能动。所以网段就是192.168.1.64-192.168.1.127

同理192.168.0.0/23就是前23位不能动,192.168不能动 8*2=16位  后面有7位不能动后面的0就是0000 0000其中前7位不能动,后一位随便是0或者1 所以网段是192.168.0-192.168.1

192.168.0.0/24前24位不能动,192.168.0不能动 8*3=24位  后面有8位随便动 所以网段是192.168.0

192.168.0.0/32是前32位都不能动所以网段是 192.168.0.0

下面提供一下网上摘抄的java如何判断是否在某一个网段

import java.math.BigDecimal;  
import java.util.ArrayList;  
import java.util.List;  
import java.util.regex.Pattern;  
  
  
public class IpUtil {  
      
    /** 
     * 在main方法里面可以测试下 
     * 主要功能都在main方法里,需要什么自己找 
     */  
    public static void main(String[] args) {  
        String ip="172.31.0.4";//ip  
        String mask="31";//位数,如果只知道子网掩码不知道位数的话在参考getMaskMap()方法  
          
        //获得起始IP和终止IP的方法(包含网络地址和广播地址)  
        String startIp=getBeginIpStr(ip, mask);  
        String endIp=getEndIpStr(ip, mask);  
        System.out.println("起始IP:" + startIp + "终止IP:" + endIp);  
          
        //获得起始IP和终止IP的方法(不包含网络地址和广播地址)  
        String subStart=startIp.split("\\.")[0]+"."+startIp.split("\\.")[1]+"."+startIp.split("\\.")[2]+".";  
        String subEnd=endIp.split("\\.")[0]+"."+endIp.split("\\.")[1]+"."+endIp.split("\\.")[2]+".";  
        startIp=subStart+(Integer.parseInt(startIp.split("\\.")[3])+1);  
        endIp=subEnd+(Integer.parseInt(endIp.split("\\.")[3])-1);  
        System.out.println("起始IP:" + startIp + "终止IP:" + endIp);  
          
        //判断一个IP是否属于某个网段  
        boolean flag = isInRange("10.2.0.0", "10.3.0.0/17");  
        System.out.println(flag);  
          
        //根据位数查询IP数量  
        int ipCount = getIpCount("8");  
        System.out.println(ipCount);  
          
        //判断是否是一个IP  
        System.out.println(isIP("192.168.1.0"));  
          
        //把ip转换为数字(mysql中inet_aton()的实现)  
        System.out.println(ipToDouble("192.168.1.1"));  
          
       //打印IP段所有IP(IP过多会内存溢出)  
//      List<String> list = parseIpMaskRange(ip, mask);  
//      for (String s : list){  
//          System.out.println(s);  
//      }  
    }  
      
    /** 
     * 功能:判断一个IP是不是在一个网段下的 
     * 格式:isInRange("192.168.8.3", "192.168.9.10/22"); 
     */  
    public static boolean isInRange(String ip, String cidr) {    
        String[] ips = ip.split("\\.");    
        int ipAddr = (Integer.parseInt(ips[0]) << 24)    
                | (Integer.parseInt(ips[1]) << 16)    
                | (Integer.parseInt(ips[2]) << 8) | Integer.parseInt(ips[3]);    
        int type = Integer.parseInt(cidr.replaceAll(".*/", ""));    
        int mask = 0xFFFFFFFF << (32 - type);    
        String cidrIp = cidr.replaceAll("/.*", "");    
        String[] cidrIps = cidrIp.split("\\.");    
        int cidrIpAddr = (Integer.parseInt(cidrIps[0]) << 24)    
                | (Integer.parseInt(cidrIps[1]) << 16)    
                | (Integer.parseInt(cidrIps[2]) << 8)    
                | Integer.parseInt(cidrIps[3]);    
    
        return (ipAddr & mask) == (cidrIpAddr & mask);    
    }  
      
    /** 
     * 功能:根据IP和位数返回该IP网段的所有IP 
     * 格式:parseIpMaskRange("192.192.192.1.", "23") 
     */  
    public static List<String> parseIpMaskRange(String ip,String mask){  
        List<String> list=new ArrayList<>();  
        if ("32".equals(mask)) {  
            list.add(ip);  
        }else{  
            String startIp=getBeginIpStr(ip, mask);  
            String endIp=getEndIpStr(ip, mask);  
            if (!"31".equals(mask)) {  
                String subStart=startIp.split("\\.")[0]+"."+startIp.split("\\.")[1]+"."+startIp.split("\\.")[2]+".";  
                String subEnd=endIp.split("\\.")[0]+"."+endIp.split("\\.")[1]+"."+endIp.split("\\.")[2]+".";  
                startIp=subStart+(Integer.parseInt(startIp.split("\\.")[3])+1);  
                endIp=subEnd+(Integer.parseInt(endIp.split("\\.")[3])-1);  
            }  
            list=parseIpRange(startIp, endIp);  
        }  
        return list;  
    }  
      
    /** 
     * 功能:根据位数返回IP总数 
     * 格式:parseIpMaskRange("192.192.192.1", "23") 
     */  
    public static int getIpCount(String mask) {  
        return BigDecimal.valueOf(Math.pow(2, 32 - Integer.parseInt(mask))).setScale(0, BigDecimal.ROUND_DOWN).intValue();//IP总数,去小数点  
    }  
      
    /** 
     * 功能:根据位数返回IP总数 
     * 格式:isIP("192.192.192.1") 
     */  
    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();    
    }    
      
    public static List<String> parseIpRange(String ipfrom, String ipto) {  
        List<String> ips = new ArrayList<String>();  
        String[] ipfromd = ipfrom.split("\\.");  
        String[] iptod = ipto.split("\\.");  
        int[] int_ipf = new int[4];  
        int[] int_ipt = new int[4];  
        for (int i = 0; i < 4; i++) {  
            int_ipf[i] = Integer.parseInt(ipfromd[i]);  
            int_ipt[i] = Integer.parseInt(iptod[i]);  
        }  
        for (int A = int_ipf[0]; A <= int_ipt[0]; A++) {  
            for (int B = (A == int_ipf[0] ? int_ipf[1] : 0); B <= (A == int_ipt[0] ? int_ipt[1]  
                    : 255); B++) {  
                for (int C = (B == int_ipf[1] ? int_ipf[2] : 0); C <= (B == int_ipt[1] ? int_ipt[2]  
                        : 255); C++) {  
                    for (int D = (C == int_ipf[2] ? int_ipf[3] : 0); D <= (C == int_ipt[2] ? int_ipt[3]  
                            : 255); D++) {  
                        ips.add(A + "." + B + "." + C + "." + D);  
                    }  
                }  
            }  
        }  
        return ips;  
    }  
      
    /** 
     * 把long类型的Ip转为一般Ip类型:xx.xx.xx.xx 
     * 
     * @param ip 
     * @return 
     */  
    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;  
    }  
    /** 
     * 把xx.xx.xx.xx类型的转为long类型的 
     * 
     * @param ip 
     * @return 
     */  
    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;  
    }  
    /** 
     * 根据掩码位获取掩码 
     * 
     * @param maskBit 
     *            掩码位数,如"28"、"30" 
     * @return 
     */  
    public static String getMaskByMaskBit(String maskBit)  
    {  
        return "".equals(maskBit) ? "error, maskBit is null !" : getMaskMap(maskBit);  
    }  
      
    /** 
     * 根据 ip/掩码位 计算IP段的起始IP 如 IP串 218.240.38.69/30 
     * 
     * @param ip 
     *            给定的IP,如218.240.38.69 
     * @param maskBit 
     *            给定的掩码位,如30 
     * @return 起始IP的字符串表示 
     */  
    public static String getBeginIpStr(String ip, String maskBit)  
    {  
        return getIpFromLong(getBeginIpLong(ip, maskBit));  
    }  
    /** 
     * 根据 ip/掩码位 计算IP段的起始IP 如 IP串 218.240.38.69/30 
     * 
     * @param ip 
     *            给定的IP,如218.240.38.69 
     * @param maskBit 
     *            给定的掩码位,如30 
     * @return 起始IP的长整型表示 
     */  
    public static Long getBeginIpLong(String ip, String maskBit)  
    {  
        return getIpFromString(ip) & getIpFromString(getMaskByMaskBit(maskBit));  
    }  
    /** 
     * 根据 ip/掩码位 计算IP段的终止IP 如 IP串 218.240.38.69/30 
     * 
     * @param ip 
     *            给定的IP,如218.240.38.69 
     * @param maskBit 
     *            给定的掩码位,如30 
     * @return 终止IP的字符串表示 
     */  
    public static String getEndIpStr(String ip, String maskBit)  
    {  
        return getIpFromLong(getEndIpLong(ip, maskBit));  
    }  
      
     /** 
     * 根据 ip/掩码位 计算IP段的终止IP 如 IP串 218.240.38.69/30 
     * 
     * @param ip 
     *            给定的IP,如218.240.38.69 
     * @param maskBit 
     *            给定的掩码位,如30 
     * @return 终止IP的长整型表示 
     */  
    public static Long getEndIpLong(String ip, String maskBit)  
    {  
        return getBeginIpLong(ip, maskBit)  
                + ~getIpFromString(getMaskByMaskBit(maskBit));  
    }  
      
      
      /** 
     * 根据子网掩码转换为掩码位 如 255.255.255.252转换为掩码位 为 30 
     * 
     * @param netmarks 
     * @return 
     */  
    public static int getNetMask(String netmarks)  
    {  
        StringBuilder sbf;  
        String str;  
        int inetmask = 0;  
        int count = 0;  
        String[] ipList = netmarks.split("\\.");  
        for (int n = 0; n < ipList.length; n++)  
        {  
            sbf = toBin(Integer.parseInt(ipList[n]));  
            str = sbf.reverse().toString();  
            count = 0;  
            for (int i = 0; i < str.length(); i++)  
            {  
                i = str.indexOf('1', i);  
                if (i == -1)  
                {  
                    break;  
                }  
                count++;  
            }  
            inetmask += count;  
        }  
        return inetmask;  
    }  
      
    /** 
     * 计算子网大小 
     * 
     * @param netmask 
     *            掩码位 
     * @return 
     */  
    public static int getPoolMax(int maskBit)  
    {  
        if (maskBit <= 0 || maskBit >= 32)  
        {  
            return 0;  
        }  
        return (int) Math.pow(2, 32 - maskBit) - 2;  
    }  
    private static StringBuilder toBin(int x)  
    {  
        StringBuilder result = new StringBuilder();  
        result.append(x % 2);  
        x /= 2;  
        while (x > 0)  
        {  
            result.append(x % 2);  
            x /= 2;  
        }  
        return result;  
    }  
      
    public static String getMaskMap(String maskBit) {  
        if ("1".equals(maskBit)) return "128.0.0.0";  
        if ("2".equals(maskBit)) return "192.0.0.0";  
        if ("3".equals(maskBit)) return "224.0.0.0";  
        if ("4".equals(maskBit)) return "240.0.0.0";  
        if ("5".equals(maskBit)) return "248.0.0.0";  
        if ("6".equals(maskBit)) return "252.0.0.0";  
        if ("7".equals(maskBit)) return "254.0.0.0";  
        if ("8".equals(maskBit)) return "255.0.0.0";  
        if ("9".equals(maskBit)) return "255.128.0.0";  
        if ("10".equals(maskBit)) return "255.192.0.0";  
        if ("11".equals(maskBit)) return "255.224.0.0";  
        if ("12".equals(maskBit)) return "255.240.0.0";  
        if ("13".equals(maskBit)) return "255.248.0.0";  
        if ("14".equals(maskBit)) return "255.252.0.0";  
        if ("15".equals(maskBit)) return "255.254.0.0";  
        if ("16".equals(maskBit)) return "255.255.0.0";  
        if ("17".equals(maskBit)) return "255.255.128.0";  
        if ("18".equals(maskBit)) return "255.255.192.0";  
        if ("19".equals(maskBit)) return "255.255.224.0";  
        if ("20".equals(maskBit)) return "255.255.240.0";  
        if ("21".equals(maskBit)) return "255.255.248.0";  
        if ("22".equals(maskBit)) return "255.255.252.0";  
        if ("23".equals(maskBit)) return "255.255.254.0";  
        if ("24".equals(maskBit)) return "255.255.255.0";  
        if ("25".equals(maskBit)) return "255.255.255.128";  
        if ("26".equals(maskBit)) return "255.255.255.192";  
        if ("27".equals(maskBit)) return "255.255.255.224";  
        if ("28".equals(maskBit)) return "255.255.255.240";  
        if ("29".equals(maskBit)) return "255.255.255.248";  
        if ("30".equals(maskBit)) return "255.255.255.252";  
        if ("31".equals(maskBit)) return "255.255.255.254";  
        if ("32".equals(maskBit)) return "255.255.255.255";  
        return "-1";  
    }  
      
    public static double ipToDouble(String ip) {  
        String[] arr = ip.split("\\.");  
        double d1 = Double.parseDouble(arr[0]);  
        double d2 = Double.parseDouble(arr[1]);  
        double d3 = Double.parseDouble(arr[2]);  
        double d4 = Double.parseDouble(arr[3]);  
        return d1 * Math.pow(256, 3) + d2 * Math.pow(256, 2) + d3 * 256 + d4;  
    }  
}


发表评论(对文章涉及的知识点还有疑问,可以在这里留言,老高看到后会及时回复的。)

表情