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
use core::fmt;
use std::io;
use tokio::io::unix::AsyncFd;
use tokio::io::{Interest, Ready};
use crate::connection::builder::AuthKind;
use crate::error::{Error, ErrorKind, Result};
use crate::lossy_str::LossyStr;
use crate::sasl::Auth;
use crate::{Buffers, SendBuf};
use super::{ConnectionBuilder, Transport};
#[derive(Debug, Clone, Copy)]
pub(crate) enum Sasl {
/// The stage to realize.
Stage(bool, SaslStage),
/// Sending data.
Send(SaslStage),
/// Receiving data.
Recv(SaslStage),
}
impl fmt::Display for Sasl {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Sasl::Stage(_, stage) => write!(f, "sasl-{stage}"),
Sasl::Send(stage) => write!(f, "sasl-send-{stage}"),
Sasl::Recv(stage) => write!(f, "sasl-recv-{stage}"),
}
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum SaslStage {
Auth,
Begin,
}
impl fmt::Display for SaslStage {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SaslStage::Auth => write!(f, "auth"),
SaslStage::Begin => write!(f, "begin"),
}
}
}
#[derive(Debug, Clone, Copy)]
enum ConnectionState {
/// SASL negotiation.
Sasl(Sasl),
/// Connection is idle.
Idle,
/// Body is being received.
Message(usize),
}
impl ConnectionState {
/// Test if connection is in a state where it is interested in writing.
#[inline]
fn is_writing(&self) -> bool {
matches!(
self,
Self::Sasl(Sasl::Send(..)) | Self::Message(_) | Self::Idle
)
}
}
/// An asynchronous D-Bus client.
pub struct Connection {
state: ConnectionState,
/// Poller for the underlying file descriptor.
transport: AsyncFd<Transport>,
}
impl Connection {
/// Construct a new asynchronous D-Bus client.
pub(crate) fn new(auth: AuthKind, transport: Transport) -> io::Result<Self> {
transport.set_nonblocking(true)?;
Ok(Self {
state: match auth {
AuthKind::Uid => ConnectionState::Sasl(Sasl::Stage(true, SaslStage::Auth)),
AuthKind::None => ConnectionState::Sasl(Sasl::Stage(true, SaslStage::Begin)),
},
transport: AsyncFd::new(transport)?,
})
}
/// Shorthand for connecting the client to the system bus using the default
/// configuration.
#[inline]
pub fn session_bus() -> Result<Self> {
ConnectionBuilder::new().session_bus().build()
}
/// Shorthand for connecting the client to the system bus using the default
/// configuration.
#[inline]
pub fn system_bus() -> Result<Self> {
ConnectionBuilder::new().system_bus().build()
}
/// Authenticate connection.
#[cfg(feature = "libc")]
fn sasl_auth_uid(&mut self, send: &mut SendBuf) -> Result<()> {
let mut auth_buf = [0; 32];
match Auth::external_from_uid(&mut auth_buf) {
Auth::External(external) => {
send.extend_from_slice(b"AUTH EXTERNAL ");
send.extend_from_slice(external);
send.extend_from_slice(b"\r\n");
}
}
Ok(())
}
#[cfg(not(feature = "libc"))]
fn sasl_auth_uid(&mut self, _: &mut SendBuf) -> Result<()> {
Err(Error::new(ErrorKind::UnsupportedAuthUid))
}
fn sasl_begin(&mut self, send: &mut SendBuf) {
send.extend_from_slice(b"BEGIN\r\n");
}
/// Test if the connection is fully established.
pub fn is_connected(&self) -> bool {
matches!(
self.state,
ConnectionState::Idle | ConnectionState::Message(_)
)
}
/// Wait until the connection is fully established.
///
/// This must be used before messages can be sent or received over this
/// connection.
pub async fn connect(&mut self, buf: &mut Buffers) -> Result<()> {
if !self.is_connected() {
// During the connection stage, the send buffer is used to
// communicate in both directions. We clear it now to ensure there's
// nothing unexpected on it.
buf.send.buf_mut().clear();
while !self.is_connected() {
self.io(buf).await?;
}
}
Ok(())
}
/// Wait for the next incoming message on this connection.
///
/// This is the main entry of this connection, and is required to call to
/// have it make progress when passing D-Bus messages.
///
/// If you just want to block while sending messages, use [`flush()`]
/// instead.
///
/// [`flush()`]: Self::flush
///
/// # Examples
///
/// ```no_run
/// use tokio_dbus::{Buffers, Connection, Message};
///
/// # #[tokio::main] async fn main() -> tokio_dbus::Result<()> {
/// let mut c = Connection::session_bus()?;
/// let mut buf = Buffers::new();
/// c.connect(&mut buf).await?;
/// c.wait(&mut buf).await?;
/// let message: Message<'_> = buf.recv.last_message()?;
/// # Ok(()) }
/// ```
pub async fn wait(&mut self, buf: &mut Buffers) -> Result<()> {
// Drop the previous message, but only when it was received in full. A
// cancelled call leaves a partially received message behind, which the
// next call picks up where it left off. Clearing it would desync the
// stream, since the bytes already read cannot be read again.
if self.message_ready(buf) {
buf.recv.clear();
}
while !self.message_ready(buf) {
self.io(buf).await?;
}
Ok(())
}
/// Test if a message has been received in full.
fn message_ready(&self, buf: &Buffers) -> bool {
matches!(self.state, ConnectionState::Idle) && buf.recv.has_message()
}
/// Write out every message which has been buffered for sending.
///
/// Messages are only handed to the bus while the connection is making
/// progress, so this is needed to ensure that a message is on its way before
/// the connection is dropped.
///
/// Note that incoming messages may be received while flushing, in which case
/// the last one is available through [`RecvBuf::last_message()`] just like
/// after a call to [`wait()`].
///
/// [`RecvBuf::last_message()`]: crate::RecvBuf::last_message
/// [`wait()`]: Self::wait
///
/// # Examples
///
/// ```no_run
/// use tokio_dbus::{Buffers, Connection};
///
/// # #[tokio::main] async fn main() -> tokio_dbus::Result<()> {
/// let mut c = Connection::session_bus()?;
/// let mut buf = Buffers::new();
/// c.connect(&mut buf).await?;
///
/// buf.hello()?;
/// c.flush(&mut buf).await?;
/// # Ok(()) }
/// ```
pub async fn flush(&mut self, buf: &mut Buffers) -> Result<()> {
while !buf.send.buf().is_empty() {
self.io(buf).await?;
}
Ok(())
}
async fn io(&mut self, buf: &mut Buffers) -> Result<()> {
if let ConnectionState::Sasl(Sasl::Stage(initial, stage)) = self.state {
if initial {
buf.send.extend_from_slice(b"\0");
}
match stage {
SaslStage::Auth => {
self.sasl_auth_uid(&mut buf.send)?;
self.state = ConnectionState::Sasl(Sasl::Send(SaslStage::Auth));
}
SaslStage::Begin => {
self.sasl_begin(&mut buf.send);
self.state = ConnectionState::Sasl(Sasl::Send(SaslStage::Begin));
}
}
}
let mut interest = Interest::READABLE;
if self.state.is_writing() && !buf.send.buf().is_empty() {
interest |= Interest::WRITABLE;
}
let mut guard = self.transport.ready_mut(interest).await?;
loop {
if guard.ready().is_writable() {
match guard.get_inner_mut().send_buf(buf.send.buf_mut()) {
Ok(()) => {
if let ConnectionState::Sasl(Sasl::Send(stage)) = self.state {
match stage {
SaslStage::Auth => {
self.state = ConnectionState::Sasl(Sasl::Recv(stage));
}
// NB: We do not expect a response after we've
// sent BEGIN, but we *also* do not have a
// message yet.
SaslStage::Begin => {
self.state = ConnectionState::Idle;
}
}
}
if buf.send.buf().is_empty() {
guard.clear_ready_matching(Ready::WRITABLE);
}
}
Err(e) if e.would_block() => {
guard.clear_ready_matching(Ready::WRITABLE);
}
Err(e) => return Err(e),
}
continue;
}
if guard.ready().is_readable() {
match recv(self.state, guard.get_inner_mut(), buf) {
Ok(state) => {
self.state = state;
if matches!(self.state, ConnectionState::Idle) {
return Ok(());
}
}
Err(e) if e.would_block() => {
guard.clear_ready_matching(Ready::READABLE);
}
Err(e) => return Err(e),
}
continue;
}
return Ok(());
}
}
}
fn recv(
state: ConnectionState,
transport: &mut Transport,
buf: &mut Buffers,
) -> Result<ConnectionState> {
match state {
ConnectionState::Sasl(sasl) => {
// During the SASL negotiation stage, we use a single buffer for
// sending and receiving.
let io = buf.send.buf_mut();
let n = transport.recv_line(io)?;
let Some(bytes) = io.get().get(..n) else {
return Err(Error::new(ErrorKind::InvalidSasl));
};
let state = match sasl {
Sasl::Recv(state) => match state {
SaslStage::Auth => {
_ = ok_guid(bytes)?;
ConnectionState::Sasl(Sasl::Stage(false, SaslStage::Begin))
}
SaslStage::Begin => ConnectionState::Idle,
},
sasl => {
return Err(Error::new(ErrorKind::InvalidSaslState(sasl)));
}
};
io.advance(n);
Ok(state)
}
ConnectionState::Idle => {
let total = transport.idle(&mut buf.recv)?;
Ok(ConnectionState::Message(total))
}
ConnectionState::Message(total) => {
transport.recv_body(&mut buf.recv, total)?;
Ok(ConnectionState::Idle)
}
}
}
/// Parse an OK GUID.
pub(crate) fn ok_guid(bytes: &[u8]) -> Result<&LossyStr> {
let line = crate::utils::trim_end(bytes);
let Some((command, rest)) = crate::utils::split_once(line, b' ') else {
return Err(Error::new(ErrorKind::InvalidSasl));
};
match command {
b"OK" => Ok(LossyStr::new(rest)),
_ => Err(Error::new(ErrorKind::InvalidSaslResponse)),
}
}