Port Scanner
Lightweight TCP port scanner for Linux
|
#include <scanner.hpp>
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_t > | parsePort (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. | |
The Scanner class is designed to perform port scanning operations on a specified IP address for a given vector of ports.
Definition at line 21 of file scanner.hpp.
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.
ip | Target IP address to scan. |
ports | A vector of null-terminated C-string port representations (e.g., "80", "443") to scan. |
std::invalid_argument | If the IP address or any port string is invalid. |
std::bad_alloc | If memory allocation for internal storage fails. |
std::length_error | If the requested storage size exceeds the maximum allowed size. |
Definition at line 15 of file scanner.cpp.
|
privatenoexcept |
Checks if the given IP address string is a syntactically valid IPv4 address.
Exception Safety: No-throw guarantee.
ip | The IP address string to validate. |
Definition at line 51 of file scanner.cpp.
|
privatenoexcept |
Checks if the given IP address string is a syntactically valid IPv6 address.
Exception Safety: No-throw guarantee.
ip | The IP address string to validate. |
Definition at line 56 of file scanner.cpp.
|
private |
Attempts to establish a TCP connection to the specified port. This function incorporates a timeout (TIMEOUT_MS).
Exception Safety: Strong guarantee.
port | The TCP port number to check for accessibility. |
std::bad_alloc | If memory allocation for internal storage fails. |
Definition at line 73 of file scanner.cpp.
|
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.
str | The C-string containing the potential port number. |
Definition at line 61 of file scanner.cpp.
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.
Definition at line 31 of file scanner.cpp.
|
private |
Target IP address.
Definition at line 55 of file scanner.hpp.
|
mutableprivate |
Mutable mutex for thread safety.
Exception safety: No-throw guarantee.
Definition at line 69 of file scanner.hpp.
|
private |
|
staticconstexpr |
Maximum valid port number.
Definition at line 52 of file scanner.hpp.
|
staticconstexpr |
Minimum valid port number.
Definition at line 50 of file scanner.hpp.