Port Scanner
Lightweight TCP port scanner for Linux
Loading...
Searching...
No Matches
scanner.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <mutex>
4#include <optional>
5#include <vector>
6
7#include "types.hpp"
8
9namespace {
10int constexpr TIMEOUT_MS{200};
11}
12
21class Scanner final {
22 public:
37 Scanner(ipaddr_t const &ip, std::vector<char const *> const &ports);
38
39 public:
47 [[nodiscard]] std::vector<std::pair<port_t, bool>> scan() const;
48
49 public:
50 static port_t constexpr MIN_PORT{};
51
52 static port_t constexpr MAX_PORT{65535};
53
54 private:
56
63
69 mutable std::mutex m_mtx{};
70
71 private:
80 [[nodiscard]] bool isIPv4(ipaddr_t const &ip) const noexcept;
81
90 [[nodiscard]] bool isIPv6(ipaddr_t const &ip) const noexcept;
91
102 [[nodiscard]] std::optional<port_t> parsePort(char const *str) const noexcept;
103
115 [[nodiscard]] bool isPortAccessible(port_t const port) const;
116};
ports_t m_ports
Target ports.
Definition scanner.hpp:62
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:50
std::mutex m_mtx
Mutable mutex for thread safety.
Definition scanner.hpp:69
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:52
ipaddr_t m_ip
Target IP address.
Definition scanner.hpp:55
int constexpr TIMEOUT_MS
Timeout in milliseconds for socket connection attempts.
Definition scanner.hpp:10
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