multicast6.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. mod utils;
  2. use std::os::unix::io::AsRawFd;
  3. use smoltcp::iface::{Config, Interface, SocketSet};
  4. use smoltcp::phy::wait as phy_wait;
  5. use smoltcp::phy::{Device, Medium};
  6. use smoltcp::socket::udp;
  7. use smoltcp::time::Instant;
  8. use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv6Address};
  9. // Note: If testing with a tap interface in linux, you may need to specify the
  10. // interface index when addressing. E.g.,
  11. //
  12. // ```
  13. // ncat -u ff02::1234%tap0 8123
  14. // ```
  15. //
  16. // will send packets to the multicast group we join below on tap0.
  17. const PORT: u16 = 8123;
  18. const GROUP: Ipv6Address = Ipv6Address::new(0xff02, 0, 0, 0, 0, 0, 0, 0x1234);
  19. const LOCAL_ADDR: Ipv6Address = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x101);
  20. const ROUTER_ADDR: Ipv6Address = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x100);
  21. fn main() {
  22. utils::setup_logging("warn");
  23. let (mut opts, mut free) = utils::create_options();
  24. utils::add_tuntap_options(&mut opts, &mut free);
  25. utils::add_middleware_options(&mut opts, &mut free);
  26. let mut matches = utils::parse_options(&opts, free);
  27. let device = utils::parse_tuntap_options(&mut matches);
  28. let fd = device.as_raw_fd();
  29. let mut device =
  30. utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);
  31. // Create interface
  32. let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
  33. let mut config = match device.capabilities().medium {
  34. Medium::Ethernet => Config::new(ethernet_addr.into()),
  35. Medium::Ip => Config::new(smoltcp::wire::HardwareAddress::Ip),
  36. Medium::Ieee802154 => todo!(),
  37. };
  38. config.random_seed = rand::random();
  39. let mut iface = Interface::new(config, &mut device, Instant::now());
  40. iface.update_ip_addrs(|ip_addrs| {
  41. ip_addrs
  42. .push(IpCidr::new(IpAddress::from(LOCAL_ADDR), 64))
  43. .unwrap();
  44. });
  45. iface
  46. .routes_mut()
  47. .add_default_ipv6_route(ROUTER_ADDR)
  48. .unwrap();
  49. // Create sockets
  50. let mut sockets = SocketSet::new(vec![]);
  51. let udp_rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY; 4], vec![0; 1024]);
  52. let udp_tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 0]);
  53. let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer);
  54. let udp_handle = sockets.add(udp_socket);
  55. // Join a multicast group
  56. iface.join_multicast_group(GROUP).unwrap();
  57. loop {
  58. let timestamp = Instant::now();
  59. iface.poll(timestamp, &mut device, &mut sockets);
  60. let socket = sockets.get_mut::<udp::Socket>(udp_handle);
  61. if !socket.is_open() {
  62. socket.bind(PORT).unwrap()
  63. }
  64. if socket.can_recv() {
  65. socket
  66. .recv()
  67. .map(|(data, sender)| println!("traffic: {} UDP bytes from {}", data.len(), sender))
  68. .unwrap_or_else(|e| println!("Recv UDP error: {:?}", e));
  69. }
  70. phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
  71. }
  72. }