Class: Socket::AncillaryData
- Inherits:
-
Object
- Object
- Socket::AncillaryData
- Defined in:
- lib/socket/ancillary_data.rb
Instance Attribute Summary collapse
-
#family ⇒ Object
readonly
Returns the value of attribute family.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .int(family, level, type, integer) ⇒ Object
- .ip_pktinfo(addr, ifindex, spec_dst = nil) ⇒ Object
- .ipv6_pktinfo(addr, ifindex) ⇒ Object
- .unix_rights(*ios) ⇒ Object
Instance Method Summary collapse
- #cmsg_is?(level, type) ⇒ Boolean
- #data ⇒ Object
-
#initialize(family, level, type, data) ⇒ AncillaryData
constructor
A new instance of AncillaryData.
- #int ⇒ Object
- #ip_pktinfo ⇒ Object
- #ipv6_pktinfo ⇒ Object
- #ipv6_pktinfo_addr ⇒ Object
- #ipv6_pktinfo_ifindex ⇒ Object
- #unix_rights ⇒ Object
Constructor Details
#initialize(family, level, type, data) ⇒ AncillaryData
Returns a new instance of AncillaryData.
52 53 54 55 56 57 |
# File 'lib/socket/ancillary_data.rb', line 52 def initialize(family, level, type, data) @family = RubySL::Socket.address_family(family) @data = RubySL::Socket.coerce_to_string(data) @level = RubySL::Socket::AncillaryData.level(level) @type = RubySL::Socket::AncillaryData.type(@family, @level, type) end |
Instance Attribute Details
#family ⇒ Object (readonly)
Returns the value of attribute family.
3 4 5 |
# File 'lib/socket/ancillary_data.rb', line 3 def family @family end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
3 4 5 |
# File 'lib/socket/ancillary_data.rb', line 3 def level @level end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/socket/ancillary_data.rb', line 3 def type @type end |
Class Method Details
.int(family, level, type, integer) ⇒ Object
5 6 7 |
# File 'lib/socket/ancillary_data.rb', line 5 def self.int(family, level, type, integer) new(family, level, type, [integer].pack('I')) end |
.ip_pktinfo(addr, ifindex, spec_dst = nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/socket/ancillary_data.rb', line 28 def self.ip_pktinfo(addr, ifindex, spec_dst = nil) spec_dst ||= addr instance = new(:INET, :IP, :PKTINFO, '') pkt_info = [ Addrinfo.ip(addr.ip_address), ifindex, Addrinfo.ip(spec_dst.ip_address) ] instance.instance_variable_set(:@ip_pktinfo, pkt_info) instance end |
.ipv6_pktinfo(addr, ifindex) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/socket/ancillary_data.rb', line 43 def self.ipv6_pktinfo(addr, ifindex) instance = new(:INET6, :IPV6, :PKTINFO, '') pkt_info = [Addrinfo.ip(addr.ip_address), ifindex] instance.instance_variable_set(:@ipv6_pktinfo, pkt_info) instance end |
.unix_rights(*ios) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/socket/ancillary_data.rb', line 9 def self.unix_rights(*ios) descriptors = ios.map do |io| unless io.is_a?(IO) raise TypeError, "IO expected, got #{io.class} instead" end io.fileno end instance = new(:UNIX, :SOCKET, :RIGHTS, descriptors.pack('I*')) # MRI sets this using a hidden instance variable ("unix_rights"). Because # you can't set hidden instance variables from within Ruby we'll have to # use a regular instance variable. Lets hope people don't mess with it. instance.instance_variable_set(:@unix_rights, ios) instance end |
Instance Method Details
#cmsg_is?(level, type) ⇒ Boolean
59 60 61 62 63 64 |
# File 'lib/socket/ancillary_data.rb', line 59 def cmsg_is?(level, type) level = RubySL::Socket::AncillaryData.level(level) type = RubySL::Socket::AncillaryData.type(@family, level, type) @level == level && @type == type end |
#data ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/socket/ancillary_data.rb', line 84 def data if @ip_pktinfo or @ipv6_pktinfo raise NotImplementedError, 'AncillaryData#data is not supported as its output depends on ' \ 'MRI specific internals, use #ip_pktinfo or #ipv6_pktinfo instead' else @data end end |
#int ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/socket/ancillary_data.rb', line 66 def int unpacked = @data.unpack('I')[0] unless unpacked raise TypeError, 'data could not be unpacked into a Fixnum' end unpacked end |
#ip_pktinfo ⇒ Object
94 95 96 97 98 |
# File 'lib/socket/ancillary_data.rb', line 94 def ip_pktinfo addr, ifindex, spec = @ip_pktinfo [addr.dup, ifindex, spec.dup] end |
#ipv6_pktinfo ⇒ Object
100 101 102 103 104 |
# File 'lib/socket/ancillary_data.rb', line 100 def ipv6_pktinfo addr, ifindex = @ipv6_pktinfo [addr.dup, ifindex] end |
#ipv6_pktinfo_addr ⇒ Object
106 107 108 |
# File 'lib/socket/ancillary_data.rb', line 106 def ipv6_pktinfo_addr ipv6_pktinfo[0] end |
#ipv6_pktinfo_ifindex ⇒ Object
110 111 112 |
# File 'lib/socket/ancillary_data.rb', line 110 def ipv6_pktinfo_ifindex ipv6_pktinfo[1] end |