Skip to content

Commit 9a8ec51

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

Some content is hidden

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

51 files changed

+849
-95
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
name: rake
3+
version: '13.0'
4+
source:
5+
type: git
6+
name: ruby/gem_rbs_collection
7+
revision: f83c613ed7d038c0031f25fcbcdf12603ba36e79
8+
remote: https://github.com/ruby/gem_rbs_collection.git
9+
repo_dir: gems
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dependencies:
2+
- name: fileutils
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Rake
2+
class TaskLib
3+
include Rake::DSL
4+
end
5+
6+
class Task
7+
end
8+
9+
class TaskArguments
10+
include Enumerable[untyped]
11+
12+
def []: (untyped index) -> untyped
13+
def each: () ?{ (untyped, untyped) -> void } -> void
14+
end
15+
16+
module DSL
17+
private
18+
19+
include FileUtils
20+
21+
def desc: (String description) -> void
22+
def directory: (*untyped args) ?{ () -> void } -> void
23+
def file: (*untyped args) ?{ () -> void } -> void
24+
def file_create: (*untyped args) ?{ () -> void } -> void
25+
def import: (*String fns) -> void
26+
def multitask: (*untyped args) ?{ () -> void } -> void
27+
def namespace: (?untyped name) ?{ () -> void } -> void
28+
def rule: (*untyped args) ?{ () -> void } -> void
29+
def task: (*untyped args) ?{ (Rake::Task, Rake::TaskArguments) -> void } -> void
30+
end
31+
end
32+
33+
module FileUtils
34+
def sh: (*String cmd, **untyped options) ?{ (bool, Process::Status) -> void } -> void
35+
| (Hash[String, String] env, *String cmd, **untyped options) ?{ (bool, Process::Status) -> void } -> void
36+
def ruby: (*String args, **untyped options) ?{ (bool, Process::Status) -> void } -> void
37+
def safe_ln: (*untyped args, **untyped options) -> void
38+
def split_all: (String path) -> Array[String]
39+
end

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'

0 commit comments

Comments
 (0)