package com.bocloud.ims.service.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.utils.ListTool;
import com.bocloud.ims.entity.resource.IpPool;
import org.apache.commons.lang.StringUtils;
import org.openstack4j.model.network.Pool;
import org.openstack4j.openstack.networking.domain.NeutronPool;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;


public class IpRangeUtil {
	public static final String IPV4_REGEX = "((\\d|[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})";
	public static final String DEFAULT_NETMASK = "255.255.255.255";

	/**
	 * IP段、子网掩码判断有效性
	 *

	 * @return
	 */
	public static boolean checkIpMask(String startIp, String endIp, String maskOrCidr) {
		if (!ipV4Validate(startIp)) {
			return false;
		}
		if (!ipV4Validate(endIp)) {
			return false;
		}
		long startIpValue = ipOrMaskToLong(startIp);
		long endIpValue = ipOrMaskToLong(endIp);
		if (startIpValue > endIpValue) {
			return false;
		}
		long maskValue;
		long ipStartValue, ipEndValue;
		int index = maskOrCidr.indexOf("/");
		if (index == -1) {
			maskValue = ipOrMaskToLong(maskOrCidr);
		} else {
			String maskStr = maskOrCidr.substring(index + 1);
			if (StringUtils.isEmpty(maskStr)) {
				return false;
			}
			// 通过掩码截取，后面可变的部分全部变为0 即为最小值
			String min = ipInteger2Binary(maskOrCidr.substring(0, index)).substring(0, Integer.parseInt(maskStr))
					+ ipInteger2Binary(maskOrCidr.substring(0, index)).substring(Integer.parseInt(maskStr)).replace("1",
							"0");
			ipStartValue = ipOrMaskToLong(ipBinary2IntegerIp(min));
			// 通过掩码截取，后面可变的部分全部变为1 即为最大值
			String max = ipInteger2Binary(maskOrCidr.substring(0, index)).substring(0, Integer.parseInt(maskStr))
					+ ipInteger2Binary(maskOrCidr.substring(0, index)).substring(Integer.parseInt(maskStr)).replace("0",
							"1");
			ipEndValue = ipOrMaskToLong(ipBinary2IntegerIp(max));
			if (startIpValue < ipStartValue || endIpValue > ipEndValue) {
				return false;
			}
			maskValue = getMaskLong(Integer.parseInt(maskStr));
		}
		return (maskValue & startIpValue) == (maskValue & endIpValue);
	}

	/**
	 * 十进制的ip格式 转为32位2进制
	 *
	 * @param ipString
	 * @return
	 */
	public static String ipInteger2Binary(String ipString) {
		long[] ip = ipSplit(ipString);
		return (binary2EigthLength(Long.toBinaryString(ip[0])))
				+ (binary2EigthLength(Long.toBinaryString(ip[1])))
				+ (binary2EigthLength(Long.toBinaryString(ip[2])))
				+ (binary2EigthLength(Long.toBinaryString(ip[3])));

	}

	/**
	 * 32的二进制字符串 转为十进制的ip格式
	 *
	 * @param ipString
	 * @return
	 */
	public static String ipBinary2IntegerIp(String ipString) {
		return Integer.parseInt(ipString.substring(0, 8), 2) + "." + Integer.parseInt(ipString.substring(8, 16), 2)
				+ "." + Integer.parseInt(ipString.substring(16, 24), 2) + "."
				+ Integer.parseInt(ipString.substring(24), 2);

	}

	/**
	 * 二进制补满8位
	 *
	 * @param value
	 * @return
	 */
	private static String binary2EigthLength(String value) {
		if (value == null) {
			return "00000000";
		}
		if (value.length() == 8) {
			return value;
		}
		int length = value.length();
		String zero = "";
		for (int i = 0; i < 8 - length; i++) {
			zero += "0";
		}
		return zero + value;

	}

	/**
	 * @param maskLength
	 * @return
	 */
	public static long getMaskLong(int maskLength) {
		int subLength = (32 - maskLength);
		long r = 0;
		for (int i = 0; i < subLength; i++) {
			r += (1 << i);
		}
		return ~r;
	}

	/**
	 * 获取网络号
	 *
	 * @param cidr
	 * @return
	 */
	public static String getNetId(String cidr) {
		int index = cidr.indexOf("/");
		if (cidr.contains(".") && index == -1) {
			return "";
		} else {
			// ip地址
			String ip = cidr.substring(0, index);
			// 子网掩码
			String maskip = maskToIpMask(cidr);
			long[] ipList = new long[4];
			long[] maskList = new long[4];
			long[] temp = new long[4];
			ipList = ipSplit(ip);
			maskList = ipSplit(maskip);
			// 将两个二进制数逻辑与（AND）运算后得出的结果即为网络部分
			for (int i = 0; i < 4; i++) {
				temp[i] = ipList[i] & maskList[i];
			}
			return temp[0] + "." + temp[1] + "." + temp[2] + "."
					+ temp[3];
		}
	}

	/**
	 * 获取网络号
	 *
	 * @param maskOrCidr
	 * @return
	 */
	public static String getNetId(String ip, String maskOrCidr) {
		int index = maskOrCidr.indexOf("/");
		if (maskOrCidr.contains(".") && index != -1) {
			return getNetId(maskOrCidr);
		} else {
			long[] ipList = new long[4];
			long[] maskList = new long[4];
			long[] temp = new long[4];
			ipList = ipSplit(ip);
			maskList = ipSplit(maskOrCidr);
			// 将两个二进制数逻辑与（AND）运算后得出的结果即为网络部分
			for (int i = 0; i < 4; i++) {
				temp[i] = ipList[i] & maskList[i];
			}
			return temp[0] + "." + temp[1] + "." + temp[2] + "."
					+ temp[3];
		}
	}

	/**
	 * 生成默认网关
	 */
	public static String getDefaultGateway(String cidr) {
		if (cidr == null) {
			return "";
		}
		int index = cidr.indexOf("/");
		// ip地址
		String ip = cidr.substring(0, index);
		// 子网掩码
		String maskip = maskToIpMask(cidr);
		long[] ipList = new long[4];
		long[] maskList = new long[4];
		long[] temp = new long[4];
		ipList = ipSplit(ip);
		maskList = ipSplit(maskip);
		// 将两个二进制数逻辑与（AND）运算后得出的结果即为网络部分
		for (int i = 0; i < 4; i++) {
			temp[i] = ipList[i] & maskList[i];
		}
		return temp[0] + "." + temp[1] + "." + temp[2] + "." + "1";
	}

	/**
	 * 获取子网广播地址
	 *
	 * @param maskOrCidr
	 * @return
	 */
	public static String getBroadcastAddress(String ip, String maskOrCidr) {
		int index = maskOrCidr.indexOf("/");
		if (maskOrCidr.contains(".") && index != -1) {
			return getBroadcastAddress(maskOrCidr);
		} else {
			// 子网掩码
			int maskIndex = ipInteger2Binary(maskOrCidr).indexOf("0");
			String addressBinary = ipInteger2Binary(ip).substring(0, maskIndex)
					+ ipInteger2Binary(ip).substring(maskIndex).replace("0", "1");
			return ipBinary2IntegerIp(addressBinary);
		}
	}

	/**
	 * 检查 网关是否在分配池中
	 *
	 * @param gateway

	 * @param ipPools
	 * @return
	 */
	public static boolean checkGateway(String gateway, String ipPools) {
		if (ipPools == null || "".equals(ipPools)) {
			return true;
		}
		@SuppressWarnings("unchecked")
		List<String> list = JSONObject.parseObject(ipPools, ArrayList.class);
		if (gateway == null) {
			return true;
		}
		long ipGateway = ipOrMaskToLong(gateway);
		for (String ipPool : list) {
			String[] startEndIp = ipPool.split("-");
			long ipstart, ipend;
			ipstart = ipOrMaskToLong(startEndIp[0]);// 获取起始IP的开始位置
			ipend = ipOrMaskToLong(startEndIp[1]);// 获取终止IP的结束位置
			if (ipGateway >= ipstart && ipGateway <= ipend) {
				return false;
			}

		}
		return true;
	}

	/**
	 * 检查dns格式
	 *

	 * @return
	 */
	public static boolean checkDns(String dns) {
		@SuppressWarnings("unchecked")
		List<String> list = JSONObject.parseObject(dns, ArrayList.class);
		if (dns == null) {
			return true;
		}
		for (String dnsstring : list) {
			if (!ipV4Validate(dnsstring)) {
				return false;
			}

		}
		return true;
	}

	/**
	 * 检查主机路由
	 *
	 * @param hostRoutes
	 * @return
	 */
	public static boolean checkHostRoutes(String hostRoutes) {
		if (hostRoutes == null || "".equals(hostRoutes)) {
			return true;
		}
		@SuppressWarnings("unchecked")
		List<String> list = JSONObject.parseObject(hostRoutes, ArrayList.class);
		for (String hostRoute : list) {
			if (!hostRoute.contains(",")) {
				return false;
			}
			if (!hostRoute.split(",")[0].contains("/")) {
				return false;
			}
			String[] value = hostRoute.split(",")[0].split("/");
			if (!ipV4Validate(value[0])) {
				return false;
			}
			if (Integer.parseInt(value[1]) > 32 || Integer.parseInt(value[1]) < 8) {
				return false;
			}

		}
		return true;
	}

	/**
	 * 生成分配池
	 *
	 * @param gateway
	 * @param cidr
	 * @return
	 */
	public static String buildIpPools(String gateway, String cidr) {
		long ipStartValue, ipEndValue, ipGateway;
		int index = cidr.indexOf("/");
		String maskStr = cidr.substring(index + 1);
		// 通过掩码截取，后面可变的部分全部变为0 即为最小值 排除网络号+1
		String min = ipInteger2Binary(cidr.substring(0, index)).substring(0, Integer.parseInt(maskStr))
				+ ipInteger2Binary(cidr.substring(0, index)).substring(Integer.parseInt(maskStr)).replace("1", "0");
		ipStartValue = ipOrMaskToLong(ipBinary2IntegerIp(min)) + 1L;
		// 通过掩码截取，后面可变的部分全部变为1 即为最大值 排除广播号-1
		String max = ipInteger2Binary(cidr.substring(0, index)).substring(0, Integer.parseInt(maskStr))
				+ ipInteger2Binary(cidr.substring(0, index)).substring(Integer.parseInt(maskStr)).replace("0", "1");
		ipEndValue = ipOrMaskToLong(ipBinary2IntegerIp(max)) - 1L;
		// fix bug： PBSM-2938
		// 不确认什么原因，最后一个ip的值反而比第一个值小，为解决这个问题，把前后值做一个交换。
		if (ipEndValue < ipStartValue) {
			Long temp = ipStartValue;
			ipStartValue = ipEndValue;
			ipEndValue = temp;
		}
		List<String> ipPools = new ArrayList<>();
		ipGateway = ipOrMaskToLong(gateway);
		// 最小值等于网关的话 跳过网关
		if (ipStartValue == ipGateway) {
			ipPools.add(longToIpOrMask(ipStartValue + 1L) + "-" + longToIpOrMask(ipEndValue));
		} else if (ipEndValue == ipGateway) {
			// 网关等于最大值的话 就-1
			ipPools.add(longToIpOrMask(ipStartValue) + "-" + longToIpOrMask(ipEndValue - 1L));
		} else if (ipGateway > ipStartValue && ipGateway < ipEndValue) {
			// 网关在中间的话 就分成两段
			ipPools.add(longToIpOrMask(ipStartValue) + "-" + longToIpOrMask(ipGateway - 1L));
			ipPools.add(longToIpOrMask(ipGateway + 1) + "-" + longToIpOrMask(ipEndValue));
		} else {
			ipPools.add(longToIpOrMask(ipStartValue) + "-" + longToIpOrMask(ipEndValue));
		}

		return JSONObject.toJSONString(ipPools);
	}

	/**
	 * 获取网关内ips池的ip数量
	 *
	 * @param gateway
	 * @return
	 */
	public static int countIpsByGatway(String gateway) {
		int index = gateway.indexOf("/");
		String gatewayIp = gateway.substring(0, index);
		String range = buildIpPools(gatewayIp, gateway);
		int total = 0;
		if (!StringUtils.isEmpty(range)) {
			List<String> ipPools = JSONArray.parseArray(range, String.class);
			for (String ipPool : ipPools) {
				String[] ipList = ipPool.split("-");
				long ipstart = ipOrMaskToLong(ipList[0]);// 获取起始IP的开始位置
				long ipend = ipOrMaskToLong(ipList[1]);// 获取终止IP的结束位置
				total += (int) (ipend - ipstart + 1);
			}
		}

		return total;
	}

	/**
	 * 获取子网广播地址
	 *
	 * @param cidr
	 * @return
	 */
	public static String getBroadcastAddress(String cidr) {
		int index = cidr.indexOf("/");
		if (cidr.contains(".") && index == -1) {
			return "";
		} else {
			// ip地址
			String ipStr = cidr.substring(0, index);
			// 子网掩码
			String maskStr = cidr.substring(index + 1);
			String addressBinary = ipInteger2Binary(ipStr).substring(0, Integer.parseInt(maskStr))
					+ ipInteger2Binary(ipStr).substring(Integer.parseInt(maskStr)).replace("0", "1");
			return ipBinary2IntegerIp(addressBinary);
		}
	}

	/**
	 * 将子网掩码转换成ip子网掩码形式，比如输入"255.255.255.255" 或者32统一输出为255.255.255.255  
	 *
	 * @param mask
	 * @return
	 */
	public static String maskToIpMask(String mask) {
		String ipMask = null;
		int index = mask.indexOf("/");
		// ip类型的子网掩码[0.0.0.0,255.255.255.255]则直接返回  
		if (mask.contains(".") && index == -1) {
			ipMask = mask;
		} else {
			String maskStr = mask.substring(index + 1);
			//  数字类型的子网掩码[0,32]  
			int intMask = Integer.parseInt(maskStr);
			int part = intMask / 8;
			int remainder = intMask % 8;
			int sum = 0;
			for (int i = 8; i > 8 - remainder; i--) {
				sum = sum + (int) Math.pow(2, i - 1d);
			}
			if (part == 0) {
				ipMask = sum + ".0.0.0";
			} else if (part == 1) {
				ipMask = "255." + sum + ".0.0";
			} else if (part == 2) {
				ipMask = "255.255." + sum + ".0";
			} else if (part == 3) {
				ipMask = "255.255.255." + sum;
			} else if (part == 4) {
				ipMask = DEFAULT_NETMASK;
			}
		}
		return ipMask;
	}

	public static String maskToIpMask(Integer mask) {
		if (null == mask) {
			return null;
		}
		String ipMask = null;
		//  数字类型的子网掩码[0,32]  
		int part = mask / 8;
		int remainder = mask % 8;
		int sum = 0;
		for (int i = 8; i > 8 - remainder; i--) {
			sum = sum + (int) Math.pow(2, i - 1d);
		}
		if (part == 0) {
			ipMask = sum + ".0.0.0";
		} else if (part == 1) {
			ipMask = "255." + sum + ".0.0";
		} else if (part == 2) {
			ipMask = "255.255." + sum + ".0";
		} else if (part == 3) {
			ipMask = "255.255.255." + sum;
		} else if (part == 4) {
			ipMask = DEFAULT_NETMASK;
		}
		return ipMask;
	}

	/**
	 * 判断IPV4是否合法
	 *
	 * @param ipv4
	 * @return
	 */
	public static boolean ipV4Validate(String ipv4) {
		return ipv4Validate(ipv4, IPV4_REGEX);
	}

	/**
	 * 判断IPV4是否合法
	 *
	 * @param ipv4
	 * @param regex
	 * @return
	 */
	public static boolean ipv4Validate(String ipv4, String regex) {
		if (ipv4 == null) {
			return false;
		} else {
			return Pattern.matches(regex, ipv4.trim());
		}
	}

	/**
	 * 获取ip集合
	 *

	 * @return
	 */
	public static List<Object> ipList(String startIp, String endIp) {
		long ipstart, ipend;
		ipstart = ipOrMaskToLong(startIp);// 获取起始IP的开始位置
		ipend = ipOrMaskToLong(endIp);// 获取终止IP的结束位置
		String[] result = new String[(int) (ipend - ipstart + 1)];
		List<Object> ipList = new ArrayList<>();
		for (int i = 0; i < result.length; i++) {
			ipList.add(longToIpOrMask(ipstart + i));
		}
		return ipList;
	}

	/**
	 * 获取ip集合
	 *

	 *            ：["192.168.1.1-192.168.1.255","192.168.2.1-192.168.2.255"]
	 * @return
	 */
	public static List<Object> ipList(String ipPools) {
		@SuppressWarnings("unchecked")
		List<String> list = JSONObject.parseObject(ipPools, ArrayList.class);
		List<Object> ipList = new ArrayList<>();
		for (String ipPool : list) {
			String[] startEndIp = ipPool.split("-");
			long ipstart, ipend;
			ipstart = ipOrMaskToLong(startEndIp[0]);// 获取起始IP的开始位置
			ipend = ipOrMaskToLong(startEndIp[1]);// 获取终止IP的结束位置
			String[] result = new String[(int) (ipend - ipstart + 1)];
			for (int i = 0; i < result.length; i++) {
				ipList.add(longToIpOrMask(ipstart + i));
			}
		}
		return ipList;
	}

	/**
	 * ipPool的字符串转换为list
	 *
	 * @param ipPools
	 * @return
	 */
	public static List<Pool> iPoolsString2Pool(String ipPools) {
		@SuppressWarnings("unchecked")
		List<String> list = JSONObject.parseObject(ipPools, ArrayList.class);
		List<Pool> ipPoolList = new ArrayList<>();
		for (String ipPool : list) {
			String[] startEndIp = ipPool.split("-");
			ipPoolList.add(new NeutronPool(startEndIp[0], startEndIp[1]));
		}
		return ipPoolList;
	}

	/**
	 * 检查分配池是否合法 超出范围
	 *
	 * @param ipPools
	 * @param cidr
	 * @return
	 */
	public static boolean checkIpPOools(String ipPools, String cidr) {
		@SuppressWarnings("unchecked")
		List<String> list = JSONObject.parseObject(ipPools, ArrayList.class);
		for (String ipPool : list) {
			String[] startEndIp = ipPool.split("-");
			boolean result = checkIpMask(startEndIp[0], startEndIp[1], cidr);
			if (!result) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 将127.0.0.1形式的IP地址转换成十进制整数，这里没有进行任何错误处理
	 *
	 * @param ipOrMask
	 * @return
	 */
	public static long ipOrMaskToLong(String ipOrMask) {
		long[] ip = ipSplit(ipOrMask);
		return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
	}

	/**
	 * 将4段ip拆到数组里
	 *
	 * @param ipOrMask
	 * @return
	 */
	public static long[] ipSplit(String ipOrMask) {
		long[] ip = new long[4];
		if (null == ipOrMask) {
			return ip;
		}
		// 先找到IP地址字符串中.的位置
		int position1 = ipOrMask.indexOf(".");
		int position2 = ipOrMask.indexOf(".", position1 + 1);
		int position3 = ipOrMask.indexOf(".", position2 + 1);
		// 将每个.之间的字符串转换成整型
		ip[0] = Long.parseLong(ipOrMask.substring(0, position1));
		ip[1] = Long.parseLong(ipOrMask.substring(position1 + 1, position2));
		ip[2] = Long.parseLong(ipOrMask.substring(position2 + 1, position3));
		ip[3] = Long.parseLong(ipOrMask.substring(position3 + 1));
		return ip;

	}

	/**
	 * 将十进制整数形式转换成127.0.0.1形式的ip地址
	 *
	 * @param longIpOrMask
	 * @return
	 */
	public static String longToIpOrMask(long longIpOrMask) {
		StringBuffer sb = new StringBuffer();
		// 直接右移24位
		sb.append((longIpOrMask >>> 24));
		sb.append(".");
		// 将高8位置0，然后右移16位
		sb.append(((longIpOrMask & 0x00FFFFFF) >>> 16));
		sb.append(".");
		// 将高16位置0，然后右移8位
		sb.append(((longIpOrMask & 0x0000FFFF) >>> 8));
		sb.append(".");
		// 将高24位置0
		sb.append((longIpOrMask & 0x000000FF));
		return sb.toString();
	}

	/**
	 * 判断IP是否在某个网段内
	 */
	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);
	}

	public static boolean isInRange(List<String> pools, Long value) {
		if (ListTool.isEmpty(pools)) {
			return false;
		}
		for (String pool : pools) {
			String[] startEndIp = pool.split("-");
			long ipstart = ipOrMaskToLong(startEndIp[0]);// 获取起始IP的开始位置
			long ipend = ipOrMaskToLong(startEndIp[1]);// 获取终止IP的结束位置
			if (value < ipstart || value > ipend) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 新增IP池校验，只能增加，不能减小
	 *
	 * @param ipPools
	 * @return
	 */
	public static boolean checkUpdatePool(String ipPools,String oldPools) {
		if (StringUtils.isEmpty(ipPools) || StringUtils.isEmpty(oldPools)) {
			return false;
		}
		@SuppressWarnings("unchecked")
		List<String> list = JSONObject.parseObject(ipPools, ArrayList.class);
		List<String> oldList = JSONObject.parseObject(oldPools, ArrayList.class);
		long ipStart, ipEnd,uIpStart, uIpEnd;
		for(int i=0 ;i< oldList.size();i++){
			if(i > list.size()){
				return false;
			}
			String[] startEndIp = oldList.get(i).split("-");
			ipStart = ipOrMaskToLong(startEndIp[0]);// 获取起始IP的开始位置
			ipEnd = ipOrMaskToLong(startEndIp[1]);// 获取终止IP的结束位置
			String[] uStartEndIp = list.get(i).split("-");
			uIpStart = ipOrMaskToLong(uStartEndIp[0]);// 获取起始IP的开始位置
			uIpEnd = ipOrMaskToLong(uStartEndIp[1]);// 获取终止IP的结束位置
			if(uIpStart <=0 || uIpStart > ipStart || uIpEnd < ipEnd){
				return false;
			}
		}
		return true;
	}

	public static Long getPoolofIp(List<IpPool> ipPools, Long value) {
		if (ipPools == null || ipPools.size() == 0) {
			return null;
		}
        List<String> poolList;
        long ipStart, ipEnd;
        for (IpPool ipPool : ipPools) {
			if(StringUtils.isNotBlank(ipPool.getPools())){
                poolList = JSONObject.parseObject(ipPool.getPools(), ArrayList.class);
                for (String pool : poolList) {
                    String[] startEndIp = pool.split("-");
                    ipStart = ipOrMaskToLong(startEndIp[0]);// 获取起始IP的开始位置
                    ipEnd = ipOrMaskToLong(startEndIp[1]);// 获取终止IP的结束位置
                    if (value >= ipStart && value <= ipEnd) {
                        return ipPool.getId();
                    }
                }
			}
		}
		return null;
	}

	public static boolean checkIpInRange(String ip, String cidr) {
		String[] ips = ip.split("\\.");
		long ipAddr = (Long.parseLong(ips[0]) << 24)
				| (Long.parseLong(ips[1]) << 16)
				| (Long.parseLong(ips[2]) << 8)
				| Long.parseLong(ips[3]);
		long type = Long.parseLong(cidr.replaceAll(".*/", ""));
		long mask = 0xFFFFFFFF << (32 - type);
		String cidrIp = cidr.replaceAll("/.*", "");
		String[] cidrIps = cidrIp.split("\\.");
		long networkIpAddr = (Long.parseLong(cidrIps[0]) << 24)
				| (Long.parseLong(cidrIps[1]) << 16)
				| (Long.parseLong(cidrIps[2]) << 8)
				| Long.parseLong(cidrIps[3]);
		return (ipAddr & mask) == (networkIpAddr & mask);
	}

}
