cloudmqtt 0.4.0

A pure Rust MQTT client and server library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//
//   This Source Code Form is subject to the terms of the Mozilla Public
//   License, v. 2.0. If a copy of the MPL was not distributed with this
//   file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
//! MQTTServer and internals
//!
//! Server Architecture
//! ===================
//!
//! The server consists of multiple parts:
//!
//! - An [`MQTTServer`], the main part of the whole and the user-visible part
//! - The [`SubscriptionManager`] which maintains subscription state
//!
//! Per [MQTT Spec] in 3.1.2.4 the server has to keep the following state:
//!
//! - Whether a session exists -> [`ClientSession`]
//! - The clients subscriptions -> [`SubscriptionManager`]
//!
//! This implementation utilizes "Method B" for QoS 2 protocol flow, as explained in Figure 4.3 of
//! the [MQTT Spec]. This minimizes data being held in the application.
//!
//! - QoS 1 & 2 messages which have been relayed to the client, but not yet acknowledged
//! - QoS 0 & 1 & 2 messages pending transmission
//! - QoS 2 messages which have been received from the client, but have not been acknowledged
//!
//!
//! [MQTT Spec]: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html
#![deny(missing_docs)]

/// Authentication related functionality
pub mod handler;
mod message;
mod state;
mod subscriptions;

use std::{sync::Arc, time::Duration};

use dashmap::DashMap;
use mqtt_format::v3::{
    connect_return::MConnectReturnCode,
    packet::{
        MConnack, MConnect, MDisconnect, MPacket, MPingreq, MPingresp, MPuback, MPubcomp, MPublish,
        MPubrec, MPubrel, MSuback, MSubscribe,
    },
    qos::MQualityOfService,
    strings::MString,
    subscription_acks::MSubscriptionAcks,
    will::MLastWill,
};
use tokio::{
    io::{AsyncWriteExt, DuplexStream, ReadHalf, WriteHalf},
    net::{TcpListener, ToSocketAddrs},
    sync::broadcast::Sender as BroadcastSender,
    sync::Mutex,
};
use tracing::{debug, error, info, trace, warn};

use crate::{error::MqttError, mqtt_stream::MqttStream, PacketIOError};
use subscriptions::{ClientInformation, SubscriptionManager};

use self::{
    handler::{
        AllowAllLogins, AllowAllSubscriptions, LoginError, LoginHandler, SubscriptionHandler,
    },
    message::MqttMessage,
    state::ClientState,
    subscriptions::TopicFilter,
};

/// The unique id (per server) of a connecting client
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ClientId(String);

impl ClientId {
    #[allow(dead_code)]
    pub(crate) fn new(id: String) -> Self {
        ClientId(id)
    }

    /// Get the inner client id
    pub fn get(&self) -> &str {
        &self.0
    }
}

impl<'message> TryFrom<MString<'message>> for ClientId {
    type Error = ClientError;

    fn try_from(ms: MString<'message>) -> Result<Self, Self::Error> {
        Ok(ClientId(ms.to_string()))
    }
}

/// An error that occurred while communicating with a client
#[derive(Debug, thiserror::Error)]
pub enum ClientError {
    /// An error occurred during the sending/receiving of a packet
    #[error("An error occured during the handling of a packet")]
    Packet(#[from] PacketIOError),
    /// An authentication was rejected
    #[error("An authentication was rejected")]
    Authentication(#[from] LoginError),
}

#[derive(Debug)]
pub(crate) struct ClientConnection {
    reader: Mutex<ReadHalf<MqttStream>>,
    writer: Mutex<WriteHalf<MqttStream>>,
}

#[derive(Debug)]
enum ClientSource {
    UnsecuredTcp(TcpListener),
    #[allow(dead_code)]
    Duplex(tokio::sync::mpsc::Receiver<DuplexStream>),
}

impl ClientSource {
    async fn accept(&mut self) -> Result<MqttStream, MqttError> {
        Ok({
            match self {
                ClientSource::UnsecuredTcp(listener) => listener
                    .accept()
                    .await
                    .map(|tpl| tpl.0)
                    .map(MqttStream::UnsecuredTcp)?,
                ClientSource::Duplex(recv) => recv
                    .recv()
                    .await
                    .map(MqttStream::MemoryDuplex)
                    .ok_or(MqttError::DuplexSourceClosed)?,
            }
        })
    }
}

/// A complete MQTT Server
///
/// This server should be seen as a toolkit to integrate with your application.
///
/// To use it, you first need to get a new instance of it, check out any of the `serve_*` methods
/// that create a new instance.
/// Then, you need to start listening for new connections in a long lived future.
///
/// Check out the server example for a working version.
///
pub struct MqttServer<LoginH, SubH> {
    inner: Arc<InnerServer<LoginH, SubH>>,
}

impl<LoginH, SubH> Clone for MqttServer<LoginH, SubH> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

struct InnerServer<LoginH, SubH> {
    clients: Arc<DashMap<ClientId, ClientState>>,
    client_source: Mutex<ClientSource>,
    auth_handler: LoginH,
    extra_listeners: BroadcastSender<MqttMessage>,
    subscription_manager: Arc<SubscriptionManager<SubH>>,
}

impl MqttServer<AllowAllLogins, AllowAllSubscriptions> {
    /// Create a new MQTT server listening on the given `SocketAddr`
    pub async fn serve_v3_unsecured_tcp<Addr: ToSocketAddrs>(
        addr: Addr,
    ) -> Result<Self, MqttError> {
        let bind = TcpListener::bind(addr).await?;

        let (extra_listeners, _) = tokio::sync::broadcast::channel(50);

        Ok(MqttServer {
            inner: Arc::new(InnerServer {
                clients: Arc::new(DashMap::new()),
                client_source: Mutex::new(ClientSource::UnsecuredTcp(bind)),
                auth_handler: AllowAllLogins,
                extra_listeners,
                subscription_manager: Arc::new(SubscriptionManager::new()),
            }),
        })
    }
}

impl<LH: LoginHandler, SH: SubscriptionHandler> MqttServer<LH, SH> {
    /// Switch the login handler with a new one
    ///
    /// ## Note
    ///
    /// You should only call this after instantiating the server, and before listening
    pub fn with_login_handler<NLH: LoginHandler>(
        self,
        new_login_handler: NLH,
    ) -> MqttServer<NLH, SH> {
        let inner = Arc::try_unwrap(self.inner)
            .unwrap_or_else(|_| panic!("Called after started listening"));
        MqttServer {
            inner: Arc::new(InnerServer {
                clients: inner.clients,
                client_source: inner.client_source,
                auth_handler: new_login_handler,
                extra_listeners: inner.extra_listeners,
                subscription_manager: inner.subscription_manager,
            }),
        }
    }

    /// Resets the subscription handler to a new one
    ///
    /// ## Note
    ///
    /// You should only call this after instantiating the server, and before listening
    pub fn with_subscription_handler<NSH: SubscriptionHandler>(
        self,
        new_subscription_handler: NSH,
    ) -> MqttServer<LH, NSH> {
        let inner = Arc::try_unwrap(self.inner)
            .unwrap_or_else(|_| panic!("Called after started listening"));
        MqttServer {
            inner: Arc::new(InnerServer {
                clients: inner.clients,
                client_source: inner.client_source,
                auth_handler: inner.auth_handler,
                extra_listeners: inner.extra_listeners,
                subscription_manager: Arc::new({
                    let manager = Arc::try_unwrap(inner.subscription_manager);

                    manager
                        .unwrap_or_else(|_| panic!("Called after started listening"))
                        .with_subscription_handler(new_subscription_handler)
                }),
            }),
        }
    }

    /// Start accepting new clients connecting to the server
    pub async fn accept_new_clients(&self) -> Result<(), MqttError> {
        let mut client_source = self
            .inner
            .client_source
            .try_lock()
            .map_err(|_| MqttError::AlreadyListening)?;

        loop {
            let server: MqttServer<LH, SH> = self.clone();
            let client = client_source.accept().await?;
            tokio::spawn(async move {
                if let Err(client_error) = server.accept_client(client).await {
                    tracing::error!("Client error: {}", client_error)
                }
            });
        }
    }

    /// Listen to messages sent to the given topic_paths
    pub fn subscribe_to_message<
        Fut: std::future::Future<Output = ()>,
        CB: FnMut(MqttMessage) -> Fut + 'static,
    >(
        &self,
        topic_paths: Vec<String>,
        mut callback: CB,
    ) -> impl std::future::Future<Output = Result<(), MqttError>> + 'static {
        let mut listener = self.inner.extra_listeners.subscribe();

        let topics = topic_paths
            .into_iter()
            .map(TopicFilter::parse_from)
            .collect::<Vec<_>>();

        async move {
            loop {
                let message = listener.recv().await;

                match message {
                    Ok(message) => {
                        if topics.iter().any(|topic| {
                            let msg_topic = TopicFilter::parse_from(message.topic().to_string());

                            let mut i = 0;
                            loop {
                                match (topic.get(i), msg_topic.get(i)) {
                                    (None, None) => break true,
                                    (None, Some(_)) => break false,
                                    (Some(_), None) => break false,
                                    (Some(TopicFilter::MultiWildcard), Some(_)) => break true,
                                    (Some(TopicFilter::SingleWildcard), Some(_)) => (),
                                    (Some(left), Some(right)) => {
                                        if left != right {
                                            break false;
                                        }
                                    }
                                }

                                i += 1;
                            }
                        }) {
                            callback(message).await;
                        }
                    }
                    Err(tokio::sync::broadcast::error::RecvError::Closed) => break,
                    Err(tokio::sync::broadcast::error::RecvError::Lagged(count)) => {
                        warn!("Subscriber lagged by {count} values")
                    }
                }
            }

            Ok(())
        }
    }

    /// Accept a new client connected through the `client` stream
    ///
    /// This does multiple things:
    ///
    /// - It checks whether a client with that given ID exists
    ///     - If yes, then that session is replaced when clean_session = true
    ///
    async fn accept_client(&self, mut client: MqttStream) -> Result<(), ClientError> {
        async fn send_connack(
            session_present: bool,
            connect_return_code: MConnectReturnCode,
            client: &mut MqttStream,
        ) -> Result<(), ClientError> {
            let conn_ack = MConnack {
                session_present,
                connect_return_code,
            };

            crate::write_packet(client, conn_ack).await?;

            Ok(())
        }

        #[allow(clippy::too_many_arguments)]
        async fn connect_client<'message, LH: LoginHandler, SubH: SubscriptionHandler>(
            server: &MqttServer<LH, SubH>,
            mut client: MqttStream,
            _protocol_name: MString<'message>,
            _protocol_level: u8,
            clean_session: bool,
            will: Option<MLastWill<'message>>,
            username: Option<MString<'message>>,
            password: Option<&'message [u8]>,
            keep_alive: u16,
            client_id: MString<'message>,
        ) -> Result<(), ClientError> {
            let empty_client_id = client_id.is_empty();

            let client_id = ClientId::try_from(client_id)?;

            // Check MQTT-3.1.37: "If the Client supplies a zero-byte ClientId,
            // the Client MUST also set CleanSession to 1"
            if empty_client_id && !clean_session {
                // Send a CONNACK with code 0x02/IdentifierRejected
                if let Err(e) =
                    send_connack(false, MConnectReturnCode::IdentifierRejected, &mut client).await
                {
                    debug!("Client could not shut down cleanly: {e}");
                }

                return Err(ClientError::Packet(PacketIOError::InvalidParsedPacket));
            }

            let session_present = if clean_session {
                let _ = server.inner.clients.remove(&client_id);
                false
            } else {
                server.inner.clients.contains_key(&client_id)
            };

            let client_id = Arc::new(client_id);

            if let Err(err) = server
                .inner
                .auth_handler
                .allow_login(client_id.clone(), username.as_deref(), password)
                .await
            {
                send_connack(session_present, err.as_rejection_code(), &mut client).await?;

                return Err(ClientError::Authentication(err));
            }

            send_connack(session_present, MConnectReturnCode::Accepted, &mut client).await?;
            debug!(?client_id, "Accepted new connection");

            let (client_reader, client_writer) = tokio::io::split(client);

            let client_connection = Arc::new(ClientConnection {
                reader: Mutex::new(client_reader),
                writer: Mutex::new(client_writer),
            });

            {
                let client_state = server
                    .inner
                    .clients
                    .entry((*client_id).clone())
                    .or_insert_with(ClientState::default);
                client_state
                    .set_new_connection(client_connection.clone())
                    .await;
            }

            let mut last_will: Option<MqttMessage> = will
                .as_ref()
                .map(|will| MqttMessage::from_last_will(will, client_id.clone()));

            let published_packets = server.inner.subscription_manager.clone();
            let (published_packets_send, mut published_packets_rec) =
                tokio::sync::mpsc::unbounded_channel::<MqttMessage>();

            let send_loop = {
                let publisher_client_id = client_id.clone();
                let clients = server.inner.clients.clone();
                tokio::spawn(async move {
                    loop {
                        match published_packets_rec.recv().await {
                            Some(packet) => {
                                if packet.author_id() == &*publisher_client_id {
                                    trace!(?packet, "Skipping sending message to oneself");
                                    continue;
                                }

                                let Some(client_state) = clients.get(&publisher_client_id) else {
                                    debug!(?publisher_client_id, "Associated state no longer exists");
                                    break;
                                };

                                client_state.send_message(packet).await;
                            }
                            None => {
                                debug!(
                                    ?publisher_client_id,
                                    "No more senders, stopping sending cycle"
                                );
                                break;
                            }
                        }
                    }
                })
            };

            let read_loop = {
                let keep_alive = keep_alive;
                let subscription_manager = server.inner.subscription_manager.clone();
                let client_id = client_id.clone();
                let clients = server.inner.clients.clone();
                let extra_listener = server.inner.extra_listeners.clone();

                tokio::spawn(async move {
                    let client_id = client_id;
                    let client_connection = client_connection;
                    let mut reader = client_connection.reader.lock().await;
                    let keep_alive_duration = Duration::from_secs((keep_alive as u64 * 150) / 100);
                    let subscription_manager = subscription_manager;

                    loop {
                        let packet = tokio::select! {
                            packet = crate::read_one_packet(&mut *reader) => {
                                match packet {
                                    Ok(packet) => packet,
                                    Err(e) => {
                                        debug!("Could not read the next client packet: {e}");
                                        break;
                                    }
                                }
                            },
                            _timeout = tokio::time::sleep(keep_alive_duration) => {
                                debug!("Client timed out");
                                break;
                            }
                        };

                        match packet.get_packet() {
                            MPacket::Publish(MPublish {
                                dup: _,
                                qos,
                                retain,
                                topic_name,
                                id,
                                payload,
                            }) => {
                                let message = MqttMessage::new(
                                    client_id.clone(),
                                    payload.to_vec(),
                                    topic_name.to_string(),
                                    *retain,
                                    *qos,
                                );

                                let _ = extra_listener.send(message.clone());
                                subscription_manager.route_message(message).await;

                                // Handle QoS 1/AtLeastOnce response
                                if *qos == MQualityOfService::AtLeastOnce {
                                    let packet = MPuback { id: id.unwrap() };
                                    let mut writer = client_connection.writer.lock().await;
                                    crate::write_packet(&mut *writer, packet).await?;
                                }

                                if *qos == MQualityOfService::ExactlyOnce {
                                    let Some(client_state) = clients.get(&client_id) else {
                                        debug!(?client_id, "Associated state no longer exists");
                                        break;
                                    };

                                    if let Err(_err) =
                                        client_state.save_qos_exactly_once(id.unwrap())
                                    {
                                        debug!("Encountered an error while handling a PUBACK");
                                        break;
                                    }

                                    let packet = MPubrec { id: id.unwrap() };
                                    let mut writer = client_connection.writer.lock().await;
                                    crate::write_packet(&mut *writer, packet).await?;
                                }
                            }
                            MPacket::Puback(ack @ MPuback { id }) => {
                                trace!(?client_id, ?ack, "Received puback");
                                let Some(client_state) = clients.get(&client_id) else {
                                    debug!(?client_id, "Associated state no longer exists");
                                    break;
                                };

                                if let Err(_err) = client_state.receive_puback(*id) {
                                    debug!("Encountered an error while handling a PUBACK");
                                    break;
                                }
                            }
                            MPacket::Pubrec(ack @ MPubrec { id }) => {
                                trace!(?client_id, ?ack, "Received pubrec");
                                let Some(client_state) = clients.get(&client_id) else {
                                    debug!(?client_id, "Associated state no longer exists");
                                    break;
                                };

                                if let Err(_err) = client_state.receive_pubrec(*id) {
                                    debug!("Encountered an error while handling a PUBACK");
                                    break;
                                }
                                trace!(?client_id, "Received PUBREC, responding with PUBREL");
                                let packet = MPubrel { id: *id };
                                let mut writer = client_connection.writer.lock().await;
                                crate::write_packet(&mut *writer, packet).await?;
                                trace!("Done responding to PUBREC with PUBREL");
                            }
                            MPacket::Pubrel(ack @ MPubrel { id }) => {
                                trace!(?client_id, ?ack, "Received pubrel");
                                let Some(client_state) = clients.get(&client_id) else {
                                    debug!(?client_id, "Associated state no longer exists");
                                    break;
                                };

                                if let Err(_err) = client_state.receive_pubrel(*id) {
                                    debug!("Encountered an error while handling a PUBREL");
                                    break;
                                }
                                let packet = MPubcomp { id: *id };
                                let mut writer = client_connection.writer.lock().await;
                                crate::write_packet(&mut *writer, packet).await?;
                                trace!("Done responding to PUBREL with PUBCOMP");
                            }
                            MPacket::Pubcomp(ack @ MPubcomp { id }) => {
                                trace!(?client_id, ?ack, "Received pubcomp");
                                let Some(client_state) = clients.get(&client_id) else {
                                    debug!(?client_id, "Associated state no longer exists");
                                    break;
                                };

                                if let Err(_err) = client_state.receive_pubcomp(*id) {
                                    debug!("Encountered an error while handling a PUBCOMP");
                                    break;
                                }
                            }
                            MPacket::Disconnect(MDisconnect) => {
                                last_will.take();
                                debug!("Client disconnected gracefully");
                                break;
                            }
                            MPacket::Subscribe(MSubscribe { id, subscriptions }) => {
                                let subscription_acks = subscription_manager
                                    .subscribe(
                                        Arc::new(ClientInformation {
                                            client_id: client_id.clone(),
                                            client_sender: published_packets_send.clone(),
                                        }),
                                        *subscriptions,
                                    )
                                    .await;
                                trace!(?client_id, "Received SUBSCRIBE, responding with SUBACK");
                                let packet = MSuback {
                                    id: *id,
                                    subscription_acks: MSubscriptionAcks {
                                        acks: &subscription_acks,
                                    },
                                };
                                let mut writer = client_connection.writer.lock().await;
                                crate::write_packet(&mut *writer, packet).await?;
                            }
                            MPacket::Pingreq(MPingreq) => {
                                trace!(
                                    ?client_id,
                                    "Received ping request, responding with ping response"
                                );
                                let packet = MPingresp;
                                let mut writer = client_connection.writer.lock().await;
                                crate::write_packet(&mut *writer, packet).await?;
                            }
                            packet => info!("Received packet: {packet:?}, not handling it"),
                        }
                    }

                    if let Some(will) = last_will {
                        debug!(?will, "Sending out will");
                        let _ = published_packets.route_message(will);
                    }

                    if let Err(e) = client_connection.writer.lock().await.shutdown().await {
                        debug!("Client could not shut down cleanly: {e}");
                    }

                    Ok::<(), ClientError>(())
                })
            };

            let (send_err, read_err) = tokio::join!(send_loop, read_loop);
            match send_err {
                Ok(_) => (),
                Err(join_error) => error!(
                    "Send loop of client {} had an unexpected error: {join_error}",
                    &client_id.0
                ),
            }

            match read_err {
                Ok(_) => (),
                Err(join_error) => error!(
                    "Read loop of client {} had an unexpected error: {join_error}",
                    &client_id.0
                ),
            }

            Ok(())
        }

        trace!("Accepting new client");

        let packet = crate::read_one_packet(&mut client).await?;

        if let MPacket::Connect(MConnect {
            client_id,
            clean_session,
            protocol_name,
            protocol_level,
            will,
            username,
            password,
            keep_alive,
        }) = packet.get_packet()
        {
            trace!(?client_id, "Connecting client");
            connect_client(
                self,
                client,
                *protocol_name,
                *protocol_level,
                *clean_session,
                *will,
                *username,
                *password,
                *keep_alive,
                *client_id,
            )
            .await?;
        } else {
            // Disconnect and don't worry about errors
            if let Err(e) = client.shutdown().await {
                debug!("Client could not shut down cleanly: {e}");
            }
        }

        Ok(())
    }
}