| 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 |
"internal/routebsd" |
| 9 |
"syscall" |
| 10 |
) |
| 11 |
|
| 12 |
func interfaceMessages(ifindex int) ([]routebsd.Message, error) { |
| 13 |
return routebsd.FetchRIBMessages(syscall.NET_RT_IFLIST, ifindex) |
| 14 |
} |
| 15 |
|
| 16 |
// interfaceMulticastAddrTable returns addresses for a specific |
| 17 |
// interface. |
| 18 |
func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) { |
| 19 |
msgs, err := routebsd.FetchRIBMessages(syscall.NET_RT_IFMALIST, ifi.Index) |
| 20 |
if err != nil { |
| 21 |
return nil, err |
| 22 |
} |
| 23 |
ifmat := make([]Addr, 0, len(msgs)) |
| 24 |
for _, m := range msgs { |
| 25 |
switch m := m.(type) { |
| 26 |
case *routebsd.InterfaceMulticastAddrMessage: |
| 27 |
if ifi.Index != m.Index { |
| 28 |
continue |
| 29 |
} |
| 30 |
var ip IP |
| 31 |
switch sa := m.Addrs[syscall.RTAX_IFA].(type) { |
| 32 |
case *routebsd.InetAddr: |
| 33 |
if sa.IP.Is4() { |
| 34 |
a := sa.IP.As4() |
| 35 |
ip = IPv4(a[0], a[1], a[2], a[3]) |
| 36 |
} else if sa.IP.Is6() { |
| 37 |
a := sa.IP.As16() |
| 38 |
ip = make(IP, IPv6len) |
| 39 |
copy(ip, a[:]) |
| 40 |
} |
| 41 |
} |
| 42 |
if ip != nil { |
| 43 |
ifmat = append(ifmat, &IPAddr{IP: ip}) |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
return ifmat, nil |
| 48 |
} |