netavark icon indicating copy to clipboard operation
netavark copied to clipboard

type safety: make Socket aware of namespace

Open Luap99 opened this issue 5 months ago • 5 comments

Using a PhantomData and zero sized struct we can attach an additional generic data for either the host or container namespace to the struct. Because it is zero sized and not used it is optimized away and only the type checker sees it to enforce the right types are used.

With that we basically create two different netlink socket types Socket<HostNS> and Socket<ContainerNS> so they must be used in all type signatures from now on. To keep the changes smaller I have set HostNS as default generic for the struct so we don't need to change most function signatures.

For al call sides where we pass sockets around the compiler now enforces that we use the right ones avoid any possible mix ups.

Summary by Sourcery

Enforce compile-time namespace safety by parameterizing netlink sockets with phantom generic types for host and container namespaces and updating all socket usages accordingly.

Enhancements:

  • Introduce Namespace trait with HostNS and ContainerNS marker types.
  • Make Socket generic over Namespace (defaulting to HostNS) using PhantomData.
  • Update network drivers, core utilities, DHCP proxy, and setup commands to accept Socket<ContainerNS> for container namespace operations.

Luap99 avatar Aug 06 '25 12:08 Luap99