-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
89 additions
and
357 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
require 'pio/arp/format' | ||
require 'pio/arp/request' | ||
require 'pio/arp/reply' | ||
require 'pio/message_type_selector' | ||
require 'pio/arp/request' | ||
require 'pio/parse_error' | ||
|
||
# Packet parser and generator library. | ||
module Pio | ||
# ARP parser and generator. | ||
class Arp | ||
extend MessageTypeSelector | ||
message_type Request::OPERATION => Request, Reply::OPERATION => Reply | ||
def self.read(raw_data) | ||
format = Format.read(raw_data) | ||
{ Request.operation => Request, | ||
Reply.operation => Reply }.fetch(format.operation).create(format) | ||
rescue | ||
raise Pio::ParseError, $ERROR_INFO.message | ||
end | ||
end | ||
ARP = Arp | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,20 @@ | ||
require 'pio/arp/message' | ||
require 'pio/instance_inspector' | ||
require 'pio/mac' | ||
require 'pio/options' | ||
|
||
module Pio | ||
class Arp | ||
# ARP Reply packet generator | ||
class Reply < Message | ||
include InstanceInspector | ||
|
||
OPERATION = 2 | ||
public_class_method :new | ||
|
||
# User options for creating an Arp Reply. | ||
class Options < Pio::Options | ||
mandatory_option :source_mac | ||
mandatory_option :destination_mac | ||
mandatory_option :sender_protocol_address | ||
mandatory_option :target_protocol_address | ||
|
||
def initialize(options) | ||
validate options | ||
@source_mac = Mac.new(options[:source_mac]).freeze | ||
@destination_mac = Mac.new(options[:destination_mac]).freeze | ||
@sender_protocol_address = | ||
IPv4Address.new(options[:sender_protocol_address]).freeze | ||
@target_protocol_address = | ||
IPv4Address.new(options[:target_protocol_address]).freeze | ||
end | ||
|
||
def to_hash | ||
{ | ||
operation: OPERATION, | ||
source_mac: @source_mac, | ||
destination_mac: @destination_mac, | ||
sender_hardware_address: @source_mac, | ||
target_hardware_address: @destination_mac, | ||
sender_protocol_address: @sender_protocol_address, | ||
target_protocol_address: @target_protocol_address | ||
}.freeze | ||
end | ||
end | ||
options operation: 2, | ||
source_mac: :source_mac, | ||
destination_mac: :destination_mac, | ||
sender_hardware_address: :source_mac, | ||
target_hardware_address: :destination_mac, | ||
sender_protocol_address: :sender_protocol_address, | ||
target_protocol_address: :target_protocol_address | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,20 @@ | ||
require 'pio/arp/message' | ||
require 'pio/instance_inspector' | ||
require 'pio/mac' | ||
require 'pio/options' | ||
|
||
module Pio | ||
class Arp | ||
# ARP Request packet generator | ||
class Request < Message | ||
include InstanceInspector | ||
|
||
OPERATION = 1 | ||
public_class_method :new | ||
|
||
# User options for creating an Arp Request. | ||
class Options < Pio::Options | ||
mandatory_option :source_mac | ||
mandatory_option :sender_protocol_address | ||
mandatory_option :target_protocol_address | ||
|
||
BROADCAST_MAC = Mac.new(0xffffffffffff).freeze | ||
ALL_ZERO_MAC = Mac.new(0).freeze | ||
|
||
def initialize(options) | ||
validate options | ||
@source_mac = Mac.new(options[:source_mac]).freeze | ||
@sender_protocol_address = | ||
IPv4Address.new(options[:sender_protocol_address]).freeze | ||
@target_protocol_address = | ||
IPv4Address.new(options[:target_protocol_address]).freeze | ||
end | ||
|
||
def to_hash | ||
{ | ||
operation: OPERATION, | ||
source_mac: @source_mac, | ||
destination_mac: BROADCAST_MAC, | ||
sender_hardware_address: @source_mac, | ||
target_hardware_address: ALL_ZERO_MAC, | ||
sender_protocol_address: @sender_protocol_address, | ||
target_protocol_address: @target_protocol_address | ||
}.freeze | ||
end | ||
end | ||
options operation: 1, | ||
source_mac: :source_mac, | ||
destination_mac: 'ff:ff:ff:ff:ff:ff'.freeze, | ||
sender_hardware_address: :source_mac, | ||
target_hardware_address: '00:00:00:00:00:00'.freeze, | ||
sender_protocol_address: :sender_protocol_address, | ||
target_protocol_address: :target_protocol_address | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'active_support/core_ext/class/attribute' | ||
require 'pio/ruby_dumper' | ||
|
||
module Pio | ||
# Base message class | ||
class Message | ||
include RubyDumper | ||
|
||
class_attribute :options | ||
|
||
def self.options(options) | ||
options.each_pair do |key, value| | ||
next if value.is_a?(Symbol) | ||
class_eval { class_attribute key } | ||
class_eval { __send__("#{key}=", value) } | ||
end | ||
self.options = options | ||
end | ||
|
||
private | ||
|
||
# rubocop:disable MethodLength | ||
def parse_options(user_options) | ||
options = {} | ||
self.class.options.each_pair do |key, value| | ||
if value.is_a?(Symbol) | ||
begin | ||
options[key] = user_options.fetch(value) | ||
rescue | ||
raise "#{key} option is a mandatory" | ||
end | ||
else | ||
options[key] = user_options[key] || value | ||
end | ||
end | ||
options | ||
end | ||
# rubocop:enable MethodLength | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.