Skip to content

Commit 57c5655

Browse files
Raymond Fengindutny
authored andcommitted
net: Ensure consistent binding to IPV6 if address is absent
See nodejs/node-v0.x-archive#7675 net.server.listen() behaves inconsistently depending on whether the port number is provided. 1. port === 0 && host == '' (i.e. false-y), node creates an AF_INET socket but does not call bind(). 2. port > 0 && host == '', node creates an AF_INET6 socket and calls bind(). The fix makes 1 consistent with 2. Signed-off-by: Fedor Indutny <fedor@indutny.com>
1 parent 3291035 commit 57c5655

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

lib/net.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,7 @@ var createServerHandle = exports._createServerHandle =
10511051
// assign handle in listen, and clean up if bind or listen fails
10521052
var handle;
10531053

1054+
var isTCP = false;
10541055
if (util.isNumber(fd) && fd >= 0) {
10551056
try {
10561057
handle = createHandle(fd);
@@ -1074,9 +1075,10 @@ var createServerHandle = exports._createServerHandle =
10741075
}
10751076
} else {
10761077
handle = createTCP();
1078+
isTCP = true;
10771079
}
10781080

1079-
if (address || port) {
1081+
if (address || port || isTCP) {
10801082
debug('bind to ' + (address || 'anycast'));
10811083
if (!address) {
10821084
// Try binding to ipv6 first

test/simple/test-net-server-address.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,65 @@ server_ipv6.listen(common.PORT, localhost_ipv6, function() {
5656
assert.strictEqual(address_ipv6.family, family_ipv6);
5757
server_ipv6.close();
5858
});
59+
60+
// Test without hostname or ip
61+
var anycast_ipv6 = '::';
62+
var server1 = net.createServer();
63+
64+
server1.on('error', function(e) {
65+
console.log('Error on ip socket: ' + e.toString());
66+
});
67+
68+
// Specify the port number
69+
server1.listen(common.PORT, function() {
70+
var address = server1.address();
71+
assert.strictEqual(address.address, anycast_ipv6);
72+
assert.strictEqual(address.port, common.PORT);
73+
assert.strictEqual(address.family, family_ipv6);
74+
server1.close();
75+
});
76+
77+
// Test without hostname or port
78+
var server2 = net.createServer();
79+
80+
server2.on('error', function (e) {
81+
console.log('Error on ip socket: ' + e.toString());
82+
});
83+
84+
// Don't specify the port number
85+
server2.listen(function () {
86+
var address = server2.address();
87+
assert.strictEqual(address.address, anycast_ipv6);
88+
assert.strictEqual(address.family, family_ipv6);
89+
server2.close();
90+
});
91+
92+
// Test without hostname, but with a false-y port
93+
var server3 = net.createServer();
94+
95+
server3.on('error', function (e) {
96+
console.log('Error on ip socket: ' + e.toString());
97+
});
98+
99+
// Specify a false-y port number
100+
server3.listen(0, function () {
101+
var address = server3.address();
102+
assert.strictEqual(address.address, anycast_ipv6);
103+
assert.strictEqual(address.family, family_ipv6);
104+
server3.close();
105+
});
106+
107+
// Test without hostname, but with port -1
108+
var server4 = net.createServer();
109+
110+
server4.on('error', function (e) {
111+
console.log('Error on ip socket: ' + e.toString());
112+
});
113+
114+
// Specify -1 as port number
115+
server4.listen(-1, function () {
116+
var address = server4.address();
117+
assert.strictEqual(address.address, anycast_ipv6);
118+
assert.strictEqual(address.family, family_ipv6);
119+
server4.close();
120+
});

0 commit comments

Comments
 (0)