You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

netmask.coffee 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. long2ip = (long) ->
  2. a = (long & (0xff << 24)) >>> 24;
  3. b = (long & (0xff << 16)) >>> 16;
  4. c = (long & (0xff << 8)) >>> 8;
  5. d = long & 0xff;
  6. return [a, b, c, d].join('.')
  7. ip2long = (ip) ->
  8. b = (ip + '').split('.');
  9. if b.length is 0 or b.length > 4 then throw new Error('Invalid IP')
  10. for byte, i in b
  11. if isNaN parseInt(byte, 10) then throw new Error("Invalid byte: #{byte}")
  12. if byte < 0 or byte > 255 then throw new Error("Invalid byte: #{byte}")
  13. return ((b[0] or 0) << 24 | (b[1] or 0) << 16 | (b[2] or 0) << 8 | (b[3] or 0)) >>> 0
  14. class Netmask
  15. constructor: (net, mask) ->
  16. throw new Error("Missing `net' parameter") unless typeof net is 'string'
  17. unless mask
  18. # try to find the mask in the net (i.e.: 1.2.3.4/24 or 1.2.3.4/255.255.255.0)
  19. [net, mask] = net.split('/', 2)
  20. unless mask
  21. switch net.split('.').length
  22. when 1 then mask = 8
  23. when 2 then mask = 16
  24. when 3 then mask = 24
  25. when 4 then mask = 32
  26. else throw new Error("Invalid net address: #{net}")
  27. if typeof mask is 'string' and mask.indexOf('.') > -1
  28. # Compute bitmask, the netmask as a number of bits in the network portion of the address for this block (eg.: 24)
  29. try
  30. @maskLong = ip2long(mask)
  31. catch error
  32. throw new Error("Invalid mask: #{mask}")
  33. for i in [32..0]
  34. if @maskLong == (0xffffffff << (32 - i)) >>> 0
  35. @bitmask = i
  36. break
  37. else if mask
  38. # The mask was passed as bitmask, compute the mask as long from it
  39. @bitmask = parseInt(mask, 10)
  40. @maskLong = 0
  41. if @bitmask > 0
  42. @maskLong = (0xffffffff << (32 - @bitmask)) >>> 0
  43. else
  44. throw new Error("Invalid mask: empty")
  45. try
  46. @netLong = (ip2long(net) & @maskLong) >>> 0
  47. catch error
  48. throw new Error("Invalid net address: #{net}")
  49. throw new Error("Invalid mask for ip4: #{mask}") unless @bitmask <= 32
  50. # The number of IP address in the block (eg.: 254)
  51. @size = Math.pow(2, 32 - @bitmask)
  52. # The address of the network block as a string (eg.: 216.240.32.0)
  53. @base = long2ip(@netLong)
  54. # The netmask as a string (eg.: 255.255.255.0)
  55. @mask = long2ip(@maskLong)
  56. # The host mask, the opposite of the netmask (eg.: 0.0.0.255)
  57. @hostmask = long2ip(~@maskLong)
  58. # The first usable address of the block
  59. @first = if @bitmask <= 30 then long2ip(@netLong + 1) else @base
  60. # The last usable address of the block
  61. @last = if @bitmask <= 30 then long2ip(@netLong + @size - 2) else long2ip(@netLong + @size - 1)
  62. # The block's broadcast address: the last address of the block (eg.: 192.168.1.255)
  63. @broadcast = if @bitmask <= 30 then long2ip(@netLong + @size - 1)
  64. # Returns true if the given ip or netmask is contained in the block
  65. contains: (ip) ->
  66. if typeof ip is 'string' and (ip.indexOf('/') > 0 or ip.split('.').length isnt 4)
  67. ip = new Netmask(ip)
  68. if ip instanceof Netmask
  69. return @contains(ip.base) and @contains((ip.broadcast || ip.last))
  70. else
  71. return (ip2long(ip) & @maskLong) >>> 0 == ((@netLong & @maskLong)) >>> 0
  72. # Returns the Netmask object for the block which follow this one
  73. next: (count=1) ->
  74. return new Netmask(long2ip(@netLong + (@size * count)), @mask)
  75. forEach: (fn) ->
  76. range = [ip2long(@first)..ip2long(@last)]
  77. fn long2ip(long), long, index for long, index in range
  78. # Returns the complete netmask formatted as `base/bitmask`
  79. toString: ->
  80. return @base + "/" + @bitmask
  81. exports.ip2long = ip2long
  82. exports.long2ip = long2ip
  83. exports.Netmask = Netmask