forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
229 lines (208 loc) · 7.15 KB
/
common.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
// 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 infra
import (
"context"
"fmt"
"net"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl/cert_mgmt"
"github.com/scionproto/scion/go/lib/ctrl/path_mgmt"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/scrypto/cert"
"github.com/scionproto/scion/go/lib/scrypto/trc"
"github.com/scionproto/scion/go/proto"
)
// Interface Transport wraps around low-level networking objects to provide
// reliable and unreliable delivery of network packets, together with
// context-aware networking that can be used to construct handlers with
// timeouts.
//
// Transport layers must be safe for concurrent use by multiple goroutines.
type Transport interface {
// Send an unreliable message. Unreliable transport layers do not request
// an ACK. For reliable transport layers, this is the same as SendMsgTo.
SendUnreliableMsgTo(context.Context, common.RawBytes, net.Addr) error
// Send a reliable message. Unreliable transport layers block here waiting
// for the message to be ACK'd. Reliable transport layers return
// immediately.
SendMsgTo(context.Context, common.RawBytes, net.Addr) error
// Receive a message.
RecvFrom(context.Context) (common.RawBytes, net.Addr, error)
// Clean up.
Close(context.Context) error
}
// Interface Handler is implemented by objects that can handle a request coming
// from a remote SCION network node.
type Handler interface {
Handle(*Request)
}
// Constructs a handler for request r. Handle() can be called on the
// resulting object to process the message.
type HandlerFunc func(r *Request)
func (f HandlerFunc) Handle(r *Request) {
f(r)
}
// Request describes an object received from the network that is not part of an
// exchange initiated by the local node. A Request includes its associated
// context.
type Request struct {
// Message is the inner proto.Cerealizable message, as supported by
// messenger.Messenger (e.g., a *cert_mgmt.ChainReq). For information about
// possible messages, see the package documentation for that package.
Message proto.Cerealizable
// FullMessage is the top-level SignedCtrlPld message read from the wire
FullMessage proto.Cerealizable
// Peer is the node that sent this request
Peer net.Addr
// ID is the CtrlPld top-level ID.
ID uint64
// Logger can be used to write handler-scope messages in a way that can be
// easily correlated with server request/responses.
Logger log.Logger
// ctx is a server context, used in handlers when receiving messages from
// the network.
ctx context.Context
}
func NewRequest(ctx context.Context, msg, fullMsg proto.Cerealizable, peer net.Addr,
id uint64, logger log.Logger) *Request {
return &Request{
Message: msg,
FullMessage: fullMsg,
Peer: peer,
ctx: ctx,
ID: id,
Logger: logger,
}
}
// Context returns the request's context.
func (r *Request) Context() context.Context {
return r.ctx
}
var (
// messengerContextKey is a context key. It can be used in SCION infra
// request handlers to access the messaging layer the message arrived on.
messengerContextKey = &contextKey{"infra-messenger"}
)
type contextKey struct {
name string
}
func (k *contextKey) String() string {
return "infra/messenger context value " + k.name
}
func NewContextWithMessenger(ctx context.Context, msger Messenger) context.Context {
return context.WithValue(ctx, messengerContextKey, msger)
}
type MessageType int
const (
None MessageType = iota
TRC
TRCRequest
Chain
ChainRequest
IfStateInfos
SegChangesReq
SegChangesReply
SegChangesIdReq
SegChangesIdReply
SegReg
SegRequest
SegReply
SegRev
SegSync
ChainIssueRequest
ChainIssueReply
)
func (mt MessageType) String() string {
switch mt {
case None:
return "None"
case ChainRequest:
return "ChainRequest"
case Chain:
return "Chain"
case TRCRequest:
return "TRCRequest"
case TRC:
return "TRC"
case IfStateInfos:
return "IFStateInfos"
case SegChangesReq:
return "SegChangesReq"
case SegChangesReply:
return "SegChangesReply"
case SegChangesIdReq:
return "SegChangesIdReq"
case SegChangesIdReply:
return "SegChangesIdReply"
case SegReg:
return "SegReg"
case SegRequest:
return "SegRequest"
case SegReply:
return "SegReply"
case SegRev:
return "SegRev"
case SegSync:
return "SegSync"
case ChainIssueRequest:
return "ChainIssueRequest"
case ChainIssueReply:
return "ChainIssueReply"
default:
return fmt.Sprintf("Unknown (%d)", mt)
}
}
type Messenger interface {
GetTRC(ctx context.Context, msg *cert_mgmt.TRCReq, a net.Addr,
id uint64) (*cert_mgmt.TRC, error)
SendTRC(ctx context.Context, msg *cert_mgmt.TRC, a net.Addr, id uint64) error
GetCertChain(ctx context.Context, msg *cert_mgmt.ChainReq, a net.Addr,
id uint64) (*cert_mgmt.Chain, error)
SendCertChain(ctx context.Context, msg *cert_mgmt.Chain, a net.Addr, id uint64) error
GetSegs(ctx context.Context, msg *path_mgmt.SegReq, a net.Addr,
id uint64) (*path_mgmt.SegReply, error)
SendSegReply(ctx context.Context, msg *path_mgmt.SegReply, a net.Addr, id uint64) error
SendSegSync(ctx context.Context, msg *path_mgmt.SegSync, a net.Addr, id uint64) error
GetSegChangesIds(ctx context.Context, msg *path_mgmt.SegChangesIdReq,
a net.Addr, id uint64) (*path_mgmt.SegChangesIdReply, error)
SendSegChangesIdReply(ctx context.Context,
msg *path_mgmt.SegChangesIdReply, a net.Addr, id uint64) error
GetSegChanges(ctx context.Context, msg *path_mgmt.SegChangesReq,
a net.Addr, id uint64) (*path_mgmt.SegChangesReply, error)
SendSegChangesReply(ctx context.Context,
msg *path_mgmt.SegChangesReply, a net.Addr, id uint64) error
RequestChainIssue(ctx context.Context, msg *cert_mgmt.ChainIssReq, a net.Addr,
id uint64) (*cert_mgmt.ChainIssRep, error)
SendChainIssueReply(ctx context.Context, msg *cert_mgmt.ChainIssRep, a net.Addr,
id uint64) error
AddHandler(msgType MessageType, h Handler)
ListenAndServe()
CloseServer() error
}
func MessengerFromContext(ctx context.Context) (Messenger, bool) {
msger, ok := ctx.Value(messengerContextKey).(Messenger)
return msger, ok
}
type TrustStore interface {
GetValidChain(ctx context.Context, ia addr.IA, source net.Addr) (*cert.Chain, error)
GetValidTRC(ctx context.Context, isd addr.ISD, source net.Addr) (*trc.TRC, error)
GetValidCachedTRC(ctx context.Context, isd addr.ISD) (*trc.TRC, error)
GetChain(ctx context.Context, ia addr.IA, version uint64) (*cert.Chain, error)
GetTRC(ctx context.Context, isd addr.ISD, version uint64) (*trc.TRC, error)
NewTRCReqHandler(recurseAllowed bool) Handler
NewChainReqHandler(recurseAllowed bool) Handler
SetMessenger(msger Messenger)
}