From 68e64b8f78602a073405f904abfeb2d376d77ecd Mon Sep 17 00:00:00 2001 From: Dave Cheney Date: Sat, 5 May 2012 07:59:48 +1000 Subject: [PATCH] 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 --- ssh/channel.go | 50 +++++++++++++++---------------- ssh/client.go | 72 ++++++++++++++++++++++----------------------- ssh/server.go | 16 +++++----- ssh/session.go | 16 +++++----- ssh/session_test.go | 30 +++++++++---------- ssh/tcpip.go | 6 ++-- 6 files changed, 95 insertions(+), 95 deletions(-) diff --git a/ssh/channel.go b/ssh/channel.go index 415d8d8..88f18a3 100644 --- a/ssh/channel.go +++ b/ssh/channel.go @@ -70,7 +70,7 @@ const ( ResourceShortage ) -type channel struct { +type serverChan struct { // immutable once created chanType string extraData []byte @@ -81,7 +81,7 @@ type channel struct { dead bool serverConn *ServerConn - myId, theirId uint32 + localId, remoteId uint32 myWindow, theirWindow uint32 maxPacketSize uint32 err error @@ -95,7 +95,7 @@ type channel struct { cond *sync.Cond } -func (c *channel) Accept() error { +func (c *serverChan) Accept() error { c.serverConn.lock.Lock() defer c.serverConn.lock.Unlock() @@ -104,15 +104,15 @@ func (c *channel) Accept() error { } confirm := channelOpenConfirmMsg{ - PeersId: c.theirId, - MyId: c.myId, + PeersId: c.remoteId, + MyId: c.localId, MyWindow: c.myWindow, MaxPacketSize: c.maxPacketSize, } 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() defer c.serverConn.lock.Unlock() @@ -121,7 +121,7 @@ func (c *channel) Reject(reason RejectionReason, message string) error { } reject := channelOpenFailureMsg{ - PeersId: c.theirId, + PeersId: c.remoteId, Reason: reason, Message: message, Language: "en", @@ -129,7 +129,7 @@ func (c *channel) Reject(reason RejectionReason, message string) error { return c.serverConn.writePacket(marshal(msgChannelOpenFailure, reject)) } -func (c *channel) handlePacket(packet interface{}) { +func (c *serverChan) handlePacket(packet interface{}) { c.lock.Lock() 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() defer c.lock.Unlock() @@ -182,7 +182,7 @@ func (c *channel) handleData(data []byte) { c.cond.Signal() } -func (c *channel) Stderr() io.Writer { +func (c *serverChan) Stderr() io.Writer { return extendedDataChannel{c: c, t: extendedDataStderr} } @@ -190,7 +190,7 @@ func (c *channel) Stderr() io.Writer { // data of the given type. type extendedDataChannel struct { t extendedDataTypeCode - c *channel + c *serverChan } 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[0] = msgChannelExtendedData - marshalUint32(packet[1:], c.theirId) + marshalUint32(packet[1:], c.remoteId) marshalUint32(packet[5:], uint32(edc.t)) marshalUint32(packet[9:], uint32(len(todo))) copy(packet[13:], todo) @@ -228,12 +228,12 @@ func (edc extendedDataChannel) Write(data []byte) (n int, err error) { 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) if windowAdjustment > 0 { packet := marshal(msgChannelWindowAdjust, windowAdjustMsg{ - PeersId: c.theirId, + PeersId: c.remoteId, AdditionalBytes: windowAdjustment, }) c.serverConn.lock.Lock() @@ -247,7 +247,7 @@ func (c *channel) Read(data []byte) (n int, err error) { 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() 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 // 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() defer c.lock.Unlock() @@ -324,7 +324,7 @@ func (c *channel) getWindowSpace(max uint32) (uint32, error) { 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 { var space uint32 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[0] = msgChannelData - marshalUint32(packet[1:], c.theirId) + marshalUint32(packet[1:], c.remoteId) marshalUint32(packet[5:], uint32(len(todo))) copy(packet[9:], todo) @@ -357,7 +357,7 @@ func (c *channel) Write(data []byte) (n int, err error) { return } -func (c *channel) Close() error { +func (c *serverChan) Close() error { c.serverConn.lock.Lock() defer c.serverConn.lock.Unlock() @@ -371,12 +371,12 @@ func (c *channel) Close() error { c.weClosed = true closeMsg := channelCloseMsg{ - PeersId: c.theirId, + PeersId: c.remoteId, } 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() defer c.serverConn.lock.Unlock() @@ -386,21 +386,21 @@ func (c *channel) AckRequest(ok bool) error { if !ok { ack := channelRequestFailureMsg{ - PeersId: c.theirId, + PeersId: c.remoteId, } return c.serverConn.writePacket(marshal(msgChannelFailure, ack)) } ack := channelRequestSuccessMsg{ - PeersId: c.theirId, + PeersId: c.remoteId, } return c.serverConn.writePacket(marshal(msgChannelSuccess, ack)) } -func (c *channel) ChannelType() string { +func (c *serverChan) ChannelType() string { return c.chanType } -func (c *channel) ExtraData() []byte { +func (c *serverChan) ExtraData() []byte { return c.extraData } diff --git a/ssh/client.go b/ssh/client.go index e16d9fa..5a40ec4 100644 --- a/ssh/client.go +++ b/ssh/client.go @@ -22,7 +22,7 @@ var clientVersion = []byte("SSH-2.0-Go\r\n") type ClientConn struct { *transport config *ClientConfig - chanlist // channels associated with this connection + chanList // channels associated with this connection forwardList // forwarded tcpip connections from the remote side globalRequest } @@ -217,20 +217,20 @@ func (c *ClientConn) mainLoop() { // malformed data packet 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]) packet = packet[9:] if length != uint32(len(packet)) { return } - c.getChan(peersId).stdout.handleData(packet) + c.getChan(remoteId).stdout.handleData(packet) case msgChannelExtendedData: if len(packet) < 13 { // malformed data packet 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]) length := uint32(packet[9])<<24 | uint32(packet[10])<<16 | uint32(packet[11])<<8 | uint32(packet[12]) packet = packet[13:] @@ -242,7 +242,7 @@ func (c *ClientConn) mainLoop() { // for stderr on interactive sessions. Other data types are // silently discarded. if datatype == 1 { - c.getChan(peersId).stderr.handleData(packet) + c.getChan(remoteId).stderr.handleData(packet) } default: switch msg := decode(packet).(type) { @@ -262,7 +262,7 @@ func (c *ClientConn) mainLoop() { ch.weClosed = true ch.sendClose() } - c.chanlist.remove(msg.PeersId) + c.chanList.remove(msg.PeersId) case *channelEOFMsg: ch := c.getChan(msg.PeersId) ch.stdout.eof() @@ -316,12 +316,12 @@ func (c *ClientConn) handleChanOpen(msg *channelOpenMsg) { return } ch := c.newChan(c.transport) - ch.peersId = msg.PeersId + ch.remoteId = msg.PeersId ch.stdin.win.add(msg.PeersWindow) m := channelOpenConfirmMsg{ - PeersId: ch.peersId, - MyId: ch.id, + PeersId: ch.remoteId, + MyId: ch.localId, MyWindow: 1 << 14, MaxPacketSize: 1 << 15, // RFC 4253 6.1 } @@ -356,10 +356,10 @@ func (c *ClientConn) sendGlobalRequest(m interface{}) (*globalRequestSuccessMsg, } // sendConnectionFailed rejects an incoming channel identified -// by peersId. -func (c *ClientConn) sendConnectionFailed(peersId uint32) error { +// by remoteId. +func (c *ClientConn) sendConnectionFailed(remoteId uint32) error { m := channelOpenFailureMsg{ - PeersId: peersId, + PeersId: remoteId, Reason: ConnectionFailed, Message: "invalid request", Language: "en_US.UTF-8", @@ -426,22 +426,22 @@ func (c *ClientConfig) rand() io.Reader { // over a single SSH connection. type clientChan struct { packetWriter - id, peersId uint32 - stdin *chanWriter // receives window adjustments - stdout *chanReader // receives the payload of channelData messages - stderr *chanReader // receives the payload of channelExtendedData messages - msg chan interface{} // incoming messages - 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 + localId, remoteId uint32 + stdin *chanWriter // receives window adjustments + stdout *chanReader // receives the payload of channelData messages + stderr *chanReader // receives the payload of channelExtendedData messages + msg chan interface{} // incoming messages + 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 } // 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. -func newClientChan(t *transport, id uint32) *clientChan { +func newClientChan(t *transport, localId uint32) *clientChan { c := &clientChan{ packetWriter: t, - id: id, + localId: localId, msg: make(chan interface{}, 16), } c.stdin = &chanWriter{ @@ -460,12 +460,12 @@ func newClientChan(t *transport, id uint32) *clientChan { } // 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 { switch msg := (<-c.msg).(type) { case *channelOpenConfirmMsg: - // fixup peersId field - c.peersId = msg.MyId + // fixup remoteId field + c.remoteId = msg.MyId c.stdin.win.add(msg.MyWindow) return nil case *channelOpenFailureMsg: @@ -477,20 +477,20 @@ func (c *clientChan) waitForChannelOpenResponse() error { // sendEOF sends EOF to the server. RFC 4254 Section 5.3 func (c *clientChan) sendEOF() error { return c.writePacket(marshal(msgChannelEOF, channelEOFMsg{ - PeersId: c.peersId, + PeersId: c.remoteId, })) } // sendClose signals the intent to close the channel. func (c *clientChan) sendClose() error { return c.writePacket(marshal(msgChannelClose, channelCloseMsg{ - PeersId: c.peersId, + PeersId: c.remoteId, })) } func (c *clientChan) sendWindowAdj(n int) error { msg := windowAdjustMsg{ - PeersId: c.peersId, + PeersId: c.remoteId, AdditionalBytes: uint32(n), } return c.writePacket(marshal(msgChannelWindowAdjust, msg)) @@ -506,17 +506,17 @@ func (c *clientChan) Close() error { } // Thread safe channel list. -type chanlist struct { +type chanList struct { // protects concurrent access to chans 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 // used to locate the right local clientChan in this slice. chans []*clientChan } // 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() defer c.Unlock() for i := range c.chans { @@ -532,7 +532,7 @@ func (c *chanlist) newChan(t *transport) *clientChan { return ch } -func (c *chanlist) getChan(id uint32) *clientChan { +func (c *chanList) getChan(id uint32) *clientChan { c.Lock() defer c.Unlock() if id >= uint32(len(c.chans)) { @@ -541,13 +541,13 @@ func (c *chanlist) getChan(id uint32) *clientChan { return c.chans[int(id)] } -func (c *chanlist) remove(id uint32) { +func (c *chanList) remove(id uint32) { c.Lock() defer c.Unlock() c.chans[int(id)] = nil } -func (c *chanlist) closeAll() { +func (c *chanList) closeAll() { c.Lock() 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 // be larger than 2^31 n := int(w.win.reserve(uint32(len(data)))) - peersId := w.clientChan.peersId + remoteId := w.clientChan.remoteId packet := []byte{ 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), } if err = w.clientChan.writePacket(append(packet, data[:n]...)); err != nil { diff --git a/ssh/server.go b/ssh/server.go index 155e685..293c1c5 100644 --- a/ssh/server.go +++ b/ssh/server.go @@ -98,7 +98,7 @@ type ServerConn struct { *transport config *ServerConfig - channels map[uint32]*channel + channels map[uint32]*serverChan nextChanId uint32 // 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 { conn := &ServerConn{ transport: newTransport(c, config.rand()), - channels: make(map[uint32]*channel), + channels: make(map[uint32]*serverChan), config: config, } return conn @@ -526,9 +526,9 @@ func (s *ServerConn) Accept() (Channel, error) { // malformed data packet 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() - c, ok := s.channels[peersId] + c, ok := s.channels[remoteId] if !ok { s.lock.Unlock() continue @@ -541,9 +541,9 @@ func (s *ServerConn) Accept() (Channel, error) { default: switch msg := decode(packet).(type) { case *channelOpenMsg: - c := new(channel) + c := new(serverChan) c.chanType = msg.ChanType - c.theirId = msg.PeersId + c.remoteId = msg.PeersId c.theirWindow = msg.PeersWindow c.maxPacketSize = msg.MaxPacketSize c.extraData = msg.TypeSpecificData @@ -553,9 +553,9 @@ func (s *ServerConn) Accept() (Channel, error) { c.pendingData = make([]byte, c.myWindow) s.lock.Lock() - c.myId = s.nextChanId + c.localId = s.nextChanId s.nextChanId++ - s.channels[c.myId] = c + s.channels[c.localId] = c s.lock.Unlock() return c, nil diff --git a/ssh/session.go b/ssh/session.go index 7948c00..22f9e91 100644 --- a/ssh/session.go +++ b/ssh/session.go @@ -89,7 +89,7 @@ type setenvRequest struct { // command executed by Shell or Run. func (s *Session) Setenv(name, value string) error { req := setenvRequest{ - PeersId: s.peersId, + PeersId: s.remoteId, Request: "env", WantReply: true, Name: name, @@ -120,7 +120,7 @@ type ptyRequestMsg struct { // RequestPty requests the association of a pty with the session on the remote host. func (s *Session) RequestPty(term string, h, w int) error { req := ptyRequestMsg{ - PeersId: s.peersId, + PeersId: s.remoteId, Request: "pty-req", WantReply: true, Term: term, @@ -148,7 +148,7 @@ type signalMsg struct { // sig is one of the SIG* constants. func (s *Session) Signal(sig Signal) error { req := signalMsg{ - PeersId: s.peersId, + PeersId: s.remoteId, Request: "signal", WantReply: false, Signal: string(sig), @@ -172,7 +172,7 @@ func (s *Session) Start(cmd string) error { return errors.New("ssh: session already started") } req := execMsg{ - PeersId: s.peersId, + PeersId: s.remoteId, Request: "exec", WantReply: true, Command: cmd, @@ -212,7 +212,7 @@ func (s *Session) Shell() error { return errors.New("ssh: session already started") } req := channelRequestMsg{ - PeersId: s.peersId, + PeersId: s.remoteId, Request: "shell", WantReply: true, } @@ -434,15 +434,15 @@ func (c *ClientConn) NewSession() (*Session, error) { ch := c.newChan(c.transport) if err := c.writePacket(marshal(msgChannelOpen, channelOpenMsg{ ChanType: "session", - PeersId: ch.id, + PeersId: ch.localId, PeersWindow: 1 << 14, MaxPacketSize: 1 << 15, // RFC 4253 6.1 })); err != nil { - c.chanlist.remove(ch.id) + c.chanList.remove(ch.localId) return nil, err } 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 &Session{ diff --git a/ssh/session_test.go b/ssh/session_test.go index 5d6135e..041da65 100644 --- a/ssh/session_test.go +++ b/ssh/session_test.go @@ -15,7 +15,7 @@ import ( "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. func dial(handler serverType, t *testing.T) *ClientConn { @@ -59,7 +59,7 @@ func dial(handler serverType, t *testing.T) *ClientConn { continue } ch.Accept() - go handler(ch.(*channel)) + go handler(ch.(*serverChan)) } t.Log("done") }() @@ -311,7 +311,7 @@ type exitSignalMsg struct { Lang string } -func newServerShell(ch *channel, prompt string) *ServerTerminal { +func newServerShell(ch *serverChan, prompt string) *ServerTerminal { term := terminal.NewTerminal(ch, prompt) return &ServerTerminal{ Term: term, @@ -319,7 +319,7 @@ func newServerShell(ch *channel, prompt string) *ServerTerminal { } } -func exitStatusZeroHandler(ch *channel) { +func exitStatusZeroHandler(ch *serverChan) { defer ch.Close() // this string is returned to stdout shell := newServerShell(ch, "> ") @@ -327,14 +327,14 @@ func exitStatusZeroHandler(ch *channel) { sendStatus(0, ch) } -func exitStatusNonZeroHandler(ch *channel) { +func exitStatusNonZeroHandler(ch *serverChan) { defer ch.Close() shell := newServerShell(ch, "> ") shell.ReadLine() sendStatus(15, ch) } -func exitSignalAndStatusHandler(ch *channel) { +func exitSignalAndStatusHandler(ch *serverChan) { defer ch.Close() shell := newServerShell(ch, "> ") shell.ReadLine() @@ -342,27 +342,27 @@ func exitSignalAndStatusHandler(ch *channel) { sendSignal("TERM", ch) } -func exitSignalHandler(ch *channel) { +func exitSignalHandler(ch *serverChan) { defer ch.Close() shell := newServerShell(ch, "> ") shell.ReadLine() sendSignal("TERM", ch) } -func exitSignalUnknownHandler(ch *channel) { +func exitSignalUnknownHandler(ch *serverChan) { defer ch.Close() shell := newServerShell(ch, "> ") shell.ReadLine() sendSignal("SYS", ch) } -func exitWithoutSignalOrStatus(ch *channel) { +func exitWithoutSignalOrStatus(ch *serverChan) { defer ch.Close() shell := newServerShell(ch, "> ") shell.ReadLine() } -func shellHandler(ch *channel) { +func shellHandler(ch *serverChan) { defer ch.Close() // this string is returned to stdout shell := newServerShell(ch, "golang") @@ -370,9 +370,9 @@ func shellHandler(ch *channel) { sendStatus(0, ch) } -func sendStatus(status uint32, ch *channel) { +func sendStatus(status uint32, ch *serverChan) { msg := exitStatusMsg{ - PeersId: ch.theirId, + PeersId: ch.remoteId, Request: "exit-status", WantReply: false, Status: status, @@ -380,9 +380,9 @@ func sendStatus(status uint32, ch *channel) { ch.serverConn.writePacket(marshal(msgChannelRequest, msg)) } -func sendSignal(signal string, ch *channel) { +func sendSignal(signal string, ch *serverChan) { sig := exitSignalMsg{ - PeersId: ch.theirId, + PeersId: ch.remoteId, Request: "exit-signal", WantReply: false, Signal: signal, @@ -393,7 +393,7 @@ func sendSignal(signal string, ch *channel) { ch.serverConn.writePacket(marshal(msgChannelRequest, sig)) } -func sendInvalidRecord(ch *channel) { +func sendInvalidRecord(ch *serverChan) { defer ch.Close() packet := make([]byte, 1+4+4+1) packet[0] = msgChannelData diff --git a/ssh/tcpip.go b/ssh/tcpip.go index 643f5f4..69619ac 100644 --- a/ssh/tcpip.go +++ b/ssh/tcpip.go @@ -209,7 +209,7 @@ func (c *ClientConn) dial(laddr string, lport int, raddr string, rport int) (*tc ch := c.newChan(c.transport) if err := c.writePacket(marshal(msgChannelOpen, channelOpenDirectMsg{ ChanType: "direct-tcpip", - PeersId: ch.id, + PeersId: ch.localId, PeersWindow: 1 << 14, MaxPacketSize: 1 << 15, // RFC 4253 6.1 raddr: raddr, @@ -217,11 +217,11 @@ func (c *ClientConn) dial(laddr string, lport int, raddr string, rport int) (*tc laddr: laddr, lport: uint32(lport), })); err != nil { - c.chanlist.remove(ch.id) + c.chanList.remove(ch.localId) return nil, err } 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 &tcpChan{