Skip to content

Commit

Permalink
Set maximum UDP payload to 65507 bytes (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
vrza authored Mar 10, 2020
1 parent 4c07628 commit 2896a68
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
13 changes: 9 additions & 4 deletions lib/winston-syslog.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,22 @@ class Syslog extends Transport {
if (!this.connected) {
this.queue.push(buffer);
} else {
// Get maximum syslog message size for the UDP transport in bytes.
// Maximum payload for the UDP transport
// 65535 − 8 bytes UDP header − 20 bytes IP header
// https://tools.ietf.org/html/rfc5426#section-3.2
const maxUdpLength = this.socket.getSendBufferSize();
// This makes sense on loopback interfaces with MTU = 65536
// For non-loopback messages, it's impossible to know in advance
// the MTU of each interface through which a packet might be routed
// https://nodejs.org/api/dgram.html
const MAX_UDP_PAYLOAD = 65507;
let offset = 0;

while (offset < buffer.length) {
this.inFlight++;
const length =
offset + maxUdpLength > buffer.length
offset + MAX_UDP_PAYLOAD > buffer.length
? buffer.length - offset
: maxUdpLength;
: MAX_UDP_PAYLOAD;
this._sendChunk(
buffer,
{ offset: offset, length: length, port: this.port, host: this.host },
Expand Down
20 changes: 13 additions & 7 deletions test/message-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const dgram = require('dgram');
const PORT = 11229;
let server;
let transport;
let maxUdpLength;
let maxUdpPayload;
let message;
let sentMessage;
let numChunks;
Expand All @@ -24,8 +24,14 @@ vows
const self = this;
server = dgram.createSocket('udp4');
server.on('listening', function () {
// Get the maximum buffer size for the current server.
maxUdpLength = server.getSendBufferSize();
// Maximum payload for the UDP transport
// 65535 − 8 bytes UDP header − 20 bytes IP header
// https://tools.ietf.org/html/rfc5426#section-3.2
// This makes sense on loopback interfaces with MTU = 65536
// For non-loopback messages, it's impossible to know in advance
// the MTU of each interface through which a packet might be routed
// https://nodejs.org/api/dgram.html
maxUdpPayload = 65507;
self.callback();
});

Expand All @@ -34,7 +40,7 @@ vows
'logging an oversize message': {
'topic': function () {
// Generate message larger than max UDP message size.
message = '#'.repeat(65000);
message = '#'.repeat(maxUdpPayload + 1000);
transport = new Syslog({
port: PORT
});
Expand All @@ -54,7 +60,7 @@ vows
assert(transport.chunkMessage.calledTwice);

sentMessage = transport.chunkMessage.getCall(0).args[0];
numChunks = Math.ceil(sentMessage.length / maxUdpLength);
numChunks = Math.ceil(sentMessage.length / maxUdpPayload);
assert.equal(numChunks, transport._sendChunk.callCount);
},
'correct chunks sent': function () {
Expand All @@ -64,9 +70,9 @@ vows
sentMessage = transport.chunkMessage.getCall(0).args[0];
while (offset < sentMessage.length) {
const length =
offset + maxUdpLength > sentMessage.length
offset + maxUdpPayload > sentMessage.length
? sentMessage.length - offset
: maxUdpLength;
: maxUdpPayload;
const buffer = Buffer.from(sentMessage);
const options = {
offset: offset,
Expand Down

0 comments on commit 2896a68

Please sign in to comment.