master
h 81 lines 1.82 KB
Raw
1 // Copyright (C) Microsoft Corporation. All rights reserved.
2 #pragma once
3
4 #include <ostream>
5 #include <vector>
6 #include <linux/rtnetlink.h>
7 #include <linux/fib_rules.h>
8 #include "address.h"
9
10 // TODO: Maybe use a template and __function__ to write the type name compile
11 // time ?
12
13 namespace utils {
14 template <typename T>
15 struct Attribute
16 {
17 rtattr header __attribute__((aligned(RTA_ALIGNTO)));
18 T value __attribute__((aligned(RTA_ALIGNTO)));
19 } __attribute__((packed));
20
21 template <bool Empty, typename T>
22 struct Optional;
23
24 template <typename T>
25 struct Optional<false, T>
26 {
27 /// empty, though note that empty structs are of size 1 byte.
28 };
29
30 template <typename T>
31 struct Optional<true, T>
32 {
33 T attribute;
34 };
35
36 template <typename TAddr>
37 struct AddressAttribute
38 {
39 rtattr header;
40 TAddr address;
41 } __attribute__((packed));
42
43 struct CacheInfoAttribute
44 {
45 rtattr header;
46 struct ifa_cacheinfo cacheinfo;
47 } __attribute((packed));
48
49 struct MacAddressAttribute
50 {
51 nlattr header;
52 MacAddress address;
53 } __attribute__((packed));
54
55 struct IntegerAttribute
56 {
57 rtattr header;
58 int value;
59 } __attribute__((packed));
60
61 std::ostream& FormatBinary(std::ostream& out, const void* ptr, size_t bytes);
62
63 template <typename T>
64 std::ostream& FormatArray(std::ostream& out, const std::vector<T>& content);
65
66 std::string BytesToHex(const void* ptr, size_t bytes, const std::string& separator);
67
68 template <typename TAddr>
69 void InitializeAddressAttribute(AddressAttribute<TAddr>& attribute, const Address& address, int type);
70
71 void InitializeCacheInfoAttribute(CacheInfoAttribute& attribute, const Address& address);
72
73 void InitializeIntegerAttribute(IntegerAttribute& attribute, int value, int type);
74
75 Address ComputeBroadcastAddress(const Address& address);
76
77 template <typename T>
78 std::string Stringify(const T& value);
79 } // namespace utils
80
81 #include "Utils.hxx"