Receiving

How a uftp receiver verifies, decodes and checks a UFTP message: the two ways to say no, the Endpoint pipeline, HTTP handlers, the outbox and replay protection.

On this page

Two ways to say no#

UFTP 3.1 (Appendix 4 §4) answers a refused message in one of two ways, and they are not interchangeable:

The document…HTTP statusThen
is not acceptable XML, or too large400nothing
has a bad signature401nothing
lacks a Content-Length411nothing
comes from a sender whose key cannot be looked up419, retry laternothing
comes from a sender over its quota429, with Retry-Afternothing
is valid and verified, but breaks a rule200a signed Rejected response, with the reason

A receiver that answers a broken rule with 400 tells a compliant sender that its software is broken. The sender then stops retrying a message it was owed a business answer for.

A Rejection knows which case it is. http_status() gives the status, and response(id, now) gives the Rejected response when one is due. That response goes to the sender whose signature verified, never to a domain the message merely names.

The pipeline#

Endpoint::receive(document, now, lookup) runs, in this order:

  1. open: the wrapper, the key lookup, a strict Ed25519 check over exactly the received bytes, the schema, the binding of the inner sender to the wrapper's, the role allowed to send this type, the type's rules, and the recipient.
  2. market, if set: the ISP duration and the time zone's UTC offsets.
  3. time: a Flex message whose Period is over, and a request or offer that has expired.
  4. outbox, if set: a response must answer a request this endpoint sent.
  5. replay: the same message twice is Already Submitted; the same id with other content is Duplicate Identifier.

The replay guard runs last, so it remembers only messages the endpoint accepted. If a message is refused, say for its time zone, its corrected resend with the same MessageID is judged afresh.

let mut endpoint = Endpoint::new(OpenOptions::for_recipient(me), SignedDuration::from_mins(15))
    .with_market(Market { isp_duration: IspDuration::PT15M, time_zone: "Europe/Amsterdam".parse()? })
    .with_outbox(Outbox::new(SignedDuration::from_hours(24)));

Each step is also public on its own: open, Market::check, Message::check_time, Outbox::check and ReplayGuard::check. Use them separately to build a pipeline of your own.

Over HTTP#

With the http feature, a handler on the http 1.x types:

fn handle(endpoint: &mut Endpoint, request: http::Request<Bytes>) -> http::Response<String> {
    let outcome = endpoint.receive_request(&request, now, directory.lookup());
    if let Err(e) = &outcome {
        if let Some(answer) = e.response(new_id(), now) { /* seal, then POST it later */ }
    }
    uftp::http::response(outcome.as_ref())
}

receive_request checks the headers before it reads the body:

  • a missing Content-Length is 411;
  • one that is over the limit, unparsable, given twice with different values, or not the body's real length is 400;
  • a Content-Type other than text/xml in UTF-8 is 400.

http::response turns the outcome into the HTTP answer. A message that opened, or a broken rule, gets 200 with an empty body. Anything else gets its status and one sentence as text/plain, with nosniff. A 429 also carries Retry-After.

The outbox#

Without an outbox, any participant could send you an "acceptance" for a request you never made. With one, every message you send is recorded:

endpoint.sent(&message, now);

A response is then accepted only if all of these hold:

  • it answers a recorded request of the type it answers;
  • it comes from the domain the request went to;
  • it is addressed to the domain the request came from.

Anything else is Unknown reference, named after the response's reference attribute, for example FlexOfferMessageID. endpoint.outbox()?.request_for(&response) finds the request.

Replays#

ReplayGuard refuses a TimeStamp further than its window from now. It tells a retransmission (Already Submitted) from a reused id (Duplicate Identifier) by comparing signatures: Ed25519 is deterministic, so the same signed bytes always carry the same signature. Its memory is bounded per sender. A sender that fills its quota gets 429 with the seconds until its oldest entry leaves the window, and never pushes out another sender's entries.

A FlexSettlement is answered after checking it against your orders; see Settling.