From 7958c9eacef416a50bc4eb14d6ab5435dbaf12ef Mon Sep 17 00:00:00 2001 From: anulax1225 Date: Thu, 29 Feb 2024 04:27:29 +0100 Subject: [PATCH] Refactored project folder --- bakanet/src/bakanet.h | 5 + {src => bakanet/src}/bakanet/ip_address.cpp | 0 {src => bakanet/src}/bakanet/ip_address.h | 0 {src => bakanet/src}/bakanet/ip_protocol.h | 0 {src => bakanet/src}/bakanet/ip_version.h | 1 + bakanet/src/bakanet/socket.cpp | 124 ++++++++++++++++++++ {src => bakanet/src}/bakanet/socket.h | 9 ++ src/bakanet.h | 5 - src/bakanet/socket.cpp | 77 ------------ 9 files changed, 139 insertions(+), 82 deletions(-) create mode 100644 bakanet/src/bakanet.h rename {src => bakanet/src}/bakanet/ip_address.cpp (100%) rename {src => bakanet/src}/bakanet/ip_address.h (100%) rename {src => bakanet/src}/bakanet/ip_protocol.h (100%) rename {src => bakanet/src}/bakanet/ip_version.h (84%) create mode 100644 bakanet/src/bakanet/socket.cpp rename {src => bakanet/src}/bakanet/socket.h (71%) delete mode 100644 src/bakanet.h delete mode 100644 src/bakanet/socket.cpp diff --git a/bakanet/src/bakanet.h b/bakanet/src/bakanet.h new file mode 100644 index 0000000..8f26bc9 --- /dev/null +++ b/bakanet/src/bakanet.h @@ -0,0 +1,5 @@ +#pragma once + +#include +#include +#include \ No newline at end of file diff --git a/src/bakanet/ip_address.cpp b/bakanet/src/bakanet/ip_address.cpp similarity index 100% rename from src/bakanet/ip_address.cpp rename to bakanet/src/bakanet/ip_address.cpp diff --git a/src/bakanet/ip_address.h b/bakanet/src/bakanet/ip_address.h similarity index 100% rename from src/bakanet/ip_address.h rename to bakanet/src/bakanet/ip_address.h diff --git a/src/bakanet/ip_protocol.h b/bakanet/src/bakanet/ip_protocol.h similarity index 100% rename from src/bakanet/ip_protocol.h rename to bakanet/src/bakanet/ip_protocol.h diff --git a/src/bakanet/ip_version.h b/bakanet/src/bakanet/ip_version.h similarity index 84% rename from src/bakanet/ip_version.h rename to bakanet/src/bakanet/ip_version.h index fbb7e2b..50a2d82 100644 --- a/src/bakanet/ip_version.h +++ b/bakanet/src/bakanet/ip_version.h @@ -5,6 +5,7 @@ namespace Bk::Net { enum class IpVersion { + UnSpec = AF_UNSPEC, IPv4 = AF_INET, IPv6 = AF_INET6, }; diff --git a/bakanet/src/bakanet/socket.cpp b/bakanet/src/bakanet/socket.cpp new file mode 100644 index 0000000..a02acbe --- /dev/null +++ b/bakanet/src/bakanet/socket.cpp @@ -0,0 +1,124 @@ +#include "socket.h" +#include + +namespace Bk::Net { + + std::vector dns_lookup(const std::string &host_name, IpVersion ipv = IpVersion::IPv4) + { + std::vector output; + + struct addrinfo hints, *res, *p; + int status, ai_family; + char ip_address[INET6_ADDRSTRLEN]; + + ai_family = (int)ipv; + memset(&hints, 0, sizeof hints); + hints.ai_family = ai_family; + hints.ai_socktype = SOCK_STREAM; + + if ((status = getaddrinfo(host_name.c_str(), NULL, &hints, &res)) != 0) { + //cerr << "getaddrinfo: "<< gai_strerror(status) << endl; + return output; + } + + for(p = res;p != NULL; p = p->ai_next) { + void *addr; + if (p->ai_family == AF_INET) { // IPv4 + struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; + addr = &(ipv4->sin_addr); + } else { // IPv6 + struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr; + addr = &(ipv6->sin6_addr); + } + + // convert the IP to a string + inet_ntop(p->ai_family, addr, ip_address, sizeof ip_address); + output.push_back(ip_address); + } + + freeaddrinfo(res); // free the linked list + + return output; + } + + 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) + { + std::vector buffer; + buffer.resize(size); + int status = read(socket_id, buffer.data(), buffer.size() - 1); + return status ? buffer : std::vector(0); + } + + std::vector Socket::recv(Connection conn, int size) + { + std::vector buffer; + buffer.resize(size); + int status = read(conn, buffer.data(), buffer.size() - 1); + return status ? buffer : std::vector(0); + } +} \ No newline at end of file diff --git a/src/bakanet/socket.h b/bakanet/src/bakanet/socket.h similarity index 71% rename from src/bakanet/socket.h rename to bakanet/src/bakanet/socket.h index 4e2e752..3c150e3 100644 --- a/src/bakanet/socket.h +++ b/bakanet/src/bakanet/socket.h @@ -4,16 +4,24 @@ #include #include #include +#include +#include +#include #include +#include #include "ip_address.h" #include "ip_protocol.h" +#define log(str) std::cout << str << "\n"; + namespace Bk::Net { using Connection = int; + std::vector dns_lookup(const std::string &host_name, IpVersion ipv); + class Socket { public: @@ -27,6 +35,7 @@ namespace Bk::Net { void write(std::vector packet); void write(Connection socket, std::vector packet); std::vector recv(int size); + std::vector recv(Connection socket, int size); private: diff --git a/src/bakanet.h b/src/bakanet.h deleted file mode 100644 index 64ce5d4..0000000 --- a/src/bakanet.h +++ /dev/null @@ -1,5 +0,0 @@ -#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/socket.cpp b/src/bakanet/socket.cpp deleted file mode 100644 index 7744cf4..0000000 --- a/src/bakanet/socket.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#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