Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion doc/concept/standard.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ network (it accepts any certificate, so never point it at a remote relay) and
`RUST_LOG=info,moq_net=debug` to see the negotiated version. Behavior worth
knowing when pointing another implementation at ours: we announce every
namespace we can offer unsolicited *and* ask for every prefix we may discover;
set the solicit `SETUP` option to make us wait to be asked. Single-track
set the solicit `SETUP` option to make us wait to be asked. A peer that does not
set that option still gets each match as a `NAMESPACE` on its `SUBSCRIBE_NAMESPACE`
stream on draft-16 and later, so it hears the namespace twice. We never send
`PUBLISH`: on drafts 16 and 17 a `SUBSCRIBE_NAMESPACE` asking only for `PUBLISH`
is refused, one asking for both gets only `NAMESPACE`, and any other Subscribe
Options value closes the session. Single-track
`PUBLISH` offers are declined; announce a namespace and serve the resulting
`SUBSCRIBE`s instead.
11 changes: 8 additions & 3 deletions drafts/draft-lcurley-moq-solicit.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ This is a MUST NOT rather than a SHOULD NOT because the receiver enforces it. A
A relay is the expected user of this declaration, as is any endpoint that asks for what it wants.
So is an endpoint that only publishes, which cannot subscribe to anything and therefore has no use for an advertisement of any kind.

An endpoint SHOULD NOT advertise the same namespace both ways on one session.
Whichever arrives second replaces the source the first attached, which at best wastes a stream and at worst leaves the receiver holding two independent advertisements it must reconcile.
Because this declaration decides which of the two an endpoint uses, honoring it also settles that question for the whole session.
An endpoint that did not declare 1 can hear a namespace both ways: unsolicited, and in answer to its own SUBSCRIBE_NAMESPACE.
Both report the same namespace, so the receiver holds one advertisement, not two sources, until both are withdrawn.


# Enforcement {#enforcement}
Expand Down Expand Up @@ -158,6 +157,12 @@ This document defines only the values 0 and 1; a later extension that needs to s

--- back

# Changelog

## Since draft-lcurley-moq-solicit-00 (in progress)

- A namespace heard both unsolicited and in answer to SUBSCRIBE_NAMESPACE is one advertisement until both are withdrawn, replacing the advice not to advertise it both ways.

# Acknowledgments
{:numbered="false"}

Expand Down
119 changes: 116 additions & 3 deletions js/net/src/ietf/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ import { expect, spyOn, test } from "bun:test";
import { exchangeSetup } from "../connection/handshake.ts";
import { SessionCode } from "../error.ts";
import { createMockTransportPair } from "../mock.ts";
import { Producer as OriginProducer } from "../origin.ts";
import * as Path from "../path.ts";
import { Stream, Writer } from "../stream.ts";
import { Connection } from "./connection.ts";
import * as Message from "./message.ts";
import * as Namespace from "./namespace.ts";
import { Group } from "./object.ts";
import { SetupOption, SetupOptions } from "./parameters.ts";
import { RequestError } from "./request.ts";
import { Parameters, SetupOption, SetupOptions } from "./parameters.ts";
import { RequestError, RequestOk } from "./request.ts";
import { Setup } from "./setup.ts";
import { SUBSCRIBE_TRACKS_ID } from "./subscribe_namespace.ts";
import {
SUBSCRIBE_TRACKS_ID,
SubscribeNamespace,
SubscribeNamespaceEntry,
SubscribeNamespaceLegacy,
SubscribeOptions,
} from "./subscribe_namespace.ts";
import { ALPN, type IetfVersion, Version } from "./version.ts";

const PADDING = 0x132b3e28n;
Expand Down Expand Up @@ -149,6 +159,109 @@ test("SUBSCRIBE_TRACKS refuses only its request", async () => {
}
});

/**
* A draft-16/17 SUBSCRIBE_NAMESPACE gets NAMESPACE whenever it asks for namespaces, and one
* asking for PUBLISH alone is refused, since we never send PUBLISH. Draft-18 has no
* Subscribe Options and always asks for namespaces.
*/
for (const [version, alpn, options, namespaces] of [
[Version.DRAFT_16, ALPN.DRAFT_16, SubscribeOptions.PUBLISH, false],
[Version.DRAFT_16, ALPN.DRAFT_16, SubscribeOptions.NAMESPACE, true],
[Version.DRAFT_16, ALPN.DRAFT_16, SubscribeOptions.BOTH, true],
[Version.DRAFT_17, ALPN.DRAFT_17, SubscribeOptions.PUBLISH, false],
[Version.DRAFT_17, ALPN.DRAFT_17, SubscribeOptions.NAMESPACE, true],
[Version.DRAFT_17, ALPN.DRAFT_17, SubscribeOptions.BOTH, true],
[Version.DRAFT_18, ALPN.DRAFT_18, undefined, true],
] as const) {
test(`${alpn} SUBSCRIBE_NAMESPACE with Subscribe Options ${options ?? "absent"}`, async () => {
const pair = createMockTransportPair(alpn);
const control = await Stream.open(pair.server, { version });
const origin = new OriginProducer();
origin.createBroadcast(Path.from("room")).announce();
const connection = new Connection({
url: new URL("https://example.com"),
quic: pair.server,
control,
maxRequestId: 100n,
version,
client: false,
publish: origin.consume(),
solicit: true,
});

try {
const stream = await Stream.open(pair.client, { version });
if (options === undefined) {
await stream.writer.u53(SubscribeNamespace.id);
await new SubscribeNamespace({ namespace: Path.empty(), requestId: 1n }).encode(stream.writer, version);
} else {
await stream.writer.u53(SubscribeNamespaceLegacy.id);
await new SubscribeNamespaceLegacy({
namespace: Path.empty(),
requestId: 1n,
subscribeOptions: options,
}).encode(stream.writer, version);
}

if (namespaces) {
expect(await stream.reader.u53()).toBe(RequestOk.id);
await RequestOk.decode(stream.reader, version);
expect(await stream.reader.u53()).toBe(SubscribeNamespaceEntry.id);
expect((await SubscribeNamespaceEntry.decode(stream.reader, version)).suffix).toBe(Path.from("room"));
} else {
expect(await stream.reader.u53()).toBe(RequestError.id);
const refused = await RequestError.decode(stream.reader, version);
expect(refused.errorCode).toBe(0x3);
expect(refused.requestId).toBe(version === Version.DRAFT_16 ? 1n : undefined);
expect(await stream.reader.done()).toBe(true);
}
} finally {
connection.abort();
origin.close();
}
});
}

/** The drafts define only 0x00 through 0x02; any other value closes the session, even past 2^53. */
for (const [version, alpn] of [
[Version.DRAFT_16, ALPN.DRAFT_16],
[Version.DRAFT_17, ALPN.DRAFT_17],
] as const) {
for (const options of [3n, 2n ** 53n]) {
test(`${alpn} Subscribe Options ${options} closes the session`, async () => {
const logged = spyOn(console, "error").mockImplementation(() => void 0);
const pair = createMockTransportPair(alpn);
const control = await Stream.open(pair.server, { version });
const connection = new Connection({
url: new URL("https://example.com"),
quic: pair.server,
control,
maxRequestId: 100n,
version,
client: false,
});

try {
const stream = await Stream.open(pair.client, { version });
await stream.writer.u53(SubscribeNamespaceLegacy.id);
await Message.encode(stream.writer, async (w) => {
await w.u62(1n); // Request ID
if (version === Version.DRAFT_17) await w.u62(0n); // Required Request ID delta
await Namespace.encode(w, Path.empty());
await w.u62(options);
await new Parameters().encode(w, version);
});

const info = await pair.client.closed;
expect(info.closeCode).toBe(SessionCode.ProtocolViolation);
} finally {
logged.mockRestore();
connection.abort();
}
});
}
}

/**
* Padding and group streams that beat the peer's SETUP are held and classified once it
* lands. The drafts say to buffer early data; failing the handshake over it broke a peer
Expand Down
18 changes: 16 additions & 2 deletions js/net/src/ietf/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import { Publish } from "./publish.ts";
import { PublishNamespace } from "./publish_namespace.ts";
import { Publisher } from "./publisher.ts";
import { Subscribe, SubscribeUpdate } from "./subscribe.ts";
import { SUBSCRIBE_TRACKS_ID, SubscribeNamespace, SubscribeNamespaceLegacy } from "./subscribe_namespace.ts";
import {
SUBSCRIBE_TRACKS_ID,
SubscribeNamespace,
SubscribeNamespaceLegacy,
SubscribeOptions,
} from "./subscribe_namespace.ts";
import { Subscriber } from "./subscriber.ts";
import { TrackStatusRequest } from "./track.ts";
import { type IetfVersion, Version, versionName } from "./version.ts";
Expand Down Expand Up @@ -212,6 +217,10 @@ export class Connection implements Established {

async #run(early: Reader[]): Promise<void> {
try {
// Both run together. runPublishNamespaces is a no-op when the peer asked to be
// solicited; otherwise it pushes PUBLISH_NAMESPACE. On draft-16 and later a
// SUBSCRIBE_NAMESPACE stream is filled either way. A NAMESPACE is discovery,
// not a second route.
await Promise.all([this.#runBidis(), this.#runUnis(early), this.#publisher.runPublishNamespaces()]);
} catch (err) {
if (!this.#closed) {
Expand Down Expand Up @@ -257,14 +266,19 @@ export class Connection implements Established {

switch (typeId) {
// Draft-18 SUBSCRIBE_NAMESPACE (0x50) and the legacy 0x11 message decode
// to the same request_id + namespace; the legacy options field is ignored.
// to the same request_id + namespace. We never send PUBLISH, so a legacy
// request for PUBLISH alone is refused, and one for both gets only NAMESPACE.
case BigInt(SubscribeNamespace.id): {
const msg = await SubscribeNamespace.decode(stream.reader, this.#session.version);
await this.#publisher.runSubscribeNamespace(msg, stream);
break;
}
case BigInt(SubscribeNamespaceLegacy.id): {
const legacy = await SubscribeNamespaceLegacy.decode(stream.reader, this.#session.version);
if (legacy.subscribeOptions === SubscribeOptions.PUBLISH) {
await this.#publisher.refuseSubscribeNamespace(legacy.requestId, stream);
break;
}
const msg = new SubscribeNamespace({
requestId: legacy.requestId,
namespace: legacy.namespace,
Expand Down
44 changes: 44 additions & 0 deletions js/net/src/ietf/publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2317,6 +2317,50 @@ test("draft-20: PUBLISH_DONE waits for a queued group and counts every stream",
}
});

/**
* A peer that did not send SOLICIT still gets NAMESPACE on its SUBSCRIBE_NAMESPACE
* stream on draft-16 and later: one for a match that already exists, one announced
* after, then NAMESPACE_DONE when that announcement ends.
*/
test.each([
["draft-16", Version.DRAFT_16],
["draft-18", Version.DRAFT_18],
] as const)("a non-SOLICIT %s SUBSCRIBE_NAMESPACE carries NAMESPACE then NAMESPACE_DONE", async (_, version) => {
const pair = createMockTransportPair(ALPN.DRAFT_19);
const session = new NativeSession(pair.server, version, true);
const { pub, origin } = publisher(pair.server, { requiresSolicitation: false, session });
const early = publish(origin, Path.from("early-cam"));

const subscription = await Stream.open(pair.client, { version });
const accepted = await Stream.accept(pair.server, version);
if (!accepted) throw new Error("missing subscription");
const run = pub.runSubscribeNamespace(new SubscribeNamespace({ requestId: 1n, namespace: Path.empty() }), accepted);

try {
expect(await subscription.reader.u53()).toBe(RequestOk.id);
await RequestOk.decode(subscription.reader, version);

expect(await subscription.reader.u53()).toBe(SubscribeNamespaceEntry.id);
expect((await SubscribeNamespaceEntry.decode(subscription.reader, version)).suffix).toBe(
Path.from("early-cam"),
);

publish(origin, Path.from("late-cam"));
expect(await subscription.reader.u53()).toBe(SubscribeNamespaceEntry.id);
expect((await SubscribeNamespaceEntry.decode(subscription.reader, version)).suffix).toBe(Path.from("late-cam"));

early.close();
expect(await subscription.reader.u53()).toBe(SubscribeNamespaceEntryDone.id);
expect((await SubscribeNamespaceEntryDone.decode(subscription.reader, version)).suffix).toBe(
Path.from("early-cam"),
);
} finally {
subscription.close();
origin.close();
await run;
}
});

for (const version of [Version.DRAFT_15, Version.DRAFT_19] as const) {
for (const declared of [false, true]) {
for (const solicited of [false, true]) {
Expand Down
53 changes: 36 additions & 17 deletions js/net/src/ietf/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
type SubscribeNamespace,
SubscribeNamespaceEntry,
SubscribeNamespaceEntryDone,
SubscribeNamespaceError,
SubscribeNamespaceOk,
} from "./subscribe_namespace.ts";
import type { TrackStatusRequest } from "./track.ts";
Expand Down Expand Up @@ -234,9 +235,10 @@ export class Publisher {
#hidden: boolean;

// The origin this session serves, borrowed: it outlives the session, and closing the
// session leaves its broadcasts alone. The namespaces are advertised with an unsolicited
// PUBLISH_NAMESPACE (see {@link runPublishNamespaces}), or on request if the peer asked
// for that (see {@link runSubscribeNamespace}).
// session leaves its broadcasts alone. Namespaces go out as an unsolicited
// PUBLISH_NAMESPACE (see {@link runPublishNamespaces}) unless the peer asked to be
// told on request. On draft-16 and later {@link runSubscribeNamespace} also carries
// every match: a NAMESPACE is discovery, not a second route.
#advertised: Getter<Advertisements | undefined>;
#publish?: OriginConsumer;

Expand Down Expand Up @@ -853,12 +855,12 @@ export class Publisher {
/**
* Handles an incoming SUBSCRIBE_NAMESPACE on a bidi stream.
*
* This carries the advertisements when the peer asked to be told on request (MoQ
* Solicit); otherwise {@link runPublishNamespaces} has already announced everything
* visible and repeating it here would leave the peer holding two sources for one
* broadcast, so only the hidden namespaces it may see ride here (MoQ Hidden).
* Draft-16+ streams Namespace entries inline; draft-14/15 predate those messages, so
* each advertisement is a PUBLISH_NAMESPACE request of its own.
* On draft-16 and later every visible match is a NAMESPACE on this stream, whatever
* the peer's SETUP said. A NAMESPACE is discovery, not a route, so a peer that also
* hears the unsolicited PUBLISH_NAMESPACE still gets the match here. Draft-14/15
* predate those messages, so each advertisement is a PUBLISH_NAMESPACE request, and
* only for what {@link runPublishNamespaces} does not already say (the whole set when
* the peer asked to be told on request, otherwise the hidden remainder).
*
* @internal
*/
Expand Down Expand Up @@ -896,12 +898,13 @@ export class Publisher {
await ok.encode(stream.writer, version);
}

// Hidden namespaces are left out unless the peer opted in (MoQ Hidden). Unless the
// peer asked to be told only on request, it has already heard everything visible
// from the empty prefix unasked, so this stream carries only what that hid.
// Hidden namespaces are left out unless the peer opted in (MoQ Hidden). Draft-16
// and later carry every remaining match. Draft-14/15 only add what the
// unsolicited loop cannot say.
const visible = (covered: Path.Valid) => !this.#hidden || msg.hidden || !hiddenBelow(prefix, covered);
const carries = (covered: Path.Valid) =>
(!this.#hidden || msg.hidden || !hiddenBelow(prefix, covered)) &&
(this.#requiresSolicitation || (this.#hidden && hiddenBelow(Path.empty(), covered)));
visible(covered) &&
(legacy ? this.#requiresSolicitation || (this.#hidden && hiddenBelow(Path.empty(), covered)) : true);

// Inline entries always land, and the receiver treats a repeated NAMESPACE as a
// replacement, so repricing one is just sending it again, a new original
Expand Down Expand Up @@ -982,9 +985,10 @@ export class Publisher {
*
* The peers that never send SUBSCRIBE_NAMESPACE are exactly the ones expecting a
* publisher to announce itself, so announcing is the default. A peer that would
* rather ask says so in its SETUP (MoQ Solicit) and this does nothing, leaving
* {@link runSubscribeNamespace} to carry the advertisements instead. Exactly one of
* the two is live, so the peer never hears a namespace twice.
* rather ask says so in its SETUP (MoQ Solicit) and this does nothing. On draft-16
* and later {@link runSubscribeNamespace} also carries every match, so a peer that
* did not ask hears each namespace both ways. A NAMESPACE is discovery only, not a
* second route.
*
* @internal
*/
Expand Down Expand Up @@ -1356,6 +1360,21 @@ export class Publisher {
await this.#refuseUnsupported(stream, msg.requestId, "track_status", 0x0f, "TRACK_STATUS is not supported");
}

/**
* Refuses a draft-16/17 SUBSCRIBE_NAMESPACE that asks for PUBLISH alone: we never send PUBLISH.
*
* @internal
*/
async refuseSubscribeNamespace(requestId: bigint, stream: Stream) {
await this.#refuseUnsupported(
stream,
requestId,
"subscribe_namespace",
SubscribeNamespaceError.id,
"SUBSCRIBE_NAMESPACE for PUBLISH is not supported",
);
}

/**
* Refuses an incoming SUBSCRIBE_TRACKS (draft-18+): we never send the PUBLISH it asks for.
* The reply carries no Request ID, so the message body is left unread.
Expand Down
Loading
Loading