Class: Ethernet::Frame

Inherits:
Object show all
Includes:
Args
Defined in:
lib/ethernet.rb

Constant Summary collapse

Src =
Class.new(Address)
Dst =
Class.new(Address)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Args

#ivars, #set, #to_hash

Constructor Details

#initialize(arg = {}) ⇒ Frame

Returns a new instance of Frame.



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ethernet.rb', line 133

def initialize(arg={})
  if arg.is_a?(Hash)
    @src = Src.new
    @dst = Dst.new
    @ether_type=0
    set(arg)
  elsif arg.is_a?(String)
    parse arg
  else
    raise ArgumentError, "Invalid argument :#{arg.inspect}"
  end
end

Instance Attribute Details

#dstObject (readonly)

Returns the value of attribute dst.



131
132
133
# File 'lib/ethernet.rb', line 131

def dst
  @dst
end

#ether_typeObject (readonly)

Returns the value of attribute ether_type.



131
132
133
# File 'lib/ethernet.rb', line 131

def ether_type
  @ether_type
end

#payloadObject (readonly)

Returns the value of attribute payload.



131
132
133
# File 'lib/ethernet.rb', line 131

def payload
  @payload
end

#srcObject (readonly)

Returns the value of attribute src.



131
132
133
# File 'lib/ethernet.rb', line 131

def src
  @src
end

Instance Method Details

#encode(ether_type = @ether_type, payload = nil) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ethernet.rb', line 145

def encode(ether_type=@ether_type, payload=nil)
  @ether_type = ether_type if ether_type
  @payload = payload if payload
  frame = []
  frame << @dst.encode
  frame << @src.encode
  frame << [ether_type].pack('n')
  if @payload
    if @payload.respond_to?(:encode)
      frame << @payload.encode if @payload.respond_to?(:encode)
    else
      frame << @payload
    end
  end
  frame.join
end

#ethernet_v2?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/ethernet.rb', line 170

def ethernet_v2?
  type? == :ethernet_v2
end

#ieee_802_3?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/ethernet.rb', line 167

def ieee_802_3?
  type? == :ieee_802_3
end

#parse(s) ⇒ Object



161
162
163
164
165
166
# File 'lib/ethernet.rb', line 161

def parse(s)
  self.dst = s.slice!(0,6)
  self.src = s.slice!(0,6)
  @ether_type = s.slice!(0,2).unpack('n')[0]
  @payload = s
end

#type?Boolean

Returns:

  • (Boolean)


173
174
175
176
177
178
179
# File 'lib/ethernet.rb', line 173

def type?
  if @ether_type < 0x600
    :ieee_802_3
  else
    :ethernet_v2
  end
end