export function IPv4_Address ( this , addressDotQuad , netmaskBits ) { var split = addressDotQuad . split ( "." , 4 ) ; var byte1 = Math . max ( 0 , Math . min ( 255 , parseInt ( split [ 0 ] ) ) ) ; var byte2 = Math . max ( 0 , Math . min ( 255 , parseInt ( split [ 1 ] ) ) ) ; var byte3 = Math . max ( 0 , Math . min ( 255 , parseInt ( split [ 2 ] ) ) ) ; var byte4 = Math . max ( 0 , Math . min ( 255 , parseInt ( split [ 3 ] ) ) ) ; if ( isNaN ( byte1 ) ) { byte1 = 0 ; } if ( isNaN ( byte2 ) ) { byte2 = 0 ; } if ( isNaN ( byte3 ) ) { byte3 = 0 ; } if ( isNaN ( byte4 ) ) { byte4 = 0 ; } addressDotQuad = byte1 + "." + byte2 + "." + byte3 + "." + byte4 ; this . addressDotQuad = addressDotQuad . toString ( ) ; this . netmaskBits = Math . max ( 0 , Math . min ( 32 , parseInt ( netmaskBits ) ) ) ; this . addressInteger = IPv4_dotquadA_to_intA ( this . addressDotQuad ) ; this . addressBinStr = IPv4_intA_to_binstrA ( this . addressInteger ) ; this . netmaskBinStr = IPv4_bitsNM_to_binstrNM ( this . netmaskBits ) ; this . netmaskInteger = IPv4_binstrA_to_intA ( this . netmaskBinStr ) ; this . netmaskDotQuad = IPv4_intA_to_dotquadA ( this . netmaskInteger ) ; this . netaddressBinStr = IPv4_Calc_netaddrBinStr ( this . addressBinStr , this . netmaskBinStr ) ; this . netaddressInteger = IPv4_binstrA_to_intA ( this . netaddressBinStr ) ; this . netaddressDotQuad = IPv4_intA_to_dotquadA ( this . netaddressInteger ) ; this . netbcastBinStr = IPv4_Calc_netbcastBinStr ( this . addressBinStr , this . netmaskBinStr ) ; this . netbcastInteger = IPv4_binstrA_to_intA ( this . netbcastBinStr ) ; this . netbcastDotQuad = IPv4_intA_to_dotquadA ( this . netbcastInteger ) ; } export function IPv4_dotquadA_to_intA ( strbits ) { var split = strbits . split ( "." , 4 ) ; var myInt = parseFloat ( ( split [ 0 ] * 16777216 ) as any ) + parseFloat ( ( split [ 1 ] * 65536 ) as any ) + parseFloat ( ( split [ 2 ] * 256 ) as any ) + parseFloat ( split [ 3 ] ) ; return myInt ; } export function IPv4_intA_to_dotquadA ( strnum ) { var byte1 = strnum >>> 24 ; var byte2 = ( strnum >>> 16 ) & 255 ; var byte3 = ( strnum >>> 8 ) & 255 ; var byte4 = strnum & 255 ; return byte1 + "." + byte2 + "." + byte3 + "." + byte4 ; } export function IPv4_intA_to_binstrA ( strnum ) { var numStr = strnum . toString ( 2 ) ; var numZeros = 32 - numStr . length ; if ( numZeros > 0 ) { for ( var i = 1 ; i <= numZeros ; i ++ ) { numStr = "0" + numStr ; } } return numStr ; } export function IPv4_binstrA_to_intA ( binstr ) { return parseInt ( binstr , 2 ) ; } export function IPv4_bitsNM_to_binstrNM ( bitsNM ) { var bitString = "" ; var numberOfOnes = bitsNM ; while ( numberOfOnes -- ) bitString += "1" ; let numberOfZeros = 32 - bitsNM ; while ( numberOfZeros -- ) bitString += "0" ; return bitString ; } export function IPv4_Calc_netaddrBinStr ( addressBinStr , netmaskBinStr ) { var netaddressBinStr = "" ; var aBit = 0 ; var nmBit = 0 ; for ( let pos = 0 ; pos < 32 ; pos ++ ) { aBit = addressBinStr . substr ( pos , 1 ) ; nmBit = netmaskBinStr . substr ( pos , 1 ) ; if ( aBit == nmBit ) { netaddressBinStr += aBit . toString ( ) ; } else { netaddressBinStr += "0" ; } } return netaddressBinStr ; } export function IPv4_Calc_netbcastBinStr ( addressBinStr , netmaskBinStr ) { var netbcastBinStr = "" ; var aBit = 0 ; var nmBit = 0 ; for ( let pos = 0 ; pos < 32 ; pos ++ ) { aBit = parseInt ( addressBinStr . substr ( pos , 1 ) ) ; nmBit = parseInt ( netmaskBinStr . substr ( pos , 1 ) ) ; if ( nmBit ) { nmBit = 0 ; } else { nmBit = 1 ; } if ( aBit || nmBit ) { netbcastBinStr += "1" ; } else { netbcastBinStr += "0" ; } } return netbcastBinStr ; } export function IPv4_BitShiftLeft ( mask , bits ) { return mask * Math . pow ( 2 , bits ) ; } export function IPv4_BinaryDotQuad ( binaryString ) { return ( binaryString . substr ( 0 , 8 ) + "." + binaryString . substr ( 8 , 8 ) + "." + binaryString . substr ( 16 , 8 ) + "." + binaryString . substr ( 24 , 8 ) ) ; }