| Module | L3::Misc |
| In: |
lib/l3/misc.rb
(CVS)
|
Calculate the checksum. 16 bit one‘s complement of the one‘s complement sum of all 16 bit words
# File lib/l3/misc.rb, line 55 def Misc.checksum(data) num_shorts = data.length / 2 csum = 0 count = data.length data.unpack("S#{num_shorts}").each { |x| csum += x count -= 2 } if (count == 1) csum += data[data.length - 1] end csum = (csum >> 16) + (csum & 0xffff) csum = ~((csum >> 16) + csum) & 0xffff ([csum].pack("S*")).unpack("n*")[0] end
given a "dotted quad" representing an IPv4 address, return the integer representation
# File lib/l3/misc.rb, line 46 def Misc.ipv42long(ip) quad = ip.split(/\./) quad.collect! {|s| s.to_i} # XXX: replace this with an inject quad[3] + (256 * quad[2]) + ((256**2) * quad[1]) + ((256**3) * quad[0]) end