Skip to content

Commit d9eaa68

Browse files
author
chenyuebiao
committed
dgram: support UV_UDP_LINUX_RECVERR for udp
1 parent c202696 commit d9eaa68

4 files changed

Lines changed: 32 additions & 1 deletion

File tree

doc/api/dgram.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,9 @@ changes:
968968
specified by the NAT.
969969
* `sendBlockList` {net.BlockList} `sendBlockList` can be used for disabling outbound
970970
access to specific IP addresses, IP ranges, or IP subnets.
971+
* `recvErr` {boolean} When `true` the socket will emit an error event
972+
if the peer is unreachable(On Linux only).
973+
**Default:** `false`.
971974
* `callback` {Function} Attached as a listener for `'message'` events. Optional.
972975
* Returns: {dgram.Socket}
973976

lib/dgram.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const { FastBuffer } = require('internal/buffer');
7676
const { UV_UDP_REUSEADDR } = internalBinding('constants').os;
7777

7878
const {
79-
constants: { UV_UDP_IPV6ONLY, UV_UDP_REUSEPORT },
79+
constants: { UV_UDP_IPV6ONLY, UV_UDP_REUSEPORT, UV_UDP_LINUX_RECVERR },
8080
UDP,
8181
SendWrap,
8282
} = internalBinding('udp_wrap');
@@ -157,6 +157,7 @@ function Socket(type, listener) {
157157
queue: undefined,
158158
reuseAddr: options?.reuseAddr, // Use UV_UDP_REUSEADDR if true.
159159
reusePort: options?.reusePort,
160+
recvErr: options?.recvErr,
160161
ipv6Only: options?.ipv6Only,
161162
recvBufferSize,
162163
sendBufferSize,
@@ -378,6 +379,9 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
378379
exclusive = true;
379380
flags |= UV_UDP_REUSEPORT;
380381
}
382+
if (state.recvErr) {
383+
flags |= UV_UDP_LINUX_RECVERR;
384+
}
381385

382386
if (cluster.isWorker && !exclusive) {
383387
bindServerHandle(this, {

src/udp_wrap.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ void UDPWrap::Initialize(Local<Object> target,
233233
NODE_DEFINE_CONSTANT(constants, UV_UDP_IPV6ONLY);
234234
NODE_DEFINE_CONSTANT(constants, UV_UDP_REUSEADDR);
235235
NODE_DEFINE_CONSTANT(constants, UV_UDP_REUSEPORT);
236+
NODE_DEFINE_CONSTANT(constants, UV_UDP_LINUX_RECVERR);
236237
target->Set(context,
237238
env->constants_string(),
238239
constants).Check();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
const common = require('../common');
3+
const dgram = require('dgram');
4+
5+
if (process.platform !== 'linux') {
6+
common.skip('Only Linux is supported');
7+
}
8+
9+
const socket = dgram.createSocket({ recvErr: true, type: 'udp4' });
10+
11+
function onError(err) {
12+
console.error('UV_UDP_LINUX_RECVERR is unsupported: ', err);
13+
socket.close();
14+
}
15+
16+
socket.on('error', onError);
17+
socket.bind(0, common.mustCall(() => {
18+
socket.off('error', onError);
19+
socket.on('error', common.mustCall(() => {
20+
socket.close();
21+
}));
22+
socket.send('hello', 9999, '127.0.0.1');
23+
}));

0 commit comments

Comments
 (0)