2014-05-12 11:10:28 +08:00
|
|
|
// +build windows
|
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package net
|
2014-05-12 11:10:28 +08:00
|
|
|
|
|
|
|
import (
|
2015-11-20 05:19:43 +08:00
|
|
|
"errors"
|
2014-05-16 10:33:35 +08:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
2014-11-27 09:18:15 +08:00
|
|
|
|
2015-10-19 23:04:57 +08:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2014-05-12 11:10:28 +08:00
|
|
|
)
|
|
|
|
|
2014-05-16 11:21:17 +08:00
|
|
|
var (
|
2014-05-16 15:59:46 +08:00
|
|
|
modiphlpapi = syscall.NewLazyDLL("iphlpapi.dll")
|
2016-04-01 20:34:39 +08:00
|
|
|
procGetExtendedTCPTable = modiphlpapi.NewProc("GetExtendedTcpTable")
|
|
|
|
procGetExtendedUDPTable = modiphlpapi.NewProc("GetExtendedUdpTable")
|
2014-05-16 11:21:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-08-16 02:05:26 +08:00
|
|
|
TCPTableBasicListener = iota
|
|
|
|
TCPTableBasicConnections
|
|
|
|
TCPTableBasicAll
|
|
|
|
TCPTableOwnerPIDListener
|
|
|
|
TCPTableOwnerPIDConnections
|
|
|
|
TCPTableOwnerPIDAll
|
|
|
|
TCPTableOwnerModuleListener
|
|
|
|
TCPTableOwnerModuleConnections
|
|
|
|
TCPTableOwnerModuleAll
|
2014-05-16 11:21:17 +08:00
|
|
|
)
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func IOCounters(pernic bool) ([]IOCountersStat, error) {
|
2014-05-16 10:33:35 +08:00
|
|
|
ifs, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-22 22:09:12 +08:00
|
|
|
var ret []IOCountersStat
|
2014-05-16 10:33:35 +08:00
|
|
|
|
|
|
|
for _, ifi := range ifs {
|
2016-10-25 22:44:40 +08:00
|
|
|
c := IOCountersStat{
|
|
|
|
Name: ifi.Name,
|
|
|
|
}
|
2014-05-16 10:33:35 +08:00
|
|
|
|
2016-10-25 22:44:40 +08:00
|
|
|
row := syscall.MibIfRow{Index: uint32(ifi.Index)}
|
|
|
|
e := syscall.GetIfEntry(&row)
|
|
|
|
if e != nil {
|
|
|
|
return nil, os.NewSyscallError("GetIfEntry", e)
|
2014-05-16 10:33:35 +08:00
|
|
|
}
|
2016-10-25 22:44:40 +08:00
|
|
|
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)
|
2014-05-16 10:33:35 +08:00
|
|
|
}
|
2015-01-01 20:29:25 +08:00
|
|
|
|
|
|
|
if pernic == false {
|
2016-03-22 22:09:12 +08:00
|
|
|
return getIOCountersAll(ret)
|
2015-01-01 20:29:25 +08:00
|
|
|
}
|
2014-05-16 10:33:35 +08:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2016-01-21 16:31:05 +08:00
|
|
|
// NetIOCountersByFile is an method which is added just a compatibility for linux.
|
2016-03-22 22:09:12 +08:00
|
|
|
func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
|
|
|
|
return IOCounters(pernic)
|
2016-01-21 16:31:05 +08:00
|
|
|
}
|
|
|
|
|
2014-05-16 11:21:17 +08:00
|
|
|
// Return a list of network connections opened by a process
|
2016-03-22 22:09:12 +08:00
|
|
|
func Connections(kind string) ([]ConnectionStat, error) {
|
|
|
|
var ret []ConnectionStat
|
2014-05-16 11:21:17 +08:00
|
|
|
|
2016-04-01 20:34:39 +08:00
|
|
|
return ret, common.ErrNotImplementedError
|
2014-05-16 11:21:17 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func FilterCounters() ([]FilterStat, error) {
|
2016-01-21 16:31:05 +08:00
|
|
|
return nil, errors.New("NetFilterCounters not implemented for windows")
|
|
|
|
}
|
|
|
|
|
2015-11-20 05:19:43 +08: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-03-22 22:09:12 +08:00
|
|
|
func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
|
2015-11-20 05:19:43 +08:00
|
|
|
return nil, errors.New("NetProtoCounters not implemented for windows")
|
|
|
|
}
|