-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnetroute.go
129 lines (109 loc) · 3.54 KB
/
netroute.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
package winroute
import (
"errors"
"fmt"
"syscall"
"unsafe"
log "github.com/Sirupsen/logrus"
)
type NetRoute struct {
iphlpapi *syscall.DLL
getIpInterfaceEntry *syscall.Proc
getIpForwardTable *syscall.Proc
createIpForwardEntry *syscall.Proc
setIpForwardEntry *syscall.Proc
deleteIpForwardEntry *syscall.Proc
systemError map[uint32]string
}
func NewNetRoute() *NetRoute {
iphlpapi := syscall.MustLoadDLL("iphlpapi.dll")
return &NetRoute{
iphlpapi: iphlpapi,
getIpInterfaceEntry: iphlpapi.MustFindProc("GetIpInterfaceEntry"),
getIpForwardTable: iphlpapi.MustFindProc("GetIpForwardTable"),
createIpForwardEntry: iphlpapi.MustFindProc("CreateIpForwardEntry"),
setIpForwardEntry: iphlpapi.MustFindProc("SetIpForwardEntry"),
deleteIpForwardEntry: iphlpapi.MustFindProc("DeleteIpForwardEntry"),
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
systemError: map[uint32]string{
0: "ERROR_SUCCESS",
2: "ERROR_FILE_NOT_FOUND",
5: "ERROR_ACCESS_DENIED",
50: "ERROR_NOT_SUPPORTED",
87: "ERROR_INVALID_PARAMETER",
122: "ERROR_INSUFFICIENT_BUFFER",
1168: "ERROR_NOT_FOUND",
},
}
}
func (r *NetRoute) Close() {
r.iphlpapi.Release()
}
func (r *NetRoute) GetInterfaceByIndex(index uint32) (*IPInterfaceEntry, error) {
ie := &IPInterfaceEntry{
Family: 2, // AF_INET (IPv4)
InterfaceIndex: index,
}
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa814417(v=vs.85).aspx
r1, r2, err := r.getIpInterfaceEntry.Call(uintptr(unsafe.Pointer(ie)))
log.Debugf("%+v", ie)
return ie, r.buildError(r1, r2, err)
}
func (r *NetRoute) GetRoutes() ([]IPForwardRow, error) {
// 112 bytes per route, assume 256 routes by default
bufSize := uint32(112 * 256)
buf := newDynamicMemory(bufSize)
r1, r2, err := r.getIpForwardTable.Call(
buf.Address(),
uintptr(unsafe.Pointer(&bufSize)),
0,
)
for r1 == 122 {
log.WithFields(log.Fields{
"cur_bufsize": len(buf.mem),
"req_bufsize": bufSize,
}).Warn("Insufficient Buffer")
buf = newDynamicMemory(bufSize)
r1, r2, err = r.getIpForwardTable.Call(
buf.Address(),
uintptr(unsafe.Pointer(&bufSize)),
0,
)
}
if r1 != 0 {
return nil, r.buildError(r1, r2, err)
}
num := *(*uint32)(unsafe.Pointer(buf.Address()))
rows := []IPForwardRow{}
sh_rows := (*SliceHeader)(unsafe.Pointer(&rows))
sh_rows.Addr = buf.Address() + 4
sh_rows.Len = int(num)
sh_rows.Cap = int(num)
return rows, nil
}
// AddRoute adds an IPForwardRow to the routing table.
func (r *NetRoute) AddRoute(route *IPForwardRow) error {
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365860(v=vs.85).aspx
r1, r2, err := r.createIpForwardEntry.Call(uintptr(unsafe.Pointer(route)))
return r.buildError(r1, r2, err)
}
func (r *NetRoute) UpdateRoute(route *IPForwardRow) error {
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa366363(v=vs.85).aspx
r1, r2, err := r.setIpForwardEntry.Call(uintptr(unsafe.Pointer(route)))
return r.buildError(r1, r2, err)
}
func (r *NetRoute) DeleteRoute(route *IPForwardRow) error {
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365878(v=vs.85).aspx
r1, r2, err := r.deleteIpForwardEntry.Call(uintptr(unsafe.Pointer(route)))
return r.buildError(r1, r2, err)
}
func (r *NetRoute) buildError(r1 uintptr, r2 uintptr, err error) error {
log.Debugf("r1=%v r2=%v err=%+v", r1, r2, err)
if r1 == 0 {
return nil
}
if m, ok := r.systemError[uint32(r1)]; ok {
return errors.New(m)
}
return errors.New(fmt.Sprintf("ERROR CODE %d", r1))
}