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

Call addMembership() for mdns server socket #375

Merged
merged 4 commits into from
Oct 14, 2023
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
39 changes: 31 additions & 8 deletions packages/matter-node.js/src/net/NetworkNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@ export class NetworkNode extends Network {
}
throw new NetworkError(`No IPv4 addresses on interface: ${netInterface}`);
} else {
if (process.platform !== "win32") {
return `::%${netInterface}`;
const multicastInterface = this.getMulticastInterfaceIpv6(netInterface, netInterfaceInfo);
if (multicastInterface === undefined) {
throw new NetworkError(`No IPv6 addresses on interface: ${netInterface}`);
}
for (const { address, family, scopeid } of netInterfaceInfo) {
if (family === "IPv6" && address.startsWith("fe80::")) {
return `::%${scopeid}`;
}
}
throw new NetworkError(`No IPv6 addresses on interface: ${netInterface}`);
return multicastInterface;
}
}

static getMembershipMulticastInterfaces(ipv4: boolean): (string | undefined)[] {
if (ipv4) {
return [undefined];
} else {
return Object.entries(networkInterfaces()).flatMap(([netInterface, netInterfaceInfo]) => {
if (netInterfaceInfo === undefined) return [];
const multicastInterface = this.getMulticastInterfaceIpv6(netInterface, netInterfaceInfo);
return multicastInterface === undefined ? [] : [multicastInterface];
});
}
}

Expand Down Expand Up @@ -73,6 +81,21 @@ export class NetworkNode extends Network {
}
}

private static getMulticastInterfaceIpv6(
netInterface: string,
netInterfaceInfo: NetworkInterfaceInfo[],
): string | undefined {
if (process.platform !== "win32") {
return `::%${netInterface}`;
}
for (const { address, family, scopeid } of netInterfaceInfo) {
if (family === "IPv6" && address.startsWith("fe80::")) {
return `::%${scopeid}`;
}
}
return undefined;
}

getNetInterfaces(): string[] {
const result = new Array<string>();
const interfaces = networkInterfaces();
Expand Down
16 changes: 15 additions & 1 deletion packages/matter-node.js/src/net/UdpChannelNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ function createDgramSocket(host: string | undefined, port: number | undefined, o
}

export class UdpChannelNode implements UdpChannel {
static async create({ listeningPort, type, listeningAddress, netInterface }: UdpChannelOptions) {
static async create({
listeningPort,
type,
listeningAddress,
netInterface,
membershipAddresses,
}: UdpChannelOptions) {
const socketOptions: dgram.SocketOptions = { type, reuseAddr: true };
if (type === "udp6") {
socketOptions.ipv6Only = true;
Expand All @@ -57,6 +63,14 @@ export class UdpChannelNode implements UdpChannel {
);
socket.setMulticastInterface(multicastInterface);
}
if (membershipAddresses !== undefined) {
const multicastInterfaces = NetworkNode.getMembershipMulticastInterfaces(type === "udp4");
for (const address of membershipAddresses) {
for (const multicastInterface of multicastInterfaces) {
socket.addMembership(address, multicastInterface);
}
}
}
return new UdpChannelNode(socket, netInterface);
}

Expand Down
1 change: 1 addition & 0 deletions packages/matter.js/src/net/UdpChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface UdpChannelOptions {
type: "udp4" | "udp6";
listeningAddress?: string;
netInterface?: string;
membershipAddresses?: string[];
}

export interface UdpChannel {
Expand Down
14 changes: 12 additions & 2 deletions packages/matter.js/src/net/UdpMulticastServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,18 @@ export class UdpMulticastServer {
listeningPort,
broadcastAddressIpv4 === undefined
? undefined
: await network.createUdpChannel({ type: "udp4", netInterface, listeningPort }),
await network.createUdpChannel({ type: "udp6", netInterface, listeningPort }),
: await network.createUdpChannel({
type: "udp4",
netInterface,
listeningPort,
membershipAddresses: [broadcastAddressIpv4],
}),
await network.createUdpChannel({
type: "udp6",
netInterface,
listeningPort,
membershipAddresses: [broadcastAddressIpv6],
}),
netInterface,
);
}
Expand Down