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>
This commit is contained in:
parent
20247064b6
commit
a382c9a97c
@ -258,7 +258,8 @@ Socket.prototype.send = function(buffer,
|
|||||||
if (offset < 0)
|
if (offset < 0)
|
||||||
throw new RangeError('Offset should be >= 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');
|
throw new RangeError('Offset into buffer too large');
|
||||||
|
|
||||||
// Sending a zero-length datagram is kind of pointless but it _is_
|
// Sending a zero-length datagram is kind of pointless but it _is_
|
||||||
@ -308,7 +309,7 @@ Socket.prototype.send = function(buffer,
|
|||||||
self.emit('error', ex);
|
self.emit('error', ex);
|
||||||
}
|
}
|
||||||
else if (self._handle) {
|
else if (self._handle) {
|
||||||
var req = { buffer: buffer }; // Keep reference alive.
|
var req = { buffer: buffer, length: length }; // Keep reference alive.
|
||||||
if (callback) {
|
if (callback) {
|
||||||
req.callback = callback;
|
req.callback = callback;
|
||||||
req.oncomplete = afterSend;
|
req.oncomplete = afterSend;
|
||||||
@ -332,7 +333,7 @@ Socket.prototype.send = function(buffer,
|
|||||||
|
|
||||||
|
|
||||||
function afterSend(err) {
|
function afterSend(err) {
|
||||||
this.callback(err ? errnoException(err, 'send') : null, this.buffer.length);
|
this.callback(err ? errnoException(err, 'send') : null, this.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -268,7 +268,6 @@ void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) {
|
|||||||
node::Utf8Value address(args[5]);
|
node::Utf8Value address(args[5]);
|
||||||
const bool have_callback = args[6]->IsTrue();
|
const bool have_callback = args[6]->IsTrue();
|
||||||
|
|
||||||
assert(offset < Buffer::Length(buffer_obj));
|
|
||||||
assert(length <= Buffer::Length(buffer_obj) - offset);
|
assert(length <= Buffer::Length(buffer_obj) - offset);
|
||||||
|
|
||||||
SendWrap* req_wrap = new SendWrap(env, req_wrap_obj, have_callback);
|
SendWrap* req_wrap = new SendWrap(env, req_wrap_obj, have_callback);
|
||||||
@ -396,7 +395,7 @@ void UDPWrap::OnRecv(uv_udp_t* handle,
|
|||||||
const uv_buf_t* buf,
|
const uv_buf_t* buf,
|
||||||
const struct sockaddr* addr,
|
const struct sockaddr* addr,
|
||||||
unsigned int flags) {
|
unsigned int flags) {
|
||||||
if (nread == 0) {
|
if (nread == 0 && addr == NULL) {
|
||||||
if (buf->base != NULL)
|
if (buf->base != NULL)
|
||||||
free(buf->base);
|
free(buf->base);
|
||||||
return;
|
return;
|
||||||
|
57
test/simple/test-dgram-empty-packet.js
Normal file
57
test/simple/test-dgram-empty-packet.js
Normal file
@ -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);
|
@ -35,6 +35,8 @@ 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, 0, 4, common.PORT, '127.0.0.1', ok);
|
||||||
socket.send(buf, 1, 3, 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);
|
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() {
|
assert.throws(function() {
|
||||||
socket.send(buf, 0, 5, common.PORT, '127.0.0.1', assert.fail);
|
socket.send(buf, 0, 5, common.PORT, '127.0.0.1', assert.fail);
|
||||||
@ -42,9 +44,6 @@ assert.throws(function() {
|
|||||||
assert.throws(function() {
|
assert.throws(function() {
|
||||||
socket.send(buf, 2, 3, common.PORT, '127.0.0.1', assert.fail);
|
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() {
|
assert.throws(function() {
|
||||||
socket.send(buf, 4, 4, common.PORT, '127.0.0.1', assert.fail);
|
socket.send(buf, 4, 4, common.PORT, '127.0.0.1', assert.fail);
|
||||||
});
|
});
|
||||||
|
49
test/simple/test-dgram-send-callback-buffer-length.js
Normal file
49
test/simple/test-dgram-send-callback-buffer-length.js
Normal file
@ -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
test/simple/test-dgram-send-empty-buffer.js
Normal file
46
test/simple/test-dgram-send-empty-buffer.js
Normal file
@ -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);
|
Loading…
x
Reference in New Issue
Block a user