Port Scanner
Lightweight TCP port scanner for Linux
Loading...
Searching...
No Matches
scanner.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <mutex>
8#include <optional>
9#include <vector>
10
11#include "types.hpp"
12
13namespace {
14int constexpr TIMEOUT_MS{200};
15}
16
31class Scanner final {
32 public:
47 Scanner(ipaddr_t const &ip, std::vector<char const *> const &ports);
48
49 public:
57 [[nodiscard]] std::vector<std::pair<port_t, bool>> scan() const;
58
59 public:
60 static port_t constexpr MIN_PORT{};
61
62 static port_t constexpr MAX_PORT{65535};
63
64 private:
66
73
79 mutable std::mutex m_mtx{};
80
81 private:
91 [[nodiscard]] bool isIPv4(ipaddr_t const &ip) const noexcept;
92
102 [[nodiscard]] bool isIPv6(ipaddr_t const &ip) const noexcept;
103
115 [[nodiscard]] std::optional<port_t> parsePort(char const *str) const noexcept;
116
129 [[nodiscard]] bool isPortAccessible(port_t const port) const;
130};
A class performs port scanning operations on a specified IP address for a given vector of ports.
Definition scanner.hpp:31
ports_t m_ports
Target ports.
Definition scanner.hpp:72
std::vector< std::pair< port_t, bool > > scan() const
Scans all specified ports on the target IP address.
Definition scanner.cpp:31
bool isIPv6(ipaddr_t const &ip) const noexcept
Checks if the given IP address string is a syntactically valid IPv6 address.
Definition scanner.cpp:56
static port_t constexpr MIN_PORT
Minimum valid port number.
Definition scanner.hpp:60
std::mutex m_mtx
Mutable mutex for thread safety.
Definition scanner.hpp:79
bool isPortAccessible(port_t const port) const
Attempts to establish a TCP connection to the specified port. This function incorporates a timeout (T...
Definition scanner.cpp:73
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 [...
Definition scanner.cpp:61
bool isIPv4(ipaddr_t const &ip) const noexcept
Checks if the given IP address string is a syntactically valid IPv4 address.
Definition scanner.cpp:51
static port_t constexpr MAX_PORT
Maximum valid port number.
Definition scanner.hpp:62
ipaddr_t m_ip
Target IP address.
Definition scanner.hpp:65
int constexpr TIMEOUT_MS
Timeout in milliseconds for socket connection attempts.
Definition scanner.hpp:14
std::vector< port_t > ports_t
Vector of ports type.
Definition types.hpp:10
uint16_t port_t
Port number type.
Definition types.hpp:9
std::string ipaddr_t
IP address type.
Definition types.hpp:8