diff --git a/lib/dgram.js b/lib/dgram.js index 6ff8cf25138..761b4288293 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -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_ @@ -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; @@ -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); } diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index b392e357851..f0d49133397 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -268,7 +268,6 @@ void UDPWrap::DoSend(const FunctionCallbackInfo& 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); @@ -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; diff --git a/test/simple/test-dgram-empty-packet.js b/test/simple/test-dgram-empty-packet.js new file mode 100644 index 00000000000..00638cb8114 --- /dev/null +++ b/test/simple/test-dgram-empty-packet.js @@ -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); diff --git a/test/simple/test-dgram-oob-buffer.js b/test/simple/test-dgram-oob-buffer.js index b273057da22..061c8417cdc 100644 --- a/test/simple/test-dgram-oob-buffer.js +++ b/test/simple/test-dgram-oob-buffer.js @@ -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, 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); @@ -42,9 +44,6 @@ assert.throws(function() { 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); }); diff --git a/test/simple/test-dgram-send-callback-buffer-length.js b/test/simple/test-dgram-send-callback-buffer-length.js new file mode 100644 index 00000000000..c3b7d5ccc9e --- /dev/null +++ b/test/simple/test-dgram-send-callback-buffer-length.js @@ -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); diff --git a/test/simple/test-dgram-send-empty-buffer.js b/test/simple/test-dgram-send-empty-buffer.js new file mode 100644 index 00000000000..4faa4319701 --- /dev/null +++ b/test/simple/test-dgram-send-empty-buffer.js @@ -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);