File tree 7 files changed +107
-17
lines changed
7 files changed +107
-17
lines changed Original file line number Diff line number Diff line change @@ -12,20 +12,33 @@ def initialize(msg:, count:)
12
12
end
13
13
14
14
def analyze
15
- ether = Header ::Ether . generate ( bytes : @msg . bytes [ 0 ..13 ] , count : @count )
16
- ether . dump
17
- return unless ether . type . ipv4 ?
15
+ l2 = Header ::Ether . generate ( bytes : @msg . bytes [ 0 ..] , count : @count )
16
+ l2 . dump
17
+ return unless l2 . supported_type ?
18
18
19
- ip = Header ::Ipv4 . generate ( bytes : @msg . bytes [ 14 ..33 ] )
20
- ip . dump
21
- if ip . protocol . udp?
22
- udp = Header ::Udp . generate ( bytes : @msg . bytes [ 34 ..] )
23
- udp . dump
24
- elsif ip . protocol . icmp?
25
- icmp = Header ::Icmp . generate ( bytes : @msg . bytes [ 34 ..] )
26
- icmp . dump
27
- else
28
- puts " └─ Unknown protocol #{ ip . protocol } "
19
+ l3 = layer3_header ( l2 , l2 . size )
20
+ l3 . dump
21
+ unless l3 . supported_protocol?
22
+ puts " └─ Unsupported protocol #{ l3 . protocol } "
23
+ return
24
+ end
25
+
26
+ layer4_header ( l3 , l2 . size + l3 . size ) . dump
27
+ end
28
+
29
+ def layer3_header ( l2 , offset )
30
+ if l2 . type . ipv4?
31
+ Header ::Ipv4 . generate ( bytes : @msg . bytes [ offset ..] )
32
+ elsif l2 . type . ipv6?
33
+ Header ::Ipv6 . generate ( bytes : @msg . bytes [ offset ..] )
34
+ end
35
+ end
36
+
37
+ def layer4_header ( l3 , offset )
38
+ if l3 . protocol . udp?
39
+ Header ::Udp . generate ( bytes : @msg . bytes [ offset ..] )
40
+ elsif l3 . protocol . icmp?
41
+ Header ::Icmp . generate ( bytes : @msg . bytes [ offset ..] )
29
42
end
30
43
end
31
44
end
Original file line number Diff line number Diff line change 4
4
require_relative 'header/ethernet_protocol'
5
5
require_relative 'header/icmp'
6
6
require_relative 'header/ipv4'
7
+ require_relative 'header/ipv6'
7
8
require_relative 'header/internet_protocol'
8
9
require_relative 'header/udp'
Original file line number Diff line number Diff line change @@ -12,12 +12,14 @@ def generate(bytes:, count:)
12
12
end
13
13
14
14
def initialize ( bytes :, count :)
15
- raise ArgumentError , ' bytes must be 14 bytes' unless bytes . size == 14
15
+ raise ArgumentError , " bytes must be #{ size } bytes" unless bytes . size >= size
16
16
17
17
@bytes = bytes
18
18
@count = count
19
19
end
20
20
21
+ def size = 14
22
+
21
23
def generate
22
24
@dhost = @bytes [ 0 ..5 ]
23
25
@shost = @bytes [ 6 ..11 ]
@@ -41,6 +43,10 @@ def shost
41
43
@shost . map { |b | b . to_s ( 16 ) . rjust ( 2 , '0' ) } . join ( ':' )
42
44
end
43
45
46
+ def supported_type?
47
+ @type . ipv4? || @type . ipv6?
48
+ end
49
+
44
50
private
45
51
46
52
def hex_type ( type )
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ def generate(bytes:)
10
10
end
11
11
12
12
def initialize ( bytes :)
13
- raise ArgumentError , ' bytes must be bigger than 8 bytes' unless bytes . size >= 8
13
+ raise ArgumentError , " bytes must be bigger than #{ size } bytes" unless bytes . size >= size
14
14
15
15
@bytes = bytes
16
16
end
@@ -30,6 +30,8 @@ def generate
30
30
self
31
31
end
32
32
33
+ def size = 8
34
+
33
35
def dump
34
36
puts self
35
37
end
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ def generate(bytes:)
12
12
attr_reader :protocol
13
13
14
14
def initialize ( bytes :)
15
- raise ArgumentError , ' bytes must be 20 bytes' unless bytes . size == 20
15
+ raise ArgumentError , " bytes must be #{ size } bytes" unless bytes . size >= size
16
16
17
17
@bytes = bytes
18
18
end
@@ -32,6 +32,8 @@ def generate
32
32
self
33
33
end
34
34
35
+ def size = 20
36
+
35
37
def dump
36
38
puts self
37
39
end
@@ -40,6 +42,10 @@ def to_s
40
42
" └─ 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 } "
41
43
end
42
44
45
+ def supported_protocol?
46
+ @protocol . udp? || @protocol . icmp?
47
+ end
48
+
43
49
private
44
50
45
51
def version
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ module Redhound
4
+ class Header
5
+ class Ipv6
6
+ class << self
7
+ def generate ( bytes :)
8
+ new ( bytes :) . generate
9
+ end
10
+
11
+ attr_reader :protocol
12
+
13
+ def initialize ( bytes :)
14
+ raise ArgumentError , "bytes must be bigger than #{ header_size } bytes" unless bytes . size >= header_size
15
+
16
+ @bytes = bytes
17
+ end
18
+
19
+ def size = 40
20
+
21
+ def generate
22
+ version_traffic_flow = @bytes [ 0 ..3 ] . unpack ( 'N' )
23
+ @version = ( version_traffic_flow >> 28 ) & 0xF
24
+ @traffic_class = ( version_traffic_flow >> 20 ) & 0xFF
25
+ @flow_label = version_traffic_flow & 0xFFFFF
26
+ @payload_length = @bytes [ 4 ..5 ]
27
+ @next_header = @bytes [ 6 ]
28
+ @hop_limit = @bytes [ 7 ]
29
+ @saddr = @bytes [ 8 ..23 ]
30
+ @daddr = @bytes [ 24 ..39 ]
31
+ @protocol = InternetProtocol . new ( protocol : @next_header )
32
+ end
33
+
34
+ def dump
35
+ puts self
36
+ end
37
+
38
+ def to_s
39
+ " └─ IPv6 Ver: #{ version } Traffic Class: #{ traffic_class } Flow Label: #{ flow_label } Payload Length: #{ payload_length } Next Header: #{ @protocol } Hop Limit: #{ hop_limit } Src: #{ saddr } Dst: #{ daddr } "
40
+ end
41
+
42
+ def supported_protocol?
43
+ @protocol . udp?
44
+ end
45
+
46
+ def payload_length
47
+ @payload_length . map { |b | b . to_s ( 16 ) . rjust ( 2 , '0' ) } . join . to_i ( 16 )
48
+ end
49
+
50
+ def saddr
51
+ @saddr . map { |b | b . to_s ( 16 ) . rjust ( 2 , '0' ) } . join ( ':' )
52
+ end
53
+
54
+ def daddr
55
+ @daddr . map { |b | b . to_s ( 16 ) . rjust ( 2 , '0' ) } . join ( ':' )
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ def generate(bytes:)
10
10
end
11
11
12
12
def initialize ( bytes :)
13
- raise ArgumentError , ' bytes must be bigger than 8 bytes' unless bytes . size >= 8
13
+ raise ArgumentError , " bytes must be bigger than #{ size } bytes" unless bytes . size >= size
14
14
15
15
@bytes = bytes
16
16
end
@@ -24,6 +24,8 @@ def generate
24
24
self
25
25
end
26
26
27
+ def size = 8
28
+
27
29
def dump
28
30
puts self
29
31
end
You can’t perform that action at this time.
0 commit comments