Skip to content

Commit

Permalink
Rename Arp::Request#to_hex -> Arp::Request#to_ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Jul 6, 2016
1 parent d39a712 commit 0b63478
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 40 deletions.
3 changes: 2 additions & 1 deletion features/.nav
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
- icmp.feature
- arp.feature
- arp_request.feature
- arp_reply.feature
- lldp.feature
- dhcp.feature
- udp.feature
Expand Down
30 changes: 29 additions & 1 deletion features/arp_reply.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Feature: Arp Reply
When I create a packet with:
"""
Pio::Arp::Reply.new(
source_mac: '00:16:9d:1d:9c:c4',
destination_mac: '00:26:82:eb:ea:d1',
source_mac: '00:16:9d:1d:9c:c4',
sender_protocol_address: '192.168.83.254',
target_protocol_address: '192.168.83.3'
)
Expand All @@ -25,6 +25,34 @@ Feature: Arp Reply
| target_hardware_address | 00:26:82:eb:ea:d1 |
| target_protocol_address | 192.168.83.3 |

Scenario: convert to Ruby code
When I eval the following Ruby code:
"""ruby
Pio::Arp::Reply.new(
destination_mac: '00:26:82:eb:ea:d1',
source_mac: '00:16:9d:1d:9c:c4',
sender_protocol_address: '192.168.83.254',
target_protocol_address: '192.168.83.3'
).to_ruby
"""
Then the result of eval should be:
"""ruby
[
0x00, 0x26, 0x82, 0xeb, 0xea, 0xd1, # destination_mac
0x00, 0x16, 0x9d, 0x1d, 0x9c, 0xc4, # source_mac
0x08, 0x06, # ether_type
0x00, 0x01, # hardware_type
0x08, 0x00, # protocol_type
0x06, # hardware_length
0x04, # protocol_length
0x00, 0x02, # operation
0x00, 0x16, 0x9d, 0x1d, 0x9c, 0xc4, # sender_hardware_address
0xc0, 0xa8, 0x53, 0xfe, # sender_protocol_address
0x00, 0x26, 0x82, 0xeb, 0xea, 0xd1, # target_hardware_address
0xc0, 0xa8, 0x53, 0x03, # target_protocol_address
].pack('C*')
"""

Scenario: parse arp.pcap
Then I parse a file named "arp.pcap" with "Pio::Arp" class

Expand Down
32 changes: 17 additions & 15 deletions features/arp_request.feature
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,29 @@ Feature: Arp Request
| target_hardware_address | 00:00:00:00:00:00 |
| target_protocol_address | 192.168.83.254 |

Scenario: convert to hex string
Scenario: convert to Ruby code
When I eval the following Ruby code:
"""ruby
Pio::Arp::Request.new(
source_mac: '00:26:82:eb:ea:d1',
sender_protocol_address: '192.168.83.3',
target_protocol_address: '192.168.83.254'
).to_hex
).to_ruby
"""
Then the result of eval should be:
"""
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, # destination_mac
0x00, 0x26, 0x82, 0xeb, 0xea, 0xd1, # source_mac
0x08, 0x06, # ether_type
0x00, 0x01, # hardware_type
0x08, 0x00, # protocol_type
0x06, # hardware_length
0x04, # protocol_length
0x00, 0x01, # operation
0x00, 0x26, 0x82, 0xeb, 0xea, 0xd1, # sender_hardware_address
0xc0, 0xa8, 0x53, 0x03, # sender_protocol_address
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # target_hardware_address
0xc0, 0xa8, 0x53, 0xfe, # target_protocol_address
"""ruby
[
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, # destination_mac
0x00, 0x26, 0x82, 0xeb, 0xea, 0xd1, # source_mac
0x08, 0x06, # ether_type
0x00, 0x01, # hardware_type
0x08, 0x00, # protocol_type
0x06, # hardware_length
0x04, # protocol_length
0x00, 0x01, # operation
0x00, 0x26, 0x82, 0xeb, 0xea, 0xd1, # sender_hardware_address
0xc0, 0xa8, 0x53, 0x03, # sender_protocol_address
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # target_hardware_address
0xc0, 0xa8, 0x53, 0xfe, # target_protocol_address
].pack('C*')
"""
22 changes: 7 additions & 15 deletions lib/pio/arp/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,14 @@ def method_missing(method, *args)
@format.__send__ method, *args
end

# rubocop:disable MethodLength
def to_hex
ethernet_header.to_hex + "\n" +
[:hardware_type,
:protocol_type,
:hardware_length,
:protocol_length,
:operation,
:sender_hardware_address,
:sender_protocol_address,
:target_hardware_address,
:target_protocol_address].map do |each|
__send__(each).to_hex + ", # #{each}"
end.join("\n")
# Returns a Ruby code representation of this packet, such that
# it can be eval'ed and sent later.
def to_ruby
hexes = @format.field_names.map do |each|
' ' + __send__(each).to_hex + ", # #{each}" if __send__("#{each}?")
end.compact
['[', *hexes, "].pack('C*')"].join("\n")
end
# rubocop:enable MethodLength
end
end
end
8 changes: 0 additions & 8 deletions lib/pio/ethernet_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ class Parser < BinData::Record
bit1 :vlan_cfi, onlyif: :vlan?
bit12 :vlan_vid_internal, onlyif: :vlan?
uint16 :ether_type_vlan, value: :ether_type, onlyif: :vlan?

def to_hex
[:destination_mac,
:source_mac,
:ether_type].map do |each|
__send__(each).to_hex + ", # #{each}"
end.join("\n")
end
end

# This method smells of :reek:TooManyStatements
Expand Down

0 comments on commit 0b63478

Please sign in to comment.