Retro68/gcc/libgo/go/net/interface_bsd.go

119 lines
2.7 KiB
Go
Raw Normal View History

// Copyright 2011 The Go Authors. All rights reserved.
2012-03-27 23:13:14 +00:00
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2014-09-21 17:33:12 +00:00
// +build darwin dragonfly freebsd netbsd openbsd
2012-03-27 23:13:14 +00:00
package net
import (
"syscall"
2019-06-02 15:48:37 +00:00
"internal/x/net/route"
2012-03-27 23:13:14 +00:00
)
// If the ifindex is zero, interfaceTable returns mappings of all
// network interfaces. Otherwise it returns a mapping of a specific
2012-03-27 23:13:14 +00:00
// interface.
func interfaceTable(ifindex int) ([]Interface, error) {
msgs, err := interfaceMessages(ifindex)
2012-03-27 23:13:14 +00:00
if err != nil {
return nil, err
2012-03-27 23:13:14 +00:00
}
n := len(msgs)
if ifindex != 0 {
n = 1
2012-03-27 23:13:14 +00:00
}
ift := make([]Interface, n)
n = 0
2012-03-27 23:13:14 +00:00
for _, m := range msgs {
2014-09-21 17:33:12 +00:00
switch m := m.(type) {
case *route.InterfaceMessage:
if ifindex != 0 && ifindex != m.Index {
continue
}
ift[n].Index = m.Index
ift[n].Name = m.Name
ift[n].Flags = linkFlags(m.Flags)
if sa, ok := m.Addrs[syscall.RTAX_IFP].(*route.LinkAddr); ok && len(sa.Addr) > 0 {
ift[n].HardwareAddr = make([]byte, len(sa.Addr))
copy(ift[n].HardwareAddr, sa.Addr)
}
for _, sys := range m.Sys() {
if imx, ok := sys.(*route.InterfaceMetrics); ok {
ift[n].MTU = imx.MTU
break
2014-09-21 17:33:12 +00:00
}
2012-03-27 23:13:14 +00:00
}
n++
if ifindex == m.Index {
return ift[:n], nil
}
2012-03-27 23:13:14 +00:00
}
}
return ift[:n], nil
2012-03-27 23:13:14 +00:00
}
func linkFlags(rawFlags int) Flags {
2012-03-27 23:13:14 +00:00
var f Flags
if rawFlags&syscall.IFF_UP != 0 {
f |= FlagUp
}
if rawFlags&syscall.IFF_BROADCAST != 0 {
f |= FlagBroadcast
}
if rawFlags&syscall.IFF_LOOPBACK != 0 {
f |= FlagLoopback
}
if rawFlags&syscall.IFF_POINTOPOINT != 0 {
f |= FlagPointToPoint
}
if rawFlags&syscall.IFF_MULTICAST != 0 {
f |= FlagMulticast
}
return f
}
2014-09-21 17:33:12 +00:00
// If the ifi is nil, interfaceAddrTable returns addresses for all
// network interfaces. Otherwise it returns addresses for a specific
2014-09-21 17:33:12 +00:00
// interface.
func interfaceAddrTable(ifi *Interface) ([]Addr, error) {
index := 0
if ifi != nil {
index = ifi.Index
}
msgs, err := interfaceMessages(index)
2012-03-27 23:13:14 +00:00
if err != nil {
return nil, err
2012-03-27 23:13:14 +00:00
}
ifat := make([]Addr, 0, len(msgs))
2012-03-27 23:13:14 +00:00
for _, m := range msgs {
2014-09-21 17:33:12 +00:00
switch m := m.(type) {
case *route.InterfaceAddrMessage:
if index != 0 && index != m.Index {
continue
}
var mask IPMask
switch sa := m.Addrs[syscall.RTAX_NETMASK].(type) {
case *route.Inet4Addr:
mask = IPv4Mask(sa.IP[0], sa.IP[1], sa.IP[2], sa.IP[3])
case *route.Inet6Addr:
mask = make(IPMask, IPv6len)
copy(mask, sa.IP[:])
}
var ip IP
switch sa := m.Addrs[syscall.RTAX_IFA].(type) {
case *route.Inet4Addr:
ip = IPv4(sa.IP[0], sa.IP[1], sa.IP[2], sa.IP[3])
case *route.Inet6Addr:
ip = make(IP, IPv6len)
copy(ip, sa.IP[:])
}
if ip != nil && mask != nil { // NetBSD may contain route.LinkAddr
ifat = append(ifat, &IPNet{IP: ip, Mask: mask})
2012-03-27 23:13:14 +00:00
}
}
}
return ifat, nil
}