-
Notifications
You must be signed in to change notification settings - Fork 155
/
util.go
109 lines (97 loc) · 3.41 KB
/
util.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
// Copyright 2018 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 revcache
import (
"context"
"github.com/scionproto/scion/pkg/addr"
"github.com/scionproto/scion/pkg/private/common"
"github.com/scionproto/scion/pkg/private/ctrl/path_mgmt"
seg "github.com/scionproto/scion/pkg/segment"
"github.com/scionproto/scion/private/storage/cleaner"
)
// NewCleaner creates a cleaner task that deletes expired revocations.
func NewCleaner(rc RevCache, s string) *cleaner.Cleaner {
return cleaner.New(func(ctx context.Context) (int, error) {
cnt, err := rc.DeleteExpired(ctx)
return int(cnt), err
}, s)
}
// FilterNew filters the given revocations against the revCache, only the ones
// which are not in the cache are returned. This is a convenience wrapper
// around the Revocations type and its filter new method.
func FilterNew(ctx context.Context, revCache RevCache,
revocations []*path_mgmt.RevInfo) ([]*path_mgmt.RevInfo, error) {
rMap, err := RevocationToMap(revocations)
if err != nil {
return nil, err
}
if err = rMap.FilterNew(ctx, revCache); err != nil {
return nil, err
}
return rMap.ToSlice(), nil
}
// newerInfo returns whether the received info is newer than the existing.
func newerInfo(existing, received *path_mgmt.RevInfo) bool {
return !received.SameIntf(existing) ||
received.Timestamp().After(existing.Timestamp())
}
// NoRevokedHopIntf returns true if there is no on-segment revocation.
func NoRevokedHopIntf(ctx context.Context, revCache RevCache,
s *seg.PathSegment) (bool, error) {
revKeys := make(KeySet)
addRevKeys([]*seg.PathSegment{s}, revKeys, true)
revs, err := revCache.Get(ctx, revKeys)
return len(revs) == 0, err
}
// RelevantRevInfos finds all revocations for the given segments.
func RelevantRevInfos(ctx context.Context, revCache RevCache,
allSegs ...[]*seg.PathSegment) ([]*path_mgmt.RevInfo, error) {
revKeys := make(KeySet)
for _, segs := range allSegs {
addRevKeys(segs, revKeys, false)
}
revs, err := revCache.Get(ctx, revKeys)
if err != nil {
return nil, err
}
allRevs := make([]*path_mgmt.RevInfo, 0, len(revs))
for _, rev := range revs {
allRevs = append(allRevs, rev)
}
return allRevs, nil
}
// addRevKeys adds all revocations keys for the given segments to the keys set.
// If hopOnly is set, only the first hop entry is considered.
func addRevKeys(segs []*seg.PathSegment, keys KeySet, hopOnly bool) {
addIntfs := func(ia addr.IA, ingress, egress uint16) {
if ingress != 0 {
keys[*NewKey(ia, common.IFIDType(ingress))] = struct{}{}
}
if egress != 0 {
keys[*NewKey(ia, common.IFIDType(egress))] = struct{}{}
}
}
for _, s := range segs {
for _, asEntry := range s.ASEntries {
hop := asEntry.HopEntry.HopField
addIntfs(asEntry.Local, hop.ConsIngress, hop.ConsEgress)
if hopOnly {
continue
}
for _, peer := range asEntry.PeerEntries {
addIntfs(asEntry.Local, peer.HopField.ConsIngress, peer.HopField.ConsEgress)
}
}
}
}