forked from platinasystems/xeth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fib.go
133 lines (122 loc) · 2.95 KB
/
fib.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
/* Copyright(c) 2018 Platina Systems, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*
* The full GNU General Public License is included in this distribution in
* the file called "COPYING".
*
* Contact Information:
* Platina Systems, 3180 Del La Cruz Blvd, Santa Clara, CA 95054
*/
package xeth
import (
"fmt"
"net"
"reflect"
"unsafe"
)
const (
FIB_EVENT_ENTRY_REPLACE = iota
FIB_EVENT_ENTRY_APPEND
FIB_EVENT_ENTRY_ADD
FIB_EVENT_ENTRY_DEL
FIB_EVENT_RULE_ADD
FIB_EVENT_RULE_DEL
FIB_EVENT_NH_ADD
FIB_EVENT_NH_DEL
)
type FibEntryEvent int
type FibRuleEvent int
type FibNHEvent int
func (event FibEntryEvent) String() string {
var events = []string{
FIB_EVENT_ENTRY_REPLACE: "replace",
FIB_EVENT_ENTRY_APPEND: "append",
FIB_EVENT_ENTRY_ADD: "add",
FIB_EVENT_ENTRY_DEL: "del",
}
var s string
i := int(event)
if i < len(events) {
s = events[i]
}
if len(s) == 0 {
s = fmt.Sprint("@", i)
}
return s
}
func (event FibRuleEvent) String() string {
var events = []string{
FIB_EVENT_RULE_ADD: "add",
FIB_EVENT_RULE_DEL: "del",
}
var s string
i := int(event)
if i < len(events) {
s = events[i]
}
if len(s) == 0 {
s = fmt.Sprint("@", i)
}
return s
}
func (event FibNHEvent) String() string {
var events = []string{
FIB_EVENT_NH_ADD: "add",
FIB_EVENT_NH_DEL: "del",
}
var s string
i := int(event)
if i < len(events) {
s = events[i]
}
if len(s) == 0 {
s = fmt.Sprint("@", i)
}
return s
}
func (fe *MsgFibentry) NextHops() []NextHop {
ptr := unsafe.Pointer(fe)
nhs := int(fe.Nhs)
return *(*[]NextHop)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(ptr) + uintptr(SizeofMsgFibentry),
Len: nhs,
Cap: nhs,
}))
}
func (fe *MsgFibentry) Prefix() *net.IPNet {
ipBuf := make([]byte, 4)
maskBuf := make([]byte, 4)
*(*uint32)(unsafe.Pointer(&ipBuf[0])) = fe.Address
*(*uint32)(unsafe.Pointer(&maskBuf[0])) = fe.Mask
ipNet := new(net.IPNet)
ipNet.IP = net.IP(ipBuf)
ipNet.Mask = net.IPMask(maskBuf)
return ipNet
}
func (fe *MsgFibentry) String() string {
kind := Kind(fe.Kind)
event := FibEntryEvent(fe.Event)
prefix := fe.Prefix()
return fmt.Sprintln(kind, event, Rtn(fe.Type), prefix,
"netns", Netns(fe.Net),
"table", RtTable(fe.Id),
fe.NextHops())
}
func (nh *NextHop) IP() net.IP {
buf := make([]byte, 4)
*(*uint32)(unsafe.Pointer(&buf[0])) = nh.Gw
return net.IP(buf)
}