host daemon (#2)

* feat: host daemon api scaffold

* fix: use sparse writes

* fix: unix socket length (<108 bytes)
This commit is contained in:
Hari 2026-04-08 11:23:19 -04:00 committed by GitHub
parent 4028bb5a1d
commit e2f9e54970
21 changed files with 2111 additions and 372 deletions

View file

@ -4,6 +4,7 @@ import (
"context"
"encoding/binary"
"fmt"
"net"
"net/netip"
"os/exec"
"strings"
@ -47,6 +48,30 @@ func (n NetworkAllocation) GuestIP() netip.Addr {
return n.GuestCIDR.Addr()
}
// AllocationFromGuestIP reconstructs the host-side allocation from a guest IP and tap name.
func AllocationFromGuestIP(guestIP string, tapName string) (NetworkAllocation, error) {
parsed := net.ParseIP(strings.TrimSpace(guestIP))
if parsed == nil {
return NetworkAllocation{}, fmt.Errorf("parse guest ip %q", guestIP)
}
addr, ok := netip.AddrFromSlice(parsed.To4())
if !ok {
return NetworkAllocation{}, fmt.Errorf("guest ip %q must be IPv4", guestIP)
}
base := ipv4ToUint32(addr) - 2
hostIP := uint32ToIPv4(base + 1)
guest := uint32ToIPv4(base + 2)
return NetworkAllocation{
InterfaceID: defaultInterfaceID,
TapName: strings.TrimSpace(tapName),
HostCIDR: netip.PrefixFrom(hostIP, defaultNetworkPrefixBits),
GuestCIDR: netip.PrefixFrom(guest, defaultNetworkPrefixBits),
GatewayIP: hostIP,
GuestMAC: macForIPv4(guest),
}, nil
}
// NewNetworkAllocator returns a new /30 allocator rooted at the provided IPv4 prefix.
func NewNetworkAllocator(cidr string) (*NetworkAllocator, error) {
cidr = strings.TrimSpace(cidr)