Android-cuttlefish cvd tool
nft_messages_helper.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 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
17#ifndef ALLOCD_TEST_NFT_MESSAGES_HELPER_H_
18#define ALLOCD_TEST_NFT_MESSAGES_HELPER_H_
19
20#include <stdint.h>
21
22#include <string>
23#include <vector>
24
25#include "absl/strings/string_view.h"
26
28
29// This is the test-side inverse of cuttlefish::nft.
30// It decodes client request bytes and builds fake kernel reply bytes.
31namespace cuttlefish::nft_test {
32
33struct Attr {
34 // With the NLA_F_* flags stripped.
35 uint16_t type;
36 std::vector<char> payload;
37};
38
39struct Message {
40 uint16_t type;
41 uint16_t flags;
42 uint8_t family;
43 // In host byte order.
44 uint16_t res_id;
45 // Attributes following the nfgenmsg.
46 std::vector<Attr> attrs;
47};
48
49std::vector<Message> ParseMessages(const std::vector<char>& data);
50
51// Decodes a single message, without batch begin and end around it.
52std::vector<Message> ParseMessages(const NetlinkRequest& message);
53
54std::string GetStringAttr(const std::vector<Attr>& attrs, uint16_t type);
55uint32_t GetUint32BigEndianAttr(const std::vector<Attr>& attrs, uint16_t type);
56std::vector<Attr> GetNestedAttrs(const std::vector<Attr>& attrs, uint16_t type);
57
58// Returns the IPv4 address inside a nested NFTA_DATA_VALUE.
59std::string GetIpv4AddressAttr(const std::vector<Attr>& attrs, uint16_t type);
60
61struct Expression {
62 std::string name;
63 // From inside NFTA_EXPR_DATA.
64 std::vector<Attr> attrs;
65};
66std::vector<Expression> GetRuleExpressions(const Message& rule);
67
68uint16_t NftType(uint16_t message);
69
70std::vector<char> BuildErrorReply(int error);
71
72std::vector<char> BuildTableReply(absl::string_view name);
73std::vector<char> Concat(std::vector<char> a, const std::vector<char>& b);
74
75} // namespace cuttlefish::nft_test
76
77#endif // ALLOCD_TEST_NFT_MESSAGES_HELPER_H_
Definition: netlink_request.h:33
#define error(format, args...)
Definition: fec_private.h:199
char data[Size]
Definition: incremental_server.cpp:1
Definition: nft_messages_helper.cc:38
std::vector< char > BuildTableReply(absl::string_view name)
Definition: nft_messages_helper.cc:192
std::string GetIpv4AddressAttr(const std::vector< Attr > &attrs, uint16_t type)
Definition: nft_messages_helper.cc:149
std::vector< Message > ParseMessages(const std::vector< char > &data)
Definition: nft_messages_helper.cc:81
uint32_t GetUint32BigEndianAttr(const std::vector< Attr > &attrs, uint16_t type)
Definition: nft_messages_helper.cc:128
std::string GetStringAttr(const std::vector< Attr > &attrs, uint16_t type)
Definition: nft_messages_helper.cc:118
std::vector< char > Concat(std::vector< char > a, const std::vector< char > &b)
Definition: nft_messages_helper.cc:203
std::vector< char > BuildErrorReply(int error)
Definition: nft_messages_helper.cc:186
uint16_t NftType(uint16_t message)
Definition: nft_messages_helper.cc:182
std::vector< Attr > GetNestedAttrs(const std::vector< Attr > &attrs, uint16_t type)
Definition: nft_messages_helper.cc:139
std::vector< Expression > GetRuleExpressions(const Message &rule)
Definition: nft_messages_helper.cc:166
uint8_t type
Definition: pairing_connection.h:0
Definition: nft_messages_helper.h:33
uint16_t type
Definition: nft_messages_helper.h:35
std::vector< char > payload
Definition: nft_messages_helper.h:36
Definition: nft_messages_helper.h:61
std::vector< Attr > attrs
Definition: nft_messages_helper.h:64
std::string name
Definition: nft_messages_helper.h:62
Definition: nft_messages_helper.h:39
uint8_t family
Definition: nft_messages_helper.h:42
uint16_t flags
Definition: nft_messages_helper.h:41
std::vector< Attr > attrs
Definition: nft_messages_helper.h:46
uint16_t type
Definition: nft_messages_helper.h:40
uint16_t res_id
Definition: nft_messages_helper.h:44