packetfu 1.1.9 → 1.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bench/octets.rb +9 -9
- data/examples/100kpackets.rb +12 -12
- data/examples/ackscan.rb +16 -16
- data/examples/arp.rb +35 -35
- data/examples/arphood.rb +36 -36
- data/examples/dissect_thinger.rb +6 -6
- data/examples/new-simple-stats.rb +23 -23
- data/examples/packetfu-shell.rb +25 -25
- data/examples/simple-sniffer.rb +9 -9
- data/examples/simple-stats.rb +23 -23
- data/examples/slammer.rb +3 -3
- data/lib/packetfu.rb +127 -127
- data/lib/packetfu/capture.rb +169 -169
- data/lib/packetfu/config.rb +52 -52
- data/lib/packetfu/inject.rb +56 -56
- data/lib/packetfu/packet.rb +528 -528
- data/lib/packetfu/pcap.rb +579 -579
- data/lib/packetfu/protos/arp.rb +90 -90
- data/lib/packetfu/protos/arp/header.rb +158 -158
- data/lib/packetfu/protos/arp/mixin.rb +36 -36
- data/lib/packetfu/protos/eth.rb +44 -44
- data/lib/packetfu/protos/eth/header.rb +243 -243
- data/lib/packetfu/protos/eth/mixin.rb +3 -3
- data/lib/packetfu/protos/hsrp.rb +69 -69
- data/lib/packetfu/protos/hsrp/header.rb +107 -107
- data/lib/packetfu/protos/hsrp/mixin.rb +29 -29
- data/lib/packetfu/protos/icmp.rb +71 -71
- data/lib/packetfu/protos/icmp/header.rb +82 -82
- data/lib/packetfu/protos/icmp/mixin.rb +14 -14
- data/lib/packetfu/protos/invalid.rb +49 -49
- data/lib/packetfu/protos/ip.rb +69 -69
- data/lib/packetfu/protos/ip/header.rb +291 -291
- data/lib/packetfu/protos/ip/mixin.rb +40 -40
- data/lib/packetfu/protos/ipv6.rb +50 -50
- data/lib/packetfu/protos/ipv6/header.rb +188 -188
- data/lib/packetfu/protos/ipv6/mixin.rb +29 -29
- data/lib/packetfu/protos/tcp.rb +176 -176
- data/lib/packetfu/protos/tcp/ecn.rb +35 -35
- data/lib/packetfu/protos/tcp/flags.rb +74 -74
- data/lib/packetfu/protos/tcp/header.rb +268 -268
- data/lib/packetfu/protos/tcp/hlen.rb +32 -32
- data/lib/packetfu/protos/tcp/mixin.rb +46 -46
- data/lib/packetfu/protos/tcp/option.rb +321 -321
- data/lib/packetfu/protos/tcp/options.rb +95 -95
- data/lib/packetfu/protos/tcp/reserved.rb +35 -35
- data/lib/packetfu/protos/udp.rb +116 -116
- data/lib/packetfu/protos/udp/header.rb +91 -91
- data/lib/packetfu/protos/udp/mixin.rb +3 -3
- data/lib/packetfu/structfu.rb +280 -280
- data/lib/packetfu/utils.rb +226 -217
- data/lib/packetfu/version.rb +41 -41
- data/packetfu.gemspec +2 -1
- data/spec/ethpacket_spec.rb +48 -48
- data/spec/packet_spec.rb +57 -57
- data/spec/packet_subclasses_spec.rb +8 -8
- data/spec/packetfu_spec.rb +59 -59
- data/spec/structfu_spec.rb +268 -268
- data/spec/tcp_spec.rb +75 -75
- data/test/all_tests.rb +13 -13
- data/test/func_lldp.rb +3 -3
- data/test/ptest.rb +2 -2
- data/test/test_arp.rb +116 -116
- data/test/test_capture.rb +45 -45
- data/test/test_eth.rb +68 -68
- data/test/test_hsrp.rb +9 -9
- data/test/test_icmp.rb +52 -52
- data/test/test_inject.rb +18 -18
- data/test/test_invalid.rb +16 -16
- data/test/test_ip.rb +36 -36
- data/test/test_ip6.rb +48 -48
- data/test/test_octets.rb +21 -21
- data/test/test_packet.rb +154 -154
- data/test/test_pcap.rb +170 -170
- data/test/test_structfu.rb +97 -97
- data/test/test_tcp.rb +320 -320
- data/test/test_udp.rb +76 -76
- metadata +4 -3
data/lib/packetfu/utils.rb
CHANGED
@@ -1,224 +1,233 @@
|
|
1
1
|
# -*- coding: binary -*-
|
2
2
|
require 'singleton'
|
3
|
+
require 'timeout'
|
3
4
|
module PacketFu
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
6
|
+
# Utils is a collection of various and sundry network utilities that are useful for packet
|
7
|
+
# manipulation.
|
8
|
+
class Utils
|
9
|
+
|
10
|
+
# Returns the MAC address of an IP address, or nil if it's not responsive to arp. Takes
|
11
|
+
# a dotted-octect notation of the target IP address, as well as a number of parameters:
|
12
|
+
#
|
13
|
+
# === Parameters
|
14
|
+
# :iface
|
15
|
+
# Interface. Defaults to "eth0"
|
16
|
+
# :eth_saddr
|
17
|
+
# Source MAC address. Defaults to "00:00:00:00:00:00".
|
18
|
+
# :ip_saddr
|
19
|
+
# Source IP address. Defaults to "0.0.0.0"
|
20
|
+
# :flavor
|
21
|
+
# The flavor of the ARP request. Defaults to :none.
|
22
|
+
# :timeout
|
23
|
+
# Timeout in seconds. Defaults to 3.
|
24
|
+
#
|
25
|
+
# === Example
|
26
|
+
# PacketFu::Utils::arp("192.168.1.1") #=> "00:18:39:01:33:70"
|
27
|
+
# PacketFu::Utils::arp("192.168.1.1", :iface => "wlan2", :timeout => 5, :flavor => :hp_deskjet)
|
28
|
+
#
|
29
|
+
# === Warning
|
30
|
+
#
|
31
|
+
# It goes without saying, spewing forged ARP packets on your network is a great way to really
|
32
|
+
# irritate your co-workers.
|
33
|
+
def self.arp(target_ip,args={})
|
34
|
+
iface = args[:iface] || :eth0
|
35
|
+
args[:config] ||= whoami?(:iface => iface)
|
36
|
+
arp_pkt = PacketFu::ARPPacket.new(:flavor => (args[:flavor] || :none), :config => args[:config])
|
37
|
+
arp_pkt.eth_daddr = "ff:ff:ff:ff:ff:ff"
|
38
|
+
arp_pkt.arp_daddr_mac = "00:00:00:00:00:00"
|
39
|
+
arp_pkt.arp_daddr_ip = target_ip
|
40
|
+
# Stick the Capture object in its own thread.
|
41
|
+
cap_thread = Thread.new do
|
42
|
+
target_mac = nil
|
43
|
+
cap = PacketFu::Capture.new(:iface => iface, :start => true,
|
44
|
+
:filter => "arp src #{target_ip} and ether dst #{arp_pkt.eth_saddr}")
|
45
|
+
arp_pkt.to_w(iface) # Shorthand for sending single packets to the default interface.
|
46
|
+
timeout = 0
|
47
|
+
while target_mac.nil? && timeout <= (args[:timeout] || 3)
|
48
|
+
if cap.save > 0
|
49
|
+
arp_response = PacketFu::Packet.parse(cap.array[0])
|
50
|
+
target_mac = arp_response.arp_saddr_mac if arp_response.arp_saddr_ip = target_ip
|
51
|
+
end
|
52
|
+
timeout += 0.1
|
53
|
+
sleep 0.1 # Check for a response ten times per second.
|
54
|
+
end
|
55
|
+
target_mac
|
56
|
+
end # cap_thread
|
57
|
+
cap_thread.value
|
58
|
+
end
|
59
|
+
|
60
|
+
# Since 177/8 is IANA reserved (for now), this network should
|
61
|
+
# be handled by your default gateway and default interface.
|
62
|
+
def self.rand_routable_daddr
|
63
|
+
IPAddr.new((rand(16777216) + 2969567232), Socket::AF_INET)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Discovers the local IP and Ethernet address, which is useful for writing
|
67
|
+
# packets you expect to get a response to. Note, this is a noisy
|
68
|
+
# operation; a UDP packet is generated and dropped on to the default (or named)
|
69
|
+
# interface, and then captured (which means you need to be root to do this).
|
70
|
+
#
|
71
|
+
# whoami? returns a hash of :eth_saddr, :eth_src, :ip_saddr, :ip_src,
|
72
|
+
# :ip_src_bin, :eth_dst, and :eth_daddr (the last two are usually suitable
|
73
|
+
# for a gateway mac address). It's most useful as an argument to
|
74
|
+
# PacketFu::Config.new, or as an argument to the many Packet constructors.
|
75
|
+
#
|
76
|
+
# Note that if you have multiple interfaces with the same route (such as when
|
77
|
+
# wlan0 and eth0 are associated to the same network), the "first" one
|
78
|
+
# according to Pcap.lookupdev will be used, regardless of which :iface you
|
79
|
+
# pick.
|
80
|
+
#
|
81
|
+
# === Parameters
|
82
|
+
# :iface => "eth0"
|
83
|
+
# An interface to listen for packets on. Note that since we rely on the OS to send the probe packet,
|
84
|
+
# you will need to specify a target which will use this interface.
|
85
|
+
# :target => "1.2.3.4"
|
86
|
+
# A target IP address. By default, a packet will be sent to a random address in the 177/8 network.
|
87
|
+
def self.whoami?(args={})
|
88
|
+
unless args.kind_of? Hash
|
89
|
+
raise ArgumentError, "Argument to `whoami?' must be a Hash"
|
90
|
+
end
|
91
|
+
if args[:iface].to_s =~ /^lo/ # Linux loopback more or less. Need a switch for windows loopback, too.
|
92
|
+
dst_host = "127.0.0.1"
|
93
|
+
else
|
94
|
+
dst_host = (args[:target] || rand_routable_daddr.to_s)
|
95
|
+
end
|
96
|
+
|
97
|
+
dst_port = rand(0xffff-1024)+1024
|
98
|
+
msg = "PacketFu whoami? packet #{(Time.now.to_i + rand(0xffffff)+1)}"
|
99
|
+
iface = (args[:iface] || ENV['IFACE'] || Pcap.lookupdev || :lo ).to_s
|
100
|
+
cap = PacketFu::Capture.new(:iface => iface, :promisc => false, :start => true, :filter => "udp and dst host #{dst_host} and dst port #{dst_port}")
|
101
|
+
udp_sock = UDPSocket.new
|
102
|
+
udp_sock.send(msg,0,dst_host,dst_port)
|
103
|
+
udp_sock = nil
|
104
|
+
|
105
|
+
my_data = nil
|
106
|
+
|
107
|
+
begin
|
108
|
+
Timeout::timeout(1) {
|
109
|
+
pkt = nil
|
110
|
+
|
111
|
+
while pkt.nil?
|
112
|
+
raw_pkt = cap.next
|
113
|
+
next if raw_pkt.nil?
|
114
|
+
|
115
|
+
pkt = Packet.parse(raw_pkt)
|
116
|
+
|
117
|
+
if pkt.payload == msg
|
118
|
+
|
119
|
+
my_data = {
|
120
|
+
:iface => (args[:iface] || ENV['IFACE'] || Pcap.lookupdev || "lo").to_s,
|
121
|
+
:pcapfile => args[:pcapfile] || "/tmp/out.pcap",
|
122
|
+
:eth_saddr => pkt.eth_saddr,
|
123
|
+
:eth_src => pkt.eth_src.to_s,
|
124
|
+
:ip_saddr => pkt.ip_saddr,
|
125
|
+
:ip_src => pkt.ip_src,
|
126
|
+
:ip_src_bin => [pkt.ip_src].pack("N"),
|
127
|
+
:eth_dst => pkt.eth_dst.to_s,
|
128
|
+
:eth_daddr => pkt.eth_daddr
|
129
|
+
}
|
130
|
+
|
131
|
+
else raise SecurityError,
|
132
|
+
"whoami() packet doesn't match sent data. Something fishy's going on."
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
}
|
137
|
+
rescue Timeout::Error
|
138
|
+
raise SocketError, "Didn't receive the whoami() packet, can't automatically configure."
|
139
|
+
end
|
140
|
+
|
141
|
+
my_data
|
142
|
+
end
|
143
|
+
|
144
|
+
# This is a brute-force approach at trying to find a suitable interface with an IP address.
|
145
|
+
def self.lookupdev
|
146
|
+
# XXX cycle through eth0-9 and wlan0-9, and if a cap start throws a RuntimeErorr (and we're
|
147
|
+
# root), it's not a good interface. Boy, really ought to fix lookupdev directly with another
|
148
|
+
# method that returns an array rather than just the first candidate.
|
149
|
+
end
|
150
|
+
|
151
|
+
# Handles ifconfig for various (okay, two) platforms.
|
152
|
+
# Will have Windows done shortly.
|
153
|
+
#
|
154
|
+
# Takes an argument (either string or symbol) of the interface to look up, and
|
155
|
+
# returns a hash which contains at least the :iface element, and if configured,
|
156
|
+
# these additional elements:
|
157
|
+
#
|
158
|
+
# :eth_saddr # A human readable MAC address
|
159
|
+
# :eth_src # A packed MAC address
|
160
|
+
# :ip_saddr # A dotted-quad string IPv4 address
|
161
|
+
# :ip_src # A packed IPv4 address
|
162
|
+
# :ip4_obj # An IPAddr object with bitmask
|
163
|
+
# :ip6_saddr # A colon-delimited hex IPv6 address, with bitmask
|
164
|
+
# :ip6_obj # An IPAddr object with bitmask
|
165
|
+
#
|
166
|
+
# === Example
|
167
|
+
# PacketFu::Utils.ifconfig :wlan0 # Not associated yet
|
168
|
+
# #=> {:eth_saddr=>"00:1d:e0:73:9d:ff", :eth_src=>"\000\035\340s\235\377", :iface=>"wlan0"}
|
169
|
+
# PacketFu::Utils.ifconfig("eth0") # Takes 'eth0' as default
|
170
|
+
# #=> {:eth_saddr=>"00:1c:23:35:70:3b", :eth_src=>"\000\034#5p;", :ip_saddr=>"10.10.10.9", :ip4_obj=>#<IPAddr: IPv4:10.10.10.0/255.255.254.0>, :ip_src=>"\n\n\n\t", :iface=>"eth0", :ip6_saddr=>"fe80::21c:23ff:fe35:703b/64", :ip6_obj=>#<IPAddr: IPv6:fe80:0000:0000:0000:0000:0000:0000:0000/ffff:ffff:ffff:ffff:0000:0000:0000:0000>}
|
171
|
+
# PacketFu::Utils.ifconfig :lo
|
172
|
+
# #=> {:ip_saddr=>"127.0.0.1", :ip4_obj=>#<IPAddr: IPv4:127.0.0.0/255.0.0.0>, :ip_src=>"\177\000\000\001", :iface=>"lo", :ip6_saddr=>"::1/128", :ip6_obj=>#<IPAddr: IPv6:0000:0000:0000:0000:0000:0000:0000:0001/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>}
|
173
|
+
def self.ifconfig(iface='eth0')
|
174
|
+
ret = {}
|
175
|
+
iface = iface.to_s.scan(/[0-9A-Za-z]/).join # Sanitizing input, no spaces, semicolons, etc.
|
176
|
+
case RUBY_PLATFORM
|
177
|
+
when /linux/i
|
178
|
+
ifconfig_data = %x[ifconfig #{iface}]
|
179
|
+
if ifconfig_data =~ /#{iface}/i
|
180
|
+
ifconfig_data = ifconfig_data.split(/[\s]*\n[\s]*/)
|
181
|
+
else
|
182
|
+
raise ArgumentError, "Cannot ifconfig #{iface}"
|
183
|
+
end
|
184
|
+
real_iface = ifconfig_data.first
|
185
|
+
ret[:iface] = real_iface.split.first.downcase
|
186
|
+
if real_iface =~ /[\s]HWaddr[\s]+([0-9a-fA-F:]{17})/i
|
187
|
+
ret[:eth_saddr] = $1.downcase
|
188
|
+
ret[:eth_src] = EthHeader.mac2str(ret[:eth_saddr])
|
189
|
+
end
|
190
|
+
ifconfig_data.each do |s|
|
191
|
+
case s
|
192
|
+
when /inet addr:[\s]*([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)(.*Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+))?/i
|
193
|
+
ret[:ip_saddr] = $1
|
194
|
+
ret[:ip_src] = [IPAddr.new($1).to_i].pack("N")
|
195
|
+
ret[:ip4_obj] = IPAddr.new($1)
|
196
|
+
ret[:ip4_obj] = ret[:ip4_obj].mask($3) if $3
|
197
|
+
when /inet6 addr:[\s]*([0-9a-fA-F:\x2f]+)/
|
198
|
+
ret[:ip6_saddr] = $1
|
199
|
+
ret[:ip6_obj] = IPAddr.new($1)
|
200
|
+
end
|
201
|
+
end # linux
|
202
|
+
when /darwin/i
|
203
|
+
ifconfig_data = %x[ifconfig #{iface}]
|
204
|
+
if ifconfig_data =~ /#{iface}/i
|
205
|
+
ifconfig_data = ifconfig_data.split(/[\s]*\n[\s]*/)
|
206
|
+
else
|
207
|
+
raise ArgumentError, "Cannot ifconfig #{iface}"
|
208
|
+
end
|
209
|
+
real_iface = ifconfig_data.first
|
210
|
+
ret[:iface] = real_iface.split(':')[0]
|
211
|
+
ifconfig_data.each do |s|
|
212
|
+
case s
|
213
|
+
when /ether[\s]([0-9a-fA-F:]{17})/i
|
214
|
+
ret[:eth_saddr] = $1
|
215
|
+
ret[:eth_src] = EthHeader.mac2str(ret[:eth_saddr])
|
216
|
+
when /inet[\s]*([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)(.*Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+))?/i
|
217
|
+
ret[:ip_saddr] = $1
|
218
|
+
ret[:ip_src] = [IPAddr.new($1).to_i].pack("N")
|
219
|
+
ret[:ip4_obj] = IPAddr.new($1)
|
220
|
+
ret[:ip4_obj] = ret[:ip4_obj].mask($3) if $3
|
221
|
+
when /inet6[\s]*([0-9a-fA-F:\x2f]+)/
|
222
|
+
ret[:ip6_saddr] = $1
|
223
|
+
ret[:ip6_obj] = IPAddr.new($1)
|
224
|
+
end
|
225
|
+
end # darwin
|
226
|
+
end # RUBY_PLATFORM
|
227
|
+
ret
|
228
|
+
end
|
229
|
+
|
230
|
+
end
|
222
231
|
|
223
232
|
end
|
224
233
|
|
data/lib/packetfu/version.rb
CHANGED
@@ -1,51 +1,51 @@
|
|
1
1
|
# -*- coding: binary -*-
|
2
2
|
module PacketFu
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
# Check the repo's for version release histories
|
5
|
+
VERSION = "1.1.10"
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
# Returns PacketFu::VERSION
|
8
|
+
def self.version
|
9
|
+
VERSION
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
12
|
+
# Returns a version string in a binary format for easy comparisons.
|
13
|
+
def self.binarize_version(str)
|
14
|
+
if(str.respond_to?(:split) && str =~ /^[0-9]+(\.([0-9]+)(\.[0-9]+)?)?\..+$/)
|
15
|
+
bin_major,bin_minor,bin_teeny = str.split(/\x2e/).map {|x| x.to_i}
|
16
|
+
bin_version = (bin_major.to_i << 16) + (bin_minor.to_i << 8) + bin_teeny.to_i
|
17
|
+
else
|
18
|
+
raise ArgumentError, "Compare version malformed. Should be \x22x.y.z\x22"
|
19
|
+
end
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
22
|
+
# Returns true if the version is equal to or greater than the compare version.
|
23
|
+
# If the current version of PacketFu is "0.3.1" for example:
|
24
|
+
#
|
25
|
+
# PacketFu.at_least? "0" # => true
|
26
|
+
# PacketFu.at_least? "0.2.9" # => true
|
27
|
+
# PacketFu.at_least? "0.3" # => true
|
28
|
+
# PacketFu.at_least? "1" # => true after 1.0's release
|
29
|
+
# PacketFu.at_least? "1.12" # => false
|
30
|
+
# PacketFu.at_least? "2" # => false
|
31
|
+
def self.at_least?(str)
|
32
|
+
this_version = binarize_version(self.version)
|
33
|
+
ask_version = binarize_version(str)
|
34
|
+
this_version >= ask_version
|
35
|
+
end
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
37
|
+
# Returns true if the current version is older than the compare version.
|
38
|
+
def self.older_than?(str)
|
39
|
+
return false if str == self.version
|
40
|
+
this_version = binarize_version(self.version)
|
41
|
+
ask_version = binarize_version(str)
|
42
|
+
this_version < ask_version
|
43
|
+
end
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
45
|
+
# Returns true if the current version is newer than the compare version.
|
46
|
+
def self.newer_than?(str)
|
47
|
+
return false if str == self.version
|
48
|
+
!self.older_than?(str)
|
49
|
+
end
|
50
50
|
|
51
51
|
end
|