Skip to content

Commit

Permalink
dgram: support blocklist in udp
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed Nov 29, 2024
1 parent 4cf6fab commit e00dee2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
36 changes: 33 additions & 3 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const {
_createSocketHandle,
newHandle,
} = require('internal/dgram');
const { isIP } = require('internal/net');
const {
isInt32,
validateAbortSignal,
Expand Down Expand Up @@ -104,6 +105,8 @@ function Socket(type, listener) {
let lookup;
let recvBufferSize;
let sendBufferSize;
let receiveBlockList;
let sendBlockList;

let options;
if (type !== null && typeof type === 'object') {
Expand All @@ -112,6 +115,14 @@ function Socket(type, listener) {
lookup = options.lookup;
recvBufferSize = options.recvBufferSize;
sendBufferSize = options.sendBufferSize;
// TODO: validate the params
// https://github.com/nodejs/node/pull/56078
if (options.receiveBlockList) {
receiveBlockList = options.receiveBlockList;
}
if (options.sendBlockList) {
sendBlockList = options.sendBlockList;
}
}

const handle = newHandle(type, lookup);
Expand All @@ -134,6 +145,8 @@ function Socket(type, listener) {
ipv6Only: options?.ipv6Only,
recvBufferSize,
sendBufferSize,
receiveBlockList,
sendBlockList,
};

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

if (!ex) {
const err = state.handle.connect(ip, port);
if (err) {
ex = new ExceptionWithHostPort(err, 'connect', address, port);
if (state.sendBlockList?.check(ip, `ipv${isIP(ip)}`)) {
// TODO
ex = new Error();

Check failure on line 452 in lib/dgram.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Use an error exported by 'internal/errors' instead

Check failure on line 452 in lib/dgram.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Use `const { Error } = primordials;` instead of the global
} else {
const err = state.handle.connect(ip, port);
if (err) {
ex = new ExceptionWithHostPort(err, 'connect', address, port);
}
}
}

Expand Down Expand Up @@ -696,6 +714,14 @@ function doSend(ex, self, ip, list, address, port, callback) {
return;
}

if (port && state.sendBlockList?.check(ip, `ipv${isIP(ip)}`)) {
if (callback) {
// TODO
process.nextTick(callback, new Error());

Check failure on line 720 in lib/dgram.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Use an error exported by 'internal/errors' instead

Check failure on line 720 in lib/dgram.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Use `const { Error } = primordials;` instead of the global
}
return;
}

const req = new SendWrap();
req.list = list; // Keep reference alive.
req.address = address;
Expand Down Expand Up @@ -944,6 +970,10 @@ function onMessage(nread, handle, buf, rinfo) {
if (nread < 0) {
return self.emit('error', new ErrnoException(nread, 'recvmsg'));
}
if (self[kStateSymbol]?.receiveBlockList?.check(rinfo.address,
rinfo.family.toLocaleLowerCase())) {

Check failure on line 974 in lib/dgram.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 50 spaces but found 6
return;
}
rinfo.size = buf.length; // compatibility
self.emit('message', buf, rinfo);
}
Expand Down
37 changes: 37 additions & 0 deletions test/parallel/test-dgram-blocklist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
const net = require('net');

const blockList = new net.BlockList();
blockList.addAddress(common.localhostIPv4);

const connectSocket = dgram.createSocket({ type: 'udp4', sendBlockList: blockList });
connectSocket.bind(0, common.mustCall(() => {
connectSocket.connect(9999, common.localhostIPv4, common.mustCall(err => {

Check failure on line 13 in test/parallel/test-dgram-blocklist.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected parentheses around arrow function argument
assert.ok(err);
connectSocket.close();
}))

Check failure on line 16 in test/parallel/test-dgram-blocklist.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
}));

const sendSocket = dgram.createSocket({ type: 'udp4', sendBlockList: blockList });
sendSocket.bind(0, common.mustCall(() => {
sendSocket.send("hello", 9999, common.localhostIPv4, common.mustCall(err => {

Check failure on line 21 in test/parallel/test-dgram-blocklist.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote

Check failure on line 21 in test/parallel/test-dgram-blocklist.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected parentheses around arrow function argument
assert.ok(err);
sendSocket.close();
}))

Check failure on line 24 in test/parallel/test-dgram-blocklist.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
}));

const receiveSocket = dgram.createSocket({ type: 'udp4', receiveBlockList: blockList });
receiveSocket.om('message', common.mustNotCall());

Check failure on line 28 in test/parallel/test-dgram-blocklist.js

View workflow job for this annotation

GitHub Actions / test-linux

--- stderr --- /home/runner/work/node/node/test/parallel/test-dgram-blocklist.js:28 receiveSocket.om('message', common.mustNotCall()); ^ TypeError: receiveSocket.om is not a function at Object.<anonymous> (/home/runner/work/node/node/test/parallel/test-dgram-blocklist.js:28:15) at Module._compile (node:internal/modules/cjs/loader:1566:14) at Object..js (node:internal/modules/cjs/loader:1718:10) at Module.load (node:internal/modules/cjs/loader:1305:32) at Function._load (node:internal/modules/cjs/loader:1119:12) at TracingChannel.traceSync (node:diagnostics_channel:322:14) at wrapModuleLoad (node:internal/modules/cjs/loader:220:24) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5) at node:internal/main/run_main_module:36:49 Node.js v24.0.0-pre Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /home/runner/work/node/node/test/parallel/test-dgram-blocklist.js

Check failure on line 28 in test/parallel/test-dgram-blocklist.js

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- /Users/runner/work/node/node/test/parallel/test-dgram-blocklist.js:28 receiveSocket.om('message', common.mustNotCall()); ^ TypeError: receiveSocket.om is not a function at Object.<anonymous> (/Users/runner/work/node/node/test/parallel/test-dgram-blocklist.js:28:15) at Module._compile (node:internal/modules/cjs/loader:1566:14) at Object..js (node:internal/modules/cjs/loader:1718:10) at Module.load (node:internal/modules/cjs/loader:1305:32) at Function._load (node:internal/modules/cjs/loader:1119:12) at TracingChannel.traceSync (node:diagnostics_channel:322:14) at wrapModuleLoad (node:internal/modules/cjs/loader:220:24) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5) at node:internal/main/run_main_module:36:49 Node.js v24.0.0-pre Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /Users/runner/work/node/node/test/parallel/test-dgram-blocklist.js
receiveSocket.bind(0, common.mustCall(() => {
const client = dgram.createSocket('udp4');
client.bind(0, common.mustCall(() => {
const addressInfo = receiveSocket.address();
client.send(addressInfo.port, addressInfo.address, common.mustCall(() => {
client.close();
}));
}))
}));

0 comments on commit e00dee2

Please sign in to comment.