blob: 5862afce00753e76ff6bc90aaaad38cdffa086ab [file] [log] [blame]
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build fuchsia
package net
import (
"context"
"sync"
"syscall/zx"
"syscall/zx/fdio"
"syscall/zx/net/stack"
)
var stackHandle struct {
mu struct {
sync.Mutex
h zx.Handle
}
}
func stackClient() (stack.StackWithCtxInterface, error) {
stackHandle.mu.Lock()
defer stackHandle.mu.Unlock()
if stackHandle.mu.h == zx.HandleInvalid {
local, remote, err := zx.NewChannel(0)
if err != nil {
return stack.StackWithCtxInterface{}, err
}
if err := fdio.ServiceConnect("/svc/"+stack.StackName, zx.Handle(remote)); err != nil {
return stack.StackWithCtxInterface{}, err
}
stackHandle.mu.h = zx.Handle(local)
}
return stack.StackWithCtxInterface{Channel: zx.Channel(stackHandle.mu.h)}, nil
}
// If the ifindex is zero, interfaceTable returns mappings of all
// network interfaces. Otherwise it returns a mapping of a specific interface.
func interfaceTable(ifindex int) ([]Interface, error) {
c, err := stackClient()
if err != nil {
return nil, err
}
ifInfos, err := c.ListInterfaces(context.Background())
if err != nil {
return nil, err
}
ifs := make([]Interface, 0, len(ifInfos))
for _, info := range ifInfos {
ifs = append(ifs, Interface{
// TODO(jayzhuang): fill `Flags` when they are all available in stack.InterfaceInfo.
Index: int(info.Id),
MTU: int(info.Properties.Mtu),
Name: info.Properties.Name,
HardwareAddr: info.Properties.Mac.Octets[:],
})
}
return ifs, nil
}
// If the ifi is nil, interfaceAddrTable returns addresses for all
// network interfaces. Otherwise it returns addresses for a specific interface.
func interfaceAddrTable(ifi *Interface) ([]Addr, error) {
// TODO(44532): implement
return nil, nil
}
// interfaceMulticastAddrTable returns addresses for a specific interface.
func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) {
// TODO(44532): implement
return nil, nil
}