123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package net
- import (
- "context"
- "errors"
- "net"
- "os"
- "github.com/shirou/gopsutil/internal/common"
- "golang.org/x/sys/windows"
- )
- var (
- modiphlpapi = windows.NewLazyDLL("iphlpapi.dll")
- procGetExtendedTCPTable = modiphlpapi.NewProc("GetExtendedTcpTable")
- procGetExtendedUDPTable = modiphlpapi.NewProc("GetExtendedUdpTable")
- )
- const (
- TCPTableBasicListener = iota
- TCPTableBasicConnections
- TCPTableBasicAll
- TCPTableOwnerPIDListener
- TCPTableOwnerPIDConnections
- TCPTableOwnerPIDAll
- TCPTableOwnerModuleListener
- TCPTableOwnerModuleConnections
- TCPTableOwnerModuleAll
- )
- func IOCounters(pernic bool) ([]IOCountersStat, error) {
- return IOCountersWithContext(context.Background(), pernic)
- }
- func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
- ifs, err := net.Interfaces()
- if err != nil {
- return nil, err
- }
- var ret []IOCountersStat
- for _, ifi := range ifs {
- c := IOCountersStat{
- Name: ifi.Name,
- }
- row := windows.MibIfRow{Index: uint32(ifi.Index)}
- e := windows.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)
- }
- if pernic == false {
- return getIOCountersAll(ret)
- }
- return ret, nil
- }
- func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
- return IOCountersByFileWithContext(context.Background(), pernic, filename)
- }
- func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
- return IOCounters(pernic)
- }
- func Connections(kind string) ([]ConnectionStat, error) {
- return ConnectionsWithContext(context.Background(), kind)
- }
- func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
- var ret []ConnectionStat
- return ret, common.ErrNotImplementedError
- }
- func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) {
- return ConnectionsMaxWithContext(context.Background(), kind, max)
- }
- func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) {
- return []ConnectionStat{}, common.ErrNotImplementedError
- }
- func FilterCounters() ([]FilterStat, error) {
- return FilterCountersWithContext(context.Background())
- }
- func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
- return nil, errors.New("NetFilterCounters not implemented for windows")
- }
- func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
- return ProtoCountersWithContext(context.Background(), protocols)
- }
- func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
- return nil, errors.New("NetProtoCounters not implemented for windows")
- }
|