Class: Nl::Socket

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

Overview

Netlink socket

Defined Under Namespace

Modules: Constants

Constant Summary

Constants included from Constants

Constants::NETLINK_ADD_MEMBERSHIP, Constants::NETLINK_DROP_MEMBERSHIP, Constants::PF_NETLINK, Constants::SOL_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



34
35
36
# File 'nl/lib/nl/socket.rb', line 34

def initialize(protonum)
  super(PF_NETLINK, SOCK_RAW, protonum)
end

Class Method Details

.open(protonum) ⇒ Object

Opens a Netlink socket for the specified protocol number.



42
43
44
45
46
47
48
49
50
# File 'nl/lib/nl/socket.rb', line 42

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

Packs Netlink socket address



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

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

.unpack_sockaddr_nl(sockaddr) ⇒ Object

Unpacks Netlink socket address



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

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

Instance Method Details

#add_membership(group_id) ⇒ Object

Adds this socket to a Netlink multicast group.



61
62
63
# File 'nl/lib/nl/socket.rb', line 61

def add_membership(group_id)
  setsockopt(SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, group_id)
end

#drop_membership(group_id) ⇒ Object

Removes this socket from a Netlink multicast group.



68
69
70
# File 'nl/lib/nl/socket.rb', line 68

def drop_membership(group_id)
  setsockopt(SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, group_id)
end

#local_port_id ⇒ Integer

Returns Local Netlink port ID assigned to this socket.

Returns:

  • (Integer) —

    Local Netlink port ID assigned to this socket



54
55
56
# File 'nl/lib/nl/socket.rb', line 54

def local_port_id
  Socket.unpack_sockaddr_nl(local_address.to_sockaddr).first
end