From afdcfdc9a83a63a69143b4c69f0284ce645c4a0b Mon Sep 17 00:00:00 2001 From: anulax1225 Date: Wed, 28 Feb 2024 23:45:20 +0100 Subject: [PATCH] Created simple abstraction for socket usage --- src/bakanet.h | 5 +++ src/bakanet/ip_address.cpp | 14 +++++++ src/bakanet/ip_address.h | 23 ++++++++++++ src/bakanet/ip_protocol.h | 11 ++++++ src/bakanet/ip_version.h | 11 ++++++ src/bakanet/socket.cpp | 77 ++++++++++++++++++++++++++++++++++++++ src/bakanet/socket.h | 40 ++++++++++++++++++++ 7 files changed, 181 insertions(+) create mode 100644 src/bakanet.h create mode 100644 src/bakanet/ip_address.cpp create mode 100644 src/bakanet/ip_address.h create mode 100644 src/bakanet/ip_protocol.h create mode 100644 src/bakanet/ip_version.h create mode 100644 src/bakanet/socket.cpp create mode 100644 src/bakanet/socket.h diff --git a/src/bakanet.h b/src/bakanet.h new file mode 100644 index 0000000..64ce5d4 --- /dev/null +++ b/src/bakanet.h @@ -0,0 +1,5 @@ +#pragma once + +#include "bakanet/ip_address.h" +#include "bakanet/ip_protocol.h" +#include "bakanet/socket.h" \ No newline at end of file diff --git a/src/bakanet/ip_address.cpp b/src/bakanet/ip_address.cpp new file mode 100644 index 0000000..1ecb8d7 --- /dev/null +++ b/src/bakanet/ip_address.cpp @@ -0,0 +1,14 @@ +#include "ip_address.h" + +namespace Bk::Net { + IpAddress::IpAddress(const char* ip) + : str(ip) + { + if (inet_pton(AF_INET, str, &bytes) <= 0) perror("Bad IP"); + } + + void IpAddress::from_dns(char* dns) + { + + } +} \ No newline at end of file diff --git a/src/bakanet/ip_address.h b/src/bakanet/ip_address.h new file mode 100644 index 0000000..c274ebb --- /dev/null +++ b/src/bakanet/ip_address.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "ip_version.h" + +namespace Bk::Net { + class IpAddress + { + public: + IpAddress(const char* ip); + + static void from_dns(char* dns); + + const char* str; + in_addr bytes; + IpVersion version = IpVersion::IPv4; + }; +} \ No newline at end of file diff --git a/src/bakanet/ip_protocol.h b/src/bakanet/ip_protocol.h new file mode 100644 index 0000000..86cbb6e --- /dev/null +++ b/src/bakanet/ip_protocol.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace Bk::Net { + enum class IpProtocol + { + TCP = SOCK_STREAM, + UCP, + }; +} \ No newline at end of file diff --git a/src/bakanet/ip_version.h b/src/bakanet/ip_version.h new file mode 100644 index 0000000..fbb7e2b --- /dev/null +++ b/src/bakanet/ip_version.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace Bk::Net { + enum class IpVersion + { + IPv4 = AF_INET, + IPv6 = AF_INET6, + }; +} \ No newline at end of file diff --git a/src/bakanet/socket.cpp b/src/bakanet/socket.cpp new file mode 100644 index 0000000..7744cf4 --- /dev/null +++ b/src/bakanet/socket.cpp @@ -0,0 +1,77 @@ +#include "socket.h" +#include + +#define log(str) std::cout << str << "\n"; + +namespace Bk::Net { + + Socket::Socket(IpAddress ip, int port,IpProtocol proto) + : ip_addr(ip), ip_proto(proto) + { + //Socket creation step + if ((socket_id = socket((int)ip_addr.version, (int)ip_proto, 0)) < 0) + { + perror("socket failed"); + exit(EXIT_FAILURE); + } + addr.sin_addr = ip_addr.bytes; + addr.sin_family = (int)ip_addr.version; + addr.sin_port = htons(port); + } + + Socket::~Socket() + { + close(socket_id); + } + + bool Socket::init() + { + //Binding step + if (bind(socket_id, (struct sockaddr*)&addr, sizeof(addr)) < 0) + { + return false; + } + return true; + } + + bool Socket::start(int cpt_conn) + { + //Listening step + if (listen(socket_id, cpt_conn) < 0) + { + return false; + } + return true; + } + + Connection Socket::ack() + { + + socklen_t addrlen = sizeof(addr); + return accept(socket_id, (struct sockaddr*)&addr, &addrlen); + } + + bool Socket::conn() + { + if (connect(socket_id, (struct sockaddr*)&addr, sizeof(addr)) < 0) + { + return false; + } + return true; + } + + void Socket::write(std::vector packet) + { + send(socket_id, packet.data(), packet.size(), 0); + } + + void Socket::write(Connection conn, std::vector packet) + { + send(conn, packet.data(), packet.size(), 0); + } + + std::vector Socket::recv(int size) + { + return { 0 }; + } +} \ No newline at end of file diff --git a/src/bakanet/socket.h b/src/bakanet/socket.h new file mode 100644 index 0000000..4e2e752 --- /dev/null +++ b/src/bakanet/socket.h @@ -0,0 +1,40 @@ +#pragma once + +#include +#include +#include +#include + +#include + +#include "ip_address.h" +#include "ip_protocol.h" + +namespace Bk::Net { + + using Connection = int; + + class Socket + { + public: + Socket(IpAddress ip, int port, IpProtocol proto); + ~Socket(); + + bool init(); + bool start(int cpt_conn); + Connection ack(); + bool conn(); + void write(std::vector packet); + void write(Connection socket, std::vector packet); + std::vector recv(int size); + + + private: + Socket(int socket_id); + + int socket_id; + struct sockaddr_in addr; + IpAddress ip_addr; + IpProtocol ip_proto; + }; +} \ No newline at end of file