include iostream include boost pfr precise hpp include linux netlink i

  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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <iostream>
#include <boost/pfr/precise.hpp>
#include <linux/netlink.h>
#include <netlink/attr.h>
#include <netlink/msg.h>
#include <map>
#include "/home/sargarass/work/es/garda_shared/bastion_libs/src/include/common/nonstd/expected.hpp"
#include "/home/sargarass/work/es/garda_shared/bastion_libs/src/include/common/nonstd/span.hpp"
#include "/home/sargarass/work/es/garda_shared/bastion_libs/src/include/common/nonstd/byte.hpp"
using nonstd::expected;
using nonstd::make_unexpected;
using nonstd::byte;
using nonstd::span;
enum class NetlinkMessageType : int {
unspec = NLA_UNSPEC, /**< Unspecified type, binary data chunk */
uint8 = NLA_U8, /**< 8 bit integer */
uint16 = NLA_U16, /**< 16 bit integer */
uint32 = NLA_U32, /**< 32 bit integer */
uint64 = NLA_U64, /**< 64 bit integer */
string = NLA_STRING, /**< NUL terminated character string */
binary = NLA_BINARY,
int8 = NLA_S8,
int16 = NLA_S16,
int32 = NLA_S32,
int64 = NLA_S64,
nested = NLA_NESTED,
empty_field = __NLA_TYPE_MAX,
};
struct messages {
int a;
std::string b;
std::map<std::string, std::string> c;
int d;
};
expected<void, std::runtime_error> netlink_put(nl_msg &nm, NetlinkMessageType type, uint32_t size, byte const *bytes) {
if (nla_put(&nm, static_cast<int>(type), size, bytes) < 0) {
return make_unexpected(std::runtime_error { "nla_put" });
}
return {};
}
expected<void, std::runtime_error> netlink_put(nl_msg &nm, int8_t v) {
if (nla_put_s8(&nm, static_cast<int>(NetlinkMessageType::int8), v) < 0) {
return make_unexpected(std::runtime_error { "nla_put_s8" });
}
return {};
}
expected<void, std::runtime_error> netlink_put(nl_msg &nm, int16_t v) {
if (nla_put_s16(&nm, static_cast<int>(NetlinkMessageType::int16), v) < 0) {
return make_unexpected(std::runtime_error { "nla_put_s16" });
}
return {};
}
expected<void, std::runtime_error> netlink_put(nl_msg &nm, int32_t v) {
if (nla_put_s32(&nm, static_cast<int>(NetlinkMessageType::int32), v) < 0) {
return make_unexpected(std::runtime_error { "nla_put_s32" });
}
return {};
}
expected<void, std::runtime_error> netlink_put(nl_msg &nm, int64_t v) {
if (nla_put_s64(&nm, static_cast<int>(NetlinkMessageType::int64), v) < 0) {
return make_unexpected(std::runtime_error { "nla_put_s64" });
}
return {};
}
expected<void, std::runtime_error> netlink_put(nl_msg &nm, uint8_t v) {
if (nla_put_u8(&nm, static_cast<int>(NetlinkMessageType::uint8), v) < 0) {
return make_unexpected(std::runtime_error { "nla_put_u8" });
}
return {};
}
expected<void, std::runtime_error> netlink_put(nl_msg &nm, uint16_t v) {
if (nla_put_u16(&nm, static_cast<int>(NetlinkMessageType::uint16), v) < 0) {
return make_unexpected(std::runtime_error { "nla_put_u16" });
}
return {};
}
expected<void, std::runtime_error> netlink_put(nl_msg &nm, uint32_t v) {
if (nla_put_u32(&nm, static_cast<int>(NetlinkMessageType::uint32), v) < 0) {
return make_unexpected(std::runtime_error { "nla_put_u32" });
}
return {};
}
expected<void, std::runtime_error> netlink_put(nl_msg &nm, uint64_t v) {
if (nla_put_u64(&nm, static_cast<int>(NetlinkMessageType::uint64), v) < 0) {
return make_unexpected(std::runtime_error { "nla_put_u64" });
}
return {};
}
expected<nlattr*, std::runtime_error> netlink_nested_start(nl_msg &nm) noexcept {
auto result = nla_nest_start(&nm, static_cast<int>(NetlinkMessageType::nested));
if (!result) {
return make_unexpected(std::runtime_error { "nla_nest_start failed" });
}
return { result };
}
void netlink_nested_end(nl_msg &nm, nlattr *start) {
nla_nest_end(&nm, start);
}
namespace serialization_impl {
template <typename T>
size_t serialize_size(T &&v) = delete;
template <>
size_t serialize_size(int const &) {
return static_cast<size_t>( nla_total_size(sizeof(int)) );
}
template <>
size_t serialize_size(std::string const &s) {
return static_cast<size_t>(nla_total_size(static_cast<int>( s.length())));
}
template <>
size_t serialize_size(size_t const &) {
return static_cast<size_t>( nla_total_size(sizeof(size_t)) );
}
template <typename K, typename V>
size_t serialize_size(std::map<K, V> const &v) {
size_t map_size = static_cast<size_t>(nla_total_size(sizeof(size_t)));
for (auto &&p : v) {
map_size += serialize_size(p.first);
map_size += serialize_size(p.second);
}
return static_cast<size_t>(nla_total_size(static_cast<int>(map_size)));
}
template <typename T>
expected<void, std::runtime_error> serialize(nl_msg &nm, T &&v) = delete;
template <>
expected<void, std::runtime_error> serialize(nl_msg &nm, const int &v) {
return netlink_put(nm, v);
}
template <>
expected<void, std::runtime_error> serialize(nl_msg &nm, std::size_t &&v) {
return netlink_put(nm, v);
}
template <>
expected<void, std::runtime_error> serialize(nl_msg &nm, std::string const &v) {
if (v.size() >= 0xFFFFFFFFFFFFFFFFull) {
return make_unexpected(std::runtime_error {"value is too big"});
}
return netlink_put(nm, NetlinkMessageType::string, v.length(), reinterpret_cast<byte const *>(v.c_str()));
}
template <typename K, typename V>
expected<void, std::runtime_error> serialize(nl_msg &nm, std::map<K, V> const &v) {
if (v.empty()) {
auto result = netlink_put(nm, NetlinkMessageType::empty_field, 0, nullptr);
if (!result) {
return result;
}
return {};
}
auto start_nested = netlink_nested_start(nm);
if (!start_nested) {
return start_nested.get_unexpected();
}
for (auto &&p : v) {
serialization_impl::serialize(nm, p.first);
serialization_impl::serialize(nm, p.second);
}
netlink_nested_end(nm, start_nested.value());
}
template <typename T>
void deserialize(nlattr *attr, T &&v) = delete;
template <>
void deserialize(nlattr *attr, int &v) {
if (nla_type(attr) != NLA_S32) {
throw std::runtime_error("expected NLA_S32");
}
v = nla_get_s32(attr);
}
template <>
void deserialize(nlattr *attr, std::string &v) {
if (nla_type(attr) != NLA_STRING) {
throw std::runtime_error("expected NLA_STRING");
}
v = std::string(nla_get_string(attr), nla_len(attr));
}
template <typename K, typename V>
void deserialize(nlattr *attr, std::map<K, V> &v) {
if (nla_type(attr) == static_cast<int>(NetlinkMessageType::empty_field)) {
return;
}
if (nla_type(attr) != NLA_NESTED) {
throw std::runtime_error("expected NLA_NESTED");
}
nlattr *nested = reinterpret_cast<nlattr *>( nla_data(attr) );
auto nested_size = nla_len(attr);
while(nla_ok(nested, nested_size)) {
K key;
V value;
serialization_impl::deserialize(nested, key);
nested = nla_next(nested, &nested_size);
if (!nla_ok(nested, nested_size)) {
throw std::runtime_error("no value for map");
}
serialization_impl::deserialize(nested, value);
v.insert(make_pair(std::move(key), std::move(value)));
nested = nla_next(nested, &nested_size);
}
}
}
template<typename T>
void serialize(nl_msg &nm, T &&value) {
boost::pfr::for_each_field(value, [&nm](const auto &field) {
auto result = serialization_impl::serialize(nm, field);
if (!result) {
throw result.error();
}
});
}
template<typename T>
T deserialize(nlattr *attr, uint32_t &size) {
T result;
int isize = size;
boost::pfr::for_each_field(result, [&isize, &attr](auto &field) {
if (!nla_ok(attr, isize)) {
throw std::runtime_error("error");
}
serialization_impl::deserialize(attr, field);
attr = nla_next(attr, &isize);
});
size = isize;
return result;
}
int main()
{
std::cout << boost::pfr::tuple_size<messages>::value << std::endl;
messages v { 123, "aaaadf e23", {{"a", "bb"}, {"az", "bb"}}};
v.d = 5;
size_t message_size = 0;
boost::pfr::for_each_field(v, [&message_size](const auto &field) {
message_size += serialization_impl::serialize_size(field);
});
nl_msg *nm = nlmsg_alloc_size(nlmsg_total_size(message_size));
nlmsghdr *hdr = nlmsg_hdr(nm);
serialize(*nm, v);
auto attr = (nlattr *)nlmsg_data(hdr);
uint32_t size = nlmsg_datalen(hdr);
auto v2 = deserialize<messages>(attr, size);
std::cout << v2.a << " " << v2.b << std::endl;
for (auto && q : v2.c) {
std::cout << q.first << " " << q.second << std::endl;
}
std::cout << v2.d << std::endl;
return 0;
}