Skip to content

Instantly share code, notes, and snippets.

@JustinAzoff
Created May 28, 2015 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinAzoff/cc20d4cf28f864da0ecd to your computer and use it in GitHub Desktop.
Save JustinAzoff/cc20d4cf28f864da0ecd to your computer and use it in GitHub Desktop.
module TerminateConnection;
export {
redef enum Notice::Type += {
TerminatingConnection, # connection will be terminated
TerminatingConnectionIgnored, # connection terminated disabled
};
# Whether we're allowed (and/or are capable) to terminate connections
# using "rst".
const activate_terminate_connection = F &redef;
# Terminate the given connection.
global terminate_connection: function(c: connection);
}
function full_id_string(c: connection): string
{
local id = c$id;
return fmt("%s:%s -> %s:%s", id$orig_h, id$orig_p, id$resp_h, id$resp_p);
}
function terminate_connection(c: connection)
{
local id = c$id;
if ( activate_terminate_connection )
{
local local_init = Site::is_local_addr(id$orig_h);
local term_cmd = fmt("rst %s -n 32 -d 20 %s %d %d %s %d %d",
local_init ? "-R" : "",
id$orig_h, id$orig_p, get_orig_seq(id),
id$resp_h, id$resp_p, get_resp_seq(id));
if ( reading_live_traffic() )
system(term_cmd);
else
NOTICE([$note=TerminatingConnection, $conn=c,
$msg=term_cmd, $sub="first termination command"]);
term_cmd = fmt("rst %s -r 2 -n 4 -s 512 -d 20 %s %d %d %s %d %d",
local_init ? "-R" : "",
id$orig_h, id$orig_p, get_orig_seq(id),
id$resp_h, id$resp_p, get_resp_seq(id));
if ( reading_live_traffic() )
system(term_cmd);
else
NOTICE([$note=TerminatingConnection, $conn=c,
$msg=term_cmd, $sub="second termination command"]);
NOTICE([$note=TerminatingConnection, $conn=c,
$msg=fmt("terminating %s", full_id_string(c))]);
}
else
NOTICE([$note=TerminatingConnectionIgnored, $conn=c,
$msg=fmt("ignoring request to terminate %s",
full_id_string(c))]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment