当前位置: 首页 > news >正文

IP地址工具,判断IP是否在指定范围内(支持ipv6)

常用方法,判断一个ip是否在指定的ip范围内,范围可能包括起始ip范围或者掩码形式,无其它依赖,

package com.yk.ip;import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;public class IpUtil {/*** 判断目标ip是否在ip范围内(起始ip范围),支持ipv6* @param ipAddress 目标ip* @param startIp 开始ip* @param endIp 结束ip* @return boolean* @throws UnknownHostException 异常*/public static boolean isInRange(String ipAddress,String startIp,String endIp) throws UnknownHostException {InetAddress address = InetAddress.getByName(ipAddress);InetAddress startAddress = InetAddress.getByName(startIp);InetAddress endAddress = InetAddress.getByName(endIp);BigInteger start = new BigInteger(1, startAddress.getAddress());BigInteger end = new BigInteger(1, endAddress.getAddress());BigInteger target = new BigInteger(1, address.getAddress());int st = start.compareTo(target);int te = target.compareTo(end);return (st <= 0) && (te <= 0);}/*** 判断目标ip是否在ip范围内(掩码),支持ipv6* @param ipAddress 目标ip* @param ipWithMask 带掩码ip* @return boolean* @throws UnknownHostException 异常*/public static boolean isInRange(String ipAddress,String ipWithMask) throws UnknownHostException {if (ipWithMask.contains("/")) {String addressPart = ipWithMask.substring(0, ipWithMask.indexOf("/"));String networkPart = ipWithMask.substring(ipWithMask.indexOf("/") + 1);ByteBuffer maskBuffer;int targetSize;InetAddress inetAddress = InetAddress.getByName(addressPart);if (inetAddress.getAddress().length == 4) {maskBuffer = ByteBuffer.allocate(4).putInt(-1);targetSize = 4;} else {maskBuffer = ByteBuffer.allocate(16).putLong(-1L).putLong(-1L);targetSize = 16;}BigInteger mask = (new BigInteger(1, maskBuffer.array())).not().shiftRight(Integer.parseInt(networkPart));ByteBuffer buffer = ByteBuffer.wrap(inetAddress.getAddress());BigInteger ipVal = new BigInteger(1, buffer.array());BigInteger startIp = ipVal.and(mask);BigInteger endIp = startIp.add(mask.not());byte[] startIpArr = toBytes(startIp.toByteArray(), targetSize);byte[] endIpArr = toBytes(endIp.toByteArray(), targetSize);InetAddress startAddress = InetAddress.getByAddress(startIpArr);InetAddress endAddress = InetAddress.getByAddress(endIpArr);return isInRange(ipAddress, startAddress.getHostAddress(), endAddress.getHostAddress());} else {throw new IllegalArgumentException("not an valid CIDR format!");}}private static byte[] toBytes(byte[] array, int targetSize) {int counter = 0;List<Byte> newArr = new ArrayList<Byte>();while (counter < targetSize && (array.length - 1 - counter >= 0)) {newArr.add(0, array[array.length - 1 - counter]);counter++;}int size = newArr.size();for (int i = 0; i < (targetSize - size); i++) {newArr.add(0, (byte) 0);}byte[] ret = new byte[newArr.size()];for (int i = 0; i < newArr.size(); i++) {ret[i] = newArr.get(i);}return ret;}
}
http://www.lryc.cn/news/311648.html

相关文章:

  • Redis 之六:Redis 的哨兵模式(Sentinel)
  • 总线要点笔记
  • RK3566 linux iperf网络测试
  • 【无标题】计算机主要应用于哪些领域
  • 力扣精选算法100道——颜色分类(双指针和三指针俩种方法解决此题)
  • 基于Java SSM springboot+VUE+redis实现的前后端分类版网上商城项目
  • 01——什么是人工智能
  • AI国漫女神这样画!傻瓜式的云端SD(stable diffusion)部署教程 - 白嫖4090、无需代码、一键启动
  • 消息队列、共享内存、信号灯
  • K次取反后最大化的数组和 加油站 分发糖果 柠檬水找零
  • Standoff: 独特的基于真实商业基础架构的网络战
  • 如何成为fpga工程师
  • 基础算法(二)#蓝桥杯
  • 运筹学_1.1.4 线性规划问题-解的概念
  • 物联网主机:为智能交通赋能
  • 「Vue3系列」Vue3简介及安装
  • Javascript:分支语句
  • 从零开始学习PX4源码2(PX4姿态误差计算)
  • git安装与使用4.3
  • Python:关于数据服务中的Web API的设计
  • VMwareWorkstation17.0虚拟机安装搭建PcDos2000虚拟机(完整图文详细步骤教程)
  • 第七个程序:两个字符串连接后计算长度
  • 【大数据】-- dataworks 创建odps 的 hudi 外表
  • ChatGPT与GEE+ENVI+python高光谱,多光谱等成像遥感数据处理技术
  • 学习linux从0到初级工程师-3
  • java实现文件上传到本地
  • 基于springboot+vue的多媒体素材库的开发与应用系统
  • 《GitHub新手入门指南:从零开始掌握基本用法》
  • K8S存储卷与PV,PVC
  • (科目三)简答题汇总