nl 0.2.4 → 0.3.0

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.
@@ -0,0 +1,36 @@
1
+ # rbs_inline: enabled
2
+
3
+ require_relative 'error'
4
+
5
+ module Nl
6
+ # Allocates nonzero 32-bit Netlink sequence numbers.
7
+ class SequenceAllocator
8
+ MAX = 0xFFFFFFFF
9
+ private_constant :MAX
10
+
11
+ def initialize
12
+ @last = 0
13
+ end
14
+
15
+ # Returns the next sequence number not rejected by the optional block.
16
+ def next
17
+ first = advance
18
+ candidate = first
19
+
20
+ loop do
21
+ return candidate unless block_given? && yield(candidate)
22
+
23
+ candidate = advance
24
+ if candidate == first
25
+ raise ExhaustedSequenceNumber, 'all Netlink sequence numbers are in use'
26
+ end
27
+ end
28
+ end
29
+
30
+ private def advance
31
+ @last = @last == MAX ? 1 : @last + 1
32
+ end
33
+ end
34
+
35
+ private_constant :SequenceAllocator
36
+ end
data/lib/nl/socket.rb CHANGED
@@ -8,6 +8,11 @@ module Nl
8
8
  module Constants
9
9
  # From include/linux/socket.h
10
10
  PF_NETLINK = AF_NETLINK = 16
11
+
12
+ # From include/uapi/linux/netlink.h
13
+ SOL_NETLINK = 270
14
+ NETLINK_ADD_MEMBERSHIP = 1
15
+ NETLINK_DROP_MEMBERSHIP = 2
11
16
  end
12
17
  include Constants
13
18
 
@@ -21,7 +26,6 @@ module Nl
21
26
  # @param protonum [Integer] Netlink protocol number
22
27
  def initialize(protonum)
23
28
  super(PF_NETLINK, SOCK_RAW, protonum)
24
- @seq = 0 # last-used sequence number
25
29
  end
26
30
 
27
31
  def self.open(protonum)
@@ -34,20 +38,19 @@ module Nl
34
38
  end
35
39
  end
36
40
 
37
- # XXX: Should Protocol manage next_seq?
41
+ # @return [Integer] Local Netlink port ID assigned to this socket
42
+ def local_port_id
43
+ Socket.unpack_sockaddr_nl(local_address.to_sockaddr).first
44
+ end
38
45
 
39
- # @return [Integer] Get next sequence number
40
- def next_seq
41
- nseq = (@seq + 1) & 0xFFFFFFFF
42
- nseq = 1 if nseq == 0 # seq=0 is for notification
43
- @seq = nseq
46
+ # Adds this socket to a Netlink multicast group.
47
+ def add_membership(group_id)
48
+ setsockopt(SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, group_id)
44
49
  end
45
50
 
46
- def complete(hdr)
47
- [
48
- hdr.seq ||= next_seq,
49
- hdr.pid ||= Socket.unpack_sockaddr_nl(local_address.to_sockaddr).first,
50
- ]
51
+ # Removes this socket from a Netlink multicast group.
52
+ def drop_membership(group_id)
53
+ setsockopt(SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, group_id)
51
54
  end
52
55
  end
53
56
  end
data/lib/nl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nl
2
- VERSION = '0.2.4'
2
+ VERSION = '0.3.0'
3
3
  end
data/lib/nl.rb CHANGED
@@ -1,14 +1,13 @@
1
- require_relative 'nl/version'
1
+ require_relative 'nl/error'
2
2
  require_relative 'nl/core'
3
+ require_relative 'nl/notification'
4
+ require_relative 'nl/connection'
5
+ require_relative 'nl/family'
3
6
  require_relative 'nl/genl'
4
7
  require_relative 'nl/socket'
5
- require_relative 'nl/family'
6
- require_relative 'nl/protocols/raw'
7
- require_relative 'nl/protocols/genl'
8
+ require_relative 'nl/version'
8
9
 
9
10
  module Nl
10
- class Error < StandardError; end
11
-
12
11
  include Core
13
12
  include Genl
14
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasumi Hanazuki
@@ -9,7 +9,8 @@ bindir: exe
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
- description: Linux Netlink client
12
+ description: nl is a general-purpose Netlink implementation for building Ruby applications
13
+ that communicate with Linux kernel APIs or other userspace processes.
13
14
  email:
14
15
  - kasumi@rollingapple.net
15
16
  executables: []
@@ -22,14 +23,28 @@ files:
22
23
  - README.md
23
24
  - Rakefile
24
25
  - lib/nl.rb
26
+ - lib/nl/async.rb
27
+ - lib/nl/async/dispatcher.rb
28
+ - lib/nl/async/driver.rb
29
+ - lib/nl/async/mailbox.rb
30
+ - lib/nl/async/operation.rb
31
+ - lib/nl/blocking_transport.rb
32
+ - lib/nl/connection.rb
25
33
  - lib/nl/core.rb
34
+ - lib/nl/datagram.rb
26
35
  - lib/nl/decoder.rb
27
36
  - lib/nl/encoder.rb
28
37
  - lib/nl/endian.rb
38
+ - lib/nl/error.rb
39
+ - lib/nl/exchange.rb
29
40
  - lib/nl/family.rb
30
41
  - lib/nl/genl.rb
42
+ - lib/nl/genl/connection.rb
43
+ - lib/nl/notification.rb
44
+ - lib/nl/notification_router.rb
31
45
  - lib/nl/protocols/genl.rb
32
46
  - lib/nl/protocols/raw.rb
47
+ - lib/nl/sequence_allocator.rb
33
48
  - lib/nl/socket.rb
34
49
  - lib/nl/version.rb
35
50
  homepage: https://github.com/hanazuki/nl
@@ -37,7 +52,7 @@ licenses:
37
52
  - MIT
38
53
  metadata:
39
54
  homepage_uri: https://github.com/hanazuki/nl
40
- source_code_uri: https://github.com/hanazuki/nl/tree/v0.2.4
55
+ source_code_uri: https://github.com/hanazuki/nl/tree/v0.3.0
41
56
  changelog_uri: https://github.com/hanazuki/nl/blob/master/CHANGELOG.md
42
57
  rdoc_options: []
43
58
  require_paths:
@@ -46,7 +61,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
46
61
  requirements:
47
62
  - - ">="
48
63
  - !ruby/object:Gem::Version
49
- version: '3.1'
64
+ version: '3.4'
50
65
  required_rubygems_version: !ruby/object:Gem::Requirement
51
66
  requirements:
52
67
  - - ">="
@@ -55,5 +70,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
70
  requirements: []
56
71
  rubygems_version: 4.0.6
57
72
  specification_version: 4
58
- summary: Linux Netlink client
73
+ summary: Netlink protocol support for Ruby
59
74
  test_files: []