net_windows.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // +build windows
  2. package net
  3. import (
  4. "context"
  5. "errors"
  6. "net"
  7. "os"
  8. "github.com/shirou/gopsutil/internal/common"
  9. "golang.org/x/sys/windows"
  10. )
  11. var (
  12. modiphlpapi = windows.NewLazyDLL("iphlpapi.dll")
  13. procGetExtendedTCPTable = modiphlpapi.NewProc("GetExtendedTcpTable")
  14. procGetExtendedUDPTable = modiphlpapi.NewProc("GetExtendedUdpTable")
  15. )
  16. const (
  17. TCPTableBasicListener = iota
  18. TCPTableBasicConnections
  19. TCPTableBasicAll
  20. TCPTableOwnerPIDListener
  21. TCPTableOwnerPIDConnections
  22. TCPTableOwnerPIDAll
  23. TCPTableOwnerModuleListener
  24. TCPTableOwnerModuleConnections
  25. TCPTableOwnerModuleAll
  26. )
  27. func IOCounters(pernic bool) ([]IOCountersStat, error) {
  28. return IOCountersWithContext(context.Background(), pernic)
  29. }
  30. func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
  31. ifs, err := net.Interfaces()
  32. if err != nil {
  33. return nil, err
  34. }
  35. var ret []IOCountersStat
  36. for _, ifi := range ifs {
  37. c := IOCountersStat{
  38. Name: ifi.Name,
  39. }
  40. row := windows.MibIfRow{Index: uint32(ifi.Index)}
  41. e := windows.GetIfEntry(&row)
  42. if e != nil {
  43. return nil, os.NewSyscallError("GetIfEntry", e)
  44. }
  45. c.BytesSent = uint64(row.OutOctets)
  46. c.BytesRecv = uint64(row.InOctets)
  47. c.PacketsSent = uint64(row.OutUcastPkts)
  48. c.PacketsRecv = uint64(row.InUcastPkts)
  49. c.Errin = uint64(row.InErrors)
  50. c.Errout = uint64(row.OutErrors)
  51. c.Dropin = uint64(row.InDiscards)
  52. c.Dropout = uint64(row.OutDiscards)
  53. ret = append(ret, c)
  54. }
  55. if pernic == false {
  56. return getIOCountersAll(ret)
  57. }
  58. return ret, nil
  59. }
  60. // NetIOCountersByFile is an method which is added just a compatibility for linux.
  61. func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
  62. return IOCountersByFileWithContext(context.Background(), pernic, filename)
  63. }
  64. func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
  65. return IOCounters(pernic)
  66. }
  67. // Return a list of network connections opened by a process
  68. func Connections(kind string) ([]ConnectionStat, error) {
  69. return ConnectionsWithContext(context.Background(), kind)
  70. }
  71. func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
  72. var ret []ConnectionStat
  73. return ret, common.ErrNotImplementedError
  74. }
  75. // Return a list of network connections opened returning at most `max`
  76. // connections for each running process.
  77. func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) {
  78. return ConnectionsMaxWithContext(context.Background(), kind, max)
  79. }
  80. func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) {
  81. return []ConnectionStat{}, common.ErrNotImplementedError
  82. }
  83. func FilterCounters() ([]FilterStat, error) {
  84. return FilterCountersWithContext(context.Background())
  85. }
  86. func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
  87. return nil, errors.New("NetFilterCounters not implemented for windows")
  88. }
  89. // NetProtoCounters returns network statistics for the entire system
  90. // If protocols is empty then all protocols are returned, otherwise
  91. // just the protocols in the list are returned.
  92. // Not Implemented for Windows
  93. func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
  94. return ProtoCountersWithContext(context.Background(), protocols)
  95. }
  96. func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
  97. return nil, errors.New("NetProtoCounters not implemented for windows")
  98. }