Web开发中Javascript校验IP是否属于指定的网段

 Web开发中通常会有很多数据需要校验,有时一些校验不是一句简单的正则表达式能搞定的,需要复杂的计算规则,例如最近遇到了一个较为复杂的数据校验,当我们输入一个IP地址的时候需要检验这个IP地址是否属于我们所指定的子网内,下面的code是自己写的一个较为粗糙的一个校验方法,以后有时间还会继续重构一下。

function ValidateIPRange(subnet, netmask){
    /**
     * subnetBin: 子网的二进制表示
     * netmaskBin: 子网掩码的二进制表示
     * networkLength: 子网的网络地址长度
     * subnetNetBit: 子网的网络地址位二进制表示
     * subnetHostBit: 子网的主机地址二进制表示
     * broadcastHostBit: 广播地址二进制表示
     */
    var subnetBin = _ipToBin(subnet);
    var netmaskBin = _ipToBin(netmask);
    var networkLength = _getNetworkLength();
    var subnetNetBit = "";
    var subnetHostBit = "";
    var broadcastHostBit = "";
    
    //判断目标IP是不是属于指定的子网内
    this.isInRange = function(targetIP){
        this.getNetwork()
        var targetIPNetworkBin = _ipToBin(targetIP);
        var targetIPNetBit = targetIPNetworkBin.substr(0, networkLength);
        var targetIPHostBit = targetIPNetworkBin.substr(networkLength);
        //排除网络地址与广播地址
        if(targetIPNetBit == subnetNetBit && targetIPHostBit != subnetHostBit && targetIPHostBit != broadcastHostBit){
            return true;
        }else{
            return false;
        }
    }

    this.getNetwork = function(){
        var networkBin = _andOpration(subnetBin, netmaskBin);
        subnetNetBit = networkBin.substr(0, networkLength);
        subnetHostBit = networkBin.substr(networkLength);
        broadcastHostBit = subnetHostBit.replace(/0/g, "1");
        return _binToIp(networkBin);
    }

    this.getBroadcast = function(){
        this.getNetwork();
        var broadcastBin = subnetNetBit + broadcastHostBit;
        return _binToIp(broadcastBin);
    }
    
        
    function _getNetworkLength(){
        var length = 0;
        for(var i = 0; i < 32; i++){
            if(netmaskBin.charAt(i) == "1"){
                length++;
            }else{
                break;
            }
        }
        return length;
    }

    function _andOpration(ipBin, netmaskBin){
        var result = "";
        for(var i = 0; i < 32; i++){
            result += (parseInt(ipBin.charAt(i)) & parseInt(netmaskBin.charAt(i)));
        }
        return result;
    }

    function _ipToBin(ip){
        var subIpArray = ip.split(".");
        var ipBin = "";
        for(var i = 0; i < subIpArray.length; i++){
            ipBin += _binCompletion(parseInt(subIpArray[i]).toString(2));
        }
        return ipBin;
    }
    
    function _binToIp(bin){
        var ip = "";
        for(var i = 0; i < bin.length; i += 8){
            ip += ("." + parseInt(bin.substr(i, 8), 2));
        }
        ip = ip.substr(1);
        return ip;
    }

    function _binCompletion(subBin){
        var complBin = subBin;
        if(subBin.length < 8){
            var length = subBin.length;
            for (var i = 0; i < (8 - length); i++)
            {
                complBin = "0" + complBin;
            }
        }
        return complBin;
    }
    
    //ip2number和number2ip是在网上找到的更简洁的方法,需要一点算法基础
    function ip2number(ip) {
        var tokens = ip.split('.');
        var numval = 0
        for (num in tokens) {
            numval = (numval << 8) + parseInt(tokens[num]);
        }
        return numval
    }

    function number2ip(number) {
        byte1 = number & 0xff;
        byte2 = (number & 0xff00);
        byte2 >>>= 8;
        byte3 = (number & 0xff0000);
        byte3 >>>= 16;
        byte4 = (number & 0xff000000);
        byte4 >>>= 24;
        var result = byte4 + "." + byte3 + "." + byte2 + "." + byte1;
        return result;
    }
}

var validate = new ValidateIPRange("172.17.16.0", "255.255.0.0");
alert(validate.isInRange("172.17.16.4"));
alert(validate.getNetwork());
alert(validate.getBroadcast());

 

 

 

posted @ 2012-09-10 15:28  DanielXLee  阅读(2685)  评论(0编辑  收藏  举报