forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
path.go
246 lines (229 loc) · 6.65 KB
/
path.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
// Copyright 2016 ETH Zurich
// Copyright 2018 ETH Zurich, Anapaya Systems
//
// 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 spath
import (
"hash"
"math"
"time"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/serrors"
"github.com/scionproto/scion/go/lib/util"
)
const (
MaxTimestamp = math.MaxUint32
)
var (
// MaxExpirationTime is the maximum absolute expiration time of SCION hop
// fields.
MaxExpirationTime = time.Unix(MaxTimestamp, 0).Add(MaxTTLField.ToDuration())
)
type Path struct {
Raw common.RawBytes
InfOff int // Offset of current Info Field
HopOff int // Offset of current Hop Field
}
func New(raw common.RawBytes) *Path {
return &Path{Raw: raw}
}
// NewOneHop creates a new one hop path with. If necessary, the caller has
// to initialize the offsets.
func NewOneHop(isd addr.ISD, ifid common.IFIDType, ts time.Time, exp ExpTimeType,
hfmac hash.Hash) *Path {
info := InfoField{
ConsDir: true,
Hops: 2,
ISD: uint16(isd),
TsInt: util.TimeToSecs(ts),
}
hop := HopField{
ConsEgress: ifid,
ExpTime: exp,
}
hop.Mac = hop.CalcMac(hfmac, info.TsInt, nil)
raw := make(common.RawBytes, InfoFieldLength+2*HopFieldLength)
info.Write(raw[:InfoFieldLength])
hop.Write(raw[InfoFieldLength:])
return New(raw)
}
func (p *Path) Copy() *Path {
return &Path{append(common.RawBytes(nil), p.Raw...), p.InfOff, p.HopOff}
}
func (p *Path) Reverse() error {
if len(p.Raw) == 0 {
// Empty path doesn't need reversal.
return nil
}
var infOffs = make([]int, 0, 3) // Indexes of Info Fields
var infoFs = make([]*InfoField, 0, 3) // Info Fields
var origOff = 0
// First pass: parse Info Fields and save offsets.
for i := 0; i < 3; i++ {
infOffs = append(infOffs, origOff)
infoF, err := InfoFFromRaw(p.Raw[origOff:])
if err != nil {
return err
}
infoFs = append(infoFs, infoF)
origOff += InfoFieldLength + int(infoF.Hops)*HopFieldLength
if origOff == len(p.Raw) {
break
} else if origOff > len(p.Raw) {
return common.NewBasicError("Unable to reverse corrupt path", nil,
"currOff", origOff, "max", len(p.Raw))
}
}
revRaw := make(common.RawBytes, len(p.Raw))
revOff := 0
newInfIdx := 0
switch {
case p.InfOff == 0:
newInfIdx = len(infOffs) - 1
case p.InfOff == infOffs[len(infOffs)-1]:
newInfIdx = 0
default:
newInfIdx = 1
}
idx := 0
// Fill in reversed path, starting with last segment.
for i := len(infoFs) - 1; i >= 0; i-- {
if idx == newInfIdx {
p.InfOff = revOff
}
infoF := infoFs[i]
infoF.ConsDir = !infoF.ConsDir // Reverse ConsDir flag
infoF.Write(revRaw[revOff:])
infoF, _ = InfoFFromRaw(revRaw[revOff:])
revOff += InfoFieldLength
hOffBase := infOffs[i] + InfoFieldLength
// Copy segment Hop Fields in reverse.
for j := int(infoF.Hops) - 1; j >= 0; j-- {
hOff := hOffBase + j*HopFieldLength
copy(revRaw[revOff:], p.Raw[hOff:hOff+HopFieldLength])
revOff += HopFieldLength
}
idx++
}
// Calculate Hop Field offset.
p.HopOff = len(p.Raw) - p.HopOff
// Update path with reversed copy.
p.Raw = revRaw
return nil
}
// InitOffsets computes the initial Hop Field offset (in bytes) for a newly
// created packet.
func (path *Path) InitOffsets() error {
var err error
var infoF *InfoField
var hopF *HopField
path.InfOff = 0
path.HopOff = common.LineLen
// Cannot initialize an empty path
if path.IsEmpty() {
return serrors.New("Unable to initialize empty path")
}
// Skip Peer with Xover HF
if infoF, err = path.GetInfoField(path.InfOff); err != nil {
return err
}
if infoF.Peer {
if hopF, err = path.GetHopField(path.HopOff); err != nil {
return err
}
if hopF.Xover {
path.HopOff += HopFieldLength
}
}
err = path.incOffsets(0)
if err != nil {
return err
}
if path.InfOff != 0 {
return serrors.New("Unable to find routing Hop Field in first path segment")
}
return nil
}
// IncOffsets updates the info and hop indices to the next routing field, while skipping
// verify only fields.
func (path *Path) IncOffsets() error {
var err error
if path.HopOff == 0 {
// Path not initialized yet
return path.InitOffsets()
}
if _, err = path.GetHopField(path.HopOff); err != nil {
return common.NewBasicError("Hop Field parse error", err, "offset", path.HopOff)
}
return path.incOffsets(HopFieldLength)
}
// IsEmpty returns true if the path is nil or empty (no raw data).
func (path *Path) IsEmpty() bool {
return path == nil || len(path.Raw) == 0
}
// incOffsets jumps ahead skip bytes, and searches for the first routing Hop
// Field starting at that location
func (path *Path) incOffsets(skip int) error {
var hopF *HopField
infoF, err := path.GetInfoField(path.InfOff)
if err != nil {
return common.NewBasicError("Info Field parse error", err, "offset", path.InfOff)
}
path.HopOff += skip
for {
if path.HopOff-path.InfOff > int(infoF.Hops)*common.LineLen {
// Switch to next segment
path.InfOff = path.HopOff
infoF, err = path.GetInfoField(path.InfOff)
if err != nil {
return common.NewBasicError("Info Field parse error", err, "offset", path.InfOff)
}
path.HopOff += common.LineLen
}
if hopF, err = path.GetHopField(path.HopOff); err != nil {
return common.NewBasicError("Hop Field parse error", err, "offset", path.HopOff)
}
if !hopF.VerifyOnly {
break
}
path.HopOff += HopFieldLength
}
return nil
}
func (path *Path) GetInfoField(offset int) (*InfoField, error) {
if offset < 0 {
return nil, common.NewBasicError("Negative InfoF offset", nil, "offset", offset)
}
if path.IsEmpty() {
return nil, serrors.New("Unable to get infoField from empty path")
}
infoF, err := InfoFFromRaw(path.Raw[offset:])
if err != nil {
return nil, common.NewBasicError("Unable to parse Info Field", err, "offset", offset)
}
return infoF, nil
}
func (path *Path) GetHopField(offset int) (*HopField, error) {
if offset < 0 {
return nil, common.NewBasicError("Negative HopF offset", nil, "offset", offset)
}
if path.IsEmpty() {
return nil, serrors.New("Unable to get hopField from empty path")
}
hopF, err := HopFFromRaw(path.Raw[offset:])
if err != nil {
return nil, common.NewBasicError("Unable to parse Hop Field", err, "offset", offset)
}
return hopF, nil
}