multicast.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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, Device, Medium};
  5. use smoltcp::socket::{raw, udp};
  6. use smoltcp::time::Instant;
  7. use smoltcp::wire::{
  8. EthernetAddress, IgmpPacket, IgmpRepr, IpAddress, IpCidr, IpProtocol, IpVersion, Ipv4Address,
  9. Ipv4Packet, Ipv6Address,
  10. };
  11. const MDNS_PORT: u16 = 5353;
  12. const MDNS_GROUP: [u8; 4] = [224, 0, 0, 251];
  13. fn main() {
  14. utils::setup_logging("warn");
  15. let (mut opts, mut free) = utils::create_options();
  16. utils::add_tuntap_options(&mut opts, &mut free);
  17. utils::add_middleware_options(&mut opts, &mut free);
  18. let mut matches = utils::parse_options(&opts, free);
  19. let device = utils::parse_tuntap_options(&mut matches);
  20. let fd = device.as_raw_fd();
  21. let mut device =
  22. utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);
  23. // Create interface
  24. let mut config = Config::new();
  25. config.random_seed = rand::random();
  26. if device.capabilities().medium == Medium::Ethernet {
  27. config.hardware_addr = Some(EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into());
  28. }
  29. let mut iface = Interface::new(config, &mut device);
  30. iface.update_ip_addrs(|ip_addrs| {
  31. ip_addrs
  32. .push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24))
  33. .unwrap();
  34. ip_addrs
  35. .push(IpCidr::new(IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1), 64))
  36. .unwrap();
  37. ip_addrs
  38. .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64))
  39. .unwrap();
  40. });
  41. iface
  42. .routes_mut()
  43. .add_default_ipv4_route(Ipv4Address::new(192, 168, 69, 100))
  44. .unwrap();
  45. iface
  46. .routes_mut()
  47. .add_default_ipv6_route(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x100))
  48. .unwrap();
  49. // Create sockets
  50. let mut sockets = SocketSet::new(vec![]);
  51. // Must fit at least one IGMP packet
  52. let raw_rx_buffer = raw::PacketBuffer::new(vec![raw::PacketMetadata::EMPTY; 2], vec![0; 512]);
  53. // Will not send IGMP
  54. let raw_tx_buffer = raw::PacketBuffer::new(vec![], vec![]);
  55. let raw_socket = raw::Socket::new(
  56. IpVersion::Ipv4,
  57. IpProtocol::Igmp,
  58. raw_rx_buffer,
  59. raw_tx_buffer,
  60. );
  61. let raw_handle = sockets.add(raw_socket);
  62. // Must fit mDNS payload of at least one packet
  63. let udp_rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY; 4], vec![0; 1024]);
  64. // Will not send mDNS
  65. let udp_tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 0]);
  66. let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer);
  67. let udp_handle = sockets.add(udp_socket);
  68. // Join a multicast group to receive mDNS traffic
  69. iface
  70. .join_multicast_group(
  71. &mut device,
  72. Ipv4Address::from_bytes(&MDNS_GROUP),
  73. Instant::now(),
  74. )
  75. .unwrap();
  76. loop {
  77. let timestamp = Instant::now();
  78. iface.poll(timestamp, &mut device, &mut sockets);
  79. let socket = sockets.get_mut::<raw::Socket>(raw_handle);
  80. if socket.can_recv() {
  81. // For display purposes only - normally we wouldn't process incoming IGMP packets
  82. // in the application layer
  83. match socket.recv() {
  84. Err(e) => println!("Recv IGMP error: {e:?}"),
  85. Ok(buf) => {
  86. Ipv4Packet::new_checked(buf)
  87. .and_then(|ipv4_packet| IgmpPacket::new_checked(ipv4_packet.payload()))
  88. .and_then(|igmp_packet| IgmpRepr::parse(&igmp_packet))
  89. .map(|igmp_repr| println!("IGMP packet: {igmp_repr:?}"))
  90. .unwrap_or_else(|e| println!("parse IGMP error: {e:?}"));
  91. }
  92. }
  93. }
  94. let socket = sockets.get_mut::<udp::Socket>(udp_handle);
  95. if !socket.is_open() {
  96. socket.bind(MDNS_PORT).unwrap()
  97. }
  98. if socket.can_recv() {
  99. socket
  100. .recv()
  101. .map(|(data, sender)| {
  102. println!("mDNS traffic: {} UDP bytes from {}", data.len(), sender)
  103. })
  104. .unwrap_or_else(|e| println!("Recv UDP error: {e:?}"));
  105. }
  106. phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
  107. }
  108. }