diff --git a/Makefile b/Makefile index dfbc8fa..9f1c8f9 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,14 @@ -# **************************************************************************** # +#******************************************************************************# # # # ::: :::::::: # # Makefile :+: :+: :+: # # +:+ +:+ +:+ # -# By: vavaas +#+ +:+ +#+ # +# By: vvaas +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/08/09 15:08:49 by vavaas #+# #+# # -# Updated: 2024/01/21 12:04:28 by maldavid ### ########.fr # +# Updated: 2024/01/21 17:23:26 by vvaas ### ########.fr # # # -# **************************************************************************** # +#******************************************************************************# NAME = ircserv @@ -24,7 +24,12 @@ OBJ_DIR = objs OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o)) CXX = c++ -CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -I includes +FLAGS ?= true +CXXFLAGS = -std=c++98 -I includes +ifeq ($(FLAGS), true) + CXXFLAGS += -Wall -Wextra -Werror +endif + DEBUG ?= false MODE = "release" diff --git a/includes/client.hpp b/includes/client.hpp index 3286fb4..e9c4fd6 100644 --- a/includes/client.hpp +++ b/includes/client.hpp @@ -1,14 +1,14 @@ -/* ************************************************************************** */ +/******************************************************************************/ /* */ /* ::: :::::::: */ /* client.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ +/* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:33:17 by maldavid #+# #+# */ -/* Updated: 2024/01/21 10:34:13 by maldavid ### ########.fr */ +/* Updated: 2024/01/21 16:32:05 by vvaas ### ########.fr */ /* */ -/* ************************************************************************** */ +/******************************************************************************/ #ifndef __CLIENT__ #define __CLIENT__ @@ -18,11 +18,12 @@ namespace irc class Client { public: - Client(); + Client(int fd); ~Client(); private: + int _fd; }; } diff --git a/includes/irc.hpp b/includes/irc.hpp index 6f26063..9a52065 100644 --- a/includes/irc.hpp +++ b/includes/irc.hpp @@ -1,14 +1,14 @@ -/* ************************************************************************** */ +/******************************************************************************/ /* */ /* ::: :::::::: */ /* irc.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: vvaas +#+ +:+ +#+ */ +/* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/29 13:24:01 by vvaas #+# #+# */ -/* Updated: 2023/11/29 16:32:34 by vvaas ### ########.fr */ +/* Updated: 2024/01/21 14:20:31 by vvaas ### ########.fr */ /* */ -/* ************************************************************************** */ +/******************************************************************************/ #ifndef IRC_H # define IRC_H @@ -20,5 +20,5 @@ # include # include # include - +# include #endif diff --git a/includes/server.hpp b/includes/server.hpp index b1c24f5..e2bd3df 100644 --- a/includes/server.hpp +++ b/includes/server.hpp @@ -1,21 +1,24 @@ -/* ************************************************************************** */ +/******************************************************************************/ /* */ /* ::: :::::::: */ /* server.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ +/* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 09:12:28 by maldavid #+# #+# */ -/* Updated: 2024/01/21 10:37:58 by maldavid ### ########.fr */ +/* Updated: 2024/01/21 17:18:17 by vvaas ### ########.fr */ /* */ -/* ************************************************************************** */ +/******************************************************************************/ #ifndef __SERVER_IRC__ #define __SERVER_IRC__ #include #include +#include +#include +#define MAX_USERS 20 namespace irc { class Server @@ -24,12 +27,24 @@ namespace irc Server(int port, const std::string& password); ~Server(); + inline unsigned long get_port(void) { return (htons(_port)); } private: + void init_socket(void); + void wait(void); + + private: + sockaddr_in _s_data; + socklen_t _s_len; + fd_set _fd_set; + std::vector _channels; + std::vector > _client; const std::string _password; + const std::string _ip; const int _port; - int _socket; + int _main_socket; + bool _active; }; } diff --git a/srcs/client.cpp b/srcs/client.cpp index a9a13df..6689993 100644 --- a/srcs/client.cpp +++ b/srcs/client.cpp @@ -1,20 +1,20 @@ -/* ************************************************************************** */ +/******************************************************************************/ /* */ /* ::: :::::::: */ /* client.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ +/* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:35:52 by maldavid #+# #+# */ -/* Updated: 2024/01/21 10:36:20 by maldavid ### ########.fr */ +/* Updated: 2024/01/21 16:32:12 by vvaas ### ########.fr */ /* */ -/* ************************************************************************** */ +/******************************************************************************/ #include namespace irc { - Client::Client() + Client::Client(int fd) : _fd(fd) { } diff --git a/srcs/main.cpp b/srcs/main.cpp index abe5d1b..5254c2b 100644 --- a/srcs/main.cpp +++ b/srcs/main.cpp @@ -1,14 +1,14 @@ -/* ************************************************************************** */ +/******************************************************************************/ /* */ /* ::: :::::::: */ /* main.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ +/* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/20 09:27:04 by maldavid #+# #+# */ -/* Updated: 2024/01/21 12:16:41 by maldavid ### ########.fr */ +/* Updated: 2024/01/21 16:14:07 by vvaas ### ########.fr */ /* */ -/* ************************************************************************** */ +/******************************************************************************/ #include #include @@ -24,13 +24,12 @@ int main(int ac, char** av) irc::logs::report(irc::log_message, "usage './ircserv ', try again dumbass"); return 0; } - if(av[1] == NULL || av[2] == NULL) irc::logs::report(irc::log_fatal_error, "invalid argv, argv[1] or argv[2] is NULL (wtf)"); char* end; - long port = std::strtol(av[1], &end, 10); - if(errno == ERANGE || *end != 0 || port < 0 || port > 0xffff) + int port = std::strtol(av[1], &end, 10); + if(errno == ERANGE || *end != 0 || port < 0 || port > 0xFFFF || std::strlen(av[1]) == 0) irc::logs::report(irc::log_fatal_error, "invalid port"); irc::Server server(port, av[2]); diff --git a/srcs/server.cpp b/srcs/server.cpp index 1772652..4bede37 100644 --- a/srcs/server.cpp +++ b/srcs/server.cpp @@ -1,18 +1,20 @@ -/* ************************************************************************** */ +/******************************************************************************/ /* */ /* ::: :::::::: */ /* server.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ +/* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 09:31:17 by maldavid #+# #+# */ -/* Updated: 2024/01/21 12:13:51 by maldavid ### ########.fr */ +/* Updated: 2024/01/21 17:31:15 by vvaas ### ########.fr */ /* */ -/* ************************************************************************** */ +/******************************************************************************/ #include #include #include +#include +#include /** Commands to handle * NICK @@ -31,13 +33,41 @@ namespace irc { - Server::Server(int port, const std::string& password) : _password(password), _port(port) + Server::Server(int port, const std::string& password) : _s_len(sizeof(_s_data)), _password(password), _port(port), _active(true) { - (void)_port; - (void)_password; - (void)_socket; - (void)_channels; + std::memset(&_s_data, 0, sizeof(sockaddr)); + init_socket(); + wait(); + } + void Server::init_socket(void) + { + _s_data.sin_family = AF_INET; + _s_data.sin_addr.s_addr = INADDR_ANY; + _s_data.sin_port = htons(_port); + _main_socket = socket(AF_INET, SOCK_STREAM, 0); // AF_INET == IPv4, SOCK_STREAM == TCP + if (_main_socket < 0) + irc::logs::report(irc::log_fatal_error, "socket error"); + std::cout << "Socket creating succesful" << std::endl; + if (bind(_main_socket, (struct sockaddr *)&_s_data, sizeof(_s_data)) != 0) + irc::logs::report(irc::log_fatal_error, "bind error"); + std::cout << "bind succesful, starting listen loop" << std::endl; + if (listen(_main_socket, MAX_USERS) != 0) + irc::logs::report(irc::log_fatal_error, "listen error"); + std::cout << "listen queue created succesful" << std::endl; } - Server::~Server() {} + void Server::wait(void) + { + while (_active) + { + FD_ZERO(&_fd_set); + FD_SET(_main_socket, &_fd_set); + } + } + Server::~Server() + { + if (_main_socket > 0) + close(_main_socket); + } + }