web-dev-qa-db-fra.com

Comment puis-je déterminer le réseau et l'adresse de diffusion à partir de l'adresse IP et du masque de sous-réseau?

Par exemple:

  • Adresse IP: 130.45.34.36
  • Masque: 255.255.240.0

Quelle serait l'adresse Net ID/sous-réseau et l'adresse de diffusion?

16
Apprentice

Écrivons les deux en binaire:

130.45.34.36 = 10000010.00101101.00100010.00100100

255.255.240.0 = 11111111.11111111.11110000.00000000

Un bit à bit [~ # ~] et [~ # ~] entre les deux nous donnerait l'adresse réseau:

10000010.00101101.00100010.00100100   (ip address)
AND
11111111.11111111.11110000.00000000   (subnet mask)
=
10000010.00101101.00100000.00000000 = 130.45.32.0 (the resulting network address)

Un bitwise [~ # ~] ou [~ # ~] entre l'adresse réseau et le masque de sous-réseau inversé nous donnerait l'adresse de diffusion:

10000010.00101101.00100000.00000000   (netadress)
OR
00000000.00000000.00001111.11111111   (inverted subnet mask)
=
10000010.00101101.00101111.11111111 = 130.45.47.255 (broadcast address)
48
Malt
var network = calculateNetworkIP("192.168.0.101", "255.255.255.0");

var broadcast = calculateBroadcastIP("192.168.0.101", "255.255.255.0");

function calculateNetworkIP(ipAddress, maskIP){

    var binaryIP = convertIPToBinaryIP(ipAddress);
    var maskBinaryIP = convertIPToBinaryIP(maskIP);

    var binaryNetwork = [];
    for (var j = 0; j < maskBinaryIP.length; j++) {
        binaryNetwork.Push(bitwiseAND(binaryIP[j], maskBinaryIP[j]));
    }

    var NetworkIPArr = convertBinaryIPToDecIP(binaryNetwork);

    var NetworkIPStr = "";
    for (var k = 0; k < NetworkIPArr.length; k++) {
        NetworkIPStr += NetworkIPArr[k]+".";
    }
    return NetworkIPStr.slice(0, -1);
}

function calculateBroadcastIP(ipAddress, maskIP){

    var binaryIP = convertIPToBinaryIP(ipAddress);
    var maskBinaryIP = convertIPToBinaryIP(maskIP);
    var invertedMark = [];
    for (var i = 0; i < maskBinaryIP.length; i++) {
        invertedMark.Push(invertedBinary(maskBinaryIP[i]));
    }

    var binaryBroadcast = [];
    for (var j = 0; j < maskBinaryIP.length; j++) {
        binaryBroadcast.Push(bitwiseOR(binaryIP[j], invertedMark[j]));
    }

    var broadcastIPArr = convertBinaryIPToDecIP(binaryBroadcast);

    var broadcastIPStr = "";
    for (var k = 0; k < broadcastIPArr.length; k++) {
        broadcastIPStr += broadcastIPArr[k]+".";
    }
    return broadcastIPStr.slice(0, -1);
}

function invertedBinary(number){

    var no = number+"";
    var noArr = no.split("");
    var newNo = "";
    for(var i = 0; i < noArr.length; i++){
        if(noArr[i] == "0"){
            newNo += "1";
        }else{
            newNo += "0";
        }
    }
    return newNo;
}

function bitwiseAND(firstBinary, secondBinary){

    var firstArr = [];
    var secondArr = [];
    firstArr = firstBinary.split("");
    secondArr = secondBinary.split("");
    var newAdded = "";
    for(var i = 0; i < firstArr.length; i++){
        if(firstArr[i]+"+"+secondArr[i] == "1+0"){
            newAdded += "0";
        }else if(firstArr[i]+"+"+secondArr[i] == "0+1"){
            newAdded += "0";
        }else if(firstArr[i]+"+"+secondArr[i] == "1+1"){
            newAdded += "1";
        }else if(firstArr[i]+"+"+secondArr[i] == "0+0"){
            newAdded += "0";
        }
    }
    return newAdded;
}

function bitwiseOR(firstBinary, secondBinary){

    var firstArr = [];
    var secondArr = [];
    firstArr = firstBinary.split("");
    secondArr = secondBinary.split("");
    var newAdded = "";
    for(var i = 0; i < firstArr.length; i++){
        if(firstArr[i]+"+"+secondArr[i] == "1+0"){
            newAdded += "1";
        }else if(firstArr[i]+"+"+secondArr[i] == "0+1"){
            newAdded += "1";
        }else if(firstArr[i]+"+"+secondArr[i] == "1+1"){
            newAdded += "1";
        }else if(firstArr[i]+"+"+secondArr[i] == "0+0"){
            newAdded += "0";
        }
    }
    return newAdded;
}

function convertBinaryIPToDecIP(binaryIPArr){

    var broadcastIP = [];
    for (var i = 0; i < binaryIPArr.length; i++) {
        broadcastIP.Push(parseInt(parseInt(binaryIPArr[i]), 2));
    }
    return broadcastIP;
}

function convertIPToBinaryIP(ipAddress) {

    var ipArr = ipAddress.split(".");
    var binaryIP = [];
    for (var i = 0; i < ipArr.length; i++) {
        var binaryNo = parseInt(ipArr[i]).toString(2);
        if(binaryNo.length == 8){
            binaryIP.Push(binaryNo);
        }else{
            var diffNo = 8 - binaryNo.length;
            var createBinary = '';
            for (var j = 0; j < diffNo; j++) {
               createBinary += '0';
            }
            createBinary += binaryNo;
            binaryIP.Push(createBinary);
        }
    }
   return binaryIP; 
}
2
Chitranshu Gupta

Un autre raccourci pour le calcul d'adresse de diffusion après avoir obtenu l'adresse netwotk est:

  1. calculer le nombre total d'hôtes (dans ce cas, il s'agit de 2 ^ 12 = 4096)

  2. Divisez-le par 256 (dans ce cas, il est 16) et ajoutez le résultat - 1 (dans ce cas 15) dans * l'octet correspondant (dans ce cas, le deuxième octet, c'est-à-dire 32 + 15 = 47) et créez un autre octet 255

* nous pouvons obtenir l'octet correspondant en regardant le nombre d'hôtes. Par exemple, si le nombre d'hôtes est supérieur à 256, nous devons l'ajouter au 2ème octet de l'adresse réseau, etc.

1

Exemple de code basé sur la réponse de Malt:

const
    ipadr = '130.45.34.36',
    subnet = '255.255.240.0',
    ipadrs = ipadr.split('.'),
    subnets = subnet.split('.');

let networks = [],
    broadcasts = [];

for (let i in ipadrs) {
    networks[i] = ipadrs[i] & subnets[i];
}

console.log('netaddress: ', networks.join('.')) // netaddress:  130.45.32.0

for (let i in networks) {
    broadcasts[i] = networks[i] | ~subnets[i] + 256;
}

console.log('broadcast address: ', broadcasts.join('.')) // broadcast address:  130.45.47.255
0
Ben