9 // streamLocalChannelOpenDirectMsg is a struct used for SSH_MSG_CHANNEL_OPEN message
10 // with "direct-streamlocal@openssh.com" string.
12 // See openssh-portable/PROTOCOL, section 2.4. connection: Unix domain socket forwarding
13 // https://github.com/openssh/openssh-portable/blob/master/PROTOCOL#L235
14 type streamLocalChannelOpenDirectMsg struct {
20 // forwardedStreamLocalPayload is a struct used for SSH_MSG_CHANNEL_OPEN message
21 // with "forwarded-streamlocal@openssh.com" string.
22 type forwardedStreamLocalPayload struct {
27 // streamLocalChannelForwardMsg is a struct used for SSH2_MSG_GLOBAL_REQUEST message
28 // with "streamlocal-forward@openssh.com"/"cancel-streamlocal-forward@openssh.com" string.
29 type streamLocalChannelForwardMsg struct {
33 // ListenUnix is similar to ListenTCP but uses a Unix domain socket.
34 func (c *Client) ListenUnix(socketPath string) (net.Listener, error) {
35 m := streamLocalChannelForwardMsg{
39 ok, _, err := c.SendRequest("streamlocal-forward@openssh.com", true, Marshal(&m))
44 return nil, errors.New("ssh: streamlocal-forward@openssh.com request denied by peer")
46 ch := c.forwards.add(&net.UnixAddr{Name: socketPath, Net: "unix"})
48 return &unixListener{socketPath, c, ch}, nil
51 func (c *Client) dialStreamLocal(socketPath string) (Channel, error) {
52 msg := streamLocalChannelOpenDirectMsg{
53 socketPath: socketPath,
55 ch, in, err := c.OpenChannel("direct-streamlocal@openssh.com", Marshal(&msg))
59 go DiscardRequests(in)
63 type unixListener struct {
70 // Accept waits for and returns the next connection to the listener.
71 func (l *unixListener) Accept() (net.Conn, error) {
76 ch, incoming, err := s.newCh.Accept()
80 go DiscardRequests(incoming)
95 // Close closes the listener.
96 func (l *unixListener) Close() error {
97 // this also closes the listener.
98 l.conn.forwards.remove(&net.UnixAddr{Name: l.socketPath, Net: "unix"})
99 m := streamLocalChannelForwardMsg{
102 ok, _, err := l.conn.SendRequest("cancel-streamlocal-forward@openssh.com", true, Marshal(&m))
103 if err == nil && !ok {
104 err = errors.New("ssh: cancel-streamlocal-forward@openssh.com failed")
109 // Addr returns the listener's network address.
110 func (l *unixListener) Addr() net.Addr {
111 return &net.UnixAddr{