net: support DNS hints in createConnection()
This commit adds support for passing DNS lookup hints to createConnection(). PR-URL: https://github.com/nodejs/node/pull/6000 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
d939152230
commit
39de601e1c
@ -375,6 +375,8 @@ For TCP sockets, `options` argument should be an object which specifies:
|
|||||||
|
|
||||||
- `family` : Version of IP stack. Defaults to `4`.
|
- `family` : Version of IP stack. Defaults to `4`.
|
||||||
|
|
||||||
|
- `hints`: [`dns.lookup()` hints][]. Defaults to `0`.
|
||||||
|
|
||||||
- `lookup` : Custom lookup function. Defaults to `dns.lookup`.
|
- `lookup` : Custom lookup function. Defaults to `dns.lookup`.
|
||||||
|
|
||||||
For local domain sockets, `options` argument should be an object which
|
For local domain sockets, `options` argument should be an object which
|
||||||
@ -720,6 +722,7 @@ Returns true if input is a version 6 IP address, otherwise returns false.
|
|||||||
[`connect()`]: #net_socket_connect_options_connectlistener
|
[`connect()`]: #net_socket_connect_options_connectlistener
|
||||||
[`destroy()`]: #net_socket_destroy
|
[`destroy()`]: #net_socket_destroy
|
||||||
[`dns.lookup()`]: dns.html#dns_dns_lookup_hostname_options_callback
|
[`dns.lookup()`]: dns.html#dns_dns_lookup_hostname_options_callback
|
||||||
|
[`dns.lookup()` hints]: #dns_supported_getaddrinfo_flags
|
||||||
[`end()`]: #net_socket_end_data_encoding
|
[`end()`]: #net_socket_end_data_encoding
|
||||||
[`EventEmitter`]: events.html#events_class_events_eventemitter
|
[`EventEmitter`]: events.html#events_class_events_eventemitter
|
||||||
[`net.Socket`]: #net_class_net_socket
|
[`net.Socket`]: #net_class_net_socket
|
||||||
|
@ -946,10 +946,10 @@ function lookupAndConnect(self, options) {
|
|||||||
|
|
||||||
var dnsopts = {
|
var dnsopts = {
|
||||||
family: options.family,
|
family: options.family,
|
||||||
hints: 0
|
hints: options.hints || 0
|
||||||
};
|
};
|
||||||
|
|
||||||
if (dnsopts.family !== 4 && dnsopts.family !== 6) {
|
if (dnsopts.family !== 4 && dnsopts.family !== 6 && dnsopts.hints === 0) {
|
||||||
dnsopts.hints = dns.ADDRCONFIG;
|
dnsopts.hints = dns.ADDRCONFIG;
|
||||||
// The AI_V4MAPPED hint is not supported on FreeBSD or Android,
|
// The AI_V4MAPPED hint is not supported on FreeBSD or Android,
|
||||||
// and getaddrinfo returns EAI_BADFLAGS. However, it seems to be
|
// and getaddrinfo returns EAI_BADFLAGS. However, it seems to be
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
var common = require('../common');
|
const common = require('../common');
|
||||||
var assert = require('assert');
|
const assert = require('assert');
|
||||||
var net = require('net');
|
const dns = require('dns');
|
||||||
|
const net = require('net');
|
||||||
|
|
||||||
var tcpPort = common.PORT;
|
const tcpPort = common.PORT;
|
||||||
var expectedConnections = 7;
|
const expectedConnections = 7;
|
||||||
var clientConnected = 0;
|
var clientConnected = 0;
|
||||||
var serverConnected = 0;
|
var serverConnected = 0;
|
||||||
|
|
||||||
var server = net.createServer(function(socket) {
|
const server = net.createServer(function(socket) {
|
||||||
socket.end();
|
socket.end();
|
||||||
if (++serverConnected === expectedConnections) {
|
if (++serverConnected === expectedConnections) {
|
||||||
server.close();
|
server.close();
|
||||||
@ -87,6 +88,10 @@ server.listen(tcpPort, 'localhost', function() {
|
|||||||
fail({
|
fail({
|
||||||
port: 65536
|
port: 65536
|
||||||
}, RangeError, '"port" option should be >= 0 and < 65536: 65536');
|
}, RangeError, '"port" option should be >= 0 and < 65536: 65536');
|
||||||
|
|
||||||
|
fail({
|
||||||
|
hints: (dns.ADDRCONFIG | dns.V4MAPPED) + 42,
|
||||||
|
}, TypeError, 'Invalid argument: hints must use valid flags');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Try connecting to random ports, but do so once the server is closed
|
// Try connecting to random ports, but do so once the server is closed
|
||||||
|
Loading…
x
Reference in New Issue
Block a user