Skip to content

Commit a72f287

Browse files
committed
Initial commit
0 parents  commit a72f287

File tree

11 files changed

+685
-0
lines changed

11 files changed

+685
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
build/
2+
3+
compile_commands.json
4+
5+
*.tmp
6+
*.gch

CMakeLists.txt

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
##
2+
## Project CppSockets, 2022
3+
##
4+
## Author Francois Michaut
5+
##
6+
## Started on Sun Aug 28 19:26:51 2022 Francois Michaut
7+
## Last update Thu Sep 15 14:08:03 2022 Francois Michaut
8+
##
9+
## CMakeLists.txt : CMake to build the CppSockets library
10+
##
11+
12+
cmake_minimum_required (VERSION 3.15)
13+
set(CMAKE_CXX_STANDARD 17)
14+
set(CMAKE_CXX_STANDARD_REQUIRED True)
15+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
16+
17+
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
18+
19+
project(LibCppSockets VERSION 0.1.0 LANGUAGES C CXX)
20+
configure_file(include/CppSockets/Version.hpp.in CppSockets/Version.hpp)
21+
22+
include_directories(${CMAKE_CURRENT_BINARY_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/include")
23+
24+
add_library(cppsockets
25+
source/Address.cpp
26+
source/IPv4.cpp
27+
source/Socket.cpp
28+
source/TlsSocket.cpp
29+
)
30+
31+
add_custom_target(test
32+
COMMAND ${CMAKE_COMMAND}
33+
-B "${CMAKE_CURRENT_BINARY_DIR}/tests"
34+
-S "${CMAKE_CURRENT_SOURCE_DIR}/tests"
35+
-G ${CMAKE_GENERATOR}
36+
COMMAND ${CMAKE_COMMAND} -E cmake_echo_color
37+
--switch=$(COLOR) --cyan "Building tests..."
38+
COMMAND ${CMAKE_COMMAND}
39+
--build "${CMAKE_CURRENT_BINARY_DIR}/tests"
40+
COMMAND ${CMAKE_MAKE_PROGRAM}
41+
-C "${CMAKE_CURRENT_BINARY_DIR}/tests" test
42+
)

include/CppSockets/Address.hpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
** Project CppSockets, 2022
3+
**
4+
** Author Francois Michaut
5+
**
6+
** Started on Sun Feb 13 17:09:05 2022 Francois Michaut
7+
** Last update Mon Aug 29 20:46:03 2022 Francois Michaut
8+
**
9+
** Address.hpp : Interface to represent network addresses
10+
*/
11+
12+
#pragma once
13+
14+
#include <string>
15+
#include <type_traits>
16+
17+
namespace CppSockets {
18+
class IAddress {
19+
public:
20+
[[nodiscard]]
21+
virtual std::uint32_t getAddress() const = 0;
22+
[[nodiscard]]
23+
virtual int getFamily() const = 0;
24+
[[nodiscard]]
25+
virtual const std::string &toString() const = 0;
26+
};
27+
28+
class IEndpoint {
29+
public:
30+
[[nodiscard]]
31+
virtual int getPort() const = 0;
32+
[[nodiscard]]
33+
virtual const IAddress &getAddr() const = 0;
34+
[[nodiscard]]
35+
virtual const std::string &toString() const = 0;
36+
37+
protected:
38+
[[nodiscard]]
39+
std::string makeString() const;
40+
};
41+
42+
template <class T>
43+
class Endpoint : public IEndpoint {
44+
static_assert(std::is_base_of<IAddress, T>::value,
45+
"Endpoint address must derive from IAddress"
46+
);
47+
public:
48+
Endpoint(T addr, int port) :
49+
addr(std::move(addr)), port(port), str(makeString())
50+
{};
51+
52+
[[nodiscard]]
53+
int getPort() const override {
54+
return port;
55+
}
56+
57+
[[nodiscard]]
58+
const T &getAddr() const override {
59+
return addr;
60+
}
61+
62+
[[nodiscard]]
63+
const std::string &toString() const override {
64+
return str;
65+
}
66+
private:
67+
int port;
68+
T addr;
69+
std::string str;
70+
};
71+
}

include/CppSockets/IPv4.hpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
** Project CppSockets, 2022
3+
**
4+
** Author Francois Michaut
5+
**
6+
** Started on Sun Feb 13 17:05:02 2022 Francois Michaut
7+
** Last update Wed Sep 14 22:28:32 2022 Francois Michaut
8+
**
9+
** IPv4.hpp : Class used to represent and manipulate IPv4 addresses
10+
*/
11+
12+
#pragma once
13+
14+
#include "CppSockets/Address.hpp"
15+
16+
namespace CppSockets {
17+
class IPv4 : public IAddress {
18+
public:
19+
explicit IPv4(std::uint32_t addr);
20+
explicit IPv4(const char *addr);
21+
22+
[[nodiscard]]
23+
std::uint32_t getAddress() const override;
24+
25+
[[nodiscard]]
26+
int getFamily() const override;
27+
28+
[[nodiscard]]
29+
const std::string &toString() const override;
30+
31+
private:
32+
std::uint32_t addr;
33+
std::string str;
34+
};
35+
}

include/CppSockets/Socket.hpp

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
** Project CppSockets, 2022
3+
**
4+
** Author Francois Michaut
5+
**
6+
** Started on Sat Jan 15 01:17:42 2022 Francois Michaut
7+
** Last update Wed Sep 14 22:18:29 2022 Francois Michaut
8+
**
9+
** Socket.hpp : Portable C++ socket class
10+
*/
11+
12+
#pragma once
13+
14+
#ifdef _WIN32
15+
#define NOMINMAX
16+
#include <winsock2.h>
17+
using RawSocketType=SOCKET;
18+
using socklen_t=int;
19+
#else
20+
#include <sys/socket.h>
21+
using RawSocketType=int;
22+
#endif
23+
24+
#include <memory>
25+
26+
#include "CppSockets/Address.hpp"
27+
28+
namespace CppSockets {
29+
class Socket {
30+
public:
31+
Socket(int domain, int type, int protocol);
32+
~Socket();
33+
34+
Socket(const Socket &other) = delete;
35+
Socket(Socket &&other) noexcept;
36+
Socket &operator=(const Socket &other) = delete;
37+
Socket &operator=(Socket &&other) noexcept;
38+
39+
std::string read(std::size_t len = -1);
40+
std::size_t read(char *buff, std::size_t size);
41+
std::size_t write(const std::string &buff);
42+
std::size_t write(const char *buff, std::size_t len);
43+
44+
int getsockopt(int level, int optname, void *optval, socklen_t *optlen);
45+
int setsockopt(int level, int optname, const void *optval, socklen_t optlen);
46+
47+
int connect(const IEndpoint &endpoint);
48+
int connect(const std::string &addr, int port);
49+
50+
int bind(std::uint32_t addr, int port); // TODO remove ?
51+
int bind(const IEndpoint &endpoint);
52+
int bind(const std::string &addr, int port);
53+
int listen(int backlog);
54+
std::shared_ptr<Socket> accept(void *addr_out);
55+
56+
void set_blocking(bool val);
57+
58+
[[nodiscard]]
59+
RawSocketType get_fd() const; // TODO check if windows SOCKET can be
60+
// converted to int
61+
[[nodiscard]]
62+
int get_type() const;
63+
[[nodiscard]]
64+
int get_domain() const;
65+
[[nodiscard]]
66+
int get_protocol() const;
67+
68+
[[nodiscard]]
69+
bool connected() const;
70+
private:
71+
Socket(int domain, int type, int protocol, int sockfd);
72+
73+
static void init();
74+
75+
static int getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen);
76+
static int get_errno();
77+
static char *strerror(int err);
78+
static char *strerror();
79+
80+
int domain;
81+
int type;
82+
int protocol;
83+
RawSocketType sockfd;
84+
bool is_connected = false;
85+
};
86+
}

include/CppSockets/Version.hpp.in

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
** Project CppSockets, 2022
3+
**
4+
** Author Francois Michaut
5+
**
6+
** Started on Sun Aug 28 19:32:30 2022 Francois Michaut
7+
** Last update Wed Sep 14 22:26:40 2022 Francois Michaut
8+
**
9+
** Version.hpp.in : Library version
10+
*/
11+
12+
#pragma once
13+
14+
#define LIB_CPP_SOCKET_MAJOR @LibCppSocket_VERSION_MAJOR@
15+
#define LIB_CPP_SOCKET_MINOR @LibCppSocket_VERSION_MINOR@
16+
#define LIB_CPP_SOCKET_PATCH @LibCppSocket_VERSION_PATCH@

source/Address.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
** Project CppSocket, 2022
3+
**
4+
** Author Francois Michaut
5+
**
6+
** Started on Sun Feb 13 22:03:32 2022 Francois Michaut
7+
** Last update Mon Aug 29 20:45:51 2022 Francois Michaut
8+
**
9+
** Address.cpp : Implementation of generic Address classes & functions
10+
*/
11+
12+
#include "CppSockets/Address.hpp"
13+
14+
namespace CppSockets {
15+
std::string IEndpoint::makeString() const {
16+
return this->getAddr().toString() + ":" + std::to_string(this->getPort());
17+
}
18+
}

source/IPv4.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
** Project CppSocket, 2022
3+
**
4+
** Author Francois Michaut
5+
**
6+
** Started on Sun Feb 13 18:52:28 2022 Francois Michaut
7+
** Last update Mon Aug 29 20:45:54 2022 Francois Michaut
8+
**
9+
** IPv4.cpp : Implementation of IPv4 class
10+
*/
11+
12+
#include "CppSockets/IPv4.hpp"
13+
#include "CppSockets/Socket.hpp"
14+
15+
#include <arpa/inet.h>
16+
17+
namespace CppSockets {
18+
IPv4::IPv4(std::uint32_t addr) :
19+
addr(htonl(addr))
20+
{
21+
struct in_addr tmp {this->addr};
22+
23+
str = inet_ntoa(tmp);
24+
}
25+
26+
IPv4::IPv4(const char *addr) :
27+
addr(inet_addr(addr))
28+
{}
29+
30+
std::uint32_t IPv4::getAddress() const {
31+
return addr;
32+
}
33+
34+
const std::string &IPv4::toString() const {
35+
return str;
36+
}
37+
38+
int IPv4::getFamily() const {
39+
return AF_INET;
40+
}
41+
}

0 commit comments

Comments
 (0)