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
pub use self::send_buf::SendBuf;
mod send_buf;

pub use self::recv_buf::RecvBuf;
mod recv_buf;

use std::io;
use std::num::{NonZeroU32, NonZeroUsize};

use tokio::io::unix::AsyncFd;
use tokio::io::{Interest, Ready};

use crate::connection::{sasl_recv, MessageRef};
use crate::error::{ErrorKind, Result};
use crate::org_freedesktop_dbus::{self, NameFlag, NameReply};
use crate::sasl::{SaslRequest, SaslResponse};
use crate::{BodyBuf, ClientBuilder, Connection, Error, Message, MessageKind, ObjectPath};

/// The high level state of a client.
pub(crate) enum ClientState {
    /// Just initialized.
    Init,
    /// Sent the Hello() message and is awaiting response.
    HelloSent(NonZeroU32),
    /// Client is in a normal idle state.
    Idle,
}

/// An asynchronous D-Bus client.
pub struct Client {
    /// Poller for the underlying file descriptor.
    connection: AsyncFd<Connection>,
    /// Hello serial.
    state: ClientState,
    /// Receive buffer.
    recv: RecvBuf,
    /// Send buffer.
    send: SendBuf,
    /// Body buffer.
    body: BodyBuf,
    /// The name of the client.
    name: Option<Box<str>>,
}

impl Client {
    /// Shorthand for connecting the client to the system bus using the default
    /// configuration.
    #[inline]
    pub async fn session_bus() -> Result<Self> {
        ClientBuilder::new().session_bus().connect().await
    }

    /// Shorthand for connecting the client to the system bus using the default
    /// configuration.
    #[inline]
    pub async fn system_bus() -> Result<Self> {
        ClientBuilder::new().system_bus().connect().await
    }

    /// Process the current connection.
    ///
    /// This is the main entry of this connection, and is required to call to
    /// have it make progress when passing D-Bus messages.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use tokio_dbus::{Client, Message};
    ///
    /// # #[tokio::main] async fn main() -> tokio_dbus::Result<()> {
    /// let mut c = Client::session_bus().await?;
    ///
    /// let message = c.process().await?;
    /// let message: Message<'_> = c.read_message(&message)?;
    /// # Ok(()) }    
    /// ```
    pub async fn process(&mut self) -> Result<MessageRef, Error> {
        loop {
            let Some(message_ref) = self.io(false).await? else {
                continue;
            };

            self.recv.last_serial = NonZeroU32::new(message_ref.header.serial);

            // Read once for internal processing. Avoid this once borrow checker
            // allows returning a reference here directly.
            let message = self.recv.read_message(&message_ref)?;

            if let ClientState::HelloSent(serial) = self.state {
                match message.kind {
                    MessageKind::MethodReturn { reply_serial } if reply_serial == serial => {
                        self.name = Some(message.body().read::<str>()?.into());
                        self.state = ClientState::Idle;
                        continue;
                    }
                    _ => {}
                }
            }

            // TODO: Ignore freedesktop signals for now, but eventually we might
            // want to handle internally.
            if let (Some(org_freedesktop_dbus::INTERFACE), MessageKind::Signal { .. }) =
                (message.interface, message.kind)
            {
                continue;
            }

            return Ok(message_ref);
        }
    }

    /// Flush all outgoing messages and return when the send buffer is empty.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use tokio_dbus::Client;
    ///
    /// # #[tokio::main] async fn main() -> tokio_dbus::Result<()> {
    /// let mut c = Client::session_bus().await?;
    /// c.flush().await?;
    /// # Ok(()) }
    /// ```
    pub async fn flush(&mut self) -> Result<(), Error> {
        while self.io(true).await?.is_some() {}
        Ok(())
    }

    /// Construct a new [`Message`] corresponding to a method call.
    pub fn method_call<'a>(&mut self, path: &'a ObjectPath, member: &'a str) -> Message<'a> {
        self.send.method_call(path, member)
    }

    /// Write a message to the send buffer.
    ///
    /// This can be used to queue messages to be sent during the next call to
    /// [`process()`]. To both receive and send in parallel, see the
    /// [`buffers()`] method.
    ///
    /// [`process()`]: Self::process
    /// [`buffers()`]: Self::buffers
    pub fn write_message(&mut self, message: &Message<'_>) -> Result<()> {
        self.send.write_message(message)
    }

    /// Read a [`MessageRef`] into a [`Message`].
    ///
    /// Note that if the [`MessageRef`] is outdated by calling process again,
    /// the behavior of this function is not well-defined (but safe).
    ///
    /// # Errors
    ///
    /// Errors if the message reference is out of date, such as if another
    /// message has been received.
    pub fn read_message(&self, message_ref: &MessageRef) -> Result<Message<'_>> {
        self.recv.read_message(message_ref)
    }

    /// Access the underlying buffers of the connection.
    ///
    /// This is usually needed to solve lifetime issues, such as holding onto a
    /// message constructed from a [`MessageRef`] while buffering a response.
    ///
    /// The [`RecvBuf`] is used to translate [`MessageRef`] as returned by
    /// [`process()`] into [`Message`] instances and [`SendBuf`] is used to
    /// queue messages to be sent.
    ///
    /// The returned [`BodyBuf`] is the internal buffer that the client uses to
    /// construct message bodies. It is empty when it's returned.
    ///
    /// [`process()`]: Self::process
    pub fn buffers(&mut self) -> (&RecvBuf, &mut SendBuf, &mut BodyBuf) {
        self.body.clear();
        (&self.recv, &mut self.send, &mut self.body)
    }

    /// Construct a new asynchronous D-Bus client.
    pub(crate) fn new(connection: Connection) -> io::Result<Self> {
        connection.set_nonblocking(true)?;

        Ok(Self {
            connection: AsyncFd::new(connection)?,
            state: ClientState::Init,
            recv: RecvBuf::new(),
            send: SendBuf::new(),
            body: BodyBuf::new(),
            name: None,
        })
    }

    /// Send a SASL message and receive a response.
    pub(crate) async fn sasl_request(
        &mut self,
        sasl: &SaslRequest<'_>,
    ) -> Result<SaslResponse<'_>> {
        loop {
            let mut guard = self.connection.writable_mut().await?;

            match guard.get_inner_mut().sasl_send(&mut self.send.buf, sasl) {
                Err(e) if e.would_block() => {
                    guard.clear_ready();
                    continue;
                }
                Err(e) => return Err(e),
                Ok(()) => break,
            }
        }

        loop {
            let mut guard = self.connection.readable_mut().await?;

            match guard.get_inner_mut().sasl_recv(&mut self.recv.buf) {
                Err(e) if e.would_block() => {
                    guard.clear_ready();
                    continue;
                }
                Err(e) => return Err(e),
                Ok(len) => {
                    return sasl_recv(self.recv.buf.read_until(len).get());
                }
            }
        }
    }

    /// Send the SASL `BEGIN` message.
    ///
    /// This does not expect a response from the server, instead it is expected
    /// to transition into the binary D-Bus protocol.
    pub(crate) async fn sasl_begin(&mut self) -> Result<()> {
        loop {
            let mut guard = self.connection.writable_mut().await?;

            match guard.get_inner_mut().sasl_begin(&mut self.send.buf) {
                Err(e) if e.would_block() => {
                    guard.clear_ready();
                    continue;
                }
                Err(e) => return Err(e),
                Ok(()) => return Ok(()),
            }
        }
    }

    /// Send "Hello" message.
    pub(crate) fn hello(&mut self) -> Result<()> {
        let m = self
            .send
            .method_call(org_freedesktop_dbus::PATH, "Hello")
            .with_destination(org_freedesktop_dbus::DESTINATION);

        self.send.write_message(&m)?;
        self.state = ClientState::HelloSent(m.serial());
        Ok(())
    }

    /// Request the given well-known name.
    pub async fn request_name(&mut self, name: &str, flags: NameFlag) -> Result<NameReply> {
        self.body.write(name)?;
        self.body.store(flags)?;

        let m = self
            .send
            .method_call(org_freedesktop_dbus::PATH, "RequestName")
            .with_destination(org_freedesktop_dbus::DESTINATION)
            .with_body_buf(&self.body);

        self.send.write_message(&m)?;
        let serial = m.serial();

        self.body.clear();

        loop {
            let message_ref = self.process().await?;
            let message = self.recv.read_message(&message_ref)?;

            match message.kind {
                MessageKind::MethodReturn { reply_serial } if reply_serial == serial => {
                    let reply = message.body().load::<NameReply>()?;
                    return Ok(reply);
                }
                MessageKind::Error {
                    error_name,
                    reply_serial,
                } if reply_serial == serial => {
                    let message = message.body().read::<str>()?;

                    return Err(Error::new(ErrorKind::ResponseError(
                        error_name.into(),
                        message.into(),
                    )));
                }
                _ => {
                    // Ignore other messages
                }
            }
        }
    }

    async fn io(&mut self, flush: bool) -> Result<Option<MessageRef>, Error> {
        loop {
            if let Some(advance) = self.recv.advance.take() {
                self.recv.buf.advance(advance.get());
                self.recv.buf.update_alignment_base();
                self.recv.last_serial = None;
            }

            let interest = if !flush {
                let mut interest = Interest::READABLE;

                if !self.send.buf.is_empty() {
                    interest |= Interest::WRITABLE;
                }

                interest
            } else if self.send.buf.is_empty() {
                Interest::WRITABLE
            } else {
                return Ok(None);
            };

            let mut guard = self.connection.ready_mut(interest).await?;

            if guard.ready().is_readable() {
                match guard.get_inner_mut().recv_message(&mut self.recv.buf) {
                    Ok(message_ref) => {
                        self.recv.advance = NonZeroUsize::new(message_ref.total);
                        return Ok(Some(message_ref));
                    }
                    Err(e) if e.would_block() => {
                        guard.clear_ready_matching(Ready::READABLE);
                        continue;
                    }
                    Err(e) => return Err(e),
                }
            }

            if guard.ready().is_writable() {
                match guard.get_inner().send_buf(&mut self.send.buf) {
                    Ok(()) => {}
                    Err(e) if e.would_block() => {
                        guard.clear_ready_matching(Ready::WRITABLE);
                        continue;
                    }
                    Err(e) => return Err(e),
                }
            }
        }
    }
}