2016-02-12 18:02:16 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2018-03-06 00:52:43 +00:00
|
|
|
"context"
|
2016-02-12 18:02:16 +00:00
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2017-07-28 16:46:44 +00:00
|
|
|
"golang.org/x/sys/windows"
|
2016-02-12 18:02:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-07-28 16:46:44 +00:00
|
|
|
modiphlpapi = windows.NewLazyDLL("iphlpapi.dll")
|
2016-05-09 15:19:04 +00:00
|
|
|
procGetExtendedTCPTable = modiphlpapi.NewProc("GetExtendedTcpTable")
|
|
|
|
procGetExtendedUDPTable = modiphlpapi.NewProc("GetExtendedUdpTable")
|
2016-02-12 18:02:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TCPTableBasicListener = iota
|
|
|
|
TCPTableBasicConnections
|
|
|
|
TCPTableBasicAll
|
|
|
|
TCPTableOwnerPIDListener
|
|
|
|
TCPTableOwnerPIDConnections
|
|
|
|
TCPTableOwnerPIDAll
|
|
|
|
TCPTableOwnerModuleListener
|
|
|
|
TCPTableOwnerModuleConnections
|
|
|
|
TCPTableOwnerModuleAll
|
|
|
|
)
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
func IOCounters(pernic bool) ([]IOCountersStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return IOCountersWithContext(context.Background(), pernic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
|
2016-02-12 18:02:16 +00:00
|
|
|
ifs, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-09 15:19:04 +00:00
|
|
|
var ret []IOCountersStat
|
2016-02-12 18:02:16 +00:00
|
|
|
|
|
|
|
for _, ifi := range ifs {
|
2017-02-02 06:41:29 +00:00
|
|
|
c := IOCountersStat{
|
|
|
|
Name: ifi.Name,
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2017-02-02 06:41:29 +00:00
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
row := windows.MibIfRow{Index: uint32(ifi.Index)}
|
|
|
|
e := windows.GetIfEntry(&row)
|
2017-02-02 06:41:29 +00:00
|
|
|
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)
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if pernic == false {
|
2016-05-09 15:19:04 +00:00
|
|
|
return getIOCountersAll(ret)
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
// NetIOCountersByFile is an method which is added just a compatibility for linux.
|
|
|
|
func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return IOCountersByFileWithContext(context.Background(), pernic, filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
return IOCounters(pernic)
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
// Return a list of network connections opened by a process
|
2016-05-09 15:19:04 +00:00
|
|
|
func Connections(kind string) ([]ConnectionStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return ConnectionsWithContext(context.Background(), kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
var ret []ConnectionStat
|
2016-02-12 18:02:16 +00:00
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
return ret, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 06:41:29 +00:00
|
|
|
// Return a list of network connections opened returning at most `max`
|
|
|
|
// connections for each running process.
|
|
|
|
func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return ConnectionsMaxWithContext(context.Background(), kind, max)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) {
|
2017-02-02 06:41:29 +00:00
|
|
|
return []ConnectionStat{}, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
func FilterCounters() ([]FilterStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return FilterCountersWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
return nil, errors.New("NetFilterCounters not implemented for windows")
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:02:16 +00: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
|
2016-05-09 15:19:04 +00:00
|
|
|
func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return ProtoCountersWithContext(context.Background(), protocols)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
|
2016-02-12 18:02:16 +00:00
|
|
|
return nil, errors.New("NetProtoCounters not implemented for windows")
|
|
|
|
}
|