Merge pull request #1 from scientiist/Headers

Headers
This commit is contained in:
Josh O'Leary
2023-04-29 19:54:03 -05:00
committed by GitHub
25 changed files with 635 additions and 40 deletions

1
.gitignore vendored
View File

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

22
CMakeLists.txt Normal file
View File

@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 2.9)
project(Sockets)
include(CTest)
file(GLOB SOURCES src/Sockets/*.cpp)
include_directories(include)
add_library(${PROJECT_NAME} SHARED ${SOURCES} )
set(CMAKE_CXX_FLAGS "-pedantic -Wall -Wextra")
install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME})
file(GLOB HEADERS include/Sockets/*.hpp)
install(FILES ${HEADERS} DESTINATION include/${PROJECT_NAME})
add_subdirectory(tests)
add_executable(SocketDemo main.cpp)
target_link_libraries(SocketDemo ${PROJECT_NAME})

View File

@@ -1,2 +1,12 @@
# TcpSocket
C++ Implementation of a TcpSocket wrapper
# Sockets Library
C++ TCP/UDP Sockets library
## What's inside?
## Compiling
## Examples
## Known Problems
## Licensing

View File

@@ -0,0 +1,66 @@
#pragma once
#include <iostream>
#include <string>
#include <netdb.h>
#include <memory>
namespace Socket {
class SocketException : public std::exception {
protected:
const char * message;
public:
SocketException(const std::string& msg) : message(msg.c_str()) {}
SocketException(std::string msg) : message(msg.c_str()) {}
SocketException(const char *msg) : message(msg) {}
const char * what() {
return message;
}
};
class BadUriException : public SocketException {
public:
BadUriException(char *msg) : SocketException{msg} {}
};
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} {}
};
}

View File

@@ -0,0 +1 @@
#pragma once

View File

@@ -0,0 +1 @@
#pragma once

View File

@@ -0,0 +1 @@
#pragma once

View File

@@ -0,0 +1,48 @@
#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>
#include <Sockets/Exceptions.hpp>
namespace Socket
{
typedef struct sockaddr_in sockaddr_in_t;
typedef struct sockaddr sockaddr_t;
struct IPAddress
{
std::array<uint8_t, 4> octets{};
uint16_t port{};
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};}
// Tries to resolve an IP address from a URL
static IPAddress Resolve(std::string const& uri, uint16_t port);
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;
bool operator == (const IPAddress& other) const;
bool operator != (const IPAddress& other) const;
[[nodiscard]] std::string addr_string() const;
[[nodiscard]] std::string port_string() const;
[[nodiscard]] std::string to_string() const;
operator std::string() const { return this->to_string();}
private:
IPAddress(uint32_t ipaddr, uint16_t portno);
};
}

View File

View File

View File

@@ -4,9 +4,19 @@
#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 <Sockets/IPAddress.hpp>
#ifndef INPORT_ANY
#define INPORT_ANY 0
#endif
namespace Socket {
class TcpSocket {
public:
TcpSocket();
@@ -16,15 +26,18 @@ namespace Socket {
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);
void Bind(uint16_t port);
void BindAny();
void BindAny(uint16_t&portno);
void Connect(const IPAddress& ipaddr);
void Connect(std::string address, uint16_t 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();
bool Receive(char* msg, int len, int flags);
void Close();
private:
void setInfo(int port);
void setInfo(std::string address, int port);

View File

View File

@@ -0,0 +1,49 @@
#pragma once
#include <Sockets/IPAddress.hpp>
#ifndef INPORT_ANY
#define INPORT_ANY 0
#endif
namespace Socket
{
typedef struct sockaddr_in sockaddr_in_t;
typedef struct sockaddr sockaddr_t;
typedef std::vector<uint8_t> msg_t;
class UdpSocket {
public:
UdpSocket() {}
UdpSocket(int family, int flags);
UdpSocket(int socket, addrinfo info, bool connected, bool bound);
virtual ~UdpSocket();
UdpSocket(const UdpSocket &socket) = default;
UdpSocket &operator=(const UdpSocket &socket) = delete;
void Open();
int Close();
bool IsClosed() const { return sock < 0;}
void Bind(const IPAddress& ipAddress);
void Bind(uint16_t portno);
void BindAny();
void BindAny(uint16_t& portno);
void Connect(const IPAddress& ipaddr);
void Connect(uint16_t portno);
[[nodiscard]] IPAddress GetSelfIP() const {return self_addr;}
[[nodiscard]] IPAddress GetPeerIP() const {return peer_addr;}
public:
template <typename T>
int Send(const T& message, const IPAddress& ipaddr) const;
template <typename T>
int Receive(T& message, IPAddress& ipaddr) const;
int Broadcast(int opt) const;
void Interrupt() const;
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);
};
}

43
include/Sockets/Uri.hpp Normal file
View File

@@ -0,0 +1,43 @@
#pragma once
#include <string>
#include <Sockets/IPAddress.hpp>
namespace Socket
{
class Auth
{
public:
Auth(std::string const& user, std::string const& host, uint16_t port);
Auth();
std::string const& GetUser() const { return user_;}
std::string const& GetHost() const { return host_;}
uint16_t GetPort() const { return port_;}
IPAddress GetIPAddress() const;
private:
std::string user_;
std::string host_;
uint16_t port_;
};
class Uri
{
public:
Uri(const char* value);
Uri(std::string const& value);
Uri();
std::string const& GetScheme() const { return scheme_;}
Auth const& GetAuthority() const { return authority_;}
std::string const& GetPath() const { return path_;}
std::string const& GetHost() const { return authority_.GetHost();}
uint16_t GetPort() const { return authority_.GetPort();}
void SchemeIs(std::string const& scheme);
void AuthorityIs(std::string const& authority);
private:
std::string scheme_;
Auth authority_;
std::string path_;
};
}

View File

@@ -1,4 +1,4 @@
#include "TcpSocket.hpp"
#include <Sockets/TcpSocket.hpp>
#include <iostream>
#include <exception>
@@ -14,26 +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;
return -1;
}
// 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;
}

92
src/Sockets/IPAddress.cpp Normal file
View File

@@ -0,0 +1,92 @@
#include "Sockets/IPAddress.hpp"
#include <Sockets/Exceptions.hpp>
namespace Socket
{
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];
}
IPAddress::IPAddress(uint32_t ipaddr, uint16_t portno)
{
*(uint32_t*)octets.data() = htonl(ipaddr);
port = portno;
}
IPAddress::IPAddress(const std::string& ipaddr, uint16_t portno)
{
int ret = ::inet_pton(AF_INET, ipaddr.c_str(), (uint32_t*)octets.data());
if (ret > 0) {
port = portno;
} else {
throw SocketException(strerror(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);
}
IPAddress::operator 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();
}
IPAddress IPAddress::Resolve(const std::string &uri, uint16_t port) {
struct hostent *he = gethostbyname(uri.c_str());
if (he == nullptr)
{
throw BadUriException("");
}
char *ip = inet_ntoa(*(struct in_addr*)he->h_addr_list[0]);
return IPAddress(std::string(ip), port);
}
}

View File

@@ -1,5 +1,5 @@
#include "Exceptions.hpp"
#include "TcpSocket.hpp"
#include <Sockets/Exceptions.hpp>
#include <Sockets/TcpSocket.hpp>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
@@ -40,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,12 +53,12 @@ namespace Socket {
TcpSocket::~TcpSocket()
{
if (!mClosed)
close();
if (mSockCreated && !mClosed)
Close();
freeaddrinfo(mInfo);
}
void TcpSocket::bind(int port)
void TcpSocket::Bind(uint16_t port)
{
if (mBound && mConnected)
throw SocketBindingException("Already bound!");
@@ -92,7 +89,21 @@ namespace Socket {
throw SocketBindingException("Can't bind to port");
}
void TcpSocket::connect(std::string address, int port)
void TcpSocket::BindAny() {
Bind(INPORT_ANY);
}
void TcpSocket::BindAny(uint16_t& port)
{
this->Bind(INPORT_ANY);
//portno = port;
}
void TcpSocket::Connect(const IPAddress &ipaddr) {
Connect(ipaddr.addr_string(), ipaddr.port);
}
void TcpSocket::Connect(std::string address, uint16_t port)
{
if (mConnected)
throw SocketConnectException("Already Connected!");
@@ -119,14 +130,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
@@ -136,15 +147,12 @@ 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, (unsigned int*)0))==-1) {
//if ((newSock = ::accept(mSock, (sockaddr*)&address.s, &addressSize)) == -1) {
DEBUG(strerror(errno));
throw SocketAcceptException(strerror(errno));
}
DEBUG("1 client accepted");
@@ -161,7 +169,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;
@@ -180,7 +188,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)
@@ -191,7 +199,7 @@ namespace Socket {
return true;
}
void TcpSocket::close()
void TcpSocket::Close()
{
if (::close(mSock) == -1)
throw SocketCloseException(strerror(errno));

View File

137
src/Sockets/UdpSocket.cpp Normal file
View File

@@ -0,0 +1,137 @@
#include "Sockets/UdpSocket.hpp"
namespace Socket
{
void UdpSocket::Open()
{
this->Close();
sock = (int)::socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (this->IsClosed()) {
throw SocketCreationException(strerror(errno));
}
}
int UdpSocket::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);
#endif
if (ret < 0) {
throw SocketCloseException(strerror(errno));
}
sock = -1;
}
return 0;
}
void UdpSocket::Bind(const IPAddress& ipAddress)
{
self_addr = ipAddress;
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 SocketException(strerror(errno));
}
ret = ::bind(sock, (sockaddr_t*)&self_addr, self_addr_len);
if (ret < 0) {
throw SocketBindingException(strerror(errno));
}
ret = ::getsockname(sock, (sockaddr_t*)&self_addr, &self_addr_len);
if (ret < 0) {
throw SocketException(strerror(errno));
}
}
void UdpSocket::Bind(uint16_t portno)
{
auto ipaddr = IPAddress::Any(portno);
this->Bind(ipaddr);
}
void UdpSocket::BindAny()
{
this->Bind(INPORT_ANY);
}
void UdpSocket::BindAny(uint16_t& portno)
{
this->Bind(INPORT_ANY);
//portno = IPAddress{self_addr}.port;
}
void UdpSocket::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));
}
}
void UdpSocket::Connect(uint16_t portno)
{
auto ipaddr = IPAddress::Loopback(portno);
this->Connect(ipaddr);
}
template <typename T>
int UdpSocket::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>
int UdpSocket::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 UdpSocket::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;
}
void UdpSocket::Interrupt() const
{
uint16_t portno = IPAddress{self_addr}.port;
auto ipaddr = IPAddress::Loopback(portno);
this->Send(msg_t{}, ipaddr);
}
}

17
src/Sockets/Uri.cpp Normal file
View File

@@ -0,0 +1,17 @@
#include <Sockets/Uri.hpp>
namespace Socket {
Uri::Uri()
{
}
Uri::Uri(const char* value)
{
}
Uri::Uri(std::string const& value)
{
}
}

8
tests/CMakeLists.txt Normal file
View File

@@ -0,0 +1,8 @@
file(GLOB_RECURSE TEST_SRCS "*.cpp")
add_executable(Test ${TEST_SRCS})
target_link_libraries(Test PUBLIC Sockets)
find_package(GTest REQUIRED)
target_link_libraries(Test PRIVATE GTest::gtest)
include_directories("include")
include_directories("../include/Sockets")
add_test(NAME "SocketTests" COMMAND SocketTests)

3
tests/ExceptionTests.cpp Normal file
View File

@@ -0,0 +1,3 @@
#include <gtest/gtest.h>
#include <Sockets/Exceptions.hpp>

61
tests/IPAddressTests.cpp Normal file
View File

@@ -0,0 +1,61 @@
#include <gtest/gtest.h>
#include <Sockets/IPAddress.hpp>
#include <Sockets/Exceptions.hpp>
TEST(IPAddressTest, IPAddress_Constructor_Default)
{
Socket::IPAddress addr;
ASSERT_TRUE(addr.addr_string() == "0.0.0.0");
ASSERT_TRUE(addr.port == 0);
}
TEST(IPAddressTest, IPAddress_Constructor_String)
{
Socket::IPAddress addr("127.0.0.1", 4444);
ASSERT_TRUE(addr.addr_string() == "127.0.0.1");
ASSERT_TRUE(addr.port == 4444);
}
TEST(IPAddressTest, IPAddress_Constructor_Octets)
{
Socket::IPAddress addr(255, 0, 255, 0, 42069);
ASSERT_TRUE(addr.octets[0]==255);
ASSERT_TRUE(addr.octets[1]==0);
ASSERT_TRUE(addr.octets[2]==255);
ASSERT_TRUE(addr.octets[3]==0);
ASSERT_TRUE(addr.port == 42069);
}
TEST(IPAddressTest, IPAddress_Constructor_Invalid_Address)
{
ASSERT_THROW({
Socket::IPAddress addr("127.0.0,", 4444);
}, std::exception);
ASSERT_THROW({
Socket::IPAddress addr("www.google.com", 4444);
}, std::exception);
}
TEST(IPAddressTest, IPAddress_Resolve)
{
Socket::IPAddress addr = Socket::IPAddress::Resolve("www.google.com", 80);
}
TEST(IPAddressTest, IPAddress_Resolve_Invalid)
{
ASSERT_THROW({
Socket::IPAddress addr = Socket::IPAddress::Resolve("GOOGLEPLS", 80);
}, std::exception);
}
TEST(IPAddressTest, IPAddress_Operator_Equality)
{
Socket::IPAddress a("127.0.0.1", 42069);
Socket::IPAddress b("127.0.0.1", 42069);
Socket::IPAddress c("255.255.255.255", 666);
ASSERT_TRUE(a == b);
ASSERT_TRUE(a != c);
}

7
tests/UriTests.cpp Normal file
View File

@@ -0,0 +1,7 @@
#include <gtest/gtest.h>
#include <Sockets/Uri.hpp>
TEST(UriTest, Uri_Constructor_Default)
{
}

6
tests/tests.cpp Normal file
View File

@@ -0,0 +1,6 @@
#include <gtest/gtest.h>
GTEST_API_ int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}