コード例 #1
0
ファイル: dns.js プロジェクト: octaviansoldea/node
// lookupService(address, port, callback)
function lookupService(host, port, callback) {
  if (arguments.length !== 3)
    throw new ERR_MISSING_ARGS('host', 'port', 'callback');

  if (isIP(host) === 0)
    throw new ERR_INVALID_OPT_VALUE('host', host);

  if (!isLegalPort(port))
    throw new ERR_SOCKET_BAD_PORT(port);

  if (typeof callback !== 'function')
    throw new ERR_INVALID_CALLBACK();

  port = +port;

  var req = new GetNameInfoReqWrap();
  req.callback = callback;
  req.host = host;
  req.port = port;
  req.oncomplete = onlookupservice;

  var err = cares.getnameinfo(req, host, port);
  if (err) throw dnsException(err, 'getnameinfo', host);
  return req;
}
コード例 #2
0
ファイル: dgram.js プロジェクト: dnalborczyk/node
function validatePort(port) {
  const legal = isLegalPort(port);
  if (legal)
    port = port | 0;

  if (!legal || port === 0)
    throw new ERR_SOCKET_BAD_PORT(port);

  return port;
}
コード例 #3
0
ファイル: promises.js プロジェクト: reneweb/node
function lookupService(hostname, port) {
  if (arguments.length !== 2)
    throw new ERR_MISSING_ARGS('hostname', 'port');

  if (isIP(hostname) === 0)
    throw new ERR_INVALID_OPT_VALUE('hostname', hostname);

  if (!isLegalPort(port))
    throw new ERR_SOCKET_BAD_PORT(port);

  return createLookupServicePromise(hostname, +port);
}
コード例 #4
0
ファイル: master.js プロジェクト: Frrank1/node
function createWorkerProcess(id, env) {
  const workerEnv = util._extend({}, process.env);
  const execArgv = cluster.settings.execArgv.slice();
  const debugArgRegex = /--inspect(?:-brk|-port)?|--debug-port/;
  const nodeOptions = process.env.NODE_OPTIONS ?
    process.env.NODE_OPTIONS : '';

  util._extend(workerEnv, env);
  workerEnv.NODE_UNIQUE_ID = '' + id;

  if (execArgv.some((arg) => arg.match(debugArgRegex)) ||
      nodeOptions.match(debugArgRegex)) {
    let inspectPort;
    if ('inspectPort' in cluster.settings) {
      if (typeof cluster.settings.inspectPort === 'function')
        inspectPort = cluster.settings.inspectPort();
      else
        inspectPort = cluster.settings.inspectPort;

      if (!isLegalPort(inspectPort)) {
        throw new ERR_SOCKET_BAD_PORT(inspectPort);
      }
    } else {
      inspectPort = process.debugPort + debugPortOffset;
      if (inspectPort > maxPort)
        inspectPort = inspectPort - maxPort + minPort - 1;
      debugPortOffset++;
    }

    execArgv.push(`--inspect-port=${inspectPort}`);
  }

  return fork(cluster.settings.exec, cluster.settings.args, {
    cwd: cluster.settings.cwd,
    env: workerEnv,
    silent: cluster.settings.silent,
    windowsHide: cluster.settings.windowsHide,
    execArgv: execArgv,
    stdio: cluster.settings.stdio,
    gid: cluster.settings.gid,
    uid: cluster.settings.uid
  });
}
コード例 #5
0
ファイル: master.js プロジェクト: vitorfhc/node
function createWorkerProcess(id, env) {
  const workerEnv = util._extend({}, process.env);
  const execArgv = cluster.settings.execArgv.slice();
  const debugArgRegex = /--inspect(?:-brk|-port)?|--debug-port/;

  util._extend(workerEnv, env);
  workerEnv.NODE_UNIQUE_ID = '' + id;

  if (execArgv.some((arg) => arg.match(debugArgRegex))) {
    let inspectPort;
    if ('inspectPort' in cluster.settings) {
      if (typeof cluster.settings.inspectPort === 'function')
        inspectPort = cluster.settings.inspectPort();
      else
        inspectPort = cluster.settings.inspectPort;

      if (!isLegalPort(inspectPort)) {
        throw new TypeError('cluster.settings.inspectPort' +
          ' is invalid');
      }
    } else {
      inspectPort = process.debugPort + debugPortOffset;
      debugPortOffset++;
    }

    execArgv.push(`--inspect-port=${inspectPort}`);
  }

  return fork(cluster.settings.exec, cluster.settings.args, {
    env: workerEnv,
    silent: cluster.settings.silent,
    windowsHide: cluster.settings.windowsHide,
    execArgv: execArgv,
    stdio: cluster.settings.stdio,
    gid: cluster.settings.gid,
    uid: cluster.settings.uid
  });
}
コード例 #6
0
Server.prototype.listen = function(...args) {
  var normalized = normalizeArgs(args);
  var options = normalized[0];
  var cb = normalized[1];

  if (this._handle) {
    throw new errors.Error('ERR_SERVER_ALREADY_LISTEN');
  }

  var hasCallback = (cb !== null);
  if (hasCallback) {
    this.once('listening', cb);
  }
  var backlogFromArgs =
    // (handle, backlog) or (path, backlog) or (port, backlog)
    toNumber(args.length > 1 && args[1]) ||
    toNumber(args.length > 2 && args[2]);  // (port, host, backlog)

  options = options._handle || options.handle || options;
  // (handle[, backlog][, cb]) where handle is an object with a handle
  if (options instanceof TCP) {
    this._handle = options;
    this[async_id_symbol] = this._handle.getAsyncId();
    listenInCluster(this, null, -1, -1, backlogFromArgs);
    return this;
  }
  // (handle[, backlog][, cb]) where handle is an object with a fd
  if (typeof options.fd === 'number' && options.fd >= 0) {
    listenInCluster(this, null, null, null, backlogFromArgs, options.fd);
    return this;
  }

  // ([port][, host][, backlog][, cb]) where port is omitted,
  // that is, listen(), listen(null), listen(cb), or listen(null, cb)
  // or (options[, cb]) where options.port is explicitly set as undefined or
  // null, bind to an arbitrary unused port
  if (args.length === 0 || typeof args[0] === 'function' ||
      (typeof options.port === 'undefined' && 'port' in options) ||
      options.port === null) {
    options.port = 0;
  }
  // ([port][, host][, backlog][, cb]) where port is specified
  // or (options[, cb]) where options.port is specified
  // or if options.port is normalized as 0 before
  var backlog;
  if (typeof options.port === 'number' || typeof options.port === 'string') {
    if (!isLegalPort(options.port)) {
      throw new errors.RangeError('ERR_SOCKET_BAD_PORT', options.port);
    }
    backlog = options.backlog || backlogFromArgs;
    // start TCP server listening on host:port
    if (options.host) {
      lookupAndListen(this, options.port | 0, options.host, backlog,
                      options.exclusive);
    } else { // Undefined host, listens on unspecified address
      // Default addressType 4 will be used to search for master server
      listenInCluster(this, null, options.port | 0, 4,
                      backlog, undefined, options.exclusive);
    }
    return this;
  }

  // (path[, backlog][, cb]) or (options[, cb])
  // where path or options.path is a UNIX domain socket or Windows pipe
  if (options.path && isPipeName(options.path)) {
    var pipeName = this._pipeName = options.path;
    backlog = options.backlog || backlogFromArgs;
    listenInCluster(this, pipeName, -1, -1,
                    backlog, undefined, options.exclusive);
    return this;
  }

  throw new errors.Error('ERR_INVALID_OPT_VALUE',
                         'options',
                         util.inspect(options));
};
コード例 #7
0
function lookupAndConnect(self, options) {
  var host = options.host || 'localhost';
  var port = options.port;
  var localAddress = options.localAddress;
  var localPort = options.localPort;

  if (localAddress && !cares.isIP(localAddress)) {
    throw new errors.TypeError('ERR_INVALID_IP_ADDRESS', localAddress);
  }

  if (localPort && typeof localPort !== 'number') {
    throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
                               'options.localPort',
                               'number',
                               localPort);
  }

  if (typeof port !== 'undefined') {
    if (typeof port !== 'number' && typeof port !== 'string') {
      throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
                                 'options.port',
                                 ['number', 'string'],
                                 port);
    }
    if (!isLegalPort(port)) {
      throw new errors.RangeError('ERR_SOCKET_BAD_PORT', port);
    }
  }
  port |= 0;

  // If host is an IP, skip performing a lookup
  var addressType = cares.isIP(host);
  if (addressType) {
    nextTick(self[async_id_symbol], function() {
      if (self.connecting)
        internalConnect(self, host, port, addressType, localAddress, localPort);
    });
    return;
  }

  if (options.lookup && typeof options.lookup !== 'function')
    throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
                               'options.lookup',
                               'Function',
                               options.lookup);

  var dnsopts = {
    family: options.family,
    hints: options.hints || 0
  };

  if (dnsopts.family !== 4 && dnsopts.family !== 6 && dnsopts.hints === 0) {
    dnsopts.hints = dns.ADDRCONFIG;
  }

  debug('connect: find host', host);
  debug('connect: dns options', dnsopts);
  self._host = host;
  var lookup = options.lookup || dns.lookup;
  setInitTriggerId(self[async_id_symbol]);
  lookup(host, dnsopts, function emitLookup(err, ip, addressType) {
    self.emit('lookup', err, ip, addressType, host);

    // It's possible we were destroyed while looking this up.
    // XXX it would be great if we could cancel the promise returned by
    // the look up.
    if (!self.connecting) return;

    if (err) {
      // net.createConnection() creates a net.Socket object and
      // immediately calls net.Socket.connect() on it (that's us).
      // There are no event listeners registered yet so defer the
      // error event to the next tick.
      err.host = options.host;
      err.port = options.port;
      err.message = err.message + ' ' + options.host + ':' + options.port;
      process.nextTick(connectErrorNT, self, err);
    } else {
      self._unrefTimer();
      internalConnect(self,
                      ip,
                      port,
                      addressType,
                      localAddress,
                      localPort);
    }
  });
}
コード例 #8
0
ファイル: test-net-internal.js プロジェクト: Frrank1/node
bad.forEach((i) => assert(!isLegalPort(i)));
コード例 #9
0
ファイル: test-net-internal.js プロジェクト: Frrank1/node
'use strict';

// Flags: --expose-internals

require('../common');
const assert = require('assert');
const isLegalPort = require('internal/net').isLegalPort;

for (let n = 0; n <= 0xFFFF; n++) {
  assert(isLegalPort(n));
  assert(isLegalPort(String(n)));
  assert(`0x${n.toString(16)}`);
  assert(`0o${n.toString(8)}`);
  assert(`0b${n.toString(2)}`);
}

const bad = [-1, 'a', {}, [], false, true, 0xFFFF + 1, Infinity,
             -Infinity, NaN, undefined, null, '', ' ', 1.1, '0x',
             '-0x1', '-0o1', '-0b1', '0o', '0b'];
bad.forEach((i) => assert(!isLegalPort(i)));
コード例 #10
0
ファイル: net.js プロジェクト: hegdeashwin/node
Server.prototype.listen = function(...args) {
  var normalized = normalizeArgs(args);
  var options = normalized[0];
  var cb = normalized[1];

  if (this._handle) {
    throw new ERR_SERVER_ALREADY_LISTEN();
  }

  if (cb !== null) {
    this.once('listening', cb);
  }
  var backlogFromArgs =
    // (handle, backlog) or (path, backlog) or (port, backlog)
    toNumber(args.length > 1 && args[1]) ||
    toNumber(args.length > 2 && args[2]);  // (port, host, backlog)

  options = options._handle || options.handle || options;
  const flags = getFlags(options.ipv6Only);
  // (handle[, backlog][, cb]) where handle is an object with a handle
  if (options instanceof TCP) {
    this._handle = options;
    this[async_id_symbol] = this._handle.getAsyncId();
    listenInCluster(this, null, -1, -1, backlogFromArgs);
    return this;
  }
  // (handle[, backlog][, cb]) where handle is an object with a fd
  if (typeof options.fd === 'number' && options.fd >= 0) {
    listenInCluster(this, null, null, null, backlogFromArgs, options.fd);
    return this;
  }

  // ([port][, host][, backlog][, cb]) where port is omitted,
  // that is, listen(), listen(null), listen(cb), or listen(null, cb)
  // or (options[, cb]) where options.port is explicitly set as undefined or
  // null, bind to an arbitrary unused port
  if (args.length === 0 || typeof args[0] === 'function' ||
      (typeof options.port === 'undefined' && 'port' in options) ||
      options.port === null) {
    options.port = 0;
  }
  // ([port][, host][, backlog][, cb]) where port is specified
  // or (options[, cb]) where options.port is specified
  // or if options.port is normalized as 0 before
  var backlog;
  if (typeof options.port === 'number' || typeof options.port === 'string') {
    if (!isLegalPort(options.port)) {
      throw new ERR_SOCKET_BAD_PORT(options.port);
    }
    backlog = options.backlog || backlogFromArgs;
    // start TCP server listening on host:port
    if (options.host) {
      lookupAndListen(this, options.port | 0, options.host, backlog,
                      options.exclusive, flags);
    } else { // Undefined host, listens on unspecified address
      // Default addressType 4 will be used to search for master server
      listenInCluster(this, null, options.port | 0, 4,
                      backlog, undefined, options.exclusive);
    }
    return this;
  }

  // (path[, backlog][, cb]) or (options[, cb])
  // where path or options.path is a UNIX domain socket or Windows pipe
  if (options.path && isPipeName(options.path)) {
    var pipeName = this._pipeName = options.path;
    backlog = options.backlog || backlogFromArgs;
    listenInCluster(this, pipeName, -1, -1,
                    backlog, undefined, options.exclusive);

    if (!this._handle) {
      // Failed and an error shall be emitted in the next tick.
      // Therefore, we directly return.
      return this;
    }

    let mode = 0;
    if (options.readableAll === true)
      mode |= PipeConstants.UV_READABLE;
    if (options.writableAll === true)
      mode |= PipeConstants.UV_WRITABLE;
    if (mode !== 0) {
      const err = this._handle.fchmod(mode);
      if (err) {
        this._handle.close();
        this._handle = null;
        throw errnoException(err, 'uv_pipe_chmod');
      }
    }
    return this;
  }

  if (!(('port' in options) || ('path' in options))) {
    throw new ERR_INVALID_ARG_VALUE('options', options,
                                    'must have the property "port" or "path"');
  }

  throw new ERR_INVALID_OPT_VALUE('options', util.inspect(options));
};
コード例 #11
0
'use strict';

// Flags: --expose-internals

require('../common');
const assert = require('assert');
const isLegalPort = require('internal/net').isLegalPort;

for (let n = 0; n <= 0xFFFF; n++) {
  assert(isLegalPort(n));
  assert(isLegalPort('' + n));
  assert(`0x${n.toString(16)}`);
  assert(`0o${n.toString(8)}`);
  assert(`0b${n.toString(2)}`);
}

const bad = [-1, 'a', {}, [], false, true, 0xFFFF + 1, Infinity,
             -Infinity, NaN, undefined, null, '', ' ', 1.1, '0x',
             '-0x1', '-0o1', '-0b1', '0o', '0b'];
bad.forEach((i) => assert(!isLegalPort(i)));