Add typed high-level ruleset and set API

This commit is contained in:
Vibe Myass
2026-03-16 03:51:49 +00:00
parent 458494221e
commit 1dfc6aebfd
12 changed files with 644 additions and 7 deletions

View File

@@ -1,14 +1,15 @@
# libnftables-dotnet
`libnftables-dotnet` is a command-centric .NET wrapper over system-installed `libnftables`, with low-level SWIG-generated bindings and a small managed API for common workflows.
`libnftables-dotnet` is a typed-first .NET wrapper over system-installed `libnftables`, with a high-level object model for common workflows and low-level SWIG-generated bindings for advanced control.
## Current Scope
This library is intentionally narrow.
- High-level managed API:
- `Validate`
- `Apply`
- typed `NftRuleset` / `NftTable` / `NftSet` authoring
- `ValidateRuleset`
- `ApplyRuleset`
- `Snapshot`
- `Restore`
- Low-level managed wrapper:
@@ -16,7 +17,7 @@ This library is intentionally narrow.
Non-goals for the current release:
- Typed .NET models for tables, chains, rules, sets, or maps
- Typed rule expressions, maps, and snapshot parsing back into object models
- Event monitoring or subscriptions
- Cross-platform support beyond Linux x64
@@ -67,13 +68,31 @@ using LibNftables;
INftablesClient client = new NftablesClient();
var validation = client.Validate(NftApplyRequest.FromText("add table inet my_table"));
var ruleset = new NftRuleset();
var table = new NftTable
{
Family = NftFamily.Inet,
Name = "filter",
};
var blocked = new NftSet
{
Name = "blocked_ipv4",
Type = NftSetType.Ipv4Address,
};
blocked.Elements.Add("10.0.0.1");
blocked.Elements.Add("10.0.0.2");
table.Sets.Add(blocked);
ruleset.Tables.Add(table);
var validation = client.ValidateRuleset(ruleset);
if (validation.IsValid)
{
client.Apply(NftApplyRequest.FromText("add table inet my_table"));
client.ApplyRuleset(ruleset);
}
```
Raw command text remains available through `NftApplyRequest` as a fallback for nft syntax not yet modeled by the typed API.
## Low-Level Example
```csharp
@@ -113,7 +132,7 @@ Some operations require elevated privileges or `CAP_NET_ADMIN`, especially when
### Validation failures
`Validate` returns `IsValid = false` for invalid nft syntax. `Apply` and `Restore` throw when the request shape is invalid or native parsing fails.
`ValidateRuleset` returns `IsValid = false` for invalid nft syntax after rendering the typed model. `ApplyRuleset` and `Restore` throw when the typed/request shape is invalid or native parsing fails.
## Bindings and Regeneration