class Rex::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
The mutex protecting the routes array.
The routes array.
Public Class Methods
Adds a route to the switch board routing table using the supplied Comm
instance.
# File lib/rex/socket/switch_board.rb, line 76 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
Enumerate each route in the routing table.
# File lib/rex/socket/switch_board.rb, line 106 def self.each(&block) self.instance.each(&block) end
Flush all the routes from the switch board routing table.
# File lib/rex/socket/switch_board.rb, line 99 def self.flush_routes ret = self.instance.flush_routes end
# File lib/rex/socket/switch_board.rb, line 25 def initialize @_initialized = false end
Removes all routes that go through the supplied Comm
.
# File lib/rex/socket/switch_board.rb, line 132 def self.remove_by_comm(comm) self.instance.remove_by_comm(comm) end
Removes a route from the switch board routing table for the supplied subnet routing through the supplied Comm
instance.
# File lib/rex/socket/switch_board.rb, line 88 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
# File lib/rex/socket/switch_board.rb, line 117 def self.route_exists?(subnet, mask) self.instance.route_exists?(subnet, mask) end
Returns the array of routes.
# File lib/rex/socket/switch_board.rb, line 113 def self.routes self.instance.routes end
Public Instance Methods
Adds a route for a given subnet and netmask destined through a given comm instance.
# File lib/rex/socket/switch_board.rb, line 146 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
Finds the best possible comm for the supplied target address.
# File lib/rex/socket/switch_board.rb, line 230 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
Enumerates each entry in the routing table.
# File lib/rex/socket/switch_board.rb, line 221 def each(&block) _init routes.each(&block) end
Flushes all established routes.
# File lib/rex/socket/switch_board.rb, line 192 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 all routes that go through the supplied comm.
# File lib/rex/socket/switch_board.rb, line 252 def remove_by_comm(comm) _init mutex.synchronize { routes.delete_if { |route| route.comm == comm } } end
Removes a route for a given subnet and netmask destined through a given comm instance.
# File lib/rex/socket/switch_board.rb, line 169 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
Checks to see if a route already exists for the supplied subnet and netmask.
# File lib/rex/socket/switch_board.rb, line 210 def route_exists?(subnet, netmask) each { |route| return true if (route.subnet == subnet and route.netmask == netmask) } false end
Protected Instance Methods
Initializes the underlying stuff.
# File lib/rex/socket/switch_board.rb, line 277 def _init if (@_initialized != true) @_initialized = true self.routes = Array.new self.mutex = Mutex.new end end