1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-28 13:48:49 +08:00
shirou_gopsutil/net/net_windows.go

117 lines
2.9 KiB
Go
Raw Normal View History

2014-05-12 12:10:28 +09:00
// +build windows
2014-12-30 22:09:05 +09:00
package net
2014-05-12 12:10:28 +09:00
import (
2015-11-19 14:19:43 -07:00
"errors"
2014-05-16 11:33:35 +09:00
"net"
"os"
"syscall"
"unsafe"
2015-10-20 00:04:57 +09:00
"github.com/shirou/gopsutil/internal/common"
2014-05-12 12:10:28 +09:00
)
var (
modiphlpapi = syscall.NewLazyDLL("iphlpapi.dll")
procGetExtendedTcpTable = modiphlpapi.NewProc("GetExtendedTcpTable")
procGetExtendedUdpTable = modiphlpapi.NewProc("GetExtendedUdpTable")
)
const (
2014-08-15 20:05:26 +02:00
TCPTableBasicListener = iota
TCPTableBasicConnections
TCPTableBasicAll
TCPTableOwnerPIDListener
TCPTableOwnerPIDConnections
TCPTableOwnerPIDAll
TCPTableOwnerModuleListener
TCPTableOwnerModuleConnections
TCPTableOwnerModuleAll
)
2014-05-12 12:10:28 +09:00
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
2014-05-16 11:33:35 +09:00
ifs, err := net.Interfaces()
if err != nil {
return nil, err
}
ai, err := getAdapterList()
if err != nil {
return nil, err
}
var ret []NetIOCountersStat
for _, ifi := range ifs {
name := ifi.Name
for ; ai != nil; ai = ai.Next {
2014-11-27 22:28:05 +09:00
name = common.BytePtrToString(&ai.Description[0])
2014-05-16 11:33:35 +09:00
c := NetIOCountersStat{
Name: name,
}
row := syscall.MibIfRow{Index: ai.Index}
e := syscall.GetIfEntry(&row)
if e != nil {
return nil, os.NewSyscallError("GetIfEntry", e)
}
c.BytesSent = uint64(row.OutOctets)
c.BytesRecv = uint64(row.InOctets)
c.PacketsSent = uint64(row.OutUcastPkts)
c.PacketsRecv = uint64(row.InUcastPkts)
c.Errin = uint64(row.InErrors)
c.Errout = uint64(row.OutErrors)
c.Dropin = uint64(row.InDiscards)
c.Dropout = uint64(row.OutDiscards)
ret = append(ret, c)
}
}
2015-01-01 21:29:25 +09:00
if pernic == false {
return getNetIOCountersAll(ret)
}
2014-05-16 11:33:35 +09:00
return ret, nil
}
// NetIOCountersByFile is an method which is added just a compatibility for linux.
func NetIOCountersByFile(pernic bool, filename string) ([]NetIOCountersStat, error) {
return NetIOCounters(pernic)
}
// Return a list of network connections opened by a process
func NetConnections(kind string) ([]NetConnectionStat, error) {
var ret []NetConnectionStat
return ret, common.NotImplementedError
}
2014-05-16 11:33:35 +09:00
// borrowed from src/pkg/net/interface_windows.go
func getAdapterList() (*syscall.IpAdapterInfo, error) {
b := make([]byte, 1000)
l := uint32(len(b))
a := (*syscall.IpAdapterInfo)(unsafe.Pointer(&b[0]))
err := syscall.GetAdaptersInfo(a, &l)
if err == syscall.ERROR_BUFFER_OVERFLOW {
b = make([]byte, l)
a = (*syscall.IpAdapterInfo)(unsafe.Pointer(&b[0]))
err = syscall.GetAdaptersInfo(a, &l)
}
if err != nil {
return nil, os.NewSyscallError("GetAdaptersInfo", err)
}
return a, nil
2014-05-12 12:10:28 +09:00
}
2015-11-19 14:19:43 -07:00
func NetFilterCounters() ([]NetFilterStat, error) {
return nil, errors.New("NetFilterCounters not implemented for windows")
}
2015-11-19 14:19:43 -07:00
// NetProtoCounters returns network statistics for the entire system
// If protocols is empty then all protocols are returned, otherwise
// just the protocols in the list are returned.
// Not Implemented for Windows
func NetProtoCounters(protocols []string) ([]NetProtoCountersStat, error) {
return nil, errors.New("NetProtoCounters not implemented for windows")
}