diff --git a/.vscode/settings.json b/.vscode/settings.json index c66a6ef..3ca687a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,8 +1,5 @@ { "files.associations": { - "sstream": "cpp", - "iosfwd": "cpp", - "ostream": "cpp", "cctype": "cpp", "cmath": "cpp", "cstdarg": "cpp", @@ -45,12 +42,15 @@ "format": "cpp", "initializer_list": "cpp", "iomanip": "cpp", + "iosfwd": "cpp", "istream": "cpp", "limits": "cpp", "mutex": "cpp", "new": "cpp", + "ostream": "cpp", "semaphore": "cpp", "span": "cpp", + "sstream": "cpp", "stdexcept": "cpp", "stop_token": "cpp", "streambuf": "cpp", @@ -70,7 +70,7 @@ "print": "cpp", "queue": "cpp", "stack": "cpp", - "assert": "cpp", - "zstring": "cpp" + "fstream": "cpp", + "regex": "cpp" } } \ No newline at end of file diff --git a/Server/CMakeLists.txt b/Server/CMakeLists.txt index 4c9e48c..f076a1a 100644 --- a/Server/CMakeLists.txt +++ b/Server/CMakeLists.txt @@ -11,7 +11,7 @@ include(FetchContent) FetchContent_Declare( spdlog GIT_REPOSITORY "https://github.com/gabime/spdlog.git" - GIT_TAG "v1.15.2" + GIT_TAG "v1.11.0" GIT_SHALLOW ON ) FetchContent_MakeAvailable(spdlog) diff --git a/Server/src/Chattering.log b/Server/src/Chattering.log new file mode 100644 index 0000000..b3dafae --- /dev/null +++ b/Server/src/Chattering.log @@ -0,0 +1,7 @@ +[2025-04-22 20:30:06.838] [Chattering Logger] [critical] [bind()] Invalid argument +[2025-04-22 22:26:22.198] [Chattering Logger] [critical] [bind()] Socket operation on non-socket +[2025-04-22 23:38:21.098] [Chattering Logger] [critical] [listen()] Operation not supported +[2025-04-22 23:39:00.008] [Chattering Logger] [critical] [listen()] Operation not supported +[2025-04-22 23:40:54.696] [Chattering Logger] [critical] [listen()] Operation not supported +[2025-04-22 23:41:11.774] [Chattering Logger] [critical] [listen()] Operation not supported +[2025-04-22 23:41:21.669] [Chattering Logger] [critical] [listen()] Operation not supported diff --git a/Server/src/config.json b/Server/src/config.json new file mode 100644 index 0000000..5d7237d --- /dev/null +++ b/Server/src/config.json @@ -0,0 +1,8 @@ +{ + "IP Version" : 6, + "Listen Port" : 9010, + "LogLevel" : 1, + "LogfileCount" : 5, + "LogfileName" : "Chattering.log", + "LogfileSize" : 4294967295 +} \ No newline at end of file diff --git a/Server/src/server.cpp b/Server/src/server.cpp index 1926c83..b430caf 100644 --- a/Server/src/server.cpp +++ b/Server/src/server.cpp @@ -1,68 +1,37 @@ -#include "Socket/Socket.hpp" +#include "Socket/TCPSocket.hpp" #include "Socket/Log.hpp" +#include "Utils/ConfigManager.hpp" #include "precomp.hpp" -#include -#include -#include -#include int main() { - Json::Value configJsonRoot; - std::uint32_t ipVersion = 0; - std::uint32_t listenPort = 0; - spdlog::level::level_enum logLevel; - gsl::czstring logFileName; - std::uint32_t logfileSize; - std::uint32_t logfileCount; + auto config = Chattr::ConfigManager::load(); + Chattr::log::setDefaultLogger(config.logLevel, config.logFileName, config.logfileSize, config.logfileCount); - std::ifstream config("config.json", std::ifstream::binary); - if (!config.is_open()) { - std::ofstream defaultConfig("config.json", std::ios::out); - configJsonRoot["IP Version"] = 6; - configJsonRoot["Listen Port"] = 9010; - configJsonRoot["LogLevel"] = 1; - configJsonRoot["LogfileName"] = "Chattering.log"; - configJsonRoot["LogfileSize"] = UINT32_MAX; - configJsonRoot["LogfileCount"] = 5; - defaultConfig << configJsonRoot; - defaultConfig.close(); - spdlog::critical("\"config.json\" is missing. Default configuration has been written."); - std::exit(EXIT_FAILURE); + + struct Chattr::TCPSocket sock; + if (config.ipVersion == 4) { + sock.init(AF_INET); + + struct sockaddr_in serveraddr; + serveraddr.sin_family = AF_INET; + serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); + serveraddr.sin_port = htons(config.listenPort); + + sock.bind(&serveraddr); } - try { - config >> configJsonRoot; + else { + sock.init(AF_INET6); - ipVersion = configJsonRoot["IP Version"].asInt(); - if (ipVersion != 4 && ipVersion != 6) - throw std::runtime_error("Invalid IP Version."); + struct sockaddr_in6 serveraddr; + serveraddr.sin6_family = AF_INET6; + serveraddr.sin6_addr = in6addr_any; + serveraddr.sin6_port = htons(config.listenPort); - listenPort = configJsonRoot["Listen Port"].asInt(); - if (listenPort < 0 || listenPort > 65535) - throw std::runtime_error("Invalid listen port."); - - int ll_ = configJsonRoot["LogLevel"].asInt(); - if (ll_ >= 0 && ll_ < spdlog::level::n_levels) - logLevel = (spdlog::level::level_enum)ll_; - else - throw std::runtime_error("Invalid log level."); - - logFileName = configJsonRoot["LogfileName"].asCString(); - logfileSize = configJsonRoot["LogfileSize"].asUInt(); - logfileCount = configJsonRoot["LogfileCount"].asUInt(); + sock.bind(&serveraddr); } - catch (Json::RuntimeError e) { - spdlog::critical(std::string(std::string("[Json Error: ]") + e.what()).c_str()); - std::exit(EXIT_FAILURE); - } - - Chattr::log::setDefaultLogger(logLevel, logFileName, logfileSize, logfileCount); - - struct Chattr::Socket sock(AF_INET, SOCK_DGRAM, 0); - - struct sockaddr_in serveraddr = {}; - serveraddr.sin_family = AF_INET; - serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); - serveraddr.sin_port = htons(9010); - - sock.bind(sock, &serveraddr); + sock.listen(SOMAXCONN); + struct Chattr::TCPSocket clientSock; + struct Chattr::Address clientAddr; + spdlog::info("Waiting for connection..."); + sock.accept(clientSock, clientAddr); } \ No newline at end of file diff --git a/impl/Socket/Chattering.log b/impl/Socket/Chattering.log new file mode 100644 index 0000000..59f9b8a --- /dev/null +++ b/impl/Socket/Chattering.log @@ -0,0 +1 @@ +[2025-04-22 23:42:27.045] [Chattering Logger] [critical] [listen()] Operation not supported diff --git a/impl/Socket/Socket.cpp b/impl/Socket/Socket.cpp index 81a25f5..2a705b5 100644 --- a/impl/Socket/Socket.cpp +++ b/impl/Socket/Socket.cpp @@ -10,11 +10,7 @@ Socket::Socket(int domain, int type, int protocol) { if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) log::critical("WSAStartup()"); #endif - sock_ = ::socket(domain, type, protocol); - if (sock_ == INVALID_SOCKET) - log::critical("socket()"); - - valid_ = true; + init(domain, type, protocol); } Socket::~Socket() { @@ -28,37 +24,76 @@ Socket::~Socket() { #endif } +void Socket::init(int domain, int type, int protocol) { + if (domain == AF_INET) + bindAddr.length = sizeof(sockaddr_in); + else if (domain == AF_INET6) + bindAddr.length = sizeof(sockaddr_in6); + + sock_ = ::socket(domain, type, protocol); + if (sock_ == INVALID_SOCKET) + log::critical("socket()"); + + valid_ = true; +} + Socket::operator SOCKET() const { if (valid_) return sock_; spdlog::critical("No valid socket created."); return INVALID_SOCKET; -}; - -void Socket::bind(int __fd, const sockaddr *__addr) { - bind(__fd, (struct sockaddr *)__addr, sizeof(*__addr)); } -void Socket::bind(int __fd, const sockaddr *__addr, socklen_t __len) { - int retVal = ::bind(__fd, __addr, __len); +void Socket::move(const SOCKET __sock) { + if (sock_ == INVALID_SOCKET) + log::critical("socket()"); + + if (valid_) { +#ifdef _WIN32 + closesocket(sock_); + WSACleanup(); +#elif __linux__ + ::close(sock_); +#endif + } + else { +#ifdef _WIN32 + WSADATA wsa; + if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) + log::critical("WSAStartup()"); +#endif + } + + sock_ = __sock; + valid_ = true; +}; + +void Socket::bind(const sockaddr *__addr) { + bind((struct sockaddr *)__addr, sizeof(*__addr)); +} + +void Socket::bind(const sockaddr *__addr, socklen_t __len) { + bindAddr.length = __len; + std::memcpy(&bindAddr, __addr, __len); + int retVal = ::bind(sock_, __addr, __len); if (retVal == INVALID_SOCKET) log::critical("bind()"); } -void Socket::bind(int __fd, const sockaddr_in *__addr) { - bind(__fd, (struct sockaddr *)__addr, sizeof(*__addr)); +void Socket::bind(const sockaddr_in *__addr) { + bind((struct sockaddr *)__addr, sizeof(*__addr)); } -void Socket::bind(int __fd, const sockaddr_in *__addr, socklen_t __len) { - bind(__fd, (struct sockaddr *)__addr, __len); +void Socket::bind(const sockaddr_in *__addr, socklen_t __len) { + bind((struct sockaddr *)__addr, __len); } -void Socket::bind(int __fd, const sockaddr_in6 *__addr) { - bind(__fd, (struct sockaddr *)__addr, sizeof(*__addr)); +void Socket::bind(const sockaddr_in6 *__addr) { + bind((struct sockaddr *)__addr, sizeof(*__addr)); } -void Socket::bind(int __fd, const sockaddr_in6 *__addr, socklen_t __len) { - bind(__fd, (struct sockaddr *)__addr, __len); +void Socket::bind(const sockaddr_in6 *__addr, socklen_t __len) { + bind((struct sockaddr *)__addr, __len); } } \ No newline at end of file diff --git a/impl/Socket/TCPSocket.cpp b/impl/Socket/TCPSocket.cpp new file mode 100644 index 0000000..6789a28 --- /dev/null +++ b/impl/Socket/TCPSocket.cpp @@ -0,0 +1,26 @@ +#include "Socket/TCPSocket.hpp" +#include "Socket/Log.hpp" +#include "precomp.hpp" + +namespace Chattr { + +void TCPSocket::init(int domain) { + sock_ = ::socket(domain, SOCK_STREAM, 0); + if (sock_ == INVALID_SOCKET) + log::critical("socket()"); + + valid_ = true; +} + +void TCPSocket::listen(int __n) { + int retVal = ::listen(sock_, __n); + if (retVal == INVALID_SOCKET) + log::critical("listen()"); +} + +void TCPSocket::accept(TCPSocket& newSock, Address& __addr) { + newSock.move(::accept(sock_, &__addr.sockaddr, &__addr.length)); + if (newSock == INVALID_SOCKET) + log::critical("accept()"); +} +} \ No newline at end of file diff --git a/impl/Socket/config.json b/impl/Socket/config.json new file mode 100644 index 0000000..5d7237d --- /dev/null +++ b/impl/Socket/config.json @@ -0,0 +1,8 @@ +{ + "IP Version" : 6, + "Listen Port" : 9010, + "LogLevel" : 1, + "LogfileCount" : 5, + "LogfileName" : "Chattering.log", + "LogfileSize" : 4294967295 +} \ No newline at end of file diff --git a/impl/Utils/ConfigManager.cpp b/impl/Utils/ConfigManager.cpp new file mode 100644 index 0000000..1e1c8e0 --- /dev/null +++ b/impl/Utils/ConfigManager.cpp @@ -0,0 +1,53 @@ +#include "Utils/ConfigManager.hpp" +#include "precomp.hpp" +#include +#include +#include +#include + +namespace Chattr { +ConfigManager::Config ConfigManager::load() { + Config config; + + std::ifstream configfile("config.json", std::ifstream::binary); + if (!configfile.is_open()) { + std::ofstream defaultConfig("config.json", std::ios::out); + config.configJsonRoot["IP Version"] = 6; + config.configJsonRoot["Listen Port"] = 9010; + config.configJsonRoot["LogLevel"] = 1; + config.configJsonRoot["LogfileName"] = "Chattering.log"; + config.configJsonRoot["LogfileSize"] = UINT32_MAX; + config.configJsonRoot["LogfileCount"] = 5; + defaultConfig << config.configJsonRoot; + defaultConfig.close(); + spdlog::critical("\"config.json\" is missing. Default configuration has been written."); + } + try { + if (configfile.is_open()) + configfile >> config.configJsonRoot; + + config.ipVersion = config.configJsonRoot["IP Version"].asInt(); + if (config.ipVersion != 4 && config.ipVersion != 6) + throw std::runtime_error("Invalid IP Version."); + + config.listenPort = config.configJsonRoot["Listen Port"].asInt(); + if (config.listenPort < 0 || config.listenPort > 65535) + throw std::runtime_error("Invalid listen port."); + + int ll_ = config.configJsonRoot["LogLevel"].asInt(); + if (ll_ >= 0 && ll_ < spdlog::level::n_levels) + config.logLevel = (spdlog::level::level_enum)ll_; + else + throw std::runtime_error("Invalid log level."); + + config.logFileName = config.configJsonRoot["LogfileName"].asCString(); + config.logfileSize = config.configJsonRoot["LogfileSize"].asUInt(); + config.logfileCount = config.configJsonRoot["LogfileCount"].asUInt(); + } + catch (Json::RuntimeError e) { + spdlog::critical(std::string(std::string("[Json Error: ]") + e.what()).c_str()); + std::exit(EXIT_FAILURE); + } + return config; +} +} \ No newline at end of file diff --git a/include/Socket/Address.hpp b/include/Socket/Address.hpp new file mode 100644 index 0000000..a9102d3 --- /dev/null +++ b/include/Socket/Address.hpp @@ -0,0 +1,15 @@ +#pragma once +#include "precomp.hpp" + +namespace Chattr { + +struct Address { + union { + struct sockaddr sockaddr; + struct sockaddr_in sockaddr_in; + struct sockaddr_in6 sockaddr_in6; + }; + socklen_t length; +}; + +} \ No newline at end of file diff --git a/include/Socket/Socket.hpp b/include/Socket/Socket.hpp index cbe3dfe..34f70fc 100644 --- a/include/Socket/Socket.hpp +++ b/include/Socket/Socket.hpp @@ -1,5 +1,6 @@ #pragma once #include "precomp.hpp" +#include "Address.hpp" namespace Chattr { @@ -9,22 +10,27 @@ public: Socket(int domain, int type, int protocol); ~Socket(); + void init(int domain, int type, int protocol); + operator SOCKET() const; + void move(const SOCKET); - void bind(int __fd, const sockaddr *__addr); - void bind(int __fd, const sockaddr *__addr, socklen_t __len); + void bind(const sockaddr *__addr); + void bind(const sockaddr *__addr, socklen_t __len); //IPV4 - void bind(int __fd, const sockaddr_in *__addr); - void bind(int __fd, const sockaddr_in *__addr, socklen_t __len); + void bind(const sockaddr_in *__addr); + void bind(const sockaddr_in *__addr, socklen_t __len); //IPV6 - void bind(int __fd, const sockaddr_in6 *__addr); - void bind(int __fd, const sockaddr_in6 *__addr, socklen_t __len); + void bind(const sockaddr_in6 *__addr); + void bind(const sockaddr_in6 *__addr, socklen_t __len); Socket(const Socket&) = delete; Socket& operator=(const Socket&) = delete; -private: + + struct Address bindAddr; +protected: bool valid_ = false; SOCKET sock_ = INVALID_SOCKET; }; diff --git a/include/Socket/TCPSocket.hpp b/include/Socket/TCPSocket.hpp new file mode 100644 index 0000000..9349b9b --- /dev/null +++ b/include/Socket/TCPSocket.hpp @@ -0,0 +1,15 @@ +#pragma once +#include "Socket.hpp" +#include "precomp.hpp" + +namespace Chattr { + +class TCPSocket : public Socket { +public: + using Socket::init; + void init(int domain); + void listen(int __n); + void accept(TCPSocket& newSock, Address& addr); +}; + +} \ No newline at end of file diff --git a/include/Utils/ConfigManager.hpp b/include/Utils/ConfigManager.hpp new file mode 100644 index 0000000..e088907 --- /dev/null +++ b/include/Utils/ConfigManager.hpp @@ -0,0 +1,20 @@ +#pragma once +#include "precomp.hpp" +#include + +namespace Chattr { +class ConfigManager { +public: + struct Config { + Json::Value configJsonRoot; + std::uint32_t ipVersion = 0; + std::uint32_t listenPort = 0; + spdlog::level::level_enum logLevel = spdlog::level::off; + gsl::czstring logFileName = ""; + std::uint32_t logfileSize = 0; + std::uint32_t logfileCount = 0; + }; + + static Config load(); +}; +} \ No newline at end of file diff --git a/include/precomp.hpp b/include/precomp.hpp index 5b6a007..ea6bbd6 100644 --- a/include/precomp.hpp +++ b/include/precomp.hpp @@ -3,6 +3,7 @@ #ifdef _WIN32 #include #include +#include #include #elif __linux__ #include