This repository has been archived by the owner on Jun 20, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
iphelper_windows.go
230 lines (205 loc) · 5.39 KB
/
iphelper_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//go:build windows
package filter
import (
"errors"
"net"
"unsafe"
"golang.org/x/sys/windows"
)
var (
iphlpapi = windows.MustLoadDLL("iphlpapi.dll")
getExtendedTcpTable = iphlpapi.MustFindProc("GetExtendedTcpTable")
getExtendedUdpTable = iphlpapi.MustFindProc("GetExtendedUdpTable")
getBestInterfaceEx = iphlpapi.MustFindProc("GetBestInterfaceEx")
)
// GetTCPTable is ...
func GetTCPTable(buf []byte) ([]TCPRow, error) {
b, err := GetExtendedTcpTable(0, windows.AF_INET, 4 /* TCP_TABLE_OWNER_PID_CONNECTIONS */, buf)
if err != nil {
return nil, err
}
t := (*TCPTable)(unsafe.Pointer(&b[0]))
return unsafe.Slice((*TCPRow)(unsafe.Pointer(&t.Table)), t.Len), nil
}
// TCPTable is ...
type TCPTable struct {
Len uint32
Table [1]TCPRow
}
// TCPRow is ...
type TCPRow struct {
State uint32
LocalAddr uint32
LocalPort uint32
RemoteAddr uint32
RemotePort uint32
OwningPid uint32
}
// GetTCP6Table is ...
func GetTCP6Table(buf []byte) ([]TCP6Row, error) {
b, err := GetExtendedTcpTable(0, windows.AF_INET6, 4 /* TCP_TABLE_OWNER_PID_CONNECTIONS */, buf)
if err != nil {
return nil, err
}
t := (*TCP6Table)(unsafe.Pointer(&b[0]))
return unsafe.Slice((*TCP6Row)(unsafe.Pointer(&t.Table)), t.Len), nil
}
// TCP6Table is ...
type TCP6Table struct {
Len uint32
Table [1]TCP6Row
}
// TCP6Row is ...
type TCP6Row struct {
LocalAddr [4]uint32
LocalScopeId uint32
LocalPort uint32
RemoteAddr [4]uint32
RemoteScopeId uint32
RemotePort uint32
State uint32
OwningPid uint32
}
// GetExtendedTcpTable is ...
func GetExtendedTcpTable(order uint32, ulAf uint32, tableClass uint32, buf []byte) ([]byte, error) {
pTcpTable := &buf[0]
dwSize := uint32(len(buf))
for {
// DWORD GetExtendedTcpTable(
// PVOID pTcpTable,
// PDWORD pdwSize,
// BOOL bOrder,
// ULONG ulAf,
// TCP_TABLE_CLASS TableClass,
// ULONG Reserved
// );
// https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getextendedtcptable
ret, _, errno := getExtendedTcpTable.Call(
uintptr(unsafe.Pointer(pTcpTable)),
uintptr(unsafe.Pointer(&dwSize)),
uintptr(order),
uintptr(ulAf),
uintptr(tableClass),
uintptr(uint32(0)),
)
if ret == windows.NO_ERROR {
return buf, nil
}
if windows.Errno(ret) == windows.ERROR_INSUFFICIENT_BUFFER {
buf = make([]byte, dwSize)
pTcpTable = &buf[0]
continue
}
return nil, errno
}
}
// GetUDPTable is ...
func GetUDPTable(buf []byte) ([]UDPRow, error) {
b, err := GetExtendedUdpTable(0, windows.AF_INET, 1 /* UDP_TABLE_OWNER_PID */, buf)
if err != nil {
return nil, err
}
t := (*UDPTable)(unsafe.Pointer(&b[0]))
return unsafe.Slice((*UDPRow)(unsafe.Pointer(&t.Table)), t.Len), nil
}
// UDPTable is ...
type UDPTable struct {
Len uint32
Table [1]UDPRow
}
// UDPRow is ...
type UDPRow struct {
LocalAddr uint32
LocalPort uint32
OwningPid uint32
}
// GetUDP6Table is ...
func GetUDP6Table(buf []byte) ([]UDP6Row, error) {
b, err := GetExtendedUdpTable(0, windows.AF_INET6, 1 /* UDP_TABLE_OWNER_PID */, buf)
if err != nil {
return nil, err
}
t := (*UDP6Table)(unsafe.Pointer(&b[0]))
return unsafe.Slice((*UDP6Row)(unsafe.Pointer(&t.Table)), t.Len), nil
}
// UDP6Table is ...
type UDP6Table struct {
Len uint32
Table [1]UDP6Row
}
// UDP6Row is ...
type UDP6Row struct {
LocalAddr [4]uint32
LocalScopeId uint32
LocalPort uint32
OwningPid uint32
}
// GetExtendedUdpTable is ...
func GetExtendedUdpTable(order uint32, ulAf uint32, tableClass uint32, buf []byte) ([]byte, error) {
pUdpTable := &buf[0]
dwSize := uint32(len(buf))
for {
// DWORD GetExtendedUdpTable(
// PVOID pUdpTable,
// PDWORD pdwSize,
// BOOL bOrder,
// ULONG ulAf,
// UDP_TABLE_CLASS TableClass,
// ULONG Reserved
// );
// https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getextendedudptable
ret, _, errno := getExtendedUdpTable.Call(
uintptr(unsafe.Pointer(pUdpTable)),
uintptr(unsafe.Pointer(&dwSize)),
uintptr(order),
uintptr(ulAf),
uintptr(tableClass),
uintptr(uint32(0)),
)
if ret == windows.NO_ERROR {
return buf, nil
}
if windows.Errno(ret) == windows.ERROR_INSUFFICIENT_BUFFER {
buf = make([]byte, dwSize)
pUdpTable = &buf[0]
continue
}
return nil, errno
}
}
// GetInterfaceIndex is ...
func GetInterfaceIndex(s string) (int, error) {
destAddr := windows.RawSockaddr{}
ip := net.ParseIP(s)
if ip == nil {
return 0, errors.New("parse ip error")
}
if ipv4 := ip.To4(); ipv4 != nil {
addr := (*windows.RawSockaddrInet4)(unsafe.Pointer(&destAddr))
addr.Family = windows.AF_INET
copy(addr.Addr[:], ipv4)
} else {
ipv6 := ip.To16()
addr := (*windows.RawSockaddrInet6)(unsafe.Pointer(&destAddr))
addr.Family = windows.AF_INET6
copy(addr.Addr[:], ipv6)
}
return GetBestInterfaceEx(&destAddr)
}
// GetBestInterfaceEx is ...
func GetBestInterfaceEx(addr *windows.RawSockaddr) (int, error) {
dwBestIfIndex := int32(0)
// IPHLPAPI_DLL_LINKAGE DWORD GetBestInterfaceEx(
// sockaddr *pDestAddr,
// PDWORD pdwBestIfIndex
// );
// https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getbestinterfaceex
ret, _, errno := getBestInterfaceEx.Call(
uintptr(unsafe.Pointer(addr)),
uintptr(unsafe.Pointer(&dwBestIfIndex)),
)
if ret == windows.NO_ERROR {
return int(dwBestIfIndex), nil
}
return 0, errno
}