Jump to content

Calculating broadcast address ?


This topic is 6484 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I have a javascript which takes an ip address and a subnet mask and returns the broadcast address of the two. I'd like to turn this into a custom function, but haven't got a clue how to start.

Anyone have any ideas? Heres the javascript:

<SCRIPT language=JAVASCRIPT>

<!--

nGlobal=0;

function ClearBroadcast(f)

{

f.Broadcast.value = "";

f.Network.value = "";

}

function Len2MaskCalc(nMask)

{

if (nMask < 1)

{

return 0;

}

nCalc = 0;

for (nX = 7;nX > -1 ; nX--)

{

nCalc=nCalc + raiseP(2 , nX);

nMask = nMask -1;

nGlobal=nMask;

if (nMask <1)

{

return nCalc;

}

}

return nCalc;

}

function raiseP(x,y)

{ total=1;

for (j=0; j < y; j++)

{ total*=x; }

return total; //result of x raised to y power

}

function CalcLen2Mask(f)

{

if ((f.MaskLen.value < 0)|| (f.MaskLen.value>32))

{

alert("Your mask length can only be 0 - 32");

return 0;

}

nMaskLen = f.MaskLen.value;

f.IP1.value=Len2MaskCalc(nMaskLen);

f.IP2.value=Len2MaskCalc(nGlobal);

f.IP3.value=Len2MaskCalc(nGlobal);

f.IP4.value=Len2MaskCalc(nGlobal);

}

function CalcMask2Len(f)

{

var m = new Array(1,2,3,4);

m[0] = f.IP1.value;

m[1] = f.IP2.value;

m[2] = f.IP3.value;

m[3] = f.IP4.value;

ipcheck=TestSN(m[0],m[1],m[2],m[3]);

if (ipcheck > 0)

{

alert("Subnet mask is not legal!");

return 0;

}

mask = 0;

for (loop=0; loop<4; loop++)

{

div = 256;

while (div > 1)

{

div = div/2;

test = m[loop]-div;

if ( test >-1)

{

mask=mask+1;

m[loop]=test;

}

else

{

break;

}

}

}

f.MaskLen.value = mask;

}

function CalcBroadcast(f)

{

ClearBroadcast(f);

var ipcheck=TestIP(f.IP1.value,f.IP2.value,f.IP3.value,f.IP4.value);

if (ipcheck > 0)

{

alert("IP address is not legal!");

return 0;

}

ipcheck=TestSN(f.SN1.value,f.SN2.value,f.SN3.value,f.SN4.value);

if (ipcheck > 0)

{

alert("Subnet mask is not legal!" );

return 0;

}

// this will calculate the network address

var nOctA1=f.IP1.value & f.SN1.value

var nOctA2=f.IP2.value & f.SN2.value

var nOctA3=f.IP3.value & f.SN3.value

var nOctA4=f.IP4.value & f.SN4.value

// This will calculate the broadcast address

var nOctB1=f.IP1.value | (f.SN1.value ^ 255)

var nOctB2=f.IP2.value | (f.SN2.value ^ 255)

var nOctB3=f.IP3.value | (f.SN3.value ^ 255)

var nOctB4=f.IP4.value | (f.SN4.value ^ 255)

f.Broadcast.value = nOctB1+"."+nOctB2+"."+nOctB3+"."+nOctB4

f.Network.value = nOctA1+"."+nOctA2+"."+nOctA3+"."+nOctA4

}

function TestIP(IP1, IP2, IP3, IP4)

{

// alert(IP1+"."+IP2+"."+IP3+"."+IP4)

if ((IP1 > 255) || (IP1 < 1))

{

return 1;

}

if ((IP2 > 255) || (IP2 < 0))

{

return 2;

}

if ((IP3 > 255) || (IP3 < 0))

{

return 3;

}

if ((IP4 > 255) || (IP4 < 0))

{

return 4;

}

return 0;

}

function TestSN(IP1, IP2, IP3, IP4)

{

// alert(IP1+"."+IP2+"."+IP3+"."+IP4)

if ((IP1 > 255) || (IP1 < 0))

{

return 1;

}

if ((IP2 > 255) || (IP2 < 0))

{

return 2;

}

if ((IP3 > 255) || (IP3 < 0))

{

return 3;

}

if ((IP4 > 255) || (IP4 < 0))

{

return 4;

}

var IPX =5;

// find where it changes

if (IP1 < 255)

{

if((IP2 > 0) || (IP3 > 0) || (IP4 > 0))

{

return 5;

}

IPX = IP1;

}

else

{

if (IP2 < 255)

{

if((IP3 > 0) || (IP4 > 0))

{

return 6;

}

IPX = IP2;

}

else

{

if (IP3 < 255)

{

if((IP4 > 0))

{

return 7;

}

IPX = IP3;

}

else

{

IPX = IP4;

}

}

}

// determine if IPX is a good

switch (IPX)

{

case "255":

case "128":

case "192":

case "224":

case "240":

case "248":

case "252":

case "254":

case "0":

return 0;

default:

return 8;

}

return 0;

}

//-->

</SCRIPT>

</HEAD>

<BODY>

<DIV align=left> </DIV>

<DIV align=left>

<TABLE width=808 border=0>

<TBODY>

<TR>

<TD width=105></TD>

<TD width=689>

<H3>CALCULATE YOUR NETWORK's BROADCAST ADDRESS</H3>

<P>The SaturnManager Station must be on the same physical and logical network as <BR>

the workstations you wish to control.</P>

<FORM name=fCalcBroadcast>

<TABLE cellSpacing=0 cellPadding=0 border=0>

<TBODY>

<TR>

<TD>

<DIV align=right>

<P>Enter the IP address of your SaturnManager Station:</P></DIV></TD>

<TD><INPUT onclick=ClearBroadcast(this.form) maxLength=3 size=3

name=IP1>.<INPUT onclick=ClearBroadcast(this.form) maxLength=3

size=3 name=IP2>. <INPUT onclick=ClearBroadcast(this.form)

maxLength=3 size=3 name=IP3>.<INPUT

onclick=ClearBroadcast(this.form) maxLength=3 size=3 name=IP4></TD></TR>

<TR>

<TD>

<DIV align=right>

<P>Enter your networks subnet mask (Probably 255.255.255.0):</P></DIV></TD>

<TD><INPUT onclick=ClearBroadcast(this.form) maxLength=3 size=3

value=255 name=SN1>.<INPUT onclick=ClearBroadcast(this.form)

maxLength=3 size=3 value=255 name=SN2>. <INPUT

onclick=ClearBroadcast(this.form) maxLength=3 size=3 value=255

name=SN3>.<INPUT onclick=ClearBroadcast(this.form) maxLength=3

size=3 value=0 name=SN4></TD></TR></TBODY></TABLE>

<P><INPUT onclick=CalcBroadcast(this.form) type=button value=Calculate name=B1>

</P>

<P>Here is your Network Broadcast Address that you must enter in both<BR>

SaturnManager and SaturnClient for the system to work correctly:</P>

<DIV align=left>

<TABLE cellSpacing=1 border=0>

<TBODY>

<TR>

<TD>

<DIV align=right>

<P>Broadcast Address:</P></DIV></TD>

<TD><INPUT name=Broadcast></TD></TR>

<TR>

<TD>

<DIV align=right>

<P>Network Address (not needed):</P></DIV></TD>

<TD><INPUT name=Network></TD>

</TD>

</TR></TBODY></TABLE></DIV></BODY></HTML>

Version: Developer v7

Platform: Windows XP

Link to comment
Share on other sites

Hi,

I don't know much about javaScript, but I think the broadcast address is roughly:

( IP bitAnd subnet ) BitOr bitNot(subnet)

You can find a bitwise and/or/not custom function implementation here:

http://www.clevelandconsulting.com/support/viewtopic.php?t=81

Then, your function might look something like:

BroadCast( ip1, ip2, ip3, ip4, subnet1, subnet2, subnet3, subnet4 ) =

   BitOr( BitAnd( ip1; subnet1); BitNot( subnet1 ) )

   & "." & BitOr( BitAnd( ip2; subnet2); BitNot( subnet2 ) )

   & "." & BitOr( BitAnd( ip3; subnet3); BitNot( subnet3 ) )

   & "." & BitOr( BitAnd( ip4; subnet4); BitNot( subnet4 ) )

Merging the parsing of an ip into 4 numbers into the function would probably make a better interface, but this should at least give you a start.

Link to comment
Share on other sites

Here is a custom function to calculate the network broadcast Address. It relies on three other bit-twiddling functions as provided by "The Shadow" (Who deserves a big box of chocolates as it's made my life Soooo much easier! Thank you !) The use is: broadcast(ip;netmask) and the result is the quad-dotted notation for the network broadcast address.

===========================================================================

Function: Broadcast(ip;netmask)

Code:

BitOr(

BitAnd(

Left(ip;Position(ip;".";1;1)-1)

;

Left(subnet;Position(subnet;".";1;1)-1)

)

;

BitNot(

Left(subnet;Position(subnet;".";1;1)-1)

)

)

& "." &

BitOr(

BitAnd(

(Middle(ip;Position(ip;".";1;1)+1;

Position(ip;".";1;2)-Position(ip;".";1;1)-1));

(Middle(subnet;Position(subnet;".";1;1)+1;

Position(subnet;".";1;2)-Position(subnet;".";1;1)-1))

)

;

BitNot(

(Middle(subnet;Position(subnet;".";1;1)+1;

Position(subnet;".";1;2)-Position(subnet;".";1;1)-1))

)

)

& "." &

BitOr(

BitAnd(

(Middle(ip;Position(ip;".";1;2)+1;

Position(ip;".";1;3)-Position(ip;".";1;2)-1));

(Middle(subnet;Position(subnet;".";1;2)+1;

Position(subnet;".";1;3)-Position(subnet;".";1;2)-1))

)

;

BitNot(

(Middle(subnet;Position(subnet;".";1;2)+1;

Position(subnet;".";1;3)-Position(subnet;".";1;2)-1))

)

)

& "." &

BitOr(

BitAnd(

(Middle(ip;Position(ip&".";".";1;3)+1;

Position(ip&".";".";1;4)-Position(ip&".";".";1;3)-1));

(Middle(subnet;Position(subnet&".";".";1;3)+1;

Position(subnet&".";".";1;4)-Position(subnet&".";".";1;3)-1))

)

;

BitNot(

(Middle(subnet;Position(subnet&".";".";1;3)+1;

Position(subnet&".";".";1;4)-Position(subnet&".";".";1;3)-1))

)

)

=======================================================================

Function: BitOR (x;y)

Code:

Case ( (x = 0); y;

(y = 0); x;

BitOr( Div(x;2); Div(y;2) ) * 2 + If ( Mod(x;2) =1 or Mod(y;2) = 1; 1; 0 )

===============================================================================

Function: BitAND(x;y)

Code:

If ( (x = 0) or (y = 0); 0;

BitAnd( Div(x;2); Div(y;2) ) * 2 + If ( Mod(x;2) =1 and Mod(y;2) = 1; 1; 0 )

)

==============================================================================

Function: BitNot(x)

Code:

If ( (x = 0); 255;

Mod( BitNot( Div(x;2) ) * 2; 256 ) + If ( Mod(x;2) =1; 0; 1 )

)

)

FileMaker Version: Dev 7

Platform: Windows XP

Link to comment
Share on other sites

The bit-twiddling is due to the guys on the cleveland consulting site, and there's more goodies there now:

http://www.clevelandconsulting.com/support/viewtopic.php?t=90

With this stuff, you can do your function like this:

Broadcast( ip, subnet ) =

Join( Map( "BitOr( BitAnd( $#1; $_2); BitNot( $#2 ) )"; Split( ip; "." ); Split( subnet; "." ) ); "." )

Yeah, that's it. Of course, the smaller it becomes the more difficult it is to understand - the complexity of the problem doesn't change, it is just spread over less lines. :^)

Link to comment
Share on other sites

  • 2 weeks later...

This topic is 6484 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.