NL IP Address Calculator?

For the Compleat Fan
Locked
ax0n
Posts: 19
Joined: Thu Feb 01, 2007 3:03 am

NL IP Address Calculator?

Post by ax0n »

I'm having some trouble figuring out some stuff.

The first thing is netmask. If I have 192.168.0.49/24, I'm tempted to try this to turn /24 into a network mask:

(link posted to log because I can't get the code to show up right)

http://stuff.h-i-r.net/nl2.txt


How do I limit hex to only 32 bits (i.e. 0xffffff00 instead of 0xffffffff00) ???

Also, do you know of a good way to take a 32-bit number and make it into an IP address in dotted decimal notation?

T.I.A.

--ax0n
http://www.h-i-r.net/

Elica
Posts: 57
Joined: Wed Feb 13, 2008 6:41 pm

Re: NL IP Address Calculator?

Post by Elica »

ax0n wrote: How do I limit hex to only 32 bits (i.e. 0xffffff00 instead of 0xffffffff00) ???
If hex is a number, truncate (clear) all bits above 32-nd.

Code: Select all

(set 'hex 0xffeeddccbbaa)
(format "%x" (& hex 0xffffffff))
ax0n wrote:Also, do you know of a good way to take a 32-bit number and make it into an IP address in dotted decimal notation?
Not experienced in newLisp, but this should do the job:

Code: Select all

(set 'addr 0xffaa0410)

(string
  (mod (/ addr 0x1000000) 0x100) "."
  (mod (/ addr 0x10000) 0x100) "."
  (mod (/ addr 0x100) 0x100) "."
  (mod addr 0x100)
)

Elica
Posts: 57
Joined: Wed Feb 13, 2008 6:41 pm

Post by Elica »

And here is a lispish variant that takes and prints only the last 4 bytes:

Code: Select all

(define (ip4 addr (level 4)) 
  (if (> level 1) 
    (string
       (ip4 (/ addr 256) (- level 1))
       "." (mod addr 256)) 
    (mod addr 256) 
  ) 
)


(ip4 0xff01a00210)

"1.160.2.16"

ax0n
Posts: 19
Joined: Thu Feb 01, 2007 3:03 am

Post by ax0n »

Awesome, I didn't even think to AND it against 0xffffffff even though as part of this calculator I will have to AND the IP against the netmask to come up with the network address, so I'm not sure why that logic did 't come to me earlier.

That helps leaps and bounds.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

... here is yet another way to convert from IP-no to string:

Code: Select all

(set 'addr 0xffaa0410)

(format "%d.%d.%d.%d" (unpack "bbbb" (pack ">lu" addr))) 

=> "255.170.4.16"
be aware of correct byte-order you may have to change the ">lu" to a "<lu" on big endian CPUs

Elica
Posts: 57
Joined: Wed Feb 13, 2008 6:41 pm

Post by Elica »

ImageImageImage
Vir sapit qui pauca loquitur

ax0n
Posts: 19
Joined: Thu Feb 01, 2007 3:03 am

Post by ax0n »

I posted the following article. Thanks for your help, guys!

http://www.h-i-r.net/2008/03/ip-subnett ... wlisp.html

Locked