From 5dc182836593466892e60a6b2627a0d380b7486c Mon Sep 17 00:00:00 2001 From: Andrius Bentkus Date: Wed, 2 Jul 2014 15:24:17 +0200 Subject: [PATCH 1/6] udp: make it possible to receive empty udp packets A udp packet can have 0 content. In that case nread will be equal 0, but addr != NULL. --- src/udp_wrap.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index b392e357851..063444c699c 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -396,7 +396,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; From 694e81a8449f3629ca49999dc100176b2eb4bf9a Mon Sep 17 00:00:00 2001 From: Andrius Bentkus Date: Wed, 2 Jul 2014 15:48:41 +0200 Subject: [PATCH 2/6] test: add test case for empty data gram packets --- test/simple/test-dgram-empty-packet.js | 59 ++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 test/simple/test-dgram-empty-packet.js diff --git a/test/simple/test-dgram-empty-packet.js b/test/simple/test-dgram-empty-packet.js new file mode 100644 index 00000000000..93dff1eee09 --- /dev/null +++ b/test/simple/test-dgram-empty-packet.js @@ -0,0 +1,59 @@ +// 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'), + dgram = require('dgram'), + callbacks = 0, + client, + timer; + + +client = dgram.createSocket('udp4'); + +client.bind(common.PORT); + +function callback() { + callbacks++; + if (callbacks == 2) { + clearTimeout(timer); + client.close(); + } else if (callbacks > 2) { + throw "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); From fa914bb4eb1cbf4743e5969fbbaa6f18f75990fe Mon Sep 17 00:00:00 2001 From: Andrius Bentkus Date: Wed, 2 Jul 2014 17:08:50 +0200 Subject: [PATCH 3/6] udp: fix send so it accepts buffers with length 0 --- lib/dgram.js | 3 ++- src/udp_wrap.cc | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/dgram.js b/lib/dgram.js index 6ff8cf25138..0fdb4aa9c5c 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_ diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 063444c699c..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); From 839aa0630b11b5d8f78d1a03e14fe0a49faa9088 Mon Sep 17 00:00:00 2001 From: Andrius Bentkus Date: Wed, 2 Jul 2014 17:12:15 +0200 Subject: [PATCH 4/6] test: add test for sending empty buffers with udp.send --- test/simple/test-dgram-send-empty-buffer.js | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 test/simple/test-dgram-send-empty-buffer.js 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..ecd94ebbed8 --- /dev/null +++ b/test/simple/test-dgram-send-empty-buffer.js @@ -0,0 +1,48 @@ +// 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'), + dgram = require('dgram'), + callbacks = 0, + 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); From e668d8b9a14692a8bdfe46f3c1fdc84a6ae8b982 Mon Sep 17 00:00:00 2001 From: Andrius Bentkus Date: Wed, 2 Jul 2014 23:33:44 +0200 Subject: [PATCH 5/6] udp: pass the length of the sent size to the callback --- lib/dgram.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/dgram.js b/lib/dgram.js index 0fdb4aa9c5c..761b4288293 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -309,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; @@ -333,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); } From d7e191f1db1ac18d16bcc56d688fa4dbb2745724 Mon Sep 17 00:00:00 2001 From: Andrius Bentkus Date: Wed, 2 Jul 2014 18:31:16 +0200 Subject: [PATCH 6/6] test: add test for length udp send callback value --- .../test-dgram-send-callback-buffer-length.js | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 test/simple/test-dgram-send-callback-buffer-length.js 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..654dff739c1 --- /dev/null +++ b/test/simple/test-dgram-send-callback-buffer-length.js @@ -0,0 +1,51 @@ +// 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'), + dgram = require('dgram'), + callbacks = 0, + 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);