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
irb(main):003:0> Socket.getaddrinfo("localhost", 12345, :AF_UNSPEC, :DGRAM) | |
=> [ | |
["AF_INET6", 12345, "::1", "::1", 10, 2, 17], | |
["AF_INET", 12345, "127.0.0.1", "127.0.0.1", 2, 2, 17] | |
] |
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
Socket.getaddrinfo(CONNECT_ADDR,CONNECT_PORT, :AF_UNSPEC, :DGRAM).each do | con | | |
af, port, hostname, ip, pf, sock_type, ipproto = con | |
Addrinfo.udp(ip, port).connect do |socket| | |
socket.send("Connected via #{af}", FLAGS) | |
message, server = socket.recvfrom(MSG_LENGTH) | |
puts message | |
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
cmol@qui-gon:~$ ruby echo_client_udp.rb | |
[IPv6] Connected via unspecified address family socket | |
[IPv6] Connected via AF_INET6 | |
[IPv4] Connected via AF_INET |
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
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 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
[["if_name", index, "ip_address"],[...]] |
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
cmol@third-host:~$ ruby src/multicast.rb | |
Using local interface enp0s3 with address fe80::a00:27ff:fe07:dea6%enp0s3 | |
========= Sending echo REQUEST from fe80::a00:27ff:fe07:dea6%enp0s3 | |
[REPLY] Hello from fe80::71c0:aa40:6d16:7c59%wlp4s0 | |
[REPLY] Hello from fe80::ade4:80af:c170:6340%enp0s31f6 |