Skip to content

Commit a0809be

Browse files
committed
add mac_addr and base packet
1 parent 94dcb56 commit a0809be

File tree

12 files changed

+392
-100
lines changed

12 files changed

+392
-100
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
1313

1414
add_executable(mstack main.cpp)
1515

16-
target_link_libraries (mstack glog gflags)
16+
target_link_libraries (mstack glog gflags fmt)

include/defination.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace mstack {
2+
constexpr static int TUNTAP_DEV = 0x01;
3+
};

include/device.hpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
1-
class device {
2-
public:
3-
virtual void receive()
4-
}
1+
#include "l2_layer.hpp"
2+
#include <functional>
3+
#include <optional>
4+
#include <vector>
5+
namespace mstack {
6+
namespace device {
7+
class device {
8+
private:
9+
using packet_provider_type = std::function<optional<l2_packet>()>;
10+
using packet_receiver_type = std::function<(raw_packet)>> ;
11+
std::optional<packet_provider_type> provider_func;
12+
std::optional<packet_receiver_type> receiver_func;
13+
void register_provider_func(packet_provider_type func)
14+
{
15+
provider_func = func;
16+
}
17+
void register_receiver_func(packet_receiver_type func)
18+
{
19+
receiver_func = func;
20+
}
21+
22+
public:
23+
device() = delete;
24+
~device() = delete;
25+
device(const device&) = delete;
26+
device(device&&) = delete;
27+
device& operator=(const device&) = delete;
28+
device& operator=(device&&) = delete;
29+
virutal void run() = 0;
30+
};
31+
};
32+
};

include/ethernet.hpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "utils.hpp"
2+
#include <algorithm>
3+
#include <array>
4+
#include <string>
5+
#include <utility>
6+
7+
namespace mstack {
8+
struct mac_addr {
9+
private:
10+
std::array<uint8_t, 6> mac;
11+
12+
public:
13+
mac_addr() = default;
14+
~mac_addr() = default;
15+
mac_addr(const mac_addr& other)
16+
{
17+
std::copy(std::begin(other.mac), std::end(other.mac), std::begin(mac));
18+
}
19+
mac_addr(mac_addr&& other)
20+
{
21+
std::swap(mac, other.mac);
22+
}
23+
mac_addr& operator=(const mac_addr& other)
24+
{
25+
if (this != &other) {
26+
std::copy(std::begin(other.mac), std::end(other.mac), std::begin(mac));
27+
}
28+
}
29+
30+
mac_addr& operator=(mac_addr&& other)
31+
{
32+
std::swap(mac, other.mac);
33+
}
34+
35+
mac_addr(std::array<uint8_t, 6> mac_addr)
36+
: mac(mac_addr){};
37+
38+
mac_addr(std::string mac_addr)
39+
{
40+
for (int i = 0; i < 17; i += 3) {
41+
std::string tmp = mac_addr.substr(i, 2);
42+
uint8_t tmp_int = std::stoi(tmp, 0, 16);
43+
mac[i / 3] = tmp_int;
44+
}
45+
};
46+
47+
friend std::ostream& operator<<(std::ostream& out, mac_addr& m)
48+
{
49+
using u = uint32_t;
50+
out << util::format("%02X:%02X:%02X:%02X:%02X:%02X",
51+
u(m.mac[0]), u(m.mac[1]), u(m.mac[2]), u(m.mac[3]), u(m.mac[4]), u(m.mac[5]));
52+
return out;
53+
}
54+
};
55+
};

include/file_desc.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,17 @@ class file_desc {
3434
}
3535

3636
file_desc(const file_desc&) = delete;
37-
file_desc(file_desc&& x)
38-
: _fd(x._fd)
37+
file_desc(file_desc&& other)
38+
: _fd(other._fd)
3939
{
4040
x._fd = -1;
4141
}
4242

4343
file_desc& operator=(const file_desc&) = delete;
44-
file_desc& operator=(file_desc&& x)
44+
file_desc& operator=(file_desc&& other)
4545
{
46-
if (this != &x) {
47-
auto tmp = file_desc(x._fd);
48-
std::swap(_fd, tmp._fd);
46+
if (this != &other) {
47+
std::swap(_fd, other._fd);
4948
}
5049
return *this;
5150
}
@@ -67,6 +66,7 @@ class file_desc {
6766
}
6867

6968
public:
69+
int get_fd() { return fd; }
7070
template <class X>
7171
int ioctl(int request, X& data)
7272
{

include/l2_layer.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "layer.hpp"
2+
namespace mstack {
3+
4+
class l2_hook : public base_hook_funcs<l2_packet> {
5+
struct arpv4_request {
6+
};
7+
struct arpv4_cache {
8+
};
9+
};
10+
11+
class l2_layer : public layer<raw_packet, l2_packet, l2_hook> {
12+
};
13+
};

include/layer.hpp

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
#include <functional>
2-
#include <glog/logging.h>
32
#include <optional>
3+
#include <string>
44
#include <vector>
55

6+
#include "ethernet.hpp"
7+
#include "logger.hpp"
8+
#include "packet.hpp"
9+
#include "utils.hpp"
610
namespace mstack {
711

812
class raw_packet {
9-
raw_proto proto;
10-
buffer payload;
13+
using raw_proto = int;
14+
raw_proto proto; // TUNTAP_DEV
15+
packet payload;
1116
};
1217

1318
class l2_packet {
14-
mac_addr remote_mac_addr;
19+
using l2_proto = int;
20+
std::optional<mac_addr> remote_mac_addr;
1521
l2_proto proto;
16-
buffer payload;
22+
packet payload;
1723
};
1824

19-
class l3_packet {
20-
ip_addr remote_ip_addr;
21-
l3_proto proto;
22-
buffer payload
23-
};
25+
// class l3_packet {
26+
// ip_addr remote_ip_addr;
27+
// l3_proto proto;
28+
// buffer payload
29+
// };
2430

25-
class l4_packet {
26-
ip_addr remote_ip_addr;
27-
port remote_port;
28-
l4_proto proto;
29-
buffer payload;
30-
};
31+
// class l4_packet {
32+
// ip_addr remote_ip_addr;
33+
// port remote_port;
34+
// l4_proto proto;
35+
// buffer payload;
36+
// };
3137

32-
template <typname CurrentPacketType>
38+
template <typename CurrentPacketType>
3339
class base_hook_funcs {
3440
public:
3541
virtual std::optional<protocol_interface>
@@ -50,7 +56,6 @@ class base_hook_funcs {
5056
};
5157

5258
template <typename OtherPacketType, typename CurrentPacketType,
53-
typename ChildType,
5459
typename HookFuncs = base_hook_funcs<CurrentPacketType>>
5560
class layer {
5661
private:
@@ -131,26 +136,16 @@ class layer {
131136
}
132137
};
133138

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-
};
139+
// class l3_hook : public base_hook_funcs<l3_packet> {
140+
// public:
141+
// struct ipv4_reassemble {
142+
// };
143+
// };
149144

150-
class l3_layer<l2_packet, l3_packet, l3_layer, l3_hook> {
151-
};
145+
// class l3_layer<l2_packet, l3_packet, l3_hook> {
146+
// };
152147

153-
class l4_layer<l3_packet, l4_packet, l4_layer> {
154-
};
148+
// class l4_layer<l3_packet, l4_packet> {
149+
// };
155150

156151
}; // namespace mstack

include/logger.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <glog/logging.h>

include/packet.hpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include "logger.hpp"
2+
#include <memory>
3+
4+
namespace mstack {
5+
6+
class packet {
7+
private:
8+
std::vector<unique_ptr<pair<int, char[]>>> _data_stack;
9+
std::unique_ptr<char[]> _raw_data;
10+
int _total_len;
11+
int _head;
12+
int _len;
13+
14+
public:
15+
packet(char* buf, int len)
16+
: _raw_data(std::make_unique<char[]>(len))
17+
, _head(0)
18+
, _len(len)
19+
{
20+
std::copy(buf, buf + len, begin(_raw_data.get()));
21+
}
22+
packet(int len)
23+
: _raw_data(std::make_unique<char[]>(len))
24+
, _head(0)
25+
, _len(len)
26+
{
27+
}
28+
29+
~packet() = default;
30+
packet(packet&) = delete;
31+
packet(packet&& other)
32+
{
33+
*this = std::move(other);
34+
}
35+
packet& operator=(packet&) = delete;
36+
packet& operator=(packet&& other)
37+
{
38+
raw_data = std::move(other.raw_data);
39+
std::swap(head, other.head);
40+
std::swap(len, other.len);
41+
}
42+
43+
public:
44+
void reflush_packet(int len)
45+
{
46+
_data_stack.push_back({ _len, std::move(_raw_data) });
47+
_head = len;
48+
_len = 0;
49+
_raw_data = sdt::make_unique<char[]>(len);
50+
}
51+
52+
void export_data(char* buf, int& len)
53+
{
54+
if (_total_len > len) {
55+
len = 0;
56+
return;
57+
}
58+
int index = 0;
59+
for (int i = 0; i < _data_stack.size(); i++) {
60+
for (int j = 0; j < _data_stack[i].first; j++) {
61+
buf[index++] = data_stack[i].second[j];
62+
}
63+
}
64+
len = index;
65+
}
66+
};
67+
68+
template <typename From, typename To>
69+
To make_packet(From packet)
70+
{
71+
LOG(ERROR) << "UNKNOW PACKET";
72+
return To();
73+
}
74+
75+
template <>
76+
l2_packet make_packet(raw_packet in_packet)
77+
{
78+
}
79+
80+
template <>
81+
raw_packet make_packet(l2_packet, in_packet)
82+
{
83+
}
84+
}

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