forked from skycoin/dmsg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
231 lines (197 loc) · 5.06 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
package dmsg
import (
"context"
"net"
"time"
"github.com/sirupsen/logrus"
"github.com/skycoin/yamux"
"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/dmsg/noise"
)
// Stream represents a dmsg connection between two dmsg clients.
type Stream struct {
ses *ClientSession // back reference
yStr *yamux.Stream
// The following fields are to be filled after handshake.
lAddr Addr
rAddr Addr
ns *noise.Noise
nsConn *noise.ReadWriter
close func() // to be called when closing
log logrus.FieldLogger
}
func newInitiatingStream(cSes *ClientSession) (*Stream, error) {
yStr, err := cSes.ys.OpenStream()
if err != nil {
return nil, err
}
return &Stream{ses: cSes, yStr: yStr}, nil
}
func newRespondingStream(cSes *ClientSession) (*Stream, error) {
yStr, err := cSes.ys.AcceptStream()
if err != nil {
return nil, err
}
return &Stream{ses: cSes, yStr: yStr}, nil
}
// Close closes the dmsg stream.
func (s *Stream) Close() error {
if s == nil {
return nil
}
if s.close != nil {
s.close()
}
return s.yStr.Close()
}
// Logger returns the internal logrus.FieldLogger instance.
func (s *Stream) Logger() logrus.FieldLogger {
return s.log
}
func (s *Stream) writeRequest(rAddr Addr) (req StreamRequest, err error) {
// Reserve stream in porter.
var lPort uint16
if lPort, s.close, err = s.ses.porter.ReserveEphemeral(context.Background(), s); err != nil {
return
}
// Prepare fields.
s.prepareFields(true, Addr{PK: s.ses.LocalPK(), Port: lPort}, rAddr)
// Prepare request.
var nsMsg []byte
if nsMsg, err = s.ns.MakeHandshakeMessage(); err != nil {
return
}
req = StreamRequest{
Timestamp: time.Now().UnixNano(),
SrcAddr: s.lAddr,
DstAddr: s.rAddr,
NoiseMsg: nsMsg,
}
obj := MakeSignedStreamRequest(&req, s.ses.localSK())
// Write request.
err = s.ses.writeObject(s.yStr, obj)
return
}
func (s *Stream) readRequest() (req StreamRequest, err error) {
var obj SignedObject
if obj, err = s.ses.readObject(s.yStr); err != nil {
return
}
if req, err = obj.ObtainStreamRequest(); err != nil {
return
}
if err = req.Verify(0); err != nil {
return
}
if req.DstAddr.PK != s.ses.LocalPK() {
err = ErrReqInvalidDstPK
return
}
// Prepare fields.
s.prepareFields(false, req.DstAddr, req.SrcAddr)
if err = s.ns.ProcessHandshakeMessage(req.NoiseMsg); err != nil {
return
}
return
}
func (s *Stream) writeResponse(reqHash cipher.SHA256) error {
// Obtain associated local listener.
pVal, ok := s.ses.porter.PortValue(s.lAddr.Port)
if !ok {
return ErrReqNoListener
}
lis, ok := pVal.(*Listener)
if !ok {
return ErrReqNoListener
}
// Prepare and write response.
nsMsg, err := s.ns.MakeHandshakeMessage()
if err != nil {
return err
}
resp := StreamResponse{
ReqHash: reqHash,
Accepted: true,
NoiseMsg: nsMsg,
}
obj := MakeSignedStreamResponse(&resp, s.ses.localSK())
if err := s.ses.writeObject(s.yStr, obj); err != nil {
return err
}
// Push stream to listener.
return lis.introduceStream(s)
}
func (s *Stream) readResponse(req StreamRequest) error {
obj, err := s.ses.readObject(s.yStr)
if err != nil {
return err
}
resp, err := obj.ObtainStreamResponse()
if err != nil {
return err
}
if err := resp.Verify(req); err != nil {
return err
}
return s.ns.ProcessHandshakeMessage(resp.NoiseMsg)
}
func (s *Stream) prepareFields(init bool, lAddr, rAddr Addr) {
ns, err := noise.New(noise.HandshakeKK, noise.Config{
LocalPK: s.ses.LocalPK(),
LocalSK: s.ses.localSK(),
RemotePK: rAddr.PK,
Initiator: init,
})
if err != nil {
s.log.WithError(err).Panic("Failed to prepare stream noise object.")
}
s.lAddr = lAddr
s.rAddr = rAddr
s.ns = ns
s.nsConn = noise.NewReadWriter(s.yStr, s.ns)
s.log = s.ses.log.WithField("stream", s.lAddr.ShortString()+"->"+s.rAddr.ShortString())
}
// LocalAddr returns the local address of the dmsg stream.
func (s *Stream) LocalAddr() net.Addr {
return s.lAddr
}
// RawLocalAddr returns the local address as dmsg.Addr type.
func (s *Stream) RawLocalAddr() Addr {
return s.lAddr
}
// RemoteAddr returns the remote address of the dmsg stream.
func (s *Stream) RemoteAddr() net.Addr {
return s.rAddr
}
// RawRemoteAddr returns the remote address as dmsg.Addr type.
func (s *Stream) RawRemoteAddr() Addr {
return s.rAddr
}
// ServerPK returns the remote PK of the dmsg.Server used to relay frames to and from the remote client.
func (s *Stream) ServerPK() cipher.PubKey {
return s.ses.RemotePK()
}
// StreamID returns the stream ID.
func (s *Stream) StreamID() uint32 {
return s.yStr.StreamID()
}
// Read implements io.Reader
func (s *Stream) Read(b []byte) (int, error) {
return s.nsConn.Read(b)
}
// Write implements io.Writer
func (s *Stream) Write(b []byte) (int, error) {
return s.nsConn.Write(b)
}
// SetDeadline implements net.Conn
func (s *Stream) SetDeadline(t time.Time) error {
return s.yStr.SetDeadline(t)
}
// SetReadDeadline implements net.Conn
func (s *Stream) SetReadDeadline(t time.Time) error {
return s.yStr.SetReadDeadline(t)
}
// SetWriteDeadline implements net.Conn
func (s *Stream) SetWriteDeadline(t time.Time) error {
return s.yStr.SetWriteDeadline(t)
}