MINA源码分析----怎么设置IP限制的(防火墙)



主要涉及到以下两个类  


一个是IP子网类  (IPV4)

package org.apache.mina.filter.firewall;

import java.net.Inet4Address;
import java.net.InetAddress;

/**
 * A IP subnet using the CIDR notation符号.     无类域内路由选择(Classless Inter-Domain Routing)
 Currently, only IP version 4
 * address are supported.
 *
 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
 */
public class Subnet {
	/**有符号数,这里最高位为1,即负数,负数在计算机中是以补码表示的!!
	 * 例如    IP_MASK = 0x80000000 ,是以补码形式表示的负数 ,其原码为    0x80000000  减1取反 ,最高位不变
	 *      同理  若  IP_MASK = 0x80000010  ,则其表示的负数  为   0x80000001 减 1取反,最高位为1 
	 *      即    0xffffffff  才是其真正表示的负数,这才是我们平时做题计算用的形式 ,与计算机中存储的形式不同!!
	 */
    private static final int IP_MASK = 0x80000000;//凡是在程序中表示的二进制数都表示计算机中的存储,是补码
    private static final int BYTE_MASK = 0xFF;

    private InetAddress subnet;//子网IP
    private int subnetInt;//IP的整数表示
    private int subnetMask;//子网掩码
    private int suffix;//后缀

    /**
     * Creates a subnet from CIDR notation. For example, the subnet
     * 192.168.0.0/24 would be created using the {@link InetAddress}  
     * 192.168.0.0 and the mask 24.
     * @param subnet The {@link InetAddress} of the subnet
     * @param mask The mask
     * 
     * 192.168.0.0/24这是IP地址的一个规范写法,前面是IP地址,
     * 后面跟一个斜杠以及一个数字,这条斜杠及后面的数字称为网络掩码(network mask)。
     * 斜杠后面的数字表示有意义的比特位的个数(从左到右)。
     * 例如IP地址:255.255.255.255是IPv4中最大可能的IP地址,
     * 每个数字(255)都是由8个比特位表示的,每个比特位非0即1,最大值即为11111111,即28=256(0-255)。
     * 了解了IP地址之后,就很容易理解上述的写法了。
     * 比如192.168.0.0/24中的24表示从左到右的24位(也就是前24位)有效,那么剩下的8位可以是任意数值,
     * 可以是0-254之间的任一地址(255为广播地址)。同样192.168.0.0/32也很好理解,
     * 就是上述IP地址的前32位有效,也就是所有的位都是有效的,即为192.168.0.0。
     */
    public Subnet(InetAddress subnet, int mask) {
        if(subnet == null) {
            throw new IllegalArgumentException("Subnet address can not be null");
        }
        if(!(subnet instanceof Inet4Address)) {
            throw new IllegalArgumentException("Only IPv4 supported");
        }

        if(mask < 0 || mask > 32) {
            throw new IllegalArgumentException("Mask has to be an integer between 0 and 32");
        }
        
        this.subnet = subnet;
        this.subnetInt = toInt(subnet);
        this.suffix = mask;
        
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值