Skip to content

Commit a4c626e

Browse files
committed
Enhance analyzer and header classes to include packet count in analysis and output formatting
1 parent 7b30330 commit a4c626e

File tree

6 files changed

+29
-51
lines changed

6 files changed

+29
-51
lines changed

lib/redhound/analyzer.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
module Redhound
44
class Analyzer
5-
def self.analyze(msg:)
6-
new(msg: msg).analyze
5+
def self.analyze(msg:, count:)
6+
new(msg:, count:).analyze
77
end
88

9-
def initialize(msg:)
9+
def initialize(msg:, count:)
1010
@msg = msg
11+
@count = count
1112
end
1213

1314
def analyze
14-
puts 'Analyzing...'
15-
ether = Header::Ether.generate(bytes: @msg.bytes[0..13])
15+
ether = Header::Ether.generate(bytes: @msg.bytes[0..13], count: @count)
1616
ether.dump
1717
return unless ether.ipv4?
1818

@@ -25,7 +25,6 @@ def analyze
2525
icmp = Header::Icmp.generate(bytes: @msg.bytes[34..])
2626
icmp.dump
2727
end
28-
puts
2928
end
3029
end
3130
end

lib/redhound/header/ether.rb

+5-9
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ class Ether
66
ETH_P_IP = 0x0800
77

88
class << self
9-
def generate(bytes:)
10-
new(bytes:).generate
9+
def generate(bytes:, count:)
10+
new(bytes:, count:).generate
1111
end
1212
end
1313

14-
def initialize(bytes:)
14+
def initialize(bytes:, count:)
1515
raise ArgumentError, 'bytes must be 14 bytes' unless bytes.size == 14
1616

1717
@bytes = bytes
18+
@count = count
1819
end
1920

2021
def generate
@@ -29,16 +30,11 @@ def ipv4?
2930
end
3031

3132
def dump
32-
puts 'ETHERNET HEADER----------------'
3333
puts self
3434
end
3535

3636
def to_s
37-
<<~ETHER
38-
Destination MAC: #{dhost}
39-
Source MAC: #{shost}
40-
Type: #{type}
41-
ETHER
37+
"[#{@count}] Ethernet Dst: #{dhost} Src: #{shost} Type: #{type}"
4238
end
4339

4440
def dhost

lib/redhound/header/icmp.rb

+7-14
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,19 @@ def generate
3131
end
3232

3333
def dump
34-
puts 'ICMP HEADER----------------'
3534
puts self
3635
end
3736

3837
def to_s
3938
if @type.zero? || @type == 8
40-
<<~ICMP
41-
Type: #{@type}
42-
Code: #{@code}
43-
Checksum: #{check}
44-
ID: #{id}
45-
Sequence: #{seq}
46-
Data: #{data}
39+
<<-ICMP.chomp
40+
└─ ICMP Type: #{@type} Code: #{@code} Checksum: #{check} ID: #{id} Sequence: #{seq}
41+
└─ Payload: #{data}
4742
ICMP
4843
else
49-
<<~ICMP
50-
Type: #{@type}
51-
Code: #{@code}
52-
Checksum: #{check}
53-
Data: #{data}
44+
<<-ICMP.chomp
45+
└─ ICMP Type: #{@type} Code: #{@code} Checksum: #{check}
46+
└─ Payload: #{data}
5447
ICMP
5548
end
5649
end
@@ -70,7 +63,7 @@ def seq
7063
end
7164

7265
def data
73-
@data.map(&:chr).join
66+
@data.map(&:chr).join.force_encoding("UTF-8")
7467
end
7568
end
7669
end

lib/redhound/header/ipv4.rb

+1-14
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,11 @@ def udp?
4343
end
4444

4545
def dump
46-
puts 'IPv4 HEADER----------------'
4746
puts self
4847
end
4948

5049
def to_s
51-
<<~IPV4
52-
Version: #{@version}
53-
IHL: #{@ihl}
54-
TOS: #{@tos}
55-
Total Length: #{tot_len}
56-
ID: #{id}
57-
Fragment Offset: #{frag_off}
58-
TTL: #{@ttl}
59-
Protocol: #{protocol}
60-
Checksum: #{check}
61-
Source IP: #{saddr}
62-
Destination IP: #{daddr}
63-
IPV4
50+
" └─ IPv4 Ver: #{version} IHL: #{ihl} TOS: #{@tos} Total Length: #{tot_len} ID: #{id} Offset: #{frag_off} TTL: #{@ttl} Protocol: #{protocol} Checksum: #{check} Src: #{saddr} Dst: #{daddr}"
6451
end
6552

6653
private

lib/redhound/header/udp.rb

+3-7
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,13 @@ def generate
2525
end
2626

2727
def dump
28-
puts 'UDP HEADER----------------'
2928
puts self
3029
end
3130

3231
def to_s
33-
<<~UDP
34-
Source Port: #{sport}
35-
Destination Port: #{dport}
36-
Length: #{len}
37-
Checksum: #{check}
38-
Data: #{data}
32+
<<-UDP
33+
└─ UDP Src: #{sport} Dst: #{dport} Len: #{len} Checksum: #{check}
34+
└─ Payload: #{data}
3935
UDP
4036
end
4137

lib/redhound/receiver.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,24 @@ def initialize(ifname:, filename:)
1717
@writer = Writer.new(filename:)
1818
@writer.start
1919
end
20+
@count = 0
2021
end
2122

2223
def run
2324
loop do
2425
msg, = @source.next_packet
25-
Analyzer.analyze(msg:)
26+
Analyzer.analyze(msg:, count: increment)
2627
@writer&.write(msg)
2728
rescue Interrupt
2829
@writer&.stop
2930
break
3031
end
3132
end
33+
34+
private
35+
36+
def increment
37+
@count.tap { @count += 1 }
38+
end
3239
end
3340
end

0 commit comments

Comments
 (0)