class SSLScan::Socket::SwitchBoard

This class provides a global routing table that associates subnets with Comm classes. Comm classes are used to instantiate objects that are tied to remote network entities. For example, the Local Comm class is used to building network connections directly from the local machine whereas, for instance, a Meterpreter Comm would build a local socket pair that is associated with a connection established by a remote entity. This can be seen as a uniform way of communicating with hosts through arbitrary channels.

Attributes

mutex[R]

The mutex protecting the routes array.

routes[R]

The routes array.

Public Class Methods

add_route(subnet, mask, comm) click to toggle source

Adds a route to the switch board routing table using the supplied Comm instance.

# File lib/ssl_scan/socket/switch_board.rb, line 75
def self.add_route(subnet, mask, comm)
  ret = self.instance.add_route(subnet, mask, comm)
  if ret && comm.respond_to?(:routes) && comm.routes.kind_of?(Array)
    comm.routes << "#{subnet}/#{mask}"
  end
  ret
end
best_comm(addr) click to toggle source

Returns the Comm instance that should be used for the supplied address. If no comm can be found, the default Local Comm is returned.

# File lib/ssl_scan/socket/switch_board.rb, line 124
def self.best_comm(addr)
  self.instance.best_comm(addr)
end
each(&block) click to toggle source

Enumerate each route in the routing table.

# File lib/ssl_scan/socket/switch_board.rb, line 105
def self.each(&block)
  self.instance.each(&block)
end
flush_routes() click to toggle source

Flush all the routes from the switch board routing table.

# File lib/ssl_scan/socket/switch_board.rb, line 98
def self.flush_routes
  ret = self.instance.flush_routes
end
new() click to toggle source
# File lib/ssl_scan/socket/switch_board.rb, line 24
def initialize
  @_initialized = false
end
remove_by_comm(comm) click to toggle source

Removes all routes that go through the supplied Comm.

# File lib/ssl_scan/socket/switch_board.rb, line 131
def self.remove_by_comm(comm)
  self.instance.remove_by_comm(comm)
end
remove_route(subnet, mask, comm) click to toggle source

Removes a route from the switch board routing table for the supplied subnet routing through the supplied Comm instance.

# File lib/ssl_scan/socket/switch_board.rb, line 87
def self.remove_route(subnet, mask, comm)
  ret = self.instance.remove_route(subnet, mask, comm)
  if ret && comm.respond_to?(:routes) && comm.routes.kind_of?(Array)
    comm.routes.delete "#{subnet}/#{mask}"
  end
  ret
end
route_exists?(subnet, mask) click to toggle source
# File lib/ssl_scan/socket/switch_board.rb, line 116
def self.route_exists?(subnet, mask)
  self.instance.route_exists?(subnet, mask)
end
routes() click to toggle source

Returns the array of routes.

# File lib/ssl_scan/socket/switch_board.rb, line 112
def self.routes
  self.instance.routes
end

Public Instance Methods

add_route(subnet, mask, comm) click to toggle source

Adds a route for a given subnet and netmask destined through a given comm instance.

# File lib/ssl_scan/socket/switch_board.rb, line 145
def add_route(subnet, mask, comm)
  # If a bitmask was supplied, convert it.
  netmask = (mask.to_s =~ /^\d+$/) ? Rex::Socket.bit2netmask(mask.to_i) : mask
  rv      = true

  _init

  mutex.synchronize {
    # If the route already exists, return false to the caller.
    if (route_exists?(subnet, netmask) == false)
      self.routes << Route.new(subnet, netmask, comm)
    else
      rv = false
    end
  }

  rv
end
best_comm(addr) click to toggle source

Finds the best possible comm for the supplied target address.

# File lib/ssl_scan/socket/switch_board.rb, line 229
def best_comm(addr)

  addr_nbo = Socket.resolv_nbo_i(addr)
  comm     = nil
  msb      = 0

  each { |route|
    if ((route.subnet_nbo & route.netmask_nbo) ==
        (addr_nbo & route.netmask_nbo))
      if (route.bitmask >= msb)
        comm = route.comm
        msb  = route.bitmask
      end
    end
  }

  comm
end
each(&block) click to toggle source

Enumerates each entry in the routing table.

# File lib/ssl_scan/socket/switch_board.rb, line 220
def each(&block)
  _init

  routes.each(&block)
end
flush_routes() click to toggle source

Flushes all established routes.

# File lib/ssl_scan/socket/switch_board.rb, line 191
def flush_routes
  _init

  # Remove each of the individual routes so the comms don't think they're
  # still routing after a flush.
  self.routes.each { |r|
    if r.comm.respond_to? :routes
      r.comm.routes.delete("#{r.subnet}/#{r.netmask}")
    end
  }
  # Re-initialize to an empty array
  self.routes = Array.new
end
remove_by_comm(comm) click to toggle source

Remove all routes that go through the supplied comm.

# File lib/ssl_scan/socket/switch_board.rb, line 251
def remove_by_comm(comm)
  _init
  mutex.synchronize {
    routes.delete_if { |route|
      route.comm == comm
    }
  }
end
remove_route(subnet, mask, comm) click to toggle source

Removes a route for a given subnet and netmask destined through a given comm instance.

# File lib/ssl_scan/socket/switch_board.rb, line 168
def remove_route(subnet, mask, comm)
  # If a bitmask was supplied, convert it.
  netmask = (mask.to_s =~ /^\d+$/) ? Rex::Socket.bit2netmask(mask.to_i) : mask
  rv      = false

  _init

  mutex.synchronize {
    self.routes.delete_if { |route|
      if (route.subnet == subnet and route.netmask == netmask and route.comm == comm)
        rv = true
      else
        false
      end
    }
  }

  rv
end
route_exists?(subnet, netmask) click to toggle source

Checks to see if a route already exists for the supplied subnet and netmask.

# File lib/ssl_scan/socket/switch_board.rb, line 209
def route_exists?(subnet, netmask)
  each { |route|
    return true if (route.subnet == subnet and route.netmask == netmask)
  }

  false
end

Protected Instance Methods

_init() click to toggle source

Initializes the underlying stuff.

# File lib/ssl_scan/socket/switch_board.rb, line 276
def _init
  if (@_initialized != true)
    @_initialized = true
    self.routes   = Array.new
    self.mutex    = Mutex.new
  end
end