-
Notifications
You must be signed in to change notification settings - Fork 35
/
misc.cpp
153 lines (124 loc) · 3.79 KB
/
misc.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
#include <chrono>
#include <fstream>
#include <cstdio>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <boost/bimap.hpp>
#include <boost/container/allocator.hpp>
#include <boost/assign.hpp>
#include "misc.hpp"
uint64_t
nanoseconds()
{
struct timespec l_timespec;
clock_gettime(CLOCK_MONOTONIC, &l_timespec);
return l_timespec.tv_nsec + (l_timespec.tv_sec * 1000000000);
}
std::string
nf_hook_to_string(const nf_hook_t p_hook)
{
switch (p_hook) {
case nf_hook_t::IP_PRE_ROUTING:
return std::string("IP_PRE_ROUTING"); break;
case nf_hook_t::IP_LOCAL_IN:
return std::string("IP_LOCAL_IN"); break;
case nf_hook_t::IP_FORWARD:
return std::string("IP_FORWARD"); break;
case nf_hook_t::IP_LOCAL_OUT:
return std::string("IP_LOCAL_OUT"); break;
case nf_hook_t::IP_POST_ROUTING:
return std::string("IP_POST_ROUTING"); break;
}
return std::string("unknown");
}
typedef boost::bimaps::bimap<
ip_protocol_t,
std::string,
boost::container::allocator<void>
> g_protocol_map_type;
const g_protocol_map_type g_protocol_map =
boost::assign::list_of<g_protocol_map_type::relation>
( ip_protocol_t::HOPOPT, "HOPOPT" )
( ip_protocol_t::ICMP, "ICMP" )
( ip_protocol_t::IGMP, "IGMP" )
( ip_protocol_t::GGP, "GGP" )
( ip_protocol_t::IPV4, "IPV4" )
( ip_protocol_t::ST, "ST" )
( ip_protocol_t::TCP, "TCP" )
( ip_protocol_t::CBT, "CBT" )
( ip_protocol_t::EGP, "EGP" )
( ip_protocol_t::IGP, "IGP" )
( ip_protocol_t::BBNRCCMON, "BBNRCCMON" )
( ip_protocol_t::NVPII, "NVPII" )
( ip_protocol_t::PUP, "PUP" )
( ip_protocol_t::ARGUS, "ARGUS" )
( ip_protocol_t::EMCON, "EMCON" )
( ip_protocol_t::XNET, "XNET" )
( ip_protocol_t::CHAOS, "CHAOS" )
( ip_protocol_t::UDP, "UDP" );
ip_protocol_t
ip_protocol_from_string(const std::string &p_protocol)
{
return g_protocol_map.right.find(p_protocol)->second;
}
std::string
ip_protocol_to_string(const ip_protocol_t p_protocol)
{
return g_protocol_map.left.find(p_protocol)->second;
}
std::string
file_to_string(const std::string &p_path) {
std::ifstream l_stream(p_path);
if (l_stream.is_open() == false) {
throw std::runtime_error("std::ifstream() failed");
}
return std::string(
(std::istreambuf_iterator<char>(l_stream)),
std::istreambuf_iterator<char>()
);
}
void
atomically_write_file(const std::string &p_path, const std::string &p_data)
{
const std::string l_temp_path = p_path + ".tmp";
std::ofstream l_stream(l_temp_path);
if (!l_stream) {
throw std::runtime_error("std::of_stream() failed");
}
l_stream << p_data;
l_stream.close();
if (std::rename(l_temp_path.c_str(), p_path.c_str())) {
throw std::runtime_error("std::rename failed");
}
}
std::string
ipv4_to_string(const uint32_t p_address)
{
char l_buffer[INET_ADDRSTRLEN];
const char *const l_status = inet_ntop(
AF_INET,
&p_address,
l_buffer,
INET_ADDRSTRLEN
);
if (l_status == NULL) {
throw std::runtime_error("inet_ntop() failed");
}
return std::string(l_buffer);
}
std::string
ipv6_to_string(const __uint128_t p_address)
{
char l_buffer[INET6_ADDRSTRLEN];
const char *const l_status = inet_ntop(
AF_INET6,
&p_address,
l_buffer,
INET6_ADDRSTRLEN
);
if (l_status == NULL) {
throw std::runtime_error("inet_ntop() failed");
}
return std::string(l_buffer);
}