Android-cuttlefish cvd tool
netlink_request.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef ALLOCD_NET_NETLINK_REQUEST_H_
17#define ALLOCD_NET_NETLINK_REQUEST_H_
18
19#include <linux/netlink.h>
20#include <netinet/in.h>
21#include <stddef.h>
22
23#include <array>
24#include <cstdint>
25#include <string>
26#include <type_traits>
27#include <utility>
28#include <vector>
29
30namespace cuttlefish {
31// Abstraction of Network link request.
32// Used to supply kernel with information about which interface needs to be
33// changed, and how.
35 public:
36 // Create new Netlink Request structure.
37 // Parameter |type| specifies netlink request type (eg. RTM_NEWLINK), while
38 // |flags| are netlink and request specific flags (eg. NLM_F_DUMP).
39 NetlinkRequest(int32_t type, int32_t flags);
41
42 ~NetlinkRequest() = default;
43
44 // Add an IFLA tag followed by a string.
45 // Returns true, if successful.
46 void AddString(uint16_t type, const std::string& value);
47
48 // Add an IFLA tag followed by an integer.
49 template <typename T>
50 void AddInt(uint16_t type, T value) {
51 static_assert(std::is_integral<T>::value,
52 "AddInt must be used on integer types.");
53 AppendTag(type, &value, sizeof(value));
54 }
55
56 // Add an interface info structure.
57 // Parameter |if_index| specifies particular interface index to which the
58 // attributes following the IfInfo apply.
59 void AddIfInfo(int32_t if_index, bool is_operational);
60
61 // Add an address info to a specific interface.
62 void AddAddrInfo(int32_t if_index, int prefix_len = 24);
63
64 // Add an ethernet address/mac address
65 void AddMacAddress(const std::array<unsigned char, 6>& address);
66
67 // Add an ipv4 address
68 void AddInAddr(uint16_t type, in_addr_t* addr);
69
70 // Creates new list.
71 // List mimic recursive structures in a flat, continuous representation.
72 // Each call to PushList() should have a corresponding call to PopList
73 // indicating end of sub-attribute list.
74 void PushList(uint16_t type);
75
76 // Indicates end of previously declared list.
77 void PopList();
78
79 // Request data.
80 void* RequestData() const;
81
82 // Request length.
83 size_t RequestLength() const;
84
85 // Request Sequence Number.
86 uint32_t SeqNo() const;
87
88 // Append raw data to buffer.
89 // data must not be null.
90 // Returns pointer to copied location.
91 void* AppendRaw(const void* data, size_t length);
92
93 // Reserve |length| number of bytes in the buffer.
94 // Returns pointer to reserved location.
95 void* ReserveRaw(size_t length);
96
97 // Append specialized data.
98 template <typename T>
99 T* Append(const T& data) {
100 return static_cast<T*>(AppendRaw(&data, sizeof(T)));
101 }
102
103 // Reserve specialized data.
104 template <typename T>
105 T* Reserve() {
106 return static_cast<T*>(ReserveRaw(sizeof(T)));
107 }
108
109 private:
110 nlattr* AppendTag(uint16_t type, const void* data, uint16_t length);
111
112 std::vector<std::pair<nlattr*, int32_t>> lists_;
113 std::vector<char> request_;
115
118};
119} // namespace cuttlefish
120#endif // COMMON_LIBS_NET_NETLINK_REQUEST_H_
Definition: netlink_request.h:34
void * AppendRaw(const void *data, size_t length)
Definition: netlink_request.cc:44
void AddIfInfo(int32_t if_index, bool is_operational)
Definition: netlink_request.cc:87
std::vector< std::pair< nlattr *, int32_t > > lists_
Definition: netlink_request.h:112
size_t RequestLength() const
Definition: netlink_request.cc:136
void PopList()
Definition: netlink_request.cc:119
nlmsghdr * header_
Definition: netlink_request.h:114
NetlinkRequest(const NetlinkRequest &)=delete
std::vector< char > request_
Definition: netlink_request.h:113
uint32_t SeqNo() const
Definition: netlink_request.cc:42
void * ReserveRaw(size_t length)
Definition: netlink_request.cc:51
nlattr * AppendTag(uint16_t type, const void *data, uint16_t length)
Definition: netlink_request.cc:57
void * RequestData() const
Definition: netlink_request.cc:130
void AddInt(uint16_t type, T value)
Definition: netlink_request.h:50
T * Append(const T &data)
Definition: netlink_request.h:99
void AddString(uint16_t type, const std::string &value)
Definition: netlink_request.cc:83
NetlinkRequest(int32_t type, int32_t flags)
Definition: netlink_request.cc:66
void AddInAddr(uint16_t type, in_addr_t *addr)
Definition: netlink_request.cc:109
void AddAddrInfo(int32_t if_index, int prefix_len=24)
Definition: netlink_request.cc:95
void AddMacAddress(const std::array< unsigned char, 6 > &address)
Definition: netlink_request.cc:104
NetlinkRequest & operator=(const NetlinkRequest &)=delete
T * Reserve()
Definition: netlink_request.h:105
void PushList(uint16_t type)
Definition: netlink_request.cc:113
#define data
Definition: dict.c:56
Definition: alloc_driver.h:20
uint8_t type
Definition: pairing_connection.h:0
Definition: netlink.h:229
Definition: netlink.h:52