Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: support DNS hints in createConnection() #6000

Merged
merged 1 commit into from
Apr 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/api/net.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ For TCP sockets, `options` argument should be an object which specifies:

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

- `hints`: [`dns.lookup()` hints][]. Defaults to `0`.

- `lookup` : Custom lookup function. Defaults to `dns.lookup`.

For local domain sockets, `options` argument should be an object which
Expand Down Expand Up @@ -720,6 +722,7 @@ Returns true if input is a version 6 IP address, otherwise returns false.
[`connect()`]: #net_socket_connect_options_connectlistener
[`destroy()`]: #net_socket_destroy
[`dns.lookup()`]: dns.html#dns_dns_lookup_hostname_options_callback
[`dns.lookup()` hints]: #dns_supported_getaddrinfo_flags
[`end()`]: #net_socket_end_data_encoding
[`EventEmitter`]: events.html#events_class_events_eventemitter
[`net.Socket`]: #net_class_net_socket
Expand Down
4 changes: 2 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -944,10 +944,10 @@ function lookupAndConnect(self, options) {

var dnsopts = {
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;
// The AI_V4MAPPED hint is not supported on FreeBSD or Android,
// and getaddrinfo returns EAI_BADFLAGS. However, it seems to be
Expand Down
17 changes: 11 additions & 6 deletions test/parallel/test-net-create-connection.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';
var common = require('../common');
var assert = require('assert');
var net = require('net');
const common = require('../common');
const assert = require('assert');
const dns = require('dns');
const net = require('net');

var tcpPort = common.PORT;
var expectedConnections = 7;
const tcpPort = common.PORT;
const expectedConnections = 7;
var clientConnected = 0;
var serverConnected = 0;

var server = net.createServer(function(socket) {
const server = net.createServer(function(socket) {
socket.end();
if (++serverConnected === expectedConnections) {
server.close();
Expand Down Expand Up @@ -87,6 +88,10 @@ server.listen(tcpPort, 'localhost', function() {
fail({
port: 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
Expand Down