-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
phys.cpp
166 lines (133 loc) · 4.52 KB
/
phys.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// (C) 2020-2024 by folkert van heusden <[email protected]>, released under Apache License v2.0
#include <algorithm>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/sockios.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "buffer_out.h"
#include "hash.h"
#include "log.h"
#include "packet.h"
#include "phys.h"
#include "str.h"
phys::phys(const size_t dev_index, stats *const s, const std::string & name, router *const r) :
r(r),
dev_index(dev_index), // used for SNMP
name(name)
{
// 1.3.6.1.4.1.57850.1.8: physical device
phys_recv_frame = s->register_stat("phys_recv_frame", "1.3.6.1.4.1.57850.1.8.1");
phys_invl_frame = s->register_stat("phys_invl_frame", "1.3.6.1.4.1.57850.1.8.2");
phys_ign_frame = s->register_stat("phys_ign_frame", "1.3.6.1.4.1.57850.1.8.3");
phys_ifInOctets = s->register_stat("phys_ifInOctets", myformat("1.3.6.1.2.1.2.2.1.10.%zu", dev_index), snmp_integer::si_counter32);
phys_ifHCInOctets = s->register_stat("phys_ifHCInOctets", myformat("1.3.6.1.2.1.31.1.1.1.6.%zu", dev_index), snmp_integer::si_counter64);
phys_ifInUcastPkts = s->register_stat("phys_ifInUcastPkts", myformat("1.3.6.1.2.1.2.2.1.11.%zu", dev_index), snmp_integer::si_counter32);
phys_ifOutOctets = s->register_stat("phys_ifOutOctets", myformat("1.3.6.1.2.1.2.2.1.16.%zu", dev_index), snmp_integer::si_counter32);
phys_ifHCOutOctets = s->register_stat("phys_ifHCOutOctets", myformat("1.3.6.1.2.1.31.1.1.1.10.%zu", dev_index), snmp_integer::si_counter64);
phys_ifOutUcastPkts = s->register_stat("phys_ifOutUcastPkts", myformat("1.3.6.1.2.1.2.2.1.17.%zu", dev_index), snmp_integer::si_counter32);
// MTU size for Ethernet
mtu_size = 1500;
}
phys::~phys()
{
stop_pcap();
stop_flag = true;
if (th) {
th->join();
delete th;
}
}
void phys::start()
{
}
void phys::ask_to_stop()
{
stop_flag = true;
}
timespec phys::gen_packet_timestamp(const int fd)
{
timespec ts { 0, 0 };
if (ioctl(fd, SIOCGSTAMPNS_OLD, &ts) == -1) {
if (SIOCGSTAMPNS_OLD_error_emitted == false) {
CDOLOG(ll_info, "[phys]", "ioctl(SIOCGSTAMPNS_OLD) failed: %s\n", strerror(errno));
SIOCGSTAMPNS_OLD_error_emitted = true;
}
if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
CDOLOG(ll_warning, "[phys]", "clock_gettime failed: %s", strerror(errno));
}
return ts;
}
void phys::start_pcap(const std::string & pcap_file, const bool in, const bool out, const uint32_t link_type)
{
std::unique_lock<std::mutex> lck(pcap_lock);
if (!ph)
ph = pcap_open_dead_with_tstamp_precision(link_type, 65535, PCAP_TSTAMP_PRECISION_MICRO);
pcap_write_incoming = in;
pcap_write_outgoing = out;
if (pdh)
CDOLOG(ll_error, "[phys]", "pcap already running\n");
else if (in || out) {
std::string temp = myformat(pcap_file.c_str(), md5hex(myformat("%s-%zu", name.c_str(), dev_index)).substr(0, 4).c_str());
pdh = pcap_dump_open(ph, temp.c_str());
if (!pdh)
error_exit(false, "pcap_dump_open(%s) failed: %s", temp.c_str(), pcap_geterr(ph));
pcap_dump_flush(pdh);
}
}
void phys::stop_pcap()
{
std::unique_lock<std::mutex> lck(pcap_lock);
if (pdh) {
pcap_dump_flush(pdh);
pcap_dump_close(pdh);
}
}
void phys::pcap_write_packet_incoming(const timespec & ts, const uint8_t *const data, const size_t n)
{
std::unique_lock<std::mutex> lck(pcap_lock);
if (pcap_write_incoming) {
pcap_pkthdr header { 0 };
header.ts.tv_sec = ts.tv_sec;
header.ts.tv_usec = ts.tv_nsec / 1000;
header.len = header.caplen = n;
pcap_dump(reinterpret_cast<u_char *>(pdh), &header, data);
}
}
void phys::pcap_write_packet_outgoing(const timespec & ts, const uint8_t *const data, const size_t n)
{
std::unique_lock<std::mutex> lck(pcap_lock);
if (pcap_write_outgoing) {
pcap_pkthdr header { 0 };
header.ts.tv_sec = ts.tv_sec;
header.ts.tv_usec = ts.tv_nsec / 1000;
header.len = header.caplen = n;
pcap_dump(reinterpret_cast<u_char *>(pdh), &header, data);
}
}
void phys::register_protocol(const uint16_t ether_type, network_layer *const p)
{
prot_map.insert({ ether_type, p });
p->register_default_phys(this);
}
network_layer *phys::get_protocol(const uint16_t protocol)
{
auto it = prot_map.find(protocol);
if (it == prot_map.end())
return nullptr;
return it->second;
}
bool phys::transmit_packet(const any_addr & dst_mac, const any_addr & src_mac, const uint16_t ether_type, const uint8_t *payload, const size_t pl_size)
{
return false;
}
void phys::operator()()
{
CDOLOG(ll_info, "[phys]", "thread stopped\n");
}