addresses/ancillary_data_no_parens.rb

tests/_examples/addresses/ancillary_data_no_parens.rb in the metorex repository.

require "socket"

# A control message names its family, level and type by number or by name,
# with a type's name read within its level.
message = Socket::AncillaryData.new :INET, :IP, :TTL, "x"
p [message.family == Socket::AF_INET, message.level == Socket::IPPROTO_IP]
p message.type == Socket::IP_TTL
p message.cmsg_is? :IP, :TTL
p message

begin
  Socket::AncillaryData.new :INET, :SOCKET, :MOO, ""
rescue SocketError => problem
  p problem.message
end
begin
  Socket::AncillaryData.new :UNIX, :IP, :TTL, ""
rescue SocketError => problem
  p problem.message
end

# An integer is carried as a C int.
counted = Socket::AncillaryData.int :INET, :IP, :TTL, 64
p counted.int
p counted

# Descriptors handed on over a UNIX socket.
rights = Socket::AncillaryData.unix_rights STDOUT, STDERR
p rights.unix_rights == [STDOUT, STDERR]
p rights.data.unpack("i*") == [STDOUT.fileno, STDERR.fileno]
begin
  message.unix_rights
rescue TypeError => problem
  p problem.message
end

# Where a packet was sent and which interface took it.
source = Addrinfo.tcp "127.0.0.1", 80
target = Addrinfo.ip "127.0.0.5"
info = Socket::AncillaryData.ip_pktinfo source, 4, target
address, index, destination = info.ip_pktinfo
p [address.ip_address, address.ip_port, index, destination.ip_address]
loopback = Addrinfo.ip "::1"
info = Socket::AncillaryData.ipv6_pktinfo loopback, 3
p [info.ipv6_pktinfo_addr.ip_address, info.ipv6_pktinfo_ifindex]

Output:

[true, true]
true
true
#<Socket::AncillaryData: INET IP TTL "x">
"unknown UNIX control message: MOO"
"unknown protocol level: IP"
64
#<Socket::AncillaryData: INET IP TTL "@\x00\x00\x00">
true
true
"SCM_RIGHTS ancillary data expected"
["127.0.0.1", 0, 4, "127.0.0.5"]
["::1", 3]