Skip to content

Commit e00dee2

Browse files
committed
dgram: support blocklist in udp
1 parent 4cf6fab commit e00dee2

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

lib/dgram.js

+33-3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const {
5555
_createSocketHandle,
5656
newHandle,
5757
} = require('internal/dgram');
58+
const { isIP } = require('internal/net');
5859
const {
5960
isInt32,
6061
validateAbortSignal,
@@ -104,6 +105,8 @@ function Socket(type, listener) {
104105
let lookup;
105106
let recvBufferSize;
106107
let sendBufferSize;
108+
let receiveBlockList;
109+
let sendBlockList;
107110

108111
let options;
109112
if (type !== null && typeof type === 'object') {
@@ -112,6 +115,14 @@ function Socket(type, listener) {
112115
lookup = options.lookup;
113116
recvBufferSize = options.recvBufferSize;
114117
sendBufferSize = options.sendBufferSize;
118+
// TODO: validate the params
119+
// https://github.com/nodejs/node/pull/56078
120+
if (options.receiveBlockList) {
121+
receiveBlockList = options.receiveBlockList;
122+
}
123+
if (options.sendBlockList) {
124+
sendBlockList = options.sendBlockList;
125+
}
115126
}
116127

117128
const handle = newHandle(type, lookup);
@@ -134,6 +145,8 @@ function Socket(type, listener) {
134145
ipv6Only: options?.ipv6Only,
135146
recvBufferSize,
136147
sendBufferSize,
148+
receiveBlockList,
149+
sendBlockList,
137150
};
138151

139152
if (options?.signal !== undefined) {
@@ -434,9 +447,14 @@ function doConnect(ex, self, ip, address, port, callback) {
434447
return;
435448

436449
if (!ex) {
437-
const err = state.handle.connect(ip, port);
438-
if (err) {
439-
ex = new ExceptionWithHostPort(err, 'connect', address, port);
450+
if (state.sendBlockList?.check(ip, `ipv${isIP(ip)}`)) {
451+
// TODO
452+
ex = new Error();
453+
} else {
454+
const err = state.handle.connect(ip, port);
455+
if (err) {
456+
ex = new ExceptionWithHostPort(err, 'connect', address, port);
457+
}
440458
}
441459
}
442460

@@ -696,6 +714,14 @@ function doSend(ex, self, ip, list, address, port, callback) {
696714
return;
697715
}
698716

717+
if (port && state.sendBlockList?.check(ip, `ipv${isIP(ip)}`)) {
718+
if (callback) {
719+
// TODO
720+
process.nextTick(callback, new Error());
721+
}
722+
return;
723+
}
724+
699725
const req = new SendWrap();
700726
req.list = list; // Keep reference alive.
701727
req.address = address;
@@ -944,6 +970,10 @@ function onMessage(nread, handle, buf, rinfo) {
944970
if (nread < 0) {
945971
return self.emit('error', new ErrnoException(nread, 'recvmsg'));
946972
}
973+
if (self[kStateSymbol]?.receiveBlockList?.check(rinfo.address,
974+
rinfo.family.toLocaleLowerCase())) {
975+
return;
976+
}
947977
rinfo.size = buf.length; // compatibility
948978
self.emit('message', buf, rinfo);
949979
}

test/parallel/test-dgram-blocklist.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
require('../common');
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const dgram = require('dgram');
6+
const net = require('net');
7+
8+
const blockList = new net.BlockList();
9+
blockList.addAddress(common.localhostIPv4);
10+
11+
const connectSocket = dgram.createSocket({ type: 'udp4', sendBlockList: blockList });
12+
connectSocket.bind(0, common.mustCall(() => {
13+
connectSocket.connect(9999, common.localhostIPv4, common.mustCall(err => {
14+
assert.ok(err);
15+
connectSocket.close();
16+
}))
17+
}));
18+
19+
const sendSocket = dgram.createSocket({ type: 'udp4', sendBlockList: blockList });
20+
sendSocket.bind(0, common.mustCall(() => {
21+
sendSocket.send("hello", 9999, common.localhostIPv4, common.mustCall(err => {
22+
assert.ok(err);
23+
sendSocket.close();
24+
}))
25+
}));
26+
27+
const receiveSocket = dgram.createSocket({ type: 'udp4', receiveBlockList: blockList });
28+
receiveSocket.om('message', common.mustNotCall());
29+
receiveSocket.bind(0, common.mustCall(() => {
30+
const client = dgram.createSocket('udp4');
31+
client.bind(0, common.mustCall(() => {
32+
const addressInfo = receiveSocket.address();
33+
client.send(addressInfo.port, addressInfo.address, common.mustCall(() => {
34+
client.close();
35+
}));
36+
}))
37+
}));

0 commit comments

Comments
 (0)