Android-cuttlefish cvd tool
nft_nat_table.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_NET_NFT_NAT_TABLE_H_
18#define ALLOCD_NET_NFT_NAT_TABLE_H_
19
20#include <memory>
21#include <string>
22#include <vector>
23
24#include "absl/strings/string_view.h"
25
28
29namespace cuttlefish {
30
31// Owns an ip family nftables table that masquerades a fixed set of source
32// networks.
34 public:
35 // Fails if a table with this name already exists.
37 absl::string_view name,
38 const std::vector<std::string>& masquerade_sources);
40 std::unique_ptr<NfnetlinkSocket> socket, absl::string_view name,
41 const std::vector<std::string>& masquerade_sources);
42
43 static Result<bool> Exists(absl::string_view name);
44 // For tests to pass in a fake socket.
45 static Result<bool> Exists(NfnetlinkSocket& socket, absl::string_view name);
46
48
49 NftNatTable(NftNatTable&& t) noexcept = default;
51 NftNatTable(const NftNatTable& t) = delete;
52 NftNatTable& operator=(const NftNatTable& t) = delete;
53
54 private:
55 NftNatTable(std::unique_ptr<NfnetlinkSocket> socket, absl::string_view name);
56
57 // The kernel deletes the table when this socket closes.
58 std::unique_ptr<NfnetlinkSocket> socket_;
59 std::string name_;
60};
61
62} // namespace cuttlefish
63
64#endif // ALLOCD_NET_NFT_NAT_TABLE_H_
Definition: nfnetlink_socket.h:27
Definition: nft_nat_table.h:33
NftNatTable(const NftNatTable &t)=delete
static Result< NftNatTable > Create(absl::string_view name, const std::vector< std::string > &masquerade_sources)
Definition: nft_nat_table.cc:47
~NftNatTable()
Definition: nft_nat_table.cc:85
std::unique_ptr< NfnetlinkSocket > socket_
Definition: nft_nat_table.h:58
static Result< bool > Exists(absl::string_view name)
Definition: nft_nat_table.cc:67
std::string name_
Definition: nft_nat_table.h:59
NftNatTable & operator=(const NftNatTable &t)=delete
NftNatTable & operator=(NftNatTable &&t)=delete
NftNatTable(NftNatTable &&t) noexcept=default
Definition: alloc_driver.h:21
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30