/
Injector.cpp
129 lines (111 loc) · 2.66 KB
/
Injector.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
#include "Injector.h"
#include <unistd.h>
#include <dirent.h>
#include <sstream>
constexpr char *PATHPREFIX = (char *)"./static-data";
bool has_ending (std::string const &fullString, std::string const &ending) {
if (fullString.length() >= ending.length()) {
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
} else {
return false;
}
}
Injector::Injector(MessageSink *ms) : ms(ms)
{
DIR *d;
struct dirent *dir;
d = opendir(PATHPREFIX);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
// we looking for files ending in .pcapng
if (strlen(dir->d_name) < 8)
{
continue;
}
std::string name(dir->d_name);
if (!has_ending(name, ".pcapng"))
{
continue;
}
std::cout << "Injector found: " << name << std::endl;
files.push_back(name);
}
closedir(d);
}
}
void Injector::inject(uint32_t id)
{
if (id > files.size() - 1)
{
return;
}
if (injecting)
{
return;
}
injecting = true;
thread_injecting = true;
injector_thread = new std::thread(&Injector::iterate_pcap, this, id);
injector_thread->detach();
}
void Injector::stop_injecting()
{
thread_injecting = false;
}
void Injector::pcap_loop_callback(u_char *userData, const struct pcap_pkthdr *pkthdr, const u_char *packet)
{
auto args = (pcap_loop_args_t *)userData;
if (!args->i->thread_injecting)
{
pcap_breakloop(args->fp);
}
auto buf = (uint8_t *)calloc(1, pkthdr->len);
memcpy(buf, packet, pkthdr->len);
if (args->last.tv_sec != 0)
{
// not-first iteration, sleep for the diff
struct timeval diff = {};
timersub(&pkthdr->ts, &args->last, &diff);
auto sleeptime = (uint32_t)((diff.tv_sec * 1'000'000 + diff.tv_usec) / args->i->get_time_factor());
if (sleeptime > args->i->get_max_usleep_time())
sleeptime = args->i->get_max_usleep_time();
usleep(sleeptime);
}
auto *e = (ethernet_t *)buf;
if (ntohs(e->type) != ETHERTYPE_GEONET)
{
return;
}
args->last = pkthdr->ts;
args->ms->add_msg(buf, pkthdr->len);
args->i->inc_counter();
}
void Injector::iterate_pcap(uint32_t id)
{
pcap_t *fp;
char errbuf[PCAP_ERRBUF_SIZE];
std::stringstream ss;
ss << PATHPREFIX << "/" << files[id];
fp = pcap_open_offline(ss.str().c_str(), errbuf);
if (fp == nullptr)
{
std::cout << "pcap_open_offline error: " << errbuf << std::endl;
inject_msg_counter = 0;
injecting = false;
return;
}
pcap_loop_args_t loop_args = { ms, {0, 0}, this, fp };
if (pcap_loop(fp, 0, pcap_loop_callback, (u_char *)&loop_args) < 0)
{
std::cout << "pcap_loop error: " << errbuf << std::endl;
inject_msg_counter = 0;
injecting = false;
pcap_close(fp);
return;
}
pcap_close(fp);
inject_msg_counter = 0;
injecting = false;
}