Class: Nl::Socket

Inherits:
Socket
  • Object
show all
Includes:
Constants
Defined in:
lib/nl/socket.rb

Overview

Netlink socket

Defined Under Namespace

Modules: Constants

Constant Summary

Constants included from Constants

Constants::PF_NETLINK

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(protonum) ⇒ Socket

Returns a new instance of Socket.

Parameters:

  • protonum (Integer)

    Netlink protocol number



22
23
24
25
# File 'lib/nl/socket.rb', line 22

def initialize(protonum)
  super(PF_NETLINK, SOCK_RAW, protonum)
  @seq = 0  # last-used sequence number
end

Class Method Details

.open(protonum) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/nl/socket.rb', line 27

def self.open(protonum)
  return new(protonum) unless block_given?
  begin
    socket = new(protonum)
    yield socket
  ensure
    socket&.close
  end
end

.pack_sockaddr_nl(pid, groups) ⇒ Object Also known as: sockaddr_nl



15
# File 'lib/nl/socket.rb', line 15

def pack_sockaddr_nl(pid, groups) = [Socket::AF_NETLINK, 0, pid, groups].pack('S!S!LL')

.unpack_sockaddr_nl(sockaddr) ⇒ Object



18
# File 'lib/nl/socket.rb', line 18

def unpack_sockaddr_nl(sockaddr) = sockaddr.unpack('S!S!LL')[2..3]

Instance Method Details

#complete(hdr) ⇒ Object



46
47
48
49
50
51
# File 'lib/nl/socket.rb', line 46

def complete(hdr)
  [
    hdr.seq ||= next_seq,
    hdr.pid ||= Socket.unpack_sockaddr_nl(local_address.to_sockaddr).first,
  ]
end

#next_seqInteger

Returns Get next sequence number.

Returns:

  • (Integer)

    Get next sequence number



40
41
42
43
44
# File 'lib/nl/socket.rb', line 40

def next_seq
  nseq = (@seq + 1) & 0xFFFFFFFF
  nseq = 1 if nseq == 0  # seq=0 is for notification
  @seq = nseq
end