Node.js Reference ยท HTTP, HTTPS, net, TLS, dgram and dns
Node.js dns.reverse() method
Reference for dns.reverse() and dns.lookupService() in Node.js: turn an IP address back into host names with PTR queries or the OS resolver.
dns.reverse() does the opposite of a normal lookup: given an IP address, it returns the host names that the address owner has published for it. Under the hood Node rewrites the address into a special name (203.0.113.10 becomes 10.113.0.203.in-addr.arpa) and asks for its PTR records.
Reverse DNS is mostly used for logging ("who connected to us?"), for mail servers checking a sender, and for diagnostics. Treat the answer as a hint: whoever controls the address block controls what the PTR record says, so never use it for authentication without also checking the forward direction.
import dns from 'node:dns/promises'; const names = await dns.reverse('203.0.113.10'); // ['host.example.org']
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
dns.reverse(ip, callback) // callback(err, hostnames) dnsPromises.reverse(ip): Promise<string[]> dns.lookupService(address, port, callback) // callback(err, hostname, service) dnsPromises.lookupService(address, port): Promise<{ hostname: string, service: string }>
| Name | Description |
|---|---|
ip / address |
An IPv4 or IPv6 address as a string. |
port |
For lookupService(): an integer from 0 to 65535. |
callback |
Called with the results or an error. |
Returns: reverse() gives an array of host names (possibly more than one). lookupService() gives one host name and a service name such as ssh or http, or the port as a string when no service name is known.
Errors: an invalid address makes reverse() fail with EINVAL; no PTR record gives ENOTFOUND or ENODATA. Bad arguments to lookupService() throw ERR_INVALID_ARG_VALUE (a non-address string) or ERR_SOCKET_BAD_PORT (a port out of range).
Usage notes
reverse()is a DNS query. It goes to your configured DNS servers (or aResolver's servers) and ignores the hosts file.lookupService()goes through the operating system and does use it.- Most addresses have no PTR record. Home connections, cloud instances and private ranges often return nothing, so handle
ENOTFOUNDas the normal case, not as a bug. - Several names are possible. Take the first only if any name will do; otherwise check that a name resolves forward to the same address (forward-confirmed reverse DNS).
- IPv6 uses
ip6.arpawith each hex digit as its own label; Node builds that name for you. - Never block a request on it. Reverse lookups can be slow when the server is dead. Log the IP immediately and resolve names off the hot path, with a short
timeouton a dedicatedResolver. lookupService()uses the thread pool, just likedns.lookup().- Service names come from the system's services database, so the same port can have a different name (or none) on another machine.
Related
dns.lookup(),dns.resolve(),dns.Resolver,dns/promises- Lessons: UDP and DNS