Method: Rex::Socket.cidr_crack

Defined in:
lib/rex/socket.rb

.cidr_crack(cidr, v6 = false) ⇒ Object

Converts a CIDR subnet into an array (base, bcast)



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/rex/socket.rb', line 442

def self.cidr_crack(cidr, v6=false)
  tmp = cidr.split('/')

  tst,scope = tmp[0].split("%",2)
  scope     = "%" + scope if scope
  scope   ||= ""

  addr = addr_atoi(tst)

  bits = 32
  mask = 0
  use6 = false

  if (addr > 0xffffffff or v6 or cidr =~ /:/)
    use6 = true
    bits = 128
  end

  mask = (2 ** bits) - (2 ** (bits - tmp[1].to_i))
  base = addr & mask

  stop = base + (2 ** (bits - tmp[1].to_i)) - 1
  return [self.addr_itoa(base, use6) + scope, self.addr_itoa(stop, use6) + scope]
end