forked from netsec-ethz/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
235 lines (211 loc) · 5.88 KB
/
group.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
// Copyright 2019 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 hiddenpath
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl/path_mgmt"
)
// Parsing errors
const (
// ErrInvalidGroupIdFormat indicates an invalid GroupId format
ErrInvalidGroupIdFormat common.ErrMsg = "Invalid GroupId format"
// ErrInvalidGroupIdSuffix indicates an invalid GroupId suffix
ErrInvalidGroupIdSuffix common.ErrMsg = "Invalid GroupId suffix"
)
// Validation errors
const (
// ErrMissingGroupId indicates a missing GroupId
ErrMissingGroupId common.ErrMsg = "Missing GroupId"
// ErrInvalidVersion indicates a missing version
ErrInvalidVersion common.ErrMsg = "Invalid version"
// ErrMissingOwner indicates a missing Owner
ErrMissingOwner common.ErrMsg = "Missing Owner"
// ErrOwnerMismatch indicates a mismatch between Owner and GroupId.OwnerAS
ErrOwnerMismatch common.ErrMsg = "Owner mismatch"
// ErrEmptyWriters indicates an empty Writer section
ErrEmptyWriters common.ErrMsg = "Writer section cannot be empty"
// ErrEmptyRegistries indicates an empty Registires section
ErrEmptyRegistries common.ErrMsg = "Registry section cannot be empty"
)
type GroupId struct {
OwnerAS addr.AS
Suffix uint16
}
func (id GroupId) String() string {
return fmt.Sprintf("%s-%x", id.OwnerAS, id.Suffix)
}
func (id *GroupId) UnmarshalText(data []byte) error {
v := string(data)
v = strings.Replace(v, "_", ":", 2)
parts := strings.Split(v, "-")
if len(parts) != 2 {
return common.NewBasicError(ErrInvalidGroupIdFormat, nil, "GroupId", v)
}
ownerAS, err := addr.ASFromString(parts[0])
if err != nil {
return err
}
suffix, err := strconv.ParseUint(parts[1], 16, 16)
if err != nil {
return common.NewBasicError(ErrInvalidGroupIdSuffix, err, "Suffix", parts[1])
}
id.OwnerAS = ownerAS
id.Suffix = uint16(suffix)
return nil
}
func (id GroupId) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
func (id *GroupId) UnmarshalJSON(data []byte) (err error) {
var v string
if err = json.Unmarshal(data, &v); err != nil {
return err
}
return id.UnmarshalText([]byte(v))
}
func (id GroupId) MarshalJSON() ([]byte, error) {
return json.Marshal(id.String())
}
// ToMsg returns id as Cerializable message suitable to be sent via messenger
func (id GroupId) ToMsg() *path_mgmt.HPGroupId {
return &path_mgmt.HPGroupId{
OwnerAS: id.OwnerAS,
GroupId: id.Suffix,
}
}
// IdFromMsg returns a GroupId from the Cerializable representation
func IdFromMsg(id *path_mgmt.HPGroupId) GroupId {
return GroupId{
OwnerAS: id.OwnerAS,
Suffix: id.GroupId,
}
}
type Group struct {
Id GroupId `json:"GroupID"`
Version uint
Owner addr.IA
Writers []addr.IA
Readers []addr.IA
Registries []addr.IA
}
func (g *Group) UnmarshalJSON(data []byte) (err error) {
type groupAlias Group
var v groupAlias
if err = json.Unmarshal(data, &v); err != nil {
return err
}
if v.Id == (GroupId{}) {
return common.NewBasicError(ErrMissingGroupId, nil)
}
if v.Version == 0 {
return common.NewBasicError(ErrInvalidVersion, nil)
}
if v.Owner == (addr.IA{}) {
return common.NewBasicError(ErrMissingOwner, nil)
}
if v.Owner.A != v.Id.OwnerAS {
return common.NewBasicError(ErrOwnerMismatch, nil,
"OwnerAS", v.Owner.A, "GroupId.OwnerAS", v.Id.OwnerAS)
}
if len(v.Writers) == 0 {
return common.NewBasicError(ErrEmptyWriters, nil)
}
if len(v.Registries) == 0 {
return common.NewBasicError(ErrEmptyRegistries, nil)
}
*g = Group(v)
return nil
}
// HasWriter returns true if ia is a Writer of h
func (g *Group) HasWriter(ia addr.IA) bool {
for _, w := range g.Writers {
if w == ia {
return true
}
}
return false
}
// HasReader returns true if ia is a Reader of h
func (g *Group) HasReader(ia addr.IA) bool {
for _, r := range g.Readers {
if r == ia {
return true
}
}
return false
}
// HasRegistry returns true if ia is a Registry of h
func (g *Group) HasRegistry(ia addr.IA) bool {
for _, r := range g.Registries {
if r == ia {
return true
}
}
return false
}
// ToMsg returns h as Cerializable message suitable to be sent via messenger
func (g *Group) ToMsg() *path_mgmt.HPCfg {
return &path_mgmt.HPCfg{
GroupId: g.Id.ToMsg(),
Version: uint32(g.Version),
OwnerISD: g.Owner.I,
Writers: toIAInt(g.Writers),
Readers: toIAInt(g.Readers),
Registries: toIAInt(g.Registries),
}
}
// GroupFromMsg returns a HPCfg from the Cerializable representation
func GroupFromMsg(m *path_mgmt.HPCfg) *Group {
return &Group{
Id: IdFromMsg(m.GroupId),
Version: uint(m.Version),
Owner: addr.IA{
I: m.OwnerISD,
A: m.GroupId.OwnerAS,
},
Writers: toIA(m.Writers),
Readers: toIA(m.Readers),
Registries: toIA(m.Registries),
}
}
func toIAInt(in []addr.IA) []addr.IAInt {
out := make([]addr.IAInt, 0, len(in))
for _, i := range in {
out = append(out, i.IAInt())
}
return out
}
func toIA(in []addr.IAInt) []addr.IA {
out := make([]addr.IA, 0, len(in))
for _, i := range in {
out = append(out, i.IA())
}
return out
}
// GroupIdSet is a set of hidden path GroupIds
type GroupIdSet map[GroupId]struct{}
// GroupIdsToSet converts a list of GroupIds to a GroupIdSet,
// ensuring no duplcates.
func GroupIdsToSet(ids ...GroupId) GroupIdSet {
set := make(GroupIdSet, len(ids))
for _, id := range ids {
set[id] = struct{}{}
}
return set
}