Retro68/gcc/libgo/go/net/interface_darwin.go

63 lines
1.7 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00:00
// Copyright 2011 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.
package net
import (
"os"
"syscall"
)
2014-09-21 17:33:12 +00:00
// interfaceMulticastAddrTable returns addresses for a specific
// interface.
func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) {
tab, err := syscall.RouteRIB(syscall.NET_RT_IFLIST2, ifi.Index)
2012-03-27 23:13:14 +00:00
if err != nil {
2017-04-10 11:32:00 +00:00
return nil, os.NewSyscallError("routerib", err)
2012-03-27 23:13:14 +00:00
}
msgs, err := syscall.ParseRoutingMessage(tab)
if err != nil {
2017-04-10 11:32:00 +00:00
return nil, os.NewSyscallError("parseroutingmessage", err)
2012-03-27 23:13:14 +00:00
}
var ifmat []Addr
for _, m := range msgs {
2014-09-21 17:33:12 +00:00
switch m := m.(type) {
2012-03-27 23:13:14 +00:00
case *syscall.InterfaceMulticastAddrMessage:
2014-09-21 17:33:12 +00:00
if ifi.Index == int(m.Header.Index) {
ifma, err := newMulticastAddr(ifi, m)
2012-03-27 23:13:14 +00:00
if err != nil {
return nil, err
}
2017-04-10 11:32:00 +00:00
if ifma != nil {
ifmat = append(ifmat, ifma)
}
2012-03-27 23:13:14 +00:00
}
}
}
return ifmat, nil
}
2017-04-10 11:32:00 +00:00
func newMulticastAddr(ifi *Interface, m *syscall.InterfaceMulticastAddrMessage) (*IPAddr, error) {
2012-03-27 23:13:14 +00:00
sas, err := syscall.ParseRoutingSockaddr(m)
if err != nil {
2017-04-10 11:32:00 +00:00
return nil, os.NewSyscallError("parseroutingsockaddr", err)
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
switch sa := sas[syscall.RTAX_IFA].(type) {
case *syscall.SockaddrInet4:
return &IPAddr{IP: IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])}, nil
case *syscall.SockaddrInet6:
ifma := IPAddr{IP: make(IP, IPv6len)}
copy(ifma.IP, sa.Addr[:])
// NOTE: KAME based IPv6 protcol stack usually embeds
// the interface index in the interface-local or
// link-local address as the kernel-internal form.
if ifma.IP.IsInterfaceLocalMulticast() || ifma.IP.IsLinkLocalMulticast() {
ifma.IP[2], ifma.IP[3] = 0, 0
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
return &ifma, nil
default:
return nil, nil
2012-03-27 23:13:14 +00:00
}
}