Source file src/net/interface_solaris.go

     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.
     4  
     5  package net
     6  
     7  import (
     8  	"syscall"
     9  
    10  	"golang.org/x/net/lif"
    11  )
    12  
    13  // If the ifindex is zero, interfaceTable returns mappings of all
    14  // network interfaces. Otherwise it returns a mapping of a specific
    15  // interface.
    16  func interfaceTable(ifindex int) ([]Interface, error) {
    17  	lls, err := lif.Links(syscall.AF_UNSPEC, "")
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	var ift []Interface
    22  	for _, ll := range lls {
    23  		if ifindex != 0 && ifindex != ll.Index {
    24  			continue
    25  		}
    26  		ifi := Interface{Index: ll.Index, MTU: ll.MTU, Name: ll.Name, Flags: linkFlags(ll.Flags)}
    27  		if len(ll.Addr) > 0 {
    28  			ifi.HardwareAddr = HardwareAddr(ll.Addr)
    29  		}
    30  		ift = append(ift, ifi)
    31  	}
    32  	return ift, nil
    33  }
    34  
    35  func linkFlags(rawFlags int) Flags {
    36  	var f Flags
    37  	if rawFlags&syscall.IFF_UP != 0 {
    38  		f |= FlagUp
    39  	}
    40  	if rawFlags&syscall.IFF_BROADCAST != 0 {
    41  		f |= FlagBroadcast
    42  	}
    43  	if rawFlags&syscall.IFF_LOOPBACK != 0 {
    44  		f |= FlagLoopback
    45  	}
    46  	if rawFlags&syscall.IFF_POINTOPOINT != 0 {
    47  		f |= FlagPointToPoint
    48  	}
    49  	if rawFlags&syscall.IFF_MULTICAST != 0 {
    50  		f |= FlagMulticast
    51  	}
    52  	return f
    53  }
    54  
    55  // If the ifi is nil, interfaceAddrTable returns addresses for all
    56  // network interfaces. Otherwise it returns addresses for a specific
    57  // interface.
    58  func interfaceAddrTable(ifi *Interface) ([]Addr, error) {
    59  	var name string
    60  	if ifi != nil {
    61  		name = ifi.Name
    62  	}
    63  	as, err := lif.Addrs(syscall.AF_UNSPEC, name)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	var ifat []Addr
    68  	for _, a := range as {
    69  		var ip IP
    70  		var mask IPMask
    71  		switch a := a.(type) {
    72  		case *lif.Inet4Addr:
    73  			ip = IPv4(a.IP[0], a.IP[1], a.IP[2], a.IP[3])
    74  			mask = CIDRMask(a.PrefixLen, 8*IPv4len)
    75  		case *lif.Inet6Addr:
    76  			ip = make(IP, IPv6len)
    77  			copy(ip, a.IP[:])
    78  			mask = CIDRMask(a.PrefixLen, 8*IPv6len)
    79  		}
    80  		ifat = append(ifat, &IPNet{IP: ip, Mask: mask})
    81  	}
    82  	return ifat, nil
    83  }
    84  
    85  // interfaceMulticastAddrTable returns addresses for a specific
    86  // interface.
    87  func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) {
    88  	return nil, nil
    89  }
    90  

View as plain text