Class: BetterCap::Network::Protos::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bettercap/network/protos/base.rb

Constant Summary collapse

TYPES =
[
    :uint8,
    :uint16,
    :uint24,
    :uint32,
    :uint32rev,
    :ip,
    :mac,
    :bytes,
    :string,
    :stringz
].freeze

Class Method Summary collapse

Class Method Details

.method_missing(method_name, *arguments, &block) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bettercap/network/protos/base.rb', line 32

def self.method_missing(method_name, *arguments, &block)
  type = method_name.to_sym
  name = arguments[0]
  if TYPES.include?(type)
    unless self.class_variables.include?(:@@fields)
      class_eval "@@fields = {}"
    end

    class_eval "@@fields[:#{name}] = { :type => :#{type}, :opts => #{arguments.length > 1 ? arguments[1] : {}} }"
    class_eval "attr_accessor :#{name}"
  else
    raise NoMethodError, method_name
  end
end

.offset(info, pkt, default) ⇒ Object


147
148
149
150
151
152
153
154
155
156
157
# File 'lib/bettercap/network/protos/base.rb', line 147

def self.offset( info, pkt, default )
  if info[:opts].has_key?(:offset)
    if info[:opts][:offset].is_a?(Integer)
      return info[:opts][:offset]
    else
      return default + pkt.send( info[:opts][:offset] )
    end
  else
    return default
  end
end

.parse(data) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
# File 'lib/bettercap/network/protos/base.rb', line 47

def self.parse( data )
  pkt = self.new

  begin
    offset = 0
    limit  = data.length
    value  = nil

    self.class_variable_get(:@@fields).each do |name, info|
      value = nil

      case info[:type]
      when :uint8
        value = data[offset].ord
        offset += 1

      when :uint16
        value = data[offset..offset + 1].unpack('S')[0]
        offset += 2

      when :uint24
        value = data[offset..offset + 2].unpack('S')[0]
        offset += 3

      when :uint32
        value = data[offset..offset + 3].unpack('L')[0]
        offset += 4

      when :uint32rev
        value = data[offset..offset + 3].reverse.unpack('L')[0]
        offset += 4

      when :ip
        tmp   = data[offset..offset + 3].reverse.unpack('L')[0]
        value = IPAddr.new(tmp, Socket::AF_INET)
        offset += 4

      when :mac
        tmp   = data[offset..offset + 7]
        value = tmp.bytes.map(&(Proc.new {|x| sprintf('%02X',x) })).join(':')
        offset += size( info, pkt, 16 )

      when :bytes
        size = size( info, pkt, data.length )
        offset = offset( info, pkt, offset  )

        value = data[offset..offset + size - 1].bytes
        offset += size

      when :string
        size = size( info, pkt, data.length )
        offset = offset( info, pkt, offset  )

        value = data[offset..offset + size - 1].bytes.pack('c*')
        offset += size

      when :stringz
        value = ""
        loop do
          value += data[offset]
          offset += 1
          break if data[offset].ord == 0x00
        end
        offset += 1

      end

      if info[:opts].has_key?(:check)
        check = info[:opts][:check]
        check = check.force_encoding('ASCII-8BIT') if check.respond_to? :force_encoding
        if value != check
          raise "Unexpected value '#{value}', expected '#{check}' ."
        end
      end

      pkt.send("#{name}=", value)
    end

  rescue Exception => e
    #puts e.message
    #puts e.backtrace.join("\n")
    pkt = nil
  end

  pkt
end

.size(info, pkt, default) ⇒ Object


134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/bettercap/network/protos/base.rb', line 134

def self.size( info, pkt, default )
  if info[:opts].has_key?(:size)
    if info[:opts][:size].is_a?(Integer)
      return info[:opts][:size]
    else
      n = pkt.send( info[:opts][:size] )
      return n
    end
  else
    return default
  end
end