-
Notifications
You must be signed in to change notification settings - Fork 14
/
streamcontrol.go
83 lines (73 loc) · 1.92 KB
/
streamcontrol.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
// 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
type StreamControl struct {
NeverForRelay
NoBuffers
circuitID CircuitID
streamID StreamID
data StreamMessageType
reason StreamEndReason
remoteAddr []byte
}
func (sd *StreamControl) CircID() CircuitID {
return sd.circuitID
}
func (sc *StreamControl) Handle(c *OnionConnection, circ *Circuit) ActionableError {
switch sc.data {
case STREAM_CONNECTED:
var data []byte
if sc.remoteAddr != nil {
if len(sc.remoteAddr) == 4 {
data = make([]byte, 8) //XXX
copy(data, sc.remoteAddr)
data[6] = 1
data[7] = 44
} else if len(sc.remoteAddr) == 16 {
data = make([]byte, 25) //XXX
data[4] = 6
copy(data[5:], sc.remoteAddr)
data[23] = 1
data[24] = 44
}
}
return c.sendRelayCell(circ, sc.streamID, BackwardDirection, RELAY_CONNECTED, data)
case STREAM_DISCONNECTED:
stream, ok := circ.streams[sc.streamID]
if !ok {
return nil
}
delete(circ.streams, sc.streamID)
stream.Destroy()
// We need to inform the OP that the connection died
var data []byte
if sc.remoteAddr != nil {
if len(sc.remoteAddr) == 4 {
data = make([]byte, 9) //XXX
data[0] = byte(sc.reason)
copy(data[1:], sc.remoteAddr)
data[7] = 1
data[8] = 44
} else if len(sc.remoteAddr) == 16 {
data = make([]byte, 26) //XXX
data[0] = byte(sc.reason)
data[5] = 6
copy(data[6:], sc.remoteAddr)
data[24] = 1
data[25] = 44
} else {
data = []byte{byte(sc.reason)}
}
}
return c.sendRelayCell(circ, sc.streamID, BackwardDirection, RELAY_END, data)
case STREAM_SENDME:
_, ok := circ.streams[sc.streamID]
if !ok {
return nil
}
return c.sendRelayCell(circ, sc.streamID, BackwardDirection, RELAY_SENDME, nil)
default:
panic("Did not understand our StreamControl message!")
}
}