From cbb11f182263c3efb7fda667416cf6d02ccecea9 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 13 May 2025 12:04:58 +0200 Subject: [PATCH] feat(parsing): adding the check of the port --- sources/core/check.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 sources/core/check.cpp diff --git a/sources/core/check.cpp b/sources/core/check.cpp new file mode 100644 index 0000000..efa33b3 --- /dev/null +++ b/sources/core/check.cpp @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/13 11:25:04 by rparodi #+# #+# */ +/* Updated: 2025/05/13 11:59:01 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "core.hpp" +#include +#include +#include + +/** + * @brief function to check if the port is valid + * + * @param input input given bu the user + * @return value between 1 and 65535 if valid, 0 if not + */ +unsigned short int valid_port(char *input) +{ + if (input == NULL) + return (0); + for (size_t i = 0; input[i]; i++) { + if (!isdigit(input[i])) { + if (DEBUG) + std::cerr << LOG << CLR_RED << "Error: Not only number in port: " << input << std::endl; + return (0); + } + } + errno = 0; + int port = strtol(input, NULL, 10); + if (errno == ERANGE) { + if (DEBUG) + std::cerr << LOG << CLR_RED << "Error: Port out of range: " << input << std::endl; + return (0); + } + if (port < 1 || port > 65535) + return (0); + return (port); +}