Skip to main content

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// The crates whose types appear in this crate's API, re-exported so consumers
17// can name 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. It's mostly a debug
25/// utility to help detect protocol version mismatches without having to guess
26/// who compiled what into where.
27pub const VERSION: &str = env!("CARGO_PKG_VERSION");
28
29use std::fmt;
30use std::sync::atomic::{AtomicU64, Ordering};
31
32/// Labels a session or a message in log lines and nothing else. Sessions get
33/// a process-local number the peer never sees. The type derives no equality
34/// or hashing and exposes no number, so nothing can route or match on it.
35#[derive(Clone, Copy, Debug, Default)]
36pub(crate) struct LogId(u64);
37
38impl From<u64> for LogId {
39    fn from(id: u64) -> Self {
40        Self(id)
41    }
42}
43
44impl fmt::Display for LogId {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        self.0.fmt(f)
47    }
48}
49
50/// Numbers the sessions of every client and server in the process, so log
51/// lines can be followed from a session's establishment to its end.
52static SESSIONS: AtomicU64 = AtomicU64::new(0);
53
54/// Allocates the next session label, starting from one.
55pub(crate) fn next_log_id() -> LogId {
56    LogId(SESSIONS.fetch_add(1, Ordering::Relaxed) + 1)
57}
58
59#[cfg(test)]
60#[cfg_attr(coverage_nightly, coverage(off))]
61mod testing;