-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcmdrouter.go
116 lines (91 loc) · 2.79 KB
/
cmdrouter.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
// 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 (
"errors"
"fmt"
)
type CircuitCommand interface {
CircID() CircuitID
ForRelay() bool
Handle(*OnionConnection, *Circuit) ActionableError
HandleRelay(*OnionConnection, *RelayCircuit) ActionableError
ReleaseBuffers()
}
type NeverForRelay struct {
}
type NoBuffers struct {
}
func (c *NeverForRelay) ForRelay() bool {
return false
}
func (c *NoBuffers) ReleaseBuffers() {
}
func (c *NeverForRelay) HandleRelay(*OnionConnection, *RelayCircuit) ActionableError {
panic("reached unreachable code")
}
func (c *OnionConnection) routeCellToFunction(cell Cell) ActionableError {
switch cell.Command() {
case CMD_CREATE_FAST:
return c.handleCreateFast(cell)
case CMD_RELAY, CMD_RELAY_EARLY:
// XXX CircType()
circID := cell.CircID()
circ, ok := c.circuits[circID]
if ok {
return c.handleRelayForward(circ, cell)
}
rcirc, ok := c.relayCircuits[circID]
if ok {
if cell.Command() == CMD_RELAY_EARLY {
return CloseConnection(errors.New("Dropping the connection - no way we're routing a RELAY_EARLY cell back"))
}
return c.handleRelayBackward(rcirc, cell)
}
// Not an error
Log(LOG_INFO, "Received a %s cell for an unknown circuit - dropping", cell.Command())
case CMD_CREATE, CMD_CREATE2:
newHandshake := cell.Command() == CMD_CREATE2
return c.handleCreate(cell, newHandshake)
case CMD_DESTROY:
return c.handleDestroy(cell)
case CMD_CREATED, CMD_CREATED2:
return c.handleCreated(cell, cell.Command() == CMD_CREATED2)
case CMD_PADDING, CMD_VPADDING:
// Can be ignored
case CMD_CERTS, CMD_NETINFO, CMD_AUTH_CHALLENGE, CMD_AUTHORIZE, CMD_AUTHENTICATE:
return CloseConnection(errors.New(fmt.Sprintf("Command %s not allowed at this point. Disconnecting", cell.Command())))
default:
Log(LOG_NOTICE, "Got a cell with command %s - dropping", cell.Command())
Log(LOG_DEBUG, "%v", cell)
}
return nil
}
func (c *OnionConnection) routeCircuitCommandToFunction(cmd CircuitCommand) ActionableError {
circID := cmd.CircID()
forRelay := cmd.ForRelay()
if !forRelay {
if circID == 0 { // Control command
return cmd.Handle(c, nil)
}
// Look in c.circuits
circ, ok := c.circuits[circID]
if !ok {
Log(LOG_INFO, "got internal command for nonexisting circuit %v", cmd)
return nil // It happens, nothing to worry about
}
return cmd.Handle(c, circ)
} else {
if circID == 0 { // Control command
return cmd.HandleRelay(c, nil)
}
// Look in c.relayCircuits
circ, ok := c.relayCircuits[circID]
if !ok {
Log(LOG_INFO, "got internal command for nonexisting relayCircuit %v", cmd)
return nil // It happens, nothing to worry about
}
return cmd.HandleRelay(c, circ)
}
}