This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
wt_mib_ipforward_row2.go
269 lines (206 loc) · 6.78 KB
/
wt_mib_ipforward_row2.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
*/
package winipcfg
import (
"fmt"
"net"
"os"
"unsafe"
"golang.org/x/sys/windows"
)
// https://docs.microsoft.com/en-us/windows/win32/api/netioapi/ns-netioapi-mib_ipforward_row2
// MIB_IPFORWARD_ROW2 defined in netioapi.h
type wtMibIpforwardRow2 struct {
//
// Key Structure.
//
InterfaceLuid uint64 // Windows type: NET_LUID
InterfaceIndex uint32 // Windows type: NET_IFINDEX
DestinationPrefix wtIpAddressPrefix
NextHop wtSockaddrInet
//
// Read-Write Fields.
//
SitePrefixLength uint8 // Windows type: UCHAR
ValidLifetime uint32 // Windows type: ULONG
PreferredLifetime uint32 // Windows type: ULONG
Metric uint32 // Windows type: ULONG
Protocol NlRouteProtocol
Loopback uint8 // Windows type: BOOLEAN
AutoconfigureAddress uint8 // Windows type: BOOLEAN
Publish uint8 // Windows type: BOOLEAN
Immortal uint8 // Windows type: BOOLEAN
//
// Read-Only Fields.
//
Age uint32 // Windows type: ULONG
Origin NlRouteOrigin
}
// Uses GetIpForwardTable2 function
// (https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-getipforwardtable2).
func getWtMibIpforwardRow2s(family AddressFamily) ([]*wtMibIpforwardRow2, error) {
var pTable *wtMibIpforwardTable2 = nil
result := getIpForwardTable2(family, unsafe.Pointer(&pTable))
if pTable != nil {
defer freeMibTable(unsafe.Pointer(pTable))
}
if result != 0 {
return nil, os.NewSyscallError("iphlpapi.GetIpForwardTable2", windows.Errno(result))
}
rows := make([]*wtMibIpforwardRow2, pTable.NumEntries, pTable.NumEntries)
pFirstRow := uintptr(unsafe.Pointer(&pTable.Table[0]))
rowSize := uintptr(wtMibIpforwardRow2_Size) // Should be equal to unsafe.Sizeof(pTable.Table[0])
for i := uint32(0); i < pTable.NumEntries; i++ {
// Dereferencing and rereferencing in order to force copying.
row := *(*wtMibIpforwardRow2)(unsafe.Pointer(pFirstRow + rowSize*uintptr(i)))
rows[i] = &row
}
return rows, nil
}
// Uses InitializeIpForwardEntry function
// (https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-initializeipforwardentry).
func getInitializedWtMibIpforwardRow2(interfaceLuid uint64) *wtMibIpforwardRow2 {
row := wtMibIpforwardRow2{InterfaceLuid: interfaceLuid}
_ = initializeIpForwardEntry(&row)
row.InterfaceLuid = interfaceLuid
return &row
}
// Alternative version (with different input arguments) of getWtMibIpforwardRow2.
func getWtMibIpforwardRow2Alt(interfaceLuid uint64, destination *net.IPNet, nextHop *net.IP) (*wtMibIpforwardRow2, error) {
wtDest, err := createWtIpAddressPrefix(destination)
if err != nil {
return nil, err
}
wtNextHop, err := createWtSockaddrInet(nextHop, 0)
if err != nil {
return nil, err
}
return getWtMibIpforwardRow2(interfaceLuid, wtDest, wtNextHop)
}
// Uses GetIpForwardEntry2 function
// (https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-getipforwardentry2).
func getWtMibIpforwardRow2(interfaceLuid uint64, destination *wtIpAddressPrefix, nextHop *wtSockaddrInet) (*wtMibIpforwardRow2, error) {
row := getInitializedWtMibIpforwardRow2(interfaceLuid)
row.DestinationPrefix = *destination
row.NextHop = *nextHop
result := getIpForwardEntry2(row)
if result == 0 {
return row, nil
} else {
return nil, os.NewSyscallError("iphlpapi.GetIpForwardEntry2", windows.Errno(result))
}
}
func createAndAddWtMibIpforwardRow2(interfaceLuid uint64, routeData *RouteData) error {
wtdest, err := createWtIpAddressPrefix(&routeData.Destination)
if err != nil {
return err
}
wtsaNextHop, err := createWtSockaddrInet(&routeData.NextHop, 0)
if err != nil {
return err
}
row := getInitializedWtMibIpforwardRow2(interfaceLuid)
row.DestinationPrefix = *wtdest
row.NextHop = *wtsaNextHop
row.Metric = routeData.Metric
return row.add()
}
// Uses CreateIpForwardEntry2 function
// (https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-createipforwardentry2).
func (r *wtMibIpforwardRow2) add() error {
result := createIpForwardEntry2(r)
if result == 0 {
return nil
} else {
return os.NewSyscallError("iphlpapi.CreateIpForwardEntry2", windows.Errno(result))
}
}
// Uses SetIpForwardEntry2 function
// (https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-setipforwardentry2).
func (r *wtMibIpforwardRow2) set() error {
result := setIpForwardEntry2(r)
if result == 0 {
return nil
} else {
return os.NewSyscallError("iphlpapi.SetIpForwardEntry2", windows.Errno(result))
}
}
// Uses DeleteIpForwardEntry2 function
// (https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-deleteipforwardentry2).
func (r *wtMibIpforwardRow2) delete() error {
result := deleteIpForwardEntry2(r)
if result == 0 {
return nil
} else {
return os.NewSyscallError("iphlpapi.DeleteIpForwardEntry2", windows.Errno(result))
}
}
func (r *wtMibIpforwardRow2) toRoute() (*Route, error) {
if r == nil {
return nil, nil
}
iap, err := r.DestinationPrefix.toIpAddressPrefix()
if err != nil {
return nil, err
}
sainet, err := r.NextHop.toSockaddrInet()
if err != nil {
return nil, err
}
return &Route{
InterfaceLuid: r.InterfaceLuid,
InterfaceIndex: r.InterfaceIndex,
DestinationPrefix: *iap,
NextHop: *sainet,
SitePrefixLength: r.SitePrefixLength,
ValidLifetime: r.ValidLifetime,
PreferredLifetime: r.PreferredLifetime,
Metric: r.Metric,
Protocol: r.Protocol,
Loopback: uint8ToBool(r.Loopback),
AutoconfigureAddress: uint8ToBool(r.AutoconfigureAddress),
Publish: uint8ToBool(r.Publish),
Immortal: uint8ToBool(r.Immortal),
Age: r.Age,
Origin: r.Origin,
}, nil
}
func (r *Route) ToRouteData() (*RouteData, error) {
dest, err := r.DestinationPrefix.toNetIpNet()
if err != nil {
return nil, err
}
return &RouteData{
Destination: *dest,
NextHop: r.NextHop.Address,
Metric: r.Metric,
}, nil
}
func (r *wtMibIpforwardRow2) String() string {
if r == nil {
return "<nil>"
}
return fmt.Sprintf(`InterfaceLuid: %d
InterfaceIndex: %d
DestinationPrefix: %s
NextHop: %s
SitePrefixLength: %d
ValidLifetime: %d
PreferredLifetime: %d
Metric: %d
Protocol: %s
Loopback: %d
AutoconfigureAddress: %d
Publish: %d
Immortal: %d
Age: %d
Origin: %s`, r.InterfaceLuid, r.InterfaceIndex, r.DestinationPrefix.String(), r.NextHop.String(), r.SitePrefixLength,
r.ValidLifetime, r.PreferredLifetime, r.Metric, r.Protocol.String(), r.Loopback, r.AutoconfigureAddress,
r.Publish, r.Immortal, r.Age, r.Origin.String())
}
type planetSorter struct {
planets []*wtMibIpforwardRow2
by func(p1, p2 *wtMibIpforwardRow2) bool // Closure used in the Less method.
}