1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
10 "golang_org/x/net/lif"
13 // If the ifindex is zero, interfaceTable returns mappings of all
14 // network interfaces. Otherwise it returns a mapping of a specific
16 func interfaceTable(ifindex
int) ([]Interface
, error
) {
17 lls
, err
:= lif
.Links(syscall
.AF_UNSPEC
, "")
22 for _
, ll
:= range lls
{
23 if ifindex
!= 0 && ifindex
!= ll
.Index
{
26 ifi
:= Interface
{Index
: ll
.Index
, MTU
: ll
.MTU
, Name
: ll
.Name
, Flags
: linkFlags(ll
.Flags
)}
28 ifi
.HardwareAddr
= HardwareAddr(ll
.Addr
)
30 ift
= append(ift
, ifi
)
37 sysIFF_BROADCAST
= 0x2
40 sysIFF_POINTOPOINT
= 0x10
41 sysIFF_NOTRAILERS
= 0x20
44 sysIFF_PROMISC
= 0x100
45 sysIFF_ALLMULTI
= 0x200
46 sysIFF_INTELLIGENT
= 0x400
47 sysIFF_MULTICAST
= 0x800
48 sysIFF_MULTI_BCAST
= 0x1000
49 sysIFF_UNNUMBERED
= 0x2000
50 sysIFF_PRIVATE
= 0x8000
53 func linkFlags(rawFlags
int) Flags
{
55 if rawFlags
&sysIFF_UP
!= 0 {
58 if rawFlags
&sysIFF_BROADCAST
!= 0 {
61 if rawFlags
&sysIFF_LOOPBACK
!= 0 {
64 if rawFlags
&sysIFF_POINTOPOINT
!= 0 {
67 if rawFlags
&sysIFF_MULTICAST
!= 0 {
73 // If the ifi is nil, interfaceAddrTable returns addresses for all
74 // network interfaces. Otherwise it returns addresses for a specific
76 func interfaceAddrTable(ifi
*Interface
) ([]Addr
, error
) {
81 as
, err
:= lif
.Addrs(syscall
.AF_UNSPEC
, name
)
86 for _
, a
:= range as
{
89 switch a
:= a
.(type) {
91 ip
= IPv4(a
.IP
[0], a
.IP
[1], a
.IP
[2], a
.IP
[3])
92 mask
= CIDRMask(a
.PrefixLen
, 8*IPv4len
)
94 ip
= make(IP
, IPv6len
)
96 mask
= CIDRMask(a
.PrefixLen
, 8*IPv6len
)
98 ifat
= append(ifat
, &IPNet
{IP
: ip
, Mask
: mask
})
103 // interfaceMulticastAddrTable returns addresses for a specific
105 func interfaceMulticastAddrTable(ifi
*Interface
) ([]Addr
, error
) {