mirror of
https://github.com/mjl-/mox.git
synced 2025-06-28 22:58:15 +03:00
45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
// 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 adns
|
|
|
|
import (
|
|
"os"
|
|
"syscall"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
// adapterAddresses returns a list of IP adapter and address
|
|
// structures. The structure contains an IP adapter and flattened
|
|
// multiple IP addresses including unicast, anycast and multicast
|
|
// addresses.
|
|
func adapterAddresses() ([]*windows.IpAdapterAddresses, error) {
|
|
var b []byte
|
|
l := uint32(15000) // recommended initial size
|
|
for {
|
|
b = make([]byte, l)
|
|
const flags = windows.GAA_FLAG_INCLUDE_PREFIX | windows.GAA_FLAG_INCLUDE_GATEWAYS
|
|
err := windows.GetAdaptersAddresses(syscall.AF_UNSPEC, flags, 0, (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])), &l)
|
|
if err == nil {
|
|
if l == 0 {
|
|
return nil, nil
|
|
}
|
|
break
|
|
}
|
|
if err.(syscall.Errno) != syscall.ERROR_BUFFER_OVERFLOW {
|
|
return nil, os.NewSyscallError("getadaptersaddresses", err)
|
|
}
|
|
if l <= uint32(len(b)) {
|
|
return nil, os.NewSyscallError("getadaptersaddresses", err)
|
|
}
|
|
}
|
|
var aas []*windows.IpAdapterAddresses
|
|
for aa := (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])); aa != nil; aa = aa.Next {
|
|
aas = append(aas, aa)
|
|
}
|
|
return aas, nil
|
|
}
|