-
Notifications
You must be signed in to change notification settings - Fork 160
/
frame.go
142 lines (129 loc) · 3.94 KB
/
frame.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
// Copyright 2018 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package reliable
import (
"net"
"github.com/scionproto/scion/go/lib/common"
)
// OverlayPacket contains metadata about a SCION packet going through the
// reliable socket framing protocol.
type OverlayPacket struct {
Address *net.UDPAddr
Payload []byte
}
func (p *OverlayPacket) SerializeTo(b []byte) (int, error) {
var f Frame
f.Cookie = expectedCookie
f.AddressType = byte(getAddressType(p.Address))
f.Length = uint32(len(p.Payload))
if p.Address != nil {
if err := f.insertAddress(p.Address); err != nil {
return 0, err
}
}
f.Payload = p.Payload
return f.SerializeTo(b)
}
func (p *OverlayPacket) DecodeFromBytes(b []byte) error {
var f Frame
if err := f.DecodeFromBytes(b); err != nil {
return err
}
if f.Cookie != expectedCookie {
return common.NewBasicError(ErrBadCookie, nil)
}
p.Address = f.extractAddress()
p.Payload = f.Payload
return nil
}
// Frame describes the wire format of the reliable socket framing protocol.
type Frame struct {
Cookie uint64
AddressType byte
Length uint32
Address []byte
Port []byte
Payload []byte
}
func (f *Frame) SerializeTo(b []byte) (int, error) {
totalLength := f.length()
if totalLength > len(b) {
return 0, common.NewBasicError(ErrBufferTooSmall, nil, "have", len(b), "want", totalLength)
}
common.Order.PutUint64(b, f.Cookie)
b[8] = f.AddressType
common.Order.PutUint32(b[9:], uint32(f.Length))
copy(b[13:], f.Address)
copy(b[13+len(f.Address):], f.Port)
copy(b[13+len(f.Address)+len(f.Port):], f.Payload)
return totalLength, nil
}
func (f *Frame) DecodeFromBytes(data []byte) error {
if len(data) < f.headerLength() {
return common.NewBasicError(ErrIncompleteFrameHeader, nil)
}
f.Cookie = common.Order.Uint64(data)
f.AddressType = data[8]
f.Length = common.Order.Uint32(data[9:])
addressType := AddressType(f.AddressType)
if !addressType.IsValid() {
return common.NewBasicError(ErrBadAddressType, nil, "type", addressType)
}
addrSize := addressType.AddressLength()
portSize := addressType.PortLength()
if len(data[13:]) < addrSize {
return common.NewBasicError(ErrIncompleteAddress, nil)
}
f.Address = data[13 : 13+addrSize]
if len(data[13+addrSize:]) < portSize {
return common.NewBasicError(ErrIncompletePort, nil)
}
f.Port = data[13+addrSize : 13+addrSize+portSize]
f.Payload = data[13+addrSize+portSize:]
if len(f.Payload) != int(f.Length) {
return common.NewBasicError(ErrBadLength, nil)
}
return nil
}
// length returns the total length of the frame (including payload).
func (f *Frame) length() int {
return f.headerLength() + len(f.Address) + len(f.Port) + len(f.Payload)
}
// header length returns the length of the fixed size start of the frame
// (cookie, address type and payload length field).
func (f *Frame) headerLength() int {
return 8 + 1 + 4
}
func (f *Frame) insertAddress(address *net.UDPAddr) error {
if address.IP == nil || address.IP.IsUnspecified() {
return common.NewBasicError(ErrNoAddress, nil)
}
if address.Port == 0 {
return common.NewBasicError(ErrNoPort, nil)
}
f.Address = []byte(normalizeIP(address.IP))
f.Port = make([]byte, 2)
common.Order.PutUint16(f.Port, uint16(address.Port))
return nil
}
func (f *Frame) extractAddress() *net.UDPAddr {
t := AddressType(f.AddressType)
if t == AddressTypeIPv4 || t == AddressTypeIPv6 {
return &net.UDPAddr{
IP: net.IP(f.Address),
Port: int(common.Order.Uint16(f.Port)),
}
}
return nil
}