OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [ftpclient/] [current/] [src/] [ftpclient.c] - Blame information for rev 838

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      ftpclient.c
4
//
5
//      A simple FTP client
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    andrew.lunn@ascom.ch
43
// Contributors: andrew.lunn@ascom.ch
44
// Date:         2001-11-4
45
// Purpose:      
46
// Description:  
47
//              
48
//####DESCRIPTIONEND####
49
//
50
//==========================================================================
51
 
52
#include <pkgconf/system.h>
53
#include <pkgconf/net_ftpclient.h>
54
 
55
#include <network.h>
56
#include <stdio.h>
57
#include <stdlib.h>
58
#include <sys/socket.h>
59
#include <netinet/in.h>
60
#include <arpa/inet.h>
61
#include <ctype.h>
62
#include "ftpclient.h"
63
 
64
/* Build one command to send to the FTP server */
65
static int
66
build_cmd(char *buf,
67
          unsigned bufsize,
68
          char *cmd,
69
          char *arg1)
70
{
71
  int cnt;
72
 
73
  if (arg1) {
74
    cnt = snprintf(buf,bufsize,"%s %s\r\n",cmd,arg1);
75
  } else {
76
    cnt = snprintf(buf,bufsize,"%s\r\n",cmd);
77
  }
78
 
79
  if (cnt < bufsize)
80
    return 1;
81
 
82
  return 0;
83
}
84
 
85
 
86
/* Read one line from the server, being careful not to overrun the
87
   buffer. If we do reach the end of the buffer, discard the rest of
88
   the line. */
89
static int
90
get_line(int s, char *buf, unsigned buf_size, ftp_printf_t ftp_printf)
91
{
92
  int eol = 0;
93
  int cnt = 0;
94
  int ret;
95
  char c;
96
 
97
  while(!eol) {
98
    ret = read(s,&c,1);
99
 
100
    if (ret != 1) {
101
      ftp_printf(1,"read %s\n",strerror(errno));
102
      return FTP_BAD;
103
    }
104
 
105
    if (c == '\n') {
106
      eol = 1;
107
      continue;
108
    }
109
 
110
    if (cnt < buf_size) {
111
      buf[cnt++] = c;
112
    }
113
  }
114
  if (cnt < buf_size) {
115
    buf[cnt++] = '\0';
116
  } else {
117
    buf[buf_size -1] = '\0';
118
  }
119
  return 0;
120
}
121
 
122
/* Read the reply from the server and return the MSB from the return
123
   code. This gives us a basic idea if the command failed/worked. The
124
   reply can be spread over multiple lines. When this happens the line
125
   will start with a - to indicate there is more*/
126
static int
127
get_reply(int s, ftp_printf_t ftp_printf)
128
{
129
  char buf[BUFSIZ];
130
  int more = 0;
131
  int ret;
132
  int first_line=1;
133
  int code=0;
134
 
135
  do {
136
 
137
    if ((ret=get_line(s,buf,sizeof(buf),ftp_printf)) < 0) {
138
      return(ret);
139
    }
140
 
141
    ftp_printf(0,"FTP: %s\n",buf);
142
 
143
    if (first_line) {
144
      code = strtoul(buf,NULL,0);
145
      first_line=0;
146
      more = (buf[3] == '-');
147
    } else {
148
      if (isdigit(buf[0]) && isdigit(buf[1]) && isdigit(buf[2]) &&
149
          (code == strtoul(buf,NULL,0)) &&
150
          buf[3]==' ') {
151
        more=0;
152
      } else {
153
        more =1;
154
      }
155
    }
156
  } while (more);
157
 
158
  return (buf[0] - '0');
159
}
160
 
161
/* Send a command to the server */
162
static int
163
send_cmd(int s, char * msgbuf, ftp_printf_t ftp_printf)
164
{
165
  int len;
166
  int slen = strlen(msgbuf);
167
 
168
  if ((len = write(s,msgbuf,slen)) != slen) {
169
    if (len < 0) {
170
      ftp_printf(1,"write %s\n",strerror(errno));
171
      return FTP_BAD;
172
    } else {
173
      ftp_printf(1,"write truncated!\n");
174
      return FTP_BAD;
175
    }
176
  }
177
  return 1;
178
}
179
 
180
/* Send a complete command to the server and receive the reply. Return the
181
   MSB of the reply code. */
182
static int
183
command(char * cmd,
184
        char * arg,
185
        int s,
186
        char *msgbuf,
187
        int msgbuflen,
188
        ftp_printf_t ftp_printf)
189
{
190
  int err;
191
 
192
  if (!build_cmd(msgbuf,msgbuflen,cmd,arg)) {
193
    ftp_printf(1,"FTP: %s command to long\n",cmd);
194
    return FTP_BAD;
195
  }
196
 
197
  ftp_printf(0,"FTP: Sending %s command\n",cmd);
198
 
199
  if ((err=send_cmd(s,msgbuf,ftp_printf)) < 0) {
200
    return(err);
201
  }
202
 
203
  return (get_reply(s,ftp_printf));
204
}
205
 
206
/* Open a socket and connect it to the server. Also print out the
207
   address of the server for debug purposes.*/
208
 
209
static int
210
connect_to_server(char *hostname,
211
                  struct sockaddr * local,
212
                  ftp_printf_t ftp_printf)
213
{
214
  int s;
215
  socklen_t len;
216
  int error;
217
  struct addrinfo *res, *nai;
218
  char name[80];
219
  char port[8];
220
 
221
  error = getaddrinfo(hostname, "ftp", NULL, &res);
222
  if (error != EAI_NONE) {
223
    return FTP_NOSUCHHOST;
224
  }
225
 
226
  nai=res;
227
  while (nai) {
228
    s = socket(nai->ai_family, nai->ai_socktype,nai->ai_protocol);
229
    if (s < 0) {
230
      nai = nai->ai_next;
231
      continue;
232
    }
233
 
234
    if (connect(s, nai->ai_addr, nai->ai_addrlen) < 0) {
235
      getnameinfo(nai->ai_addr, nai->ai_addrlen,
236
                  name, sizeof(name), NULL,0, NI_NUMERICHOST);
237
      ftp_printf(1,"FTP Connect to %s failed: %s\n",name, strerror(errno));
238
      close (s);
239
      nai = nai->ai_next;
240
      continue;
241
    }
242
 
243
    len = sizeof(struct sockaddr);
244
    if (getsockname(s, (struct sockaddr *)local, &len) < 0) {
245
      ftp_printf(1,"getsockname failed %s\n",strerror(errno));
246
      close(s);
247
      nai = nai->ai_next;
248
      continue;
249
    }
250
    getnameinfo(nai->ai_addr, nai->ai_addrlen,
251
                name, sizeof(name), port, sizeof(port),
252
                NI_NUMERICHOST|NI_NUMERICSERV);
253
 
254
    ftp_printf(0,"FTP: Connected to %s:%s\n", name, port);
255
    freeaddrinfo(res);
256
    return (s);
257
  }
258
  freeaddrinfo(res);
259
  return FTP_NOSUCHHOST;
260
}
261
 
262
/* Perform a login to the server. Pass the username and password and
263
   put the connection into binary mode. This assumes a passwd is
264
   always needed. Is this true? */
265
 
266
static int
267
login(char * username,
268
      char *passwd,
269
      int s,
270
      char *msgbuf,
271
      unsigned msgbuflen,
272
      ftp_printf_t ftp_printf) {
273
 
274
  int ret;
275
 
276
  ret = command("USER",username,s,msgbuf,msgbuflen,ftp_printf);
277
  if (ret != 3) {
278
    ftp_printf(1,"FTP: User %s not accepted\n",username);
279
    return (FTP_BADUSER);
280
  }
281
 
282
  ret = command("PASS",passwd,s,msgbuf,msgbuflen,ftp_printf);
283
  if (ret < 0) {
284
    return (ret);
285
  }
286
  if (ret != 2) {
287
    ftp_printf(1,"FTP: Login failed for User %s\n",username);
288
    return (FTP_BADUSER);
289
  }
290
 
291
  ftp_printf(0,"FTP: Login sucessfull\n");
292
 
293
  ret = command("TYPE","I",s,msgbuf,msgbuflen,ftp_printf);
294
  if (ret < 0) {
295
    return (ret);
296
  }
297
  if (ret != 2) {
298
    ftp_printf(1,"FTP: TYPE failed!\n");
299
    return (FTP_BAD);
300
  }
301
  return (ret);
302
}
303
 
304
 
305
/* Open a data socket. This is a client socket, i.e. its listening
306
waiting for the FTP server to connect to it. Once the socket has been
307
opened send the port command to the server so the server knows which
308
port we are listening on.*/
309
static int
310
opendatasock(int ctrl_s,
311
             struct sockaddr *ctrl,
312
             char *msgbuf,
313
             unsigned msgbuflen,
314
             ftp_printf_t ftp_printf)
315
{
316
    struct sockaddr local;
317
    char name[64];
318
    char port[10];
319
    socklen_t len;
320
    int on = 1;
321
    char buf[80];
322
    int ret;
323
    int s;
324
 
325
    s = socket(ctrl->sa_family, SOCK_STREAM, 0);
326
    if (s < 0) {
327
        ftp_printf(1,"socket: %s\n",strerror(errno));
328
        return FTP_BAD;
329
    }
330
 
331
    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)) < 0) {
332
        ftp_printf(1,"setsockopt: %s\n",strerror(errno));
333
        close(s);
334
        return FTP_BAD;
335
    }
336
 
337
    memcpy(&local,ctrl,sizeof(struct sockaddr));
338
    switch (ctrl->sa_family) {
339
    case AF_INET: {
340
        struct sockaddr_in * sa4 = (struct sockaddr_in *) &local;
341
        sa4->sin_port = 0;
342
        break;
343
    }
344
#ifdef CYGPKG_NET_INET6
345
    case AF_INET6: {
346
        struct sockaddr_in6 * sa6 = (struct sockaddr_in6 *) &local;
347
        sa6->sin6_port = 0;
348
        break;
349
    }
350
#endif
351
    default:
352
        close (s);
353
        return FTP_BAD;
354
    }
355
 
356
    if (bind(s,&local,local.sa_len) < 0) {
357
        ftp_printf(1,"bind: %s\n",strerror(errno));
358
        close(s);
359
        return FTP_BAD;
360
    }
361
 
362
    len = sizeof(local);
363
    if (getsockname(s,&local,&len) < 0) {
364
        ftp_printf(1,"getsockname: %s\n",strerror(errno));
365
        close(s);
366
        return FTP_BAD;
367
    }
368
 
369
    if (listen(s, 1) < 0) {
370
        ftp_printf(1,"listen: %s\n",strerror(errno));
371
        close(s);
372
        return FTP_BAD;
373
    }
374
 
375
    getnameinfo(&local, sizeof(local), name, sizeof(name), port, sizeof(port),
376
                NI_NUMERICHOST|NI_NUMERICSERV);
377
    switch (local.sa_family) {
378
    case AF_INET: {
379
        snprintf(buf, sizeof(buf), "|1|%s|%s|", name, port);
380
        break;
381
    }
382
#ifdef CYGPKG_NET_INET6
383
    case AF_INET6: {
384
        snprintf(buf, sizeof(buf), "|2|%s|%s|", name, port);
385
        break;
386
    }
387
#endif
388
    default:
389
        close (s);
390
        return (FTP_BAD);
391
    }
392
 
393
    ret = command("EPRT",buf,ctrl_s,msgbuf,msgbuflen,ftp_printf);
394
    if (ret < 0) {
395
        close(s);
396
        return (ret);
397
    }
398
 
399
    if (ret != 2) {
400
        int _port = atoi(port);
401
        char *str = name;
402
        while (*str) {
403
            if (*str == '.') *str = ',';
404
            str++;
405
        }
406
        snprintf(buf, sizeof(buf), "%s,%d,%d", name, _port/256, _port%256);
407
        ret = command("PORT",buf,ctrl_s,msgbuf,msgbuflen,ftp_printf);
408
        if (ret < 0) {
409
            close(s);
410
            return (ret);
411
        }
412
        if (ret != 2) {
413
            ftp_printf(1,"FTP: PORT failed!\n");
414
            close(s);
415
            return (FTP_BAD);
416
        }
417
    }
418
    return (s);
419
}
420
 
421
/* Receive the file into the buffer and close the data socket
422
   afterwards */
423
static int
424
receive_file(int data_s, ftp_write_t ftp_write, void *ftp_write_priv, ftp_printf_t ftp_printf)
425
{
426
    char *buf;
427
    int finished = 0;
428
    int total_size=0;
429
    int len, wlen;
430
    int s;
431
 
432
    if ((buf = (char *)malloc(CYGNUM_NET_FTPCLIENT_BUFSIZE)) == (char *)0) {
433
        return FTP_NOMEMORY;
434
    }
435
    s = accept(data_s, NULL, 0);
436
    if (s < 0) {
437
        ftp_printf(1, "listen: %s\n",strerror(errno));
438
        free(buf);
439
        return FTP_BAD;
440
    }
441
 
442
    do {
443
        len = read(s, buf, CYGNUM_NET_FTPCLIENT_BUFSIZE);
444
        if (len < 0) {
445
            ftp_printf(1, "read: %s\n",strerror(errno));
446
            close(s);
447
            free(buf);
448
            return FTP_BAD;
449
        }
450
 
451
        if (len == 0) {
452
            finished = 1;
453
        } else {
454
            wlen = (*ftp_write)(buf, len, ftp_write_priv);
455
            if (wlen != len) {
456
                ftp_printf(1, "FTP: File too big!\n");
457
                close(s);
458
                free(buf);
459
                return FTP_TOOBIG;
460
            }
461
            total_size += len;
462
        }
463
    } while (!finished);
464
 
465
    close(s);
466
    free(buf);
467
    return total_size;
468
}
469
 
470
/* Receive the file into the buffer and close the socket afterwards*/
471
static int
472
send_file(int data_s, ftp_read_t ftp_read, void *ftp_read_priv, ftp_printf_t ftp_printf)
473
{
474
    char *buf;
475
    int len, rlen;
476
    int s;
477
 
478
    if ((buf = (char *)malloc(CYGNUM_NET_FTPCLIENT_BUFSIZE)) == (char *)0) {
479
        return FTP_NOMEMORY;
480
    }
481
    s = accept(data_s,NULL,0);
482
    if (s<0) {
483
        ftp_printf(1,"listen: %s\n",strerror(errno));
484
        free(buf);
485
        return FTP_BAD;
486
    }
487
 
488
    do {
489
        rlen = (*ftp_read)(buf, CYGNUM_NET_FTPCLIENT_BUFSIZE, ftp_read_priv);
490
        if (rlen > 0) {
491
            len = write(s, buf, rlen);
492
            if (len < 0) {
493
                ftp_printf(1,"write: %s\n",strerror(errno));
494
                close(s);
495
                free(buf);
496
                return FTP_BAD;
497
            }
498
        }
499
    } while (rlen > 0);
500
 
501
    close(s);
502
    free(buf);
503
    return 0;
504
}
505
 
506
/* All done, say bye, bye */
507
static int quit(int s,
508
                char *msgbuf,
509
                unsigned msgbuflen,
510
                ftp_printf_t ftp_printf) {
511
 
512
  int ret;
513
 
514
  ret = command("QUIT",NULL,s,msgbuf,msgbuflen,ftp_printf);
515
  if (ret < 0) {
516
    return (ret);
517
  }
518
  if (ret != 2) {
519
    ftp_printf(1,"FTP: Quit failed!\n");
520
    return (FTP_BAD);
521
  }
522
 
523
  ftp_printf(0,"FTP: Connection closed\n");
524
  return (0);
525
}
526
 
527
/* Get a file from an FTP server. Hostname is the name/IP address of
528
   the server. username is the username used to connect to the server
529
   with. Passwd is the password used to authentificate the
530
   username. filename is the name of the file to receive. It should be
531
   the full pathname of the file. buf is a pointer to a buffer the
532
   contents of the file should be placed in and buf_size is the size
533
   of the buffer. If the file is bigger than the buffer, buf_size
534
   bytes will be retrieved and an error code returned. ftp_printf is a
535
   function to be called to perform printing. On success the number of
536
   bytes received is returned. On error a negative value is returned
537
   indicating the type of error. */
538
 
539
struct _ftp_data{
540
    char *buf;
541
    int   len;
542
    int   max_len;
543
};
544
 
545
static int _ftp_read(char *buf, int len, void *priv)
546
{
547
    struct _ftp_data *dp = (struct _ftp_data *)priv;
548
    int res = 0;
549
 
550
    // FTP data channel desires to write 'len' bytes.  Fetch up
551
    // to that amount into 'buf'
552
    if (dp->len > 0) {
553
        res = dp->len;
554
        if (res > len) res = len;
555
        memcpy(buf, dp->buf, res);
556
        dp->buf += len;
557
        dp->len -= res;
558
    }
559
    return res;
560
}
561
 
562
static int _ftp_write(char *buf, int len, void *priv)
563
{
564
    struct _ftp_data *dp = (struct _ftp_data *)priv;
565
    int res = 0;
566
 
567
    // FTP data channel has 'len' bytes that have been read.
568
    // Move into 'buf', respecting the max size of 'buf'
569
    if (dp->len < dp->max_len) {
570
        res = dp->max_len - dp->len;
571
        if (res > len) {
572
            res = len;
573
        }
574
        memcpy(dp->buf, buf, res);
575
        dp->buf += len;
576
        dp->len += res;
577
    }
578
    return res;
579
}
580
 
581
int ftp_get(char * hostname,
582
            char * username,
583
            char * passwd,
584
            char * filename,
585
            char * buf,
586
            unsigned buf_size,
587
            ftp_printf_t ftp_printf)
588
{
589
    struct _ftp_data ftp_data;
590
 
591
    ftp_data.buf = buf;
592
    ftp_data.len = 0;
593
    ftp_data.max_len = buf_size;
594
    return ftp_get_var(hostname, username, passwd, filename, _ftp_write, &ftp_data, ftp_printf);
595
}
596
 
597
int ftp_get_var(char *hostname,
598
                char *username,
599
                char *passwd,
600
                char *filename,
601
                ftp_write_t ftp_write,
602
                void *ftp_write_priv,
603
                ftp_printf_t ftp_printf)
604
{
605
 
606
  struct sockaddr local;
607
  char msgbuf[256];
608
  int s,data_s;
609
  int bytes;
610
  int ret;
611
 
612
  s = connect_to_server(hostname,&local,ftp_printf);
613
  if (s < 0) {
614
    return (s);
615
  }
616
 
617
  /* Read the welcome message from the server */
618
  if (get_reply(s,ftp_printf) != 2) {
619
    ftp_printf(0,"FTP: Server refused connection\n");
620
    close(s);
621
    return FTP_BAD;
622
  }
623
 
624
  ret = login(username,passwd,s,msgbuf,sizeof(msgbuf),ftp_printf);
625
  if (ret < 0) {
626
    close(s);
627
    return (ret);
628
  }
629
 
630
  /* We are now logged in and ready to transfer the file. Open the
631
     data socket ready to receive the file. It also build the PORT
632
     command ready to send */
633
  data_s = opendatasock(s,&local,msgbuf,sizeof(msgbuf),ftp_printf);
634
  if (data_s < 0) {
635
    close (s);
636
    return (data_s);
637
  }
638
 
639
  /* Ask for the file */
640
  ret = command("RETR",filename,s,msgbuf,sizeof(msgbuf),ftp_printf);
641
  if (ret < 0) {
642
    close(s);
643
    close(data_s);
644
    return (ret);
645
  }
646
 
647
  if (ret != 1) {
648
    ftp_printf(0,"FTP: RETR failed!\n");
649
    close (data_s);
650
    close(s);
651
    return (FTP_BADFILENAME);
652
  }
653
 
654
  if ((bytes=receive_file(data_s,ftp_write,ftp_write_priv,ftp_printf)) < 0) {
655
    ftp_printf(0,"FTP: Receiving file failed\n");
656
    close (data_s);
657
    close(s);
658
    return (bytes);
659
  }
660
 
661
  if (get_reply(s,ftp_printf) != 2) {
662
    ftp_printf(0,"FTP: Transfer failed!\n");
663
    close (data_s);
664
    close(s);
665
    return (FTP_BAD);
666
  }
667
 
668
  ret = quit(s,msgbuf,sizeof(msgbuf),ftp_printf);
669
  if (ret < 0) {
670
    close(s);
671
    close(data_s);
672
    return (ret);
673
  }
674
 
675
  close (data_s);
676
  close(s);
677
  return bytes;
678
}
679
 
680
/* Put a file on an FTP server. Hostname is the name/IP address of the
681
   server. username is the username used to connect to the server
682
   with. Passwd is the password used to authentificate the
683
   username. filename is the name of the file to receive. It should be
684
   the full pathname of the file. buf is a pointer to a buffer the
685
   contents of the file should be placed in and buf_size is the size
686
   of the buffer. ftp_printf is a function to be called to perform
687
   printing. On success 0 is returned. On error a negative value is
688
   returned indicating the type of error. */
689
 
690
int ftp_put(char * hostname,
691
            char * username,
692
            char * passwd,
693
            char * filename,
694
            char * buf,
695
            unsigned buf_size,
696
            ftp_printf_t ftp_printf)
697
{
698
    struct _ftp_data ftp_data;
699
 
700
    ftp_data.buf = buf;
701
    ftp_data.len = buf_size;
702
    return ftp_put_var(hostname, username, passwd, filename, _ftp_read, &ftp_data, ftp_printf);
703
}
704
 
705
int ftp_put_var(char *hostname,
706
                char *username,
707
                char *passwd,
708
                char *filename,
709
                ftp_read_t ftp_read,
710
                void *ftp_read_priv,
711
                ftp_printf_t ftp_printf)
712
{
713
 
714
  struct sockaddr local;
715
  char msgbuf[256];
716
  int s,data_s;
717
  int ret;
718
 
719
  s = connect_to_server(hostname,&local,ftp_printf);
720
  if (s < 0) {
721
    return (s);
722
  }
723
 
724
  /* Read the welcome message from the server */
725
  if (get_reply(s,ftp_printf) != 2) {
726
    ftp_printf(1,"FTP: Server refused connection\n");
727
    close(s);
728
    return FTP_BAD;
729
  }
730
 
731
  ret = login(username,passwd,s,msgbuf,sizeof(msgbuf),ftp_printf);
732
  if (ret < 0) {
733
    close(s);
734
    return (ret);
735
  }
736
 
737
  /* We are now logged in and ready to transfer the file. Open the
738
     data socket ready to receive the file. It also build the PORT
739
     command ready to send */
740
  data_s = opendatasock(s,&local,msgbuf,sizeof(msgbuf),ftp_printf);
741
  if (data_s < 0) {
742
    close (s);
743
    return (data_s);
744
  }
745
 
746
  /* Ask for the file */
747
  ret = command("STOR",filename,s,msgbuf,sizeof(msgbuf),ftp_printf);
748
  if (ret < 0) {
749
    close(s);
750
    close(data_s);
751
    return (ret);
752
  }
753
 
754
  if (ret != 1) {
755
    ftp_printf(1,"FTP: STOR failed!\n");
756
    close (data_s);
757
    close(s);
758
    return (FTP_BADFILENAME);
759
  }
760
 
761
  if ((ret = send_file(data_s,ftp_read,ftp_read_priv,ftp_printf)) < 0) {
762
    ftp_printf(1,"FTP: Sending file failed\n");
763
    close (data_s);
764
    close(s);
765
    return (ret);
766
  }
767
 
768
  if (get_reply(s,ftp_printf) != 2) {
769
    ftp_printf(1,"FTP: Transfer failed!\n");
770
    close (data_s);
771
    close(s);
772
    return (FTP_BAD);
773
  }
774
 
775
  ret = quit(s,msgbuf,sizeof(msgbuf),ftp_printf);
776
  if (ret < 0) {
777
    close(s);
778
    close(data_s);
779
    return (ret);
780
  }
781
 
782
  close (data_s);
783
  close(s);
784
  return 0;
785
}
786
 
787
/* An example ftp_printf function. This uses the standard eCos diag
788
output device for outputting error and diagnostic messages. The
789
function take one addition parameter to the normal printf function. The
790
first parameter indicates when the message is an error message when
791
true. This can be used to filter errors from diagnostic output. In
792
this example the error parameter is ignored and everything printed. */
793
 
794
void ftpclient_printf(unsigned error, const char *fmt, ...)
795
{
796
  va_list ap;
797
 
798
  va_start(ap, fmt);
799
  diag_vprintf( fmt, ap);
800
  va_end(ap);
801
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.