/
proto.go
261 lines (230 loc) · 6.78 KB
/
proto.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
package proto
import (
"encoding/binary"
"encoding/json"
"fmt"
"time"
)
// Data structures for the Netopus wire protocol
// Msg is the type of a single Netopus protocol datagram
type Msg []byte
// MsgType enumerates the types of Netopus protocol messages
type MsgType int
const (
MsgTypeError MsgType = -1
MsgTypeData MsgType = 0
MsgTypeInit MsgType = 1
MsgTypeRouteOOB MsgType = 2
MsgTypeOOB MsgType = 3
MsgTypeKeepalive MsgType = 4
MsgTypeKeepaliveReply MsgType = 5
MaxMsgType MsgType = 5
)
// InitMsg is a message type sent at connection initialization time
type InitMsg struct {
MyAddr IP
}
// RoutingNodes stores the known connection information for a network of nodes
type RoutingNodes map[IP]RoutingConns
// RoutingConns stores the known connections and costs directly connected to a node
type RoutingConns map[Subnet]float32
// RoutingPolicy is a routing table giving the next hop for a list of subnets
type RoutingPolicy map[Subnet]IP
// RoutingUpdate is a message type carrying routing information
type RoutingUpdate struct {
Origin IP
NodeName string
UpdateEpoch uint64
UpdateSequence uint64
Connections RoutingConns
Names map[string]IP
ConfigVersion time.Time
ConfigData []byte
ConfigSignature []byte
}
// OOBMessage is an out-of-band message
type OOBMessage struct {
Hops byte
SourceAddr IP
SourcePort uint16
DestAddr IP
DestPort uint16
Data []byte
}
type RouteOOBMessage OOBMessage
type KeepaliveMsg struct{}
type KeepaliveReplyMsg struct{}
var ErrUnknownMessageType = fmt.Errorf("unknown message type")
// Type returns the MsgType of a Msg
func (m Msg) Type() MsgType {
if len(m) < 1 {
return MsgTypeError
}
b := m[0]
switch {
case b>>4 == 6: // Data packets are just unmodified IPv6 packets
return MsgTypeData
case MsgType(b) <= MaxMsgType:
return MsgType(b)
default:
return MsgTypeError
}
}
// Unmarshal unmarshals a message, returning []byte for a data message or a pointer to struct for other types
func (m Msg) Unmarshal() (any, error) {
unmarshalMsg := func(v any) (any, error) {
err := json.Unmarshal(m[1:], v)
if err != nil {
return nil, err
}
return v, nil
}
switch m.Type() {
case MsgTypeData:
return []byte(m), nil
case MsgTypeInit:
return unmarshalMsg(&InitMsg{})
case MsgTypeRouteOOB:
return m.unmarshalRouteOOB()
case MsgTypeOOB:
return m.unmarshalOOB()
case MsgTypeKeepalive:
return unmarshalMsg(&KeepaliveMsg{})
case MsgTypeKeepaliveReply:
return unmarshalMsg(&KeepaliveReplyMsg{})
}
return nil, ErrUnknownMessageType
}
// marshalMsg marshals a message of some type to a []byte
func marshalMsg[T any](m T, msgType MsgType) ([]byte, error) {
d, err := json.Marshal(m)
if err != nil {
return nil, err
}
d = append([]byte{byte(msgType)}, d...)
return d, nil
}
// Marshal marshals an InitMsg to a []byte
func (m *InitMsg) Marshal() ([]byte, error) {
d, err := marshalMsg(m, MsgTypeInit)
if err != nil {
return nil, err
}
return d, nil
}
// Marshal marshals a KeepaliveMsg to a []byte
func (m *KeepaliveMsg) Marshal() ([]byte, error) {
d, err := marshalMsg(m, MsgTypeKeepalive)
if err != nil {
return nil, err
}
return d, nil
}
// Marshal marshals a KeepaliveReplyMsg to a []byte
func (m *KeepaliveReplyMsg) Marshal() ([]byte, error) {
d, err := marshalMsg(m, MsgTypeKeepaliveReply)
if err != nil {
return nil, err
}
return d, nil
}
const oobHeaderHops = 1
const oobHeaderSourceAddr = 2
const oobHeaderSourcePort = 18
const oobHeaderDestAddr = 20
const oobHeaderDestPort = 37
const oobHeaderData = 39
// Marshal marshals an OOBMessage to a []byte
func (oob *OOBMessage) Marshal() ([]byte, error) {
b := make([]byte, oobHeaderData+len(oob.Data))
b[0] = byte(MsgTypeOOB)
b[oobHeaderHops] = oob.Hops
copy(b[oobHeaderSourceAddr:oobHeaderSourceAddr+16], oob.SourceAddr)
binary.BigEndian.PutUint16(b[oobHeaderSourcePort:oobHeaderSourcePort+2], oob.SourcePort)
copy(b[oobHeaderDestAddr:oobHeaderDestAddr+16], oob.DestAddr)
binary.BigEndian.PutUint16(b[oobHeaderDestPort:oobHeaderDestPort+2], oob.DestPort)
copy(b[oobHeaderData:], oob.Data)
return b, nil
}
// unmarshalOOB unmarshals an OOBMessage from a Msg
func (m Msg) unmarshalOOB() (*OOBMessage, error) {
if len(m) < oobHeaderData {
return nil, fmt.Errorf("message too short")
}
if m[0] != byte(MsgTypeOOB) {
return nil, fmt.Errorf("message is not an OOBMessage")
}
return &OOBMessage{
Hops: m[oobHeaderHops],
SourceAddr: IP(m[oobHeaderSourceAddr : oobHeaderSourceAddr+16]),
SourcePort: binary.BigEndian.Uint16(m[oobHeaderSourcePort : oobHeaderSourcePort+2]),
DestAddr: IP(m[oobHeaderDestAddr : oobHeaderDestAddr+16]),
DestPort: binary.BigEndian.Uint16(m[oobHeaderDestPort : oobHeaderDestPort+2]),
Data: m[oobHeaderData:],
}, nil
}
// String produces a string representation of an OOBMessage
func (oob *OOBMessage) String() string {
return fmt.Sprintf("from %s:%d to %s:%d: %d bytes",
oob.SourceAddr, oob.SourcePort, oob.DestAddr, oob.DestPort, len(oob.Data))
}
// Marshal marshals a RouteOOBMessage to a []byte
func (rm *RouteOOBMessage) Marshal() ([]byte, error) {
oob, err := (*OOBMessage)(rm).Marshal()
if err != nil {
return nil, err
}
oob[0] = byte(MsgTypeRouteOOB)
return oob, nil
}
// unmarshalOOB unmarshals a RouteOOBMessage from a Msg
func (m Msg) unmarshalRouteOOB() (*RouteOOBMessage, error) {
if len(m) < oobHeaderData {
return nil, fmt.Errorf("message too short")
}
if m[0] != byte(MsgTypeRouteOOB) {
return nil, fmt.Errorf("message is not a RouteOOBMessage")
}
return &RouteOOBMessage{
Hops: m[oobHeaderHops],
SourceAddr: IP(m[oobHeaderSourceAddr : oobHeaderSourceAddr+16]),
SourcePort: binary.BigEndian.Uint16(m[oobHeaderSourcePort : oobHeaderSourcePort+2]),
DestAddr: IP(m[oobHeaderDestAddr : oobHeaderDestAddr+16]),
DestPort: binary.BigEndian.Uint16(m[oobHeaderDestPort : oobHeaderDestPort+2]),
Data: m[oobHeaderData:],
}, nil
}
// String produces a string representation of a RouteOOBMessage
func (rm *RouteOOBMessage) String() string {
return (*OOBMessage)(rm).String()
}
func (ru *RoutingUpdate) String() string {
return fmt.Sprintf("%s/%d/%d", ru.Origin.String(), ru.UpdateEpoch, ru.UpdateSequence)
}
//goland:noinspection GoMixedReceiverTypes
func (rc RoutingConns) MarshalJSON() ([]byte, error) {
m := make(map[string]float32)
for k, v := range rc {
m[k.String()] = v
}
return json.Marshal(m)
}
//goland:noinspection GoMixedReceiverTypes
func (rc *RoutingConns) UnmarshalJSON(data []byte) error {
m := make(map[string]float32)
err := json.Unmarshal(data, &m)
if err != nil {
return err
}
newRc := make(RoutingConns)
for k, v := range m {
var subnet Subnet
_, subnet, err = ParseCIDR(k)
if err != nil {
return err
}
newRc[subnet] = v
}
*rc = newRc
return nil
}