Class: Nl::Protocols::Raw

Inherits:
Object
  • Object
show all
Defined in:
lib/nl/protocols/raw.rb

Overview

The raw Netlink protocol

Direct Known Subclasses

Genl

Defined Under Namespace

Modules: DataTypes Classes: Ack, AttributeSet, Done, Message

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, protonum) ⇒ Raw

Returns a new instance of Raw.



15
16
17
18
# File 'lib/nl/protocols/raw.rb', line 15

def initialize(name, protonum)
  @name = name
  @protonum = protonum
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/nl/protocols/raw.rb', line 13

def name
  @name
end

#protonumObject (readonly)

Returns the value of attribute protonum.



13
14
15
# File 'lib/nl/protocols/raw.rb', line 13

def protonum
  @protonum
end

Instance Method Details

#decode_message(decoder, message_class) ⇒ Object



24
25
26
27
28
29
# File 'lib/nl/protocols/raw.rb', line 24

def decode_message(decoder, message_class)
  header = NlMsgHdr.decode(decoder)
  decoder.limit(header.len - Core::NLMSG_HDRLEN) do
    message_class.decode(decoder, header)
  end
end

#encode_message(encoder, message) ⇒ Object



20
21
22
# File 'lib/nl/protocols/raw.rb', line 20

def encode_message(encoder, message)
  message.encode(encoder)
end

#exchange_message(socket, type, request_class, reply_class, args) ⇒ Object

Parameters:

  • socket (Socket)

    Netlink socket

  • type (:do, :dump)

    Request type

  • request_class (Class)

    Request message class

  • reply_class (Class)

    Reply message class

  • args (Hash)

    Request arguments



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/nl/protocols/raw.rb', line 80

def exchange_message(socket, type, request_class, reply_class, args)
  flags = Core::NLM_F_REQUEST
  flags |= type == :dump ? Core::NLM_F_DUMP : Core::NLM_F_ACK

  request = request_class.from_params(args)
  request.nlmsg_header.flags = flags
  seq_pid = send_message(socket, request)

  result = [] unless block_given?

  done = false
  begin
    recv_message(socket, seq_pid, reply_class) do |message|
      case message
      when Done, Ack
        done = true
      when Exception
        raise message
      else
        if block_given?
          yield message
        else
          result << message
        end
      end
    end
  end until done

  unless block_given?
    type == :dump ? result : result.first
  end
end

#recv_message(socket, seq_pid, message_class) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/nl/protocols/raw.rb', line 39

def recv_message(socket, seq_pid, message_class)
  data, = socket.recvmsg

  decoder = Decoder.new(IO::Buffer.for(data))
  while decoder.available?(Core::NLMSG_HDRLEN)
    header = Core::NlMsgHdr.decode(decoder)
    decoder.align_to(Core::NLMSG_ALIGNTO)
    raise binding.irb unless [header.seq, header.pid] == seq_pid
    if header.type < Core::NLMSG_MIN_TYPE
      # Control messages
      case header.type
      when Core::NLMSG_ERROR
        errno = decoder.get_value(Endian::Host::SINT)
        if errno == 0
          yield Ack.new
        else
          yield SystemCallError.new(-errno)
        end
        decoder.skip(header.len - Core::NLMSG_HDRLEN - 4)
      when Core::NLMSG_DONE
        yield Done.new
        decoder.skip(header.len - Core::NLMSG_HDRLEN)
      else
        # just ignore NLMSG_NOOP and other unknown control messages
        decoder.skip(header.len - Core::NLMSG_HDRLEN)
      end
    else
      # Subsystem-specific messages
      decoder.limit(header.len - Core::NLMSG_HDRLEN) do
        decoder.align_to(Core::NLMSG_ALIGNTO)
        yield message_class.decode(decoder, header)
      end
    end
  end
end

#send_message(socket, message) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/nl/protocols/raw.rb', line 31

def send_message(socket, message)
  seq_pid = socket.complete(message.nlmsg_header)
  encoder = Encoder.new
  encode_message(encoder, message)
  socket.sendmsg(encoder.buffer.get_string, 0, Socket.sockaddr_nl(0, 0))
  seq_pid
end