Skip to content

Commit 9e32c2e

Browse files
committed
dgram: fix double implicit bind error
Calling send() on an unbound socket forces an implicit bind to a random port. 332fea5 made the 'listening' event asynchronous. Unfortunately, it also introduced a bug where the implicit bind was tried more than once if send() was called again before the first bind operation completed. Address that by keeping track of the bind status and making sure that bind() is called only once. Fixes nodejs#4499.
1 parent 30bd774 commit 9e32c2e

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

lib/dgram.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ var events = require('events');
2424

2525
var UDP = process.binding('udp_wrap').UDP;
2626

27+
var BIND_STATE_UNBOUND = 0;
28+
var BIND_STATE_BINDING = 1;
29+
var BIND_STATE_BOUND = 2;
30+
2731
// lazily loaded
2832
var dns = null;
2933
var net = null;
@@ -90,7 +94,7 @@ function Socket(type, listener) {
9094

9195
this._handle = handle;
9296
this._receiving = false;
93-
this._bound = false;
97+
this._bindState = BIND_STATE_UNBOUND;
9498
this.type = type;
9599
this.fd = null; // compatibility hack
96100

@@ -116,6 +120,8 @@ Socket.prototype.bind = function(port, address, callback) {
116120

117121
// resolve address first
118122
self._handle.lookup(address, function(err, ip) {
123+
self._bindState = BIND_STATE_UNBOUND;
124+
119125
if (!self._handle)
120126
return; // handle has been closed in the mean time
121127

@@ -132,11 +138,13 @@ Socket.prototype.bind = function(port, address, callback) {
132138
self._handle.onmessage = onMessage;
133139
self._handle.recvStart();
134140
self._receiving = true;
135-
self._bound = true;
141+
self._bindState = BIND_STATE_BOUND;
136142
self.fd = -42; // compatibility hack
137143

138144
self.emit('listening');
139145
});
146+
147+
self._bindState = BIND_STATE_BINDING;
140148
};
141149

142150

@@ -175,8 +183,10 @@ Socket.prototype.send = function(buffer,
175183

176184
self._healthCheck();
177185

178-
if (!self._bound) {
186+
if (self._bindState == BIND_STATE_UNBOUND)
179187
self.bind(0, null);
188+
189+
if (self._bindState != BIND_STATE_BOUND) {
180190
self.once('listening', function() {
181191
self.send(buffer, offset, length, port, address, callback);
182192
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var dgram = require('dgram');
25+
26+
var source = dgram.createSocket('udp4');
27+
var target = dgram.createSocket('udp4');
28+
var messages = 0;
29+
30+
process.on('exit', function() {
31+
assert.equal(messages, 2);
32+
});
33+
34+
target.on('message', function(buf) {
35+
if (buf.toString() === 'abc') ++messages;
36+
if (buf.toString() === 'def') ++messages;
37+
if (messages === 2) {
38+
source.close();
39+
target.close();
40+
}
41+
});
42+
43+
target.on('listening', function() {
44+
// Second .send() call should not throw a bind error.
45+
source.send(Buffer('abc'), 0, 3, common.PORT, '127.0.0.1');
46+
source.send(Buffer('def'), 0, 3, common.PORT, '127.0.0.1');
47+
});
48+
49+
target.bind(common.PORT);

0 commit comments

Comments
 (0)