// 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 (
	
	
	
	
)

// BUG(mikio): On JS, methods and functions related to
// Interface are not implemented.

// BUG(mikio): On AIX, DragonFly BSD, NetBSD, OpenBSD, Plan 9 and
// Solaris, the MulticastAddrs method of Interface is not implemented.

var (
	errInvalidInterface         = errors.New("invalid network interface")
	errInvalidInterfaceIndex    = errors.New("invalid network interface index")
	errInvalidInterfaceName     = errors.New("invalid network interface name")
	errNoSuchInterface          = errors.New("no such network interface")
	errNoSuchMulticastInterface = errors.New("no such multicast network interface")
)

// Interface represents a mapping between network interface name
// and index. It also represents network interface facility
// information.
type Interface struct {
	Index        int          // positive integer that starts at one, zero is never used
	MTU          int          // maximum transmission unit
	Name         string       // e.g., "en0", "lo0", "eth0.100"
	HardwareAddr HardwareAddr // IEEE MAC-48, EUI-48 and EUI-64 form
	Flags        Flags        // e.g., FlagUp, FlagLoopback, FlagMulticast
}

type Flags uint

const (
	FlagUp           Flags = 1 << iota // interface is administratively up
	FlagBroadcast                      // interface supports broadcast access capability
	FlagLoopback                       // interface is a loopback interface
	FlagPointToPoint                   // interface belongs to a point-to-point link
	FlagMulticast                      // interface supports multicast access capability
	FlagRunning                        // interface is in running state
)

var flagNames = []string{
	"up",
	"broadcast",
	"loopback",
	"pointtopoint",
	"multicast",
	"running",
}

func ( Flags) () string {
	 := ""
	for ,  := range flagNames {
		if &(1<<uint()) != 0 {
			if  != "" {
				 += "|"
			}
			 += 
		}
	}
	if  == "" {
		 = "0"
	}
	return 
}

// Addrs returns a list of unicast interface addresses for a specific
// interface.
func ( *Interface) () ([]Addr, error) {
	if  == nil {
		return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterface}
	}
	,  := interfaceAddrTable()
	if  != nil {
		 = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: }
	}
	return , 
}

// MulticastAddrs returns a list of multicast, joined group addresses
// for a specific interface.
func ( *Interface) () ([]Addr, error) {
	if  == nil {
		return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterface}
	}
	,  := interfaceMulticastAddrTable()
	if  != nil {
		 = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: }
	}
	return , 
}

// Interfaces returns a list of the system's network interfaces.
func () ([]Interface, error) {
	,  := interfaceTable(0)
	if  != nil {
		return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: }
	}
	if len() != 0 {
		zoneCache.update(, false)
	}
	return , nil
}

// InterfaceAddrs returns a list of the system's unicast interface
// addresses.
//
// The returned list does not identify the associated interface; use
// Interfaces and Interface.Addrs for more detail.
func () ([]Addr, error) {
	,  := interfaceAddrTable(nil)
	if  != nil {
		 = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: }
	}
	return , 
}

// InterfaceByIndex returns the interface specified by index.
//
// On Solaris, it returns one of the logical network interfaces
// sharing the logical data link; for more precision use
// InterfaceByName.
func ( int) (*Interface, error) {
	if  <= 0 {
		return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterfaceIndex}
	}
	,  := interfaceTable()
	if  != nil {
		return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: }
	}
	,  := interfaceByIndex(, )
	if  != nil {
		 = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: }
	}
	return , 
}

func ( []Interface,  int) (*Interface, error) {
	for ,  := range  {
		if  == .Index {
			return &, nil
		}
	}
	return nil, errNoSuchInterface
}

// InterfaceByName returns the interface specified by name.
func ( string) (*Interface, error) {
	if  == "" {
		return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterfaceName}
	}
	,  := interfaceTable(0)
	if  != nil {
		return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: }
	}
	if len() != 0 {
		zoneCache.update(, false)
	}
	for ,  := range  {
		if  == .Name {
			return &, nil
		}
	}
	return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errNoSuchInterface}
}

// An ipv6ZoneCache represents a cache holding partial network
// interface information. It is used for reducing the cost of IPv6
// addressing scope zone resolution.
//
// Multiple names sharing the index are managed by first-come
// first-served basis for consistency.
type ipv6ZoneCache struct {
	sync.RWMutex                // guard the following
	lastFetched  time.Time      // last time routing information was fetched
	toIndex      map[string]int // interface name to its index
	toName       map[int]string // interface index to its name
}

var zoneCache = ipv6ZoneCache{
	toIndex: make(map[string]int),
	toName:  make(map[int]string),
}

// update refreshes the network interface information if the cache was last
// updated more than 1 minute ago, or if force is set. It reports whether the
// cache was updated.
func ( *ipv6ZoneCache) ( []Interface,  bool) ( bool) {
	.Lock()
	defer .Unlock()
	 := time.Now()
	if ! && .lastFetched.After(.Add(-60*time.Second)) {
		return false
	}
	.lastFetched = 
	if len() == 0 {
		var  error
		if ,  = interfaceTable(0);  != nil {
			return false
		}
	}
	.toIndex = make(map[string]int, len())
	.toName = make(map[int]string, len())
	for ,  := range  {
		.toIndex[.Name] = .Index
		if ,  := .toName[.Index]; ! {
			.toName[.Index] = .Name
		}
	}
	return true
}

func ( *ipv6ZoneCache) ( int) string {
	if  == 0 {
		return ""
	}
	 := zoneCache.update(nil, false)
	zoneCache.RLock()
	,  := zoneCache.toName[]
	zoneCache.RUnlock()
	if ! && ! {
		zoneCache.update(nil, true)
		zoneCache.RLock()
		,  = zoneCache.toName[]
		zoneCache.RUnlock()
	}
	if ! { // last resort
		 = itoa.Uitoa(uint())
	}
	return 
}

func ( *ipv6ZoneCache) ( string) int {
	if  == "" {
		return 0
	}
	 := zoneCache.update(nil, false)
	zoneCache.RLock()
	,  := zoneCache.toIndex[]
	zoneCache.RUnlock()
	if ! && ! {
		zoneCache.update(nil, true)
		zoneCache.RLock()
		,  = zoneCache.toIndex[]
		zoneCache.RUnlock()
	}
	if ! { // last resort
		, _, _ = dtoi()
	}
	return 
}