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

Commit

Permalink
udp: make it possible to receive empty udp packets
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
txdv authored and trevnorris committed Jul 3, 2014
1 parent 2024706 commit a382c9a
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 8 deletions.
7 changes: 4 additions & 3 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ Socket.prototype.send = function(buffer,
if (offset < 0)
throw new RangeError('Offset should be >= 0');

if (offset >= buffer.length)
if ((length == 0 && offset > buffer.length) ||
(length > 0 && offset >= buffer.length))
throw new RangeError('Offset into buffer too large');

// Sending a zero-length datagram is kind of pointless but it _is_
Expand Down Expand Up @@ -308,7 +309,7 @@ Socket.prototype.send = function(buffer,
self.emit('error', ex);
}
else if (self._handle) {
var req = { buffer: buffer }; // Keep reference alive.
var req = { buffer: buffer, length: length }; // Keep reference alive.
if (callback) {
req.callback = callback;
req.oncomplete = afterSend;
Expand All @@ -332,7 +333,7 @@ Socket.prototype.send = function(buffer,


function afterSend(err) {
this.callback(err ? errnoException(err, 'send') : null, this.buffer.length);
this.callback(err ? errnoException(err, 'send') : null, this.length);
}


Expand Down
3 changes: 1 addition & 2 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) {
node::Utf8Value address(args[5]);
const bool have_callback = args[6]->IsTrue();

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

SendWrap* req_wrap = new SendWrap(env, req_wrap_obj, have_callback);
Expand Down Expand Up @@ -396,7 +395,7 @@ void UDPWrap::OnRecv(uv_udp_t* handle,
const uv_buf_t* buf,
const struct sockaddr* addr,
unsigned int flags) {
if (nread == 0) {
if (nread == 0 && addr == NULL) {
if (buf->base != NULL)
free(buf->base);
return;
Expand Down
57 changes: 57 additions & 0 deletions test/simple/test-dgram-empty-packet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.


var common = require('../common');
var assert = require('assert');

var fs = require('fs');
var dgram = require('dgram');
var callbacks = 0;
var client;
var timer;


client = dgram.createSocket('udp4');

client.bind(common.PORT);

function callback() {
callbacks++;
if (callbacks == 2) {
clearTimeout(timer);
client.close();
} else if (callbacks > 2) {
throw new Error("the callbacks should be called only two times");
}
}

client.on('message', function (buffer, bytes) {
callback();
});

client.send(new Buffer(1), 0, 0, common.PORT, "127.0.0.1", function (err, len) {
callback();
});

timer = setTimeout(function() {
throw new Error('Timeout');
}, 200);
5 changes: 2 additions & 3 deletions test/simple/test-dgram-oob-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ socket.send(buf, 0, 0, common.PORT, '127.0.0.1', ok); // useful? no
socket.send(buf, 0, 4, common.PORT, '127.0.0.1', ok);
socket.send(buf, 1, 3, common.PORT, '127.0.0.1', ok);
socket.send(buf, 3, 1, common.PORT, '127.0.0.1', ok);
// Since length of zero means nothing, don't error despite OOB.
socket.send(buf, 4, 0, common.PORT, '127.0.0.1', ok);

assert.throws(function() {
socket.send(buf, 0, 5, common.PORT, '127.0.0.1', assert.fail);
});
assert.throws(function() {
socket.send(buf, 2, 3, common.PORT, '127.0.0.1', assert.fail);
});
assert.throws(function() {
socket.send(buf, 4, 0, common.PORT, '127.0.0.1', assert.fail);
});
assert.throws(function() {
socket.send(buf, 4, 4, common.PORT, '127.0.0.1', assert.fail);
});
Expand Down
49 changes: 49 additions & 0 deletions test/simple/test-dgram-send-callback-buffer-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.


var common = require('../common');
var assert = require('assert');

var fs = require('fs');
var dgram = require('dgram');
var callbacks = 0;
var client, timer, buf, len, offset;


client = dgram.createSocket('udp4');

buf = new Buffer(256);
offset = 20;

len = buf.length - offset;


client.send(buf, offset, len, common.PORT, "127.0.0.1", function (err, bytes) {
assert.notEqual(bytes, buf.length);
assert.equal(bytes, buf.length - offset);
clearTimeout(timer);
client.close();
});

timer = setTimeout(function() {
throw new Error('Timeout');
}, 200);
46 changes: 46 additions & 0 deletions test/simple/test-dgram-send-empty-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.


var common = require('../common');
var assert = require('assert');

var fs = require('fs');
var dgram = require('dgram');
var callbacks = 0;
var client, timer, buf;


client = dgram.createSocket('udp4');

client.bind(common.PORT);

client.on('message', function (buffer, bytes) {
clearTimeout(timer);
client.close();
});

buf = new Buffer(0);
client.send(buf, 0, 0, common.PORT, "127.0.0.1", function (err, len) { });

timer = setTimeout(function() {
throw new Error('Timeout');
}, 200);

0 comments on commit a382c9a

Please sign in to comment.