multicast.rs 4.0 KB

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