Class: Ethernet::Frame
Constant Summary collapse
Instance Attribute Summary collapse
-
#dst ⇒ Object
readonly
Returns the value of attribute dst.
-
#ether_type ⇒ Object
readonly
Returns the value of attribute ether_type.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#src ⇒ Object
readonly
Returns the value of attribute src.
Instance Method Summary collapse
- #encode(ether_type = @ether_type, payload = nil) ⇒ Object
- #ethernet_v2? ⇒ Boolean
- #ieee_802_3? ⇒ Boolean
-
#initialize(arg = {}) ⇒ Frame
constructor
A new instance of Frame.
- #parse(s) ⇒ Object
- #type? ⇒ Boolean
Methods included from Args
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
#dst ⇒ Object (readonly)
Returns the value of attribute dst.
131 132 133 |
# File 'lib/ethernet.rb', line 131 def dst @dst end |
#ether_type ⇒ Object (readonly)
Returns the value of attribute ether_type.
131 132 133 |
# File 'lib/ethernet.rb', line 131 def ether_type @ether_type end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
131 132 133 |
# File 'lib/ethernet.rb', line 131 def payload @payload end |
#src ⇒ Object (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
170 171 172 |
# File 'lib/ethernet.rb', line 170 def ethernet_v2? type? == :ethernet_v2 end |
#ieee_802_3? ⇒ 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
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 |