github.com/pkg/sftp@v1.13.6/internal/encoding/ssh/filexfer/filexfer.go (about)

     1  // Package sshfx implements the wire encoding for secsh-filexfer as described in https://filezilla-project.org/specs/draft-ietf-secsh-filexfer-02.txt
     2  package sshfx
     3  
     4  // PacketMarshaller narrowly defines packets that will only be transmitted.
     5  //
     6  // ExtendedPacket types will often only implement this interface,
     7  // since decoding the whole packet body of an ExtendedPacket can only be done dependent on the ExtendedRequest field.
     8  type PacketMarshaller interface {
     9  	// MarshalPacket is the primary intended way to encode a packet.
    10  	// The request-id for the packet is set from reqid.
    11  	//
    12  	// An optional buffer may be given in b.
    13  	// If the buffer has a minimum capacity, it shall be truncated and used to marshal the header into.
    14  	// The minimum capacity for the packet must be a constant expression, and should be at least 9.
    15  	//
    16  	// It shall return the main body of the encoded packet in header,
    17  	// and may optionally return an additional payload to be written immediately after the header.
    18  	//
    19  	// It shall encode in the first 4-bytes of the header the proper length of the rest of the header+payload.
    20  	MarshalPacket(reqid uint32, b []byte) (header, payload []byte, err error)
    21  }
    22  
    23  // Packet defines the behavior of a full generic SFTP packet.
    24  //
    25  // InitPacket, and VersionPacket are not generic SFTP packets, and instead implement (Un)MarshalBinary.
    26  //
    27  // ExtendedPacket types should not iplement this interface,
    28  // since decoding the whole packet body of an ExtendedPacket can only be done dependent on the ExtendedRequest field.
    29  type Packet interface {
    30  	PacketMarshaller
    31  
    32  	// Type returns the SSH_FXP_xy value associated with the specific packet.
    33  	Type() PacketType
    34  
    35  	// UnmarshalPacketBody decodes a packet body from the given Buffer.
    36  	// It is assumed that the common header values of the length, type and request-id have already been consumed.
    37  	//
    38  	// Implementations should not alias the given Buffer,
    39  	// instead they can consider prepopulating an internal buffer as a hint,
    40  	// and copying into that buffer if it has sufficient length.
    41  	UnmarshalPacketBody(buf *Buffer) error
    42  }
    43  
    44  // ComposePacket converts returns from MarshalPacket into an equivalent call to MarshalBinary.
    45  func ComposePacket(header, payload []byte, err error) ([]byte, error) {
    46  	return append(header, payload...), err
    47  }
    48  
    49  // Default length values,
    50  // Defined in draft-ietf-secsh-filexfer-02 section 3.
    51  const (
    52  	DefaultMaxPacketLength = 34000
    53  	DefaultMaxDataLength   = 32768
    54  )