Skip to content

Commit

Permalink
Merge 6bfa04f into 83eb1fc
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Jul 5, 2016
2 parents 83eb1fc + 6bfa04f commit 69d7a65
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 26 deletions.
27 changes: 1 addition & 26 deletions features/arp.feature → features/arp_reply.feature
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
Feature: Arp
Scenario: create an ARP request
When I create a packet with:
"""
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'
)
"""
Then the packet has the following fields and values:
| field | value |
| class | Pio::Arp::Request |
| destination_mac | ff:ff:ff:ff:ff:ff |
| source_mac | 00:26:82:eb:ea:d1 |
| ether_type | 2054 |
| hardware_type | 1 |
| protocol_type | 2048 |
| hardware_length | 6 |
| protocol_length | 4 |
| operation | 1 |
| sender_hardware_address | 00:26:82:eb:ea:d1 |
| sender_protocol_address | 192.168.83.3 |
| target_hardware_address | 00:00:00:00:00:00 |
| target_protocol_address | 192.168.83.254 |

Feature: Arp Reply
Scenario: create an ARP reply
When I create a packet with:
"""
Expand Down
50 changes: 50 additions & 0 deletions features/arp_request.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Feature: Arp Request
Scenario: create an ARP request
When I create a packet with:
"""
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'
)
"""
Then the packet has the following fields and values:
| field | value |
| class | Pio::Arp::Request |
| destination_mac | ff:ff:ff:ff:ff:ff |
| source_mac | 00:26:82:eb:ea:d1 |
| ether_type | 2054 |
| hardware_type | 1 |
| protocol_type | 2048 |
| hardware_length | 6 |
| protocol_length | 4 |
| operation | 1 |
| sender_hardware_address | 00:26:82:eb:ea:d1 |
| sender_protocol_address | 192.168.83.3 |
| target_hardware_address | 00:00:00:00:00:00 |
| target_protocol_address | 192.168.83.254 |

Scenario: convert to hex string
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
"""
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
"""
7 changes: 7 additions & 0 deletions features/step_definitions/ruby_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
When(/^I eval the following Ruby code:$/) do |ruby_code|
@result = Pio.module_eval(ruby_code)
end

Then(/^the result of eval should be:$/) do |expected|
expect(@result.to_s).to eq expected
end
17 changes: 17 additions & 0 deletions lib/pio/arp/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ def initialize(user_options)
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")
end
# rubocop:enable MethodLength
end
end
end
27 changes: 27 additions & 0 deletions lib/pio/ethernet_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ module EtherType
LLDP = 0x88cc
end

# Ethernet header parser
class Parser < BinData::Record
endian :big

mac_address :destination_mac
mac_address :source_mac
uint16 :ether_type
bit3 :vlan_pcp_internal, onlyif: :vlan?
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
def self.included(klass)
def klass.ethernet_header(options)
Expand All @@ -24,6 +45,12 @@ def klass.ethernet_header(options)
end
end

def ethernet_header
Parser.new(destination_mac: destination_mac,
source_mac: source_mac,
ether_type: ether_type)
end

def vlan_vid
vlan? ? vlan_vid_internal : 0xffff
end
Expand Down
4 changes: 4 additions & 0 deletions lib/pio/mac.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def to_a
to_s.split(':').map(&:hex)
end

def to_hex
to_a.map(&:to_hex).join(', ')
end

# @!endgroup

# @!group Predicates
Expand Down
4 changes: 4 additions & 0 deletions lib/pio/type/ip_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def &(other)
def ==(other)
get == other
end

def to_hex
octets.map(&:to_hex).join(', ')
end
end
end
end

0 comments on commit 69d7a65

Please sign in to comment.