Skip to content

Commit

Permalink
more context for EOF during client setup
Browse files Browse the repository at this point in the history
  • Loading branch information
puellanivis committed Jun 29, 2022
1 parent a17a626 commit cc19e20
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
20 changes: 17 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"os"
Expand Down Expand Up @@ -226,15 +227,22 @@ func NewClientPipe(rd io.Reader, wr io.WriteCloser, opts ...ClientOption) (*Clie

if err := sftp.sendInit(); err != nil {
wr.Close()
return nil, err
return nil, fmt.Errorf("error sending init packet to server: %w", err)
}

if err := sftp.recvVersion(); err != nil {
wr.Close()
return nil, err
return nil, fmt.Errorf("error receiving version packet from server: %w", err)
}

sftp.clientConn.wg.Add(1)
go sftp.loop()
go func() {
defer sftp.clientConn.wg.Done()

if err := sftp.clientConn.recv(); err != nil {
sftp.clientConn.broadcastErr(err)
}
}()

return sftp, nil
}
Expand Down Expand Up @@ -267,8 +275,13 @@ func (c *Client) nextID() uint32 {
func (c *Client) recvVersion() error {
typ, data, err := c.recvPacket(0)
if err != nil {
if err == io.EOF {
return io.ErrUnexpectedEOF
}

return err
}

if typ != sshFxpVersion {
return &unexpectedPacketErr{sshFxpVersion, typ}
}
Expand All @@ -277,6 +290,7 @@ func (c *Client) recvVersion() error {
if err != nil {
return err
}

if version != sftpProtocolVersion {
return &unexpectedVersionErr{sftpProtocolVersion, version}
}
Expand Down
8 changes: 0 additions & 8 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ func (c *clientConn) Close() error {
return c.conn.Close()
}

func (c *clientConn) loop() {
defer c.wg.Done()
err := c.recv()
if err != nil {
c.broadcastErr(err)
}
}

// recv continuously reads from the server and forwards responses to the
// appropriate channel.
func (c *clientConn) recv() error {
Expand Down

0 comments on commit cc19e20

Please sign in to comment.