-
Notifications
You must be signed in to change notification settings - Fork 14
/
stream.go
194 lines (168 loc) · 4.12 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
// Copyright 2015 The GoTor Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"net"
"sync/atomic"
"time"
)
type StreamMessageType byte
const (
STREAM_CONNECTED StreamMessageType = iota
STREAM_DISCONNECTED
STREAM_SENDME
)
type Stream struct {
id StreamID
writeChan chan []byte
forwardWindow, backwardWindow *Window
finished int32
}
/* Stream cleanups
*
* When the writer fails to write we close the socket, triggering the reader to fail.
* When the reader fails to read we close the channel. Closing the channel will cause the goroutine to finish.
* When the circuit tells us to close by closing our channel, we just finish the goroutine, causing the other cleanup to happen.
*
* Finishing the goroutine means closing the socket and informing the channel that we're done (which should then dealloc us)
*/
func NewStream(id StreamID) (*Stream, error) {
s := &Stream{
id: id,
writeChan: make(chan []byte, 505),
forwardWindow: NewWindow(500),
backwardWindow: NewWindow(500),
}
return s, nil
}
func (s *Stream) Destroy() {
close(s.writeChan)
}
var dialer = net.Dialer{
KeepAlive: 0,
DualStack: true,
Timeout: 5 * time.Second,
}
func (s *Stream) Run(circID CircuitID, circWindow *Window, queue CircReadQueue, address string, port uint16, isDir bool, ep ExitPolicy) {
addr := ResolveDNS(address)[0]
if !(addr.Type == 4 || addr.Type == 6) {
queue <- &StreamControl{
circuitID: circID,
streamID: s.id,
data: STREAM_DISCONNECTED,
reason: STREAM_REASON_RESOLVEFAILED,
}
}
if !isDir && !ep.AllowsConnect(addr.Value, port) {
queue <- &StreamControl{
circuitID: circID,
streamID: s.id,
data: STREAM_DISCONNECTED,
reason: STREAM_REASON_EXITPOLICY,
remoteAddr: addr.Value,
}
return
}
conn, err := dialer.Dial("tcp", fmt.Sprintf("%s:%d", addr.String(), port))
if err != nil {
queue <- &StreamControl{
circuitID: circID,
streamID: s.id,
data: STREAM_DISCONNECTED,
reason: STREAM_REASON_CONNECTREFUSED,
}
return
}
queue <- &StreamControl{
circuitID: circID,
streamID: s.id,
data: STREAM_CONNECTED,
remoteAddr: addr.Value,
}
defer func() {
conn.Close()
atomic.StoreInt32(&s.finished, 1)
s.backwardWindow.Abort()
s.forwardWindow.Abort()
circWindow.Abort()
queue <- &StreamControl{
circuitID: circID,
streamID: s.id,
data: STREAM_DISCONNECTED,
reason: STREAM_REASON_DONE,
} // XXX this could deadlock
Log(LOG_CIRC, "Disconnected stream %d to %s", s.id, address)
}()
readQueue := make(chan []byte, 5)
go s.reader(conn, circWindow, readQueue)
for {
select {
case data, ok := <-s.writeChan:
if !ok {
return
}
_, err := conn.Write(data)
if err != nil {
return
}
ReturnCellBuf(data)
for len(s.writeChan) < 10 && s.forwardWindow.GetLevel() <= 450 {
s.forwardWindow.Refill(50)
queue <- &StreamControl{
data: STREAM_SENDME,
circuitID: circID,
streamID: s.id,
}
}
case data, ok := <-readQueue:
if !ok {
return
}
queue <- &StreamData{
circuitID: circID,
streamID: s.id,
data: data,
} // XXX this could deadlock
}
}
}
func (s *Stream) reader(conn net.Conn, circWindow *Window, queue chan []byte) {
var readBuf [4096]byte
for {
hasWnd1 := false
hasWnd2 := false
// Try to obtain permission to send the data.
for !(hasWnd1 && hasWnd2) {
done := atomic.LoadInt32(&s.finished)
if done != 0 {
close(queue)
return
}
if !hasWnd1 {
hasWnd1 = s.backwardWindow.Take()
continue
}
if !hasWnd2 {
hasWnd2 = circWindow.Take()
continue
}
}
bytes, err := conn.Read(readBuf[:])
if err != nil && bytes <= 0 {
close(queue)
return
}
for i := 0; i < bytes; {
s := MAX_RELAY_LEN
if s > bytes-i {
s = bytes-i
}
cell := GetCellBuf(false)
copy(cell, readBuf[i:])
queue <- cell[0:s] // XXX would it make sense to add a timeout here? This has proven to deadlock
i += s
}
}
}