updates.
[silc.git] / lib / silcsftp / tests / sftp_client.c
1 /*
2
3   sftp_client.c 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2001 Pekka Riikonen
8
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.
12
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.
17
18 */
19
20 #include "silcincludes.h"
21 #include "silcsftp.h"
22
23 typedef struct {
24   SilcSchedule schedule;
25   SilcSocketConnection sock;
26   SilcSFTP sftp;
27 } *Client;
28
29 Client gclient;
30
31 char *dir;
32 char *file;
33 bool opendir;
34 uint64 offset;
35
36 static void sftp_name(SilcSFTP sftp, SilcSFTPStatus status,
37                       const SilcSFTPName name, void *context);
38 static void sftp_handle(SilcSFTP sftp, SilcSFTPStatus status,
39                         SilcSFTPHandle handle, void *context);
40 static void sftp_data(SilcSFTP sftp, SilcSFTPStatus status,
41                       const unsigned char *data, uint32 data_len,
42                       void *context);
43
44 static void send_packet(SilcSocketConnection sock,
45                         SilcBuffer packet, void *context)
46 {
47   Client client = (Client)context;
48   SilcPacketContext packetdata;
49   int ret;
50
51   memset(&packetdata, 0, sizeof(packetdata));
52   packetdata.type = SILC_PACKET_FTP;
53   packetdata.truelen = packet->len + SILC_PACKET_HEADER_LEN;
54   packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
55   silc_packet_send_prepare(sock,
56                            SILC_PACKET_HEADER_LEN,
57                            packetdata.padlen,
58                            packet->len);
59   packetdata.buffer = sock->outbuf;
60   silc_buffer_put(sock->outbuf, packet->data, packet->len);
61   silc_packet_assemble(&packetdata);
62   ret = silc_packet_send(sock, TRUE);
63   if (ret != -2)
64     return;
65
66   silc_schedule_set_listen_fd(client->schedule, sock->sock, 
67                               (SILC_TASK_READ | SILC_TASK_WRITE));
68   SILC_SET_OUTBUF_PENDING(sock);
69 }
70
71 static void packet_parse(SilcPacketParserContext *parser)
72 {
73   Client client = (Client)parser->context;
74   SilcSocketConnection sock = parser->sock;
75   SilcPacketContext *packet = parser->packet;
76   int ret;
77   
78   ret = silc_packet_parse(packet);
79   assert(packet->type == SILC_PACKET_FTP);
80
81   silc_sftp_client_receive_process(client->sftp, sock, packet);
82 }
83
84 SILC_TASK_CALLBACK(packet_process)
85 {
86   Client client = (Client)context;
87   SilcSocketConnection sock = client->sock;
88   int ret;
89
90   if (type == SILC_TASK_WRITE) {
91     if (sock->outbuf->data - sock->outbuf->head)
92      silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
93
94     ret = silc_packet_send(sock, TRUE);
95     if (ret < 0)
96       return;
97
98     silc_schedule_set_listen_fd(client->schedule, fd, SILC_TASK_READ);
99     SILC_UNSET_OUTBUF_PENDING(sock);
100     silc_buffer_clear(sock->outbuf);
101     return;
102   }
103
104   if (type == SILC_TASK_READ) {
105     ret = silc_packet_receive(sock);
106     if (ret < 0)
107       return;
108
109     if (ret == 0) {
110       silc_net_close_connection(sock->sock);
111       silc_socket_free(sock);
112       exit(0);
113     }
114
115     silc_packet_receive_process(sock, NULL, NULL, packet_parse, client);
116   }
117 }
118
119 static void sftp_data(SilcSFTP sftp, SilcSFTPStatus status,
120                       const unsigned char *data, uint32 data_len,
121                       void *context)
122 {
123   SilcSFTPHandle handle = (SilcSFTPHandle)context;
124   int debug = silc_debug;
125
126   if (status != SILC_SFTP_STATUS_OK) {
127     SilcSFTPAttributesStruct attrs;
128
129     fprintf(stderr, "Status %d\n", status);
130
131     if (!strcmp(file, "/sftp/sftp_server.c"))
132       return;
133
134     /* Open another file */
135     opendir = FALSE;
136     memset(&attrs, 0, sizeof(attrs));
137     file = "/sftp/sftp_server.c";
138     fprintf(stderr, "Opening file %s\n", file);
139     offset = 0;
140     silc_sftp_open(sftp, file, SILC_SFTP_FXF_READ,
141                    &attrs, sftp_handle, gclient);
142     return;
143   }
144
145   if (!debug)
146     silc_debug = 1;
147   SILC_LOG_HEXDUMP(("data"), (unsigned char *)data, data_len);
148   silc_debug = debug;
149
150   offset += data_len;
151
152   /* Attempt to read more */
153   fprintf(stderr, "Reading more of file %s\n", file);
154   silc_sftp_read(sftp, handle, offset, 2048, sftp_data, handle);
155 }
156
157 static void sftp_name(SilcSFTP sftp, SilcSFTPStatus status,
158                       const SilcSFTPName name, void *context)
159 {
160   Client client = (Client)context;
161   int i;
162
163   SILC_LOG_DEBUG(("Name"));
164   fprintf(stderr, "Status %d\n", status);
165
166   fprintf(stderr, "Directory: %s\n", dir);
167   for (i = 0; i < name->count; i++) {
168     fprintf(stderr, "%s\n", name->long_filename[i]);
169   }
170
171   if (!strcmp(dir, "sftp")) {
172     SilcSFTPAttributesStruct attrs;
173
174     /* open */
175     opendir = FALSE;
176     memset(&attrs, 0, sizeof(attrs));
177     file = "passwd";
178     fprintf(stderr, "Opening file %s\n", file);
179     offset = 0;
180     silc_sftp_open(sftp, file, SILC_SFTP_FXF_READ,
181                    &attrs, sftp_handle, client);
182     return;
183   }
184
185   if (!strcmp(dir, "/"))
186     dir = "sftp";
187
188   fprintf(stderr, "Opening %s\n", dir);
189
190   /* opendir */
191   opendir = TRUE;
192   silc_sftp_opendir(sftp, dir, sftp_handle, client);
193 }
194
195 static void sftp_handle(SilcSFTP sftp, SilcSFTPStatus status,
196                         SilcSFTPHandle handle, void *context)
197 {
198   Client client = (Client)context;
199
200   SILC_LOG_DEBUG(("Handle"));
201   fprintf(stderr, "Status %d\n", status);
202   if (status != SILC_SFTP_STATUS_OK)
203     return;
204
205   if (opendir) {
206     fprintf(stderr, "Reading %s\n", dir);
207     /* Readdir */
208     silc_sftp_readdir(sftp, handle, sftp_name, client);
209   } else {
210     fprintf(stderr, "Reading file %s\n", file);
211
212     /* Read */
213     silc_sftp_read(sftp, handle, 0, 2048, sftp_data, handle);
214   }
215 }
216
217 static void sftp_version(SilcSFTP sftp, SilcSFTPStatus status,
218                          SilcSFTPVersion version, void *context)
219 {
220   Client client = (Client)context;
221   fprintf(stderr, "Version: %d\n", (int)version);
222
223   SILC_LOG_DEBUG(("Version"));
224   fprintf(stderr, "Status %d\n", status);
225
226   /* opendir */
227   dir = "/";
228   fprintf(stderr, "Opening %s\n", dir);
229   opendir = TRUE;
230   silc_sftp_opendir(sftp, dir, sftp_handle, client);
231 }
232
233 int main(int argc, char **argv)
234 {
235   Client client = silc_calloc(1, sizeof(*client));
236   int sock;
237
238   gclient = client;
239
240   if (argc > 1 && !strcmp(argv[1], "-d"))
241     silc_debug = 1;
242
243   client->schedule = silc_schedule_init(100);
244   if (!client->schedule)
245     return -1;
246
247   /* Connecto to server */
248   sock = silc_net_create_connection(NULL, 5000, "127.0.0.1");
249   if (sock < 0)
250     return -1;
251   silc_socket_alloc(sock, 0, NULL, &client->sock);
252   silc_schedule_task_add(client->schedule, sock,
253                          packet_process, client, 0, 0,
254                          SILC_TASK_GENERIC, SILC_TASK_PRI_NORMAL);
255
256   /* Start SFTP session */
257   client->sftp = silc_sftp_client_start(client->sock, send_packet, client,
258                                         sftp_version, client);
259
260   silc_schedule(client->schedule);
261   return 0;
262 }