-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcircuit.go
215 lines (181 loc) · 4.29 KB
/
circuit.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
// 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 (
"github.com/tvdw/gotor/aes"
"github.com/tvdw/gotor/sha1"
"sync"
)
type CircuitID uint32
func (id CircuitID) MSB(vers LinkVersion) bool {
if vers < 4 {
if id&0x8000 == 0x8000 {
return true // outgoing
} else {
return false // incoming
}
} else {
if id&0x80000000 == 0x80000000 {
return true // outgoing
} else {
return false // incoming
}
}
}
type DirectionalCircuitState struct {
cipher aes.Cipher
digest *sha1.Digest
}
type DataDirection bool
const (
ForwardDirection = true
BackwardDirection = false
)
type Circuit struct {
id CircuitID
forward, backward DirectionalCircuitState
forwardWindow int
backwardWindow *Window
nextHop CircReadQueue
nextHopID CircuitID
streams map[StreamID]*Stream
extendState *CircuitHandshakeState
}
type RelayCircuit struct {
id, theirID CircuitID
previousHop CircReadQueue
}
type CircuitHandshakeState struct {
lock sync.Mutex
aborted bool
nextHop CircReadQueue
nextHopID CircuitID
}
type CircuitRequest struct {
NeverForRelay
localID CircuitID
connHint ConnectionHint
successQueue CircReadQueue
handshakeType uint16
handshakeData []byte
newHandshake bool
handshakeState *CircuitHandshakeState
}
type CircuitCreated struct {
NeverForRelay
id CircuitID
handshakeData []byte
newHandshake bool
}
func (c *CircuitRequest) CircID() CircuitID {
return 0
}
func (c *CircuitRequest) ReleaseBuffers() {
ReturnCellBuf(c.handshakeData)
c.handshakeState = nil
c.handshakeData = nil
c.successQueue = nil
}
func (c *CircuitCreated) CircID() CircuitID {
return c.id
}
func (c *CircuitCreated) ReleaseBuffers() {
ReturnCellBuf(c.handshakeData)
}
var zeroIv [16]byte
func NewCircuit(id CircuitID, fSeed, bSeed, fKey, bKey []byte) *Circuit {
if id == 0 {
panic("wtf?")
}
StatsNewCircuit()
aes_fwd := aes.New(fKey, zeroIv[:])
aes_rev := aes.New(bKey, zeroIv[:])
dig_fwd := sha1.New()
dig_fwd.Write(fSeed)
dig_rev := sha1.New()
dig_rev.Write(bSeed)
circ := &Circuit{
id: id,
forward: DirectionalCircuitState{
cipher: aes_fwd,
digest: dig_fwd,
},
backward: DirectionalCircuitState{
cipher: aes_rev,
digest: dig_rev,
},
backwardWindow: NewWindow(1000),
forwardWindow: 1000,
streams: make(map[StreamID]*Stream),
}
return circ
}
func (c *OnionConnection) destroyCircuit(circ *Circuit, announce, shouldRemove bool, reason DestroyReason) {
if shouldRemove {
delete(c.circuits, circ.id)
}
circ.backwardWindow.Abort()
for _, stream := range circ.streams {
stream.Destroy()
}
StatsDestroyCircuit()
if circ.extendState != nil {
circ.extendState.lock.Lock()
circ.extendState.aborted = true
if circ.extendState.nextHop != nil {
if circ.nextHop != nil {
panic("wtf-case")
}
circ.nextHop = circ.extendState.nextHop
circ.nextHopID = circ.extendState.nextHopID
}
circ.extendState.lock.Unlock()
circ.extendState = nil
}
if announce && circ.nextHop != nil {
circ.nextHop <- &CircuitDestroyed{
id: circ.nextHopID,
reason: reason,
forRelay: true,
}
}
// Set things to nil to mitigate possible memory leaks caused by other objects retaining this circuit (which is obviously a bug)
circ.nextHop = nil
circ.forward = DirectionalCircuitState{}
circ.backward = DirectionalCircuitState{}
circ.streams = nil
circ.backwardWindow = nil
}
func (c *OnionConnection) destroyRelayCircuit(circ *RelayCircuit, announce, shouldRemove bool, reason DestroyReason) {
if shouldRemove {
delete(c.relayCircuits, circ.id)
}
if announce && circ.previousHop != nil {
circ.previousHop <- &CircuitDestroyed{
id: circ.theirID,
reason: reason,
forRelay: false,
//truncate: true, // XXX?
}
}
}
func (c *OnionConnection) NewCircID() CircuitID {
for {
var b [4]byte
CRandBytes(b[:])
if c.isOutbound {
b[0] |= 0x80
} else {
b[0] &= 0x7f
}
cID := CircuitID(BigEndian.Uint32(b[:]))
if c.negotiatedVersion < 4 {
cID = (cID & 0xffff0000) >> 16 // Cut off the last 16 bits as we can't transmit them
}
_, exists := c.circuits[cID]
if !exists { // XXX check infinite loop
return cID
}
}
}