diff --git a/host/lux_netif.go b/host/lux_netif.go index 471fe7a..d1dce94 100644 --- a/host/lux_netif.go +++ b/host/lux_netif.go @@ -1,10 +1,10 @@ package host import ( - "fmt" "lux/proto" "net" "net/netip" + "strconv" "strings" ) @@ -24,6 +24,21 @@ const ( LuxNetAddrType6GUA = 3 ) +func (addrType LuxNetAddrType) String() string { + switch addrType { + case LuxNetAddrType4: + return "ip4" + case LuxNetAddrType6LL: + return "ip6ll" + case LuxNetAddrType6ULA: + return "ip6ula" + case LuxNetAddrType6GUA: + return "ip6gua" + default: + return strconv.Itoa(int(addrType)) + } +} + type LuxNetAddr struct { Type LuxNetAddrType Addr netip.Addr @@ -179,7 +194,6 @@ func (opt *LuxOptionNetIf) EnumerateNetlink() error { if len(netif.Addrs) > 0 { // only add if we got atleast 1 ip, since we might want to skip // netif loopback that has no IP because we skip one in FromNetlink - fmt.Println(netif) opt.Interfaces[netif.Name] = &netif } } @@ -198,6 +212,8 @@ func (opt *LuxOptionNetIf) Read(rd *proto.LuxBuffer) error { if err := netif.Read(rd); err != nil { return err } + + opt.Interfaces[netif.Name] = &netif } return nil diff --git a/host/lux_state.go b/host/lux_state.go index d7b5c74..d569192 100644 --- a/host/lux_state.go +++ b/host/lux_state.go @@ -66,7 +66,26 @@ func (state *LuxState) IntoRpc() rpc.LuxRpcState { wan := opt.(*LuxOptionWAN) rpcState.WAN = rpc.LuxRpcWAN{ Addr4: wan.Addr4.String(), - Addr6: wan.Addr4.String(), + Addr6: wan.Addr6.String(), + } + case LuxOptionTypeNetIf: + netif := opt.(*LuxOptionNetIf) + + rpcState.NetIf.Interfaces = make([]rpc.LuxRpcNetInterface, 0) + for _, optIf := range netif.Interfaces { + rpcIf := rpc.LuxRpcNetInterface{ + Name: optIf.Name, + Index: optIf.Index, + Addrs: make([]rpc.LuxRpcNetAddr, 0), + } + for _, optAddr := range optIf.Addrs { + rpcIf.Addrs = append(rpcIf.Addrs, rpc.LuxRpcNetAddr{ + Type: optAddr.Type.String(), + Addr: optAddr.Addr.String(), + }) + } + + rpcState.NetIf.Interfaces = append(rpcState.NetIf.Interfaces, rpcIf) } default: // encode option in base64 blob diff --git a/main.go b/main.go index 712ff00..0ff493b 100644 --- a/main.go +++ b/main.go @@ -828,6 +828,12 @@ func rpcMain() { fmt.Printf("host %s hostname %s\n", host.HostID, host.Hostname) fmt.Printf("wan addr4 %s addr6 %s\n", host.State.WAN.Addr4, host.State.WAN.Addr6) + for _, netif := range host.State.NetIf.Interfaces { + fmt.Printf("netif %s idx %d\n", netif.Name, netif.Index) + for _, addr := range netif.Addrs { + fmt.Printf("\t%s\t%s\n", addr.Type, addr.Addr) + } + } } } diff --git a/rpc/lux_rpc_data.go b/rpc/lux_rpc_data.go index a6ffe7e..3c94f47 100644 --- a/rpc/lux_rpc_data.go +++ b/rpc/lux_rpc_data.go @@ -17,10 +17,32 @@ type LuxRpcOption struct { Blob string `xml:",innerxml"` } +type LuxRpcNetAddr struct { + XMLName xml.Name `xml:"addr"` + Type string `xml:"type,attr"` + Addr string `xml:",innerxml"` +} + +type LuxRpcNetInterface struct { + XMLName xml.Name `xml:"if"` + + Name string `xml:"name,attr"` + Index int `xml:"idx,attr"` + + Addrs []LuxRpcNetAddr `xml:"addr"` +} + +type LuxRpcNetIf struct { + XMLName xml.Name `xml:"netif"` + + Interfaces []LuxRpcNetInterface `xml:"if"` +} + type LuxRpcState struct { XMLName xml.Name `xml:"state"` - WAN LuxRpcWAN `xml:"wan"` + WAN LuxRpcWAN `xml:"wan"` + NetIf LuxRpcNetIf `xml:"netif"` Options []LuxRpcOption `xml:"option"` }