1 // Copyright 2023 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_BASE_ADDRESS_TRACKER_LINUX_TEST_UTIL_H_ 6 #define NET_BASE_ADDRESS_TRACKER_LINUX_TEST_UTIL_H_ 7 8 #include <linux/netlink.h> 9 #include <linux/rtnetlink.h> 10 #include <stdint.h> 11 12 #include <cstddef> 13 #include <vector> 14 15 bool operator==(const struct ifaddrmsg& lhs, const struct ifaddrmsg& rhs); 16 17 namespace net { 18 class IPAddress; 19 } 20 21 namespace net::test { 22 23 using NetlinkBuffer = std::vector<char>; 24 25 class NetlinkMessage { 26 public: 27 explicit NetlinkMessage(uint16_t type); 28 ~NetlinkMessage(); 29 void AddPayload(const void* data, size_t length); 30 template <typename T> AddPayload(const T & data)31 void AddPayload(const T& data) { 32 AddPayload(&data, sizeof(data)); 33 } 34 void AddAttribute(uint16_t type, const void* data, size_t length); 35 void AppendTo(NetlinkBuffer* output) const; 36 37 private: 38 void Append(const void* data, size_t length); 39 void Align(); header()40 struct nlmsghdr* header() { 41 return reinterpret_cast<struct nlmsghdr*>(buffer_.data()); 42 } 43 44 NetlinkBuffer buffer_; 45 }; 46 47 void MakeAddrMessageWithCacheInfo(uint16_t type, 48 uint8_t flags, 49 uint8_t family, 50 int index, 51 const IPAddress& address, 52 const IPAddress& local, 53 uint32_t preferred_lifetime, 54 NetlinkBuffer* output); 55 56 void MakeAddrMessage(uint16_t type, 57 uint8_t flags, 58 uint8_t family, 59 int index, 60 const IPAddress& address, 61 const IPAddress& local, 62 NetlinkBuffer* output); 63 64 void MakeLinkMessage(uint16_t type, 65 uint32_t flags, 66 uint32_t index, 67 NetlinkBuffer* output, 68 bool clear_output = true); 69 70 void MakeWirelessLinkMessage(uint16_t type, 71 uint32_t flags, 72 uint32_t index, 73 NetlinkBuffer* output, 74 bool clear_output = true); 75 } // namespace net::test 76 77 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_TEST_UTIL_H_ 78