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