/
conn.go
184 lines (156 loc) · 4.04 KB
/
conn.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
package curvecp
import (
"container/list"
"errors"
"net"
"time"
"github.com/johnwchadwick/curvecp/ringbuf"
"golang.org/x/crypto/nacl/box"
)
const (
numSendBlocks = 128 // *1024 = 128k of send buffer.
recvBufferSize = 64 * 1024 // 64k
)
var (
// TODO: make it an appropriate net.Error
deadlineExceeded = errors.New("deadline exceeded")
)
type opResult struct {
n int
err error
}
type block struct {
// The data to be sent.
buf []byte
// Position of the first byte of buf in the overall stream.
pos int64
// The backing array for buf. Static so that we can preallocate
// all the memory associated with a connection at the beginning.
arr [1024]byte
}
// Implements net.Conn. Used by both client and server, with different
// message/packet pumps.
type conn struct {
// Peer's long-term public key, aka its identity.
peerIdentity [32]byte
// The shared key used to seal/open boxes to/from this client.
sharedKey [32]byte
// The domain requested during initiation.
domain string
// from pump to conn, packets to process. Only Initiate and
// Message packets come through here.
packetIn chan packet
// The socket for sending. Don't read this, use packetIn for
// reading.
sock *net.UDPConn
// From user to pump, request to read/write some data.
readRequest chan []byte
writeRequest chan []byte
// Deadlines for those ops
readDeadline time.Time
writeDeadline time.Time
// From pump to user, result of a read or write.
ioResult chan opResult
// Blocks that needs to be sent.
toSend *list.List // of *block
// Freelist of blocks. All allocated on creation of the conn, we
// never allocate more.
sendFree *list.List // of *block
// Received data waiting for a reader.
received *ringbuf.Ringbuf
}
func newConn(sock *net.UDPConn, peerIdentity, publicKey, privateKey []byte, domain string) *conn {
if len(peerIdentity) != 32 || len(publicKey) != 32 || len(privateKey) != 32 {
panic("wrong key size")
}
c := &conn{
domain: domain,
packetIn: make(chan packet),
sock: sock,
readRequest: make(chan []byte),
writeRequest: make(chan []byte),
ioResult: make(chan opResult),
toSend: list.New(),
sendFree: list.New(),
received: ringbuf.New(recvBufferSize),
}
// Key setup.
copy(c.peerIdentity[:], peerIdentity)
var pub, priv [32]byte
copy(pub[:], publicKey)
copy(priv[:], privateKey)
box.Precompute(&c.sharedKey, &pub, &priv)
// Send blocks
for i := 0; i < numSendBlocks; i++ {
c.sendFree.PushBack(new(block))
}
go c.pump()
return c
}
func (c *conn) Read(b []byte) (int, error) {
var deadline <-chan time.Time
if !c.readDeadline.IsZero() {
deadline = time.After(c.readDeadline.Sub(time.Now()))
}
select {
case c.readRequest <- b:
case <-deadline:
return 0, deadlineExceeded
}
// Once readRequest has succeeded, this will return promptly, so
// don't reapply the deadline (plus, it would corrupt the stream
// to do so - pump is performing an operation on our behalf,
// ignoring that would cause a gap in the data).
res := <-c.ioResult
return res.n, res.err
}
func (c *conn) Write(b []byte) (int, error) {
var deadline <-chan time.Time
if !c.writeDeadline.IsZero() {
deadline = time.After(c.writeDeadline.Sub(time.Now()))
}
written := 0
for len(b) > 0 {
select {
case c.writeRequest <- b:
case <-deadline:
return written, deadlineExceeded
}
// See above, no deadline here.
res := <-c.ioResult
written += res.n
b = b[res.n:]
if res.err != nil {
return written, res.err
}
}
return written, nil
}
func (c *conn) Close() error {
return notImplemented
}
func (c *conn) LocalAddr() net.Addr {
return c.sock.LocalAddr()
}
func (c *conn) RemoteAddr() net.Addr {
return c.sock.RemoteAddr()
}
func (c *conn) SetDeadline(t time.Time) error {
// Not thread-safe. TODO: figure out if it's supposed to be.
c.readDeadline = t
c.writeDeadline = t
return nil
}
func (c *conn) SetReadDeadline(t time.Time) error {
c.readDeadline = t
return nil
}
func (c *conn) SetWriteDeadline(t time.Time) error {
c.writeDeadline = t
return nil
}
func (c *conn) pump() {
for {
select {}
}
}