Skip to content

Commit 2ae98d0

Browse files
committed
Add RBS inline annotations and generated RBS files for Redhound classes
1 parent 9aef178 commit 2ae98d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+802
-95
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/.bundle/
22
/.yardoc
3+
/.gem_rbs_collection/
34
/_yardoc/
5+
/.irbrc
6+
/.vscode/
47
/coverage/
58
/doc/
69
/pkg/

Steepfile

+4-32
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,4 @@
1-
# D = Steep::Diagnostic
2-
#
3-
# target :lib do
4-
# signature "sig"
5-
# ignore_signature "sig/test"
6-
#
7-
# check "lib" # Directory name
8-
# check "path/to/source.rb" # File name
9-
# check "app/models/**/*.rb" # Glob
10-
# # ignore "lib/templates/*.rb"
11-
#
12-
# # library "pathname" # Standard libraries
13-
# # library "strong_json" # Gems
14-
#
15-
# # configure_code_diagnostics(D::Ruby.default) # `default` diagnostics setting (applies by default)
16-
# # configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
17-
# # configure_code_diagnostics(D::Ruby.lenient) # `lenient` diagnostics setting
18-
# # configure_code_diagnostics(D::Ruby.silent) # `silent` diagnostics setting
19-
# # configure_code_diagnostics do |hash| # You can setup everything yourself
20-
# # hash[D::Ruby::NoMethod] = :information
21-
# # end
22-
# end
23-
24-
# target :test do
25-
# unreferenced! # Skip type checking the `lib` code when types in `test` target is changed
26-
# signature "sig/test" # Put RBS files for tests under `sig/test`
27-
# check "test" # Type check Ruby scripts under `test`
28-
#
29-
# configure_code_diagnostics(D::Ruby.lenient) # Weak type checking for test code
30-
#
31-
# # library "pathname" # Standard libraries
32-
# end
1+
target :lib do
2+
signature "sig"
3+
check "lib"
4+
end

lib/redhound/analyzer.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
1+
# rbs_inline: enabled
12
# frozen_string_literal: true
23

34
module Redhound
45
class Analyzer
6+
# @rbs (msg: String, count: Integer) -> void
57
def self.analyze(msg:, count:)
68
new(msg:, count:).analyze
79
end
810

11+
# @rbs (msg: String, count: Integer) -> void
912
def initialize(msg:, count:)
1013
@msg = msg
1114
@count = count
1215
end
1316

17+
# @rbs () -> void
1418
def analyze
1519
l2 = L2::Ether.generate(bytes: @msg.bytes[0..], count: @count)
1620
l2.dump
1721
return unless l2.supported_type?
1822

1923
l3 = L3::Resolver.resolve(bytes: @msg.bytes[l2.size..], l2:)
24+
return if !l3 || @msg.bytes.size <= l2.size + l3.size
2025
l3.dump
21-
return if @msg.bytes.size <= l2.size + l3.size
2226
unless l3.supported_protocol?
2327
puts " └─ Unsupported protocol #{l3.protocol}"
2428
return
2529
end
2630

27-
L4::Resolver.resolve(bytes: @msg.bytes[(l2.size + l3.size)..], l3: l3).dump
31+
L4::Resolver.resolve(bytes: @msg.bytes[(l2.size + l3.size)..], l3:)&.dump
2832
end
2933
end
3034
end

lib/redhound/builder/packet_mreq.rb

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# rbs_inline: enabled
12
# frozen_string_literal: true
23

34
require 'socket'
@@ -7,6 +8,7 @@ module Builder
78
class PacketMreq
89
PACKET_MR_PROMISC = 0x0001 # NOTE: netpacket/packet.h
910

11+
# @rbs (ifname: String) -> void
1012
def initialize(ifname:)
1113
@ifname = ifname
1214
end
@@ -18,28 +20,34 @@ def initialize(ifname:)
1820
# unsigned short mr_alen; /* address length */
1921
# unsigned char mr_address[8]; /* physical-layer address */
2022
# };
23+
# @rbs () -> String
2124
def build
2225
mr_ifindex + mr_type + mr_alen + mr_address
2326
end
2427

28+
# @rbs () -> String
2529
def mr_ifindex
2630
@mr_ifindex ||= [[index].pack('I')].pack('a4')
2731
end
2832

2933
private
3034

35+
# @rbs () -> Integer?
3136
def index
3237
::Socket.getifaddrs.find { |ifaddr| ifaddr.name == @ifname }&.ifindex
3338
end
3439

40+
# @rbs () -> String
3541
def mr_type
3642
@mr_type ||= [PACKET_MR_PROMISC].pack('S')
3743
end
3844

45+
# @rbs () -> String
3946
def mr_alen
4047
@mr_alen ||= [0].pack('S')
4148
end
4249

50+
# @rbs () -> String
4351
def mr_address
4452
@mr_address ||= [0].pack('C') * 8
4553
end

lib/redhound/builder/socket.rb

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# rbs_inline: enabled
12
# frozen_string_literal: true
23

34
require 'socket'
@@ -11,15 +12,18 @@ class Socket
1112
PACKED_ETH_P_ALL = [ETH_P_ALL].pack('S').unpack1('S>')
1213

1314
class << self
15+
# @rbs (ifname: String) -> Redhound::Source::Socket
1416
def build(ifname:)
1517
new(ifname:).build
1618
end
1719
end
1820

21+
# @rbs (ifname: String) -> void
1922
def initialize(ifname:)
2023
@mq_req = PacketMreq.new(ifname:)
2124
end
2225

26+
# @rbs () -> Redhound::Source::Socket
2327
def build
2428
socket = ::Socket.new(::Socket::AF_PACKET, ::Socket::SOCK_RAW, ETH_P_ALL)
2529
socket.bind([::Socket::AF_PACKET, PACKED_ETH_P_ALL, @mq_req.mr_ifindex].pack('SS>a16'))

lib/redhound/command.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
# rbs_inline: enabled
12
# frozen_string_literal: true
23

34
require 'optparse'
45
require 'socket'
56

67
module Redhound
78
class Command
9+
# @rbs () -> void
810
def initialize
911
@options = { ifname: nil }
1012
end
1113

14+
# @rbs (Array[untyped] argv) -> void
1215
def run(argv)
1316
parse(argv)
1417
if @options[:ifname].nil?
@@ -18,8 +21,9 @@ def run(argv)
1821
Receiver.run(ifname: @options[:ifname], filename: @options[:filename])
1922
end
2023

24+
# @rbs (Array[untyped] argv) -> void
2125
def parse(argv)
22-
OptionParser.new do |o|
26+
OptionParser.new do |o| # steep:ignore
2327
o.banner = <<~'BANNER' + <<~BANNER2
2428
___ ____ __
2529
/ _ \___ ___/ / / ___ __ _____ ___/ /
@@ -55,6 +59,7 @@ def parse(argv)
5559

5660
private
5761

62+
# @rbs () -> void
5863
def list_interfaces
5964
::Socket.getifaddrs.each { |ifaddr| puts ifaddr.name }
6065
end

lib/redhound/l2/ether.rb

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,65 @@
1+
# rbs_inline: enabled
12
# frozen_string_literal: true
23

34
module Redhound
45
class L2
56
class Ether
6-
attr_reader :type
7+
attr_reader :type #: Protocol
78

89
class << self
10+
# @rbs (bytes: Array[Integer], count: Integer) -> Redhound::L2::Ether
911
def generate(bytes:, count:)
1012
new(bytes:, count:).generate
1113
end
1214
end
1315

16+
# @rbs (bytes: Array[Integer], count: Integer) -> void
1417
def initialize(bytes:, count:)
1518
raise ArgumentError, "bytes must be #{size} bytes" unless bytes.size >= size
1619

1720
@bytes = bytes
1821
@count = count
1922
end
2023

24+
# @rbs () -> Integer
2125
def size = 14
2226

27+
# @rbs () -> Redhound::L2::Ether
2328
def generate
2429
@dhost = @bytes[0..5]
2530
@shost = @bytes[6..11]
2631
@type = Protocol.new(protocol: hex_type(@bytes[12..13]))
2732
self
2833
end
2934

35+
# @rbs () -> void
3036
def dump
3137
puts self
3238
end
3339

40+
# @rbs () -> String
3441
def to_s
3542
"[#{@count}] Ethernet Dst: #{dhost} Src: #{shost} Type: #{@type}"
3643
end
3744

45+
# @rbs () -> bool
46+
def supported_type?
47+
@type.ipv4? || @type.ipv6? || @type.arp? # steep:ignore
48+
end
49+
50+
private
51+
52+
# @rbs () -> String
3853
def dhost
3954
@dhost.map { |b| b.to_s(16).rjust(2, '0') }.join(':')
4055
end
4156

57+
# @rbs () -> String
4258
def shost
4359
@shost.map { |b| b.to_s(16).rjust(2, '0') }.join(':')
4460
end
4561

46-
def supported_type?
47-
@type.ipv4? || @type.ipv6? || @type.arp?
48-
end
49-
50-
private
51-
62+
# @rbs (Array[Integer] type) -> Integer
5263
def hex_type(type)
5364
type.map { |b| b.to_s(16).rjust(2, '0') }.join.to_i(16)
5465
end

lib/redhound/l2/protocol.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# rbs_inline: enabled
12
# frozen_string_literal: true
23

34
module Redhound
@@ -11,10 +12,12 @@ class Protocol
1112
0x8100 => 'VLAN',
1213
}
1314

15+
# @rbs (protocol: Integer) -> void
1416
def initialize(protocol:)
1517
@protocol = protocol
1618
end
1719

20+
# @rbs () -> String
1821
def to_s
1922
PROTO_TABLE[@protocol] || 'Unknown'
2023
end

lib/redhound/l3.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require_relative 'l3/arp'
4+
require_relative 'l3/base'
45
require_relative 'l3/ipv4'
56
require_relative 'l3/ipv6'
67
require_relative 'l3/protocol'

lib/redhound/l3/arp.rb

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1+
# rbs_inline: enabled
12
# frozen_string_literal: true
23

34
module Redhound
45
class L3
5-
class Arp
6+
class Arp < Base
67
class << self
8+
# @rbs (bytes: Array[Integer]) -> Redhound::L3::Arp
79
def generate(bytes:)
810
new(bytes:).generate
911
end
1012
end
1113

14+
# @rbs (bytes: Array[Integer]) -> void
1215
def initialize(bytes:)
1316
raise ArgumentError, "bytes must be bigger than #{arp_size} bytes" unless bytes.size >= arp_size
1417

1518
@bytes = bytes
1619
end
1720

21+
# @rbs () -> Redhound::L3::Arp
1822
def generate
1923
@htype = @bytes[0..1]
2024
@ptype = @bytes[2..3]
@@ -30,8 +34,10 @@ def generate
3034
self
3135
end
3236

37+
# @rbs () -> Integer
3338
def arp_size = 28
3439

40+
# @rbs () -> Integer
3541
def size
3642
if @l3.nil?
3743
arp_size
@@ -40,52 +46,60 @@ def size
4046
end
4147
end
4248

43-
def dump
44-
puts self
45-
end
46-
49+
# @rbs () -> String
4750
def to_s
4851
" └─ ARP HType: #{htype} PType: #{ptype} HLen: #{@hlen} PLen: #{@plen} Oper: #{oper} SHA: #{sha} SPA: #{spa} THA: #{tha} TPA: #{tpa}"
4952
end
5053

54+
# @rbs () -> bool
5155
def supported_protocol?
52-
@l3.supported_protocol? if @l3
56+
return false if @l3.nil?
57+
@l3.supported_protocol?
5358
end
5459

60+
# @rbs () -> String?
5561
def protocol
5662
@l3.protocol if @l3
5763
end
5864

5965
private
6066

67+
# @rbs () -> Integer
6168
def htype
6269
@htype.map { |b| b.to_s(16).rjust(2, '0') }.join.to_i(16)
6370
end
6471

72+
# @rbs () -> Integer
6573
def ptype
6674
@ptype.map { |b| b.to_s(16).rjust(2, '0') }.join.to_i(16)
6775
end
6876

77+
# @rbs () -> Integer
6978
def oper
7079
@oper.map { |b| b.to_s(16).rjust(2, '0') }.join.to_i(16)
7180
end
7281

82+
# @rbs () -> String
7383
def sha
7484
@sha.map { |b| b.to_s(16).rjust(2, '0') }.join(':')
7585
end
7686

87+
# @rbs () -> String
7788
def spa
7889
@spa.map { |b| b.to_s(16).rjust(2, '0') }.join('.')
7990
end
8091

92+
# @rbs () -> String
8193
def tha
8294
@tha.map { |b| b.to_s(16).rjust(2, '0') }.join(':')
8395
end
8496

97+
# @rbs () -> String
8598
def tpa
8699
@tpa.map { |b| b.to_s(16).rjust(2, '0') }.join('.')
87100
end
88101

102+
# @rbs () -> Redhound::L3::Base?
89103
def generate_l3
90104
return if @bytes.size == arp_size
91105

0 commit comments

Comments
 (0)