Repo of no-std crates for my personal embedded projects
1#![no_std]
2
3mod dns;
4pub(crate) mod encoder;
5pub mod server;
6mod service;
7mod state;
8
9extern crate alloc;
10
11use core::net::{Ipv4Addr, SocketAddrV4};
12
13pub use crate::service::Service;
14pub use crate::state::MdnsAction;
15use crate::{dns::flags::Flags, server::Server, state::MdnsStateMachine};
16
17/// Standard port for mDNS (5353).
18pub const MDNS_PORT: u16 = 5353;
19
20/// Standard IPv4 multicast address for mDNS (224.0.0.251).
21pub const GROUP_ADDR_V4: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 251);
22pub const GROUP_SOCK_V4: SocketAddrV4 = SocketAddrV4::new(GROUP_ADDR_V4, MDNS_PORT);
23
24#[derive(Debug)]
25#[cfg_attr(feature = "defmt", derive(defmt::Format))]
26pub struct MdnsService {
27 server: Server,
28 state: MdnsStateMachine,
29}
30
31impl MdnsService {
32 pub fn new(service: Service) -> Self {
33 Self {
34 server: Server::new(service),
35 state: Default::default(),
36 }
37 }
38
39 pub fn next_action(&mut self) -> MdnsAction {
40 self.state.drive_next_action()
41 }
42
43 pub fn send_announcement<'buffer>(&self, outgoing: &'buffer mut [u8]) -> Option<&'buffer [u8]> {
44 self.server.broadcast(
45 server::ResponseKind::Announcement,
46 Flags::standard_response(),
47 1,
48 alloc::vec::Vec::new(),
49 outgoing,
50 )
51 }
52
53 pub fn listen_for_queries<'buffer>(
54 &mut self,
55 incoming: &[u8],
56 outgoing: &'buffer mut [u8],
57 ) -> Option<&'buffer [u8]> {
58 self.server.respond(incoming, outgoing)
59 }
60}