Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8af4bb8

Browse filesBrowse files
committed
dgram: default send address to 127.0.0.1 or ::1
In net we default to 'localhost' as the default address for connect. Not doing the same on dgram is confusing, because sending to 0.0.0.0 works on Linux/OS X but not on Windows. Defaulting that to 127.0.0.1 / ::1 addresses that. Related: #5407 Related: #5398 Fixes: #5487 PR-URL: #5493 Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io>
1 parent a37401e commit 8af4bb8
Copy full SHA for 8af4bb8

File tree

Expand file treeCollapse file tree

4 files changed

+46
-18
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+46
-18
lines changed
Open diff view settings
Collapse file

‎doc/api/dgram.markdown‎

Copy file name to clipboardExpand all lines: doc/api/dgram.markdown
+1-4Lines changed: 1 addition & 4 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,7 @@ If `msg` is an array, `offset` and `length` must not be specified.
209209

210210
The `address` argument is a string. If the value of `address` is a host name,
211211
DNS will be used to resolve the address of the host. If the `address` is not
212-
specified or is an empty string, `'0.0.0.0'` or `'::0'` will be used instead.
213-
It is possible, depending on the network configuration, that these defaults
214-
may not work; accordingly, it is best to be explicit about the destination
215-
address.
212+
specified or is an empty string, `'127.0.0.1'` or `'::1'` will be used instead.
216213

217214
If the socket has not been previously bound with a call to `bind`, the socket
218215
is assigned a random port number and is bound to the "all interfaces" address
Collapse file

‎lib/dgram.js‎

Copy file name to clipboardExpand all lines: lib/dgram.js
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ function lookup(address, family, callback) {
2929

3030

3131
function lookup4(address, callback) {
32-
return lookup(address || '0.0.0.0', 4, callback);
32+
return lookup(address || '127.0.0.1', 4, callback);
3333
}
3434

3535

3636
function lookup6(address, callback) {
37-
return lookup(address || '::0', 6, callback);
37+
return lookup(address || '::1', 6, callback);
3838
}
3939

4040

@@ -166,6 +166,13 @@ Socket.prototype.bind = function(port_ /*, address, callback*/) {
166166
exclusive = false;
167167
}
168168

169+
// defaulting address for bind to all interfaces
170+
if (!address && self._handle.lookup === lookup4) {
171+
address = '0.0.0.0';
172+
} else if (!address && self._handle.lookup === lookup6) {
173+
address = '::';
174+
}
175+
169176
// resolve address first
170177
self._handle.lookup(address, function(err, ip) {
171178
if (err) {
Collapse file

‎test/parallel/test-dgram-send-default-host.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-dgram-send-default-host.js
-12Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,8 @@ const common = require('../common');
44
const assert = require('assert');
55
const dgram = require('dgram');
66

7-
if (common.isWindows) {
8-
// on Windows this test will fail
9-
// see https://github.com/nodejs/node/pull/5407
10-
console.log('1..0 # Skipped: This test does not apply on Windows.');
11-
return;
12-
}
13-
147
const client = dgram.createSocket('udp4');
158

16-
const timer = setTimeout(function() {
17-
throw new Error('Timeout');
18-
}, common.platformTimeout(2000));
19-
209
const toSend = [new Buffer(256), new Buffer(256), new Buffer(256), 'hello'];
2110

2211
toSend[0].fill('x');
@@ -36,7 +25,6 @@ client.on('message', function(buf, info) {
3625

3726
if (toSend.length === 0) {
3827
client.close();
39-
clearTimeout(timer);
4028
}
4129
});
4230

Collapse file
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const dgram = require('dgram');
6+
7+
if (!common.hasIPv6) {
8+
console.log('1..0 # Skipped: no IPv6 support');
9+
return;
10+
}
11+
12+
const client = dgram.createSocket('udp6');
13+
14+
const toSend = [new Buffer(256), new Buffer(256), new Buffer(256), 'hello'];
15+
16+
toSend[0].fill('x');
17+
toSend[1].fill('y');
18+
toSend[2].fill('z');
19+
20+
client.on('listening', function() {
21+
client.send(toSend[0], 0, toSend[0].length, common.PORT);
22+
client.send(toSend[1], common.PORT);
23+
client.send([toSend[2]], common.PORT);
24+
client.send(toSend[3], 0, toSend[3].length, common.PORT);
25+
});
26+
27+
client.on('message', function(buf, info) {
28+
const expected = toSend.shift().toString();
29+
assert.ok(buf.toString() === expected, 'message was received correctly');
30+
31+
if (toSend.length === 0) {
32+
client.close();
33+
}
34+
});
35+
36+
client.bind(common.PORT);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.