DNS : DNS query

更新时间:
2024-05-13
下载文档

DNS : DNS query

The dns module provides a method to perform host name resolution.

User can use the following code to import the dns module.

var dns = require('dns'); // Synchronize

If the current operating environment is an EdgerOS application, you can also use the asynchronous dns module. This is recommended.

var dns = require('resolv'); // Asynchronous

Support

The following shows dns module APIs available for each permissions.

 User ModePrivilege Mode
dns.lookup
dns.gethostbyname
dns.getaddrinfo
dns.cacheTimeout
dns.cacheFlush
dns.server 

Dns Object [Synchronize]

dns.lookup(hostname[, domain])

  • hostname {String} Host name to be queried.
  • domain {Integer} socket.AF_INET or socket.AF_INET6. default: socket.AF_INET.
  • Returns: {String} IP address.

Find the IP address of the specified hostname. The domain name query process task is blocked until the query success or times out.

domain indicates the type of address required, must be socket.AF_INET or socket.AF_INET6, where the socket object is imported by the socket module:

var socket = require('socket');

Example

var ipaddr = dns.lookup('www.sylixos.com');

dns.gethostbyname(hostname[, domain])

  • hostname {String} Host name to be queried.
  • domain {Integer} socket.AF_INET or socket.AF_INET6. default: socket.AF_INET.
  • Returns: {Object} IP address object.

Same as dns.lookup() but returns object. If the query is correct, the returned object contains the following members:

  • addr {String} IP address string.
  • domain {Integer} socket.AF_INET or socket.AF_INET6.

Example

var res = dns.gethostbyname('www.acoinfo.com');

dns.getaddrinfo(hostname[, domain[, flags]])

  • hostname {String} Host name to be queried.
  • domain {Integer} socket.AF_INET or socket.AF_INET6. default: socket.AF_INET.
  • flags {Integer} Query flags. default: 0.
  • Returns: {Object} IP address object, same as dns.gethostbyname().

Find the IP address of the specified hostname. The domain name query process task is blocked until the query success or times out.

The following properties are flags which can be passed as hints for the dns.getaddrinfo() method.

  • dns.ADDRCONFIG {Number}

Returned address types are determined by the types of addresses supported by the current system.

  • dns.V4MAPPED {Number}

If the IPv6 family was specified, but no IPv6 addresses were found, then return IPv4 mapped IPv6 addresses.

dns.cacheTimeout([ms])

  • ms {Integer} DNS cache alive time in milliseconds. default: undefined (get current setting)
  • Returns: {Integer} Current DNS cache alive time.

In synchronous mode, set the current task DNS cache alive time. When ms is 0, it means that DNS result cache is not used, and the minimum effective time of ms is 10 seconds. Default setting is 300 seconds.

dns.cacheFlush([hostname])

  • hostname {String} Hostname. default: all hostname cache flush.

Flush all DNS cache. If the hostname argument is specified, only the DNS cache of the specified hostname will be flushed.

Dns Object [Asynchronous]

This is recommended in EdgerOS App.

dns.lookup(hostname[, domain], callback[, timeout])

  • hostname {String} Host name to be queried.
  • domain {Integer} socket.AF_INET or socket.AF_INET6. default: socket.AF_INET.
  • callback {Function} Query callback.
    • error {Error} Query error.
    • ipaddr {String} IP address.
  • timeout {Integer} Query timeout, the default value depends on EdgerOS related configuration.

Find the IP address of the specified hostname. The domain name query process task is blocked until the query success or times out.

domain indicates the type of address required, must be socket.AF_INET or socket.AF_INET6, where the socket object is imported by the socket module:

var socket = require('socket');

Example

dns.lookup('www.sylixos.com', function(error, ipaddr) {
  console.log(ipaddr);
});

dns.gethostbyname(hostname[, domain], callback[, timeout])

  • hostname {String} Host name to be queried.
  • domain {Integer} socket.AF_INET or socket.AF_INET6. default: socket.AF_INET.
  • callback {Function} Query callback.
    • error {Error} Query error.
    • saddr {Object} IP address object.
  • timeout {Integer} Query timeout, the default value depends on EdgerOS related configuration.

Same as dns.lookup() but returns object. If the query is correct, the returned object contains the following members:

  • addr {String} IP address string.
  • domain {Integer} socket.AF_INET or socket.AF_INET6.

Example

dns.gethostbyname('www.acoinfo.com', function(error, saddr) {
  console.log(saddr.addr);
});

dns.getaddrinfo(hostname[, domain[, flags]], callback[, timeout])

  • hostname {String} Host name to be queried.
  • domain {Integer} socket.AF_INET or socket.AF_INET6. default: socket.AF_INET.
  • flags {Integer} Query flags. default: 0.
  • callback {Function} Query callback.
    • error {Error} Query error.
    • saddr {Object} IP address object, same as dns.gethostbyname().

Find the IP address of the specified hostname. The domain name query process task is blocked until the query success or times out.

The following properties are flags which can be passed as hints for the dns.getaddrinfo() method.

  • dns.ADDRCONFIG {Number}

Returned address types are determined by the types of addresses supported by the current system.

  • dns.V4MAPPED {Number}

If the IPv6 family was specified, but no IPv6 addresses were found, then return IPv4 mapped IPv6 addresses.

dns.server([servers])

  • servers {Array} List of servers that need to be set.
  • Returns: {Array} List of servers currently in use.

Set or get a list of DNS servers. This method sets the system default DNS server list, this list only takes effect when the network interface does not have DNS set when query.

文档内容是否对您有所帮助?
有帮助
没帮助