Port Scanner
Lightweight TCP port scanner for Linux
Loading...
Searching...
No Matches
Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | List of all members
Scanner Class Referencefinal

A class performs port scanning operations on a specified IP address for a given vector of ports. More...

#include <scanner.hpp>

+ Collaboration diagram for Scanner:

Public Member Functions

 Scanner (ipaddr_t const &ip, std::vector< char const * > const &ports)
 Constructs a Scanner with a target IP address and a vector of ports to scan.
 
std::vector< std::pair< port_t, bool > > scan () const
 Scans all specified ports on the target IP address.
 

Static Public Attributes

static port_t constexpr MIN_PORT {}
 Minimum valid port number.
 
static port_t constexpr MAX_PORT {65535}
 Maximum valid port number.
 

Private Member Functions

bool isIPv4 (ipaddr_t const &ip) const noexcept
 Checks if the given IP address string is a syntactically valid IPv4 address.
 
bool isIPv6 (ipaddr_t const &ip) const noexcept
 Checks if the given IP address string is a syntactically valid IPv6 address.
 
std::optional< port_tparsePort (char const *str) const noexcept
 Attempts to parse a C-string into a port_t. It also validates if the parsed number falls within the [MIN_PORT, MAX_PORT] range.
 
bool isPortAccessible (port_t const port) const
 Attempts to establish a TCP connection to the specified port. This function incorporates a timeout (TIMEOUT_MS).
 

Private Attributes

ipaddr_t m_ip
 Target IP address.
 
ports_t m_ports {}
 Target ports.
 
std::mutex m_mtx {}
 Mutable mutex for thread safety.
 

Detailed Description

A class performs port scanning operations on a specified IP address for a given vector of ports.

The Scanner class is designed to perform port scanning operations on a specified IP address for a given vector of ports. Every port is being scanned in separate thread for better performance.

Invariant
Valid IP address.
Valid ports.
Examples
scanner.cpp.

Definition at line 31 of file scanner.hpp.

Constructor & Destructor Documentation

◆ Scanner()

Scanner::Scanner ( ipaddr_t const &  ip,
std::vector< char const * > const &  ports 
)

Constructs a Scanner with a target IP address and a vector of ports to scan.

Exception Safety: Strong guarantee. The object is constructed only if the IP address and all port strings are valid; otherwise, no object is created.

Parameters
ipTarget IP address to scan.
portsA vector of null-terminated C-string port representations (e.g., "80", "443") to scan.
Exceptions
std::invalid_argumentIf the IP address or any port string is invalid.
std::bad_allocIf memory allocation for internal storage fails.
std::length_errorIf the requested storage size exceeds the maximum allowed size.

Definition at line 15 of file scanner.cpp.

+ Here is the call graph for this function:

Member Function Documentation

◆ isIPv4()

bool Scanner::isIPv4 ( ipaddr_t const &  ip) const
privatenoexcept

Checks if the given IP address string is a syntactically valid IPv4 address.

Exception Safety: No-throw guarantee.

Parameters
ipThe IP address string to validate.
Returns
true if the IP is а valid IPv4, false otherwise.

Definition at line 51 of file scanner.cpp.

+ Here is the caller graph for this function:

◆ isIPv6()

bool Scanner::isIPv6 ( ipaddr_t const &  ip) const
privatenoexcept

Checks if the given IP address string is a syntactically valid IPv6 address.

Exception Safety: No-throw guarantee.

Parameters
ipThe IP address string to validate.
Returns
true if the IP is а valid IPv6, false otherwise.

Definition at line 56 of file scanner.cpp.

+ Here is the caller graph for this function:

◆ isPortAccessible()

bool Scanner::isPortAccessible ( port_t const  port) const
private

Attempts to establish a TCP connection to the specified port. This function incorporates a timeout (TIMEOUT_MS).

Exception Safety: Strong guarantee.

Parameters
portThe TCP port number to check for accessibility.
Returns
true if a connection can be established (port is accessible), false otherwise.
Exceptions
std::bad_allocIf memory allocation for internal storage fails.

Definition at line 73 of file scanner.cpp.

+ Here is the caller graph for this function:

◆ parsePort()

std::optional< port_t > Scanner::parsePort ( char const *  str) const
privatenoexcept

Attempts to parse a C-string into a port_t. It also validates if the parsed number falls within the [MIN_PORT, MAX_PORT] range.

Exception Safety: No-throw guarantee.

Parameters
strThe C-string containing the potential port number.
Returns
An std::optional<port_t> containing the valid port number if parsing is successful and the number is in range; otherwise, an empty std::optional.

Definition at line 61 of file scanner.cpp.

+ Here is the caller graph for this function:

◆ scan()

std::vector< std::pair< port_t, bool > > Scanner::scan ( ) const

Scans all specified ports on the target IP address.

Each port is scanned concurrently in a separate thread.

Returns
A vector of pairs: <port number, is port accessible (true/false)>.
Examples
scanner.cpp.

Definition at line 31 of file scanner.cpp.

+ Here is the call graph for this function:

Member Data Documentation

◆ m_ip

ipaddr_t Scanner::m_ip
private

Target IP address.

Definition at line 65 of file scanner.hpp.

◆ m_mtx

std::mutex Scanner::m_mtx {}
mutableprivate

Mutable mutex for thread safety.

Exception safety: No-throw guarantee.

Definition at line 79 of file scanner.hpp.

◆ m_ports

ports_t Scanner::m_ports {}
private

Target ports.

Exception safety: No-throw guarantee.

Definition at line 72 of file scanner.hpp.

◆ MAX_PORT

port_t constexpr Scanner::MAX_PORT {65535}
staticconstexpr

Maximum valid port number.

Definition at line 62 of file scanner.hpp.

◆ MIN_PORT

port_t constexpr Scanner::MIN_PORT {}
staticconstexpr

Minimum valid port number.

Definition at line 60 of file scanner.hpp.


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