Skip to content

Commit 6d9c0c9

Browse files
cjihrigMyles Borins
authored and
Myles Borins
committed
net: support DNS hints in createConnection()
This commit adds support for passing DNS lookup hints to createConnection(). PR-URL: #6000 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Conflicts: test/parallel/test-net-create-connection.js
1 parent d4abca5 commit 6d9c0c9

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

doc/api/net.markdown

+3
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ For TCP sockets, `options` argument should be an object which specifies:
375375

376376
- `family` : Version of IP stack. Defaults to `4`.
377377

378+
- `hints`: [`dns.lookup()` hints][]. Defaults to `0`.
379+
378380
- `lookup` : Custom lookup function. Defaults to `dns.lookup`.
379381

380382
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.
720722
[`connect()`]: #net_socket_connect_options_connectlistener
721723
[`destroy()`]: #net_socket_destroy
722724
[`dns.lookup()`]: dns.html#dns_dns_lookup_hostname_options_callback
725+
[`dns.lookup()` hints]: #dns_supported_getaddrinfo_flags
723726
[`end()`]: #net_socket_end_data_encoding
724727
[`EventEmitter`]: events.html#events_class_events_eventemitter
725728
[`net.Socket`]: #net_class_net_socket

lib/net.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -942,10 +942,10 @@ function lookupAndConnect(self, options) {
942942

943943
var dnsopts = {
944944
family: options.family,
945-
hints: 0
945+
hints: options.hints || 0
946946
};
947947

948-
if (dnsopts.family !== 4 && dnsopts.family !== 6) {
948+
if (dnsopts.family !== 4 && dnsopts.family !== 6 && dnsopts.hints === 0) {
949949
dnsopts.hints = dns.ADDRCONFIG;
950950
// The AI_V4MAPPED hint is not supported on FreeBSD or Android,
951951
// and getaddrinfo returns EAI_BADFLAGS. However, it seems to be

test/parallel/test-net-create-connection.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
'use strict';
2-
var common = require('../common');
3-
var assert = require('assert');
4-
var net = require('net');
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const dns = require('dns');
5+
const net = require('net');
56

6-
var tcpPort = common.PORT;
7-
var expectedConnections = 7;
7+
const tcpPort = common.PORT;
8+
const expectedConnections = 7;
89
var clientConnected = 0;
910
var serverConnected = 0;
1011

11-
var server = net.createServer(function(socket) {
12+
const server = net.createServer(function(socket) {
1213
socket.end();
1314
if (++serverConnected === expectedConnections) {
1415
server.close();
@@ -87,6 +88,10 @@ server.listen(tcpPort, 'localhost', function() {
8788
fail({
8889
port: 65536
8990
}, RangeError, 'port should be >= 0 and < 65536: 65536');
91+
92+
fail({
93+
hints: (dns.ADDRCONFIG | dns.V4MAPPED) + 42,
94+
}, TypeError, 'invalid argument: hints must use valid flags');
9095
});
9196

9297
// Try connecting to random ports, but do so once the server is closed

0 commit comments

Comments
 (0)