2005-12-17 06:46:23 +00:00
|
|
|
#!/usr/bin/env ruby
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-04-10 05:02:59 +00:00
|
|
|
module Rex
|
|
|
|
module Post
|
|
|
|
module Meterpreter
|
2005-04-09 20:45:09 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Constants
|
|
|
|
#
|
|
|
|
PACKET_TYPE_REQUEST = 0
|
|
|
|
PACKET_TYPE_RESPONSE = 1
|
|
|
|
PACKET_TYPE_PLAIN_REQUEST = 10
|
|
|
|
PACKET_TYPE_PLAIN_RESPONSE = 11
|
|
|
|
|
|
|
|
#
|
|
|
|
# TLV Meta Types
|
|
|
|
#
|
|
|
|
TLV_META_TYPE_NONE = 0
|
|
|
|
TLV_META_TYPE_STRING = (1 << 16)
|
|
|
|
TLV_META_TYPE_UINT = (1 << 17)
|
|
|
|
TLV_META_TYPE_RAW = (1 << 18)
|
|
|
|
TLV_META_TYPE_BOOL = (1 << 19)
|
|
|
|
TLV_META_TYPE_GROUP = (1 << 30)
|
|
|
|
TLV_META_TYPE_COMPLEX = (1 << 31)
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
#
|
|
|
|
# TLV base starting points
|
|
|
|
#
|
|
|
|
TLV_RESERVED = 0
|
|
|
|
TLV_EXTENSIONS = 20000
|
|
|
|
TLV_USER = 40000
|
|
|
|
TLV_TEMP = 60000
|
|
|
|
|
2005-04-09 20:45:09 +00:00
|
|
|
#
|
|
|
|
# TLV Specific Types
|
|
|
|
#
|
2005-04-10 05:02:59 +00:00
|
|
|
TLV_TYPE_ANY = TLV_META_TYPE_NONE | 0
|
|
|
|
TLV_TYPE_METHOD = TLV_META_TYPE_STRING | 1
|
|
|
|
TLV_TYPE_REQUEST_ID = TLV_META_TYPE_STRING | 2
|
|
|
|
TLV_TYPE_EXCEPTION = TLV_META_TYPE_GROUP | 3
|
|
|
|
TLV_TYPE_RESULT = TLV_META_TYPE_UINT | 4
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-04-10 05:02:59 +00:00
|
|
|
TLV_TYPE_STRING = TLV_META_TYPE_STRING | 10
|
|
|
|
TLV_TYPE_UINT = TLV_META_TYPE_UINT | 11
|
|
|
|
TLV_TYPE_BOOL = TLV_META_TYPE_BOOL | 12
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-04-10 05:02:59 +00:00
|
|
|
TLV_TYPE_LENGTH = TLV_META_TYPE_UINT | 25
|
|
|
|
TLV_TYPE_DATA = TLV_META_TYPE_RAW | 26
|
|
|
|
TLV_TYPE_FLAGS = TLV_META_TYPE_UINT | 27
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-04-10 05:02:59 +00:00
|
|
|
TLV_TYPE_CHANNEL_ID = TLV_META_TYPE_UINT | 50
|
|
|
|
TLV_TYPE_CHANNEL_TYPE = TLV_META_TYPE_STRING | 51
|
|
|
|
TLV_TYPE_CHANNEL_DATA = TLV_META_TYPE_RAW | 52
|
|
|
|
TLV_TYPE_CHANNEL_DATA_GROUP = TLV_META_TYPE_GROUP | 53
|
2005-04-12 05:37:11 +00:00
|
|
|
TLV_TYPE_CHANNEL_CLASS = TLV_META_TYPE_UINT | 54
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-04-13 07:31:11 +00:00
|
|
|
TLV_TYPE_SEEK_WHENCE = TLV_META_TYPE_UINT | 70
|
|
|
|
TLV_TYPE_SEEK_OFFSET = TLV_META_TYPE_UINT | 71
|
|
|
|
TLV_TYPE_SEEK_POS = TLV_META_TYPE_UINT | 72
|
|
|
|
|
2005-04-10 05:02:59 +00:00
|
|
|
TLV_TYPE_EXCEPTION_CODE = TLV_META_TYPE_UINT | 300
|
|
|
|
TLV_TYPE_EXCEPTION_STRING = TLV_META_TYPE_STRING | 301
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-04-10 05:02:59 +00:00
|
|
|
TLV_TYPE_LIBRARY_PATH = TLV_META_TYPE_STRING | 400
|
|
|
|
TLV_TYPE_TARGET_PATH = TLV_META_TYPE_STRING | 401
|
2005-04-18 08:29:30 +00:00
|
|
|
TLV_TYPE_MIGRATE_PID = TLV_META_TYPE_UINT | 402
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-04-10 05:02:59 +00:00
|
|
|
TLV_TYPE_CIPHER_NAME = TLV_META_TYPE_STRING | 500
|
|
|
|
TLV_TYPE_CIPHER_PARAMETERS = TLV_META_TYPE_GROUP | 501
|
2005-04-09 20:45:09 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Core flags
|
|
|
|
#
|
2005-04-10 04:53:53 +00:00
|
|
|
LOAD_LIBRARY_FLAG_ON_DISK = (1 << 0)
|
|
|
|
LOAD_LIBRARY_FLAG_EXTENSION = (1 << 1)
|
|
|
|
LOAD_LIBRARY_FLAG_LOCAL = (1 << 2)
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-04-10 00:39:39 +00:00
|
|
|
###
|
2005-04-09 20:45:09 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Base TLV (Type-Length-Value) class
|
2005-04-09 20:45:09 +00:00
|
|
|
#
|
2005-04-10 00:39:39 +00:00
|
|
|
###
|
2005-04-09 20:45:09 +00:00
|
|
|
class Tlv
|
|
|
|
attr_accessor :type, :value
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Constructor
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Returns an instance of a TLV.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def initialize(type, value = nil)
|
|
|
|
@type = type
|
2005-04-16 07:29:06 +00:00
|
|
|
|
|
|
|
if (value != nil)
|
|
|
|
if (type & TLV_META_TYPE_STRING == TLV_META_TYPE_STRING)
|
2005-04-21 06:32:01 +00:00
|
|
|
if (value.kind_of?(Fixnum))
|
|
|
|
@value = value.to_s
|
|
|
|
else
|
|
|
|
@value = value.dup
|
|
|
|
end
|
2005-04-16 07:29:06 +00:00
|
|
|
else
|
|
|
|
@value = value
|
|
|
|
end
|
|
|
|
end
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-09 20:45:09 +00:00
|
|
|
#
|
|
|
|
# Conditionals
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Checks to see if a TLVs meta type is equivalent to the meta type passed.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def meta_type?(meta)
|
2005-04-09 20:57:59 +00:00
|
|
|
return (self.type & meta == meta)
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Checks to see if the TLVs type is equivalent to the type passed.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def type?(type)
|
|
|
|
return self.type == type
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Checks to see if the TLVs value is equivalent to the value passed.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def value?(value)
|
|
|
|
return self.value == value
|
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-09 20:45:09 +00:00
|
|
|
#
|
|
|
|
# Serializers
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Converts the TLV to raw.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def to_r
|
2005-04-09 20:57:59 +00:00
|
|
|
raw = value.to_s;
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-04-09 20:57:59 +00:00
|
|
|
if (self.type & TLV_META_TYPE_STRING == TLV_META_TYPE_STRING)
|
2005-07-10 06:32:13 +00:00
|
|
|
raw += "\x00"
|
2005-04-09 20:57:59 +00:00
|
|
|
elsif (self.type & TLV_META_TYPE_UINT == TLV_META_TYPE_UINT)
|
|
|
|
raw = [value].pack("N")
|
|
|
|
elsif (self.type & TLV_META_TYPE_BOOL == TLV_META_TYPE_BOOL)
|
2005-04-12 06:39:33 +00:00
|
|
|
if (value == true)
|
|
|
|
raw = [1].pack("c")
|
|
|
|
else
|
|
|
|
raw = [0].pack("c")
|
|
|
|
end
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
|
2005-04-09 21:36:54 +00:00
|
|
|
return [raw.length + 8, self.type].pack("NN") + raw
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Translates the raw format of the TLV into a sanitize version.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def from_r(raw)
|
2005-04-09 21:36:54 +00:00
|
|
|
self.value = nil
|
|
|
|
|
2005-04-10 00:39:39 +00:00
|
|
|
length, self.type = raw.unpack("NN");
|
2005-04-09 21:36:54 +00:00
|
|
|
|
|
|
|
if (self.type & TLV_META_TYPE_STRING == TLV_META_TYPE_STRING)
|
|
|
|
if (raw.length > 0)
|
2005-04-15 07:03:33 +00:00
|
|
|
self.value = raw[8..length-2]
|
2005-04-09 21:36:54 +00:00
|
|
|
else
|
|
|
|
self.value = nil
|
|
|
|
end
|
|
|
|
elsif (self.type & TLV_META_TYPE_UINT == TLV_META_TYPE_UINT)
|
|
|
|
self.value = raw.unpack("NNN")[2]
|
|
|
|
elsif (self.type & TLV_META_TYPE_BOOL == TLV_META_TYPE_BOOL)
|
|
|
|
self.value = raw.unpack("NNc")[2]
|
2005-04-12 06:39:33 +00:00
|
|
|
|
|
|
|
if (self.value == 1)
|
|
|
|
self.value = true
|
|
|
|
else
|
|
|
|
self.value = false
|
|
|
|
end
|
2005-04-11 01:59:45 +00:00
|
|
|
else
|
2005-04-15 06:23:59 +00:00
|
|
|
self.value = raw[8..length-1]
|
2005-04-09 21:36:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return length;
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-04-10 00:39:39 +00:00
|
|
|
###
|
2005-04-09 20:45:09 +00:00
|
|
|
#
|
|
|
|
# Group TLVs contain zero or more TLVs
|
|
|
|
#
|
2005-04-10 00:39:39 +00:00
|
|
|
###
|
2005-04-09 20:45:09 +00:00
|
|
|
class GroupTlv < Tlv
|
|
|
|
attr_accessor :tlvs
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Constructor
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Initializes the group TLV container to the supplied type
|
2005-11-15 05:22:13 +00:00
|
|
|
# and creates an empty TLV array.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def initialize(type)
|
|
|
|
super(type)
|
|
|
|
|
|
|
|
self.tlvs = [ ]
|
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-09 20:45:09 +00:00
|
|
|
#
|
|
|
|
# Group-based TLV accessors
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Enumerates TLVs of the supplied type.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def each(type = TLV_TYPE_ANY, &block)
|
|
|
|
get_tlvs(type).each(&block)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Synonym for each.
|
|
|
|
#
|
2005-04-10 01:17:19 +00:00
|
|
|
def each_tlv(type = TLV_TYPE_ANY, &block)
|
|
|
|
each(type, block)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Enumerates TLVs of a supplied type with indexes.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def each_with_index(type = TLV_TYPE_ANY, &block)
|
|
|
|
get_tlvs(type).each_with_index(&block)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Synonym for each_with_index.
|
|
|
|
#
|
2005-04-10 01:17:19 +00:00
|
|
|
def each_tlv_with_index(type = TLV_TYPE_ANY, &block)
|
|
|
|
each_with_index(type, block)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Returns an array of TLVs for the given type.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def get_tlvs(type)
|
|
|
|
if (type == TLV_TYPE_ANY)
|
|
|
|
return self.tlvs
|
|
|
|
else
|
|
|
|
type_tlvs = []
|
|
|
|
|
|
|
|
self.tlvs.each() { |tlv|
|
|
|
|
if (tlv.type?(type))
|
|
|
|
type_tlvs << tlv
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
return type_tlvs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-09 20:45:09 +00:00
|
|
|
#
|
|
|
|
# TLV management
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Adds a TLV of a given type and value.
|
|
|
|
#
|
2005-04-10 00:39:39 +00:00
|
|
|
def add_tlv(type, value = nil, replace = false)
|
2005-04-09 20:57:59 +00:00
|
|
|
tlv = nil
|
|
|
|
|
2005-04-10 00:39:39 +00:00
|
|
|
# If we should replace any TLVs with the same type...remove them first
|
|
|
|
if (replace)
|
|
|
|
each(type) { |tlv|
|
|
|
|
if (tlv.type == type)
|
|
|
|
self.tlvs.delete(tlv)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-04-09 20:57:59 +00:00
|
|
|
if (type & TLV_META_TYPE_GROUP == TLV_META_TYPE_GROUP)
|
|
|
|
tlv = GroupTlv.new(type)
|
|
|
|
else
|
|
|
|
tlv = Tlv.new(type, value)
|
|
|
|
end
|
2005-04-09 20:45:09 +00:00
|
|
|
|
|
|
|
self.tlvs << tlv
|
|
|
|
|
|
|
|
return tlv
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Adds zero or more TLVs to the packet.
|
|
|
|
#
|
2005-04-11 04:56:24 +00:00
|
|
|
def add_tlvs(tlvs)
|
|
|
|
if (tlvs != nil)
|
|
|
|
tlvs.each { |tlv|
|
|
|
|
add_tlv(tlv['type'], tlv['value'])
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Gets the first TLV of a given type.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def get_tlv(type, index = 0)
|
|
|
|
type_tlvs = get_tlvs(type)
|
|
|
|
|
|
|
|
if (type_tlvs.length > index)
|
|
|
|
return type_tlvs[index]
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Returns the value of a TLV if it exists, otherwise nil.
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
def get_tlv_value(type, index = 0)
|
|
|
|
tlv = get_tlv(type, index)
|
|
|
|
|
|
|
|
return (tlv != nil) ? tlv.value : nil
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-06-14 04:04:20 +00:00
|
|
|
# Returns an array of values for all tlvs of type type.
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-06-14 04:04:20 +00:00
|
|
|
def get_tlv_values(type)
|
|
|
|
get_tlvs(type).collect { |a| a.value }
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Checks to see if the container has a TLV of a given type.
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
def has_tlv?(type)
|
|
|
|
return get_tlv(type) != nil
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Zeros out the array of TLVs.
|
|
|
|
#
|
2005-04-09 21:44:01 +00:00
|
|
|
def reset
|
|
|
|
self.tlvs = []
|
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-09 20:45:09 +00:00
|
|
|
#
|
|
|
|
# Serializers
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Converts all of the TLVs in the TLV array to raw and prefixes them
|
2005-11-15 05:22:13 +00:00
|
|
|
# with a container TLV of this instance's TLV type.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def to_r
|
|
|
|
raw = ''
|
|
|
|
|
|
|
|
self.each() { |tlv|
|
|
|
|
raw << tlv.to_r
|
|
|
|
}
|
|
|
|
|
2005-04-10 00:39:39 +00:00
|
|
|
return [raw.length + 8, self.type].pack("NN") + raw
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Converts the TLV group container from raw to all of the individual
|
2005-11-15 05:22:13 +00:00
|
|
|
# TLVs.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def from_r(raw)
|
2005-04-09 21:36:54 +00:00
|
|
|
offset = 8
|
|
|
|
|
|
|
|
# Reset the TLVs array
|
|
|
|
self.tlvs = []
|
2005-04-10 04:35:15 +00:00
|
|
|
self.type = raw.unpack("NN")[1]
|
2005-04-09 21:36:54 +00:00
|
|
|
|
|
|
|
# Enumerate all of the TLVs
|
2005-04-15 07:03:33 +00:00
|
|
|
while (offset < raw.length-1)
|
|
|
|
|
|
|
|
tlv = nil
|
2005-04-09 21:36:54 +00:00
|
|
|
|
|
|
|
# Get the length and type
|
|
|
|
length, type = raw[offset..offset+8].unpack("NN")
|
|
|
|
|
2005-04-15 07:03:33 +00:00
|
|
|
if (type & TLV_META_TYPE_GROUP == TLV_META_TYPE_GROUP)
|
|
|
|
tlv = GroupTlv.new(type)
|
|
|
|
else
|
|
|
|
tlv = Tlv.new(type)
|
|
|
|
end
|
2005-04-09 21:36:54 +00:00
|
|
|
|
|
|
|
tlv.from_r(raw[offset..offset+length])
|
|
|
|
|
|
|
|
# Insert it into the list of TLVs
|
|
|
|
tlvs << tlv
|
|
|
|
|
|
|
|
# Move up
|
|
|
|
offset += length
|
|
|
|
end
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2005-04-10 00:39:39 +00:00
|
|
|
###
|
2005-04-09 20:45:09 +00:00
|
|
|
#
|
|
|
|
# The logical meterpreter packet class
|
|
|
|
#
|
2005-04-10 00:39:39 +00:00
|
|
|
###
|
2005-04-09 20:45:09 +00:00
|
|
|
class Packet < GroupTlv
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-09 20:45:09 +00:00
|
|
|
#
|
|
|
|
# Factory
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-09 21:44:01 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Creates a request with the supplied method.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def Packet.create_request(method = nil)
|
|
|
|
return Packet.new(PACKET_TYPE_REQUEST, method)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Creates a response to a request if one is provided.
|
|
|
|
#
|
2005-04-09 21:44:01 +00:00
|
|
|
def Packet.create_response(request = nil)
|
2005-04-09 20:45:09 +00:00
|
|
|
response_type = PACKET_TYPE_RESPONSE
|
2005-04-09 21:44:01 +00:00
|
|
|
method = nil
|
|
|
|
|
|
|
|
if (request)
|
2005-04-10 16:21:53 +00:00
|
|
|
if (request.type?(PACKET_TYPE_PLAIN_REQUEST))
|
2005-04-09 21:44:01 +00:00
|
|
|
response_type = PACKET_TYPE_PLAIN_RESPONSE
|
|
|
|
end
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-04-09 21:44:01 +00:00
|
|
|
method = request.method
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
|
2005-04-09 21:44:01 +00:00
|
|
|
return Packet.new(response_type, method)
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-09 20:45:09 +00:00
|
|
|
#
|
|
|
|
# Constructor
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-09 20:45:09 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Initializes the packet to the supplied packet type and method,
|
|
|
|
# if any. If the packet is a request, a request identifier is
|
2005-11-15 05:22:13 +00:00
|
|
|
# created.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def initialize(type = nil, method = nil)
|
|
|
|
super(type)
|
|
|
|
|
|
|
|
if (method)
|
|
|
|
self.method = method
|
|
|
|
end
|
|
|
|
|
|
|
|
# If it's a request, generate a random request identifier
|
|
|
|
if ((type == PACKET_TYPE_REQUEST) ||
|
2005-04-10 00:39:39 +00:00
|
|
|
(type == PACKET_TYPE_PLAINTEXT_REQUEST))
|
2005-04-09 20:45:09 +00:00
|
|
|
rid = ''
|
|
|
|
|
2005-06-05 18:47:37 +00:00
|
|
|
32.times { |val| rid << rand(10).to_s }
|
2005-04-09 20:45:09 +00:00
|
|
|
|
|
|
|
add_tlv(TLV_TYPE_REQUEST_ID, rid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-10 02:05:43 +00:00
|
|
|
#
|
|
|
|
# Conditionals
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-10 02:05:43 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Checks to see if the packet is a response.
|
|
|
|
#
|
2005-04-10 02:05:43 +00:00
|
|
|
def response?
|
|
|
|
return ((self.type == PACKET_TYPE_RESPONSE) ||
|
|
|
|
(self.type == PACKET_TYPE_PLAIN_RESPONSE))
|
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-10 02:05:43 +00:00
|
|
|
#
|
|
|
|
# Accessors
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-10 02:05:43 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Checks to see if the packet's method is equal to the supplied method.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def method?(method)
|
2005-04-12 05:37:11 +00:00
|
|
|
return (get_tlv_value(TLV_TYPE_METHOD) == method)
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Sets the packet's method TLV to the method supplied.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def method=(method)
|
2005-04-10 00:39:39 +00:00
|
|
|
add_tlv(TLV_TYPE_METHOD, method, true)
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Returns the value of the packet's method TLV.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def method
|
2005-04-12 05:37:11 +00:00
|
|
|
return get_tlv_value(TLV_TYPE_METHOD)
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Checks to see if the packet's result value is equal to the supplied
|
2005-11-15 05:22:13 +00:00
|
|
|
# result.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def result?(result)
|
2005-04-12 05:37:11 +00:00
|
|
|
return (get_tlv_value(TLV_TYPE_RESULT) == result)
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Sets the packet's result TLV.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def result=(result)
|
2005-04-10 00:39:39 +00:00
|
|
|
add_tlv(TLV_TYPE_RESULT, result, true)
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Gets the value of the packet's result TLV.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def result
|
2005-04-12 05:37:11 +00:00
|
|
|
return get_tlv_value(TLV_TYPE_RESULT)
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Gets the value of the packet's request identifier TLV.
|
|
|
|
#
|
2005-04-09 20:45:09 +00:00
|
|
|
def rid
|
2005-04-12 05:37:11 +00:00
|
|
|
return get_tlv_value(TLV_TYPE_REQUEST_ID)
|
2005-04-09 20:45:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end; end; end
|