Skip to content

Commit

Permalink
Land #161, updates for Ruby 2.4.0, whitespace fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Tod Beardsley committed Feb 3, 2017
2 parents 7cfec33 + 27cd915 commit 5df0f4a
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 130 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: ruby
before_install:
- sudo apt-get install libpcap-dev -qq
rvm:
- 2.1.6
- 2.2.3
- 2.3.0
- 2.1
- 2.2
- 2.3.3
- 2.4.0
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,33 @@ Here's an example of creating a TCPPacket and sending it out on the wire:
```
2.3.0 :002 > packet = TCPPacket.new(:config => Utils.whoami?)
=> --EthHeader-------------------------------------------
eth_dst ec:08:6b:62:bc:d2 PacketFu::EthMac
eth_src ac:bc:32:85:47:3f PacketFu::EthMac
eth_proto 0x0800 StructFu::Int16
eth_dst ec:08:6b:62:bc:d2 PacketFu::EthMac
eth_src ac:bc:32:85:47:3f PacketFu::EthMac
eth_proto 0x0800 StructFu::Int16
--IPHeader--------------------------------------------
ip_v 4 Fixnum
ip_hl 5 Fixnum
ip_tos 0 StructFu::Int8
ip_len 20 StructFu::Int16
ip_id 0x77e4 StructFu::Int16
ip_frag 0 StructFu::Int16
ip_ttl 32 StructFu::Int8
ip_proto 6 StructFu::Int8
ip_sum 0xffff StructFu::Int16
ip_src 192.168.0.100 PacketFu::Octets
ip_dst 0.0.0.0 PacketFu::Octets
ip_v 4 Integer
ip_hl 5 Integer
ip_tos 0 StructFu::Int8
ip_len 20 StructFu::Int16
ip_id 0x77e4 StructFu::Int16
ip_frag 0 StructFu::Int16
ip_ttl 32 StructFu::Int8
ip_proto 6 StructFu::Int8
ip_sum 0xffff StructFu::Int16
ip_src 192.168.0.100 PacketFu::Octets
ip_dst 0.0.0.0 PacketFu::Octets
--TCPHeader-------------------------------------------
tcp_src 42653 StructFu::Int16
tcp_dst 0 StructFu::Int16
tcp_seq 0x8d65fbbf StructFu::Int32
tcp_ack 0x00000000 StructFu::Int32
tcp_hlen 5 PacketFu::TcpHlen
tcp_src 42653 StructFu::Int16
tcp_dst 0 StructFu::Int16
tcp_seq 0x8d65fbbf StructFu::Int32
tcp_ack 0x00000000 StructFu::Int32
tcp_hlen 5 PacketFu::TcpHlen
tcp_reserved 0 PacketFu::TcpReserved
tcp_ecn 0 PacketFu::TcpEcn
tcp_flags ...... PacketFu::TcpFlags
tcp_win 16384 StructFu::Int16
tcp_sum 0x7f29 StructFu::Int16
tcp_urg 0 StructFu::Int16
tcp_ecn 0 PacketFu::TcpEcn
tcp_flags ...... PacketFu::TcpFlags
tcp_win 16384 StructFu::Int16
tcp_sum 0x7f29 StructFu::Int16
tcp_urg 0 StructFu::Int16
tcp_opts PacketFu::TcpOptions
2.3.0 :003 > packet.ip_dst = "8.8.8.8"
Expand Down
50 changes: 25 additions & 25 deletions lib/packetfu/protos/eth/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ module PacketFu
#
# ==== Header Definition
#
# Fixnum :b0
# Fixnum :b1
# Fixnum :b2
# Fixnum :b3
# Fixnum :b4
# Fixnum :b5
# Fixnum :local
# Fixnum :multicast
# Integer :b0
# Integer :b1
# Integer :b2
# Integer :b3
# Integer :b4
# Integer :b5
# Integer :local
# Integer :multicast
# Int16 :oui, Default: 0x1ac5 :)
class EthOui < Struct.new(:b5, :b4, :b3, :b2, :b1, :b0, :local, :multicast, :oui)

# EthOui is unusual in that the bit values do not enjoy StructFu typing.
def initialize(args={})
args[:local] ||= 0
args[:local] ||= 0
args[:oui] ||= 0x1ac # :)
args.each_pair {|k,v| args[k] = 0 unless v}
super(args[:b5], args[:b4], args[:b3], args[:b2],
args[:b1], args[:b0], args[:local], args[:multicast],
args.each_pair {|k,v| args[k] = 0 unless v}
super(args[:b5], args[:b4], args[:b3], args[:b2],
args[:b1], args[:b0], args[:local], args[:multicast],
args[:oui])
end

Expand Down Expand Up @@ -68,23 +68,23 @@ def read(str)
#
# ==== Header Definition
#
# Fixnum :n1
# Fixnum :n2
# Fixnum :n3
# Integer:n1
# Integer:n2
# Integer:n3
#
class EthNic < Struct.new(:n0, :n1, :n2)

# EthNic does not enjoy StructFu typing.
def initialize(args={})
args.each_pair {|k,v| args[k] = 0 unless v}
args.each_pair {|k,v| args[k] = 0 unless v}
super(args[:n0], args[:n1], args[:n2])
end

# Returns the object in string form.
def to_s
[n0,n1,n2].map {|x| x.to_i}.pack("C3")
end

# Reads a string to populate the object.
def read(str)
force_binary(str)
Expand Down Expand Up @@ -125,11 +125,11 @@ def read(str)

end

# EthHeader is a complete Ethernet struct, used in EthPacket.
# It's the base header for all other protocols, such as IPHeader,
# TCPHeader, etc.
# EthHeader is a complete Ethernet struct, used in EthPacket.
# It's the base header for all other protocols, such as IPHeader,
# TCPHeader, etc.
#
# For more on the construction on MAC addresses, see
# For more on the construction on MAC addresses, see
# http://en.wikipedia.org/wiki/MAC_address
#
# TODO: Need to come up with a good way of dealing with vlan
Expand Down Expand Up @@ -185,7 +185,7 @@ def read(str)
self
end

# Converts a readable MAC (11:22:33:44:55:66) to a binary string.
# Converts a readable MAC (11:22:33:44:55:66) to a binary string.
# Readable MAC's may be split on colons, dots, spaces, or underscores.
#
# irb> PacketFu::EthHeader.mac2str("11:22:33:44:55:66")
Expand All @@ -200,7 +200,7 @@ def self.mac2str(mac)
return ret
end

# Converts a binary string to a readable MAC (11:22:33:44:55:66).
# Converts a binary string to a readable MAC (11:22:33:44:55:66).
#
# irb> PacketFu::EthHeader.str2mac("\x11\x22\x33\x44\x55\x66")
#
Expand All @@ -218,7 +218,7 @@ def eth_saddr=(mac)
self[:eth_src]
end

# Gets the source MAC address in a more readable way.
# Gets the source MAC address in a more readable way.
def eth_saddr
EthHeader.str2mac(self[:eth_src].to_s)
end
Expand All @@ -230,7 +230,7 @@ def eth_daddr=(mac)
self[:eth_dst]
end

# Gets the destination MAC address in a more readable way.
# Gets the destination MAC address in a more readable way.
def eth_daddr
EthHeader.str2mac(self[:eth_dst].to_s)
end
Expand Down
50 changes: 24 additions & 26 deletions lib/packetfu/protos/ip/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module PacketFu
class Octets < Struct.new(:ip_addr)
include StructFu

IPV4_RE = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
IPV4_RE = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
def initialize(args={})
super(
Int32.new(args[:ip_addr]))
Expand Down Expand Up @@ -46,7 +46,7 @@ def read_quad(str)
match = IPV4_RE.match(str)
if match.nil?
raise ArgumentError.new("str is not a valid IPV4 address")
end
end
a = match[1].to_i
b = match[2].to_i
c = match[3].to_i
Expand All @@ -57,15 +57,15 @@ def read_quad(str)
d >= 0 && d <= 255)
raise ArgumentError.new("str is not a valid IPV4 address")
end

self[:ip_addr].value = (a<<24) + (b<<16) + (c<<8) + d
self
end
# Returns the IP address as 4 octets
def octets
addr = self.to_i
[
[
((addr >> 24) & 0xff),
((addr >> 16) & 0xff),
((addr >> 8) & 0xff),
Expand Down Expand Up @@ -101,21 +101,21 @@ def o4
#
# ==== Header Definition
#
# Fixnum (4 bits) :ip_v, Default: 4
# Fixnum (4 bits) :ip_hl, Default: 5
# Integer (4 bits) :ip_v, Default: 4
# Integer (4 bits) :ip_hl, Default: 5
# Int8 :ip_tos, Default: 0 # TODO: Break out the bits
# Int16 :ip_len, Default: calculated
# Int16 :ip_id, Default: calculated # IRL, hardly random.
# Int16 :ip_len, Default: calculated
# Int16 :ip_id, Default: calculated # IRL, hardly random.
# Int16 :ip_frag, Default: 0 # TODO: Break out the bits
# Int8 :ip_ttl, Default: 0xff # Changes per flavor
# Int8 :ip_proto, Default: 0x01 # TCP: 0x06, UDP 0x11, ICMP 0x01
# Int16 :ip_sum, Default: calculated
# Octets :ip_src
# Octets :ip_dst
# Int16 :ip_sum, Default: calculated
# Octets :ip_src
# Octets :ip_dst
# String :body
#
# Note that IPPackets will always be somewhat incorrect upon initalization,
# and want an IPHeader#recalc() to become correct before a
# Note that IPPackets will always be somewhat incorrect upon initalization,
# and want an IPHeader#recalc() to become correct before a
# Packet#to_f or Packet#to_w.
class IPHeader < Struct.new(:ip_v, :ip_hl, :ip_tos, :ip_len,
:ip_id, :ip_frag, :ip_ttl, :ip_proto,
Expand Down Expand Up @@ -233,7 +233,7 @@ def ip_calc_len
(ip_hl * 4) + body.to_s.length
end
# Return the claimed header length
# Return the claimed header length
def ip_hlen
(ip_hl * 4)
end
Expand All @@ -250,7 +250,7 @@ def ip_calc_sum
checksum += (self.ip_src & 0xffff)
checksum += (self.ip_dst >> 16)
checksum += (self.ip_dst & 0xffff)
checksum = checksum % 0xffff
checksum = checksum % 0xffff
checksum = 0xffff - checksum
checksum == 0 ? 0xffff : checksum
end
Expand All @@ -260,14 +260,14 @@ def ip_calc_id
@random_id
end
# Sets a more readable IP address. If you wants to manipulate individual octets,
# (eg, for host scanning in one network), it would be better use ip_src.o1 through
# ip_src.o4 instead.
# Sets a more readable IP address. If you wants to manipulate individual octets,
# (eg, for host scanning in one network), it would be better use ip_src.o1 through
# ip_src.o4 instead.
def ip_saddr=(addr)
self[:ip_src].read_quad(addr)
end
# Returns a more readable IP source address.
# Returns a more readable IP source address.
def ip_saddr
self[:ip_src].to_x
end
Expand All @@ -286,21 +286,19 @@ def ip_daddr
def self.octet_array(addr)
if addr.class == String
oa = addr.split('.').collect {|x| x.to_i}
elsif addr.class == Fixnum
oa = IPAddr.new(addr, Socket::AF_INET).to_s.split('.')
elsif addr.class == Bignum
elsif addr.kind_of? Integer
oa = IPAddr.new(addr, Socket::AF_INET).to_s.split('.')
elsif addr.class == Array
elsif addr.kind_of? Array
oa = addr
else
raise ArgumentError, "IP Address should be a dotted quad string, an array of ints, or a bignum"
end
end
# Recalculate the calculated IP fields. Valid arguments are:
# :all
# :ip_len
# :ip_sum
# :all
# :ip_len
# :ip_sum
# :ip_id
def ip_recalc(arg=:all)
case arg
Expand Down
22 changes: 11 additions & 11 deletions lib/packetfu/protos/ipv6/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def to_i
(a1.to_i << 96) + (a2.to_i << 64) + (a3.to_i << 32) + a4.to_i
end

# Returns the address as a colon-delimited hex string.
# Returns the address as a colon-delimited hex string.
def to_x
IPAddr.new(self.to_i, Socket::AF_INET6).to_s
end
Expand Down Expand Up @@ -60,13 +60,13 @@ def read_x(str)

end

# IPv6Header is complete IPv6 struct, used in IPv6Packet.
# IPv6Header is complete IPv6 struct, used in IPv6Packet.
#
# ==== Header Definition
#
# Fixnum (4 bits) :ipv6_v Default: 6 # Versiom
# Fixnum (8 bits) :ipv6_class Defualt: 0 # Class
# Fixnum (20 bits) :ipv6_label Defualt: 0 # Label
# Integer(4 bits) :ipv6_v Default: 6 # Versiom
# Integer(8 bits) :ipv6_class Defualt: 0 # Class
# Integer(20 bits) :ipv6_label Defualt: 0 # Label
# Int16 :ipv6_len Default: calc # Payload length
# Int8 :ipv6_next # Next Header
# Int8 :ipv6_hop Default: 0xff # Hop limit
Expand Down Expand Up @@ -116,9 +116,9 @@ def read(str)
self
end

# Setter for the version (usually, 6).
# Setter for the version (usually, 6).
def ipv6_v=(i); self[:ip_v] = i.to_i; end
# Getter for the version (usually, 6).
# Getter for the version (usually, 6).
def ipv6_v; self[:ipv6_v].to_i; end
# Setter for the traffic class.
def ipv6_class=(i); self[:ip_class] = i.to_i; end
Expand Down Expand Up @@ -164,22 +164,22 @@ def ipv6_recalc(arg=:all)
end
end

# Get the source address in a more readable form.
# Get the source address in a more readable form.
def ipv6_saddr
self[:ipv6_src].to_x
end

# Set the source address in a more readable form.
# Set the source address in a more readable form.
def ipv6_saddr=(str)
self[:ipv6_src].read_x(str)
end

# Get the destination address in a more readable form.
# Get the destination address in a more readable form.
def ipv6_daddr
self[:ipv6_dst].to_x
end

# Set the destination address in a more readable form.
# Set the destination address in a more readable form.
def ipv6_daddr=(str)
self[:ipv6_dst].read_x(str)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/packetfu/protos/tcp/ecn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module PacketFu
# ==== Header Definition
#
#
# Fixnum (1 bit) :n
# Fixnum (1 bit) :c
# Fixnum (1 bit) :e
# Integer(1 bit) :n
# Integer(1 bit) :c
# Integer(1 bit) :e
class TcpEcn < Struct.new(:n, :c, :e)

include StructFu
Expand Down
Loading

0 comments on commit 5df0f4a

Please sign in to comment.