Skip to content

Commit

Permalink
Merge branch 'release/0.16.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Mar 2, 2015
2 parents de048a0 + c47a96a commit c8dde28
Show file tree
Hide file tree
Showing 32 changed files with 415 additions and 438 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,11 @@
## develop (unreleased)


## 0.16.0 (3/2/2015)
### New features
* [#129](https://github.com/trema/pio/pull/129): Add new class `Pio::Udp`.


## 0.15.2 (2/18/2015)
### Changes
* [#128](https://github.com/trema/pio/pull/128): Field accessors return Ruby primitives (Fixnum, Symbol, etc.).
Expand Down
1 change: 1 addition & 0 deletions features/dhcp_read.feature
@@ -1,3 +1,4 @@
@announce
Feature: Pio::Dhcp.read
Scenario: dhcp.pcap
Given a packet data file "dhcp.pcap"
Expand Down
50 changes: 50 additions & 0 deletions features/icmp_read.feature
Expand Up @@ -3,3 +3,53 @@ Feature: Pio::Icmp.read
Given a packet data file "icmp.pcap"
When I try to parse the file with "Icmp" class
Then it should finish successfully
And the parsed data #1 have the following field and value:
| field | value |
| class | Pio::Icmp::Request |
| destination_mac | 00:13:46:0b:22:ba |
| source_mac | 00:16:ce:6e:8b:24 |
| ether_type | 2048 |
| ip_version | 4 |
| ip_header_length | 5 |
| ip_type_of_service | 0 |
| ip_total_length | 60 |
| ip_identifier | 36507 |
| ip_flag | 0 |
| ip_fragment | 0 |
| ip_ttl | 128 |
| ip_protocol | 1 |
| ip_header_checksum | 10850 |
| ip_source_address | 192.168.0.114 |
| ip_destination_address | 192.168.0.1 |
| ip_option | |
| icmp_type | 8 |
| icmp_code | 0 |
| icmp_checksum | 18780 |
| icmp_identifier | 768 |
| icmp_sequence_number | 256 |
| echo_data | abcdefghijklmnopqrstuvwabcdefghi |
And the parsed data #2 have the following field and value:
| field | value |
| class | Pio::Icmp::Reply |
| destination_mac | 00:16:ce:6e:8b:24 |
| source_mac | 00:13:46:0b:22:ba |
| ether_type | 2048 |
| ip_version | 4 |
| ip_header_length | 5 |
| ip_type_of_service | 0 |
| ip_total_length | 60 |
| ip_identifier | 24150 |
| ip_flag | 0 |
| ip_fragment | 0 |
| ip_ttl | 127 |
| ip_protocol | 1 |
| ip_header_checksum | 23463 |
| ip_source_address | 192.168.0.1 |
| ip_destination_address | 192.168.0.114 |
| ip_option | |
| icmp_type | 0 |
| icmp_code | 0 |
| icmp_checksum | 20828 |
| icmp_identifier | 768 |
| icmp_sequence_number | 256 |
| echo_data | abcdefghijklmnopqrstuvwabcdefghi |
16 changes: 15 additions & 1 deletion features/step_definitions/packet_data_steps.rb
Expand Up @@ -17,8 +17,11 @@
@result = parser_klass.read(IO.read(@raw))
elsif @pcap
File.open(@pcap) do |file|
@result = []
pcap = Pio::Pcap::Frame.read(file)
pcap.records.each { |each| parser_klass.read each.data }
pcap.records.each do |each|
@result << parser_klass.read(each.data)
end
end
else
fail 'Packet data file is not specified.'
Expand All @@ -41,3 +44,14 @@
expect(output.to_s).to eq(each['value'])
end
end

# rubocop:disable LineLength
Then(/^the parsed data \#(\d+) have the following field and value:$/) do |index, table|
table.hashes.each do |each|
output = each['field'].split('.').inject(@result[index.to_i - 1]) do |memo, method|
memo.__send__(method)
end
expect(output.to_s).to eq(each['value'])
end
end
# rubocop:enable LineLength
29 changes: 29 additions & 0 deletions features/udp_read.feature
@@ -0,0 +1,29 @@
Feature: Pio::Udp.read
Scenario: dhcp.pcap
Given a packet data file "dhcp.pcap"
When I try to parse the file with "Udp" class
Then it should finish successfully
And the parsed data #1 have the following field and value:
| field | value |
| class | Pio::Udp |
| destination_mac | ff:ff:ff:ff:ff:ff |
| source_mac | 00:0b:82:01:fc:42 |
| ether_type | 2048 |
| ip_version | 4 |
| ip_header_length | 5 |
| ip_type_of_service | 0 |
| ip_total_length | 300 |
| ip_identifier | 43062 |
| ip_flag | 0 |
| ip_fragment | 0 |
| ip_ttl | 250 |
| ip_protocol | 17 |
| ip_header_checksum | 6027 |
| ip_source_address | 0.0.0.0 |
| ip_destination_address | 255.255.255.255 |
| ip_option | |
| udp_source_port | 68 |
| udp_destination_port | 67 |
| udp_length | 280 |
| udp_checksum | 22815 |
| udp_payload.length | 272 |
1 change: 1 addition & 0 deletions lib/pio.rb
Expand Up @@ -13,3 +13,4 @@
require 'pio/packet_in'
require 'pio/packet_out'
require 'pio/port_status'
require 'pio/udp'
4 changes: 2 additions & 2 deletions lib/pio/arp/format.rb
@@ -1,13 +1,13 @@
require 'bindata'
require 'pio/type/ethernet_header'
require 'pio/ethernet_header'
require 'pio/type/ip_address'
require 'pio/type/mac_address'

module Pio
class Arp
# ARP parser.
class Format < BinData::Record
extend Type::EthernetHeader
include EthernetHeader

endian :big

Expand Down
4 changes: 2 additions & 2 deletions lib/pio/dhcp/boot_reply_options.rb
Expand Up @@ -31,8 +31,8 @@ def to_hash
source_mac: source_mac,
ip_destination_address: ip_destination_address,
ip_source_address: ip_source_address,
udp_src_port: BOOTPS,
udp_dst_port: BOOTPC,
udp_source_port: BOOTPS,
udp_destination_port: BOOTPC,
dhcp: dhcp_data
}
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pio/dhcp/boot_request_options.rb
Expand Up @@ -27,8 +27,8 @@ def to_hash
source_mac: source_mac,
ip_source_address: QUAD_ZERO_IP_ADDRESS,
ip_destination_address: BROADCAST_IP_ADDRESS,
udp_src_port: BOOTPC,
udp_dst_port: BOOTPS,
udp_source_port: BOOTPC,
udp_destination_port: BOOTPS,
dhcp: dhcp_data
}
end
Expand Down
81 changes: 0 additions & 81 deletions lib/pio/dhcp/csum_util.rb

This file was deleted.

87 changes: 16 additions & 71 deletions lib/pio/dhcp/frame.rb
@@ -1,92 +1,37 @@
require 'pio/type/ethernet_header'
require 'pio/type/ipv4_header'
require 'pio/type/udp_header'
require 'pio/dhcp/dhcp_field'
require 'pio/dhcp/field_util'
require 'pio/dhcp/csum_util'
require 'pio/ipv4_header'
require 'pio/ethernet_header'
require 'pio/udp_header'

module Pio
class Dhcp
# Dhcp frame parser.
class Frame < BinData::Record
extend Type::EthernetHeader
extend Type::IPv4Header
extend Type::UdpHeader
include FieldUtil
include CsumUtil

ETHER_TYPE_IP = 0x0800
IP_PROTOCOL_UDP = 17
IP_HEADER_LENGTH = 20
UDP_HEADER_LENGTH = 8
DHCP_OPTION_FIELD_LENGTH = 60
OPTION_FIELD_LENGTH = 60

endian :big
include EthernetHeader
include IPv4Header
include UdpHeader

ethernet_header ether_type: ETHER_TYPE_IP
ipv4_header ip_protocol: IP_PROTOCOL_UDP,
ip_header_checksum: -> { ip_sum },
ip_total_length: -> { ip_len }
udp_header udp_length: -> { udp_len },
udp_checksum: -> { udp_sum }
endian :big
ethernet_header ether_type: EthernetHeader::ETHER_TYPE_IP
ipv4_header ip_protocol: IPv4Header::IP_PROTOCOL_UDP
udp_header
dhcp_field :dhcp

def ip_sum
~((ip_csum & 0xffff) + (ip_csum >> 16)) & 0xffff
end

def udp_sum
~((udp_csum & 0xffff) + (udp_csum >> 16)) & 0xffff
end

def ip_len
IP_HEADER_LENGTH + udp_len
end

def udp_len
UDP_HEADER_LENGTH + dhcp_len
end
string :padding, read_length: 0, initial_value: :ff_and_padding

def to_binary
to_binary_s + trail_data
to_binary_s
end

private

def dhcp_len
dhcp.num_bytes + trail_data.bytesize
end

def dhcp_binary
dhcp.to_binary_s
end

def should_padding?
option_length < DHCP_OPTION_FIELD_LENGTH
end

def option_length
dhcp.optional_tlvs.num_bytes + 1
end

def trail_data
if should_padding?
end_of_pdu + padding
else
end_of_pdu
end
end

def end_of_pdu
[0xff].pack('C*')
end

def padding
[0x00].pack('C*') * padding_length
end

def padding_length
DHCP_OPTION_FIELD_LENGTH - option_length
def ff_and_padding
padding_length = OPTION_FIELD_LENGTH - dhcp.optional_tlvs.num_bytes - 1
"\xFF" + (padding_length > 0 ? "\x00" * padding_length : '')
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pio/dhcp/message.rb
Expand Up @@ -22,8 +22,8 @@ class Message
def_delegators :@frame, :ip_header_checksum
def_delegators :@frame, :ip_source_address
def_delegators :@frame, :ip_destination_address
def_delegators :@frame, :udp_src_port
def_delegators :@frame, :udp_dst_port
def_delegators :@frame, :udp_source_port
def_delegators :@frame, :udp_destination_port
def_delegators :@frame, :udp_length
def_delegators :@frame, :udp_checksum
def_delegators :@frame, :dhcp
Expand Down

0 comments on commit c8dde28

Please sign in to comment.