Module: RubySL::Socket::AncillaryData

Defined in:
lib/rubysl/socket/ancillary_data.rb

Constant Summary collapse

LEVEL_PREFIXES =
{
  ::Socket::SOL_SOCKET   => %w{SCM_ UNIX},
  ::Socket::IPPROTO_IP   => %w{IP_ IP},
  ::Socket::IPPROTO_IPV6 => %w{IPV6_ IPV6},
  ::Socket::IPPROTO_TCP  => %w{TCP_ TCP},
  ::Socket::IPPROTO_UDP  => %w{UDP_ UDP}
}

Class Method Summary collapse

Class Method Details

.level(raw_level) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rubysl/socket/ancillary_data.rb', line 12

def self.level(raw_level)
  if raw_level.is_a?(Fixnum)
    raw_level
  else
    level = Socket.coerce_to_string(raw_level)

    if level == 'SOL_SOCKET' or level == 'SOCKET'
      ::Socket::SOL_SOCKET

    # Translates "TCP" into "IPPROTO_TCP", "UDP" into "IPPROTO_UDP", etc.
    else
      Socket.prefixed_socket_constant(level, 'IPPROTO_') do
        "unknown protocol level: #{level}"
      end
    end
  end
end

.type(family, level, raw_type) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubysl/socket/ancillary_data.rb', line 30

def self.type(family, level, raw_type)
  if raw_type.is_a?(Fixnum)
    raw_type
  else
    type = Socket.coerce_to_string(raw_type)

    if family == ::Socket::AF_INET or family == ::Socket::AF_INET6
      prefix, label = LEVEL_PREFIXES[level]
    else
      prefix, label = LEVEL_PREFIXES[::Socket::SOL_SOCKET]
    end

    # Translates "RIGHTS" into "SCM_RIGHTS", "CORK" into "TCP_CORK" (when
    # the level is IPPROTO_TCP), etc.
    if prefix and label
      Socket.prefixed_socket_constant(type, prefix) do
        "Unknown #{label} control message: #{type}"
      end
    else
      raise TypeError,
        "no implicit conversion of #{type.class} into Integer"
    end
  end
end