Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multicast groups for LR nodes #7042

Merged
merged 2 commits into from
Jul 25, 2024
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
30 changes: 30 additions & 0 deletions packages/core/src/security/Manager2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,21 @@ test("createMulticastGroup() -> should return a different group ID for a differe
t.not(group1, group2);
});

test("createMulticastGroup() -> should return a different group ID for a different node set for LR nodes", (t) => {
const man = new SecurityManager2();
dummyInit(man);
const group1 = man.createMulticastGroup(
[260, 261, 262],
SecurityClass.S2_Authenticated,
);
const group2 = man.createMulticastGroup(
[259, 260, 261],
SecurityClass.S2_Authenticated,
);

t.not(group1, group2);
});

//

test("createMulticastGroup() -> should return the same group ID for a previously used node set", (t) => {
Expand All @@ -208,6 +223,21 @@ test("createMulticastGroup() -> should return the same group ID for a previously
t.is(group1, group2);
});

test("createMulticastGroup() -> should return the same group ID for a previously used LR node set", (t) => {
const man = new SecurityManager2();
dummyInit(man);
const group1 = man.createMulticastGroup(
[260, 261, 262],
SecurityClass.S2_Authenticated,
);
const group2 = man.createMulticastGroup(
[260, 261, 262],
SecurityClass.S2_Authenticated,
);

t.is(group1, group2);
});

test("getMulticastKeyAndIV() -> should throw if the MPAN state for the given multicast group has not been initialized", (t) => {
const man = new SecurityManager2();
assertZWaveError(t, () => man.getMulticastKeyAndIV(1), {
Expand Down
16 changes: 13 additions & 3 deletions packages/core/src/security/Manager2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import { createWrappingCounter, getEnumMemberName } from "@zwave-js/shared";
import * as crypto from "node:crypto";
import { deflateSync } from "node:zlib";
import { ZWaveError, ZWaveErrorCodes } from "../error/ZWaveError";
import { encodeNodeBitMask } from "../index_safe";
import { MAX_NODES_LR, encodeBitMask } from "../index_safe";
import { highResTimestamp } from "../util/date";
import { type S2SecurityClass, SecurityClass } from "./SecurityClass";
import { increment } from "./bufferUtils";
Expand Down Expand Up @@ -155,7 +156,7 @@ export class SecurityManager2 {
s2SecurityClass: S2SecurityClass,
): number {
// Check if we already have a group for these nodes
const newHash = encodeNodeBitMask(nodeIDs).toString("hex");
const newHash = hashNodeIds(nodeIDs);
if (this.multicastGroupLookup.has(newHash)) {
return this.multicastGroupLookup.get(newHash)!;
}
Expand All @@ -167,7 +168,7 @@ export class SecurityManager2 {
if (this.multicastGroups.has(groupId)) {
const oldGroup = this.multicastGroups.get(groupId)!;
this.multicastGroups.delete(groupId);
const oldHash = encodeNodeBitMask(oldGroup.nodeIDs).toString("hex");
const oldHash = hashNodeIds(oldGroup.nodeIDs);
this.multicastGroupLookup.delete(oldHash);
}

Expand Down Expand Up @@ -555,3 +556,12 @@ export class SecurityManager2 {
this.peerMPANs.get(peerNodeId)!.set(groupId, mpanState);
}
}

/** Creates a unique string that can be used to look up existing node ID arrays */
function hashNodeIds(nodeIds: readonly number[]): string {
const raw = encodeBitMask(nodeIds, MAX_NODES_LR);
// Compress the bitmask to avoid 1000 character strings as keys.
// This compresses considerably well, usually in the 12-20 byte range
const compressed = deflateSync(raw);
return compressed.toString("hex");
}