ControlConn::ControlConn(int fd, bool local) : fd(fd), local(local)
{
+ touch();
shell = new Shell;
configure();
show_prompt();
}
if (n == 0 && commands_found == 0)
return -1;
-
+ touch();
return commands_found;
}
removed = true;
}
+void ControlConn::touch()
+{
+ touched = time(nullptr);
+}
+
+time_t ControlConn::get_touched() const
+{
+ return touched;
+}
+
void ControlConn::unblock()
{
if (blocked)
else
bytes_written += n;
}
+ touch();
return true;
}
bool is_closed() const { return (fd == -1); }
bool is_removed() const { return removed; }
bool has_pending_command() const { return !pending_commands.empty(); }
+ time_t get_touched() const;
void configure() const;
int read_commands();
private:
bool respond(const char* format, va_list& ap);
bool show_prompt();
+ void touch();
private:
std::queue<std::string> pending_commands;
bool local = false;
bool blocked = false;
bool removed = false;
+ time_t touched;
};
#define LogRespond(cn, ...) if (cn) cn->respond(__VA_ARGS__); else LogMessage(__VA_ARGS__)
using namespace snort;
static constexpr unsigned MAX_CONTROL_FDS = 16;
+static constexpr unsigned MAX_CONTROL_IDLE_TIME = 60;
static int listener = -1;
static socklen_t sock_addr_size = 0;
static int epoll_fd = -1;
static unsigned nfds;
+static void delete_expired_controls();
+
static bool init_controls()
{
epoll_fd = epoll_create1(0);
static bool register_control_fd(const int fd)
{
+ if (nfds + 2 >= MAX_CONTROL_FDS)
+ delete_expired_controls();
+
if (nfds == MAX_CONTROL_FDS)
{
WarningMessage("Failed to add file descriptor, exceed max (%d)\n", nfds);
controls.clear();
}
+static void delete_expired_controls()
+{
+ int fds[MAX_CONTROL_FDS], n=0;
+ time_t curr_time = time(nullptr);
+ for (const auto& p : controls)
+ {
+ ControlConn* ctrlcon = p.second;
+ if (!ctrlcon->is_local() and (curr_time - ctrlcon->get_touched()) >= MAX_CONTROL_IDLE_TIME)
+ fds[n++] = p.first;
+ }
+ for(int i=0; i<n; i++)
+ {
+ LogMessage("Control: closing fd=%d that was idle for more than %d seconds.\n", fds[i], MAX_CONTROL_IDLE_TIME);
+ delete_control(fds[i]);
+ }
+}
+
//-------------------------------------------------------------------------
// Public API
//-------------------------------------------------------------------------