net: support DNS hints in createConnection() · nodejs/node@39de601 (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -375,6 +375,8 @@ For TCP sockets, `options` argument should be an object which specifies:
375 375
376 376 - `family` : Version of IP stack. Defaults to `4`.
377 377
378 +- `hints`: [`dns.lookup()` hints][]. Defaults to `0`.
379 +
378 380 - `lookup` : Custom lookup function. Defaults to `dns.lookup`.
379 381
380 382 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.
720 722 [`connect()`]: #net_socket_connect_options_connectlistener
721 723 [`destroy()`]: #net_socket_destroy
722 724 [`dns.lookup()`]: dns.html#dns_dns_lookup_hostname_options_callback
725 +[`dns.lookup()` hints]: #dns_supported_getaddrinfo_flags
723 726 [`end()`]: #net_socket_end_data_encoding
724 727 [`EventEmitter`]: events.html#events_class_events_eventemitter
725 728 [`net.Socket`]: #net_class_net_socket
Original file line number Diff line number Diff line change
@@ -946,10 +946,10 @@ function lookupAndConnect(self, options) {
946 946
947 947 var dnsopts = {
948 948 family: options.family,
949 -hints: 0
949 +hints: options.hints |
950 950 };
951 951
952 -if (dnsopts.family !== 4 && dnsopts.family !== 6) {
952 +if (dnsopts.family !== 4 && dnsopts.family !== 6 && dnsopts.hints === 0) {
953 953 dnsopts.hints = dns.ADDRCONFIG;
954 954 // The AI_V4MAPPED hint is not supported on FreeBSD or Android,
955 955 // and getaddrinfo returns EAI_BADFLAGS. However, it seems to be
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
1 1 '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');
5 6
6 -var tcpPort = common.PORT;
7 -var expectedConnections = 7;
7 +const tcpPort = common.PORT;
8 +const expectedConnections = 7;
8 9 var clientConnected = 0;
9 10 var serverConnected = 0;
10 11
11 -var server = net.createServer(function(socket) {
12 +const server = net.createServer(function(socket) {
12 13 socket.end();
13 14 if (++serverConnected === expectedConnections) {
14 15 server.close();
@@ -87,6 +88,10 @@ server.listen(tcpPort, 'localhost', function() {
87 88 fail({
88 89 port: 65536
89 90 }, RangeError, '"port" option 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');
90 95 });
91 96
92 97 // Try connecting to random ports, but do so once the server is closed