Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(824)

Side by Side Diff: ssh/connection.go

Issue 37730043: code review 37730043: gosshnew/ssh: introduce ssh.Conn, representing the conn... (Closed)
Patch Set: diff -r 90e892abd218 https://hanwen%40google.com@code.google.com/p/gosshnew/ Created 10 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 package ssh
agl 2013/12/11 20:52:47 Copyright header.
hanwen-google 2013/12/12 10:38:19 Done.
2
3 import (
4 "fmt"
5 "net"
6 )
7
8 // OpenChannelError is returned if the other side rejects our
agl 2013/12/11 20:52:47 s/our/an/
hanwen-google 2013/12/12 10:38:19 Done.
9 // OpenChannel request.
10 type OpenChannelError struct {
11 Reason RejectionReason
12 Message string
13 }
14
15 func (e *OpenChannelError) Error() string {
16 return fmt.Sprintf("ssh: rejected: %s (%s)", e.Reason, e.Message)
17 }
18
19 // Conn represents an SSH connection. It is equal for server and
agl 2013/12/11 20:52:47 // Conn represents an SSH connection for both clie
hanwen-google 2013/12/12 10:38:19 Done.
20 // client roles.
21 type Conn interface {
22 ConnMetadata
23
24 // SendRequest sends a global request, and returns the
25 // reply. If wantReply is true, it returns the response status
26 // and payload. See also RFC4254, section 4.
27 SendRequest(name string, wantReply bool, payload []byte) (bool, []byte, error)
28
29 // OpenChannel tries to open an channel. If the request is
30 // rejected, it returns *OpenChannelError. On success, returns
31 // the SSH Channel, and a Go channel for incoming out-of-band
32 // requests. The Go channel must be serviced, or the
33 // connection will hang.
34 OpenChannel(name string, data []byte) (Channel, <-chan *Request, error)
35
36 // Close closes the underlying network connection
37 Close() error
38
39 // Wait blocks until the connection has shut down, and returns the
40 // error causing the shutdown.
41 Wait() error
42
43 // TODO(hanwen): consider exposing:
44 // RequestKeyChange
45 // Disconnect
46 }
47
48 // DiscardIncoming rejects all incoming requests.
49 func DiscardIncoming(in <-chan *Request) {
50 for req := range in {
51 if req.WantReply {
52 req.Reply(false, nil)
53 }
54 }
55 }
56
57 // A serverConn represents an incoming connection.
58 type connection struct {
59 transport *handshakeTransport
60 sshConn
61
62 // The connection protocol.
63 *mux
64 }
65
66 func (c *connection) Close() error {
67 return c.sshConn.conn.Close()
68 }
69
70 // sshconn provides net.Conn metadata, but disallows direct reads and
71 // writes.
72 type sshConn struct {
73 conn net.Conn
74
75 user string
76 sessionID []byte
77 clientVersion []byte
78 serverVersion []byte
79 }
80
81 func dup(src []byte) []byte {
82 dst := make([]byte, len(src))
83 copy(dst, src)
84 return dst
85 }
86
87 func (c *sshConn) User() string {
88 return c.user
89 }
90
91 func (c *sshConn) RemoteAddr() net.Addr {
92 return c.conn.RemoteAddr()
93 }
94
95 func (c *sshConn) Close() error {
96 return c.conn.Close()
97 }
98
99 func (c *sshConn) LocalAddr() net.Addr {
100 return c.conn.LocalAddr()
101 }
102
103 func (c *sshConn) SessionID() []byte {
104 return dup(c.sessionID)
105 }
106
107 func (c *sshConn) ClientVersion() []byte {
108 return dup(c.clientVersion)
109 }
110
111 func (c *sshConn) ServerVersion() []byte {
112 return dup(c.serverVersion)
113 }
114
115 // ConnMetadata holds metadata for the connection.
116 type ConnMetadata interface {
agl 2013/12/11 20:52:47 Unclear why this exists. Why not put the contents
hanwen-google 2013/12/12 10:38:19 It's passed into the server auth callbacks so they
117 // User returns the user ID for this connection.
118 // It is empty if no authentication is used.
119 User() string
120
121 // SessionID returns the sesson hash, also denoted by H.
122 SessionID() []byte
123
124 // ClientVersion returns the client's version string as hashed
125 // into the session ID.
126 ClientVersion() []byte
127
128 // ServerVersion returns the client's version string as hashed
129 // into the session ID.
130 ServerVersion() []byte
131
132 RemoteAddr() net.Addr
133 LocalAddr() net.Addr
134 }
OLDNEW
« ssh/client.go ('K') | « ssh/common_test.go ('k') | ssh/example_test.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b