commit fa237eb31c25483c9804f70deb14e62795d28600 Author: Anthony Debucquoy Date: Fri Nov 4 13:25:09 2022 +0100 First Commit, not finished diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c9aa9b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +mcping diff --git a/Connection.cpp b/Connection.cpp new file mode 100644 index 0000000..a833f54 --- /dev/null +++ b/Connection.cpp @@ -0,0 +1,47 @@ +#include "Connection.h" +#include +#include +#include +#include + +void Connection::p_HandleError(){ + std::cout << "an error occured during execution" << std::endl; + switch (error) { + case ErrorTypes::socket_creation: + perror("error while trying to create a file descriptor"); + break; + case ErrorTypes::get_ip: + perror("error while trying to get ip"); + break; + case ErrorTypes::connect: + perror("error while trying to connect"); + break; + default: + std::cerr << "An unknown error occured" << std::endl; + } +} + +int Connection::status(){ + return static_cast(error); +} + +Connection::Connection(std::string server_ip, int port){ + fd = socket(AF_INET, SOCK_STREAM, 0); + if (fd < 0){ + error = ErrorTypes::socket_creation; + } + + in_addr in_a; + if(!inet_aton(server_ip.data(), &in_a)){ + error = ErrorTypes::get_ip; + } + + sockaddr_in addr({ AF_INET, htons(port), in_a }); + if(connect(fd, (sockaddr*) &addr, sizeof(addr)) != 0){ + error = ErrorTypes::connect; + } +} + +Connection::~Connection(){ + close(fd); +} diff --git a/Connection.h b/Connection.h new file mode 100644 index 0000000..3d4ccb4 --- /dev/null +++ b/Connection.h @@ -0,0 +1,38 @@ +#ifndef CONNECTION_H +#define CONNECTION_H + +#include +#include + +class Payload; //TODO + +class Connection +{ +private: + int fd; + + enum class ErrorTypes{ + none = 0, + + socket_creation = 10, + get_ip, + connect, + }error; + + void p_HandleError(); + +public: + Connection(std::string server_ip, int port); + ~Connection(); + + bool send(std::vector payload); + bool recv(void* data, int size); + + /** + * Return the error code from the last command if there is one + * @return error_code + */ + int status(); +}; + +#endif /* CONNECTION_H */ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dcaabb1 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +CXX=c++ +CXXFLAGS= -g -Wall +LDLIBS= + +SOURCES=$(wildcard *.cpp) + +all: mcping + +mcping: $(SOURCES) + $(CXX) $(CXXFLAGS) -o $< $(SOURCES) $(LDLIBS) + +run: mcping + ./mcping + +clean: + rm -f *.o mcping diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..ac59ec4 --- /dev/null +++ b/main.cpp @@ -0,0 +1,8 @@ +#include "Connection.h" + +int main(int argc, char *argv[]) +{ + Connection c("145.239.73.162", 25565); + + return 0; +}