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

UDP - impossible to receive 0 length udp packets #7884

Closed
txdv opened this issue Jul 2, 2014 · 2 comments
Closed

UDP - impossible to receive 0 length udp packets #7884

txdv opened this issue Jul 2, 2014 · 2 comments
Labels

Comments

@txdv
Copy link

txdv commented Jul 2, 2014

First of all: You can send UDP packets 0 content (no data at all, only udp headers) over the wire.

This issue has 3 bugs, I'll start with some code and the output and then explain what is wrong.

var dgram = require('dgram');
var s = dgram.createSocket('udp4');

s.bind(1234);

s.on('message', function (buffer, bytes) {
    console.log('recv:', buffer, bytes);
});

s.send(new Buffer(2), 0, 0, 1234, "127.0.0.1", function (err, bytes) {
    console.log('send:', err, bytes);
    s.send(new Buffer(0), 0, 0, 1234, "127.0.0.1", function (err, bytes) { });
});

The output:

send: null 2
dgram.js:249
    throw new RangeError('Offset into buffer too large');
          ^
RangeError: Offset into buffer too large
    at Socket.send (dgram.js:249:11)
    at Object.callback (/home/bentkus/Projects/c/node/udp.js:12:4)
    at Object.afterSend [as oncomplete] (dgram.js:322:8)

Let's start with

send: null 2

If you take a look on

s.send(new Buffer(2), 0, 0, 1234, "127.0.0.1", function (err, bytes) {

You see that we pass 0 as the length parameter, yet we get the length of the buffer in the callback even though we sent an empty packet over there wire (I checked with wireshark) we get a 2 in return (which is the size of the buffer).

Second bug:

The callback doesn't get called, because of this guard checking for nread == 0. It should actually check (nread == 0 && addr == NULL). Remember UDP packets can have no content, so 0 length.

Third bug:

s.send(new Buffer(0), 0, 0, 1234, "127.0.0.1", function (err, bytes) { });

is throwing:

dgram.js:249
    throw new RangeError('Offset into buffer too large');
          ^
RangeError: Offset into buffer too large
    at Socket.send (dgram.js:249:11)
    at Object.callback (/home/bentkus/Projects/c/node/udp.js:12:4)
    at Object.afterSend [as oncomplete] (dgram.js:322:8)

We have an offset of 0, length of 0 and the buffer length is 0. So this exception actually shouldn't occur.

@txdv
Copy link
Author

txdv commented Jul 2, 2014

Fix for Bug 1: #7885

@txdv
Copy link
Author

txdv commented Jul 6, 2014

All bugs fixed in that pull request.

@txdv txdv closed this as completed Jul 6, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants