Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/api/dgram.md
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,9 @@ changes:
specified by the NAT.
* `sendBlockList` {net.BlockList} `sendBlockList` can be used for disabling outbound
access to specific IP addresses, IP ranges, or IP subnets.
* `recvErr` {boolean} When `true` the socket will emit an error event
if the peer is unreachable(On Linux only).
**Default:** `false`.
* `callback` {Function} Attached as a listener for `'message'` events. Optional.
* Returns: {dgram.Socket}

Expand Down
6 changes: 5 additions & 1 deletion lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const { FastBuffer } = require('internal/buffer');
const { UV_UDP_REUSEADDR } = internalBinding('constants').os;

const {
constants: { UV_UDP_IPV6ONLY, UV_UDP_REUSEPORT },
constants: { UV_UDP_IPV6ONLY, UV_UDP_REUSEPORT, UV_UDP_LINUX_RECVERR },
UDP,
SendWrap,
} = internalBinding('udp_wrap');
Expand Down Expand Up @@ -157,6 +157,7 @@ function Socket(type, listener) {
queue: undefined,
reuseAddr: options?.reuseAddr, // Use UV_UDP_REUSEADDR if true.
reusePort: options?.reusePort,
recvErr: options?.recvErr,
ipv6Only: options?.ipv6Only,
recvBufferSize,
sendBufferSize,
Expand Down Expand Up @@ -378,6 +379,9 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
exclusive = true;
flags |= UV_UDP_REUSEPORT;
}
if (state.recvErr) {
flags |= UV_UDP_LINUX_RECVERR;
}

if (cluster.isWorker && !exclusive) {
bindServerHandle(this, {
Expand Down
1 change: 1 addition & 0 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ void UDPWrap::Initialize(Local<Object> target,
NODE_DEFINE_CONSTANT(constants, UV_UDP_IPV6ONLY);
NODE_DEFINE_CONSTANT(constants, UV_UDP_REUSEADDR);
NODE_DEFINE_CONSTANT(constants, UV_UDP_REUSEPORT);
NODE_DEFINE_CONSTANT(constants, UV_UDP_LINUX_RECVERR);
target->Set(context,
env->constants_string(),
constants).Check();
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-dgram-recvErr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
const common = require('../common');
const dgram = require('dgram');

if (process.platform !== 'linux') {
common.skip('Only Linux is supported');
}

const socket = dgram.createSocket({ recvErr: true, type: 'udp4' });

function onError(err) {
console.error('UV_UDP_LINUX_RECVERR is unsupported: ', err);
socket.close();
}

socket.on('error', onError);
socket.bind(0, common.mustCall(() => {
socket.off('error', onError);
socket.on('error', common.mustCall(() => {
socket.close();
}));
socket.send('hello', 9999, '127.0.0.1');
}));
Loading