Big Refactor, currently broken

This commit is contained in:
scientiist
2023-04-20 10:29:32 -05:00
parent ca29755d1e
commit 16150ef874
13 changed files with 378 additions and 62 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
build
# Prerequisites
*.d

15
CMakeLists.txt Normal file
View File

@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 2.8)
project(Sockets)
file(GLOB SOURCES src/*.cpp)
include_directories(include)
add_library(${PROJECT_NAME} SHARED ${SOURCES})
install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME})
file(GLOB HEADERS include/*.hpp)
install(FILES ${HEADERS} DESTINATION include/${PROJECT_NAME})

View File

@@ -17,75 +17,44 @@ namespace Socket {
return message;
}
};
class SocketBindingException : public SocketException {
public:
SocketBindingException(char *msg) : SocketException{msg} {}
};
class SocketConnectException : public SocketException {
public:
SocketConnectException(char *msg) : SocketException{msg} {}
};
class SocketCreationException : public SocketException {
public:
SocketCreationException(char * msg) : SocketException{msg} {}
};
class SocketListenException : public SocketException {
public:
SocketListenException(char * msg) : SocketException{msg} {}
};
class SocketAcceptException : public SocketException {
public:
SocketAcceptException(char * msg) : SocketException{msg} {}
};
class SocketSendException : public SocketException {
public:
SocketSendException(char * msg) : SocketException{msg} {}
};
class SocketReceiveException : public SocketException {
public:
SocketReceiveException(char * msg) : SocketException{msg} {}
};
class SocketCloseException : public SocketException {
public:
SocketCloseException(char * msg) : SocketException{msg} {}
};
class TcpSocket {
public:
TcpSocket();
TcpSocket(int family, int flags);
TcpSocket(int socket, addrinfo info, bool connected, bool bound);
virtual ~TcpSocket();
TcpSocket(const TcpSocket &socket) = default;
TcpSocket &operator=(const TcpSocket &socket) = delete;
void bind(int port);
void connect(std::string address, int port);
void listen(int maxQueue);
std::shared_ptr<TcpSocket> accept();
void send(const char *data, unsigned int length, int flags);
// Receive data (blocking)
// @return true if socket is still open, false otherwise
bool receive(char* msg, int len, int flags);
void close();
private:
void setInfo(int port);
void setInfo(std::string address, int port);
void openSocket(addrinfo *info);
addrinfo * mInfo;
int mSock = -1;
bool mSockCreated = false;
bool mBound = false;
bool mConnected = false;
bool mClosed = false;
};
}

117
include/IPAddress.hpp Normal file
View File

@@ -0,0 +1,117 @@
#pragma once
#include <cstring>
#include <array>
#include <string>
#include <vector>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
namespace Socket
{
struct IPAddress
{
public:
static IPAddress Any(uint16_t portno) { return IPAddress{INADDR_ANY, portno};}
static IPAddress Loopback(uint16_t portno) { return IPAddress{INADDR_LOOPBACK, portno};}
static IPAddress Broadcast(uint16_t portno) { return IPAddress{INADDR_BROADCAST, portno};}
IPAddress() {}
IPAddress(const std::string& ipaddr, uint16_t port);
IPAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t portno);
IPAddress(const sockaddr_in_t& addr_in);
operator sockaddr_in_t() const;
const uint8_t& operator[](size_t octet) const;
uint8_t& operator[](size_t octet);
bool operator==(const IPAddress& other) const;
bool operator !=(const IPAddress& other) const;
std::string addr_string() const;
std::string port_string() const;
std::string to_string() const;
operator std::string() const { return this->to_string();}
private:
IPAddress(uint32_t ipaddr, uint16_t portno);
};
bool IPAddress::operator==(const IPAddress& other) const
{
return this->octets = other.octets && this->port == other.port;
}
bool IPAddress::operator !=(const IPAddress& other) const
{
return !(*this == other);
}
const uint8_t& IPAddress::operator[](size_t octet) const
{
return octets[octet];
}
uint8_t& IPAddress::operator[](size_t octet) const
{
return octets[octet];
}
IPAddress::IPAddress(uint32_t ipaddr, uint16_t portno)
{
*(uint32_t*)octets.data() = htonl(ipaddr);
port = portno;
}
IPAddress::IPAddress(const char* ipaddr)
{
int ret = ::inet_pton(AF_INET, ipaddr.c_str(), (uint32_t*)octets.data());
if (ret > 0) {
port = portno;
} else {
throw InvalidAddressException(errno);
}
}
IPAddress::IPAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t portno)
{
octets[0] = a;
octets[1] = b;
octets[2] = c;
octets[3] = d;
port = portno;
}
IPAddress::IPAddress(const sockaddr_in_t& addr_in) {
*(uint32_t*)octets.data() = addr_in.sin_addr.s_addr;
port = ntohs(addr_in.sin_port);
}
operator IPAddress::sockaddr_in_t() const
{
sockaddr_in_t addr_in;
std::memset(&addr_in, 0, sizeof(addr_in));
addr_in.sin_family = AF_INET;
addr_in.sin_addr.s_addr = *(uint32_t*)octets.data();
addr_in.sin_port = htons(port);
return addr_in;
}
std::string IPAddress::addr_string() const
{
return std::to_string(octets[0]) +
'.' + std::to_string(octets[1]) +
'.' + std::to_string(octets[2]) +
'.' + std::to_string(octets[3]);
}
std::string IPAddress::port_string() const
{
return std::to_string(this->port);
}
std::string IPAddress::to_string() const
{
return this->addr_string() + ":" + this->port_string();
}
}

0
include/TcpClient.hpp Normal file
View File

0
include/TcpServer.hpp Normal file
View File

49
include/TcpSocket.hpp Normal file
View File

@@ -0,0 +1,49 @@
#pragma once
#include <iostream>
#include <string>
#include <netdb.h>
#include <memory>
#include <cstring>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include "IPAddress.hpp"
#ifndef INPORT_ANY
#define INPORT_ANY
#endif
namespace Socket {
class TcpSocket {
public:
TcpSocket();
TcpSocket(int family, int flags);
TcpSocket(int socket, addrinfo info, bool connected, bool bound);
virtual ~TcpSocket();
TcpSocket(const TcpSocket &socket) = default;
TcpSocket &operator=(const TcpSocket &socket) = delete;
void Bind(int port);
void Connect(std::string address, int port);
void Listen(int maxQueue);
std::shared_ptr<TcpSocket> Accept();
void Send(const char *data, unsigned int length, int flags);
// Receive data (blocking)
// @return true if socket is still open, false otherwise
bool Receive(char* msg, int len, int flags);
void Close();
private:
void setInfo(int port);
void setInfo(std::string address, int port);
void openSocket(addrinfo *info);
addrinfo * mInfo;
int mSock = -1;
bool mSockCreated = false;
bool mBound = false;
bool mConnected = false;
bool mClosed = false;
};
}

0
include/UdpClient.hpp Normal file
View File

164
include/UdpSocket.hpp Normal file
View File

@@ -0,0 +1,164 @@
#pragma once
#include <IPAddress.hpp>
#ifndef INPORT_ANY
#define INPORT_ANY
#
namespace Socket
{
typedef struct sockaddr_in sockaddr_in_t;
typedef struct sockaddr sockaddr_t;
typedef std::vector<uint8_t> msg_t;
class UdpSocket {
private:
int sock{-1};
sockaddr_in_t self_addr{};
socklen_t self_addr_len = sizeof(self_addr);
sockaddr_in_t peer_addr{};
socklen_t peer_addr_len = sizeof(peer_addr);
public:
UdpSocket();
UdpSocket(int family, int flags);
UdpSocket(int socket, addrinfo info, bool connected, bool bound);
virtual ~UdpSocket();
UdpSocket(const UdpSocket &socket) = default;
void Open()
{
this->Close();
sock = (int)::socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (this->IsClosed()) {
throw SocketCreationException(strerror(errno));
}
}
int Close()
{
if (!this->IsClosed())
{
#ifdef _WIN32
int ret = ::shutdown(sock, SD_BOTH);
#else
int ret = ::shutdown(sock, SHUT_RDWR);
#endif
if (ret < 0)
{
throw SocketException(strerror(errno));
}
#ifdef _WIN32
ret = ::closesocket(sock);
#else
ret = ::close(sock);
if (ret < 0) {
throw SocketCloseException(strerror(errno));
}
sock = -1;
}
return 0;
}
bool IsClosed() const { return sock < 0;}
void Bind(const IPAddress& ipaddr)
{
self_addr = ipaddr;
self_addr_len = sizeof(self_addr);
int opt = 1;
int ret = ::setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
if (ret < 0)
{
throw SocketSetOptionException(strerror(errno));
}
ret = ::bind(sock, (sockaddr_t*)&self_addr, self_addr_len);
if (ret < 0)
{
throw SocketBindingError(strerror(errno));
}
ret = ::getsockname(sock, (sockaddr_t*)&self_addr, &self_addr_len);
if (ret < 0)
{
throw SocketGetNameError(strerror(errno));
}
}
int Bind(uint16_t portno)
{
auto ipaddr = IPAddress::Any(portno);
return this->Bind(ipaddr);
}
int BindAny()
{
return this->Bind(INPORT_ANY);
}
int BindAny(uint16_t& portno)
{
int ret = this->Bind(INPORT_ANY);
if (ret < 0) {
return ret;
}
portno = IPAddress{self_addr}.port;
return 0;
}
int Connect(const IPAddress& ipaddr)
{
peer_addr = ipaddr;
peer_addr_len = sizeof(peer_addr);
int ret = ::connect(sock, (sockaddr_t*)&peer_addr, peer_addr_len);
if (ret < 0) {
throw SocketConnectException(strerror(errno));
}
}
int Connect(uint16_t portno)
{
auto ipaddr = IPAddress::Loopback(portno);
return this->connect(ipaddr);
}
IPAddress GetSelfIP() const {return self_addr;}
IPAddress GetPeerIP() const {return peer_addr;}
template <typename T, typename = typename std::enable_if<sizeof(typename T::value_type) == sizeof(uint8_t)>::type>
int Send(const T& message, const IPAddress& ipaddr) const
{
sockaddr_in_t addr_in = ipaddr;
socklen_t addr_in_len = sizeof(addr_in);
int ret = ::sendto(sock, (const char*)message.data(), message.size(), 0, (sockaddr_t*)&addr_in, addr_in_len);
if (ret < 0)
{
throw SocketException(strerror(errno));
}
return ret;
}
template <typename T, typename = typename
std::enable_if<sizeof(typename T::value_type) == sizeof(uint8_t)>::type>
int Receive(T& message, IPAddress& ipaddr) const
{
sockaddr_in_t addr_in;
socklen_t addr_in_len = sizeof(addr_in);
typename T::value_type buffer[10 * 1024];
int ret = ::recvfrom(sock, (char*)buffer, sizeof(buffer), 0, (sockaddr_t*)&addr_in, &addr_in_len);
if (ret < 0)
{
throw SocketReceiveException(strerror(errno));
}
ipaddr = addr_in;
message = { std::begin(buffer), std::begin(buffer)+ret};
return ret;
}
int Broadcast(int opt) const
{
int ret = ::setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const char*)&opt, sizeof(opt));
if (ret < 0) {
throw SocketException(strerror(errno));
}
return 0;
}
int Interrupt() const
{
uint16_t portno = IPAddress{self_addr}.port;
auto ipaddr -> IPAddress::Loopback(portno);
return this->Send(msg_t{}. ipaddr);
}
};
}

View File

@@ -14,25 +14,27 @@ int main(int argc, char *argv[])
Socket_p client;
try {
sock->bind(11170);
sock->listen(5);
client = sock->accept();
sock->Bind(42069);
sock->Listen(5);
client = sock->Accept();
} catch(std::exception &e) {
std::cout << e.what() << std::endl;
return;
}
// Welcoming the new user
client->send("Welcome !\n\f", 15, 0);
client->Send("Welcome !\n\f", 15, 0);
// Closing the listening socket, we want nobody else.
sock->close();
sock->Close();
char data[512];
memset(&data, 0, 512);
while (client->receive(data, sizeof data, 0))
while (client->Receive(data, sizeof data, 0))
{
client->send(data, sizeof data, 0);
std::cout << "[Recv] " << data << std::endl;
client->Send(data, sizeof data, 0);
memset(&data, 0, 512);
}
client->close();
client->Close();
return 0;
}

View File

@@ -1,3 +1,4 @@
#include "Exceptions.hpp"
#include "TcpSocket.hpp"
#include <sys/types.h>
#include <sys/socket.h>
@@ -39,10 +40,7 @@ namespace Socket {
mSock = socket(mInfo->ai_family, mInfo->ai_socktype, 0);
if (mSock == -1)
{
SocketCreationException except(strerror(errno));
throw except;
}
throw SocketCreationException(strerror(errno));
// Socket successfully "opened"
mSockCreated = true;
}
@@ -56,11 +54,11 @@ namespace Socket {
TcpSocket::~TcpSocket()
{
if (!mClosed)
close();
Close();
freeaddrinfo(mInfo);
}
void TcpSocket::bind(int port)
void TcpSocket::Bind(int port)
{
if (mBound && mConnected)
throw SocketBindingException("Already bound!");
@@ -91,7 +89,7 @@ namespace Socket {
throw SocketBindingException("Can't bind to port");
}
void TcpSocket::connect(std::string address, int port)
void TcpSocket::Connect(std::string address, int port)
{
if (mConnected)
throw SocketConnectException("Already Connected!");
@@ -118,14 +116,14 @@ namespace Socket {
throw SocketConnectException("Can't connect to host");
}
void TcpSocket::listen(int maxQueue)
void TcpSocket::Listen(int maxQueue)
{
if (::listen(mSock, maxQueue) != 0)
throw SocketListenException(strerror(errno));
DEBUG("Listening...");
}
std::shared_ptr<TcpSocket> TcpSocket::accept()
std::shared_ptr<TcpSocket> TcpSocket::Accept()
{
DEBUG("Starting to accept");
union
@@ -135,12 +133,13 @@ namespace Socket {
sockaddr_in6 in6;
sockaddr_storage s;
} address;
DEBUG("?");
socklen_t addressSize = sizeof(sockaddr_storage);
DEBUG("?");
int newSock;
if ((newSock = ::accept(mSock, (sockaddr*)&address.s, &addressSize)) == -1)
if ((newSock = ::accept(mSock, (struct sockaddr*)0, (int*)0))==-1) {
//if ((newSock = ::accept(mSock, (sockaddr*)&address.s, &addressSize)) == -1) {
DEBUG(strerror(errno));
throw SocketAcceptException(strerror(errno));
}
DEBUG("1 client accepted");
@@ -157,7 +156,7 @@ namespace Socket {
return std::shared_ptr<TcpSocket>(new TcpSocket(newSock, info, true, false));
}
void TcpSocket::send(const char *data, unsigned int length, int flags)
void TcpSocket::Send(const char *data, unsigned int length, int flags)
{
const char * buff = data;
int status = 0;
@@ -176,7 +175,7 @@ namespace Socket {
}
}
bool TcpSocket::receive(char* msg, int len, int flags)
bool TcpSocket::Receive(char* msg, int len, int flags)
{
int status;
if ((status = ::recv(mSock, msg, len, flags)) == -1)
@@ -187,7 +186,7 @@ namespace Socket {
return true;
}
void TcpSocket::close()
void TcpSocket::Close()
{
if (::close(mSock) == -1)
throw SocketCloseException(strerror(errno));

0
src/UdpServer.cpp Normal file
View File

0
src/UdpSocket.cpp Normal file
View File