STTNet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Member Functions | List of all members
stt::security::ConnectionLimiter Class Reference

Unified connection & request security gate (IP-level + fd-level, multi-strategy limiting + blacklist). More...

#include <sttnet_English.h>

Public Member Functions

 ConnectionLimiter (const int &maxConn=20, const int &idleTimeout=60)
 Constructor. More...
 
void setConnectStrategy (const RateLimitType &type)
 Set the strategy used for connection-rate limiting. More...
 
void setRequestStrategy (const RateLimitType &type)
 Set the strategy used for fd-level request limiting. More...
 
void setPathStrategy (const RateLimitType &type)
 Set the strategy used for path-level extra limiting. More...
 
void setPathLimit (const std::string &path, const int &times, const int &secs)
 Configure extra rate limits for a specific path (path-level rule). More...
 
DefenseDecision allowConnect (const std::string &ip, const int &fd, const int &times, const int &secs)
 Security decision for a newly accepted connection (IP-level gate). More...
 
DefenseDecision allowRequest (const std::string &ip, const int &fd, const std::string_view &path, const int &times, const int &secs)
 Security decision for a single request on an existing connection. More...
 
void clearIP (const std::string &ip, const int &fd)
 Reclaim state for an fd when the connection is closed. More...
 
bool connectionDetect (const std::string &ip, const int &fd)
 Detect and cleanup an idle/zombie connection. More...
 

Detailed Description

Unified connection & request security gate (IP-level + fd-level, multi-strategy limiting + blacklist).

ConnectionLimiter is a "Security Gate". All connections and requests must pass through it before business logic runs.

This class does NOT directly perform side effects (close/send/sleep). Instead, it returns a DefenseDecision for the caller to enforce.


Design Overview

Layered defense model:

Decision Semantics

allowConnect / allowRequest return DefenseDecision:

Note
  • Connect stage usually uses only ALLOW / CLOSE.
  • DROP is mainly used in request stage.

Strategies

Supported algorithms (see RateLimitType):

Defaults:

Thread Safety

Warning
This class is NOT thread-safe by itself. Concurrent access to internal tables (table/pathConfig/blacklist) must be protected externally (e.g. single event-loop thread, or a mutex).

Lifecycle

Constructor & Destructor Documentation

stt::security::ConnectionLimiter::ConnectionLimiter ( const int &  maxConn = 20,
const int &  idleTimeout = 60 
)
inline

Constructor.

Parameters
maxConnMax concurrent connections allowed per IP (activeConnections cap).
idleTimeoutIdle timeout (seconds) used for zombie detection. If < 0, disable.

Member Function Documentation

DefenseDecision stt::security::ConnectionLimiter::allowConnect ( const std::string &  ip,
const int &  fd,
const int &  times,
const int &  secs 
)

Security decision for a newly accepted connection (IP-level gate).

Parameters
ipRemote IP address.
fdNewly accepted file descriptor.
timesMaximum allowed connection attempts within secs.
secsConnection-rate window size (seconds).
Returns
DefenseDecision
  • ALLOW: connection is allowed and fd will be registered
  • CLOSE: reject and caller should close the connection immediately
Note
  • Connect stage typically does not use DROP.
  • If the IP is blacklisted or in a high-risk state, returns CLOSE directly.
DefenseDecision stt::security::ConnectionLimiter::allowRequest ( const std::string &  ip,
const int &  fd,
const std::string_view &  path,
const int &  times,
const int &  secs 
)

Security decision for a single request on an existing connection.

Parameters
ipRemote IP address.
fdFile descriptor associated with the request.
pathRequest path (used for path-level extra limiting).
timesRequest-rate limit (max requests within secs).
secsRequest-rate window size (seconds).
Returns
DefenseDecision
  • ALLOW: process normally
  • DROP: ignore silently (no response)
  • CLOSE: close connection
void stt::security::ConnectionLimiter::clearIP ( const std::string &  ip,
const int &  fd 
)

Reclaim state for an fd when the connection is closed.

Parameters
ipRemote IP address.
fdClosed file descriptor.
Note
  • Must be called after close(fd).
  • Keeps activeConnections and internal state consistent.
bool stt::security::ConnectionLimiter::connectionDetect ( const std::string &  ip,
const int &  fd 
)

Detect and cleanup an idle/zombie connection.

Parameters
ipRemote IP address.
fdFile descriptor to check.
Returns
true The connection is considered zombie and has been cleaned up.
false Not timed out or not found.
Note
  • "Activity" means allowConnect()/allowRequest() updates lastActivity.
  • Prefer calling via a timer instead of scanning hot paths.
void stt::security::ConnectionLimiter::setConnectStrategy ( const RateLimitType type)

Set the strategy used for connection-rate limiting.

Parameters
typeStrategy type, see RateLimitType.
Note
Default is RateLimitType::Cooldown.
void stt::security::ConnectionLimiter::setPathLimit ( const std::string &  path,
const int &  times,
const int &  secs 
)

Configure extra rate limits for a specific path (path-level rule).

Parameters
pathTarget path, e.g. "/login", "/register".
timesMaximum allowed requests within secs.
secsWindow size (seconds).
Note
setPathLimit() defines an additional rule:
  • The (times, secs) passed into allowRequest() is still applied first as the connection/IP-level rule.
  • If path matches a configured rule, the path-level rule is evaluated next.
  • Relationship is AND: any layer failing results in rejection.
void stt::security::ConnectionLimiter::setPathStrategy ( const RateLimitType type)

Set the strategy used for path-level extra limiting.

Parameters
typeStrategy type, see RateLimitType.
Note
Default is RateLimitType::SlidingWindow.
void stt::security::ConnectionLimiter::setRequestStrategy ( const RateLimitType type)

Set the strategy used for fd-level request limiting.

Parameters
typeStrategy type, see RateLimitType.
Note
Default is RateLimitType::SlidingWindow.

The documentation for this class was generated from the following file: