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.
5
// Network interface identification for Windows
15
func bytePtrToString(p *uint8) string {
16
a := (*[10000]uint8)(unsafe.Pointer(p))
24
func getAdapterList() (*syscall.IpAdapterInfo, os.Error) {
25
b := make([]byte, 1000)
27
a := (*syscall.IpAdapterInfo)(unsafe.Pointer(&b[0]))
28
e := syscall.GetAdaptersInfo(a, &l)
29
if e == syscall.ERROR_BUFFER_OVERFLOW {
31
a = (*syscall.IpAdapterInfo)(unsafe.Pointer(&b[0]))
32
e = syscall.GetAdaptersInfo(a, &l)
35
return nil, os.NewSyscallError("GetAdaptersInfo", e)
40
func getInterfaceList() ([]syscall.InterfaceInfo, os.Error) {
41
s, e := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
43
return nil, os.NewSyscallError("Socket", e)
45
defer syscall.Closesocket(s)
47
ii := [20]syscall.InterfaceInfo{}
49
size := uint32(unsafe.Sizeof(ii))
50
e = syscall.WSAIoctl(s, syscall.SIO_GET_INTERFACE_LIST, nil, 0, (*byte)(unsafe.Pointer(&ii[0])), size, &ret, nil, 0)
52
return nil, os.NewSyscallError("WSAIoctl", e)
54
c := ret / uint32(unsafe.Sizeof(ii[0]))
59
// If the ifindex is zero, interfaceTable returns mappings of all
60
// network interfaces. Otheriwse it returns a mapping of a specific
62
func interfaceTable(ifindex int) ([]Interface, os.Error) {
63
ai, e := getAdapterList()
68
ii, e := getInterfaceList()
74
for ; ai != nil; ai = ai.Next {
76
if ifindex == 0 || ifindex == int(index) {
79
row := syscall.MibIfRow{Index: index}
80
e := syscall.GetIfEntry(&row)
82
return nil, os.NewSyscallError("GetIfEntry", e)
85
for _, ii := range ii {
86
ip := (*syscall.RawSockaddrInet4)(unsafe.Pointer(&ii.Address)).Addr
87
ipv4 := IPv4(ip[0], ip[1], ip[2], ip[3])
88
ipl := &ai.IpAddressList
90
ips := bytePtrToString(&ipl.IpAddress.String[0])
91
if ipv4.Equal(parseIPv4(ips)) {
99
if ii.Flags&syscall.IFF_UP != 0 {
102
if ii.Flags&syscall.IFF_LOOPBACK != 0 {
103
flags |= FlagLoopback
105
if ii.Flags&syscall.IFF_BROADCAST != 0 {
106
flags |= FlagBroadcast
108
if ii.Flags&syscall.IFF_POINTTOPOINT != 0 {
109
flags |= FlagPointToPoint
111
if ii.Flags&syscall.IFF_MULTICAST != 0 {
112
flags |= FlagMulticast
116
name := bytePtrToString(&ai.AdapterName[0])
122
HardwareAddr: HardwareAddr(row.PhysAddr[:row.PhysAddrLen]),
124
ift = append(ift, ifi)
130
// If the ifindex is zero, interfaceAddrTable returns addresses
131
// for all network interfaces. Otherwise it returns addresses
132
// for a specific interface.
133
func interfaceAddrTable(ifindex int) ([]Addr, os.Error) {
134
ai, e := getAdapterList()
140
for ; ai != nil; ai = ai.Next {
142
if ifindex == 0 || ifindex == int(index) {
143
ipl := &ai.IpAddressList
144
for ; ipl != nil; ipl = ipl.Next {
146
ifa.IP = parseIPv4(bytePtrToString(&ipl.IpAddress.String[0]))
147
ifat = append(ifat, ifa.toAddr())