This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
require 'ipaddr' | |
def get_interface_info(name) | |
ifaddrs = Socket.getifaddrs.reject do |ifaddr| | |
!ifaddr.addr&.ipv6_linklocal? || (ifaddr.flags & Socket::IFF_MULTICAST == 0) | |
end | |
ifaddrs.select! {|ifaddr| ifaddr.name == name } if name | |
ifaddrs.map {|ifaddr| [ifaddr.name, ifaddr.ifindex, ifaddr.addr.ip_address] } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_socket(multicast_addr, multicast_port, ifindex) | |
# Create, bind, and return a UDP multicast socket | |
UDPSocket.new(Socket::AF_INET6).tap do | s | | |
ip = IPAddr.new(multicast_addr).hton + [ifindex].pack('I') | |
s.setsockopt(Socket::IPPROTO_IPV6, Socket::IPV6_JOIN_GROUP, ip) | |
s.setsockopt(Socket::IPPROTO_IPV6, Socket::IPV6_MULTICAST_HOPS, [1].pack('I')) | |
s.setsockopt(Socket::IPPROTO_IPV6, Socket::IPV6_MULTICAST_IF, [ifindex].pack('I')) | |
s.bind("::", multicast_port) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "socket" | |
LISTEN_ADDR = "::" | |
LISTEN_PORT = 12345 | |
MSG_LENGTH = 256 | |
FLAGS = 0 | |
# Create socket and bind it to the listen on all addresses and the given port | |
server_socket = UDPSocket.new :INET6 | |
server_socket.bind(LISTEN_ADDR, LISTEN_PORT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addr_info = Addrinfo.new(client) | |
puts "Client connected from #{addr_info.ip_address} using " + | |
"#{addr_info.ipv6_v4mapped? ? "IPv4" : "IPv6"}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def echo_listener(socket, linklocal_addr, multicast_addr, multicast_port, | |
flags, msg_length) | |
loop do | |
# Listen for messages of up to specified length | |
message, client = socket.recvfrom(msg_length) | |
# Extract client information given as array and log connection | |
addr_info = Addrinfo.new(client) | |
# We are not interested in messages from our selves |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_interface_info(name) | |
ifaddrs = Socket.getifaddrs.reject do |ifaddr| | |
!ifaddr.addr&.ipv6_linklocal? || (ifaddr.flags & Socket::IFF_MULTICAST == 0) | |
end | |
ifaddrs.select! {|ifaddr| ifaddr.name == name } if name | |
ifaddrs.map {|ifaddr| [ifaddr.name, ifaddr.ifindex, ifaddr.addr.ip_address] } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
message, client = server_socket.recvfrom(MSG_LENGTH) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "socket" | |
CONNECT_ADDR = "localhost" | |
CONNECT_PORT = 12345 | |
MSG_LENGTH = 256 | |
FLAGS = 0 | |
Addrinfo.udp(CONNECT_ADDR, CONNECT_PORT).connect do |socket| | |
socket.send("Connected via unspecified address family socket", FLAGS) | |
message, server = socket.recvfrom(MSG_LENGTH) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmol@qui-gon:~$ ruby echo_client_udp.rb | |
[IPv6] Connected via unspecified address family socket |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmol@qui-gon:~$ nc -4 -u localhost 12345 | |
Hello from IPv4! | |
[IPv4] Hello from IPv4! | |
cmol@qui-gon:~$ nc -6 -u localhost 12345 | |
Hello from IPv6! | |
[IPv6] Hello from IPv6! |
NewerOlder