50 lines
1.3 KiB
C
50 lines
1.3 KiB
C
#include <node_api.h>
|
|
#include <stdbool.h>
|
|
|
|
#ifdef __linux__
|
|
#include <netinet/in.h>
|
|
#include <netinet/tcp.h>
|
|
#include <sys/socket.h>
|
|
#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)
|