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 <vector> 13 14 bool operator==(const struct ifaddrmsg& lhs, const struct ifaddrmsg& rhs); 15 16 namespace net { 17 class IPAddress; 18 } 19 20 namespace net::test { 21 22 using NetlinkBuffer = std::vector<char>; 23 24 class NetlinkMessage { 25 public: 26 explicit NetlinkMessage(uint16_t type); 27 ~NetlinkMessage(); 28 void AddPayload(const void* data, size_t length); 29 template <typename T> AddPayload(const T & data)30 void AddPayload(const T& data) { 31 AddPayload(&data, sizeof(data)); 32 } 33 void AddAttribute(uint16_t type, const void* data, size_t length); 34 void AppendTo(NetlinkBuffer* output) const; 35 36 private: 37 void Append(const void* data, size_t length); 38 void Align(); header()39 struct nlmsghdr* header() { 40 return reinterpret_cast<struct nlmsghdr*>(buffer_.data()); 41 } 42 43 NetlinkBuffer buffer_; 44 }; 45 46 void MakeAddrMessageWithCacheInfo(uint16_t type, 47 uint8_t flags, 48 uint8_t family, 49 int index, 50 const IPAddress& address, 51 const IPAddress& local, 52 uint32_t preferred_lifetime, 53 NetlinkBuffer* output); 54 55 void MakeAddrMessage(uint16_t type, 56 uint8_t flags, 57 uint8_t family, 58 int index, 59 const IPAddress& address, 60 const IPAddress& local, 61 NetlinkBuffer* output); 62 63 void MakeLinkMessage(uint16_t type, 64 uint32_t flags, 65 uint32_t index, 66 NetlinkBuffer* output, 67 bool clear_output = true); 68 69 void MakeWirelessLinkMessage(uint16_t type, 70 uint32_t flags, 71 uint32_t index, 72 NetlinkBuffer* output, 73 bool clear_output = true); 74 } // namespace net::test 75 76 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_TEST_UTIL_H_ 77