Module: Racket::L3::Misc
- Defined in:
- lib/racket/l3/misc.rb
Overview
Miscelaneous L3 helper methods
Class Method Summary collapse
-
.checksum(data) ⇒ Object
Calculate the checksum.
-
.compressipv6(ipv6) ⇒ Object
Compress an IPv6 address Inspired by Daniele Bellucci and jacked from ipaddr.
-
.ipv42long(ip) ⇒ Object
given a “dotted quad” representing an IPv4 address, return the integer representation.
-
.ipv62long(ip) ⇒ Object
given a string representing an IPv6 address, return the integer representation.
-
.linklocaladdr(mac) ⇒ Object
Compute link local address for a given mac address From Daniele Bellucci.
-
.long2ipv4(long) ⇒ Object
given an IPv4 address packed as an integer return the friendly “dotted quad”.
-
.long2ipv6(long, compress = true) ⇒ Object
Given a long, convert it to an IPv6 address, optionally compressing the address returned.
- .randomipv4 ⇒ Object
- .randomipv6 ⇒ Object
-
.soll_mcast_addr6(addr) ⇒ Object
In addition to the regular multicast addresses, each unicast address has a special multicast address called its solicited-node address.
- .soll_mcast_mac(addr) ⇒ Object
Class Method Details
.checksum(data) ⇒ Object
Calculate the checksum. 16 bit one’s complement of the one’s complement sum of all 16 bit words
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/racket/l3/misc.rb', line 144 def Misc.checksum(data) num_shorts = data.length / 2 checksum = 0 count = data.length data.unpack("S#{num_shorts}").each { |x| checksum += x count -= 2 } if (count == 1) checksum += data[data.length - 1] end checksum = (checksum >> 16) + (checksum & 0xffff) checksum = ~((checksum >> 16) + checksum) & 0xffff ([checksum].pack("S*")).unpack("n*")[0] end |
.compressipv6(ipv6) ⇒ Object
Compress an IPv6 address Inspired by Daniele Bellucci and jacked from ipaddr
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/racket/l3/misc.rb', line 75 def Misc.compressipv6(ipv6) ipv6.gsub!(/\b0{1,3}([\da-f]+)\b/i, '\1') loop do break if ipv6.sub!(/\A0:0:0:0:0:0:0:0\Z/, '::') break if ipv6.sub!(/\b0:0:0:0:0:0:0\b/, ':') break if ipv6.sub!(/\b0:0:0:0:0:0\b/, ':') break if ipv6.sub!(/\b0:0:0:0:0\b/, ':') break if ipv6.sub!(/\b0:0:0:0\b/, ':') break if ipv6.sub!(/\b0:0:0\b/, ':') break if ipv6.sub!(/\b0:0\b/, ':') break end ipv6.sub!(/:{3,}/, '::') if /\A::(ffff:)?([\da-f]{1,4}):([\da-f]{1,4})\Z/i =~ ipv6 ipv6 = sprintf('::%s%d.%d.%d.%d', $1, $2.hex / 256, $2.hex % 256, $3.hex / 256, $3.hex % 256) end ipv6 end |
.ipv42long(ip) ⇒ Object
given a “dotted quad” representing an IPv4 address, return the integer representation
138 139 140 |
# File 'lib/racket/l3/misc.rb', line 138 def Misc.ipv42long(ip) IPAddr.new(ip).to_i end |
.ipv62long(ip) ⇒ Object
given a string representing an IPv6 address, return the integer representation
103 104 105 |
# File 'lib/racket/l3/misc.rb', line 103 def Misc.ipv62long(ip) IPAddr.new(ip).to_i end |
.linklocaladdr(mac) ⇒ Object
Compute link local address for a given mac address From Daniele Bellucci
50 51 52 53 54 |
# File 'lib/racket/l3/misc.rb', line 50 def Misc.linklocaladdr(mac) mac = mac.split(":") mac[0] = (mac[0].to_i(16) ^ (1 << 1)).to_s(16) ["fe80", "", mac[0,2].join, mac[2,2].join("ff:fe"), mac[4,2].join].join(":") end |
.long2ipv4(long) ⇒ Object
given an IPv4 address packed as an integer return the friendly “dotted quad”
35 36 37 38 39 40 41 42 |
# File 'lib/racket/l3/misc.rb', line 35 def Misc.long2ipv4(long) quad = Array.new(4) quad[0] = (long >> 24) & 255 quad[1] = (long >> 16) & 255 quad[2] = (long >> 8 ) & 255 quad[3] = long & 255 quad.join(".") end |
.long2ipv6(long, compress = true) ⇒ Object
Given a long, convert it to an IPv6 address, optionally compressing the address returned
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/racket/l3/misc.rb', line 58 def Misc.long2ipv6(long, compress=true) ipv6 = [] ipv6[0] = long >> 112 ipv6[1] = (long >> 96) & (0xFFFF) ipv6[2] = (long >> 80) & (0xFFFF) ipv6[3] = (long >> 64) & (0xFFFF) ipv6[4] = (long >> 48) & (0xFFFF) ipv6[5] = (long >> 32) & (0xFFFF) ipv6[6] = (long >> 16) & (0xFFFF) ipv6[7] = long & (0xFFFF) ipv6 = ipv6.map { |o| o.to_s(16) }.join(":") compress ? Misc.compressipv6(ipv6) : ipv6 end |
.randomipv4 ⇒ Object
44 45 46 |
# File 'lib/racket/l3/misc.rb', line 44 def Misc.randomipv4 Misc.long2ipv4(rand(2**32)) end |
.randomipv6 ⇒ Object
97 98 99 |
# File 'lib/racket/l3/misc.rb', line 97 def Misc.randomipv6 Misc.long2ipv6(rand(2**128)) end |
.soll_mcast_addr6(addr) ⇒ Object
In addition to the regular multicast addresses, each unicast address has a special multicast address called its solicited-node address. This address is created through a special mapping from the device’s unicast address. Solicited-node addresses are used by the IPv6 Neighbor Discovery (ND) protocol to provide more efficient address resolution than the ARP technique used in IPv4.
From Daniele Bellucci
114 115 116 117 118 119 120 121 122 |
# File 'lib/racket/l3/misc.rb', line 114 def Misc.soll_mcast_addr6(addr) h = addr.split(':')[-2, 2] m = [] m << 'ff' m << (h[0].to_i(16) & 0xff).to_s(16) m << ((h[1].to_i(16) & (0xff << 8)) >> 8).to_s(16) m << (h[1].to_i(16) & 0xff).to_s(16) 'ff02::1:' + [m[0,2].join, m[2,2].join].join(':') end |
.soll_mcast_mac(addr) ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'lib/racket/l3/misc.rb', line 125 def Misc.soll_mcast_mac(addr) h = addr.split(':')[-2, 2] m = [] m << 'ff' m << (h[0].to_i(16) & 0xff).to_s(16) m << ((h[1].to_i(16) & (0xff << 8)) >> 8).to_s(16) m << (h[1].to_i(16) & 0xff).to_s(16) '33:33:' + m.join(':') end |