Source file src/net/interface_darwin.go

     1  // Copyright 2011 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/route"
    11  )
    12  
    13  func interfaceMessages(ifindex int) ([]route.Message, error) {
    14  	rib, err := route.FetchRIB(syscall.AF_UNSPEC, syscall.NET_RT_IFLIST, ifindex)
    15  	if err != nil {
    16  		return nil, err
    17  	}
    18  	return route.ParseRIB(syscall.NET_RT_IFLIST, rib)
    19  }
    20  
    21  // interfaceMulticastAddrTable returns addresses for a specific
    22  // interface.
    23  func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) {
    24  	rib, err := route.FetchRIB(syscall.AF_UNSPEC, syscall.NET_RT_IFLIST2, ifi.Index)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	msgs, err := route.ParseRIB(syscall.NET_RT_IFLIST2, rib)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	ifmat := make([]Addr, 0, len(msgs))
    33  	for _, m := range msgs {
    34  		switch m := m.(type) {
    35  		case *route.InterfaceMulticastAddrMessage:
    36  			if ifi.Index != m.Index {
    37  				continue
    38  			}
    39  			var ip IP
    40  			switch sa := m.Addrs[syscall.RTAX_IFA].(type) {
    41  			case *route.Inet4Addr:
    42  				ip = IPv4(sa.IP[0], sa.IP[1], sa.IP[2], sa.IP[3])
    43  			case *route.Inet6Addr:
    44  				ip = make(IP, IPv6len)
    45  				copy(ip, sa.IP[:])
    46  			}
    47  			if ip != nil {
    48  				ifmat = append(ifmat, &IPAddr{IP: ip})
    49  			}
    50  		}
    51  	}
    52  	return ifmat, nil
    53  }
    54  

View as plain text