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