UNPKG

matterbridge

Version:
288 lines • 8.81 kB
/** * @description This file contains the class Mdns. * @file mdns.ts * @author Luca Liguori * @created 2025-03-22 * @version 1.0.0 * @license Apache-2.0 * * Copyright 2025, 2026, 2027 Luca Liguori. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import dgram from 'node:dgram'; import { Multicast } from './multicast.js'; export declare const enum DnsRecordType { A = 1, NS = 2, MD = 3, MF = 4, CNAME = 5, SOA = 6, MB = 7, MG = 8, MR = 9, NULL = 10, WKS = 11, PTR = 12, HINFO = 13, MINFO = 14, MX = 15, TXT = 16, RP = 17, AFSDB = 18, X25 = 19, ISDN = 20, RT = 21, NSAP = 22, NSAP_PTR = 23, SIG = 24, KEY = 25, PX = 26, GPOS = 27, AAAA = 28, LOC = 29, NXT = 30, EID = 31, NIMLOC = 32, SRV = 33, ATMA = 34, NAPTR = 35, KX = 36, CERT = 37, A6 = 38, DNAME = 39, SINK = 40, OPT = 41, APL = 42, DS = 43, SSHFP = 44, IPSECKEY = 45, RRSIG = 46, NSEC = 47, DNSKEY = 48, DHCID = 49, NSEC3 = 50, NSEC3PARAM = 51, TLSA = 52, SMIMEA = 53, HIP = 55, NINFO = 56, RKEY = 57, TALINK = 58, CDS = 59, CDNSKEY = 60, OPENPGPKEY = 61, CSYNC = 62, ZONEMD = 63, SVCB = 64, HTTPS = 65, SPF = 99, UINFO = 100, UID = 101, GID = 102, UNSPEC = 103, NID = 104, L32 = 105, L64 = 106, LP = 107, EUI48 = 108, EUI64 = 109, TKEY = 249, TSIG = 250, IXFR = 251, AXFR = 252, MAILB = 253, MAILA = 254, ANY = 255, URI = 256, CAA = 257, AVC = 258, DOA = 259, AMTRELAY = 260, ZONEVERSION = 261, TA = 32768, DLV = 32769 } export declare const enum DnsClass { IN = 1,// Internet CH = 3,// Chaos HS = 4,// Hesiod ANY = 255 } export declare const enum DnsClassFlag { FLUSH = 32768,// For answers (resource records) QU = 32768 } export interface MdnsMessage { id: number; qr: number; opcode: number; aa: boolean; tc: boolean; rd: boolean; ra: boolean; z: number; rcode: number; qdCount: number; anCount: number; nsCount: number; arCount: number; questions?: DnsQuestion[]; answers?: MdnsRecord[]; authorities?: MdnsRecord[]; additionals?: MdnsRecord[]; } interface DnsQuestion { name: string; type: number; class: number; } interface MdnsRecord { name: string; type: number; class: number; ttl: number; data: string; } export declare class Mdns extends Multicast { deviceQueries: Map<string, { rinfo: dgram.RemoteInfo; query: MdnsMessage; }>; deviceResponses: Map<string, { rinfo: dgram.RemoteInfo; response: MdnsMessage; dataPTR?: string; }>; /** * Creates an instance of the Mdns class. * * @param {string} name - The internal name of the mDNS server for the logs. * @param {string} multicastAddress - The multicast address for mDNS (i.e. 224.0.0.251 for udp4 or ff02::fb for udp6). * @param {number} multicastPort - The port for mDNS (i.e. 5353). * @param {('udp4' | 'udp6')} socketType - The type of socket to create (either 'udp4' or 'udp6'). * @param {boolean} [reuseAddr] - Whether to reuse the address. Defaults to true. * @param {string} [interfaceName] - The optional name of the network interface to use. * @param {string} [interfaceAddress] - The optional IP address of the network interface to use. */ constructor(name: string, multicastAddress: string, multicastPort: number, socketType: 'udp4' | 'udp6', reuseAddr?: boolean | undefined, interfaceName?: string, interfaceAddress?: string); onQuery(rinfo: dgram.RemoteInfo, _query: MdnsMessage): void; onResponse(rinfo: dgram.RemoteInfo, _response: MdnsMessage): void; onMessage(msg: Buffer, rinfo: dgram.RemoteInfo): void; /** * Decodes an mDNS message, including the header, question section, answer section, * authority section, and additional section. * * @param {Buffer} msg - The raw mDNS message buffer. * @returns {MdnsMessage} An object representing the decoded mDNS message. * @throws Error if the message is too short. */ decodeMdnsMessage(msg: Buffer): MdnsMessage; /** * Decodes a DNS name from a buffer, handling compression. * * @param {Buffer} msg - The full mDNS message buffer. * @param {number} offset - The offset at which the DNS name starts. * @returns {{ name: string; newOffset: number }} An object with the decoded name and the new offset. * @throws Error if the offset exceeds the buffer length or too many iterations are performed. */ decodeDnsName(msg: Buffer, offset: number): { name: string; newOffset: number; }; /** * Encodes a domain name into the DNS label format. * * For example, "example.local" becomes: * [7] "example" [5] "local" [0] * * @param {string} name - The domain name to encode. * @returns {Buffer} The encoded domain name as a Buffer. */ encodeDnsName(name: string): Buffer; /** * Decodes a DNS resource record. * * @param {Buffer} msg - The full mDNS message buffer. * @param {number} offset - The offset at which the resource record starts. * @returns {{ record: MdnsRecord; newOffset: number }} An object containing the decoded record and the new offset. */ decodeResourceRecord(msg: Buffer, offset: number): { record: MdnsRecord; newOffset: number; }; /** * Sends a DNS query with multiple questions. * * @param {Array<{ name: string; type: number; class: number; unicastResponse?: boolean }>} questions - Array of questions * * @remarks * Each question should have a name (e.g., "_http._tcp.local"), type (e.g., DnsRecordType.PTR), class (e.g., DnsClass.IN), * and an optional unicastResponse flag (this will add the DnsClassFlag.QU flag to the query). */ sendQuery(questions: { name: string; type: number; class: number; unicastResponse?: boolean; }[]): void; /** * Constructs an mDNS response packet and sends it to the multicast address and port. * * @param {string} name - The domain name being responded to (e.g., "example.local"). * @param {number} rtype - The response type (e.g., 1 for A, 28 for AAAA, etc.). * @param {number} rclass - The response class (typically 1 for IN). * @param {number} ttl - The time-to-live for the answer record. * @param {Buffer} rdata - The resource data for the response (e.g., 4 bytes for an A record IPv4 address). * * @example * const ptrRdata = mdnsIpv4.encodeDnsName('matterbridge._http._tcp.local'); * mdnsIpv4.sendResponse('_http._tcp.local', DnsRecordType.PTR, DnsClass.IN, 120, ptrRdata); */ sendResponse(name: string, rtype: number, rclass: number, ttl: number, rdata: Buffer): void; /** * Converts a DNS record type numeric value to its string representation. * * @param {number} type - The numeric DNS record type. * @returns {string} The string representation of the record type. */ dnsTypeToString(type: number): string; /** * Converts a DNS response class numeric value to its string representation. * * @param {number} cls - The numeric DNS class. * @returns {string} The string representation of the DNS class. */ dnsResponseClassToString(cls: number): string; /** * Converts a DNS question class to a human-readable string. * Adds support for mDNS QU (unicast-response) bit. * * @param {number} cls - The numeric question class. * @returns {string} The string representation, e.g. "IN|QU" */ dnsQuestionClassToString(cls: number): string; /** * Logs the decoded mDNS message header. * * @param {MdnsMessage} msg - The mDNS message header object. */ logMdnsMessage(msg: MdnsMessage): void; /** * Logs the discovered devices from the mDNS queries and responses. */ logDevices(): void; } export {}; //# sourceMappingURL=mdns.d.ts.map