multicast.rs 4.3 KB

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