ipv6_recvpktinfo_test.rb

Jeremy Evans, 07/30/2011 08:35 AM

Download (925 Bytes)

 
1
require 'socket'
2
port = 10000
3
sai = Addrinfo.udp('::', port)
4
p [:server_listening_on, sai]
5
socket = Socket.new(sai.pfamily, sai.socktype, sai.protocol)
6
socket.ipv6only!
7
socket.bind(sai.family_addrinfo(sai.ip_address, port))
8
socket.setsockopt(:IPV6, Socket::IPV6_RECVPKTINFO, 1)
9
th = Thread.new {
10
  r = IO.select([socket]).first.first
11
  msg, sender_addrinfo, _, *controls = r.recvmsg_nonblock
12
  p [:server_recvmsg_nonblock_output, msg, sender_addrinfo, controls]
13
  pktinfo = controls.find {|c| c.cmsg_is?(:IPV6, :PKTINFO) }
14
  args = [msg.reverse, 0, sender_addrinfo]
15
  args << pktinfo if ARGV.last == 'pktinfo'
16
  p [:server_response_sendmsg_args, args]
17
  r.sendmsg *args
18
}
19
sleep 0.1
20
cai = Addrinfo.udp(ARGV.first, port)
21
cai.connect {|s|
22
  s.sendmsg "foo"
23
  raise "no response from #{cai.inspect}" unless IO.select([s], nil, nil, 10)
24
  msg2, addr = s.recvmsg
25
  p [:response_from_server, msg2, addr]
26
}