darkbio_wire/lib.rs
1// wire-rs: encrypted protocol between Ark and host
2// Copyright 2025 Dark Bio AG. All rights reserved.
3//
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7// Allow excluding test code from coverage measurements on nightly
8#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
9// Pull in the README as the package doc
10#![doc = include_str!("../README.md")]
11
12pub mod memory;
13pub mod protocol;
14pub mod transport;
15
16// Re-export the crates whose types appear in the API, so consumers can name
17// them at the exact versions this crate was compiled against
18pub use darkbio_clock as clock;
19pub use darkbio_cobs as cobs;
20pub use darkbio_crypto as crypto;
21pub use darkbio_trust as trust;
22pub use prost;
23
24/// Version of the wire crate compiled into this process.
25///
26/// It is mostly a debugging aid, so a protocol version mismatch can be found
27/// without guessing who compiled what into where.
28pub const VERSION: &str = env!("CARGO_PKG_VERSION");
29
30use std::fmt;
31use std::sync::atomic::{AtomicU64, Ordering};
32
33/// Label of a session or a message in log lines.
34///
35/// Sessions get a process-local number the peer never sees, and the default
36/// label is zero. The type derives no equality or hashing and exposes no
37/// number, so no code can route or match on it.
38#[derive(Clone, Copy, Debug, Default)]
39pub(crate) struct LogId(
40 /// Number shown in log lines.
41 u64,
42);
43
44impl From<u64> for LogId {
45 /// Wraps a number as a log label.
46 fn from(id: u64) -> Self {
47 Self(id)
48 }
49}
50
51impl fmt::Display for LogId {
52 /// Formats the label as its bare number.
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 self.0.fmt(f)
55 }
56}
57
58/// Counter numbering the sessions of every client and server in the process,
59/// so log lines can follow a session from its establishment to its end.
60static SESSIONS: AtomicU64 = AtomicU64::new(0);
61
62/// Allocates the next session label, starting from one.
63pub(crate) fn next_log_id() -> LogId {
64 LogId(SESSIONS.fetch_add(1, Ordering::Relaxed) + 1)
65}
66
67#[cfg(test)]
68#[cfg_attr(coverage_nightly, coverage(off))]
69mod testing;