| 
									
										
										
										
											2013-11-05 08:25:17 +08:00
										 |  |  | package sftp | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-05 08:36:47 +08:00
										 |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-06-13 12:45:13 +08:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	"encoding" | 
					
						
							| 
									
										
										
										
											2016-06-13 12:45:13 +08:00
										 |  |  | 	"encoding/binary" | 
					
						
							| 
									
										
										
										
											2021-06-05 05:18:41 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2013-11-05 08:36:47 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2013-11-05 11:36:38 +08:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2013-11-05 09:03:58 +08:00
										 |  |  | 	"reflect" | 
					
						
							| 
									
										
										
										
											2013-11-05 08:36:47 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-07 10:36:47 +08:00
										 |  |  | var ( | 
					
						
							| 
									
										
										
										
											2019-09-29 14:39:33 +08:00
										 |  |  | 	errLongPacket            = errors.New("packet too long") | 
					
						
							| 
									
										
										
										
											2016-06-13 12:45:13 +08:00
										 |  |  | 	errShortPacket           = errors.New("packet too short") | 
					
						
							|  |  |  | 	errUnknownExtendedPacket = errors.New("unknown extended packet") | 
					
						
							| 
									
										
										
										
											2015-09-07 14:55:15 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2019-09-29 14:39:33 +08:00
										 |  |  | 	maxMsgLength           = 256 * 1024 | 
					
						
							| 
									
										
										
										
											2015-09-07 10:36:47 +08:00
										 |  |  | 	debugDumpTxPacket      = false | 
					
						
							|  |  |  | 	debugDumpRxPacket      = false | 
					
						
							|  |  |  | 	debugDumpTxPacketBytes = false | 
					
						
							|  |  |  | 	debugDumpRxPacketBytes = false | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-05 08:25:17 +08:00
										 |  |  | func marshalUint32(b []byte, v uint32) []byte { | 
					
						
							|  |  |  | 	return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func marshalUint64(b []byte, v uint64) []byte { | 
					
						
							| 
									
										
										
										
											2013-12-11 07:14:59 +08:00
										 |  |  | 	return marshalUint32(marshalUint32(b, uint32(v>>32)), uint32(v)) | 
					
						
							| 
									
										
										
										
											2013-11-05 08:25:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func marshalString(b []byte, v string) []byte { | 
					
						
							|  |  |  | 	return append(marshalUint32(b, uint32(len(v))), v...) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-11-05 08:36:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 22:23:51 +08:00
										 |  |  | func marshalFileInfo(b []byte, fi os.FileInfo) []byte { | 
					
						
							|  |  |  | 	// attributes variable struct, and also variable per protocol version
 | 
					
						
							|  |  |  | 	// spec version 3 attributes:
 | 
					
						
							|  |  |  | 	// uint32   flags
 | 
					
						
							|  |  |  | 	// uint64   size           present only if flag SSH_FILEXFER_ATTR_SIZE
 | 
					
						
							|  |  |  | 	// uint32   uid            present only if flag SSH_FILEXFER_ATTR_UIDGID
 | 
					
						
							|  |  |  | 	// uint32   gid            present only if flag SSH_FILEXFER_ATTR_UIDGID
 | 
					
						
							|  |  |  | 	// uint32   permissions    present only if flag SSH_FILEXFER_ATTR_PERMISSIONS
 | 
					
						
							|  |  |  | 	// uint32   atime          present only if flag SSH_FILEXFER_ACMODTIME
 | 
					
						
							|  |  |  | 	// uint32   mtime          present only if flag SSH_FILEXFER_ACMODTIME
 | 
					
						
							|  |  |  | 	// uint32   extended_count present only if flag SSH_FILEXFER_ATTR_EXTENDED
 | 
					
						
							|  |  |  | 	// string   extended_type
 | 
					
						
							|  |  |  | 	// string   extended_data
 | 
					
						
							|  |  |  | 	// ...      more extended data (extended_type - extended_data pairs),
 | 
					
						
							|  |  |  | 	// 	   so that number of pairs equals extended_count
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	flags, fileStat := fileStatFromInfo(fi) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	b = marshalUint32(b, flags) | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return marshalFileStat(b, flags, fileStat) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func marshalFileStat(b []byte, flags uint32, fileStat *FileStat) []byte { | 
					
						
							| 
									
										
										
										
											2021-07-21 22:23:51 +08:00
										 |  |  | 	if flags&sshFileXferAttrSize != 0 { | 
					
						
							|  |  |  | 		b = marshalUint64(b, fileStat.Size) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if flags&sshFileXferAttrUIDGID != 0 { | 
					
						
							|  |  |  | 		b = marshalUint32(b, fileStat.UID) | 
					
						
							|  |  |  | 		b = marshalUint32(b, fileStat.GID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if flags&sshFileXferAttrPermissions != 0 { | 
					
						
							|  |  |  | 		b = marshalUint32(b, fileStat.Mode) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if flags&sshFileXferAttrACmodTime != 0 { | 
					
						
							|  |  |  | 		b = marshalUint32(b, fileStat.Atime) | 
					
						
							|  |  |  | 		b = marshalUint32(b, fileStat.Mtime) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 20:21:45 +08:00
										 |  |  | 	if flags&sshFileXferAttrExtended != 0 { | 
					
						
							|  |  |  | 		b = marshalUint32(b, uint32(len(fileStat.Extended))) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for _, attr := range fileStat.Extended { | 
					
						
							|  |  |  | 			b = marshalString(b, attr.ExtType) | 
					
						
							|  |  |  | 			b = marshalString(b, attr.ExtData) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 22:23:51 +08:00
										 |  |  | 	return b | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func marshalStatus(b []byte, err StatusError) []byte { | 
					
						
							|  |  |  | 	b = marshalUint32(b, err.Code) | 
					
						
							|  |  |  | 	b = marshalString(b, err.msg) | 
					
						
							|  |  |  | 	b = marshalString(b, err.lang) | 
					
						
							|  |  |  | 	return b | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-05 08:36:47 +08:00
										 |  |  | func marshal(b []byte, v interface{}) []byte { | 
					
						
							|  |  |  | 	switch v := v.(type) { | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	case nil: | 
					
						
							|  |  |  | 		return b | 
					
						
							| 
									
										
										
										
											2013-11-05 11:36:38 +08:00
										 |  |  | 	case uint8: | 
					
						
							|  |  |  | 		return append(b, v) | 
					
						
							| 
									
										
										
										
											2013-11-05 08:36:47 +08:00
										 |  |  | 	case uint32: | 
					
						
							|  |  |  | 		return marshalUint32(b, v) | 
					
						
							|  |  |  | 	case uint64: | 
					
						
							|  |  |  | 		return marshalUint64(b, v) | 
					
						
							|  |  |  | 	case string: | 
					
						
							|  |  |  | 		return marshalString(b, v) | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	case []byte: | 
					
						
							|  |  |  | 		return append(b, v...) | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | 	case os.FileInfo: | 
					
						
							|  |  |  | 		return marshalFileInfo(b, v) | 
					
						
							| 
									
										
										
										
											2013-11-05 08:36:47 +08:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2013-11-05 09:03:58 +08:00
										 |  |  | 		switch d := reflect.ValueOf(v); d.Kind() { | 
					
						
							|  |  |  | 		case reflect.Struct: | 
					
						
							|  |  |  | 			for i, n := 0, d.NumField(); i < n; i++ { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:04:50 +08:00
										 |  |  | 				b = marshal(b, d.Field(i).Interface()) | 
					
						
							| 
									
										
										
										
											2013-11-05 09:03:58 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			return b | 
					
						
							| 
									
										
										
										
											2013-11-05 11:36:38 +08:00
										 |  |  | 		case reflect.Slice: | 
					
						
							|  |  |  | 			for i, n := 0, d.Len(); i < n; i++ { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:04:50 +08:00
										 |  |  | 				b = marshal(b, d.Index(i).Interface()) | 
					
						
							| 
									
										
										
										
											2013-11-05 11:36:38 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			return b | 
					
						
							| 
									
										
										
										
											2013-11-05 09:03:58 +08:00
										 |  |  | 		default: | 
					
						
							|  |  |  | 			panic(fmt.Sprintf("marshal(%#v): cannot handle type %T", v, v)) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-11-05 08:36:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-11-05 09:23:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func unmarshalUint32(b []byte) (uint32, []byte) { | 
					
						
							|  |  |  | 	v := uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24 | 
					
						
							|  |  |  | 	return v, b[4:] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | func unmarshalUint32Safe(b []byte) (uint32, []byte, error) { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	var v uint32 | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 	if len(b) < 4 { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 		return 0, nil, errShortPacket | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-08 13:50:46 +08:00
										 |  |  | 	v, b = unmarshalUint32(b) | 
					
						
							|  |  |  | 	return v, b, nil | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-05 09:23:48 +08:00
										 |  |  | func unmarshalUint64(b []byte) (uint64, []byte) { | 
					
						
							|  |  |  | 	h, b := unmarshalUint32(b) | 
					
						
							|  |  |  | 	l, b := unmarshalUint32(b) | 
					
						
							|  |  |  | 	return uint64(h)<<32 | uint64(l), b | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-11-05 09:31:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | func unmarshalUint64Safe(b []byte) (uint64, []byte, error) { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	var v uint64 | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 	if len(b) < 8 { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 		return 0, nil, errShortPacket | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-08 13:50:46 +08:00
										 |  |  | 	v, b = unmarshalUint64(b) | 
					
						
							|  |  |  | 	return v, b, nil | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-05 09:31:20 +08:00
										 |  |  | func unmarshalString(b []byte) (string, []byte) { | 
					
						
							|  |  |  | 	n, b := unmarshalUint32(b) | 
					
						
							|  |  |  | 	return string(b[:n]), b[n:] | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-11-05 11:36:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | func unmarshalStringSafe(b []byte) (string, []byte, error) { | 
					
						
							|  |  |  | 	n, b, err := unmarshalUint32Safe(b) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "", nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if int64(n) > int64(len(b)) { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 		return "", nil, errShortPacket | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return string(b[:n]), b[n:], nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | func unmarshalAttrs(b []byte) (*FileStat, []byte, error) { | 
					
						
							|  |  |  | 	flags, b, err := unmarshalUint32Safe(b) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, b, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-07-21 22:23:51 +08:00
										 |  |  | 	return unmarshalFileStat(flags, b) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | func unmarshalFileStat(flags uint32, b []byte) (*FileStat, []byte, error) { | 
					
						
							| 
									
										
										
										
											2021-07-21 22:23:51 +08:00
										 |  |  | 	var fs FileStat | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 	var err error | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 22:23:51 +08:00
										 |  |  | 	if flags&sshFileXferAttrSize == sshFileXferAttrSize { | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 		fs.Size, b, err = unmarshalUint64Safe(b) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, b, err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-07-21 22:23:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if flags&sshFileXferAttrUIDGID == sshFileXferAttrUIDGID { | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 		fs.UID, b, err = unmarshalUint32Safe(b) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, b, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		fs.GID, b, err = unmarshalUint32Safe(b) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, b, err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-07-21 22:23:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if flags&sshFileXferAttrPermissions == sshFileXferAttrPermissions { | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 		fs.Mode, b, err = unmarshalUint32Safe(b) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, b, err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-07-21 22:23:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if flags&sshFileXferAttrACmodTime == sshFileXferAttrACmodTime { | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 		fs.Atime, b, err = unmarshalUint32Safe(b) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, b, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		fs.Mtime, b, err = unmarshalUint32Safe(b) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, b, err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-07-21 22:23:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if flags&sshFileXferAttrExtended == sshFileXferAttrExtended { | 
					
						
							|  |  |  | 		var count uint32 | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 		count, b, err = unmarshalUint32Safe(b) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, b, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 22:23:51 +08:00
										 |  |  | 		ext := make([]StatExtended, count) | 
					
						
							|  |  |  | 		for i := uint32(0); i < count; i++ { | 
					
						
							|  |  |  | 			var typ string | 
					
						
							|  |  |  | 			var data string | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 			typ, b, err = unmarshalStringSafe(b) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return nil, b, err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			data, b, err = unmarshalStringSafe(b) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return nil, b, err | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-07-21 22:23:51 +08:00
										 |  |  | 			ext[i] = StatExtended{ | 
					
						
							|  |  |  | 				ExtType: typ, | 
					
						
							|  |  |  | 				ExtData: data, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		fs.Extended = ext | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 	return &fs, b, nil | 
					
						
							| 
									
										
										
										
											2021-07-21 22:23:51 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func unmarshalStatus(id uint32, data []byte) error { | 
					
						
							|  |  |  | 	sid, data := unmarshalUint32(data) | 
					
						
							|  |  |  | 	if sid != id { | 
					
						
							|  |  |  | 		return &unexpectedIDErr{id, sid} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	code, data := unmarshalUint32(data) | 
					
						
							|  |  |  | 	msg, data, _ := unmarshalStringSafe(data) | 
					
						
							|  |  |  | 	lang, _, _ := unmarshalStringSafe(data) | 
					
						
							|  |  |  | 	return &StatusError{ | 
					
						
							|  |  |  | 		Code: code, | 
					
						
							|  |  |  | 		msg:  msg, | 
					
						
							|  |  |  | 		lang: lang, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | type packetMarshaler interface { | 
					
						
							|  |  |  | 	marshalPacket() (header, payload []byte, err error) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func marshalPacket(m encoding.BinaryMarshaler) (header, payload []byte, err error) { | 
					
						
							|  |  |  | 	if m, ok := m.(packetMarshaler); ok { | 
					
						
							|  |  |  | 		return m.marshalPacket() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	header, err = m.MarshalBinary() | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-05 11:36:38 +08:00
										 |  |  | // sendPacket marshals p according to RFC 4234.
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | func sendPacket(w io.Writer, m encoding.BinaryMarshaler) error { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	header, payload, err := marshalPacket(m) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-05 05:18:41 +08:00
										 |  |  | 		return fmt.Errorf("binary marshaller failed: %w", err) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	length := len(header) + len(payload) - 4 // subtract the uint32(length) from the start
 | 
					
						
							| 
									
										
										
										
											2015-09-07 10:36:47 +08:00
										 |  |  | 	if debugDumpTxPacketBytes { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 		debug("send packet: %s %d bytes %x%x", fxp(header[4]), length, header[5:], payload) | 
					
						
							| 
									
										
										
										
											2015-09-07 10:36:47 +08:00
										 |  |  | 	} else if debugDumpTxPacket { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 		debug("send packet: %s %d bytes", fxp(header[4]), length) | 
					
						
							| 
									
										
										
										
											2015-09-07 10:36:47 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-12 12:44:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	binary.BigEndian.PutUint32(header[:4], uint32(length)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if _, err := w.Write(header); err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-05 05:18:41 +08:00
										 |  |  | 		return fmt.Errorf("failed to send packet: %w", err) | 
					
						
							| 
									
										
										
										
											2016-06-15 18:04:25 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if len(payload) > 0 { | 
					
						
							|  |  |  | 		if _, err := w.Write(payload); err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-05 05:18:41 +08:00
										 |  |  | 			return fmt.Errorf("failed to send packet payload: %w", err) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-15 18:04:25 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2013-11-05 11:36:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 02:42:19 +08:00
										 |  |  | func recvPacket(r io.Reader, alloc *allocator, orderID uint32) (uint8, []byte, error) { | 
					
						
							|  |  |  | 	var b []byte | 
					
						
							|  |  |  | 	if alloc != nil { | 
					
						
							|  |  |  | 		b = alloc.GetPage(orderID) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		b = make([]byte, 4) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if _, err := io.ReadFull(r, b[:4]); err != nil { | 
					
						
							| 
									
										
										
										
											2013-11-05 11:36:38 +08:00
										 |  |  | 		return 0, nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-03-18 16:36:07 +08:00
										 |  |  | 	length, _ := unmarshalUint32(b) | 
					
						
							|  |  |  | 	if length > maxMsgLength { | 
					
						
							|  |  |  | 		debug("recv packet %d bytes too long", length) | 
					
						
							| 
									
										
										
										
											2019-09-29 14:39:33 +08:00
										 |  |  | 		return 0, nil, errLongPacket | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-30 16:36:20 +08:00
										 |  |  | 	if length == 0 { | 
					
						
							|  |  |  | 		debug("recv packet of 0 bytes too short") | 
					
						
							|  |  |  | 		return 0, nil, errShortPacket | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-03-15 02:42:19 +08:00
										 |  |  | 	if alloc == nil { | 
					
						
							| 
									
										
										
										
											2020-03-18 16:36:07 +08:00
										 |  |  | 		b = make([]byte, length) | 
					
						
							| 
									
										
										
										
											2020-03-15 02:42:19 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-03-18 16:36:07 +08:00
										 |  |  | 	if _, err := io.ReadFull(r, b[:length]); err != nil { | 
					
						
							| 
									
										
										
										
											2023-07-21 16:17:22 +08:00
										 |  |  | 		// ReadFull only returns EOF if it has read no bytes.
 | 
					
						
							|  |  |  | 		// In this case, that means a partial packet, and thus unexpected.
 | 
					
						
							|  |  |  | 		if err == io.EOF { | 
					
						
							|  |  |  | 			err = io.ErrUnexpectedEOF | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-03-18 16:36:07 +08:00
										 |  |  | 		debug("recv packet %d bytes: err %v", length, err) | 
					
						
							| 
									
										
										
										
											2013-11-05 11:36:38 +08:00
										 |  |  | 		return 0, nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-07 10:36:47 +08:00
										 |  |  | 	if debugDumpRxPacketBytes { | 
					
						
							| 
									
										
										
										
											2020-03-18 16:36:07 +08:00
										 |  |  | 		debug("recv packet: %s %d bytes %x", fxp(b[0]), length, b[1:length]) | 
					
						
							| 
									
										
										
										
											2015-09-07 10:36:47 +08:00
										 |  |  | 	} else if debugDumpRxPacket { | 
					
						
							| 
									
										
										
										
											2020-03-18 16:36:07 +08:00
										 |  |  | 		debug("recv packet: %s %d bytes", fxp(b[0]), length) | 
					
						
							| 
									
										
										
										
											2015-09-07 10:36:47 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-03-18 16:36:07 +08:00
										 |  |  | 	return b[0], b[1:length], nil | 
					
						
							| 
									
										
										
										
											2013-11-05 11:36:38 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 04:11:16 +08:00
										 |  |  | type extensionPair struct { | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 	Name string | 
					
						
							|  |  |  | 	Data string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 04:11:16 +08:00
										 |  |  | func unmarshalExtensionPair(b []byte) (extensionPair, []byte, error) { | 
					
						
							|  |  |  | 	var ep extensionPair | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 	ep.Name, b, err = unmarshalStringSafe(b) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return ep, b, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ep.Data, b, err = unmarshalStringSafe(b) | 
					
						
							|  |  |  | 	return ep, b, err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | // Here starts the definition of packets along with their MarshalBinary
 | 
					
						
							|  |  |  | // implementations.
 | 
					
						
							|  |  |  | // Manually writing the marshalling logic wins us a lot of time and
 | 
					
						
							|  |  |  | // allocation.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type sshFxInitPacket struct { | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 	Version    uint32 | 
					
						
							| 
									
										
										
										
											2016-01-08 04:11:16 +08:00
										 |  |  | 	Extensions []extensionPair | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxInitPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 // uint32(length) + byte(type) + uint32(version)
 | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 	for _, e := range p.Extensions { | 
					
						
							|  |  |  | 		l += 4 + len(e.Name) + 4 + len(e.Data) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 	b = append(b, sshFxpInit) | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 	b = marshalUint32(b, p.Version) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 	for _, e := range p.Extensions { | 
					
						
							|  |  |  | 		b = marshalString(b, e.Name) | 
					
						
							|  |  |  | 		b = marshalString(b, e.Data) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 	return b, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 04:11:16 +08:00
										 |  |  | func (p *sshFxInitPacket) UnmarshalBinary(b []byte) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 	if p.Version, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for len(b) > 0 { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:11:16 +08:00
										 |  |  | 		var ep extensionPair | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 		ep, b, err = unmarshalExtensionPair(b) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		p.Extensions = append(p.Extensions, ep) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type sshFxVersionPacket struct { | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Version    uint32 | 
					
						
							| 
									
										
										
										
											2019-05-25 03:23:18 +08:00
										 |  |  | 	Extensions []sshExtensionPair | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type sshExtensionPair struct { | 
					
						
							|  |  |  | 	Name, Data string | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxVersionPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 // uint32(length) + byte(type) + uint32(version)
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	for _, e := range p.Extensions { | 
					
						
							|  |  |  | 		l += 4 + len(e.Name) + 4 + len(e.Data) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 	b = append(b, sshFxpVersion) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	b = marshalUint32(b, p.Version) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	for _, e := range p.Extensions { | 
					
						
							|  |  |  | 		b = marshalString(b, e.Name) | 
					
						
							|  |  |  | 		b = marshalString(b, e.Data) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	return b, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | func marshalIDStringPacket(packetType byte, id uint32, str string) ([]byte, error) { | 
					
						
							|  |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 		4 + len(str) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	b = append(b, packetType) | 
					
						
							|  |  |  | 	b = marshalUint32(b, id) | 
					
						
							|  |  |  | 	b = marshalString(b, str) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	return b, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | func unmarshalIDString(b []byte, id *uint32, str *string) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 	*id, b, err = unmarshalUint32Safe(b) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2015-09-07 14:55:15 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-16 03:00:22 +08:00
										 |  |  | 	*str, _, err = unmarshalStringSafe(b) | 
					
						
							| 
									
										
										
										
											2016-06-14 16:05:33 +08:00
										 |  |  | 	return err | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | type sshFxpReaddirPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID     uint32 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Handle string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpReaddirPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-08-06 03:57:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpReaddirPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	return marshalIDStringPacket(sshFxpReaddir, p.ID, p.Handle) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | func (p *sshFxpReaddirPacket) UnmarshalBinary(b []byte) error { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	return unmarshalIDString(b, &p.ID, &p.Handle) | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | type sshFxpOpendirPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID   uint32 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Path string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpOpendirPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-08-06 03:57:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpOpendirPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	return marshalIDStringPacket(sshFxpOpendir, p.ID, p.Path) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | func (p *sshFxpOpendirPacket) UnmarshalBinary(b []byte) error { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	return unmarshalIDString(b, &p.ID, &p.Path) | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | type sshFxpLstatPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID   uint32 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Path string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpLstatPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-05-23 20:51:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpLstatPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	return marshalIDStringPacket(sshFxpLstat, p.ID, p.Path) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | func (p *sshFxpLstatPacket) UnmarshalBinary(b []byte) error { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	return unmarshalIDString(b, &p.ID, &p.Path) | 
					
						
							| 
									
										
										
										
											2015-07-25 16:19:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-06 03:57:28 +08:00
										 |  |  | type sshFxpStatPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID   uint32 | 
					
						
							| 
									
										
										
										
											2015-08-06 03:57:28 +08:00
										 |  |  | 	Path string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpStatPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-08-06 03:57:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpStatPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	return marshalIDStringPacket(sshFxpStat, p.ID, p.Path) | 
					
						
							| 
									
										
										
										
											2015-08-06 03:57:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (p *sshFxpStatPacket) UnmarshalBinary(b []byte) error { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	return unmarshalIDString(b, &p.ID, &p.Path) | 
					
						
							| 
									
										
										
										
											2015-08-06 03:57:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | type sshFxpFstatPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID     uint32 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Handle string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpFstatPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-05-23 20:51:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpFstatPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	return marshalIDStringPacket(sshFxpFstat, p.ID, p.Handle) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-31 00:21:59 +08:00
										 |  |  | func (p *sshFxpFstatPacket) UnmarshalBinary(b []byte) error { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	return unmarshalIDString(b, &p.ID, &p.Handle) | 
					
						
							| 
									
										
										
										
											2015-07-31 00:21:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | type sshFxpClosePacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID     uint32 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Handle string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpClosePacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-08-06 03:57:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpClosePacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	return marshalIDStringPacket(sshFxpClose, p.ID, p.Handle) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-26 16:32:19 +08:00
										 |  |  | func (p *sshFxpClosePacket) UnmarshalBinary(b []byte) error { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	return unmarshalIDString(b, &p.ID, &p.Handle) | 
					
						
							| 
									
										
										
										
											2015-07-26 16:32:19 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | type sshFxpRemovePacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID       uint32 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Filename string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpRemovePacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-05-23 20:51:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpRemovePacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	return marshalIDStringPacket(sshFxpRemove, p.ID, p.Filename) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | func (p *sshFxpRemovePacket) UnmarshalBinary(b []byte) error { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	return unmarshalIDString(b, &p.ID, &p.Filename) | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | type sshFxpRmdirPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID   uint32 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Path string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpRmdirPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-05-23 20:51:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpRmdirPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	return marshalIDStringPacket(sshFxpRmdir, p.ID, p.Path) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | func (p *sshFxpRmdirPacket) UnmarshalBinary(b []byte) error { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	return unmarshalIDString(b, &p.ID, &p.Path) | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-07 16:05:16 +08:00
										 |  |  | type sshFxpSymlinkPacket struct { | 
					
						
							| 
									
										
										
										
											2022-09-29 16:51:09 +08:00
										 |  |  | 	ID uint32 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// The order of the arguments to the SSH_FXP_SYMLINK method was inadvertently reversed.
 | 
					
						
							|  |  |  | 	// Unfortunately, the reversal was not noticed until the server was widely deployed.
 | 
					
						
							|  |  |  | 	// Covered in Section 4.1 of https://github.com/openssh/openssh-portable/blob/master/PROTOCOL
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-07 16:05:16 +08:00
										 |  |  | 	Targetpath string | 
					
						
							|  |  |  | 	Linkpath   string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpSymlinkPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-09-07 16:05:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpSymlinkPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							| 
									
										
										
										
											2015-09-07 16:05:16 +08:00
										 |  |  | 		4 + len(p.Targetpath) + | 
					
						
							|  |  |  | 		4 + len(p.Linkpath) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 	b = append(b, sshFxpSymlink) | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							| 
									
										
										
										
											2015-09-07 16:05:16 +08:00
										 |  |  | 	b = marshalString(b, p.Targetpath) | 
					
						
							|  |  |  | 	b = marshalString(b, p.Linkpath) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-07 16:05:16 +08:00
										 |  |  | 	return b, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (p *sshFxpSymlinkPacket) UnmarshalBinary(b []byte) error { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	var err error | 
					
						
							|  |  |  | 	if p.ID, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2015-09-07 16:05:16 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} else if p.Targetpath, b, err = unmarshalStringSafe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2018-02-16 03:00:22 +08:00
										 |  |  | 	} else if p.Linkpath, _, err = unmarshalStringSafe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2015-09-07 16:05:16 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-25 03:23:18 +08:00
										 |  |  | type sshFxpHardlinkPacket struct { | 
					
						
							|  |  |  | 	ID      uint32 | 
					
						
							|  |  |  | 	Oldpath string | 
					
						
							|  |  |  | 	Newpath string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpHardlinkPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2019-05-25 03:23:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpHardlinkPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2019-05-25 03:23:18 +08:00
										 |  |  | 	const ext = "hardlink@openssh.com" | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							| 
									
										
										
										
											2019-05-25 03:23:18 +08:00
										 |  |  | 		4 + len(ext) + | 
					
						
							|  |  |  | 		4 + len(p.Oldpath) + | 
					
						
							|  |  |  | 		4 + len(p.Newpath) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 	b = append(b, sshFxpExtended) | 
					
						
							| 
									
										
										
										
											2019-05-25 03:23:18 +08:00
										 |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							|  |  |  | 	b = marshalString(b, ext) | 
					
						
							|  |  |  | 	b = marshalString(b, p.Oldpath) | 
					
						
							|  |  |  | 	b = marshalString(b, p.Newpath) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-25 03:23:18 +08:00
										 |  |  | 	return b, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 09:57:44 +08:00
										 |  |  | type sshFxpReadlinkPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID   uint32 | 
					
						
							| 
									
										
										
										
											2014-09-28 09:57:44 +08:00
										 |  |  | 	Path string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpReadlinkPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-05-23 20:51:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpReadlinkPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	return marshalIDStringPacket(sshFxpReadlink, p.ID, p.Path) | 
					
						
							| 
									
										
										
										
											2014-09-28 09:57:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | func (p *sshFxpReadlinkPacket) UnmarshalBinary(b []byte) error { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	return unmarshalIDString(b, &p.ID, &p.Path) | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-06 03:57:28 +08:00
										 |  |  | type sshFxpRealpathPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID   uint32 | 
					
						
							| 
									
										
										
										
											2015-08-06 03:57:28 +08:00
										 |  |  | 	Path string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpRealpathPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-08-06 03:57:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpRealpathPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	return marshalIDStringPacket(sshFxpRealpath, p.ID, p.Path) | 
					
						
							| 
									
										
										
										
											2015-08-06 03:57:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (p *sshFxpRealpathPacket) UnmarshalBinary(b []byte) error { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	return unmarshalIDString(b, &p.ID, &p.Path) | 
					
						
							| 
									
										
										
										
											2015-08-06 03:57:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | type sshFxpNameAttr struct { | 
					
						
							| 
									
										
										
										
											2015-08-06 03:57:28 +08:00
										 |  |  | 	Name     string | 
					
						
							|  |  |  | 	LongName string | 
					
						
							|  |  |  | 	Attrs    []interface{} | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpNameAttr) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	var b []byte | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | 	b = marshalString(b, p.Name) | 
					
						
							| 
									
										
										
										
											2015-08-06 03:57:28 +08:00
										 |  |  | 	b = marshalString(b, p.LongName) | 
					
						
							| 
									
										
										
										
											2015-08-01 14:09:51 +08:00
										 |  |  | 	for _, attr := range p.Attrs { | 
					
						
							|  |  |  | 		b = marshal(b, attr) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | 	return b, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type sshFxpNamePacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID        uint32 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | 	NameAttrs []*sshFxpNameAttr | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpNamePacket) marshalPacket() ([]byte, []byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							|  |  |  | 		4 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 	b = append(b, sshFxpName) | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | 	b = marshalUint32(b, uint32(len(p.NameAttrs))) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var payload []byte | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | 	for _, na := range p.NameAttrs { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 		ab, err := na.MarshalBinary() | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 			return nil, nil, err | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 		payload = append(payload, ab...) | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return b, payload, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpNamePacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	header, payload, err := p.marshalPacket() | 
					
						
							|  |  |  | 	return append(header, payload...), err | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | type sshFxpOpenPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID     uint32 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Path   string | 
					
						
							|  |  |  | 	Pflags uint32 | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	Flags  uint32 | 
					
						
							|  |  |  | 	Attrs  interface{} | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpOpenPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-05-23 20:51:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | func (p *sshFxpOpenPacket) marshalPacket() ([]byte, []byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 		4 + len(p.Path) + | 
					
						
							| 
									
										
										
										
											2014-09-28 11:00:42 +08:00
										 |  |  | 		4 + 4 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 	b = append(b, sshFxpOpen) | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	b = marshalString(b, p.Path) | 
					
						
							|  |  |  | 	b = marshalUint32(b, p.Pflags) | 
					
						
							|  |  |  | 	b = marshalUint32(b, p.Flags) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	switch attrs := p.Attrs.(type) { | 
					
						
							| 
									
										
										
										
											2024-02-06 16:41:41 +08:00
										 |  |  | 	case []byte: | 
					
						
							|  |  |  | 		return b, attrs, nil // may as well short-ciruit this case.
 | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	case os.FileInfo: | 
					
						
							|  |  |  | 		_, fs := fileStatFromInfo(attrs) // we throw away the flags, and override with those in packet.
 | 
					
						
							|  |  |  | 		return b, marshalFileStat(nil, p.Flags, fs), nil | 
					
						
							|  |  |  | 	case *FileStat: | 
					
						
							|  |  |  | 		return b, marshalFileStat(nil, p.Flags, attrs), nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return b, marshal(nil, p.Attrs), nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (p *sshFxpOpenPacket) MarshalBinary() ([]byte, error) { | 
					
						
							|  |  |  | 	header, payload, err := p.marshalPacket() | 
					
						
							|  |  |  | 	return append(header, payload...), err | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | func (p *sshFxpOpenPacket) UnmarshalBinary(b []byte) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	if p.ID, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2015-07-26 16:32:19 +08:00
										 |  |  | 	} else if p.Path, b, err = unmarshalStringSafe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2015-07-26 16:32:19 +08:00
										 |  |  | 	} else if p.Pflags, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	} else if p.Flags, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2015-07-26 16:32:19 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	p.Attrs = b | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2015-07-26 16:32:19 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | func (p *sshFxpOpenPacket) unmarshalFileStat(flags uint32) (*FileStat, error) { | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	switch attrs := p.Attrs.(type) { | 
					
						
							|  |  |  | 	case *FileStat: | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 		return attrs, nil | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	case []byte: | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 		fs, _, err := unmarshalFileStat(flags, attrs) | 
					
						
							|  |  |  | 		return fs, err | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 		return nil, fmt.Errorf("invalid type in unmarshalFileStat: %T", attrs) | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | type sshFxpReadPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID     uint32 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Len    uint32 | 
					
						
							| 
									
										
										
										
											2020-09-08 16:18:15 +08:00
										 |  |  | 	Offset uint64 | 
					
						
							|  |  |  | 	Handle string | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpReadPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-05-23 20:51:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpReadPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 		4 + len(p.Handle) + | 
					
						
							|  |  |  | 		8 + 4 // uint64 + uint32
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 	b = append(b, sshFxpRead) | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	b = marshalString(b, p.Handle) | 
					
						
							|  |  |  | 	b = marshalUint64(b, p.Offset) | 
					
						
							|  |  |  | 	b = marshalUint32(b, p.Len) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	return b, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | func (p *sshFxpReadPacket) UnmarshalBinary(b []byte) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	if p.ID, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2015-07-30 08:24:24 +08:00
										 |  |  | 	} else if p.Handle, b, err = unmarshalStringSafe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2015-07-30 08:24:24 +08:00
										 |  |  | 	} else if p.Offset, b, err = unmarshalUint64Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2018-02-16 03:00:22 +08:00
										 |  |  | 	} else if p.Len, _, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2015-07-30 08:24:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2015-07-30 08:24:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | // We need allocate bigger slices with extra capacity to avoid a re-allocation in sshFxpDataPacket.MarshalBinary
 | 
					
						
							|  |  |  | // So, we need: uint32(length) + byte(type) + uint32(id) + uint32(data_length)
 | 
					
						
							|  |  |  | const dataHeaderLen = 4 + 1 + 4 + 4 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 02:42:19 +08:00
										 |  |  | func (p *sshFxpReadPacket) getDataSlice(alloc *allocator, orderID uint32) []byte { | 
					
						
							| 
									
										
										
										
											2021-07-21 22:24:35 +08:00
										 |  |  | 	dataLen := p.Len | 
					
						
							|  |  |  | 	if dataLen > maxTxPacket { | 
					
						
							|  |  |  | 		dataLen = maxTxPacket | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-07-21 22:23:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 02:42:19 +08:00
										 |  |  | 	if alloc != nil { | 
					
						
							| 
									
										
										
										
											2020-03-18 16:36:07 +08:00
										 |  |  | 		// GetPage returns a slice with capacity = maxMsgLength this is enough to avoid new allocations in
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 		// sshFxpDataPacket.MarshalBinary
 | 
					
						
							| 
									
										
										
										
											2020-03-15 02:42:19 +08:00
										 |  |  | 		return alloc.GetPage(orderID)[:dataLen] | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-07-21 22:23:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	// allocate with extra space for the header
 | 
					
						
							|  |  |  | 	return make([]byte, dataLen, dataLen+dataHeaderLen) | 
					
						
							| 
									
										
										
										
											2020-03-10 18:46:46 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | type sshFxpRenamePacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID      uint32 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Oldpath string | 
					
						
							|  |  |  | 	Newpath string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpRenamePacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-05-23 20:51:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpRenamePacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 		4 + len(p.Oldpath) + | 
					
						
							|  |  |  | 		4 + len(p.Newpath) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 	b = append(b, sshFxpRename) | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	b = marshalString(b, p.Oldpath) | 
					
						
							|  |  |  | 	b = marshalString(b, p.Newpath) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	return b, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | func (p *sshFxpRenamePacket) UnmarshalBinary(b []byte) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	if p.ID, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | 	} else if p.Oldpath, b, err = unmarshalStringSafe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2018-02-16 03:00:22 +08:00
										 |  |  | 	} else if p.Newpath, _, err = unmarshalStringSafe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2015-08-01 06:46:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-05 19:46:03 +08:00
										 |  |  | type sshFxpPosixRenamePacket struct { | 
					
						
							|  |  |  | 	ID      uint32 | 
					
						
							|  |  |  | 	Oldpath string | 
					
						
							|  |  |  | 	Newpath string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpPosixRenamePacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2017-09-05 19:46:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpPosixRenamePacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2017-09-05 19:46:03 +08:00
										 |  |  | 	const ext = "posix-rename@openssh.com" | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							| 
									
										
										
										
											2017-09-05 19:46:03 +08:00
										 |  |  | 		4 + len(ext) + | 
					
						
							|  |  |  | 		4 + len(p.Oldpath) + | 
					
						
							|  |  |  | 		4 + len(p.Newpath) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 	b = append(b, sshFxpExtended) | 
					
						
							| 
									
										
										
										
											2017-09-05 19:46:03 +08:00
										 |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							|  |  |  | 	b = marshalString(b, ext) | 
					
						
							|  |  |  | 	b = marshalString(b, p.Oldpath) | 
					
						
							|  |  |  | 	b = marshalString(b, p.Newpath) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-05 19:46:03 +08:00
										 |  |  | 	return b, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | type sshFxpWritePacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID     uint32 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Length uint32 | 
					
						
							| 
									
										
										
										
											2020-09-08 16:18:15 +08:00
										 |  |  | 	Offset uint64 | 
					
						
							|  |  |  | 	Handle string | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Data   []byte | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpWritePacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-05-23 20:51:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpWritePacket) marshalPacket() ([]byte, []byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 		4 + len(p.Handle) + | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 		8 + // uint64
 | 
					
						
							|  |  |  | 		4 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 	b = append(b, sshFxpWrite) | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							|  |  |  | 	b = marshalString(b, p.Handle) | 
					
						
							|  |  |  | 	b = marshalUint64(b, p.Offset) | 
					
						
							|  |  |  | 	b = marshalUint32(b, p.Length) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return b, p.Data, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpWritePacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	header, payload, err := p.marshalPacket() | 
					
						
							|  |  |  | 	return append(header, payload...), err | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | func (p *sshFxpWritePacket) UnmarshalBinary(b []byte) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	if p.ID, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2015-07-30 08:37:58 +08:00
										 |  |  | 	} else if p.Handle, b, err = unmarshalStringSafe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2015-07-30 08:37:58 +08:00
										 |  |  | 	} else if p.Offset, b, err = unmarshalUint64Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2015-07-30 08:37:58 +08:00
										 |  |  | 	} else if p.Length, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2015-07-30 08:37:58 +08:00
										 |  |  | 	} else if uint32(len(b)) < p.Length { | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 		return errShortPacket | 
					
						
							| 
									
										
										
										
											2015-07-30 08:37:58 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-26 19:04:13 +08:00
										 |  |  | 	p.Data = b[:p.Length] | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2015-07-30 08:37:58 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | type sshFxpMkdirPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID    uint32 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Flags uint32 // ignored
 | 
					
						
							| 
									
										
										
										
											2020-09-08 16:18:15 +08:00
										 |  |  | 	Path  string | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpMkdirPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-05-23 20:51:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpMkdirPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 		4 + len(p.Path) + | 
					
						
							| 
									
										
										
										
											2014-10-10 02:40:02 +08:00
										 |  |  | 		4 // uint32
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 	b = append(b, sshFxpMkdir) | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	b = marshalString(b, p.Path) | 
					
						
							|  |  |  | 	b = marshalUint32(b, p.Flags) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	return b, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 04:56:04 +08:00
										 |  |  | func (p *sshFxpMkdirPacket) UnmarshalBinary(b []byte) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	if p.ID, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2015-07-26 10:07:33 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} else if p.Path, b, err = unmarshalStringSafe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2018-02-16 03:00:22 +08:00
										 |  |  | 	} else if p.Flags, _, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2015-07-26 10:07:33 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | type sshFxpSetstatPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID    uint32 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Flags uint32 | 
					
						
							| 
									
										
										
										
											2020-09-08 16:18:15 +08:00
										 |  |  | 	Path  string | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	Attrs interface{} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-07 17:13:07 +08:00
										 |  |  | type sshFxpFsetstatPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID     uint32 | 
					
						
							| 
									
										
										
										
											2015-09-07 17:13:07 +08:00
										 |  |  | 	Flags  uint32 | 
					
						
							| 
									
										
										
										
											2020-09-08 16:18:15 +08:00
										 |  |  | 	Handle string | 
					
						
							| 
									
										
										
										
											2015-09-07 17:13:07 +08:00
										 |  |  | 	Attrs  interface{} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpSetstatPacket) id() uint32  { return p.ID } | 
					
						
							|  |  |  | func (p *sshFxpFsetstatPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-05-23 20:51:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpSetstatPacket) marshalPacket() ([]byte, []byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 		4 + len(p.Path) + | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 		4 // uint32
 | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 	b = append(b, sshFxpSetstat) | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | 	b = marshalString(b, p.Path) | 
					
						
							|  |  |  | 	b = marshalUint32(b, p.Flags) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	switch attrs := p.Attrs.(type) { | 
					
						
							| 
									
										
										
										
											2024-02-06 16:41:41 +08:00
										 |  |  | 	case []byte: | 
					
						
							|  |  |  | 		return b, attrs, nil // may as well short-ciruit this case.
 | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	case os.FileInfo: | 
					
						
							|  |  |  | 		_, fs := fileStatFromInfo(attrs) // we throw away the flags, and override with those in packet.
 | 
					
						
							|  |  |  | 		return b, marshalFileStat(nil, p.Flags, fs), nil | 
					
						
							|  |  |  | 	case *FileStat: | 
					
						
							|  |  |  | 		return b, marshalFileStat(nil, p.Flags, attrs), nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	return b, marshal(nil, p.Attrs), nil | 
					
						
							| 
									
										
										
										
											2014-09-26 04:40:48 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-26 16:32:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpSetstatPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	header, payload, err := p.marshalPacket() | 
					
						
							|  |  |  | 	return append(header, payload...), err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpFsetstatPacket) marshalPacket() ([]byte, []byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							| 
									
										
										
										
											2015-09-07 17:13:07 +08:00
										 |  |  | 		4 + len(p.Handle) + | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 		4 // uint32
 | 
					
						
							| 
									
										
										
										
											2015-09-07 17:13:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 	b = append(b, sshFxpFsetstat) | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							| 
									
										
										
										
											2015-09-07 17:13:07 +08:00
										 |  |  | 	b = marshalString(b, p.Handle) | 
					
						
							|  |  |  | 	b = marshalUint32(b, p.Flags) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	switch attrs := p.Attrs.(type) { | 
					
						
							| 
									
										
										
										
											2024-02-06 16:41:41 +08:00
										 |  |  | 	case []byte: | 
					
						
							|  |  |  | 		return b, attrs, nil // may as well short-ciruit this case.
 | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	case os.FileInfo: | 
					
						
							|  |  |  | 		_, fs := fileStatFromInfo(attrs) // we throw away the flags, and override with those in packet.
 | 
					
						
							|  |  |  | 		return b, marshalFileStat(nil, p.Flags, fs), nil | 
					
						
							|  |  |  | 	case *FileStat: | 
					
						
							|  |  |  | 		return b, marshalFileStat(nil, p.Flags, attrs), nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	return b, marshal(nil, p.Attrs), nil | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpFsetstatPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	header, payload, err := p.marshalPacket() | 
					
						
							|  |  |  | 	return append(header, payload...), err | 
					
						
							| 
									
										
										
										
											2015-09-07 17:13:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (p *sshFxpSetstatPacket) UnmarshalBinary(b []byte) error { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	var err error | 
					
						
							|  |  |  | 	if p.ID, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2015-09-07 17:13:07 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} else if p.Path, b, err = unmarshalStringSafe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} else if p.Flags, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	p.Attrs = b | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | func (p *sshFxpSetstatPacket) unmarshalFileStat(flags uint32) (*FileStat, error) { | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	switch attrs := p.Attrs.(type) { | 
					
						
							|  |  |  | 	case *FileStat: | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 		return attrs, nil | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	case []byte: | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 		fs, _, err := unmarshalFileStat(flags, attrs) | 
					
						
							|  |  |  | 		return fs, err | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 		return nil, fmt.Errorf("invalid type in unmarshalFileStat: %T", attrs) | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-07 17:13:07 +08:00
										 |  |  | func (p *sshFxpFsetstatPacket) UnmarshalBinary(b []byte) error { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	var err error | 
					
						
							|  |  |  | 	if p.ID, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2015-09-07 17:13:07 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} else if p.Handle, b, err = unmarshalStringSafe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} else if p.Flags, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	p.Attrs = b | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | func (p *sshFxpFsetstatPacket) unmarshalFileStat(flags uint32) (*FileStat, error) { | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	switch attrs := p.Attrs.(type) { | 
					
						
							|  |  |  | 	case *FileStat: | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 		return attrs, nil | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	case []byte: | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 		fs, _, err := unmarshalFileStat(flags, attrs) | 
					
						
							|  |  |  | 		return fs, err | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2024-01-19 09:23:22 +08:00
										 |  |  | 		return nil, fmt.Errorf("invalid type in unmarshalFileStat: %T", attrs) | 
					
						
							| 
									
										
										
										
											2024-01-19 08:20:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-26 16:32:19 +08:00
										 |  |  | type sshFxpHandlePacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID     uint32 | 
					
						
							| 
									
										
										
										
											2015-07-26 16:32:19 +08:00
										 |  |  | 	Handle string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpHandlePacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							|  |  |  | 		4 + len(p.Handle) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	b := make([]byte, 4, l) | 
					
						
							|  |  |  | 	b = append(b, sshFxpHandle) | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							| 
									
										
										
										
											2015-07-26 16:32:19 +08:00
										 |  |  | 	b = marshalString(b, p.Handle) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-26 16:32:19 +08:00
										 |  |  | 	return b, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type sshFxpStatusPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID uint32 | 
					
						
							| 
									
										
										
										
											2015-07-26 16:32:19 +08:00
										 |  |  | 	StatusError | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpStatusPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							|  |  |  | 		4 + | 
					
						
							|  |  |  | 		4 + len(p.StatusError.msg) + | 
					
						
							|  |  |  | 		4 + len(p.StatusError.lang) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	b := make([]byte, 4, l) | 
					
						
							|  |  |  | 	b = append(b, sshFxpStatus) | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							| 
									
										
										
										
											2015-07-26 16:32:19 +08:00
										 |  |  | 	b = marshalStatus(b, p.StatusError) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-26 16:32:19 +08:00
										 |  |  | 	return b, nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-30 08:24:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type sshFxpDataPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID     uint32 | 
					
						
							| 
									
										
										
										
											2015-07-30 08:24:24 +08:00
										 |  |  | 	Length uint32 | 
					
						
							|  |  |  | 	Data   []byte | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpDataPacket) marshalPacket() ([]byte, []byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							|  |  |  | 		4 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	b := make([]byte, 4, l) | 
					
						
							|  |  |  | 	b = append(b, sshFxpData) | 
					
						
							|  |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							|  |  |  | 	b = marshalUint32(b, p.Length) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return b, p.Data, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-11 05:56:41 +08:00
										 |  |  | // MarshalBinary encodes the receiver into a binary form and returns the result.
 | 
					
						
							|  |  |  | // To avoid a new allocation the Data slice must have a capacity >= Length + 9
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // This is hand-coded rather than just append(header, payload...),
 | 
					
						
							|  |  |  | // in order to try and reuse the r.Data backing store in the packet.
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpDataPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := append(p.Data, make([]byte, dataHeaderLen)...) | 
					
						
							|  |  |  | 	copy(b[dataHeaderLen:], p.Data[:p.Length]) | 
					
						
							|  |  |  | 	// b[0:4] will be overwritten with the length in sendPacket
 | 
					
						
							|  |  |  | 	b[4] = sshFxpData | 
					
						
							|  |  |  | 	binary.BigEndian.PutUint32(b[5:9], p.ID) | 
					
						
							|  |  |  | 	binary.BigEndian.PutUint32(b[9:13], p.Length) | 
					
						
							| 
									
										
										
										
											2015-07-30 08:24:24 +08:00
										 |  |  | 	return b, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | func (p *sshFxpDataPacket) UnmarshalBinary(b []byte) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 	if p.ID, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2015-07-30 08:24:24 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} else if p.Length, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} else if uint32(len(b)) < p.Length { | 
					
						
							| 
									
										
										
										
											2019-08-26 09:21:33 +08:00
										 |  |  | 		return errShortPacket | 
					
						
							| 
									
										
										
										
											2015-07-30 08:24:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 06:50:08 +08:00
										 |  |  | 	p.Data = b[:p.Length] | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2015-07-30 08:24:24 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-08-05 12:11:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 02:37:52 +08:00
										 |  |  | type sshFxpStatvfsPacket struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID   uint32 | 
					
						
							| 
									
										
										
										
											2015-05-16 02:37:52 +08:00
										 |  |  | 	Path string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpStatvfsPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2015-05-23 20:51:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpStatvfsPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	const ext = "statvfs@openssh.com" | 
					
						
							|  |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							|  |  |  | 		4 + len(ext) + | 
					
						
							|  |  |  | 		4 + len(p.Path) | 
					
						
							| 
									
										
										
										
											2015-05-16 02:37:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 	b = append(b, sshFxpExtended) | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b = marshalString(b, ext) | 
					
						
							| 
									
										
										
										
											2015-05-16 02:37:52 +08:00
										 |  |  | 	b = marshalString(b, p.Path) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 02:37:52 +08:00
										 |  |  | 	return b, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 04:11:16 +08:00
										 |  |  | // A StatVFS contains statistics about a filesystem.
 | 
					
						
							| 
									
										
										
										
											2015-05-16 02:37:52 +08:00
										 |  |  | type StatVFS struct { | 
					
						
							| 
									
										
										
										
											2016-01-05 05:15:21 +08:00
										 |  |  | 	ID      uint32 | 
					
						
							| 
									
										
										
										
											2015-05-23 15:10:22 +08:00
										 |  |  | 	Bsize   uint64 /* file system block size */ | 
					
						
							|  |  |  | 	Frsize  uint64 /* fundamental fs block size */ | 
					
						
							|  |  |  | 	Blocks  uint64 /* number of blocks (unit f_frsize) */ | 
					
						
							|  |  |  | 	Bfree   uint64 /* free blocks in file system */ | 
					
						
							|  |  |  | 	Bavail  uint64 /* free blocks for non-root */ | 
					
						
							|  |  |  | 	Files   uint64 /* total file inodes */ | 
					
						
							|  |  |  | 	Ffree   uint64 /* free file inodes */ | 
					
						
							|  |  |  | 	Favail  uint64 /* free file inodes for to non-root */ | 
					
						
							|  |  |  | 	Fsid    uint64 /* file system id */ | 
					
						
							|  |  |  | 	Flag    uint64 /* bit mask of f_flag values */ | 
					
						
							|  |  |  | 	Namemax uint64 /* maximum filename length */ | 
					
						
							| 
									
										
										
										
											2015-05-16 02:37:52 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 04:11:16 +08:00
										 |  |  | // TotalSpace calculates the amount of total space in a filesystem.
 | 
					
						
							| 
									
										
										
										
											2015-05-16 02:37:52 +08:00
										 |  |  | func (p *StatVFS) TotalSpace() uint64 { | 
					
						
							|  |  |  | 	return p.Frsize * p.Blocks | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 04:11:16 +08:00
										 |  |  | // FreeSpace calculates the amount of free space in a filesystem.
 | 
					
						
							| 
									
										
										
										
											2015-05-16 02:37:52 +08:00
										 |  |  | func (p *StatVFS) FreeSpace() uint64 { | 
					
						
							|  |  |  | 	return p.Frsize * p.Bfree | 
					
						
							| 
									
										
										
										
											2015-05-23 15:10:22 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-13 12:45:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | // marshalPacket converts to ssh_FXP_EXTENDED_REPLY packet binary format
 | 
					
						
							|  |  |  | func (p *StatVFS) marshalPacket() ([]byte, []byte, error) { | 
					
						
							|  |  |  | 	header := []byte{0, 0, 0, 0, sshFxpExtendedReply} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 16:05:33 +08:00
										 |  |  | 	var buf bytes.Buffer | 
					
						
							|  |  |  | 	err := binary.Write(&buf, binary.BigEndian, p) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return header, buf.Bytes(), err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 19:41:05 +08:00
										 |  |  | // MarshalBinary encodes the StatVFS as an SSH_FXP_EXTENDED_REPLY packet.
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | func (p *StatVFS) MarshalBinary() ([]byte, error) { | 
					
						
							|  |  |  | 	header, payload, err := p.marshalPacket() | 
					
						
							|  |  |  | 	return append(header, payload...), err | 
					
						
							| 
									
										
										
										
											2016-06-13 12:45:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-11 20:03:31 +08:00
										 |  |  | type sshFxpFsyncPacket struct { | 
					
						
							|  |  |  | 	ID     uint32 | 
					
						
							|  |  |  | 	Handle string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpFsyncPacket) id() uint32 { return p.ID } | 
					
						
							| 
									
										
										
										
											2020-10-11 20:03:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpFsyncPacket) MarshalBinary() ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	const ext = "fsync@openssh.com" | 
					
						
							|  |  |  | 	l := 4 + 1 + 4 + // uint32(length) + byte(type) + uint32(id)
 | 
					
						
							|  |  |  | 		4 + len(ext) + | 
					
						
							| 
									
										
										
										
											2020-10-11 20:03:31 +08:00
										 |  |  | 		4 + len(p.Handle) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b := make([]byte, 4, l) | 
					
						
							| 
									
										
										
										
											2020-10-11 20:03:31 +08:00
										 |  |  | 	b = append(b, sshFxpExtended) | 
					
						
							|  |  |  | 	b = marshalUint32(b, p.ID) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 	b = marshalString(b, ext) | 
					
						
							| 
									
										
										
										
											2020-10-11 20:03:31 +08:00
										 |  |  | 	b = marshalString(b, p.Handle) | 
					
						
							| 
									
										
										
										
											2020-10-29 04:54:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-11 20:03:31 +08:00
										 |  |  | 	return b, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-13 12:45:13 +08:00
										 |  |  | type sshFxpExtendedPacket struct { | 
					
						
							|  |  |  | 	ID              uint32 | 
					
						
							|  |  |  | 	ExtendedRequest string | 
					
						
							|  |  |  | 	SpecificPacket  interface { | 
					
						
							|  |  |  | 		serverRespondablePacket | 
					
						
							|  |  |  | 		readonly() bool | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpExtendedPacket) id() uint32 { return p.ID } | 
					
						
							|  |  |  | func (p *sshFxpExtendedPacket) readonly() bool { | 
					
						
							| 
									
										
										
										
											2018-03-19 22:32:22 +08:00
										 |  |  | 	if p.SpecificPacket == nil { | 
					
						
							|  |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return p.SpecificPacket.readonly() | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-13 12:45:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpExtendedPacket) respond(svr *Server) responsePacket { | 
					
						
							| 
									
										
										
										
											2018-03-19 22:32:22 +08:00
										 |  |  | 	if p.SpecificPacket == nil { | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | 		return statusFromError(p.ID, nil) | 
					
						
							| 
									
										
										
										
											2018-03-19 22:32:22 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-13 12:45:13 +08:00
										 |  |  | 	return p.SpecificPacket.respond(svr) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (p *sshFxpExtendedPacket) UnmarshalBinary(b []byte) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 	bOrig := b | 
					
						
							|  |  |  | 	if p.ID, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2018-02-16 03:00:22 +08:00
										 |  |  | 	} else if p.ExtendedRequest, _, err = unmarshalStringSafe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-06-13 12:45:13 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// specific unmarshalling
 | 
					
						
							|  |  |  | 	switch p.ExtendedRequest { | 
					
						
							|  |  |  | 	case "statvfs@openssh.com": | 
					
						
							|  |  |  | 		p.SpecificPacket = &sshFxpExtendedPacketStatVFS{} | 
					
						
							| 
									
										
										
										
											2017-09-05 19:46:03 +08:00
										 |  |  | 	case "posix-rename@openssh.com": | 
					
						
							|  |  |  | 		p.SpecificPacket = &sshFxpExtendedPacketPosixRename{} | 
					
						
							| 
									
										
										
										
											2019-05-25 03:23:18 +08:00
										 |  |  | 	case "hardlink@openssh.com": | 
					
						
							|  |  |  | 		p.SpecificPacket = &sshFxpExtendedPacketHardlink{} | 
					
						
							| 
									
										
										
										
											2016-06-13 12:45:13 +08:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2021-06-05 05:18:41 +08:00
										 |  |  | 		return fmt.Errorf("packet type %v: %w", p.SpecificPacket, errUnknownExtendedPacket) | 
					
						
							| 
									
										
										
										
											2016-06-13 12:45:13 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return p.SpecificPacket.UnmarshalBinary(bOrig) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type sshFxpExtendedPacketStatVFS struct { | 
					
						
							|  |  |  | 	ID              uint32 | 
					
						
							|  |  |  | 	ExtendedRequest string | 
					
						
							|  |  |  | 	Path            string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpExtendedPacketStatVFS) id() uint32     { return p.ID } | 
					
						
							|  |  |  | func (p *sshFxpExtendedPacketStatVFS) readonly() bool { return true } | 
					
						
							| 
									
										
										
										
											2016-06-13 12:45:13 +08:00
										 |  |  | func (p *sshFxpExtendedPacketStatVFS) UnmarshalBinary(b []byte) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 	if p.ID, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} else if p.ExtendedRequest, b, err = unmarshalStringSafe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2018-02-16 03:00:22 +08:00
										 |  |  | 	} else if p.Path, _, err = unmarshalStringSafe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2016-06-13 12:45:13 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-05 19:46:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type sshFxpExtendedPacketPosixRename struct { | 
					
						
							|  |  |  | 	ID              uint32 | 
					
						
							|  |  |  | 	ExtendedRequest string | 
					
						
							|  |  |  | 	Oldpath         string | 
					
						
							|  |  |  | 	Newpath         string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpExtendedPacketPosixRename) id() uint32     { return p.ID } | 
					
						
							|  |  |  | func (p *sshFxpExtendedPacketPosixRename) readonly() bool { return false } | 
					
						
							| 
									
										
										
										
											2017-09-05 19:46:03 +08:00
										 |  |  | func (p *sshFxpExtendedPacketPosixRename) UnmarshalBinary(b []byte) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 	if p.ID, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} else if p.ExtendedRequest, b, err = unmarshalStringSafe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} else if p.Oldpath, b, err = unmarshalStringSafe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2018-02-16 03:00:22 +08:00
										 |  |  | 	} else if p.Newpath, _, err = unmarshalStringSafe(b); err != nil { | 
					
						
							| 
									
										
										
										
											2017-09-05 19:46:03 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpExtendedPacketPosixRename) respond(s *Server) responsePacket { | 
					
						
							| 
									
										
										
										
											2022-10-18 23:15:21 +08:00
										 |  |  | 	err := os.Rename(s.toLocalPath(p.Oldpath), s.toLocalPath(p.Newpath)) | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | 	return statusFromError(p.ID, err) | 
					
						
							| 
									
										
										
										
											2017-09-05 19:46:03 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-25 03:23:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type sshFxpExtendedPacketHardlink struct { | 
					
						
							|  |  |  | 	ID              uint32 | 
					
						
							|  |  |  | 	ExtendedRequest string | 
					
						
							|  |  |  | 	Oldpath         string | 
					
						
							|  |  |  | 	Newpath         string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://github.com/openssh/openssh-portable/blob/master/PROTOCOL
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpExtendedPacketHardlink) id() uint32     { return p.ID } | 
					
						
							|  |  |  | func (p *sshFxpExtendedPacketHardlink) readonly() bool { return true } | 
					
						
							| 
									
										
										
										
											2019-05-25 03:23:18 +08:00
										 |  |  | func (p *sshFxpExtendedPacketHardlink) UnmarshalBinary(b []byte) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 	if p.ID, b, err = unmarshalUint32Safe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} else if p.ExtendedRequest, b, err = unmarshalStringSafe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} else if p.Oldpath, b, err = unmarshalStringSafe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} else if p.Newpath, _, err = unmarshalStringSafe(b); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | func (p *sshFxpExtendedPacketHardlink) respond(s *Server) responsePacket { | 
					
						
							| 
									
										
										
										
											2022-10-18 23:15:21 +08:00
										 |  |  | 	err := os.Link(s.toLocalPath(p.Oldpath), s.toLocalPath(p.Newpath)) | 
					
						
							| 
									
										
										
										
											2021-02-22 20:00:27 +08:00
										 |  |  | 	return statusFromError(p.ID, err) | 
					
						
							| 
									
										
										
										
											2019-05-25 03:23:18 +08:00
										 |  |  | } |