Skip to content

Return broadcast ip #30

Description

@GaryFunk

Could you add the broadcast IP to the return information for active_interface?

Here is some not so pretty code that I found and modified.

function IPv4_Address( addressDotQuad, netmaskDotQuad ) {
var addressInteger = IPv4_dotquad_to_int( addressDotQuad.toString() );
var addressBinStr = IPv4_int_to_binstr( addressInteger );

var netmaskInteger = IPv4_dotquad_to_int( netmaskDotQuad.toString() );
var netmaskBinStr  = IPv4_int_to_binstr( netmaskInteger );

var netbcastBinStr = IPv4_calc_netbcastBinStr( addressBinStr, netmaskBinStr );
var netbcastInteger = IPv4_binstr_to_int( netbcastBinStr );	

return IPv4_int_to_dotquad( netbcastInteger );

}

/* dotted-quad IP to integer /
function IPv4_dotquad_to_int( strbits ) {
var split = strbits.split( '.', 4 );
var myInt = (
parseFloat( split[0] * 16777216 ) /
2^24 /
+ parseFloat( split[1] * 65536 ) /
2^16 /
+ parseFloat( split[2] * 256 ) /
2^8 */
+ parseFloat( split[3] )
);
return myInt;
}

/* integer IP to binary string representation /
function IPv4_int_to_binstr( strnum ) {
var numStr = strnum.toString( 2 ); /
Initialize return value as string /
var numZeros = 32 - numStr.length; /
Calculate no. of zeros */
if (numZeros > 0) {
for (var i = 1; i <= numZeros; i++) {
numStr = "0" + numStr
}
}
return numStr;
}

/* logical OR between address & NOT netmask /
function IPv4_calc_netbcastBinStr( addressBinStr, netmaskBinStr ) {
var netbcastBinStr = '';
var aBit = 0; var nmBit = 0;
for( pos = 0; pos < 32; pos ++ ) {
aBit = parseInt( addressBinStr.substr( pos, 1 ));
nmBit = parseInt( netmaskBinStr.substr( pos, 1 ));
if( nmBit ) {
nmBit = 0; /
flip netmask bits */
} else {
nmBit = 1;
}
if( aBit || nmBit ) {
netbcastBinStr += '1';
} else {
netbcastBinStr += '0';
}
}
return netbcastBinStr;
}

/* binary string IP to integer representation */
function IPv4_binstr_to_int( binstr ) {
return parseInt( binstr, 2 );
}

/* integer IP to dotted-quad */
function IPv4_int_to_dotquad( strnum ) {
var byte1 = ( strnum >>> 24 );
var byte2 = ( strnum >>> 16 ) & 255;
var byte3 = ( strnum >>> 8 ) & 255;
var byte4 = strnum & 255;
return ( byte1 + '.' + byte2 + '.' + byte3 + '.' + byte4 );
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions