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
use super::Error;
use super::Result;
use super::Timeout;
use crate::auth;
use crate::message_builder::MarshalledMessage;
use crate::wire::marshal;
use crate::wire::unmarshal;
use std::time;
use std::os::unix::io::RawFd;
use std::os::unix::io::{AsRawFd, FromRawFd};
use std::os::unix::net::UnixStream;
use nix::cmsg_space;
use nix::sys::socket::{
self, connect, recvmsg, sendmsg, socket, ControlMessage, ControlMessageOwned, MsgFlags,
SockAddr, UnixAddr,
};
use nix::sys::uio::IoVec;
#[derive(Debug)]
pub struct SendConn {
stream: UnixStream,
header_buf: Vec<u8>,
serial_counter: u32,
}
pub struct RecvConn {
stream: UnixStream,
msg_buf_in: Vec<u8>,
cmsgs_in: Vec<ControlMessageOwned>,
}
pub struct DuplexConn {
pub send: SendConn,
pub recv: RecvConn,
}
impl RecvConn {
pub fn can_read_from_source(&self) -> nix::Result<bool> {
let mut fdset = nix::sys::select::FdSet::new();
let fd = self.stream.as_raw_fd();
fdset.insert(fd);
use nix::sys::time::TimeValLike;
let mut zero_timeout = nix::sys::time::TimeVal::microseconds(0);
nix::sys::select::select(None, Some(&mut fdset), None, None, Some(&mut zero_timeout))?;
Ok(fdset.contains(fd))
}
fn refill_buffer(&mut self, max_buffer_size: usize, timeout: Timeout) -> Result<()> {
let bytes_to_read = max_buffer_size - self.msg_buf_in.len();
const BUFSIZE: usize = 512;
let mut tmpbuf = [0u8; BUFSIZE];
let iovec = IoVec::from_mut_slice(&mut tmpbuf[..usize::min(bytes_to_read, BUFSIZE)]);
let mut cmsgspace = cmsg_space!([RawFd; 10]);
let flags = MsgFlags::empty();
let old_timeout = self.stream.read_timeout()?;
match timeout {
Timeout::Duration(d) => {
self.stream.set_read_timeout(Some(d))?;
}
Timeout::Infinite => {
self.stream.set_read_timeout(None)?;
}
Timeout::Nonblock => {
self.stream.set_nonblocking(true)?;
}
}
let msg = recvmsg(
self.stream.as_raw_fd(),
&[iovec],
Some(&mut cmsgspace),
flags,
)
.map_err(|e| match e {
nix::errno::Errno::EAGAIN => Error::TimedOut,
_ => Error::NixError(e),
});
self.stream.set_nonblocking(false)?;
self.stream.set_read_timeout(old_timeout)?;
let msg = msg?;
self.msg_buf_in
.extend(&mut tmpbuf[..msg.bytes].iter().copied());
self.cmsgs_in.extend(msg.cmsgs());
Ok(())
}
pub fn bytes_needed_for_current_message(&self) -> Result<usize> {
if self.msg_buf_in.len() < 16 {
return Ok(16);
}
let (_, header) = unmarshal::unmarshal_header(&self.msg_buf_in, 0)?;
let (_, header_fields_len) = crate::wire::util::parse_u32(
&self.msg_buf_in[unmarshal::HEADER_LEN..],
header.byteorder,
)?;
let complete_header_size = unmarshal::HEADER_LEN + header_fields_len as usize + 4;
let padding_between_header_and_body = 8 - ((complete_header_size) % 8);
let padding_between_header_and_body = if padding_between_header_and_body == 8 {
0
} else {
padding_between_header_and_body
};
let bytes_needed = complete_header_size as usize
+ padding_between_header_and_body
+ header.body_len as usize;
Ok(bytes_needed)
}
pub fn buffer_contains_whole_message(&self) -> Result<bool> {
if self.msg_buf_in.len() < 16 {
return Ok(false);
}
let bytes_needed = self.bytes_needed_for_current_message();
match bytes_needed {
Err(e) => {
if let Error::UnmarshalError(unmarshal::Error::NotEnoughBytes) = e {
Ok(false)
} else {
Err(e)
}
}
Ok(bytes_needed) => Ok(self.msg_buf_in.len() >= bytes_needed),
}
}
pub fn read_whole_message(&mut self, timeout: Timeout) -> Result<()> {
let start_time = time::Instant::now();
while !self.buffer_contains_whole_message()? {
self.refill_buffer(
self.bytes_needed_for_current_message()?,
super::calc_timeout_left(&start_time, timeout)?,
)?;
}
Ok(())
}
pub fn read_once(&mut self, timeout: Timeout) -> Result<()> {
self.refill_buffer(self.bytes_needed_for_current_message()?, timeout)?;
Ok(())
}
pub fn get_next_message(&mut self, timeout: Timeout) -> Result<MarshalledMessage> {
self.read_whole_message(timeout)?;
let (hdrbytes, header) = unmarshal::unmarshal_header(&self.msg_buf_in, 0)?;
let (dynhdrbytes, dynheader) =
unmarshal::unmarshal_dynamic_header(&header, &self.msg_buf_in, hdrbytes)?;
let (bytes_used, mut msg) = unmarshal::unmarshal_next_message(
&header,
dynheader,
&self.msg_buf_in,
hdrbytes + dynhdrbytes,
)?;
if self.msg_buf_in.len() != bytes_used + hdrbytes + dynhdrbytes {
return Err(Error::UnmarshalError(unmarshal::Error::NotAllBytesUsed));
}
self.msg_buf_in.clear();
for cmsg in &self.cmsgs_in {
match cmsg {
ControlMessageOwned::ScmRights(fds) => {
msg.body
.raw_fds
.extend(fds.iter().map(|fd| crate::wire::UnixFd::new(*fd)));
}
_ => {
eprintln!("Cmsg other than ScmRights: {:?}", cmsg);
}
}
}
self.cmsgs_in.clear();
Ok(msg)
}
}
impl SendConn {
pub fn alloc_serial(&mut self) -> u32 {
let serial = self.serial_counter;
self.serial_counter += 1;
serial
}
pub fn send_message<'a>(
&'a mut self,
msg: &'a MarshalledMessage,
) -> Result<SendMessageContext<'a>> {
let serial = if let Some(serial) = msg.dynheader.serial {
serial
} else {
let serial = self.serial_counter;
self.serial_counter += 1;
serial
};
self.header_buf.clear();
marshal::marshal(&msg, serial, &mut self.header_buf)?;
let ctx = SendMessageContext {
msg,
conn: self,
state: SendMessageState {
bytes_sent: 0,
serial,
},
};
Ok(ctx)
}
pub fn send_message_write_all(&mut self, msg: &MarshalledMessage) -> Result<u32> {
let ctx = self.send_message(msg)?;
ctx.write_all().map_err(force_finish_on_error)
}
}
pub fn force_finish_on_error<E>((s, e): (SendMessageContext<'_>, E)) -> E {
s.force_finish();
e
}
#[must_use = "Dropping this type is considered an error since it might leave the connection in an illdefined state if only some bytes of a message have been written"]
#[derive(Debug)]
pub struct SendMessageContext<'a> {
msg: &'a MarshalledMessage,
conn: &'a mut SendConn,
state: SendMessageState,
}
#[derive(Debug, Copy, Clone)]
pub struct SendMessageState {
bytes_sent: usize,
serial: u32,
}
impl Drop for SendMessageContext<'_> {
fn drop(&mut self) {
if self.state.bytes_sent != 0 && !self.all_bytes_written() {
panic!("You dropped a SendMessageContext that only partially sent the message! This is not ok since that leaves the connection in an ill defined state. Use one of the consuming functions!");
} else {
}
}
}
impl SendMessageContext<'_> {
pub fn serial(&self) -> u32 {
self.state.serial
}
pub fn resume<'a>(
conn: &'a mut SendConn,
msg: &'a MarshalledMessage,
progress: SendMessageState,
) -> SendMessageContext<'a> {
SendMessageContext {
conn,
msg,
state: progress,
}
}
pub fn into_progress(self) -> SendMessageState {
let progress = self.state;
Self::force_finish(self);
progress
}
fn finish_if_ok<O, E>(
self,
res: std::result::Result<O, E>,
) -> std::result::Result<O, (Self, E)> {
match res {
Ok(o) => {
std::mem::drop(self);
Ok(o)
}
Err(e) => Err((self, e)),
}
}
pub fn force_finish(self) {
std::mem::forget(self)
}
pub fn write(mut self, timeout: Timeout) -> std::result::Result<u32, (Self, super::Error)> {
let start_time = std::time::Instant::now();
let res = loop {
let iteration_timeout = super::calc_timeout_left(&start_time, timeout);
let iteration_timeout = match iteration_timeout {
Err(e) => break Err(e),
Ok(t) => t,
};
match self.write_once(iteration_timeout) {
Err(e) => break Err(e),
Ok(t) => t,
};
if self.all_bytes_written() {
break Ok(self.state.serial);
}
};
self.finish_if_ok(res)
}
pub fn write_all(self) -> std::result::Result<u32, (Self, super::Error)> {
self.write(Timeout::Infinite)
}
pub fn bytes_total(&self) -> usize {
self.conn.header_buf.len() + self.msg.get_buf().len()
}
pub fn all_bytes_written(&self) -> bool {
self.state.bytes_sent == self.bytes_total()
}
pub fn write_once(&mut self, timeout: Timeout) -> Result<usize> {
let header_bytes_sent = usize::min(self.state.bytes_sent, self.conn.header_buf.len());
let header_slice_to_send = &self.conn.header_buf[header_bytes_sent..];
let body_bytes_sent = self.state.bytes_sent - header_bytes_sent;
let body_slice_to_send = &self.msg.get_buf()[body_bytes_sent..];
let iov = [
IoVec::from_slice(header_slice_to_send),
IoVec::from_slice(body_slice_to_send),
];
let flags = MsgFlags::empty();
let old_timeout = self.conn.stream.write_timeout()?;
match timeout {
Timeout::Duration(d) => {
self.conn.stream.set_write_timeout(Some(d))?;
}
Timeout::Infinite => {
self.conn.stream.set_write_timeout(None)?;
}
Timeout::Nonblock => {
self.conn.stream.set_nonblocking(true)?;
}
}
let raw_fds = if self.state.bytes_sent == 0 {
self.msg
.body
.raw_fds
.iter()
.map(|fd| fd.get_raw_fd())
.flatten()
.collect::<Vec<RawFd>>()
} else {
vec![]
};
let bytes_sent = sendmsg(
self.conn.stream.as_raw_fd(),
&iov,
&[ControlMessage::ScmRights(&raw_fds)],
flags,
None,
);
self.conn.stream.set_write_timeout(old_timeout)?;
self.conn.stream.set_nonblocking(false)?;
let bytes_sent = bytes_sent?;
self.state.bytes_sent += bytes_sent;
Ok(bytes_sent)
}
}
impl DuplexConn {
pub fn connect_to_bus(addr: UnixAddr, with_unix_fd: bool) -> super::Result<DuplexConn> {
let sock = socket(
socket::AddressFamily::Unix,
socket::SockType::Stream,
socket::SockFlag::empty(),
None,
)?;
let sock_addr = SockAddr::Unix(addr);
connect(sock, &sock_addr)?;
let mut stream = unsafe { UnixStream::from_raw_fd(sock) };
match auth::do_auth(&mut stream)? {
auth::AuthResult::Ok => {}
auth::AuthResult::Rejected => return Err(Error::AuthFailed),
}
if with_unix_fd {
match auth::negotiate_unix_fds(&mut stream)? {
auth::AuthResult::Ok => {}
auth::AuthResult::Rejected => return Err(Error::UnixFdNegotiationFailed),
}
}
auth::send_begin(&mut stream)?;
Ok(DuplexConn {
send: SendConn {
stream: stream.try_clone()?,
header_buf: Vec::new(),
serial_counter: 1,
},
recv: RecvConn {
msg_buf_in: Vec::new(),
cmsgs_in: Vec::new(),
stream,
},
})
}
pub fn send_hello(&mut self, timeout: crate::connection::Timeout) -> super::Result<String> {
let start_time = time::Instant::now();
let hello = crate::standard_messages::hello();
let serial = self
.send
.send_message(&hello)?
.write(super::calc_timeout_left(&start_time, timeout)?)
.map_err(|(ctx, e)| {
ctx.force_finish();
e
})?;
let resp = self
.recv
.get_next_message(super::calc_timeout_left(&start_time, timeout)?)?;
if resp.dynheader.response_serial != Some(serial) {
return Err(super::Error::AuthFailed);
}
let unique_name = resp.body.parser().get::<String>()?;
Ok(unique_name)
}
}
impl AsRawFd for SendConn {
fn as_raw_fd(&self) -> RawFd {
self.stream.as_raw_fd()
}
}
impl AsRawFd for RecvConn {
fn as_raw_fd(&self) -> RawFd {
self.stream.as_raw_fd()
}
}
impl AsRawFd for DuplexConn {
fn as_raw_fd(&self) -> RawFd {
self.recv.stream.as_raw_fd()
}
}