import { EventEmitter } from "events"; import { DNSResponseDefinition } from "./coder/DNSPacket"; import { AAAARecord } from "./coder/records/AAAARecord"; import { ARecord } from "./coder/records/ARecord"; import { NSECRecord } from "./coder/records/NSECRecord"; import { PTRRecord } from "./coder/records/PTRRecord"; import { SRVRecord } from "./coder/records/SRVRecord"; import { TXTRecord } from "./coder/records/TXTRecord"; import { Protocol } from "./index"; import { InterfaceName, IPAddress, NetworkManager, NetworkUpdate } from "./NetworkManager"; import { Announcer } from "./responder/Announcer"; /** * This enum defines some commonly used service types. * This is also referred to as service name (as of RFC 6763). * A service name must not be longer than 15 characters (RFC 6763 7.2). */ export declare const enum ServiceType { AIRDROP = "airdrop", AIRPLAY = "airplay", AIRPORT = "airport", COMPANION_LINK = "companion-link", DACP = "dacp",// digital audio control protocol (iTunes) HAP = "hap",// used by HomeKit accessories HOMEKIT = "homekit",// used by home hubs HTTP = "http", HTTP_ALT = "http_alt",// http alternate IPP = "ipp",// internet printing protocol IPPS = "ipps",// internet printing protocol over https RAOP = "raop",// remote audio output protocol scanner = "scanner",// bonjour scanning TOUCH_ABLE = "touch-able",// iPhone and iPod touch remote controllable DNS_SD = "dns-sd", PRINTER = "printer" } /** * Service options supplied when creating a new ciao service. */ export interface ServiceOptions { /** * Instance name of the service */ name: string; /** * Type of the service. */ type: ServiceType | string; /** * Optional array of subtypes of the service. * Refer to {@link ServiceType} for some known examples. */ subtypes?: (ServiceType | string)[]; /** * Port of the service. * If not supplied it must be set later via {@link CiaoService.updatePort} BEFORE advertising the service. */ port?: number; /** * The protocol the service uses. Default is TCP. */ protocol?: Protocol; /** * Defines a hostname under which the service can be reached. * The specified hostname must not include the TLD. It is a domain name in DNS presentation form, so a dot separates labels and a backslash escapes the * character after it, and each space in it becomes a dash. * If undefined, the hostname is derived from the service name as one label, in which each space and each dot becomes a dash. * Creating the service throws when a label of the hostname, the domain's among them, is empty or takes more than 63 octets, or when the hostname with * its domain takes more than 255 octets on the wire. */ hostname?: string; /** * If defined, a txt record will be published with the given service. */ txt?: ServiceTxt; /** * Adds ability to set custom domain. Will default to "local". * The domain will also be automatically appended to the hostname. * Creating the service throws when a label of the domain is empty or takes more than 63 octets, with an error naming the hostname it is appended to, or * when the domain carries the service's full name past 255 octets on the wire, with an error naming that name. */ domain?: string; /** * If defined it restricts the service to be advertised on the specified * ip addresses or interface names. * * If an interface name is specified, ANY address on that given interface will be advertised * (if an IP address of the given interface is also given in the array, it will be overridden). * If an IP address is specified, the service will only be advertised for the given addresses. * * Interface names and addresses can be mixed in the array. * If an ip address is given, the ip address must be valid at the time of service creation. * * If the service is set to advertise on a given interface, though the MDNSServer is * configured to ignore this interface, the service won't be advertised on the interface. */ restrictedAddresses?: (InterfaceName | IPAddress)[]; /** * The service won't advertise ipv6 address records. * This can be used to simulate binding on 0.0.0.0. * May be combined with {@link restrictedAddresses}. */ disabledIpv6?: boolean; } /** * A service txt consist of multiple key=value pairs, * which get advertised on the network. */ export type ServiceTxt = Record; /** * @private */ export declare const enum ServiceState { UNANNOUNCED = "unannounced", PROBING = "probing", PROBED = "probed",// service was probed to be unique ANNOUNCING = "announcing",// service is in the process of being announced ANNOUNCED = "announced" } /** * @private */ export interface ServiceRecords { ptr: PTRRecord; subtypePTRs?: PTRRecord[]; metaQueryPtr: PTRRecord; srv: SRVRecord; txt: TXTRecord; serviceNSEC: NSECRecord; a: Record; aaaa: Record; aaaaR: Record; aaaaULA: Record; reverseAddressPTRs: Record; } /** * Events thrown by a CiaoService */ export declare const enum ServiceEvent { /** * Event is called when the Prober identifies that the name for the service is already used * and thus resolve the name conflict by adjusting the name (e.g. adding ' (2)' to the name). * A name the number would carry past the 63 octets of a DNS label, or past the 255 of a DNS name with * the rest of the service's name, is shortened first, on a character boundary, so the new name can be shorter than the old one. * This change must be persisted and thus a listener must hook up to this event * in order for the name to be persisted. */ NAME_CHANGED = "name-change", /** * Event is called when the Prober identifies that the hostname for the service is already used * and thus resolve the name conflict by adjusting the hostname (e.g. adding '-(2)' to its last label, ahead of the domain). * That label is shortened first, as the name is, when the number would carry it past the 63 octets of a DNS label or the * hostname past the 255 of a DNS name. The event reports the hostname without the domain. * The name change must be persisted. As the hostname is an optional parameter, it is derived * from the service name if not supplied. * If you supply a custom hostname (not automatically derived from the service name) you must * hook up a listener to this event in order for the hostname to be persisted. */ HOSTNAME_CHANGED = "hostname-change" } /** * Events thrown by a CiaoService, internal use only! * @private */ export declare const enum InternalServiceEvent { PUBLISH = "publish", UNPUBLISH = "unpublish", REPUBLISH = "republish", RECORD_UPDATE = "records-update" } /** * @private */ export type PublishCallback = (error?: Error) => void; /** * @private */ export type UnpublishCallback = (error?: Error) => void; /** * @private */ export type RecordsUpdateCallback = (error?: Error | null) => void; export declare interface CiaoService { on(event: "name-change", listener: (name: string) => void): this; on(event: "hostname-change", listener: (hostname: string) => void): this; /** * @private */ on(event: InternalServiceEvent.PUBLISH, listener: (callback: PublishCallback) => void): this; /** * @private */ on(event: InternalServiceEvent.UNPUBLISH, listener: (callback: UnpublishCallback) => void): this; /** * @private */ on(event: InternalServiceEvent.REPUBLISH, listener: (callback: PublishCallback) => void): this; /** * @private */ on(event: InternalServiceEvent.RECORD_UPDATE, listener: (response: DNSResponseDefinition, callback?: (error?: Error | null) => void) => void): this; /** * @private */ emit(event: ServiceEvent.NAME_CHANGED, name: string): boolean; /** * @private */ emit(event: ServiceEvent.HOSTNAME_CHANGED, hostname: string): boolean; /** * @private */ emit(event: InternalServiceEvent.PUBLISH, callback: PublishCallback): boolean; /** * @private */ emit(event: InternalServiceEvent.UNPUBLISH, callback: UnpublishCallback): boolean; /** * @private */ emit(event: InternalServiceEvent.REPUBLISH, callback?: PublishCallback): boolean; /** * @private */ emit(event: InternalServiceEvent.RECORD_UPDATE, response: DNSResponseDefinition, callback?: (error?: Error | null) => void): boolean; } /** * The CiaoService class represents a service which can be advertised on the network. * * A service is identified by its fully qualified domain name (FQDN), which consist of * the service name, the service type, the protocol and the service domain (.local by default). * * The service defines a hostname and a port where the advertised service can be reached. * * Additionally, a TXT record can be published, which can contain information (in form of key-value pairs), * which might be useful to a querier. * * A CiaoService class is always bound to a {@link Responder} and can be created using the * {@link Responder.createService} method in the Responder class. * Once the instance is created, {@link advertise} can be called to announce the service on the network. */ export declare class CiaoService extends EventEmitter { private readonly networkManager; private name; private readonly type; private readonly subTypes?; private readonly protocol; private readonly serviceDomain; private fqdn; private loweredFqdn; private readonly typePTR; private readonly loweredTypePTR; private readonly subTypePTRs?; private hostname; private loweredHostname; private port?; private readonly restrictedAddresses?; private readonly disableIpv6?; private txt; private txtTimer?; /** * this field is entirely controlled by the Responder class * @private use by the Responder to set the current service state */ serviceState: ServiceState; /** * If service is in state {@link ServiceState.ANNOUNCING} the {@link Announcer} responsible for the * service will be linked here. This is need to cancel announcing when for example the service * should be terminated, and we still aren't fully announced yet. * @private is controlled by the {@link Responder} instance */ currentAnnouncer?: Announcer; private serviceRecords?; private destroyed; /** * Whether the service is meant to be on the network: set by {@link advertise}, and cleared only by {@link end} and {@link destroy}. A queued * advertisement reads it at its turn and is dropped once it is cleared, while the retry after a failed probe and every republish, which re-enter the * Responder without passing through {@link advertise}, find it still set. */ private advertisementIntent; /** * Constructs a new service. Please use {@link Responder.createService} to create new service. * When calling the constructor a callee must listen to certain events in order to provide * correct functionality. * @private used by the Responder instance to create a new service */ constructor(networkManager: NetworkManager, options: ServiceOptions); /** * This method start the advertising process of the service: * - The service name (and hostname) will be probed unique on all interfaces (as defined in RFC 6762 8.1). * - Once probed unique the service will be announced (as defined in RFC 6762 8.3). * * The returned promise resolves once the service was probed unique and its announcement has started. A probe query that could not be sent * is retried after a delay, and the promise resolves once a retry gets through. It also resolves, without announcing, when the advertisement * is withdrawn first: by {@link end}, by {@link destroy} or by the shutdown of the Responder. A second call while the service is still probing * resolves once that probing, and any advertisement queued behind it, has settled. The promise rejects when the service is already announced. */ advertise(): Promise; /** * This method will remove the advertisement for the service on all connected network interfaces. * If the service is still in the Probing state, probing will simply be cancelled. An advertisement still waiting for its turn to probe, * or for a retry, is dropped, and its {@link advertise} promise resolves without announcing. * * @returns Promise will resolve once the last goodbye packet was sent out, or failed to send, and at once for a service that is not announced */ end(): Promise; /** * This method must be called if you want to free the memory used by this service. * The service instance is not usable anymore after this call. * * If the service is still announced, the service will first be removed * from the network by calling {@link end}. * * @returns */ destroy(): Promise; /** * Internal lifecycle query used by {@link Responder} to avoid directly reading private state. * @private */ isDestroyed(): boolean; /** * Internal lifecycle query used by {@link Responder} to drop an advertisement that was withdrawn while it waited for its turn to probe. * @private */ hasAdvertisementIntent(): boolean; /** * @returns The fully qualified domain name of the service, used to identify the service. It is in DNS presentation form: a dot or a backslash inside the * service name is escaped with a backslash, so the service named "Mr. Coffee" is "Mr\. Coffee._hap._tcp.local.". */ getFQDN(): string; /** * @returns The service type pointer. */ getTypePTR(): string; /** * @returns Array of subtype pointers (undefined if no subtypes are specified). */ getLowerCasedSubtypePTRs(): string[] | undefined; /** * @returns The current hostname of the service. */ getHostname(): string; /** * @returns The port the service is advertising for. * {@code -1} is returned when the port is not yet set. */ getPort(): number; /** * @returns The current TXT of the service represented as Buffer array. * @private There is not need for this to be public API */ getTXT(): Buffer[]; /** * @private used for internal comparison {@link dnsLowerCase} */ getLowerCasedFQDN(): string; /** * @private used for internal comparison {@link dnsLowerCase} */ getLowerCasedTypePTR(): string; /** * @private used for internal comparison {@link dnsLowerCase} */ getLowerCasedHostname(): string; /** * Sets or updates the txt of the service. * * @param {ServiceTxt} txt - The updated txt record. * @param {boolean} silent - If set to true no announcement is sent for the updated record. */ updateTxt(txt: ServiceTxt, silent?: boolean): void; private queueTxtUpdate; private handleAnnouncementWaitFailure; /** * Sets or updates the port of the service. * A new port number can only be set when the service is still UNANNOUNCED. * Otherwise, an assertion error will be thrown. * * @param {number} port - The new port number. */ updatePort(port: number): void; /** * This method updates the name of the service. * @param name - The new service name. * @private Currently not public API and only used for bonjour conformance testing. */ updateName(name: string): Promise; private static txtBuffersFromRecord; /** * @param networkUpdate * @private */ handleNetworkInterfaceUpdate(networkUpdate: NetworkUpdate): void; /** * This method is called by the Prober when encountering a conflict on the network. * It advises the service to change its name, like incrementing a number appended to the name. * So "My Service" will become "My Service (2)", and "My Service (2)" would become "My Service (3)", while the hostname's last label * ahead of the domain takes "-(2)", so "My-Service.local." becomes "My-Service-(2).local.". A name or hostname label the number would * carry past the 63 octets of a DNS label, or a name past the 255 octets of a DNS name, is cut first, on a character boundary, so a rename * can shorten the name. When no prefix fits, the call throws with the service unchanged. * The constructor calls it with `nameCheckOnly`, which only aligns the numbers a persisted name and hostname carry and never shortens, * so an oversized name or hostname is refused there as any other is. * @private must only be called by the {@link Prober} */ incrementName(nameCheckOnly?: boolean): void; /** * @private called by the Prober once finished with probing to signal a (or more) * name change(s) happened {@see incrementName}. */ informAboutNameUpdates(): void; private formatFQDN; /** * @private called once the service data/state is updated and the records should be updated with the new data */ rebuildServiceRecords(): void; /** * Returns if the given service is advertising on the provided network interface. * * @param name - The desired interface name. The name of an interface's IPv6 socket ("en0/6") is accepted as well. * @param skipAddressCheck - If true it is not checked if the service actually has * an address record for the given interface. * @private returns if the service should be advertised on the given service */ advertisesOnInterface(name: InterfaceName, skipAddressCheck?: boolean): boolean; /** * @private used to get a copy of the main PTR record */ ptrRecord(): PTRRecord; /** * @private used to get a copy of the array of subtype PTR records */ subtypePtrRecords(): PTRRecord[]; /** * @private used to get a copy of the meta-query PTR record */ metaQueryPtrRecord(): PTRRecord; /** * @private used to get a copy of the SRV record */ srvRecord(): SRVRecord; /** * @private used to get a copy of the TXT record */ txtRecord(): TXTRecord; /** * @private used to get a copy of the A record */ aRecord(name: InterfaceName): ARecord | undefined; /** * @private used to get a copy of the AAAA record for the link-local ipv6 address */ aaaaRecord(name: InterfaceName): AAAARecord | undefined; /** * @private used to get a copy of the AAAA record for the routable ipv6 address */ aaaaRoutableRecord(name: InterfaceName): AAAARecord | undefined; /** * @private used to get a copy of the AAAA for the unique local ipv6 address */ aaaaUniqueLocalRecord(name: InterfaceName): AAAARecord | undefined; /** * @private used to get a copy of the A and AAAA records */ allAddressRecords(): (ARecord | AAAARecord)[]; /** * @param other - Another service of the same responder. * @private returns if both services publish exactly the same address records, which is what lets * them share a hostname. Two services restricted to different addresses would each keep * overwriting the other's address records for it. */ sharesAddressRecordsWith(other: CiaoService): boolean; /** * @param name - The interface's own name, which the address records are keyed by. * @private used to get the address NSEC record of an interface, or undefined for an interface on which the service holds no address record. Its bitmap * lists exactly the address types the service holds records of there: A for its A record, and AAAA for any of its link-local, routable and unique * local AAAA records. A type listed without a record keeps a querier waiting for one that never comes (RFC 6762 6.1), so the bitmap is built from the * interface's records each time it is asked for, and no stored bitmap can outlive a network change. */ addressNSECRecord(name: InterfaceName): NSECRecord | undefined; /** * @private user to get a copy of the service NSEC record */ serviceNSECRecord(shortenTTL?: boolean): NSECRecord; /** * @param address - The IP address to check. * @private used to check if given address is exposed by this service */ hasAddress(address: IPAddress): boolean; } //# sourceMappingURL=CiaoService.d.ts.map