From cc4a3ed9c19bae34952904869fde2b0755f29266 Mon Sep 17 00:00:00 2001 From: Guido Longoni Date: Wed, 6 May 2026 12:58:12 +0200 Subject: [PATCH] Aggiungi override probe TCP keepalive --- .gitignore | 1 + native/tcp_keepalive/binding.gyp | 8 ++++ native/tcp_keepalive/tcp_keepalive_native.c | 49 +++++++++++++++++++++ package-lock.json | 3 +- package.json | 3 ++ red/tcp/tcp-keepalive.js | 41 +++++++++++++++++ red/tcp/tcp.html | 6 +++ red/tcp/tcp.js | 14 +++++- scripts/build-tcp-keepalive.js | 32 ++++++++++++++ 9 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 native/tcp_keepalive/binding.gyp create mode 100644 native/tcp_keepalive/tcp_keepalive_native.c create mode 100644 red/tcp/tcp-keepalive.js create mode 100644 scripts/build-tcp-keepalive.js diff --git a/.gitignore b/.gitignore index c2658d7..32619a0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +native/tcp_keepalive/build/ diff --git a/native/tcp_keepalive/binding.gyp b/native/tcp_keepalive/binding.gyp new file mode 100644 index 0000000..7aff3b6 --- /dev/null +++ b/native/tcp_keepalive/binding.gyp @@ -0,0 +1,8 @@ +{ + "targets": [ + { + "target_name": "tcp_keepalive_native", + "sources": [ "tcp_keepalive_native.c" ] + } + ] +} diff --git a/native/tcp_keepalive/tcp_keepalive_native.c b/native/tcp_keepalive/tcp_keepalive_native.c new file mode 100644 index 0000000..3fd8f0f --- /dev/null +++ b/native/tcp_keepalive/tcp_keepalive_native.c @@ -0,0 +1,49 @@ +#include +#include + +#ifdef __linux__ +#include +#include +#include +#endif + +static napi_value make_boolean(napi_env env, bool value) { + napi_value result; + napi_get_boolean(env, value, &result); + return result; +} + +static napi_value set_keepalive_probes(napi_env env, napi_callback_info info) { + size_t argc = 2; + napi_value args[2]; + int32_t fd = -1; + int32_t probes = 0; + + napi_get_cb_info(env, info, &argc, args, NULL, NULL); + if (argc < 2) { + return make_boolean(env, false); + } + + if (napi_get_value_int32(env, args[0], &fd) != napi_ok || + napi_get_value_int32(env, args[1], &probes) != napi_ok || + fd < 0 || probes <= 0) { + return make_boolean(env, false); + } + +#ifdef __linux__ + return make_boolean(env, setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &probes, sizeof(probes)) == 0); +#else + return make_boolean(env, false); +#endif +} + +static napi_value init(napi_env env, napi_value exports) { + napi_value fn; + + napi_create_function(env, "setKeepAliveProbes", NAPI_AUTO_LENGTH, set_keepalive_probes, NULL, &fn); + napi_set_named_property(env, exports, "setKeepAliveProbes", fn); + + return exports; +} + +NAPI_MODULE(NODE_GYP_MODULE_NAME, init) diff --git a/package-lock.json b/package-lock.json index 6f7f1a8..bbd1b89 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,8 @@ "packages": { "": { "name": "red-briq-nodes", - "version": "0.2.5", + "version": "0.2.6", + "hasInstallScript": true, "license": "Apache-2.0", "dependencies": { "denque": "^1.4.1", diff --git a/package.json b/package.json index e001b19..53d7d1d 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,9 @@ "name": "red-briq-nodes", "version": "0.2.6", "description": "Various forked and original nodes", + "scripts": { + "install": "node scripts/build-tcp-keepalive.js" + }, "dependencies": { "denque": "^1.4.1", "simple-xmpp": "^1.3.1", diff --git a/red/tcp/tcp-keepalive.js b/red/tcp/tcp-keepalive.js new file mode 100644 index 0000000..9b45ab5 --- /dev/null +++ b/red/tcp/tcp-keepalive.js @@ -0,0 +1,41 @@ +"use strict"; + +let native = null; + +try { + native = require("../../native/tcp_keepalive/build/Release/tcp_keepalive_native"); +} catch (err) { + native = null; +} + +function parseKeepAliveProbes(value) { + if (value === undefined || value === null || value === "") { + return 0; + } + + value = Number(value); + if (!Number.isFinite(value) || value < 0) { + return 0; + } + + return Math.floor(value); +} + +function setKeepAliveProbes(socket, probes) { + if (!native || probes <= 0 || !socket || !socket._handle) { + return false; + } + + const fd = socket._handle.fd; + if (!Number.isInteger(fd) || fd < 0) { + return false; + } + + return native.setKeepAliveProbes(fd, probes) === true; +} + +module.exports = { + available: !!native, + parseKeepAliveProbes, + setKeepAliveProbes +}; diff --git a/red/tcp/tcp.html b/red/tcp/tcp.html index 0e03994..28418a2 100644 --- a/red/tcp/tcp.html +++ b/red/tcp/tcp.html @@ -44,6 +44,10 @@ +
+ + +