| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | #pragma once |
| 3 | |
| 4 | #include <arpa/inet.h> |
| 5 | #include "Syscall.h" |
| 6 | #include "Utils.h" |
| 7 | |
| 8 | template <typename T> |
| 9 | std::ostream& utils::FormatArray(std::ostream& out, const std::vector<T>& content) |
| 10 | { |
| 11 | out << "["; |
| 12 | |
| 13 | for (size_t i = 0; i < content.size(); i++) |
| 14 | { |
| 15 | if (i != 0) |
| 16 | { |
| 17 | out << ","; |
| 18 | } |
| 19 | |
| 20 | out << content[i]; |
| 21 | } |
| 22 | |
| 23 | return out << "]"; |
| 24 | } |
| 25 | |
| 26 | template <typename TAddr> |
| 27 | void utils::InitializeAddressAttribute(AddressAttribute<TAddr>& attribute, const Address& address, int type) |
| 28 | { |
| 29 | // We can't pass &attribute.address because the structure is packed, |
| 30 | // so that could cause an unaligned access |
| 31 | TAddr netAddress = {}; |
| 32 | Syscall(::inet_pton, address.Family(), address.Addr().c_str(), &netAddress); |
| 33 | |
| 34 | attribute.header.rta_len = RTA_LENGTH(sizeof(netAddress)); |
| 35 | attribute.header.rta_type = type; |
| 36 | attribute.address = netAddress; |
| 37 | } |
| 38 | |
| 39 | inline void utils::InitializeCacheInfoAttribute(CacheInfoAttribute& attribute, const Address& address) |
| 40 | { |
| 41 | attribute.header.rta_len = RTA_LENGTH(sizeof(struct ifa_cacheinfo)); |
| 42 | attribute.header.rta_type = IFA_CACHEINFO; |
| 43 | attribute.cacheinfo.ifa_prefered = address.PreferredLifetime(); |
| 44 | attribute.cacheinfo.ifa_valid = 0xFFFFFFFF; |
| 45 | } |
| 46 | |
| 47 | inline void utils::InitializeIntegerAttribute(IntegerAttribute& attribute, int value, int type) |
| 48 | { |
| 49 | attribute.header.rta_len = RTA_LENGTH(sizeof(int)); |
| 50 | attribute.header.rta_type = type; |
| 51 | attribute.value = value; |
| 52 | } |
| 53 | |
| 54 | template <typename T> |
| 55 | std::string utils::Stringify(const T& value) |
| 56 | { |
| 57 | std::stringstream str; |
| 58 | str << value; |
| 59 | return str.str(); |
| 60 | } |