It is currently Thu Mar 28, 2024 9:44 am


All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: M code: Calc network & broadcast addr's from IP/netmask
PostPosted: Thu Sep 15, 2011 11:42 am 
User avatar

Joined: Mon Nov 01, 2010 1:39 pm
Posts: 51
Real Name: John Willis
Began Programming in MUMPS: 01 Apr 2010
binary.m:

Code:
binary

toBinary(decimalNum)
 n i,quot,rem,last,cnt,bstr s quot="1",last=decimalNum,bstr=""
 f i=1:1 s quot=last\2,rem(i)=last#2,last=quot q:quot=0
 s cnt=i
 f i=cnt:-1:1 s bstr=bstr_rem(i)
 q bstr

toGTMBinary(binaryString)
 q $c(0)_binaryString

toDecimal(binaryString)
 n i,lastMul,len,char s lastMul=0
 f i=1:1:$l(binaryString) d
 . s char=$e(binaryString,i,i)
 . s lastMul=lastMul*2+char
 q lastMul

;
; hack to work around strange behavior of $ZBITNOT()
;
not(str)
 n i,chr,nstr s nstr=""
 s str=$p(str,$c(0),2)
 f i=1:1:$l(str) d
 . s chr=$e(str,i,i)
 . i chr="0" s nstr=nstr_"1"
 . i chr="1" s nstr=nstr_"0"
 q $c(0)_nstr


IP.m:

Code:
IP

toDecimal(addr)
 n mul,tgt,i,dv s i="",dv=0
 s mul(1)=256**3,mul(2)=256**2,mul(3)=256**1,mul(4)=256**0
 f i=1:1:4 d
 . s tgt(i)=$p(addr,".",i)
 . s dv=dv+(tgt(i)*mul(i))
 q dv

toString(decAddr)
 n mul,i,quotient,last,tgt,out
 s i="",quotient=0,last=decAddr,out=""
 s mul(1)=256**3,mul(2)=256**2,mul(3)=256**1,mul(4)=256**0
 f i=1:1:4 d
 . s quotient=last\mul(i)
 . s last=last#mul(i)
 . s tgt(i)=quotient
 f i=1:1:4 d
 . s out=out_tgt(i)_"."
 q $e(out,1,$l(out)-1)

broadcast(addr,netmask)
 n ipdec,maskdec,bcastdec,maskb,addrb,binbc
 s ipdec=$$toDecimal(addr)
 s maskdec=$$toDecimal(netmask)
 s addrb=$$toBinary^binary(ipdec),maskb=$$toBinary^binary(maskdec)
 s addrb=$$toGTMBinary^binary(addrb),maskb=$$toGTMBinary^binary(maskb)
 s invmask=$$not^binary(maskb)
 s binbc=$zbitor(addrb,invmask)
 s binbc=$p(binbc,$c(0),2)
 q $$toString($$toDecimal^binary(binbc))

network(addr,netmask)
 n ipdec,maskdec,bcastdec,maskb,addrb,binnet,invmask
 s ipdec=$$toDecimal(addr)
 s maskdec=$$toDecimal(netmask)
 s addrb=$$toBinary^binary(ipdec),maskb=$$toBinary^binary(maskdec)
 s addrb=$$toGTMBinary^binary(addrb),maskb=$$toGTMBinary^binary(maskb)
 s binnet=$zbitand(addrb,maskb)
 s binnet=$p(binnet,$c(0),2)
 q $$toString($$toDecimal^binary(binnet))


call $$broadcast^IP(IP_address,subnet_mask) to get the broadcast address.
call $$network^IP(IP_address,subnet_mask) to get the network address.

_________________
John Willis, Founder
Coherent Logic Development
http://youngmumpster.wordpress.com/
jwillis@coherent-logic.com


Top
Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net