-
Notifications
You must be signed in to change notification settings - Fork 114
/
stream.go
361 lines (314 loc) · 10.4 KB
/
stream.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package p2p
import (
"context"
"fmt"
"net"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/cockroachdb/errors"
"github.com/iotaledger/hive.go/core/autopeering/peer"
"github.com/iotaledger/hive.go/core/autopeering/peer/service"
"github.com/libp2p/go-libp2p-core/network"
libp2ppeer "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/multiformats/go-multiaddr"
"go.uber.org/atomic"
"google.golang.org/protobuf/proto"
"github.com/iotaledger/goshimmer/packages/node/libp2putil"
)
const (
defaultConnectionTimeout = 5 * time.Second // timeout after which the connection must be established.
ioTimeout = 4 * time.Second
)
var (
// ErrTimeout is returned when an expected incoming connection was not received in time.
ErrTimeout = errors.New("accept timeout")
// ErrDuplicateAccept is returned when the server already registered an accept request for that peer ID.
ErrDuplicateAccept = errors.New("accept request for that peer already exists")
// ErrNoP2P means that the given peer does not support the p2p service.
ErrNoP2P = errors.New("peer does not have a p2p service")
)
func (m *Manager) dialPeer(ctx context.Context, p *peer.Peer, opts []ConnectPeerOption) (map[protocol.ID]*PacketsStream, error) {
m.registeredProtocolsMutex.RLock()
defer m.registeredProtocolsMutex.RUnlock()
conf := buildConnectPeerConfig(opts)
p2pEndpoint := p.Services().Get(service.P2PKey)
if p2pEndpoint == nil {
return nil, ErrNoP2P
}
libp2pID, err := libp2putil.ToLibp2pPeerID(p)
if err != nil {
return nil, errors.WithStack(err)
}
addressStr := fmt.Sprintf("/ip4/%s/tcp/%d", p.IP(), p2pEndpoint.Port())
address, err := multiaddr.NewMultiaddr(addressStr)
if err != nil {
return nil, err
}
m.libp2pHost.Peerstore().AddAddr(libp2pID, address, peerstore.ConnectedAddrTTL)
if conf.useDefaultTimeout {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, defaultConnectionTimeout)
defer cancel()
}
streams := make(map[protocol.ID]*PacketsStream)
for protocolID := range m.registeredProtocols {
stream, err := m.initiateStream(ctx, libp2pID, protocolID)
if err != nil {
m.log.Errorf("dial %s / %s failed for proto %s: %w", address, p.ID(), protocolID, err)
continue
}
m.log.Debugw("outgoing stream negotiated",
"id", p.ID(),
"addr", stream.Conn().RemoteMultiaddr(),
"proto", protocolID,
)
streams[protocolID] = stream
}
if len(streams) == 0 {
return nil, errors.Errorf("no streams initiated with peer %s / %s", address, p.ID())
}
return streams, nil
}
func (m *Manager) acceptPeer(ctx context.Context, p *peer.Peer, opts []ConnectPeerOption) (map[protocol.ID]*PacketsStream, error) {
m.registeredProtocolsMutex.RLock()
defer m.registeredProtocolsMutex.RUnlock()
p2pEndpoint := p.Services().Get(service.P2PKey)
if p2pEndpoint == nil {
return nil, ErrNoP2P
}
handleInboundStream := func(ctx context.Context, protocolID protocol.ID) (*PacketsStream, error) {
if buildConnectPeerConfig(opts).useDefaultTimeout {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, defaultConnectionTimeout)
defer cancel()
}
amCtx, am, err := m.newAcceptMatcher(ctx, p, protocolID)
if err != nil {
return nil, errors.WithStack(err)
}
if am == nil {
return nil, errors.WithStack(ErrDuplicateAccept)
}
defer m.removeAcceptMatcher(am, protocolID)
m.log.Debugw("waiting for incoming stream", "id", am.Peer.ID(), "proto", protocolID)
am.StreamChMutex.RLock()
streamCh := am.StreamCh[protocolID]
am.StreamChMutex.RUnlock()
select {
case ps := <-streamCh:
if ps.Protocol() != protocolID {
return nil, errors.Errorf("accepted stream has wrong protocol: %s != %s", ps.Protocol(), protocolID)
}
return ps, nil
case <-amCtx.Done():
err := amCtx.Err()
if errors.Is(err, context.DeadlineExceeded) {
m.log.Debugw("accept timeout", "id", am.Peer.ID(), "proto", protocolID)
return nil, errors.WithStack(ErrTimeout)
}
m.log.Debugw("context error", "id", am.Peer.ID(), "err", err)
return nil, errors.WithStack(err)
}
}
var acceptWG sync.WaitGroup
streamsChan := make(chan *PacketsStream, len(m.registeredProtocols))
for protocolID := range m.registeredProtocols {
acceptWG.Add(1)
go func(protocolID protocol.ID) {
defer acceptWG.Done()
stream, err := handleInboundStream(ctx, protocolID)
if err != nil {
m.log.Errorf(
"accept %s / %s proto %s failed: %s",
net.JoinHostPort(p.IP().String(), strconv.Itoa(p2pEndpoint.Port())),
p.ID(),
protocolID,
err,
)
return
}
m.log.Debugw("incoming stream negotiated",
"id", p.ID(),
"addr", stream.Conn().RemoteMultiaddr(),
"proto", protocolID,
)
streamsChan <- stream
}(protocolID)
}
acceptWG.Wait()
close(streamsChan)
streams := make(map[protocol.ID]*PacketsStream)
for stream := range streamsChan {
streams[stream.Protocol()] = stream
}
if len(streams) == 0 {
return nil, errors.Errorf("no streams accepted from peer %s", p.ID())
}
return streams, nil
}
func (m *Manager) initiateStream(ctx context.Context, libp2pID libp2ppeer.ID, protocolID protocol.ID) (*PacketsStream, error) {
protocolHandler, registered := m.registeredProtocols[protocolID]
if !registered {
return nil, errors.Errorf("cannot initiate stream protocol %s is not registered", protocolID)
}
stream, err := m.GetP2PHost().NewStream(ctx, libp2pID, protocolID)
if err != nil {
return nil, err
}
ps := NewPacketsStream(stream, protocolHandler.PacketFactory)
if err := protocolHandler.NegotiationSend(ps); err != nil {
err = errors.Wrap(err, "failed to send negotiation block")
err = errors.CombineErrors(err, stream.Close())
return nil, err
}
return ps, nil
}
func (m *Manager) handleStream(stream network.Stream) {
m.registeredProtocolsMutex.RLock()
defer m.registeredProtocolsMutex.RUnlock()
protocolID := stream.Protocol()
protocolHandler, registered := m.registeredProtocols[protocolID]
if !registered {
m.log.Errorf("cannot accept stream: protocol %s is not registered", protocolID)
m.closeStream(stream)
return
}
ps := NewPacketsStream(stream, protocolHandler.PacketFactory)
if err := protocolHandler.NegotiationReceive(ps); err != nil {
m.log.Errorw("failed to receive negotiation message", "proto", protocolID, "err", err)
m.closeStream(stream)
return
}
am := m.matchNewStream(stream)
if am != nil {
am.StreamChMutex.RLock()
defer am.StreamChMutex.RUnlock()
streamCh := am.StreamCh[protocolID]
select {
case <-am.Ctx.Done():
case streamCh <- ps:
m.log.Debugw("incoming stream matched", "id", am.Peer.ID(), "proto", protocolID)
}
} else {
// close the connection if not matched
m.log.Debugw("unexpected connection", "addr", stream.Conn().RemoteMultiaddr(),
"id", stream.Conn().RemotePeer(), "proto", protocolID)
m.closeStream(stream)
stream.Conn().Close()
}
}
// AcceptMatcher holds data to match an existing connection with a peer.
type AcceptMatcher struct {
Peer *peer.Peer // connecting peer
Libp2pID libp2ppeer.ID
StreamChMutex sync.RWMutex
StreamCh map[protocol.ID]chan *PacketsStream
Ctx context.Context
CtxCancel context.CancelFunc
}
func (m *Manager) newAcceptMatcher(ctx context.Context, p *peer.Peer, protocolID protocol.ID) (context.Context, *AcceptMatcher, error) {
m.acceptMutex.Lock()
defer m.acceptMutex.Unlock()
libp2pID, err := libp2putil.ToLibp2pPeerID(p)
if err != nil {
return nil, nil, errors.WithStack(err)
}
acceptMatcher, acceptExists := m.acceptMap[libp2pID]
if acceptExists {
acceptMatcher.StreamChMutex.Lock()
defer acceptMatcher.StreamChMutex.Unlock()
if _, streamChanExists := acceptMatcher.StreamCh[protocolID]; streamChanExists {
return nil, nil, nil
}
acceptMatcher.StreamCh[protocolID] = make(chan *PacketsStream)
return acceptMatcher.Ctx, acceptMatcher, nil
}
cancelCtx, cancelCtxFunc := context.WithCancel(ctx)
am := &AcceptMatcher{
Peer: p,
Libp2pID: libp2pID,
StreamCh: make(map[protocol.ID]chan *PacketsStream),
Ctx: cancelCtx,
CtxCancel: cancelCtxFunc,
}
am.StreamCh[protocolID] = make(chan *PacketsStream)
m.acceptMap[libp2pID] = am
return cancelCtx, am, nil
}
func (m *Manager) removeAcceptMatcher(am *AcceptMatcher, protocolID protocol.ID) {
m.acceptMutex.Lock()
defer m.acceptMutex.Unlock()
existingAm := m.acceptMap[am.Libp2pID]
existingAm.StreamChMutex.Lock()
defer existingAm.StreamChMutex.Unlock()
delete(existingAm.StreamCh, protocolID)
if len(existingAm.StreamCh) == 0 {
delete(m.acceptMap, am.Libp2pID)
existingAm.CtxCancel()
}
}
func (m *Manager) matchNewStream(stream network.Stream) *AcceptMatcher {
m.acceptMutex.RLock()
defer m.acceptMutex.RUnlock()
am := m.acceptMap[stream.Conn().RemotePeer()]
return am
}
func (m *Manager) closeStream(s network.Stream) {
if err := s.Close(); err != nil {
m.log.Warnw("close error", "err", err)
}
}
// PacketsStream represents a stream of packets.
type PacketsStream struct {
network.Stream
packetFactory func() proto.Message
readerLock sync.Mutex
reader *libp2putil.UvarintReader
writerLock sync.Mutex
writer *libp2putil.UvarintWriter
packetsRead *atomic.Uint64
packetsWritten *atomic.Uint64
}
// NewPacketsStream creates a new PacketsStream.
func NewPacketsStream(stream network.Stream, packetFactory func() proto.Message) *PacketsStream {
return &PacketsStream{
Stream: stream,
packetFactory: packetFactory,
reader: libp2putil.NewDelimitedReader(stream),
writer: libp2putil.NewDelimitedWriter(stream),
packetsRead: atomic.NewUint64(0),
packetsWritten: atomic.NewUint64(0),
}
}
// WritePacket writes a packet to the stream.
func (ps *PacketsStream) WritePacket(message proto.Message) error {
ps.writerLock.Lock()
defer ps.writerLock.Unlock()
err := ps.writer.WriteBlk(message)
if err != nil {
return errors.WithStack(err)
}
ps.packetsWritten.Inc()
return nil
}
// ReadPacket reads a packet from the stream.
func (ps *PacketsStream) ReadPacket(message proto.Message) error {
ps.readerLock.Lock()
defer ps.readerLock.Unlock()
if err := ps.reader.ReadBlk(message); err != nil {
return errors.WithStack(err)
}
ps.packetsRead.Inc()
return nil
}
func isDeadlineUnsupportedError(err error) bool {
return strings.Contains(err.Error(), "deadline not supported")
}
func isTimeoutError(err error) bool {
return os.IsTimeout(errors.Unwrap(err))
}