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 <cstdint>
24#include <string>
25#include <type_traits>
26#include <utility>
27#include <vector>
28
29namespace cuttlefish {
30// Abstraction of Network link request.
31// Used to supply kernel with information about which interface needs to be
32// changed, and how.
34 public:
35 // Create new Netlink Request structure.
36 // Parameter |type| specifies netlink request type (eg. RTM_NEWLINK), while
37 // |flags| are netlink and request specific flags (eg. NLM_F_DUMP).
38 NetlinkRequest(int32_t type, int32_t flags);
40
41 ~NetlinkRequest() = default;
42
43 // Add an IFLA tag followed by a string.
44 // Returns true, if successful.
45 void AddString(uint16_t type, const std::string& value);
46
47 // Add an IFLA tag followed by an integer.
48 template <typename T>
49 void AddInt(uint16_t type, T value) {
50 static_assert(std::is_integral<T>::value,
51 "AddInt must be used on integer types.");
52 AppendTag(type, &value, sizeof(value));
53 }
54
55 // Add an interface info structure.
56 // Parameter |if_index| specifies particular interface index to which the
57 // attributes following the IfInfo apply.
58 void AddIfInfo(int32_t if_index, bool is_operational);
59
60 // Add an address info to a specific interface.
61 void AddAddrInfo(int32_t if_index, int prefix_len = 24);
62
63 // Add an ipv4 address
64 void AddInAddr(uint16_t type, in_addr_t* addr);
65
66 // Creates new list.
67 // List mimic recursive structures in a flat, continuous representation.
68 // Each call to PushList() should have a corresponding call to PopList
69 // indicating end of sub-attribute list.
70 void PushList(uint16_t type);
71
72 // Indicates end of previously declared list.
73 void PopList();
74
75 // Request data.
76 void* RequestData() const;
77
78 // Request length.
79 size_t RequestLength() const;
80
81 // Request Sequence Number.
82 uint32_t SeqNo() const;
83
84 // Append raw data to buffer.
85 // data must not be null.
86 // Returns pointer to copied location.
87 void* AppendRaw(const void* data, size_t length);
88
89 // Reserve |length| number of bytes in the buffer.
90 // Returns pointer to reserved location.
91 void* ReserveRaw(size_t length);
92
93 // Append specialized data.
94 template <typename T>
95 T* Append(const T& data) {
96 return static_cast<T*>(AppendRaw(&data, sizeof(T)));
97 }
98
99 // Reserve specialized data.
100 template <typename T>
101 T* Reserve() {
102 return static_cast<T*>(ReserveRaw(sizeof(T)));
103 }
104
105 private:
106 nlattr* AppendTag(uint16_t type, const void* data, uint16_t length);
107
108 std::vector<std::pair<nlattr*, int32_t>> lists_;
109 std::vector<char> request_;
111
114};
115} // namespace cuttlefish
116#endif // COMMON_LIBS_NET_NETLINK_REQUEST_H_
Definition: netlink_request.h:33
void * AppendRaw(const void *data, size_t length)
Definition: netlink_request.cc:42
void AddIfInfo(int32_t if_index, bool is_operational)
Definition: netlink_request.cc:85
std::vector< std::pair< nlattr *, int32_t > > lists_
Definition: netlink_request.h:108
size_t RequestLength() const
Definition: netlink_request.cc:129
void PopList()
Definition: netlink_request.cc:112
nlmsghdr * header_
Definition: netlink_request.h:110
NetlinkRequest(const NetlinkRequest &)=delete
std::vector< char > request_
Definition: netlink_request.h:109
uint32_t SeqNo() const
Definition: netlink_request.cc:40
void * ReserveRaw(size_t length)
Definition: netlink_request.cc:49
nlattr * AppendTag(uint16_t type, const void *data, uint16_t length)
Definition: netlink_request.cc:55
void * RequestData() const
Definition: netlink_request.cc:123
void AddInt(uint16_t type, T value)
Definition: netlink_request.h:49
T * Append(const T &data)
Definition: netlink_request.h:95
void AddString(uint16_t type, const std::string &value)
Definition: netlink_request.cc:81
NetlinkRequest(int32_t type, int32_t flags)
Definition: netlink_request.cc:64
void AddInAddr(uint16_t type, in_addr_t *addr)
Definition: netlink_request.cc:102
void AddAddrInfo(int32_t if_index, int prefix_len=24)
Definition: netlink_request.cc:93
NetlinkRequest & operator=(const NetlinkRequest &)=delete
T * Reserve()
Definition: netlink_request.h:101
void PushList(uint16_t type)
Definition: netlink_request.cc:106
char data[Size]
Definition: incremental_server.cpp:1
Definition: alloc_driver.h:21
uint8_t type
Definition: pairing_connection.h:0
Definition: netlink.h:229
Definition: netlink.h:52