-
Notifications
You must be signed in to change notification settings - Fork 468
Implement join/leave_multicast_group()
for IPv6
#914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6c06cd9
Update PR #602: implement `join_multicast_group()` for IPv6
cf1744a
IPv6: implement `leave_multicast_group()`
d52a196
Apply rustfmt on igmp.rs
307952c
Remove `Ipv6NotSupported` from `enum MulticastError`
lucasvr db25ee4
Address code review suggestions
lucasvr 4d25db8
Address code review
lucasvr d0fd2c8
Update unit test
lucasvr 20aa66b
rustfmt
lucasvr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
mod utils; | ||
|
||
use std::os::unix::io::AsRawFd; | ||
|
||
use smoltcp::iface::{Config, Interface, SocketSet}; | ||
use smoltcp::phy::wait as phy_wait; | ||
use smoltcp::phy::{Device, Medium}; | ||
use smoltcp::socket::udp; | ||
use smoltcp::time::Instant; | ||
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv6Address}; | ||
|
||
// Note: If testing with a tap interface in linux, you may need to specify the | ||
// interface index when addressing. E.g., | ||
// | ||
// ``` | ||
// ncat -u ff02::1234%tap0 8123 | ||
// ``` | ||
// | ||
// will send packets to the multicast group we join below on tap0. | ||
|
||
const PORT: u16 = 8123; | ||
const GROUP: [u16; 8] = [0xff02, 0, 0, 0, 0, 0, 0, 0x1234]; | ||
const LOCAL_ADDR: [u16; 8] = [0xfe80, 0, 0, 0, 0, 0, 0, 0x101]; | ||
const ROUTER_ADDR: [u16; 8] = [0xfe80, 0, 0, 0, 0, 0, 0, 0x100]; | ||
|
||
fn main() { | ||
utils::setup_logging("warn"); | ||
|
||
let (mut opts, mut free) = utils::create_options(); | ||
utils::add_tuntap_options(&mut opts, &mut free); | ||
utils::add_middleware_options(&mut opts, &mut free); | ||
|
||
let mut matches = utils::parse_options(&opts, free); | ||
let device = utils::parse_tuntap_options(&mut matches); | ||
let fd = device.as_raw_fd(); | ||
let mut device = | ||
utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false); | ||
|
||
// Create interface | ||
let local_addr = Ipv6Address::from_parts(&LOCAL_ADDR); | ||
let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]); | ||
let mut config = match device.capabilities().medium { | ||
Medium::Ethernet => Config::new(ethernet_addr.into()), | ||
Medium::Ip => Config::new(smoltcp::wire::HardwareAddress::Ip), | ||
Medium::Ieee802154 => todo!(), | ||
}; | ||
config.random_seed = rand::random(); | ||
|
||
let mut iface = Interface::new(config, &mut device, Instant::now()); | ||
iface.update_ip_addrs(|ip_addrs| { | ||
ip_addrs | ||
.push(IpCidr::new(IpAddress::from(local_addr), 64)) | ||
.unwrap(); | ||
}); | ||
iface | ||
.routes_mut() | ||
.add_default_ipv6_route(Ipv6Address::from_parts(&ROUTER_ADDR)) | ||
.unwrap(); | ||
|
||
// Create sockets | ||
let mut sockets = SocketSet::new(vec![]); | ||
let udp_rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY; 4], vec![0; 1024]); | ||
let udp_tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 0]); | ||
let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer); | ||
let udp_handle = sockets.add(udp_socket); | ||
|
||
// Join a multicast group | ||
iface | ||
.join_multicast_group(&mut device, Ipv6Address::from_parts(&GROUP), Instant::now()) | ||
.unwrap(); | ||
|
||
loop { | ||
let timestamp = Instant::now(); | ||
iface.poll(timestamp, &mut device, &mut sockets); | ||
|
||
let socket = sockets.get_mut::<udp::Socket>(udp_handle); | ||
if !socket.is_open() { | ||
socket.bind(PORT).unwrap() | ||
} | ||
|
||
if socket.can_recv() { | ||
socket | ||
.recv() | ||
.map(|(data, sender)| println!("traffic: {} UDP bytes from {}", data.len(), sender)) | ||
.unwrap_or_else(|e| println!("Recv UDP error: {:?}", e)); | ||
} | ||
|
||
phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error"); | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.