go.crypto/ssh: struct renaming
This CL is in preparation for 6128059. * rename channel -> serverChan * rename chanlist -> chanList * normalise theirId/MyId/id/peersId -> localId/remoteId R=agl CC=golang-dev https://golang.org/cl/6174046
This commit is contained in:
+25
-25
@@ -70,7 +70,7 @@ const (
|
|||||||
ResourceShortage
|
ResourceShortage
|
||||||
)
|
)
|
||||||
|
|
||||||
type channel struct {
|
type serverChan struct {
|
||||||
// immutable once created
|
// immutable once created
|
||||||
chanType string
|
chanType string
|
||||||
extraData []byte
|
extraData []byte
|
||||||
@@ -81,7 +81,7 @@ type channel struct {
|
|||||||
dead bool
|
dead bool
|
||||||
|
|
||||||
serverConn *ServerConn
|
serverConn *ServerConn
|
||||||
myId, theirId uint32
|
localId, remoteId uint32
|
||||||
myWindow, theirWindow uint32
|
myWindow, theirWindow uint32
|
||||||
maxPacketSize uint32
|
maxPacketSize uint32
|
||||||
err error
|
err error
|
||||||
@@ -95,7 +95,7 @@ type channel struct {
|
|||||||
cond *sync.Cond
|
cond *sync.Cond
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *channel) Accept() error {
|
func (c *serverChan) Accept() error {
|
||||||
c.serverConn.lock.Lock()
|
c.serverConn.lock.Lock()
|
||||||
defer c.serverConn.lock.Unlock()
|
defer c.serverConn.lock.Unlock()
|
||||||
|
|
||||||
@@ -104,15 +104,15 @@ func (c *channel) Accept() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
confirm := channelOpenConfirmMsg{
|
confirm := channelOpenConfirmMsg{
|
||||||
PeersId: c.theirId,
|
PeersId: c.remoteId,
|
||||||
MyId: c.myId,
|
MyId: c.localId,
|
||||||
MyWindow: c.myWindow,
|
MyWindow: c.myWindow,
|
||||||
MaxPacketSize: c.maxPacketSize,
|
MaxPacketSize: c.maxPacketSize,
|
||||||
}
|
}
|
||||||
return c.serverConn.writePacket(marshal(msgChannelOpenConfirm, confirm))
|
return c.serverConn.writePacket(marshal(msgChannelOpenConfirm, confirm))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *channel) Reject(reason RejectionReason, message string) error {
|
func (c *serverChan) Reject(reason RejectionReason, message string) error {
|
||||||
c.serverConn.lock.Lock()
|
c.serverConn.lock.Lock()
|
||||||
defer c.serverConn.lock.Unlock()
|
defer c.serverConn.lock.Unlock()
|
||||||
|
|
||||||
@@ -121,7 +121,7 @@ func (c *channel) Reject(reason RejectionReason, message string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
reject := channelOpenFailureMsg{
|
reject := channelOpenFailureMsg{
|
||||||
PeersId: c.theirId,
|
PeersId: c.remoteId,
|
||||||
Reason: reason,
|
Reason: reason,
|
||||||
Message: message,
|
Message: message,
|
||||||
Language: "en",
|
Language: "en",
|
||||||
@@ -129,7 +129,7 @@ func (c *channel) Reject(reason RejectionReason, message string) error {
|
|||||||
return c.serverConn.writePacket(marshal(msgChannelOpenFailure, reject))
|
return c.serverConn.writePacket(marshal(msgChannelOpenFailure, reject))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *channel) handlePacket(packet interface{}) {
|
func (c *serverChan) handlePacket(packet interface{}) {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
|
||||||
@@ -157,7 +157,7 @@ func (c *channel) handlePacket(packet interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *channel) handleData(data []byte) {
|
func (c *serverChan) handleData(data []byte) {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
|
||||||
@@ -182,7 +182,7 @@ func (c *channel) handleData(data []byte) {
|
|||||||
c.cond.Signal()
|
c.cond.Signal()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *channel) Stderr() io.Writer {
|
func (c *serverChan) Stderr() io.Writer {
|
||||||
return extendedDataChannel{c: c, t: extendedDataStderr}
|
return extendedDataChannel{c: c, t: extendedDataStderr}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,7 +190,7 @@ func (c *channel) Stderr() io.Writer {
|
|||||||
// data of the given type.
|
// data of the given type.
|
||||||
type extendedDataChannel struct {
|
type extendedDataChannel struct {
|
||||||
t extendedDataTypeCode
|
t extendedDataTypeCode
|
||||||
c *channel
|
c *serverChan
|
||||||
}
|
}
|
||||||
|
|
||||||
func (edc extendedDataChannel) Write(data []byte) (n int, err error) {
|
func (edc extendedDataChannel) Write(data []byte) (n int, err error) {
|
||||||
@@ -208,7 +208,7 @@ func (edc extendedDataChannel) Write(data []byte) (n int, err error) {
|
|||||||
|
|
||||||
packet := make([]byte, 1+4+4+4+len(todo))
|
packet := make([]byte, 1+4+4+4+len(todo))
|
||||||
packet[0] = msgChannelExtendedData
|
packet[0] = msgChannelExtendedData
|
||||||
marshalUint32(packet[1:], c.theirId)
|
marshalUint32(packet[1:], c.remoteId)
|
||||||
marshalUint32(packet[5:], uint32(edc.t))
|
marshalUint32(packet[5:], uint32(edc.t))
|
||||||
marshalUint32(packet[9:], uint32(len(todo)))
|
marshalUint32(packet[9:], uint32(len(todo)))
|
||||||
copy(packet[13:], todo)
|
copy(packet[13:], todo)
|
||||||
@@ -228,12 +228,12 @@ func (edc extendedDataChannel) Write(data []byte) (n int, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *channel) Read(data []byte) (n int, err error) {
|
func (c *serverChan) Read(data []byte) (n int, err error) {
|
||||||
n, err, windowAdjustment := c.read(data)
|
n, err, windowAdjustment := c.read(data)
|
||||||
|
|
||||||
if windowAdjustment > 0 {
|
if windowAdjustment > 0 {
|
||||||
packet := marshal(msgChannelWindowAdjust, windowAdjustMsg{
|
packet := marshal(msgChannelWindowAdjust, windowAdjustMsg{
|
||||||
PeersId: c.theirId,
|
PeersId: c.remoteId,
|
||||||
AdditionalBytes: windowAdjustment,
|
AdditionalBytes: windowAdjustment,
|
||||||
})
|
})
|
||||||
c.serverConn.lock.Lock()
|
c.serverConn.lock.Lock()
|
||||||
@@ -247,7 +247,7 @@ func (c *channel) Read(data []byte) (n int, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *channel) read(data []byte) (n int, err error, windowAdjustment uint32) {
|
func (c *serverChan) read(data []byte) (n int, err error, windowAdjustment uint32) {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
|
||||||
@@ -299,7 +299,7 @@ func (c *channel) read(data []byte) (n int, err error, windowAdjustment uint32)
|
|||||||
|
|
||||||
// getWindowSpace takes, at most, max bytes of space from the peer's window. It
|
// getWindowSpace takes, at most, max bytes of space from the peer's window. It
|
||||||
// returns the number of bytes actually reserved.
|
// returns the number of bytes actually reserved.
|
||||||
func (c *channel) getWindowSpace(max uint32) (uint32, error) {
|
func (c *serverChan) getWindowSpace(max uint32) (uint32, error) {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
|
||||||
@@ -324,7 +324,7 @@ func (c *channel) getWindowSpace(max uint32) (uint32, error) {
|
|||||||
return taken, nil
|
return taken, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *channel) Write(data []byte) (n int, err error) {
|
func (c *serverChan) Write(data []byte) (n int, err error) {
|
||||||
for len(data) > 0 {
|
for len(data) > 0 {
|
||||||
var space uint32
|
var space uint32
|
||||||
if space, err = c.getWindowSpace(uint32(len(data))); err != nil {
|
if space, err = c.getWindowSpace(uint32(len(data))); err != nil {
|
||||||
@@ -338,7 +338,7 @@ func (c *channel) Write(data []byte) (n int, err error) {
|
|||||||
|
|
||||||
packet := make([]byte, 1+4+4+len(todo))
|
packet := make([]byte, 1+4+4+len(todo))
|
||||||
packet[0] = msgChannelData
|
packet[0] = msgChannelData
|
||||||
marshalUint32(packet[1:], c.theirId)
|
marshalUint32(packet[1:], c.remoteId)
|
||||||
marshalUint32(packet[5:], uint32(len(todo)))
|
marshalUint32(packet[5:], uint32(len(todo)))
|
||||||
copy(packet[9:], todo)
|
copy(packet[9:], todo)
|
||||||
|
|
||||||
@@ -357,7 +357,7 @@ func (c *channel) Write(data []byte) (n int, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *channel) Close() error {
|
func (c *serverChan) Close() error {
|
||||||
c.serverConn.lock.Lock()
|
c.serverConn.lock.Lock()
|
||||||
defer c.serverConn.lock.Unlock()
|
defer c.serverConn.lock.Unlock()
|
||||||
|
|
||||||
@@ -371,12 +371,12 @@ func (c *channel) Close() error {
|
|||||||
c.weClosed = true
|
c.weClosed = true
|
||||||
|
|
||||||
closeMsg := channelCloseMsg{
|
closeMsg := channelCloseMsg{
|
||||||
PeersId: c.theirId,
|
PeersId: c.remoteId,
|
||||||
}
|
}
|
||||||
return c.serverConn.writePacket(marshal(msgChannelClose, closeMsg))
|
return c.serverConn.writePacket(marshal(msgChannelClose, closeMsg))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *channel) AckRequest(ok bool) error {
|
func (c *serverChan) AckRequest(ok bool) error {
|
||||||
c.serverConn.lock.Lock()
|
c.serverConn.lock.Lock()
|
||||||
defer c.serverConn.lock.Unlock()
|
defer c.serverConn.lock.Unlock()
|
||||||
|
|
||||||
@@ -386,21 +386,21 @@ func (c *channel) AckRequest(ok bool) error {
|
|||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
ack := channelRequestFailureMsg{
|
ack := channelRequestFailureMsg{
|
||||||
PeersId: c.theirId,
|
PeersId: c.remoteId,
|
||||||
}
|
}
|
||||||
return c.serverConn.writePacket(marshal(msgChannelFailure, ack))
|
return c.serverConn.writePacket(marshal(msgChannelFailure, ack))
|
||||||
}
|
}
|
||||||
|
|
||||||
ack := channelRequestSuccessMsg{
|
ack := channelRequestSuccessMsg{
|
||||||
PeersId: c.theirId,
|
PeersId: c.remoteId,
|
||||||
}
|
}
|
||||||
return c.serverConn.writePacket(marshal(msgChannelSuccess, ack))
|
return c.serverConn.writePacket(marshal(msgChannelSuccess, ack))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *channel) ChannelType() string {
|
func (c *serverChan) ChannelType() string {
|
||||||
return c.chanType
|
return c.chanType
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *channel) ExtraData() []byte {
|
func (c *serverChan) ExtraData() []byte {
|
||||||
return c.extraData
|
return c.extraData
|
||||||
}
|
}
|
||||||
|
|||||||
+36
-36
@@ -22,7 +22,7 @@ var clientVersion = []byte("SSH-2.0-Go\r\n")
|
|||||||
type ClientConn struct {
|
type ClientConn struct {
|
||||||
*transport
|
*transport
|
||||||
config *ClientConfig
|
config *ClientConfig
|
||||||
chanlist // channels associated with this connection
|
chanList // channels associated with this connection
|
||||||
forwardList // forwarded tcpip connections from the remote side
|
forwardList // forwarded tcpip connections from the remote side
|
||||||
globalRequest
|
globalRequest
|
||||||
}
|
}
|
||||||
@@ -217,20 +217,20 @@ func (c *ClientConn) mainLoop() {
|
|||||||
// malformed data packet
|
// malformed data packet
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
peersId := uint32(packet[1])<<24 | uint32(packet[2])<<16 | uint32(packet[3])<<8 | uint32(packet[4])
|
remoteId := uint32(packet[1])<<24 | uint32(packet[2])<<16 | uint32(packet[3])<<8 | uint32(packet[4])
|
||||||
length := uint32(packet[5])<<24 | uint32(packet[6])<<16 | uint32(packet[7])<<8 | uint32(packet[8])
|
length := uint32(packet[5])<<24 | uint32(packet[6])<<16 | uint32(packet[7])<<8 | uint32(packet[8])
|
||||||
packet = packet[9:]
|
packet = packet[9:]
|
||||||
|
|
||||||
if length != uint32(len(packet)) {
|
if length != uint32(len(packet)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.getChan(peersId).stdout.handleData(packet)
|
c.getChan(remoteId).stdout.handleData(packet)
|
||||||
case msgChannelExtendedData:
|
case msgChannelExtendedData:
|
||||||
if len(packet) < 13 {
|
if len(packet) < 13 {
|
||||||
// malformed data packet
|
// malformed data packet
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
peersId := uint32(packet[1])<<24 | uint32(packet[2])<<16 | uint32(packet[3])<<8 | uint32(packet[4])
|
remoteId := uint32(packet[1])<<24 | uint32(packet[2])<<16 | uint32(packet[3])<<8 | uint32(packet[4])
|
||||||
datatype := uint32(packet[5])<<24 | uint32(packet[6])<<16 | uint32(packet[7])<<8 | uint32(packet[8])
|
datatype := uint32(packet[5])<<24 | uint32(packet[6])<<16 | uint32(packet[7])<<8 | uint32(packet[8])
|
||||||
length := uint32(packet[9])<<24 | uint32(packet[10])<<16 | uint32(packet[11])<<8 | uint32(packet[12])
|
length := uint32(packet[9])<<24 | uint32(packet[10])<<16 | uint32(packet[11])<<8 | uint32(packet[12])
|
||||||
packet = packet[13:]
|
packet = packet[13:]
|
||||||
@@ -242,7 +242,7 @@ func (c *ClientConn) mainLoop() {
|
|||||||
// for stderr on interactive sessions. Other data types are
|
// for stderr on interactive sessions. Other data types are
|
||||||
// silently discarded.
|
// silently discarded.
|
||||||
if datatype == 1 {
|
if datatype == 1 {
|
||||||
c.getChan(peersId).stderr.handleData(packet)
|
c.getChan(remoteId).stderr.handleData(packet)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
switch msg := decode(packet).(type) {
|
switch msg := decode(packet).(type) {
|
||||||
@@ -262,7 +262,7 @@ func (c *ClientConn) mainLoop() {
|
|||||||
ch.weClosed = true
|
ch.weClosed = true
|
||||||
ch.sendClose()
|
ch.sendClose()
|
||||||
}
|
}
|
||||||
c.chanlist.remove(msg.PeersId)
|
c.chanList.remove(msg.PeersId)
|
||||||
case *channelEOFMsg:
|
case *channelEOFMsg:
|
||||||
ch := c.getChan(msg.PeersId)
|
ch := c.getChan(msg.PeersId)
|
||||||
ch.stdout.eof()
|
ch.stdout.eof()
|
||||||
@@ -316,12 +316,12 @@ func (c *ClientConn) handleChanOpen(msg *channelOpenMsg) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
ch := c.newChan(c.transport)
|
ch := c.newChan(c.transport)
|
||||||
ch.peersId = msg.PeersId
|
ch.remoteId = msg.PeersId
|
||||||
ch.stdin.win.add(msg.PeersWindow)
|
ch.stdin.win.add(msg.PeersWindow)
|
||||||
|
|
||||||
m := channelOpenConfirmMsg{
|
m := channelOpenConfirmMsg{
|
||||||
PeersId: ch.peersId,
|
PeersId: ch.remoteId,
|
||||||
MyId: ch.id,
|
MyId: ch.localId,
|
||||||
MyWindow: 1 << 14,
|
MyWindow: 1 << 14,
|
||||||
MaxPacketSize: 1 << 15, // RFC 4253 6.1
|
MaxPacketSize: 1 << 15, // RFC 4253 6.1
|
||||||
}
|
}
|
||||||
@@ -356,10 +356,10 @@ func (c *ClientConn) sendGlobalRequest(m interface{}) (*globalRequestSuccessMsg,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sendConnectionFailed rejects an incoming channel identified
|
// sendConnectionFailed rejects an incoming channel identified
|
||||||
// by peersId.
|
// by remoteId.
|
||||||
func (c *ClientConn) sendConnectionFailed(peersId uint32) error {
|
func (c *ClientConn) sendConnectionFailed(remoteId uint32) error {
|
||||||
m := channelOpenFailureMsg{
|
m := channelOpenFailureMsg{
|
||||||
PeersId: peersId,
|
PeersId: remoteId,
|
||||||
Reason: ConnectionFailed,
|
Reason: ConnectionFailed,
|
||||||
Message: "invalid request",
|
Message: "invalid request",
|
||||||
Language: "en_US.UTF-8",
|
Language: "en_US.UTF-8",
|
||||||
@@ -426,22 +426,22 @@ func (c *ClientConfig) rand() io.Reader {
|
|||||||
// over a single SSH connection.
|
// over a single SSH connection.
|
||||||
type clientChan struct {
|
type clientChan struct {
|
||||||
packetWriter
|
packetWriter
|
||||||
id, peersId uint32
|
localId, remoteId uint32
|
||||||
stdin *chanWriter // receives window adjustments
|
stdin *chanWriter // receives window adjustments
|
||||||
stdout *chanReader // receives the payload of channelData messages
|
stdout *chanReader // receives the payload of channelData messages
|
||||||
stderr *chanReader // receives the payload of channelExtendedData messages
|
stderr *chanReader // receives the payload of channelExtendedData messages
|
||||||
msg chan interface{} // incoming messages
|
msg chan interface{} // incoming messages
|
||||||
theyClosed bool // indicates the close msg has been received from the remote side
|
theyClosed bool // indicates the close msg has been received from the remote side
|
||||||
weClosed bool // incidates the close msg has been sent from our side
|
weClosed bool // incidates the close msg has been sent from our side
|
||||||
}
|
}
|
||||||
|
|
||||||
// newClientChan returns a partially constructed *clientChan
|
// newClientChan returns a partially constructed *clientChan
|
||||||
// using the local id provided. To be usable clientChan.peersId
|
// using the local id provided. To be usable clientChan.remoteId
|
||||||
// needs to be assigned once known.
|
// needs to be assigned once known.
|
||||||
func newClientChan(t *transport, id uint32) *clientChan {
|
func newClientChan(t *transport, localId uint32) *clientChan {
|
||||||
c := &clientChan{
|
c := &clientChan{
|
||||||
packetWriter: t,
|
packetWriter: t,
|
||||||
id: id,
|
localId: localId,
|
||||||
msg: make(chan interface{}, 16),
|
msg: make(chan interface{}, 16),
|
||||||
}
|
}
|
||||||
c.stdin = &chanWriter{
|
c.stdin = &chanWriter{
|
||||||
@@ -460,12 +460,12 @@ func newClientChan(t *transport, id uint32) *clientChan {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// waitForChannelOpenResponse, if successful, fills out
|
// waitForChannelOpenResponse, if successful, fills out
|
||||||
// the peerId and records any initial window advertisement.
|
// the remoteId and records any initial window advertisement.
|
||||||
func (c *clientChan) waitForChannelOpenResponse() error {
|
func (c *clientChan) waitForChannelOpenResponse() error {
|
||||||
switch msg := (<-c.msg).(type) {
|
switch msg := (<-c.msg).(type) {
|
||||||
case *channelOpenConfirmMsg:
|
case *channelOpenConfirmMsg:
|
||||||
// fixup peersId field
|
// fixup remoteId field
|
||||||
c.peersId = msg.MyId
|
c.remoteId = msg.MyId
|
||||||
c.stdin.win.add(msg.MyWindow)
|
c.stdin.win.add(msg.MyWindow)
|
||||||
return nil
|
return nil
|
||||||
case *channelOpenFailureMsg:
|
case *channelOpenFailureMsg:
|
||||||
@@ -477,20 +477,20 @@ func (c *clientChan) waitForChannelOpenResponse() error {
|
|||||||
// sendEOF sends EOF to the server. RFC 4254 Section 5.3
|
// sendEOF sends EOF to the server. RFC 4254 Section 5.3
|
||||||
func (c *clientChan) sendEOF() error {
|
func (c *clientChan) sendEOF() error {
|
||||||
return c.writePacket(marshal(msgChannelEOF, channelEOFMsg{
|
return c.writePacket(marshal(msgChannelEOF, channelEOFMsg{
|
||||||
PeersId: c.peersId,
|
PeersId: c.remoteId,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
// sendClose signals the intent to close the channel.
|
// sendClose signals the intent to close the channel.
|
||||||
func (c *clientChan) sendClose() error {
|
func (c *clientChan) sendClose() error {
|
||||||
return c.writePacket(marshal(msgChannelClose, channelCloseMsg{
|
return c.writePacket(marshal(msgChannelClose, channelCloseMsg{
|
||||||
PeersId: c.peersId,
|
PeersId: c.remoteId,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *clientChan) sendWindowAdj(n int) error {
|
func (c *clientChan) sendWindowAdj(n int) error {
|
||||||
msg := windowAdjustMsg{
|
msg := windowAdjustMsg{
|
||||||
PeersId: c.peersId,
|
PeersId: c.remoteId,
|
||||||
AdditionalBytes: uint32(n),
|
AdditionalBytes: uint32(n),
|
||||||
}
|
}
|
||||||
return c.writePacket(marshal(msgChannelWindowAdjust, msg))
|
return c.writePacket(marshal(msgChannelWindowAdjust, msg))
|
||||||
@@ -506,17 +506,17 @@ func (c *clientChan) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Thread safe channel list.
|
// Thread safe channel list.
|
||||||
type chanlist struct {
|
type chanList struct {
|
||||||
// protects concurrent access to chans
|
// protects concurrent access to chans
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
// chans are indexed by the local id of the channel, clientChan.id.
|
// chans are indexed by the local id of the channel, clientChan.localId.
|
||||||
// The PeersId value of messages received by ClientConn.mainLoop is
|
// The PeersId value of messages received by ClientConn.mainLoop is
|
||||||
// used to locate the right local clientChan in this slice.
|
// used to locate the right local clientChan in this slice.
|
||||||
chans []*clientChan
|
chans []*clientChan
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allocate a new ClientChan with the next avail local id.
|
// Allocate a new ClientChan with the next avail local id.
|
||||||
func (c *chanlist) newChan(t *transport) *clientChan {
|
func (c *chanList) newChan(t *transport) *clientChan {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
for i := range c.chans {
|
for i := range c.chans {
|
||||||
@@ -532,7 +532,7 @@ func (c *chanlist) newChan(t *transport) *clientChan {
|
|||||||
return ch
|
return ch
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *chanlist) getChan(id uint32) *clientChan {
|
func (c *chanList) getChan(id uint32) *clientChan {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
if id >= uint32(len(c.chans)) {
|
if id >= uint32(len(c.chans)) {
|
||||||
@@ -541,13 +541,13 @@ func (c *chanlist) getChan(id uint32) *clientChan {
|
|||||||
return c.chans[int(id)]
|
return c.chans[int(id)]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *chanlist) remove(id uint32) {
|
func (c *chanList) remove(id uint32) {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
c.chans[int(id)] = nil
|
c.chans[int(id)] = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *chanlist) closeAll() {
|
func (c *chanList) closeAll() {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
|
|
||||||
@@ -575,10 +575,10 @@ func (w *chanWriter) Write(data []byte) (written int, err error) {
|
|||||||
// n cannot be larger than 2^31 as len(data) cannot
|
// n cannot be larger than 2^31 as len(data) cannot
|
||||||
// be larger than 2^31
|
// be larger than 2^31
|
||||||
n := int(w.win.reserve(uint32(len(data))))
|
n := int(w.win.reserve(uint32(len(data))))
|
||||||
peersId := w.clientChan.peersId
|
remoteId := w.clientChan.remoteId
|
||||||
packet := []byte{
|
packet := []byte{
|
||||||
msgChannelData,
|
msgChannelData,
|
||||||
byte(peersId >> 24), byte(peersId >> 16), byte(peersId >> 8), byte(peersId),
|
byte(remoteId >> 24), byte(remoteId >> 16), byte(remoteId >> 8), byte(remoteId),
|
||||||
byte(n >> 24), byte(n >> 16), byte(n >> 8), byte(n),
|
byte(n >> 24), byte(n >> 16), byte(n >> 8), byte(n),
|
||||||
}
|
}
|
||||||
if err = w.clientChan.writePacket(append(packet, data[:n]...)); err != nil {
|
if err = w.clientChan.writePacket(append(packet, data[:n]...)); err != nil {
|
||||||
|
|||||||
+8
-8
@@ -98,7 +98,7 @@ type ServerConn struct {
|
|||||||
*transport
|
*transport
|
||||||
config *ServerConfig
|
config *ServerConfig
|
||||||
|
|
||||||
channels map[uint32]*channel
|
channels map[uint32]*serverChan
|
||||||
nextChanId uint32
|
nextChanId uint32
|
||||||
|
|
||||||
// lock protects err and also allows Channels to serialise their writes
|
// lock protects err and also allows Channels to serialise their writes
|
||||||
@@ -123,7 +123,7 @@ type ServerConn struct {
|
|||||||
func Server(c net.Conn, config *ServerConfig) *ServerConn {
|
func Server(c net.Conn, config *ServerConfig) *ServerConn {
|
||||||
conn := &ServerConn{
|
conn := &ServerConn{
|
||||||
transport: newTransport(c, config.rand()),
|
transport: newTransport(c, config.rand()),
|
||||||
channels: make(map[uint32]*channel),
|
channels: make(map[uint32]*serverChan),
|
||||||
config: config,
|
config: config,
|
||||||
}
|
}
|
||||||
return conn
|
return conn
|
||||||
@@ -526,9 +526,9 @@ func (s *ServerConn) Accept() (Channel, error) {
|
|||||||
// malformed data packet
|
// malformed data packet
|
||||||
return nil, ParseError{msgChannelData}
|
return nil, ParseError{msgChannelData}
|
||||||
}
|
}
|
||||||
peersId := uint32(packet[1])<<24 | uint32(packet[2])<<16 | uint32(packet[3])<<8 | uint32(packet[4])
|
remoteId := uint32(packet[1])<<24 | uint32(packet[2])<<16 | uint32(packet[3])<<8 | uint32(packet[4])
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
c, ok := s.channels[peersId]
|
c, ok := s.channels[remoteId]
|
||||||
if !ok {
|
if !ok {
|
||||||
s.lock.Unlock()
|
s.lock.Unlock()
|
||||||
continue
|
continue
|
||||||
@@ -541,9 +541,9 @@ func (s *ServerConn) Accept() (Channel, error) {
|
|||||||
default:
|
default:
|
||||||
switch msg := decode(packet).(type) {
|
switch msg := decode(packet).(type) {
|
||||||
case *channelOpenMsg:
|
case *channelOpenMsg:
|
||||||
c := new(channel)
|
c := new(serverChan)
|
||||||
c.chanType = msg.ChanType
|
c.chanType = msg.ChanType
|
||||||
c.theirId = msg.PeersId
|
c.remoteId = msg.PeersId
|
||||||
c.theirWindow = msg.PeersWindow
|
c.theirWindow = msg.PeersWindow
|
||||||
c.maxPacketSize = msg.MaxPacketSize
|
c.maxPacketSize = msg.MaxPacketSize
|
||||||
c.extraData = msg.TypeSpecificData
|
c.extraData = msg.TypeSpecificData
|
||||||
@@ -553,9 +553,9 @@ func (s *ServerConn) Accept() (Channel, error) {
|
|||||||
c.pendingData = make([]byte, c.myWindow)
|
c.pendingData = make([]byte, c.myWindow)
|
||||||
|
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
c.myId = s.nextChanId
|
c.localId = s.nextChanId
|
||||||
s.nextChanId++
|
s.nextChanId++
|
||||||
s.channels[c.myId] = c
|
s.channels[c.localId] = c
|
||||||
s.lock.Unlock()
|
s.lock.Unlock()
|
||||||
return c, nil
|
return c, nil
|
||||||
|
|
||||||
|
|||||||
+8
-8
@@ -89,7 +89,7 @@ type setenvRequest struct {
|
|||||||
// command executed by Shell or Run.
|
// command executed by Shell or Run.
|
||||||
func (s *Session) Setenv(name, value string) error {
|
func (s *Session) Setenv(name, value string) error {
|
||||||
req := setenvRequest{
|
req := setenvRequest{
|
||||||
PeersId: s.peersId,
|
PeersId: s.remoteId,
|
||||||
Request: "env",
|
Request: "env",
|
||||||
WantReply: true,
|
WantReply: true,
|
||||||
Name: name,
|
Name: name,
|
||||||
@@ -120,7 +120,7 @@ type ptyRequestMsg struct {
|
|||||||
// RequestPty requests the association of a pty with the session on the remote host.
|
// RequestPty requests the association of a pty with the session on the remote host.
|
||||||
func (s *Session) RequestPty(term string, h, w int) error {
|
func (s *Session) RequestPty(term string, h, w int) error {
|
||||||
req := ptyRequestMsg{
|
req := ptyRequestMsg{
|
||||||
PeersId: s.peersId,
|
PeersId: s.remoteId,
|
||||||
Request: "pty-req",
|
Request: "pty-req",
|
||||||
WantReply: true,
|
WantReply: true,
|
||||||
Term: term,
|
Term: term,
|
||||||
@@ -148,7 +148,7 @@ type signalMsg struct {
|
|||||||
// sig is one of the SIG* constants.
|
// sig is one of the SIG* constants.
|
||||||
func (s *Session) Signal(sig Signal) error {
|
func (s *Session) Signal(sig Signal) error {
|
||||||
req := signalMsg{
|
req := signalMsg{
|
||||||
PeersId: s.peersId,
|
PeersId: s.remoteId,
|
||||||
Request: "signal",
|
Request: "signal",
|
||||||
WantReply: false,
|
WantReply: false,
|
||||||
Signal: string(sig),
|
Signal: string(sig),
|
||||||
@@ -172,7 +172,7 @@ func (s *Session) Start(cmd string) error {
|
|||||||
return errors.New("ssh: session already started")
|
return errors.New("ssh: session already started")
|
||||||
}
|
}
|
||||||
req := execMsg{
|
req := execMsg{
|
||||||
PeersId: s.peersId,
|
PeersId: s.remoteId,
|
||||||
Request: "exec",
|
Request: "exec",
|
||||||
WantReply: true,
|
WantReply: true,
|
||||||
Command: cmd,
|
Command: cmd,
|
||||||
@@ -212,7 +212,7 @@ func (s *Session) Shell() error {
|
|||||||
return errors.New("ssh: session already started")
|
return errors.New("ssh: session already started")
|
||||||
}
|
}
|
||||||
req := channelRequestMsg{
|
req := channelRequestMsg{
|
||||||
PeersId: s.peersId,
|
PeersId: s.remoteId,
|
||||||
Request: "shell",
|
Request: "shell",
|
||||||
WantReply: true,
|
WantReply: true,
|
||||||
}
|
}
|
||||||
@@ -434,15 +434,15 @@ func (c *ClientConn) NewSession() (*Session, error) {
|
|||||||
ch := c.newChan(c.transport)
|
ch := c.newChan(c.transport)
|
||||||
if err := c.writePacket(marshal(msgChannelOpen, channelOpenMsg{
|
if err := c.writePacket(marshal(msgChannelOpen, channelOpenMsg{
|
||||||
ChanType: "session",
|
ChanType: "session",
|
||||||
PeersId: ch.id,
|
PeersId: ch.localId,
|
||||||
PeersWindow: 1 << 14,
|
PeersWindow: 1 << 14,
|
||||||
MaxPacketSize: 1 << 15, // RFC 4253 6.1
|
MaxPacketSize: 1 << 15, // RFC 4253 6.1
|
||||||
})); err != nil {
|
})); err != nil {
|
||||||
c.chanlist.remove(ch.id)
|
c.chanList.remove(ch.localId)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := ch.waitForChannelOpenResponse(); err != nil {
|
if err := ch.waitForChannelOpenResponse(); err != nil {
|
||||||
c.chanlist.remove(ch.id)
|
c.chanList.remove(ch.localId)
|
||||||
return nil, fmt.Errorf("ssh: unable to open session: %v", err)
|
return nil, fmt.Errorf("ssh: unable to open session: %v", err)
|
||||||
}
|
}
|
||||||
return &Session{
|
return &Session{
|
||||||
|
|||||||
+15
-15
@@ -15,7 +15,7 @@ import (
|
|||||||
"code.google.com/p/go.crypto/ssh/terminal"
|
"code.google.com/p/go.crypto/ssh/terminal"
|
||||||
)
|
)
|
||||||
|
|
||||||
type serverType func(*channel)
|
type serverType func(*serverChan)
|
||||||
|
|
||||||
// dial constructs a new test server and returns a *ClientConn.
|
// dial constructs a new test server and returns a *ClientConn.
|
||||||
func dial(handler serverType, t *testing.T) *ClientConn {
|
func dial(handler serverType, t *testing.T) *ClientConn {
|
||||||
@@ -59,7 +59,7 @@ func dial(handler serverType, t *testing.T) *ClientConn {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ch.Accept()
|
ch.Accept()
|
||||||
go handler(ch.(*channel))
|
go handler(ch.(*serverChan))
|
||||||
}
|
}
|
||||||
t.Log("done")
|
t.Log("done")
|
||||||
}()
|
}()
|
||||||
@@ -311,7 +311,7 @@ type exitSignalMsg struct {
|
|||||||
Lang string
|
Lang string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newServerShell(ch *channel, prompt string) *ServerTerminal {
|
func newServerShell(ch *serverChan, prompt string) *ServerTerminal {
|
||||||
term := terminal.NewTerminal(ch, prompt)
|
term := terminal.NewTerminal(ch, prompt)
|
||||||
return &ServerTerminal{
|
return &ServerTerminal{
|
||||||
Term: term,
|
Term: term,
|
||||||
@@ -319,7 +319,7 @@ func newServerShell(ch *channel, prompt string) *ServerTerminal {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func exitStatusZeroHandler(ch *channel) {
|
func exitStatusZeroHandler(ch *serverChan) {
|
||||||
defer ch.Close()
|
defer ch.Close()
|
||||||
// this string is returned to stdout
|
// this string is returned to stdout
|
||||||
shell := newServerShell(ch, "> ")
|
shell := newServerShell(ch, "> ")
|
||||||
@@ -327,14 +327,14 @@ func exitStatusZeroHandler(ch *channel) {
|
|||||||
sendStatus(0, ch)
|
sendStatus(0, ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
func exitStatusNonZeroHandler(ch *channel) {
|
func exitStatusNonZeroHandler(ch *serverChan) {
|
||||||
defer ch.Close()
|
defer ch.Close()
|
||||||
shell := newServerShell(ch, "> ")
|
shell := newServerShell(ch, "> ")
|
||||||
shell.ReadLine()
|
shell.ReadLine()
|
||||||
sendStatus(15, ch)
|
sendStatus(15, ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
func exitSignalAndStatusHandler(ch *channel) {
|
func exitSignalAndStatusHandler(ch *serverChan) {
|
||||||
defer ch.Close()
|
defer ch.Close()
|
||||||
shell := newServerShell(ch, "> ")
|
shell := newServerShell(ch, "> ")
|
||||||
shell.ReadLine()
|
shell.ReadLine()
|
||||||
@@ -342,27 +342,27 @@ func exitSignalAndStatusHandler(ch *channel) {
|
|||||||
sendSignal("TERM", ch)
|
sendSignal("TERM", ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
func exitSignalHandler(ch *channel) {
|
func exitSignalHandler(ch *serverChan) {
|
||||||
defer ch.Close()
|
defer ch.Close()
|
||||||
shell := newServerShell(ch, "> ")
|
shell := newServerShell(ch, "> ")
|
||||||
shell.ReadLine()
|
shell.ReadLine()
|
||||||
sendSignal("TERM", ch)
|
sendSignal("TERM", ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
func exitSignalUnknownHandler(ch *channel) {
|
func exitSignalUnknownHandler(ch *serverChan) {
|
||||||
defer ch.Close()
|
defer ch.Close()
|
||||||
shell := newServerShell(ch, "> ")
|
shell := newServerShell(ch, "> ")
|
||||||
shell.ReadLine()
|
shell.ReadLine()
|
||||||
sendSignal("SYS", ch)
|
sendSignal("SYS", ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
func exitWithoutSignalOrStatus(ch *channel) {
|
func exitWithoutSignalOrStatus(ch *serverChan) {
|
||||||
defer ch.Close()
|
defer ch.Close()
|
||||||
shell := newServerShell(ch, "> ")
|
shell := newServerShell(ch, "> ")
|
||||||
shell.ReadLine()
|
shell.ReadLine()
|
||||||
}
|
}
|
||||||
|
|
||||||
func shellHandler(ch *channel) {
|
func shellHandler(ch *serverChan) {
|
||||||
defer ch.Close()
|
defer ch.Close()
|
||||||
// this string is returned to stdout
|
// this string is returned to stdout
|
||||||
shell := newServerShell(ch, "golang")
|
shell := newServerShell(ch, "golang")
|
||||||
@@ -370,9 +370,9 @@ func shellHandler(ch *channel) {
|
|||||||
sendStatus(0, ch)
|
sendStatus(0, ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendStatus(status uint32, ch *channel) {
|
func sendStatus(status uint32, ch *serverChan) {
|
||||||
msg := exitStatusMsg{
|
msg := exitStatusMsg{
|
||||||
PeersId: ch.theirId,
|
PeersId: ch.remoteId,
|
||||||
Request: "exit-status",
|
Request: "exit-status",
|
||||||
WantReply: false,
|
WantReply: false,
|
||||||
Status: status,
|
Status: status,
|
||||||
@@ -380,9 +380,9 @@ func sendStatus(status uint32, ch *channel) {
|
|||||||
ch.serverConn.writePacket(marshal(msgChannelRequest, msg))
|
ch.serverConn.writePacket(marshal(msgChannelRequest, msg))
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendSignal(signal string, ch *channel) {
|
func sendSignal(signal string, ch *serverChan) {
|
||||||
sig := exitSignalMsg{
|
sig := exitSignalMsg{
|
||||||
PeersId: ch.theirId,
|
PeersId: ch.remoteId,
|
||||||
Request: "exit-signal",
|
Request: "exit-signal",
|
||||||
WantReply: false,
|
WantReply: false,
|
||||||
Signal: signal,
|
Signal: signal,
|
||||||
@@ -393,7 +393,7 @@ func sendSignal(signal string, ch *channel) {
|
|||||||
ch.serverConn.writePacket(marshal(msgChannelRequest, sig))
|
ch.serverConn.writePacket(marshal(msgChannelRequest, sig))
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendInvalidRecord(ch *channel) {
|
func sendInvalidRecord(ch *serverChan) {
|
||||||
defer ch.Close()
|
defer ch.Close()
|
||||||
packet := make([]byte, 1+4+4+1)
|
packet := make([]byte, 1+4+4+1)
|
||||||
packet[0] = msgChannelData
|
packet[0] = msgChannelData
|
||||||
|
|||||||
+3
-3
@@ -209,7 +209,7 @@ func (c *ClientConn) dial(laddr string, lport int, raddr string, rport int) (*tc
|
|||||||
ch := c.newChan(c.transport)
|
ch := c.newChan(c.transport)
|
||||||
if err := c.writePacket(marshal(msgChannelOpen, channelOpenDirectMsg{
|
if err := c.writePacket(marshal(msgChannelOpen, channelOpenDirectMsg{
|
||||||
ChanType: "direct-tcpip",
|
ChanType: "direct-tcpip",
|
||||||
PeersId: ch.id,
|
PeersId: ch.localId,
|
||||||
PeersWindow: 1 << 14,
|
PeersWindow: 1 << 14,
|
||||||
MaxPacketSize: 1 << 15, // RFC 4253 6.1
|
MaxPacketSize: 1 << 15, // RFC 4253 6.1
|
||||||
raddr: raddr,
|
raddr: raddr,
|
||||||
@@ -217,11 +217,11 @@ func (c *ClientConn) dial(laddr string, lport int, raddr string, rport int) (*tc
|
|||||||
laddr: laddr,
|
laddr: laddr,
|
||||||
lport: uint32(lport),
|
lport: uint32(lport),
|
||||||
})); err != nil {
|
})); err != nil {
|
||||||
c.chanlist.remove(ch.id)
|
c.chanList.remove(ch.localId)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := ch.waitForChannelOpenResponse(); err != nil {
|
if err := ch.waitForChannelOpenResponse(); err != nil {
|
||||||
c.chanlist.remove(ch.id)
|
c.chanList.remove(ch.localId)
|
||||||
return nil, fmt.Errorf("ssh: unable to open direct tcpip connection: %v", err)
|
return nil, fmt.Errorf("ssh: unable to open direct tcpip connection: %v", err)
|
||||||
}
|
}
|
||||||
return &tcpChan{
|
return &tcpChan{
|
||||||
|
|||||||
Reference in New Issue
Block a user