Skip to content

Commit efa1aff

Browse files
committed
remove vendor and add iphlpapi.go to implement net windows api
1 parent 3e24041 commit efa1aff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+485
-55820
lines changed

Gopkg.lock

Lines changed: 0 additions & 15 deletions
This file was deleted.

Gopkg.toml

Lines changed: 0 additions & 34 deletions
This file was deleted.

common.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,17 @@ import (
66
"fmt"
77
"net"
88
"syscall"
9-
10-
"github.com/kbinani/win"
119
)
1210

13-
// ErrInsufficientBuffer windows api ERROR_INSUFFICIENT_BUFFER
14-
const ErrInsufficientBuffer = 122
15-
16-
func decodePort(port win.DWORD) uint16 {
11+
func decodePort(port uint32) uint16 {
1712
return syscall.Ntohs(uint16(port))
1813
}
1914

20-
func parseIPv4(addr win.DWORD) string {
15+
func parseIPv4(addr uint32) string {
2116
return fmt.Sprintf("%d.%d.%d.%d", addr&255, addr>>8&255, addr>>16&255, addr>>24&255)
2217
}
2318

24-
func parseIPv6(addr [16]win.UCHAR) string {
19+
func parseIPv6(addr [16]byte) string {
2520
var ret [16]byte
2621
for i := 0; i < 16; i++ {
2722
ret[i] = uint8(addr[i])
@@ -33,7 +28,7 @@ func parseIPv6(addr [16]win.UCHAR) string {
3328
}
3429

3530
// TCPStatuses https://msdn.microsoft.com/en-us/library/windows/desktop/bb485761(v=vs.85).aspx
36-
var TCPStatuses = map[win.MIB_TCP_STATE]string{
31+
var TCPStatuses = map[MIB_TCP_STATE]string{
3732
1: "CLOSED",
3833
2: "LISTEN",
3934
3: "SYN_SENT",

iphlpapi.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// +build windows
2+
3+
package winnetstat
4+
5+
import (
6+
"syscall"
7+
"unsafe"
8+
9+
"golang.org/x/sys/windows"
10+
)
11+
12+
var (
13+
// Library
14+
modiphlpapi = windows.NewLazySystemDLL("iphlpapi.dll")
15+
16+
// Functions
17+
procGetTCPTable2 = modiphlpapi.NewProc("GetTcpTable2")
18+
procGetTCP6Table2 = modiphlpapi.NewProc("GetTcp6Table2")
19+
procGetExtendedTCPTable = modiphlpapi.NewProc("GetExtendedTcpTable")
20+
procGetExtendedUDPTable = modiphlpapi.NewProc("GetExtendedUdpTable")
21+
)
22+
23+
func getUintptrFromBool(b bool) uintptr {
24+
if b {
25+
return 1
26+
}
27+
return 0
28+
}
29+
30+
func GetTcpTable2(tcpTable PMIB_TCPTABLE2, bufSize *uint32, order bool) (errcode error) {
31+
r1, _, _ := syscall.Syscall(procGetTCPTable2.Addr(), 3, uintptr(unsafe.Pointer(tcpTable)), uintptr(unsafe.Pointer(bufSize)), getUintptrFromBool(order))
32+
if r1 != 0 {
33+
errcode = syscall.Errno(r1)
34+
}
35+
return
36+
}
37+
38+
func GetTcp6Table2(tcpTable PMIB_TCP6TABLE2, bufSize *uint32, order bool) (errcode error) {
39+
r1, _, _ := syscall.Syscall(procGetTCP6Table2.Addr(), 3, uintptr(unsafe.Pointer(tcpTable)), uintptr(unsafe.Pointer(bufSize)), getUintptrFromBool(order))
40+
if r1 != 0 {
41+
errcode = syscall.Errno(r1)
42+
}
43+
return
44+
}
45+
46+
func GetExtendedUdpTable(pUdpTable uintptr, pdwSize *uint32, bOrder bool, ulAf uint32, tableClass UDP_TABLE_CLASS, reserved uint32) (errcode error) {
47+
r1, _, _ := syscall.Syscall6(procGetExtendedUDPTable.Addr(), 6, pUdpTable, uintptr(unsafe.Pointer(pdwSize)), getUintptrFromBool(bOrder), uintptr(ulAf), uintptr(tableClass), uintptr(reserved))
48+
if r1 != 0 {
49+
errcode = syscall.Errno(r1)
50+
}
51+
return
52+
}
53+
54+
func GetExtendedTcpTable(pTcpTable uintptr, pdwSize *uint32, bOrder bool, ulAf uint32, tableClass TCP_TABLE_CLASS, reserved uint32) (errcode error) {
55+
r1, _, _ := syscall.Syscall6(procGetExtendedTCPTable.Addr(), 6, pTcpTable, uintptr(unsafe.Pointer(pdwSize)), getUintptrFromBool(bOrder), uintptr(ulAf), uintptr(tableClass), uintptr(reserved))
56+
if r1 != 0 {
57+
errcode = syscall.Errno(r1)
58+
}
59+
return
60+
}

netstat.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,4 @@ func getNetStatWithKindFile(filename string) ([]NetStat, error) {
119119
default:
120120
return nil, fmt.Errorf("invalid kind filename: %s", filename)
121121
}
122-
123-
return nil, nil
124122
}

0 commit comments

Comments
 (0)