Something went wrong. Try again.
Bundle Protocol Version 7 (RFC 9171)
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219(*--------------------------------------------------------------------------- Copyright (c) 2025 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** Bundle Protocol Agent node state machine (RFC 9171 Section 5). The I/O-free engine: {!incoming} is a pure transition [t -> now -> event -> t * action list]; the bundles produced as actions are the wire codec's own values ({!Bundle.t}).
The node drives the RFC 9171 Section 5 bundle processing pipeline. A received bundle (Section 5.6) or a locally submitted one (Section 5.2) is dispatched (Section 5.3): if its destination endpoint is one this node is a member of it is delivered locally (Section 5.7), otherwise it is forwarded toward its destination (Section 5.4).
- Reception applies Section 5.6, Step 4 to every block of an unrecognised type: its "delete bundle if block can't be processed" flag deletes the bundle for the reason "Block unsupported", and otherwise its "discard block if it can't be processed" flag removes the block. - A bundle's age is the current time minus its creation time, or its Bundle Age block when that time is zero (Section 5.5). A bundle whose age reaches its lifetime (Section 4.3.1) is deleted for the reason "Lifetime expired"; any other retained bundle's lifetime timer is armed with what remains of its lifetime, so the lifetime is counted from the creation time at every node. - A fragment addressed to this node is retained ("Reassembly pending") until the material extents of the fragments sharing its source node ID and creation timestamp form the whole ADU (Section 5.9). The ADU then replaces the payload of the fragment at offset zero, which is delivered, and every fragment of the ADU is released. - Forwarding increments the hop count block and deletes the bundle once the hop limit is exceeded (Section 4.4.3). The bundle is retained ("Forward pending") and waits its turn: one bundle at a time is handed to the convergence layer ({!Forward}), and the next only once that send has concluded ({!Forwarding_concluded}, Section 5.4, Step 5), so a send the peer withholds holds back the queue but never the node. Per Section 5.4, Step 4 the bundle handed over loses a received Previous Node block (gaining one that names this node when {!val-config} is given [previous_node]), and its Bundle Age block grows by its residence at this node up to the hand-over. - Delivery follows the registration of the local endpoint (Section 5.7, Step 2): while the application has polled it ({!Poll_registration}) it is Active and the bundle is presented at once ({!Deliver}); otherwise its delivery failure action defers the bundle until the next poll, least recently received first. - Every bundle the node holds -- retained for forwarding or reassembly, or deferred for delivery -- is charged the length of its encoding as it reached the node against the configured storage, and so is every service event the application has not yet taken ({!Poll_events}). A bundle that would take more than the storage left is deleted for the reason "Depleted storage" (Section 6.1.1). The node asks for each transfer off the convergence layer ({!Read}) only while its storage has room, so whatever a peer sends it holds at most its storage plus the last transfer it asked for and the service events that transfer raised; the peer's own transport carries the back-pressure.
Time is an input, never a side effect: every transition is given the current DTN time (RFC 9171 Section 4.2.6), and a bundle's lifetime elapses as a [Timer_expired] event, so the machine runs identically under Eio, a unikernel, and the deterministic test suite.
The node generates no bundle status reports (Section 6.1.1), fragments no bundle (Section 5.8), and never retries a forward. *)
(** {1:configuration Configuration} *)
type config(** Node configuration. *)
val config : ?previous_node:Message.eid -> storage:int -> local_eid:Message.eid -> unit -> config(** [config ?previous_node ~storage ~local_eid ()] configures a node that is a member of the endpoint [local_eid] and holds at most [storage] bytes of bundles and service events. When [previous_node] is given, a forwarded bundle carries a Previous Node block holding it (RFC 9171 Section 5.4, Step 4); it must then be this node's node ID (Section 4.2.5.2). By default a forwarded bundle carries no Previous Node block. *)
val local_eid : config -> Message.eid(** [local_eid config] is the node's endpoint identifier. *)
val storage : config -> int(** [storage config] is the bytes the node may hold. *)
(** {1:state State} *)
type t(** Opaque node state: the bundles it holds, the service events not yet taken, and the one send in flight. *)
val v : config -> t(** [v config] is the initial node state, holding nothing. It asks for its first transfer when its storage is not zero ({!reading}). *)
val config_of : t -> config(** [config_of t] is the configuration [t] was created with. *)
val held : t -> int(** [held t] is the bytes [t] holds against its storage: the bundles in {!retained}, each at the length of its encoding as it reached the node, a bundle deleted while the convergence layer was still sending it until that send concludes, and each service event in {!events} at the length of {!Message.string_of_bundle_id} of the bundle it names. Once it reaches {!storage}, [t] asks for no further transfer. *)
val reading : t -> bool(** [reading t] is [true] when [t] has asked for a transfer it has not yet received: initially when its storage is not zero, and after each {!Read}. *)
val refused : t -> int(** [refused t] is how many received transfers were not bundles and were discarded. *)
val pending : t -> Message.bundle_id list(** [pending t] is [List.map Message.bundle_id (retained t)]. *)
val retained : t -> Message.t list(** [retained t] is the bundles [t] holds, most recently stored first: those pending forwarding (RFC 9171 Section 5.4, Step 1) until their sending concludes, the fragments pending reassembly (Section 5.7, Step 1), and the bundles whose delivery is deferred (Section 5.7, Step 2). *)
val deferred : t -> Message.t list(** [deferred t] is the bundles of {!retained} whose delivery is deferred, least recently received first: the order a poll takes them in. *)
val events : t -> Event.t list(** [events t] is the service events not yet taken, oldest first. *)
(** {1:timers Timers}
A retained bundle's lifetime (RFC 9171 Section 5.5) is a countdown the caller arms on the {!Start_timer} action and feeds back as a [Timer_expired] event. *)
(** The type for timer identifiers: which countdown an action arms and an event reports. *)type timer = | Lifetime of Message.bundle_id (** Fires when a retained bundle's lifetime elapses (RFC 9171 Section 5.10). *)
val pp_timer : timer Fmt.t(** [pp_timer] pretty-prints a timer identifier. *)
val equal_timer : timer -> timer -> bool(** [equal_timer a b] is [true] when [a] and [b] name the same bundle (RFC 9171 Section 4.2): a timer's identity is the bundle it counts down for, never the bundle's content. *)
(** {1:transitions Transitions} *)
type event = | Receive of string (** The convergence layer delivered a transfer (RFC 9171 Section 4.1), the one the last {!Read} asked for. A bundle is received (Section 5.6); anything else is refused. *) | Transmit_request of Message.t (** The local application submits a bundle for transmission (RFC 9171 Section 5.2). *) | Forwarding_concluded of Message.bundle_id (** The convergence layer concluded its data-sending procedures for the bundle the last {!Forward} handed it (RFC 9171 Section 5.4, Step 5). *) | Poll_registration (** The application polls the registration of the local endpoint for one bundle (RFC 9171 Section 5.7, Step 2): the least recently received deferred bundle is delivered, or, when there is none, the next deliverable one. *) | Poll_events (** The application asks for one service event: the oldest not yet taken, or, when there is none, the next one. *) | Timer_expired of timer (** A countdown timer fired. *)
type action = | Read (** Take the next transfer off the convergence layer and return it as {!Receive}. At most one is asked for at a time, and none while the storage is held. *) | Forward of Message.t * Message.eid (** Hand the bundle to the convergence layer toward the given next-hop endpoint (RFC 9171 Section 5.4, Step 4), and return {!Forwarding_concluded} once its sending concludes. At most one is in flight at a time. *) | Deliver of Message.t (** Present the bundle to the application, answering one {!Poll_registration}: its delivery (RFC 9171 Section 3.1). *) | Store of Message.t (** Hold the bundle: "Forward pending", "Reassembly pending", or its delivery deferred. *) | Release of Message.bundle_id (** The held bundle has no retention constraint left and is discarded (RFC 9171 Section 5.11). *) | Discard of Message.t * Message.status_report_reason (** Delete the bundle, citing the reason (RFC 9171 Section 5.10). *) | Refuse of { reason : Message.error; bytes : int } (** A transfer of [bytes] bytes was not a bundle, for [reason], and was discarded (RFC 9171 Section 4.1). *) | Notify of Event.t (** Present a service event to the application, answering one {!Poll_events}. *) | Start_timer of timer * int64 (** Arm a countdown timer for the given number of milliseconds. *) | Cancel_timer of timer (** Delete a countdown timer. *)
val incoming : t -> now:int64 -> event -> t * action list(** [incoming t ~now ev] applies [ev] at the DTN time [now], in milliseconds since 2000-01-01 00:00:00 UTC (RFC 9171 Section 4.2.6), returning the next state and the actions to perform, in order. A {!Forward} action's Bundle Age block is advanced to [now] (Section 5.4, Step 4). *)
val pp : t Fmt.t(** [pp] pretty-prints the node state. *)