Skip to content

Commit 94dcb56

Browse files
committed
init project
1 parent 1c591e7 commit 94dcb56

File tree

14 files changed

+426
-0
lines changed

14 files changed

+426
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
build/
2+
13
# Prerequisites
24
*.d
35

CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project(mstack LANGUAGES CXX)
4+
5+
set(CMAKE_CXX_STANDARD 17)
6+
7+
# find_package (glog REQUIRED)
8+
add_compile_options(-g)
9+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
10+
11+
12+
# aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src SRC)
13+
14+
add_executable(mstack main.cpp)
15+
16+
target_link_libraries (mstack glog gflags)

include/arp.hpp

Whitespace-only changes.

include/device.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class device {
2+
public:
3+
virtual void receive()
4+
}

include/file_desc.hpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <optional>
5+
6+
#include <glog/logging.h>
7+
8+
#include <fcntl.h>
9+
#include <sys/ioctl.h>
10+
#include <sys/stat.h>
11+
#include <sys/types.h>
12+
#include <unistd.h>
13+
14+
namespace mstack {
15+
16+
class file_desc {
17+
private:
18+
int _fd;
19+
file_desc(int fd)
20+
: _fd(fd)
21+
{
22+
}
23+
24+
public:
25+
static constexpr int RDWR = O_RDWR;
26+
static constexpr int NONBLOCK = O_NONBLOCK;
27+
28+
public:
29+
file_desc() = delete;
30+
~file_desc()
31+
{
32+
if (_fd != -1)
33+
::close(_fd);
34+
}
35+
36+
file_desc(const file_desc&) = delete;
37+
file_desc(file_desc&& x)
38+
: _fd(x._fd)
39+
{
40+
x._fd = -1;
41+
}
42+
43+
file_desc& operator=(const file_desc&) = delete;
44+
file_desc& operator=(file_desc&& x)
45+
{
46+
if (this != &x) {
47+
auto tmp = file_desc(x._fd);
48+
std::swap(_fd, tmp._fd);
49+
}
50+
return *this;
51+
}
52+
53+
public:
54+
static std::optional<file_desc> from_fd(int fd)
55+
{
56+
if (fd == -1)
57+
return std::nullopt;
58+
return file_desc(fd);
59+
}
60+
static std::optional<file_desc> open(std::string name, int flags)
61+
{
62+
int fd = ::open(name.c_str(), flags);
63+
if (fd == -1) {
64+
LOG(ERROR) << "[OPEN FAIL] " << name << " " << strerror(errno);
65+
}
66+
return from_fd(fd);
67+
}
68+
69+
public:
70+
template <class X>
71+
int ioctl(int request, X& data)
72+
{
73+
int err = ::ioctl(_fd, request, &data);
74+
if (err < 0) {
75+
LOG(ERROR) << "[IOCTL FAIL] " << strerror(errno);
76+
}
77+
return err;
78+
}
79+
80+
template <class X>
81+
int ioctl(int request, X&& data)
82+
{
83+
return ioctl(_fd, request, data);
84+
}
85+
};
86+
}; // namespace mstack

include/icmp.hpp

Whitespace-only changes.

include/ipv4.hpp

Whitespace-only changes.

include/layer.hpp

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#include <functional>
2+
#include <glog/logging.h>
3+
#include <optional>
4+
#include <vector>
5+
6+
namespace mstack {
7+
8+
class raw_packet {
9+
raw_proto proto;
10+
buffer payload;
11+
};
12+
13+
class l2_packet {
14+
mac_addr remote_mac_addr;
15+
l2_proto proto;
16+
buffer payload;
17+
};
18+
19+
class l3_packet {
20+
ip_addr remote_ip_addr;
21+
l3_proto proto;
22+
buffer payload
23+
};
24+
25+
class l4_packet {
26+
ip_addr remote_ip_addr;
27+
port remote_port;
28+
l4_proto proto;
29+
buffer payload;
30+
};
31+
32+
template <typname CurrentPacketType>
33+
class base_hook_funcs {
34+
public:
35+
virtual std::optional<protocol_interface>
36+
hook_register(std::optional<protocol_interface> protocol)
37+
{
38+
return protocol
39+
}
40+
virtual std::optional<CurrentPacketType>
41+
hook_dispatch(std::optional<CurrentPacketType> packet)
42+
{
43+
return packet;
44+
}
45+
virtual std::optional<CurrentPacketType>
46+
hook_gather(std::optional<CurrentPacketType> packet)
47+
{
48+
return packet;
49+
}
50+
};
51+
52+
template <typename OtherPacketType, typename CurrentPacketType,
53+
typename ChildType,
54+
typename HookFuncs = base_hook_funcs<CurrentPacketType>>
55+
class layer {
56+
private:
57+
using packet_provider_type = std::function<optional<OtherPacketType>()>;
58+
using packet_receiver_type = std::function<optional<(CurrentPacketType)>>;
59+
unordered_map<int, packet_receiver_type> _protocols;
60+
vector<packet_provider_type> _packet_providers;
61+
circle_buffer<CurrentPacketType> packet_queue;
62+
HookFuncs hook_funcs;
63+
64+
public:
65+
void register_packet_privder(protocol_interface<CurrentPacketType> protocol)
66+
{
67+
68+
std::optional<protocol_interface<CurrentPacketType>> protocol = this->hook_funcs.hook_register(protocol);
69+
if (!protocol)
70+
return;
71+
72+
this->_packet_provider.push_back((*protocols).gather_packet);
73+
_protocols[protocols->proto] = (*protocols).receive;
74+
}
75+
76+
void receive(OtherPacketType packet)
77+
{
78+
79+
std::optional<int> proto = get_proto<CurrentPacketType>(packet);
80+
81+
if (!proto) {
82+
LOG(INFO) << "[UNKNOWN PACKET]" return;
83+
}
84+
if (this->_protocols.find(proto) == this->_protocols.end()) {
85+
LOG(INFO) << "[UNKNOWN PROTOCOL]";
86+
return;
87+
}
88+
std::optional<CurrentPacketType> packet = make_packet<CurrentPacketType>(packet);
89+
90+
std::optional<CurrentPacketType> packet = this->hook_funcs.hook_dispatch(packet);
91+
92+
this->_dispatch(packet);
93+
}
94+
95+
void _dispatch(std::optional<CurrentPacketType> packet)
96+
{
97+
98+
if (!packet)
99+
return;
100+
101+
std::optional<int> proto = get_proto<CurrentPacketType>(*packet);
102+
103+
CHECK(this->_protocols.find(proto) != this->_protocols.end())
104+
<< "[UNKNOW PROTCOL]";
105+
106+
this->_protocols[proto].receive(packet);
107+
}
108+
109+
std::optional<CurrentPacketType> gather_packet()
110+
{
111+
112+
if (this->packet_queue.empty()) {
113+
for (auto packet_provider : this->__packet_providers) {
114+
std::optional<CurrentPacketType> packet = packet_provider();
115+
if (!packet) {
116+
packet_queue.push_back(*packet);
117+
}
118+
}
119+
}
120+
121+
if (this->packet_queue.empty()) {
122+
return std::nullopt;
123+
}
124+
125+
std::optional<CurrentPacketType> packet = packet_queue.front();
126+
packet_queue.pop_front();
127+
128+
packet = this->hook_funcs.hook_gather(packet);
129+
130+
return packet;
131+
}
132+
};
133+
134+
class l2_hook : public base_hook_funcs<l2_packet> {
135+
struct arpv4_request {
136+
};
137+
struct arpv4_cache {
138+
};
139+
};
140+
141+
class l2_layer<raw_packet, l2_packet, l2_layer, l2_hook> {
142+
};
143+
144+
class l3_hook : public base_hook_funcs<l3_packet> {
145+
public:
146+
struct ipv4_reassemble {
147+
};
148+
};
149+
150+
class l3_layer<l2_packet, l3_packet, l3_layer, l3_hook> {
151+
};
152+
153+
class l4_layer<l3_packet, l4_packet, l4_layer> {
154+
};
155+
156+
}; // namespace mstack

include/protocol.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
namespace mstack {
3+
4+
template <CurrentPacketType>
5+
class protocol_interface {
6+
public:
7+
circle_buffer<CurrentPacketType> packet_queue;
8+
9+
virtual void receive(CurrentPacketType packet) = 0;
10+
11+
virtual CurrentPacketType gather_packet()
12+
{
13+
if (this->packet_queue.empty()) {
14+
return std::nullopt;
15+
}
16+
auto packet = packet_queue.front();
17+
packet_queue.pop_front();
18+
return packet;
19+
}
20+
}
21+
22+
template <CurrentPacketType>
23+
std::optional<int> get_proto(CurrentPacketType packet)
24+
{
25+
return;
26+
}
27+
28+
template <CurrentPacketType, OtherPacketType>
29+
std::optional<CurrentPacketType> make_packet(OtherPacketType packet)
30+
{
31+
return;
32+
}
33+
34+
} // namespace mstack
35+
}
36+
;

include/tcp.hpp

Whitespace-only changes.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy