5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 - 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 silc_sftp_client_start();
22 silc_sftp_client_receive_process();
36 SilcSchedule schedule;
49 static void sftp_name(SilcSFTP sftp, SilcSFTPStatus status,
50 const SilcSFTPName name, void *context);
51 static void sftp_handle(SilcSFTP sftp, SilcSFTPStatus status,
52 SilcSFTPHandle handle, void *context);
53 static void sftp_data(SilcSFTP sftp, SilcSFTPStatus status,
54 const unsigned char *data, SilcUInt32 data_len,
56 static void end_test(void);
58 static void sftp_status(SilcSFTP sftp, SilcSFTPStatus status,
59 const char *message, const char *lang_tag,
62 fprintf(stderr, "Status %d\n", status);
63 if (status != SILC_SFTP_STATUS_OK) {
64 SILC_LOG_DEBUG(("Error status"));
74 static void sftp_attr(SilcSFTP sftp, SilcSFTPStatus status,
75 const SilcSFTPAttributes attrs, void *context)
77 SilcSFTPHandle handle = (SilcSFTPHandle)context;
80 fprintf(stderr, "Status %d\n", status);
81 if (status != SILC_SFTP_STATUS_OK) {
82 SILC_LOG_DEBUG(("Error status"));
89 SILC_LOG_DEBUG(("Attr.flags: %d", attrs->flags));
90 SILC_LOG_DEBUG(("Attr.size: %lu", attrs->size));
91 SILC_LOG_DEBUG(("Attr.uid: %d", attrs->uid));
92 SILC_LOG_DEBUG(("Attr.gid: %d", attrs->gid));
93 SILC_LOG_DEBUG(("Attr.permissions: %d", attrs->permissions));
94 SILC_LOG_DEBUG(("Attr.atime: %d", attrs->atime));
95 SILC_LOG_DEBUG(("Attr.mtime: %d", attrs->mtime));
96 SILC_LOG_DEBUG(("Attr.extended count: %d", attrs->extended_count));
97 for (i = 0; i < attrs->extended_count; i++) {
98 SILC_LOG_HEXDUMP(("Attr.extended_type[i]:", i),
99 attrs->extended_type[i]->data,
100 silc_buffer_len(attrs->extended_type[i]));
101 SILC_LOG_HEXDUMP(("Attr.extended_data[i]:", i),
102 attrs->extended_data[i]->data,
103 silc_buffer_len(attrs->extended_data[i]));
107 fprintf(stderr, "Closing file\n");
108 silc_sftp_close(sftp, handle, sftp_status, context);
112 fprintf(stderr, "LStatting file %s\n", file);
113 silc_sftp_lstat(sftp, file, sftp_attr, context);
117 static void sftp_data(SilcSFTP sftp, SilcSFTPStatus status,
118 const unsigned char *data, SilcUInt32 data_len,
121 SilcSFTPHandle handle = (SilcSFTPHandle)context;
123 if (status != SILC_SFTP_STATUS_OK) {
124 SilcSFTPAttributesStruct attrs;
126 fprintf(stderr, "Status %d\n", status);
128 if (status != SILC_SFTP_STATUS_EOF) {
129 SILC_LOG_DEBUG(("Error status"));
135 if (!strcmp(file, "/sftp/sftp_server.c")) {
136 fprintf(stderr, "FStatting file handle %s\n", file);
137 silc_sftp_fstat(sftp, handle, sftp_attr, context);
141 /* Open another file */
143 memset(&attrs, 0, sizeof(attrs));
144 file = "/sftp/sftp_server.c";
145 fprintf(stderr, "Opening file %s\n", file);
147 silc_sftp_open(sftp, file, SILC_SFTP_FXF_READ,
148 &attrs, sftp_handle, gclient);
152 SILC_LOG_HEXDUMP(("data"), (unsigned char *)data, data_len);
156 /* Attempt to read more */
157 fprintf(stderr, "Reading more of file %s\n", file);
158 silc_sftp_read(sftp, handle, offset, 2048, sftp_data, handle);
161 static void sftp_name(SilcSFTP sftp, SilcSFTPStatus status,
162 const SilcSFTPName name, void *context)
164 Client client = (Client)context;
167 SILC_LOG_DEBUG(("Name"));
168 fprintf(stderr, "Status %d\n", status);
170 if (status != SILC_SFTP_STATUS_OK) {
171 SILC_LOG_DEBUG(("Error status"));
177 fprintf(stderr, "Directory: %s\n", dir);
178 for (i = 0; i < name->count; i++) {
179 fprintf(stderr, "%s\n", name->long_filename[i]);
182 if (!strcmp(dir, "sftp")) {
183 SilcSFTPAttributesStruct attrs;
187 memset(&attrs, 0, sizeof(attrs));
189 fprintf(stderr, "Opening file %s\n", file);
191 silc_sftp_open(sftp, file, SILC_SFTP_FXF_READ,
192 &attrs, sftp_handle, client);
196 if (!strcmp(dir, "/"))
199 fprintf(stderr, "Opening %s\n", dir);
203 silc_sftp_opendir(sftp, dir, sftp_handle, client);
206 static void sftp_handle(SilcSFTP sftp, SilcSFTPStatus status,
207 SilcSFTPHandle handle, void *context)
209 Client client = (Client)context;
211 SILC_LOG_DEBUG(("Handle"));
212 fprintf(stderr, "Status %d\n", status);
213 if (status != SILC_SFTP_STATUS_OK) {
214 SILC_LOG_DEBUG(("Error status"));
221 fprintf(stderr, "Reading %s\n", dir);
223 silc_sftp_readdir(sftp, handle, sftp_name, client);
225 fprintf(stderr, "Reading file %s\n", file);
228 silc_sftp_read(sftp, handle, 0, 2048, sftp_data, handle);
232 static void sftp_version(SilcSFTP sftp, SilcSFTPStatus status,
233 SilcSFTPVersion version, void *context)
235 Client client = (Client)context;
236 fprintf(stderr, "Version: %d\n", (int)version);
238 SILC_LOG_DEBUG(("Version"));
239 fprintf(stderr, "Status %d\n", status);
240 if (status != SILC_SFTP_STATUS_OK) {
241 SILC_LOG_DEBUG(("Error status"));
249 fprintf(stderr, "Opening %s\n", dir);
251 silc_sftp_opendir(sftp, dir, sftp_handle, client);
254 static void sftp_error(SilcSFTP sftp, SilcSFTPStatus status,
257 Client client = context;
258 SILC_LOG_DEBUG(("Error %d", status));
259 silc_stream_destroy(client->stream);
264 static void connect_callback(SilcNetStatus status, SilcStream stream,
267 Client client = context;
270 SILC_LOG_DEBUG(("Connect error"));
275 /* Start SFTP session */
276 client->stream = stream;
277 client->sftp = silc_sftp_client_start(stream, client->schedule, sftp_version,
285 int main(int argc, char **argv)
287 Client client = silc_calloc(1, sizeof(*client));
292 if (!strcmp(argv[1], "-d"))
293 silc_log_debug(TRUE);
294 if (argc > 2 && !strcmp(argv[2], "-x"))
295 silc_log_debug_hexdump(TRUE);
296 silc_log_set_debug_string("*");
299 client->schedule = silc_schedule_init(0, NULL, NULL);
300 if (!client->schedule)
303 /* Connecto to server */
304 silc_net_tcp_connect(NULL, "127.0.0.1", 5000, client->schedule,
305 connect_callback, client);
307 silc_schedule(client->schedule);
311 static void end_test(void)
313 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
314 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");