All files / lib/internal/dns utils.js

100% Statements 164/164
100% Branches 40/40
100% Functions 9/9
100% Lines 164/164

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 16516x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 1083x 1083x 16x 16x 3x 3x 16x 16x 9x 21x 21x 2x 21x 21x 9x 9x 16x 16x 67x 20x 20x 47x 47x 47x 47x 47x 47x 47x 47x 57x 16x 16x 41x 41x 41x 41x 22x 22x 22x 22x 34x 3x 3x 3x 3x 3x 3x 3x 3x 19x 19x 19x 19x 34x 18x 18x 18x 18x 18x 18x 10x 10x 18x 9x 9x 47x 47x 47x 47x 67x 1x 1x 1x 1x 1x 67x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 1034x 1034x 1034x 16x 15x 15x 15x 16x 1079x 1079x 15106x 1079x 1079x 16x 5065x 5065x 5065x 5065x 5065x 10x 10x 5065x 16x 16x 16x 24x 24x 20x 20x 4x 4x 4x 4x 4x 4x 4x 24x 16x 16x 16x 16x 16x 16x 16x 16x 16x  
'use strict';
const errors = require('internal/errors');
const { isIP } = require('internal/net');
const {
  ChannelWrap,
  strerror,
  AI_ADDRCONFIG,
  AI_V4MAPPED
} = internalBinding('cares_wrap');
const IANA_DNS_PORT = 53;
const IPv6RE = /^\[([^[\]]*)\]/;
const addrSplitRE = /(^.+?)(?::(\d+))?$/;
const {
  ERR_DNS_SET_SERVERS_FAILED,
  ERR_INVALID_ARG_TYPE,
  ERR_INVALID_IP_ADDRESS,
  ERR_INVALID_OPT_VALUE
} = errors.codes;
 
// Resolver instances correspond 1:1 to c-ares channels.
class Resolver {
  constructor() {
    this._handle = new ChannelWrap();
  }
 
  cancel() {
    this._handle.cancel();
  }
 
  getServers() {
    return this._handle.getServers().map((val) => {
      if (!val[1] || val[1] === IANA_DNS_PORT)
        return val[0];
 
      const host = isIP(val[0]) === 6 ? `[${val[0]}]` : val[0];
      return `${host}:${val[1]}`;
    });
  }
 
  setServers(servers) {
    if (!Array.isArray(servers)) {
      throw new ERR_INVALID_ARG_TYPE('servers', 'Array', servers);
    }
 
    // Cache the original servers because in the event of an error while
    // setting the servers, c-ares won't have any servers available for
    // resolution.
    const orig = this._handle.getServers();
    const newSet = [];
 
    servers.forEach((serv, index) => {
      if (typeof serv !== 'string') {
        throw new ERR_INVALID_ARG_TYPE(`servers[${index}]`, 'string', serv);
      }
      var ipVersion = isIP(serv);
 
      if (ipVersion !== 0)
        return newSet.push([ipVersion, serv, IANA_DNS_PORT]);
 
      const match = serv.match(IPv6RE);
 
      // Check for an IPv6 in brackets.
      if (match) {
        ipVersion = isIP(match[1]);
 
        if (ipVersion !== 0) {
          const port =
            parseInt(serv.replace(addrSplitRE, '$2')) || IANA_DNS_PORT;
          return newSet.push([ipVersion, match[1], port]);
        }
      }
 
      // addr::port
      const addrSplitMatch = serv.match(addrSplitRE);
 
      if (addrSplitMatch) {
        const hostIP = addrSplitMatch[1];
        const port = addrSplitMatch[2] || IANA_DNS_PORT;
 
        ipVersion = isIP(hostIP);
 
        if (ipVersion !== 0) {
          return newSet.push([ipVersion, hostIP, parseInt(port)]);
        }
      }
 
      throw new ERR_INVALID_IP_ADDRESS(serv);
    });
 
    const errorNumber = this._handle.setServers(newSet);
 
    if (errorNumber !== 0) {
      // Reset the servers to the old servers, because ares probably unset them.
      this._handle.setServers(orig.join(','));
      const err = strerror(errorNumber);
      throw new ERR_DNS_SET_SERVERS_FAILED(err, servers);
    }
  }
}
 
let defaultResolver = new Resolver();
const resolverKeys = [
  'getServers',
  'resolve',
  'resolve4',
  'resolve6',
  'resolveAny',
  'resolveCname',
  'resolveMx',
  'resolveNaptr',
  'resolveNs',
  'resolvePtr',
  'resolveSoa',
  'resolveSrv',
  'resolveTxt',
  'reverse',
];
 
function getDefaultResolver() {
  return defaultResolver;
}
 
function setDefaultResolver(resolver) {
  defaultResolver = resolver;
}
 
function bindDefaultResolver(target, source) {
  resolverKeys.forEach((key) => {
    target[key] = source[key].bind(defaultResolver);
  });
}
 
function validateHints(hints) {
  if (hints !== 0 &&
      hints !== AI_ADDRCONFIG &&
      hints !== AI_V4MAPPED &&
      hints !== (AI_ADDRCONFIG | AI_V4MAPPED)) {
    throw new ERR_INVALID_OPT_VALUE('hints', hints);
  }
}
 
let invalidHostnameWarningEmitted = false;
 
function emitInvalidHostnameWarning(hostname) {
  if (invalidHostnameWarningEmitted) {
    return;
  }
  invalidHostnameWarningEmitted = true;
  process.emitWarning(
    `The provided hostname "${hostname}" is not a valid ` +
    'hostname, and is supported in the dns module solely for compatibility.',
    'DeprecationWarning',
    'DEP0118'
  );
}
 
module.exports = {
  bindDefaultResolver,
  getDefaultResolver,
  setDefaultResolver,
  validateHints,
  Resolver,
  emitInvalidHostnameWarning,
};