Browse Source

net: cleanup connect logic

Separates out the lookup logic for net.Socket. In the event
the `host` property is an IP address, the lookup is skipped.

PR-URL: https://github.com/iojs/io.js/pull/1505
Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
Reviewed-By: Yosuke Furukawa <yosuke.furukawa@gmail.com>
v2.0.2
Evan Lucas 10 years ago
parent
commit
1bef717476
  1. 119
      lib/net.js
  2. 18
      test/parallel/test-net-dns-lookup-skip.js
  3. 2
      test/parallel/test-net-dns-lookup.js

119
lib/net.js

@ -881,64 +881,77 @@ Socket.prototype.connect = function(options, cb) {
connect(self, options.path); connect(self, options.path);
} else { } else {
const dns = require('dns'); lookupAndConnect(self, options);
var host = options.host || 'localhost'; }
var port = 0; return self;
var localAddress = options.localAddress; };
var localPort = options.localPort;
var dnsopts = {
family: options.family,
hints: 0
};
if (localAddress && !exports.isIP(localAddress))
throw new TypeError('localAddress must be a valid IP: ' + localAddress);
if (localPort && typeof localPort !== 'number')
throw new TypeError('localPort should be a number: ' + localPort);
port = options.port;
if (typeof port !== 'undefined') {
if (typeof port !== 'number' && typeof port !== 'string')
throw new TypeError('port should be a number or string: ' + port);
if (!isLegalPort(port))
throw new RangeError('port should be >= 0 and < 65536: ' + port);
}
port |= 0;
if (dnsopts.family !== 4 && dnsopts.family !== 6)
dnsopts.hints = dns.ADDRCONFIG | dns.V4MAPPED;
debug('connect: find host ' + host); function lookupAndConnect(self, options) {
debug('connect: dns options ' + dnsopts); const dns = require('dns');
self._host = host; var host = options.host || 'localhost';
dns.lookup(host, dnsopts, function(err, ip, addressType) { var port = options.port;
self.emit('lookup', err, ip, addressType); var localAddress = options.localAddress;
var localPort = options.localPort;
// It's possible we were destroyed while looking this up. if (localAddress && !exports.isIP(localAddress))
// XXX it would be great if we could cancel the promise returned by throw new TypeError('localAddress must be a valid IP: ' + localAddress);
// the look up.
if (!self._connecting) return;
if (err) { if (localPort && typeof localPort !== 'number')
// net.createConnection() creates a net.Socket object and throw new TypeError('localPort should be a number: ' + localPort);
// immediately calls net.Socket.connect() on it (that's us).
// There are no event listeners registered yet so defer the if (typeof port !== 'undefined') {
// error event to the next tick. if (typeof port !== 'number' && typeof port !== 'string')
process.nextTick(connectErrorNT, self, err, options); throw new TypeError('port should be a number or string: ' + port);
} else { if (!isLegalPort(port))
self._unrefTimer(); throw new RangeError('port should be >= 0 and < 65536: ' + port);
connect(self,
ip,
port,
addressType,
localAddress,
localPort);
}
});
} }
return self; port |= 0;
};
// If host is an IP, skip performing a lookup
// TODO(evanlucas) should we hot path this for localhost?
var addressType = exports.isIP(host);
if (addressType) {
connect(self, host, port, addressType, localAddress, localPort);
return;
}
var dnsopts = {
family: options.family,
hints: 0
};
if (dnsopts.family !== 4 && dnsopts.family !== 6)
dnsopts.hints = dns.ADDRCONFIG | dns.V4MAPPED;
debug('connect: find host ' + host);
debug('connect: dns options ' + dnsopts);
self._host = host;
dns.lookup(host, dnsopts, function(err, ip, addressType) {
self.emit('lookup', err, ip, addressType);
// 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.
process.nextTick(connectErrorNT, self, err, options);
} else {
self._unrefTimer();
connect(self,
ip,
port,
addressType,
localAddress,
localPort);
}
});
}
function connectErrorNT(self, err, options) { function connectErrorNT(self, err, options) {

18
test/parallel/test-net-dns-lookup-skip.js

@ -0,0 +1,18 @@
var common = require('../common');
var assert = require('assert');
var net = require('net');
function check(addressType) {
var server = net.createServer(function(client) {
client.end();
server.close();
});
var address = addressType === 4 ? '127.0.0.1' : '::1';
server.listen(common.PORT, address, function() {
net.connect(common.PORT, address).on('lookup', assert.fail);
});
}
check(4);
common.hasIPv6 && check(6);

2
test/parallel/test-net-dns-lookup.js

@ -9,7 +9,7 @@ var server = net.createServer(function(client) {
}); });
server.listen(common.PORT, '127.0.0.1', function() { server.listen(common.PORT, '127.0.0.1', function() {
net.connect(common.PORT, '127.0.0.1').on('lookup', function(err, ip, type) { net.connect(common.PORT, 'localhost').on('lookup', function(err, ip, type) {
assert.equal(err, null); assert.equal(err, null);
assert.equal(ip, '127.0.0.1'); assert.equal(ip, '127.0.0.1');
assert.equal(type, '4'); assert.equal(type, '4');

Loading…
Cancel
Save