Module: RubySL::Socket::SocketOptions

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

Class Method Summary collapse

Class Method Details

.constant(prefix, suffix) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/rubysl/socket/socket_options.rb', line 73

def self.constant(prefix, suffix)
  const = "#{prefix}_#{suffix}"

  if ::Socket.const_defined?(const)
    ::Socket.const_get(const)
  else
    raise SocketError, "Undefined socket constant: #{const}"
  end
end

.ip_level_to_int(level) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubysl/socket/socket_options.rb', line 59

def self.ip_level_to_int(level)
  prefixes = ["IPPROTO", "SOL", "IPV6"]

  prefixes.each do |prefix|
    const = "#{prefix}_#{level}"

    if ::Socket.const_defined?(const)
      return ::Socket.const_get(const)
    end
  end

  nil
end

.is_ip_family?(family) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/rubysl/socket/socket_options.rb', line 55

def self.is_ip_family?(family)
  family == "AF_INET" || family == "AF_INET6"
end

.socket_level(level, family = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubysl/socket/socket_options.rb', line 4

def self.socket_level(level, family = nil)
  if level.is_a?(Symbol) or level.is_a?(String)
    if ::Socket.const_defined?(level)
      ::Socket.const_get(level)
    else
      if family and is_ip_family?(family)
        ip_level_to_int(level)
      elsif level.to_s == 'SOCKET'
        ::Socket::SOL_SOCKET
      else
        constant("IPPROTO", level)
      end
    end
  elsif level.respond_to?(:to_str)
    socket_level(Socket.coerce_to_string(level), family)
  else
    level
  end
end

.socket_option(level, optname) ⇒ Object



24
25
26
27
28
29
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/socket_options.rb', line 24

def self.socket_option(level, optname)
  case optname
  when Symbol, String
    if ::Socket.const_defined?(optname)
      ::Socket.const_get(optname)
    else
      case level
      when ::Socket::SOL_SOCKET
        constant("SO", optname)
      when ::Socket::IPPROTO_IP
        constant("IP", optname)
      when ::Socket::IPPROTO_TCP
        constant("TCP", optname)
      when ::Socket::IPPROTO_UDP
        constant("UDP", optname)
      when ::Socket.const_defined?(:IPPROTO_IPV6) && ::Socket::IPPROTO_IPV6
        constant("IPV6", optname)
      else
        raise SocketError,
          "Unsupported socket level option name: #{optname}"
      end
    end
  else
    if optname.respond_to?(:to_str)
      socket_option(level, Socket.coerce_to_string(optname))
    else
      optname
    end
  end
end