Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit a382c9a

Browse files
txdvtrevnorris
authored andcommitted
udp: make it possible to receive empty udp packets
A udp packet can have 0 content. In that case nread will be equal to 0, but addr != NULL. Add test case for empty data gram packets and fixed test that checked for OOB when length == 0. Signed-off-by: Trevor Norris <trev.norris@gmail.com>
1 parent 2024706 commit a382c9a

File tree

6 files changed

+159
-8
lines changed

6 files changed

+159
-8
lines changed

lib/dgram.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ Socket.prototype.send = function(buffer,
258258
if (offset < 0)
259259
throw new RangeError('Offset should be >= 0');
260260

261-
if (offset >= buffer.length)
261+
if ((length == 0 && offset > buffer.length) ||
262+
(length > 0 && offset >= buffer.length))
262263
throw new RangeError('Offset into buffer too large');
263264

264265
// Sending a zero-length datagram is kind of pointless but it _is_
@@ -308,7 +309,7 @@ Socket.prototype.send = function(buffer,
308309
self.emit('error', ex);
309310
}
310311
else if (self._handle) {
311-
var req = { buffer: buffer }; // Keep reference alive.
312+
var req = { buffer: buffer, length: length }; // Keep reference alive.
312313
if (callback) {
313314
req.callback = callback;
314315
req.oncomplete = afterSend;
@@ -332,7 +333,7 @@ Socket.prototype.send = function(buffer,
332333

333334

334335
function afterSend(err) {
335-
this.callback(err ? errnoException(err, 'send') : null, this.buffer.length);
336+
this.callback(err ? errnoException(err, 'send') : null, this.length);
336337
}
337338

338339

src/udp_wrap.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) {
268268
node::Utf8Value address(args[5]);
269269
const bool have_callback = args[6]->IsTrue();
270270

271-
assert(offset < Buffer::Length(buffer_obj));
272271
assert(length <= Buffer::Length(buffer_obj) - offset);
273272

274273
SendWrap* req_wrap = new SendWrap(env, req_wrap_obj, have_callback);
@@ -396,7 +395,7 @@ void UDPWrap::OnRecv(uv_udp_t* handle,
396395
const uv_buf_t* buf,
397396
const struct sockaddr* addr,
398397
unsigned int flags) {
399-
if (nread == 0) {
398+
if (nread == 0 && addr == NULL) {
400399
if (buf->base != NULL)
401400
free(buf->base);
402401
return;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
23+
var common = require('../common');
24+
var assert = require('assert');
25+
26+
var fs = require('fs');
27+
var dgram = require('dgram');
28+
var callbacks = 0;
29+
var client;
30+
var timer;
31+
32+
33+
client = dgram.createSocket('udp4');
34+
35+
client.bind(common.PORT);
36+
37+
function callback() {
38+
callbacks++;
39+
if (callbacks == 2) {
40+
clearTimeout(timer);
41+
client.close();
42+
} else if (callbacks > 2) {
43+
throw new Error("the callbacks should be called only two times");
44+
}
45+
}
46+
47+
client.on('message', function (buffer, bytes) {
48+
callback();
49+
});
50+
51+
client.send(new Buffer(1), 0, 0, common.PORT, "127.0.0.1", function (err, len) {
52+
callback();
53+
});
54+
55+
timer = setTimeout(function() {
56+
throw new Error('Timeout');
57+
}, 200);

test/simple/test-dgram-oob-buffer.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,15 @@ socket.send(buf, 0, 0, common.PORT, '127.0.0.1', ok); // useful? no
3535
socket.send(buf, 0, 4, common.PORT, '127.0.0.1', ok);
3636
socket.send(buf, 1, 3, common.PORT, '127.0.0.1', ok);
3737
socket.send(buf, 3, 1, common.PORT, '127.0.0.1', ok);
38+
// Since length of zero means nothing, don't error despite OOB.
39+
socket.send(buf, 4, 0, common.PORT, '127.0.0.1', ok);
3840

3941
assert.throws(function() {
4042
socket.send(buf, 0, 5, common.PORT, '127.0.0.1', assert.fail);
4143
});
4244
assert.throws(function() {
4345
socket.send(buf, 2, 3, common.PORT, '127.0.0.1', assert.fail);
4446
});
45-
assert.throws(function() {
46-
socket.send(buf, 4, 0, common.PORT, '127.0.0.1', assert.fail);
47-
});
4847
assert.throws(function() {
4948
socket.send(buf, 4, 4, common.PORT, '127.0.0.1', assert.fail);
5049
});
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+
23+
var common = require('../common');
24+
var assert = require('assert');
25+
26+
var fs = require('fs');
27+
var dgram = require('dgram');
28+
var callbacks = 0;
29+
var client, timer, buf, len, offset;
30+
31+
32+
client = dgram.createSocket('udp4');
33+
34+
buf = new Buffer(256);
35+
offset = 20;
36+
37+
len = buf.length - offset;
38+
39+
40+
client.send(buf, offset, len, common.PORT, "127.0.0.1", function (err, bytes) {
41+
assert.notEqual(bytes, buf.length);
42+
assert.equal(bytes, buf.length - offset);
43+
clearTimeout(timer);
44+
client.close();
45+
});
46+
47+
timer = setTimeout(function() {
48+
throw new Error('Timeout');
49+
}, 200);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
23+
var common = require('../common');
24+
var assert = require('assert');
25+
26+
var fs = require('fs');
27+
var dgram = require('dgram');
28+
var callbacks = 0;
29+
var client, timer, buf;
30+
31+
32+
client = dgram.createSocket('udp4');
33+
34+
client.bind(common.PORT);
35+
36+
client.on('message', function (buffer, bytes) {
37+
clearTimeout(timer);
38+
client.close();
39+
});
40+
41+
buf = new Buffer(0);
42+
client.send(buf, 0, 0, common.PORT, "127.0.0.1", function (err, len) { });
43+
44+
timer = setTimeout(function() {
45+
throw new Error('Timeout');
46+
}, 200);

0 commit comments

Comments
 (0)