Giter VIP home page Giter VIP logo

node-net-keepalive's Introduction

NPM Node OS Codecov CI Dependencies License

net-keepalive

NPM

The Missing (TCP_KEEPINTVL and TCP_KEEPCNT) SO_KEEPALIVE socket option setters and getters for Node using ffi module. Tested on linux, should work on osx and freebsd.

Install

$ npm install --save net-keepalive

Demo

var Net = require('net')
  , NetKeepAlive = require('net-keepalive')
;

// Create a TCP Server
var srv = Net.createServer(function(s){
  console.log('Connected %j', s.address())
  // Doesn't matter what it does
  s.pipe(s)
});

// Start on some port
srv.listen(1337, function(){
  console.log('Listening on %j', srv.address())
});

// Connect to that server
var s = Net.createConnection({port:1337}, function(){
  console.log('Connected to %j', s.address())
  
  //IMPORTANT: KeepAlive must be enabled for this to work
  s.setKeepAlive(true, 1000)

  // Set TCP_KEEPINTVL for this specific socket
  NetKeepAlive.setKeepAliveInterval(s, 1000)
  
  // Get TCP_KEEPINTVL for this specific socket
  NetKeepAlive.getKeepAliveInterval(s) // 1000

  // Set TCP_KEEPCNT for this specific socket 
  NetKeepAlive.setKeepAliveProbes(s, 1)
  
  // Get TCP_KEEPCNT for this specific socket 
  NetKeepAlive.getKeepAliveProbes(s) // 1
});

Now using iptables add rule to drop all tcp packets on INPUT chain to port 1337.

$ iptables -I INPUT -m tcp -p tcp --dport 1337 -j DROP

If you were monitoring packets on loopback with tcp.srcport == 1337 || tcp.dstport == 1337 filter in wireshark. You will see the following output:

Wireshark screenshot KEEPALIVE

Have fun!

More info about SO_KEEPALIVE here: TCP Keepalive HOWTO C Code examples here: Examples

API

Note: For these methods to work you must enable SO_KEEPALIVE and set the TCP_KEEPIDLE options for socket using Net.Socket-s built in method socket.setKeepAlive([enable][, initialDelay]) !

TCP_KEEPIDLE (since Linux 2.4) The time (in seconds) the connection needs to remain idle before TCP starts sending keepalive probes, if the socket option SO_KEEPALIVE has been set on this socket. This option should not be used in code intended to be portable.
var NetSocket = require('net-keepalive')

// .....
// get socket somehow
// .....

var enable = true                                           // enable SO_KEEPALIVE
var initialDuration = 1000                                  // start probing after 1 second of inactivity
socket.setKeepAlive(enable, initialDuration)                // sets SO_KEEPALIVE and TCP_KEEPIDLE

var probeInterval = 1000                                    // after initialDuration send probes every 1 second
NetSocket.setKeepAliveInterval(socket, probeInterval)       //sets TCP_KEEPINTVL

var maxProbesBeforeFail = 10                                // after 10 failed probes connection will be dropped 
NetSocket.setKeepAliveProbes(socket, maxProbesBeforeFail)   // sets TCP_KEEPCNT

// ....
// ....

setKeepAliveInterval(socket, msecs)

  • socket - instanceof Net.Socket- Socket to modify
  • msecs - Number - Time in milliseconds between KeepAlive probes.
  • Returns true on success

Sets TCP_KEEPINTVL to msecs miliseconds (converted to seconds int internally) for the socket based on its file descriptor (fd)

TCP_KEEPINTVL (since Linux 2.4) The time (in seconds) between individual keepalive probes. This option should not be used in code intended to be portable.

getKeepAliveInterval(socket)

  • socket - instanceof Net.Socket- Socket to modify
  • Returns msecs - Number - Time in milliseconds between KeepAlive probes on success

Gets TCP_KEEPINTVL. The msecs miliseconds (converted from seconds int internally) set for the socket based on its file descriptor (fd)

TCP_KEEPINTVL (since Linux 2.4) The time (in seconds) between individual keepalive probes. This option should not be used in code intended to be portable.

setKeepAliveProbes(socket, count)

  • socket - instanceof Net.Socket- Socket to modify
  • count - Number - Number of probes to send before dropping the connection
  • Returns true on success

Sets TCP_KEEPCNT to count number of probes for the socket based on its file descriptor (fd)

TCP_KEEPCNT (since Linux 2.4) - The maximum number of keepalive probes TCP should send before dropping the connection. This option should not be used in code intended to be portable.

getKeepAliveProbes(socket)

  • socket - instanceof Net.Socket- Socket to modify
  • Returns count - Number - Number of probes to send before dropping the connection on success.

Gets TCP_KEEPCNT. The count number of probes set for the socket based on its file descriptor (fd)

TCP_KEEPCNT (since Linux 2.4) - The maximum number of keepalive probes TCP should send before dropping the connection. This option should not be used in code intended to be portable.

node-net-keepalive's People

Contributors

hertzg avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.