Class: Rex::Socket::Parameters

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/socket/parameters.rb

Overview

This class represents the set of parameters that are used to create a socket, whether it be a server or client socket.

Defined Under Namespace

Classes: UnitTest

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Parameters

Initializes the attributes from the supplied hash. The following hash keys can be specified.

PeerHost / PeerAddr

The remote host to connect to.

PeerPort

The remote port to connect to.

LocalHost / LocalAddr

The local host to communicate from, if any.

LocalPort

The local port to communicate from, if any.

Bare

Create a bare socket.

Server

Whether or not this should be a server.

SSL

Whether or not SSL should be used.

SSLVersion

Specify SSL2, SSL3, or TLS1 (SSL3 is default)

Proxies

List of proxies to use.

Proto

The underlying protocol to use.

IPv6

Force the use of IPv6.

Comm

The underlying Comm class to use to create the socket for this parameter set.

Context

A context hash that can allow users of this parameter class instance to determine who is responsible for requesting that a socket be created.

Retries

The number of times a connection should be retried.

Timeout

The number of seconds before a connection should time out



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/rex/socket/parameters.rb', line 97

def initialize(hash)
	if (hash['PeerHost'])
		self.peerhost = hash['PeerHost']
	elsif (hash['PeerAddr'])
		self.peerhost = hash['PeerAddr']
	else
		self.peerhost = nil
	end

	if (hash['LocalHost'])
		self.localhost = hash['LocalHost']
	elsif (hash['LocalAddr'])
		self.localhost = hash['LocalAddr']
	else
		self.localhost = '0.0.0.0'
	end

	if (hash['PeerPort'])
		self.peerport = hash['PeerPort'].to_i
	else
		self.peerport = 0
	end

	if (hash['LocalPort'])
		self.localport = hash['LocalPort'].to_i
	else
		self.localport = 0
	end

	if (hash['Bare'])
		self.bare = hash['Bare']
	else
		self.bare = false
	end

	if (hash['SSL'] and hash['SSL'].to_s =~ /^(t|y|1)/i)
		self.ssl = true
	else
		self.ssl = false
	end
	
	if (hash['SSLVersion'] and hash['SSLVersion'].to_s =~ /^(SSL2|SSL3|TLS1)$/i)
		self.ssl_version = hash['SSLVersion']
	end
	
	if hash['Proxies']	
		self.proxies = hash['Proxies'].split('-').map{|a| a.strip}.map{|a| a.split(':').map{|b| b.strip}}
	end
	
	# The protocol this socket will be using
	if (hash['Proto'])
		self.proto = hash['Proto'].downcase
	else
		self.proto = 'tcp'
	end

	# Whether or not the socket should be a server
	self.server    = hash['Server'] || false

	# The communication subsystem to use to create the socket
	self.comm      = hash['Comm']

	# The context that was passed in, if any.
	self.context   = hash['Context'] || {}
	
	# If no comm was supplied, try to use the comm that is best fit to
	# handle the provided host based on the current routing table.
	if( self.server )
		if (self.comm == nil and self.localhost)
			self.comm  = Rex::Socket::SwitchBoard.best_comm(self.localhost)
		end
	else
		if (self.comm == nil and self.peerhost)
			self.comm  = Rex::Socket::SwitchBoard.best_comm(self.peerhost)
		end
	end

	# If we still haven't found a comm, we default to the local comm.
	self.comm      = Rex::Socket::Comm::Local if (self.comm == nil)
	
	# If we are a UDP server, turn off the server flag as it was only set when 
	# creating the UDP socket in order to avail of the switch board above.
	if( self.server and self.proto == 'udp' )
		self.server = false
	end
	
	# The number of connection retries to make (client only)
	if hash['Retries']
		self.retries = hash['Retries'].to_i
	else
		self.retries = 0
	end

	# The number of seconds before a connect attempt times out (client only)
	if hash['Timeout']
		self.timeout = hash['Timeout'].to_i
	else
		self.timeout = 5
	end

	# Whether to force IPv6 addressing
	self.v6        = hash['IPv6'] || false
end

Instance Attribute Details

#bareObject

Whether or not this is a bare (non-extended) socket instance that should be created.



318
319
320
# File 'lib/rex/socket/parameters.rb', line 318

def bare
  @bare
end

#commObject

The Comm class that should be used to create the underlying socket.



301
302
303
# File 'lib/rex/socket/parameters.rb', line 301

def comm
  @comm
end

#contextObject

The context hash that was passed in to the structure.



305
306
307
# File 'lib/rex/socket/parameters.rb', line 305

def context
  @context
end

#localhostObject Also known as: localaddr

The local host. Equivalent to the LocalHost parameter hash key.



283
284
285
# File 'lib/rex/socket/parameters.rb', line 283

def localhost
  @localhost
end

#localportObject

The local port. Equivalent to the LocalPort parameter hash key.



287
288
289
# File 'lib/rex/socket/parameters.rb', line 287

def localport
  @localport
end

#peerhostObject Also known as: peeraddr

The remote host information, equivalent to the PeerHost parameter hash key.



275
276
277
# File 'lib/rex/socket/parameters.rb', line 275

def peerhost
  @peerhost
end

#peerportObject

The remote port. Equivalent to the PeerPort parameter hash key.



279
280
281
# File 'lib/rex/socket/parameters.rb', line 279

def peerport
  @peerport
end

#protoObject

The protocol to to use, such as TCP. Equivalent to the Proto parameter hash key.



292
293
294
# File 'lib/rex/socket/parameters.rb', line 292

def proto
  @proto
end

#proxiesObject

Returns the value of attribute proxies.



333
334
335
# File 'lib/rex/socket/parameters.rb', line 333

def proxies
  @proxies
end

#retriesObject

The number of attempts that should be made.



309
310
311
# File 'lib/rex/socket/parameters.rb', line 309

def retries
  @retries
end

#serverObject

Whether or not this is a server. Equivalent to the Server parameter hash key.



297
298
299
# File 'lib/rex/socket/parameters.rb', line 297

def server
  @server
end

#sslObject

Whether or not SSL should be used to wrap the connection.



322
323
324
# File 'lib/rex/socket/parameters.rb', line 322

def ssl
  @ssl
end

#ssl_versionObject

What version of SSL to use (SSL2, SSL3, TLS1)



326
327
328
# File 'lib/rex/socket/parameters.rb', line 326

def ssl_version
  @ssl_version
end

#timeoutObject

The number of seconds before a connection attempt should time out.



313
314
315
# File 'lib/rex/socket/parameters.rb', line 313

def timeout
  @timeout
end

#v6Object

Whether we should use IPv6



330
331
332
# File 'lib/rex/socket/parameters.rb', line 330

def v6
  @v6
end

Class Method Details

.from_hash(hash) ⇒ Object

Creates an instance of the Parameters class using the supplied hash.



20
21
22
# File 'lib/rex/socket/parameters.rb', line 20

def self.from_hash(hash)
	return self.new(hash)
end

Instance Method Details

#bare?Boolean

Returns true if the socket is a bare socket that does not inherit from any extended Rex classes.

Returns:

  • (Boolean)


246
247
248
# File 'lib/rex/socket/parameters.rb', line 246

def bare?
	return (bare == true)
end

#client?Boolean

Returns true if this represents parameters for a client.

Returns:

  • (Boolean)


217
218
219
# File 'lib/rex/socket/parameters.rb', line 217

def client?
	return (server == false)
end

#ip?Boolean

Returns true if the protocol for the parameters is IP.

Returns:

  • (Boolean)


238
239
240
# File 'lib/rex/socket/parameters.rb', line 238

def ip?
	return (proto == 'ip')
end

#server?Boolean

Returns true if this represents parameters for a server.

Returns:

  • (Boolean)


210
211
212
# File 'lib/rex/socket/parameters.rb', line 210

def server?
	return (server == true)
end

#ssl?Boolean

Returns true if SSL has been requested.

Returns:

  • (Boolean)


253
254
255
# File 'lib/rex/socket/parameters.rb', line 253

def ssl?
	return ssl
end

#tcp?Boolean

Returns true if the protocol for the parameters is TCP.

Returns:

  • (Boolean)


224
225
226
# File 'lib/rex/socket/parameters.rb', line 224

def tcp?
	return (proto == 'tcp')
end

#udp?Boolean

Returns true if the protocol for the parameters is UDP.

Returns:

  • (Boolean)


231
232
233
# File 'lib/rex/socket/parameters.rb', line 231

def udp?
	return (proto == 'udp')
end

#v6?Boolean

Returns true if IPv6 has been enabled

Returns:

  • (Boolean)


260
261
262
# File 'lib/rex/socket/parameters.rb', line 260

def v6?
	return v6
end