1 /*
2 
3   sftp_util.h
4 
5   Author: Pekka Riikonen <priikone@silcnet.org>
6 
7   Copyright (C) 2001 Pekka Riikonen
8 
9   The contents of this file are subject to one of the Licenses specified
10   in the COPYING file;  You may not use this file except in compliance
11   with the License.
12 
13   The software distributed under the License is distributed on an "AS IS"
14   basis, in the hope that it will be useful, but WITHOUT WARRANTY OF ANY
15   KIND, either expressed or implied.  See the COPYING file for more
16   information.
17 
18 */
19 
20 #ifndef SFTP_UTIL_H
21 #define SFTP_UTIL_H
22 
23 typedef SilcUInt32 SilcSFTPPacket;
24 
25 /* SFTP packet types */
26 #define SILC_SFTP_INIT               1
27 #define SILC_SFTP_VERSION            2
28 #define SILC_SFTP_OPEN               3
29 #define SILC_SFTP_CLOSE              4
30 #define SILC_SFTP_READ               5
31 #define SILC_SFTP_WRITE              6
32 #define SILC_SFTP_LSTAT              7
33 #define SILC_SFTP_FSTAT              8
34 #define SILC_SFTP_SETSTAT            9
35 #define SILC_SFTP_FSETSTAT           10
36 #define SILC_SFTP_OPENDIR            11
37 #define SILC_SFTP_READDIR            12
38 #define SILC_SFTP_REMOVE             13
39 #define SILC_SFTP_MKDIR              14
40 #define SILC_SFTP_RMDIR              15
41 #define SILC_SFTP_REALPATH           16
42 #define SILC_SFTP_STAT               17
43 #define SILC_SFTP_RENAME             18
44 #define SILC_SFTP_READLINK           19
45 #define SILC_SFTP_SYMLINK            20
46 #define SILC_SFTP_STATUS             101
47 #define SILC_SFTP_HANDLE             102
48 #define SILC_SFTP_DATA               103
49 #define SILC_SFTP_NAME               104
50 #define SILC_SFTP_ATTRS              105
51 #define SILC_SFTP_EXTENDED           200
52 #define SILC_SFTP_EXTENDED_REPLY     201
53 
54 /* SFTP attributes flags */
55 #define SILC_SFTP_ATTR_SIZE          0x00000001
56 #define SILC_SFTP_ATTR_UIDGID        0x00000002
57 #define SILC_SFTP_ATTR_PERMISSIONS   0x00000004
58 #define SILC_SFTP_ATTR_ACMODTIME     0x00000008
59 #define SILC_SFTP_ATTR_EXTENDED      0x80000000
60 
61 /* Encodes a SFTP packet of type `packet' of length `len'. The variable
62    argument list is encoded as data payload to the buffer. Returns the
63    encoded packet or NULL on error. The caller must free the returned
64    buffer. If `packet_buf' is non-NULL then the new packet data is put
65    to that buffer instead of allocating new one.  If the new data cannot
66    fit to `packet_buf' will be reallocated. */
67 SilcBuffer silc_sftp_packet_encode(SilcSFTPPacket packet,
68 				   SilcBuffer packet_buf, SilcUInt32 len, ...);
69 
70 /* Same as silc_sftp_packet_encode but takes the variable argument list
71    pointer as argument. */
72 SilcBuffer silc_sftp_packet_encode_vp(SilcSFTPPacket packet,
73 				      SilcBuffer packet_buf, SilcUInt32 len,
74 				      va_list vp);
75 
76 /* Decodes the SFTP packet data `data' and return the SFTP packet type.
77    The payload of the packet is returned to the `payload' pointer. Returns
78    NULL if error occurred during decoding. */
79 SilcSFTPPacket silc_sftp_packet_decode(SilcBuffer packet,
80 				       unsigned char **payload,
81 				       SilcUInt32 *payload_len);
82 
83 /* Encodes the SFTP attributes to a buffer and returns the allocated buffer.
84    The caller must free the buffer. */
85 SilcBuffer silc_sftp_attr_encode(SilcSFTPAttributes attr);
86 
87 /* Decodes SilcSFTPAttributes from the buffer `buffer'. Returns the allocated
88    attributes that the caller must free or NULL on error. */
89 SilcSFTPAttributes silc_sftp_attr_decode(SilcBuffer buffer);
90 
91 /* Frees the attributes context and its internals. */
92 void silc_sftp_attr_free(SilcSFTPAttributes attr);
93 
94 /* Adds an entry to the `name' context. */
95 void silc_sftp_name_add(SilcSFTPName name, const char *short_name,
96 			const char *long_name, SilcSFTPAttributes attrs);
97 
98 /* Encodes the SilcSFTPName to a buffer and returns the allocated buffer.
99    The caller must free the buffer. */
100 SilcBuffer silc_sftp_name_encode(SilcSFTPName name);
101 
102 /* Decodes a SilcSFTPName structure from the `buffer' that must include
103    `count' many name, longname and attribute values. Returns the allocated
104    structure or NULL on error. */
105 SilcSFTPName silc_sftp_name_decode(SilcUInt32 count, SilcBuffer buffer);
106 
107 /* Frees the name context and its internals. */
108 void silc_sftp_name_free(SilcSFTPName name);
109 
110 /* Maps errno to SFTP status message. */
111 SilcSFTPStatus silc_sftp_map_errno(int err);
112 
113 #endif /* SFTP_UTIL_H */
114