parent
652ee8138c
commit
afdcfdc9a8
7 changed files with 181 additions and 0 deletions
@ -0,0 +1,5 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include "bakanet/ip_address.h" |
||||||
|
#include "bakanet/ip_protocol.h" |
||||||
|
#include "bakanet/socket.h" |
@ -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)
|
||||||
|
{ |
||||||
|
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1,23 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <string> |
||||||
|
#include <netinet/in.h> |
||||||
|
#include <arpa/inet.h> |
||||||
|
#include <exception> |
||||||
|
#include <cctype> |
||||||
|
|
||||||
|
#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; |
||||||
|
}; |
||||||
|
} |
@ -0,0 +1,11 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <sys/socket.h> |
||||||
|
|
||||||
|
namespace Bk::Net { |
||||||
|
enum class IpProtocol
|
||||||
|
{ |
||||||
|
TCP = SOCK_STREAM, |
||||||
|
UCP, |
||||||
|
}; |
||||||
|
} |
@ -0,0 +1,11 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <sys/socket.h> |
||||||
|
|
||||||
|
namespace Bk::Net { |
||||||
|
enum class IpVersion |
||||||
|
{ |
||||||
|
IPv4 = AF_INET, |
||||||
|
IPv6 = AF_INET6, |
||||||
|
}; |
||||||
|
} |
@ -0,0 +1,77 @@ |
|||||||
|
#include "socket.h" |
||||||
|
#include <iostream> |
||||||
|
|
||||||
|
#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<char> packet) |
||||||
|
{ |
||||||
|
send(socket_id, packet.data(), packet.size(), 0); |
||||||
|
} |
||||||
|
|
||||||
|
void Socket::write(Connection conn, std::vector<char> packet) |
||||||
|
{ |
||||||
|
send(conn, packet.data(), packet.size(), 0); |
||||||
|
} |
||||||
|
|
||||||
|
std::vector<char> Socket::recv(int size) |
||||||
|
{ |
||||||
|
return { 0 }; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,40 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <netinet/in.h> |
||||||
|
#include <stdlib.h> |
||||||
|
#include <sys/socket.h> |
||||||
|
#include <unistd.h> |
||||||
|
|
||||||
|
#include <vector> |
||||||
|
|
||||||
|
#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<char> packet); |
||||||
|
void write(Connection socket, std::vector<char> packet); |
||||||
|
std::vector<char> recv(int size); |
||||||
|
|
||||||
|
|
||||||
|
private: |
||||||
|
Socket(int socket_id); |
||||||
|
|
||||||
|
int socket_id; |
||||||
|
struct sockaddr_in addr; |
||||||
|
IpAddress ip_addr; |
||||||
|
IpProtocol ip_proto; |
||||||
|
}; |
||||||
|
} |
Loading…
Reference in New Issue