Home | History | Annotate | Download | only in net
      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 const (
     36 	sysIFF_UP          = 0x1
     37 	sysIFF_BROADCAST   = 0x2
     38 	sysIFF_DEBUG       = 0x4
     39 	sysIFF_LOOPBACK    = 0x8
     40 	sysIFF_POINTOPOINT = 0x10
     41 	sysIFF_NOTRAILERS  = 0x20
     42 	sysIFF_RUNNING     = 0x40
     43 	sysIFF_NOARP       = 0x80
     44 	sysIFF_PROMISC     = 0x100
     45 	sysIFF_ALLMULTI    = 0x200
     46 	sysIFF_INTELLIGENT = 0x400
     47 	sysIFF_MULTICAST   = 0x800
     48 	sysIFF_MULTI_BCAST = 0x1000
     49 	sysIFF_UNNUMBERED  = 0x2000
     50 	sysIFF_PRIVATE     = 0x8000
     51 )
     52 
     53 func linkFlags(rawFlags int) Flags {
     54 	var f Flags
     55 	if rawFlags&sysIFF_UP != 0 {
     56 		f |= FlagUp
     57 	}
     58 	if rawFlags&sysIFF_BROADCAST != 0 {
     59 		f |= FlagBroadcast
     60 	}
     61 	if rawFlags&sysIFF_LOOPBACK != 0 {
     62 		f |= FlagLoopback
     63 	}
     64 	if rawFlags&sysIFF_POINTOPOINT != 0 {
     65 		f |= FlagPointToPoint
     66 	}
     67 	if rawFlags&sysIFF_MULTICAST != 0 {
     68 		f |= FlagMulticast
     69 	}
     70 	return f
     71 }
     72 
     73 // If the ifi is nil, interfaceAddrTable returns addresses for all
     74 // network interfaces. Otherwise it returns addresses for a specific
     75 // interface.
     76 func interfaceAddrTable(ifi *Interface) ([]Addr, error) {
     77 	var name string
     78 	if ifi != nil {
     79 		name = ifi.Name
     80 	}
     81 	as, err := lif.Addrs(syscall.AF_UNSPEC, name)
     82 	if err != nil {
     83 		return nil, err
     84 	}
     85 	var ifat []Addr
     86 	for _, a := range as {
     87 		var ip IP
     88 		var mask IPMask
     89 		switch a := a.(type) {
     90 		case *lif.Inet4Addr:
     91 			ip = IPv4(a.IP[0], a.IP[1], a.IP[2], a.IP[3])
     92 			mask = CIDRMask(a.PrefixLen, 8*IPv4len)
     93 		case *lif.Inet6Addr:
     94 			ip = make(IP, IPv6len)
     95 			copy(ip, a.IP[:])
     96 			mask = CIDRMask(a.PrefixLen, 8*IPv6len)
     97 		}
     98 		ifat = append(ifat, &IPNet{IP: ip, Mask: mask})
     99 	}
    100 	return ifat, nil
    101 }
    102 
    103 // interfaceMulticastAddrTable returns addresses for a specific
    104 // interface.
    105 func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) {
    106 	return nil, nil
    107 }
    108