multicast.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. mod utils;
  2. use log::debug;
  3. use std::collections::BTreeMap;
  4. use std::os::unix::io::AsRawFd;
  5. use smoltcp::iface::{InterfaceBuilder, NeighborCache, SocketSet};
  6. use smoltcp::phy::wait as phy_wait;
  7. use smoltcp::socket::{raw, udp};
  8. use smoltcp::time::Instant;
  9. use smoltcp::wire::{
  10. EthernetAddress, IgmpPacket, IgmpRepr, IpAddress, IpCidr, IpProtocol, IpVersion, Ipv4Address,
  11. Ipv4Packet,
  12. };
  13. const MDNS_PORT: u16 = 5353;
  14. const MDNS_GROUP: [u8; 4] = [224, 0, 0, 251];
  15. fn main() {
  16. utils::setup_logging("warn");
  17. let (mut opts, mut free) = utils::create_options();
  18. utils::add_tuntap_options(&mut opts, &mut free);
  19. utils::add_middleware_options(&mut opts, &mut free);
  20. let mut matches = utils::parse_options(&opts, free);
  21. let device = utils::parse_tuntap_options(&mut matches);
  22. let fd = device.as_raw_fd();
  23. let mut device =
  24. utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);
  25. let neighbor_cache = NeighborCache::new(BTreeMap::new());
  26. let local_addr = Ipv4Address::new(192, 168, 69, 2);
  27. let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
  28. let ip_addr = IpCidr::new(IpAddress::from(local_addr), 24);
  29. let mut ipv4_multicast_storage = [None; 1];
  30. let mut iface = InterfaceBuilder::new()
  31. .hardware_addr(ethernet_addr.into())
  32. .neighbor_cache(neighbor_cache)
  33. .ip_addrs([ip_addr])
  34. .ipv4_multicast_groups(&mut ipv4_multicast_storage[..])
  35. .finalize(&mut device);
  36. let now = Instant::now();
  37. // Join a multicast group to receive mDNS traffic
  38. iface
  39. .join_multicast_group(&mut device, Ipv4Address::from_bytes(&MDNS_GROUP), now)
  40. .unwrap();
  41. let mut sockets = SocketSet::new(vec![]);
  42. // Must fit at least one IGMP packet
  43. let raw_rx_buffer = raw::PacketBuffer::new(vec![raw::PacketMetadata::EMPTY; 2], vec![0; 512]);
  44. // Will not send IGMP
  45. let raw_tx_buffer = raw::PacketBuffer::new(vec![], vec![]);
  46. let raw_socket = raw::Socket::new(
  47. IpVersion::Ipv4,
  48. IpProtocol::Igmp,
  49. raw_rx_buffer,
  50. raw_tx_buffer,
  51. );
  52. let raw_handle = sockets.add(raw_socket);
  53. // Must fit mDNS payload of at least one packet
  54. let udp_rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY; 4], vec![0; 1024]);
  55. // Will not send mDNS
  56. let udp_tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 0]);
  57. let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer);
  58. let udp_handle = sockets.add(udp_socket);
  59. loop {
  60. let timestamp = Instant::now();
  61. match iface.poll(timestamp, &mut device, &mut sockets) {
  62. Ok(_) => {}
  63. Err(e) => {
  64. debug!("poll error: {}", e);
  65. }
  66. }
  67. let socket = sockets.get_mut::<raw::Socket>(raw_handle);
  68. if socket.can_recv() {
  69. // For display purposes only - normally we wouldn't process incoming IGMP packets
  70. // in the application layer
  71. match socket.recv() {
  72. Err(e) => println!("Recv IGMP error: {:?}", e),
  73. Ok(buf) => {
  74. Ipv4Packet::new_checked(buf)
  75. .and_then(|ipv4_packet| IgmpPacket::new_checked(ipv4_packet.payload()))
  76. .and_then(|igmp_packet| IgmpRepr::parse(&igmp_packet))
  77. .map(|igmp_repr| println!("IGMP packet: {:?}", igmp_repr))
  78. .unwrap_or_else(|e| println!("parse IGMP error: {:?}", e));
  79. }
  80. }
  81. }
  82. let socket = sockets.get_mut::<udp::Socket>(udp_handle);
  83. if !socket.is_open() {
  84. socket.bind(MDNS_PORT).unwrap()
  85. }
  86. if socket.can_recv() {
  87. socket
  88. .recv()
  89. .map(|(data, sender)| {
  90. println!("mDNS traffic: {} UDP bytes from {}", data.len(), sender)
  91. })
  92. .unwrap_or_else(|e| println!("Recv UDP error: {:?}", e));
  93. }
  94. phy_wait(fd, iface.poll_delay(timestamp, &sockets)).expect("wait error");
  95. }
  96. }