-
Notifications
You must be signed in to change notification settings - Fork 3
/
FTPSTING.C
1290 lines (1103 loc) · 26.3 KB
/
FTPSTING.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ftpsting.c: pftp interface to STinG
*
* Copyright (c) 2013, 2018 Roger Burrows
*
* This file is distributed under the GPL, version 2 or at your
* option any later version. See LICENSE.TXT for details.
*/
#include "ftp.h"
#include <stdio.h>
#include <limits.h>
/*
* Lattice C compatibility stuff
*/
#ifdef LATTICE
#define cdecl
#include <sys/types.h>
#include <basepage.h>
#define BASPAG BASEPAGE
#endif
#include <transprt.h>
/*
* buffer sizes: both must be <= SHRT_MAX
*/
#define IOBUFSIZE 32000 /* for ls/get/put */
#ifdef STIK1_COMPATIBLE
#define TCPBUFSIZE 8000 /* undocumented max for MagicNet's GlueSTiK (at least) */
#else
#define TCPBUFSIZE 32000 /* STinG allows this */
#endif
#define MAXCMDLEN 256 /* longest command to send to server */
/*
* range of ports to use per IANA
* *except*
* we don't use 65535 since that's reserved by STinG to
* indicate STinG-style extended addressing for TCP_open()
*/
#define FIRST_DYNAMIC_PORT 49152U
#define LAST_DYNAMIC_PORT 65534U
#define NUM_DYNAMIC_PORTS (LAST_DYNAMIC_PORT-FIRST_DYNAMIC_PORT+1)
#define HEADER_LEN 4 /* "NNN " or "NNN-" */
/* for 'tick' display */
#define XFER_QUANTUM (10 * 1024)
#define FORMAT_XFER_MSG "Bytes transferred = %ld\r"
#define BLANKOUT_XFER_MSG " \r"
/*
* globals
*/
WORD transfer_type = 'I'; /* note, image (binary) by default */
MLOCAL TPL *tpl = NULL;
MLOCAL WORD handle = -1;
MLOCAL WORD last_type_set = -1;
MLOCAL ULONG transfer_bytes; /* for measuring get/put */
MLOCAL ULONG transfer_ticks; /* transfer rates */
MLOCAL char cmdsave[80]; /* holds text of command actually sent */
MLOCAL char header[HEADER_LEN+1]; /* holds NNN prefix */
MLOCAL long reply_size = 0L; /* current size of following buffer */
MLOCAL char *reply; /* holds text of reply (including header(s)) */
MLOCAL char iobuf[IOBUFSIZE+1]; /* for file transfer */
/*
* function prototypes
*/
PRIVATE WORD abort_transfer(void);
PRIVATE char *expand_buffer(void);
PRIVATE int extract_hp(CAB *cab);
PRIVATE void display_transfer_stats(void);
PRIVATE WORD ftp_data_connect(void);
PRIVATE LONG ftp_directory(char *cmd,char *remotedir,char *localfile,BUFCTL *bufctl);
PRIVATE UWORD generate_port(void);
PRIVATE WORD get_one_line(WORD handle,char **replyptr,long maxlen);
PRIVATE WORD get_reply(WORD handle);
PRIVATE WORD open_connection(char *server,int port,ULONG *addr);
PRIVATE int prompt_and_reply(char *cmd,char *file);
PRIVATE WORD send_command(WORD handle,char *command);
PRIVATE WORD user_break(void);
PRIVATE WORD user_input(void);
PRIVATE void bufstore(BUFCTL *bufctl,char *name);
/*
* standard STinG initialisation
*/
int sting_init(void)
{
DRV_LIST *drivers;
if (!getcookie(STiK_COOKIE,(LONG *)&drivers))
return -4; /* no cookie */
if (!drivers)
return -3; /* shouldn't happen */
if (strcmp(MAGIC,drivers->magic))
return -2; /* corrupted ... */
if (!(tpl=(TPL *)get_dftab(TRANSPORT_DRIVER)))
return -1; /* no transport layer */
return 0;
}
/*
* Attempts to connect to specified server
* Returns: <0 error (standard STinG)
* 0 ok (control handle is in 'handle')
* >0 message from server is in reply[]
*/
WORD ftp_connect(char *server,WORD port)
{
int rc;
char buf[80];
char command[MAXCMDLEN];
/*
* allocate reply buffer if necessary
*/
if (!reply) {
reply = expand_buffer();
if (!reply)
return MEMORY_ERROR;
}
/*
* open connection
*/
handle = open_connection(server,port,&ip.addr);
if (handle < 0)
return handle;
rc = get_reply(handle);
if (rc == 120) {
cputs(reply);
rc = get_reply(handle);
}
if (rc != 220)
return rc;
cputs(reply);
/*
* prompt for name
*/
cprintf("Name (%d.%d.%d.%d): ",ip.quad[0],ip.quad[1],ip.quad[2],ip.quad[3]);
cgets(buf);
sprintf(command,"USER %s",buf);
rc = send_command(handle,command);
if (rc < 0)
return rc;
/*
* handle password prompt
*/
if (rc == 331) {
cputs(reply);
cputs("Password: ");
cgets_noecho(buf); /* do not display! */
sprintf(command,"PASS %s",buf);
rc = send_command(handle,command);
if (rc < 0)
return rc;
}
/*
* handle the rarely-seen account prompt
*/
if (rc == 332) {
cputs(reply);
cputs("Account: ");
cgets(buf);
sprintf(command,"ACCT %s",buf);
rc = send_command(handle,command);
if (rc < 0)
return rc;
}
return send_command(handle,"SYST");
}
/*
* Attempt to set up a data transfer connection
* Returns: <0 error (standard STinG or our own)
* >=0 handle
*/
PRIVATE WORD ftp_data_connect(void)
{
WORD rc;
CIB *cib;
IPADDR u;
CAB cab;
char command[MAXCMDLEN];
/*
* get our ip address
*/
cib = CNgetinfo(handle);
if (!cib)
return INTERNAL_ERROR;
/*
* for passive connections, need to query server to find
* out which of its ports to use before we can connect
*/
if (passive) {
rc = send_command(handle,"PASV");
message(rc);
if (rc != 227)
return NOMESSAGE_ERROR;
if (extract_hp(&cab) < 0) /* get server's ip addr & port */
return INTERNAL_ERROR;
#ifdef STIK1_COMPATIBLE
return TCP_open(cab.rhost,cab.rport,0,TCPBUFSIZE);
#else
cab.lhost = cib->address.lhost;
cab.lport = 0;
return TCP_open((uint32)&cab,TCP_ACTIVE,0,TCPBUFSIZE);
#endif
}
/*
* handle non-passive (active?) connections
*/
cab.lhost = cib->address.lhost; /* our ip address */
cab.lport = generate_port(); /* generate a port */
/*
* tell server which of our ports to use
*/
u.addr = cab.lhost;
sprintf(command,"PORT %d,%d,%d,%d,%d,%d",
u.quad[0],u.quad[1],u.quad[2],u.quad[3],cab.lport>>8,cab.lport&0xff);
if ((rc=send_command(handle,command)) < 0)
return rc;
message(rc);
#ifdef STIK1_COMPATIBLE
return TCP_open(0,cab.lport,0,TCPBUFSIZE);
#else
cab.rhost = cib->address.rhost;
cab.rport = 0;
return TCP_open((uint32)&cab,TCP_PASSIVE,0,TCPBUFSIZE);
#endif
}
/*
* Disconnect
*/
WORD ftp_disconnect(void)
{
WORD rc;
if (handle < 0)
return NOT_CONNECTED;
rc = TCP_close(handle,5,NULL);
handle = -1;
return rc;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* U S E R C O M M A N D H A N D L E R S *
* *
* Return: <0 error (standard STinG + our own) *
* =0 ok *
* >0 message from server is in reply[] *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
LONG ftp_bye(void)
{
if (handle < 0)
return NOT_CONNECTED;
return send_command(handle,"QUIT");
}
LONG ftp_cdup(void)
{
if (handle < 0)
return NOT_CONNECTED;
return send_command(handle,"CDUP");
}
LONG ftp_cwd(char *path)
{
char command[MAXCMDLEN];
if (handle < 0)
return NOT_CONNECTED;
sprintf(command,"CWD %s",path);
return send_command(handle,command);
}
LONG ftp_delete(char *remotefile,int multiple)
{
char command[MAXCMDLEN];
if (handle < 0)
return NOT_CONNECTED;
/*
* if this is an mdelete(), handle prompt
*/
if (multiple) {
switch(prompt_and_reply("mdelete",remotefile)) {
case -1:
return USER_INTERRUPT;
case 0:
return 0L;
}
}
sprintf(command,"DELE %s",remotefile);
return send_command(handle,command);
}
LONG ftp_dir(char *remotedir,char *localfile)
{
return ftp_directory("LIST",remotedir,localfile,NULL);
}
LONG ftp_get(char *localfile,char *remotefile,int multiple)
{
WORD data; /* data port handle */
WORD fh; /* file handle */
WORD n, rc;
LONG rc2;
ULONG start, prev_bytes;
char command[MAXCMDLEN];
if (handle < 0)
return NOT_CONNECTED;
/*
* if this is an mget(), handle prompt
*/
if (multiple) {
switch(prompt_and_reply("mget",remotefile)) {
case -1:
return USER_INTERRUPT;
case 0:
return 0L;
}
} else cprintf("local: %s remote: %s\r\n",localfile,remotefile);
/*
* make sure the type is correct
*/
if (last_type_set != transfer_type) {
rc = (WORD)ftp_type(transfer_type);
if (rc != 200)
return rc;
message(rc);
}
/*
* establish a data connection
*/
data = ftp_data_connect();
if (data < 0)
return data;
/*
* tell server we want to retrieve a file
*/
sprintf(command,"RETR %s",remotefile);
rc = send_command(handle,command);
/*
* see if that's ok
*/
if ((rc != 125) && (rc != 150)) {
TCP_close(data,5,NULL);
return rc;
}
message(rc);
start = clock(); /* start timing */
prev_bytes = transfer_bytes = 0UL;
/*
* copy file across network
*/
rc2 = Fcreate(localfile,0);
if (rc2 >= 0L) {
fh = (WORD)rc2;
while(1) {
if (constat()) {
if (user_break()) {
rc = abort_transfer();
break;
}
}
rc = n = CNbyte_count(data);
if (rc == 0)
continue;
if (rc < 0)
break;
if (n > IOBUFSIZE)
n = IOBUFSIZE;
rc = CNget_block(data,iobuf,n);
if (rc != n) {
if (rc >= 0)
rc = INTERNAL_ERROR;
break;
}
rc = (WORD)Fwrite(fh,n,iobuf);
if (rc != n) {
rc = FILE_WRITE_ERROR;
break;
}
transfer_bytes += n;
if (tick && (transfer_bytes-prev_bytes > XFER_QUANTUM)) {
cprintf(FORMAT_XFER_MSG,transfer_bytes);
prev_bytes = transfer_bytes;
}
}
Fclose(fh);
if (tick)
cputs(BLANKOUT_XFER_MSG);
if (rc == E_EOF)
rc = 0;
}
n = TCP_close(data,5,NULL);
if (rc == 0)
rc = n;
transfer_ticks = clock() - start;
if (rc == 0)
rc = get_reply(handle);
message(rc); /* print server msg before timing */
if (verbose && ((rc == 226) || (rc == 250)))
display_transfer_stats();
if (bell)
ring_bell();
return 0L;
}
LONG ftp_mkdir(char *remotedir)
{
char command[MAXCMDLEN];
if (handle < 0)
return NOT_CONNECTED;
sprintf(command,"MKD %s",remotedir);
return send_command(handle,command);
}
/*
* get matching files for mdelete()/mget()
*/
LONG ftp_matching(BUFCTL *bufctl,char *remotefile)
{
if (handle < 0)
return NOT_CONNECTED;
bufctl->count = 0;
bufctl->size = INITIAL_BUFCTL_SIZE;
bufctl->start = malloc(bufctl->size);
bufctl->pos = bufctl->start;
if (!bufctl->start)
return MEMORY_ERROR;
return ftp_directory("NLST",remotefile,NULL,bufctl);
}
LONG ftp_nlist(char *remotedir,char *localfile)
{
return ftp_directory("NLST",remotedir,localfile,NULL);
}
LONG ftp_put(char *localfile,char *remotefile,int multiple)
{
WORD data; /* data port handle */
WORD fh; /* file handle */
WORD rc, n, remaining;
LONG rc2;
char *p;
ULONG start, prev_bytes;
char command[MAXCMDLEN];
if (handle < 0)
return NOT_CONNECTED;
/*
* if this is an mput(), handle prompt
*/
if (multiple) {
switch(prompt_and_reply("mput",localfile)) {
case -1:
return USER_INTERRUPT;
case 0:
return 0L;
}
} else cprintf("local: %s remote: %s\r\n",localfile,remotefile);
/*
* make sure the type is correct
*/
if (last_type_set != transfer_type) {
rc = (WORD) ftp_type(transfer_type);
if (rc != 200)
return rc;
message(rc);
}
/*
* establish a data connection
*/
data = ftp_data_connect();
if (data < 0)
return data;
/*
* tell server we want to store a file
*/
sprintf(command,"STOR %s",remotefile);
rc = send_command(handle,command);
/*
* see if that's ok
*/
if ((rc != 125) && (rc != 150)) {
TCP_close(data,5,NULL);
return rc;
}
message(rc);
#ifdef STIK1_COMPATIBLE
if (debug > 1) {
rc = CNbyte_count(handle);
if (rc < 0)
cprintf("===> Unexpected TCP state %d\r\n",rc);
}
#else
if (debug > 1) {
TCPIB tcpinfo;
tcpinfo.request = TCPI_state;
TCP_info(data,&tcpinfo);
if (tcpinfo.state != TESTABLISH)
cprintf("===> Unexpected TCP state %d\r\n",tcpinfo.state);
}
#endif
start = clock(); /* start timing */
prev_bytes = transfer_bytes = 0UL;
/*
* copy file across network
*/
rc2 = Fopen(localfile,0);
if (rc2 >= 0L) {
fh = (WORD)rc2;
while(1) {
rc2 = Fread(fh,IOBUFSIZE,iobuf);
if (rc2 < 0L) {
rc = FILE_READ_ERROR;
break;
}
rc = (WORD)rc2;
if (rc == 0)
break;
remaining = rc;
for (p = iobuf; (remaining > 0) && (rc >= 0); remaining -= n, p += n) {
n = min(remaining,TCPBUFSIZE);
do {
rc = TCP_send(data,p,n);
if (constat())
if (user_break())
rc = abort_transfer();
} while (rc == E_OBUFFULL);
transfer_bytes += n;
if (tick && (transfer_bytes-prev_bytes > XFER_QUANTUM)) {
cprintf(FORMAT_XFER_MSG,transfer_bytes);
prev_bytes = transfer_bytes;
}
}
}
Fclose(fh);
if (tick)
cputs(BLANKOUT_XFER_MSG);
}
n = TCP_close(data,5,NULL);
if (rc == 0)
rc = n;
transfer_ticks = clock() - start;
if (rc == 0)
rc = get_reply(handle);
message(rc); /* print server msg before timing */
if (verbose && ((rc == 226) || (rc == 250)))
display_transfer_stats();
if (bell)
ring_bell();
return 0L;
}
LONG ftp_pwd(void)
{
if (handle < 0)
return NOT_CONNECTED;
return send_command(handle,"PWD");
}
LONG ftp_rmdir(char *remotedir)
{
char command[MAXCMDLEN];
if (handle < 0)
return NOT_CONNECTED;
sprintf(command,"RMD %s",remotedir);
return send_command(handle,command);
}
LONG ftp_rename(char *oldname,char *newname)
{
char command[MAXCMDLEN];
LONG rc;
if (handle < 0)
return NOT_CONNECTED;
sprintf(command,"RNFR %s",oldname);
rc = send_command(handle,command);
if (rc != 350)
return rc;
message(rc);
sprintf(command,"RNTO %s",newname);
return send_command(handle,command);
}
LONG ftp_system(void)
{
if (handle < 0)
return NOT_CONNECTED;
return send_command(handle,"SYST");
}
/*
* because certain commands require a specific transfer type, we need
* to make sure that we track type carefully. this is what we do:
* 1) [in ftpint.c] run_ascii()/run_binary()/run_type() call ftp_type()
* and, if successful, set 'transfer_type'
* 2) ftp_type() sends the TYPE command and, if successful, remembers
* its value in 'last_type_set'
* 3) ftp_dir() sets the type to ascii iff 'last_type_set' is not 'A'
* 4) ftp_get()/ftp_put() set the type to 'transfer_type' iff
* 'last_type_set' is not the same as 'transfer_type'
*/
LONG ftp_type(int type)
{
WORD rc;
char command[MAXCMDLEN];
if (handle < 0)
return NOT_CONNECTED;
if (type < 0) {
cprintf("Using %s mode to transfer files\r\n",
(transfer_type=='A')?"ascii":"binary");
return 0L;
}
sprintf(command,"TYPE %c",type);
rc = send_command(handle,command);
if (rc >= 0)
last_type_set = type;
return rc;
}
/* * * * * * * * * * * * * * * * * * * * * *
* *
* I N T E R N A L F U N C T I O N S *
* *
* * * * * * * * * * * * * * * * * * * * * */
/*
* Resolves server into an IP address, then opens a connection to port
* of server.
* Returns: >0 connection handle
* <0 error code
* Updates 'addr' with resolved address (if resolve() worked :-))
*/
PRIVATE WORD open_connection(char *server,int port,ULONG *addr)
{
WORD rc, handle;
CAB cab;
*addr = 0UL;
rc = resolve(server,(char **)NULL,addr,1);
if (rc < 0)
return rc;
cab.rhost = *addr;
cab.rport = port;
#ifdef STIK1_COMPATIBLE
handle = TCP_open(cab.rhost,cab.rport,0,TCPBUFSIZE);
#else
cab.lhost = 0;
cab.lport = 0;
handle = TCP_open((uint32)&cab,TCP_ACTIVE,0,TCPBUFSIZE);
#endif
if (handle < 0)
return handle;
rc = TCP_wait_state(handle,TESTABLISH,10); //STiK-style for now FIXME
if (rc < 0) {
TCP_close(handle,5,NULL);
return rc;
}
return handle;
}
/*
* Expands reply buffer dynamically
* Returns: pointer to first available place in new buffer
* NULL means expansion failed
*/
PRIVATE char *expand_buffer(void)
{
char *new;
long new_size;
new_size = reply_size + REPLY_SIZE_INCR;
new = malloc(new_size);
if (new) {
new[0] = '\0';
if (reply) { /* copy then free old buffer if it exists */
strcpy(new,reply);
free(reply);
}
reply = NULL;
reply_size = 0L;
}
if (!new) /* couldn't allocate new buffer */
return NULL; /* (the old one remains allocated) */
reply = new; /* set new buffer values */
reply_size = new_size;
if (debug)
cprintf("***> reply buffer now %ld bytes\r\n",reply_size);
return reply + strlen(reply);
}
/*
* Gets one line of reply from FTP server
* Returns: <0 error code (standard STinG, or our own)
* 0 last line
* >0 not last line
*/
PRIVATE WORD get_one_line(WORD handle,char **replyptr,long maxlen)
{
WORD rc;
char *p = *replyptr;
/*
* wait until header is available, then read it
*/
while(1) {
if (CNbyte_count(handle) >= HEADER_LEN)
break;
if (constat())
if (user_break())
return USER_INTERRUPT;
}
rc = CNget_block(handle,header,HEADER_LEN);
if (rc != HEADER_LEN)
return (rc < 0) ? rc : INTERNAL_ERROR;
header[HEADER_LEN] = '\0';
/*
* copy header into reply[], so user will see it
*/
strcpy(p,header);
p += HEADER_LEN;
/*
* read until end of line
*
* NOTE: if all the reply lines won't fit in reply[],
* we attempt to expand it dynamically
*/
while(1) {
if (maxlen > SHRT_MAX)
maxlen = SHRT_MAX;
/*
* since we insert a newline ourselves below, we must
* leave at least one byte free in the buffer!
*/
rc = CNgets(handle,p,maxlen-1,'\n');
if (rc == E_BIGBUF) { /* out of room */
p = expand_buffer(); /* so expand buffer */
if (p) {
maxlen = reply + reply_size - p;
continue; /* retry CNgets() */
}
return rc; /* buffer expansion failed */
}
if (rc != E_NODATA)
break;
if (constat())
if (user_break())
return USER_INTERRUPT;
}
if (rc >= 0) {
p += rc;
*p++ = '\n';
*p = '\0';
}
*replyptr = p;
/* set return code according to header indicator */
if (rc >= 0)
rc = (header[HEADER_LEN-1] == ' ') ? 0 : 1;
return rc;
}
/*
* Gets reply from FTP server
* Returns: <0 error (standard STinG, or our own)
* else reply code
*
* Handles multiline replies
*/
PRIVATE WORD get_reply(WORD handle)
{
WORD rc;
char *p = reply;
while(1) {
rc = get_one_line(handle,&p,reply+reply_size-p);
if (rc < 0)
break;
if (rc == 0) {
rc = atoi(header);
break;
}
}
return rc;
}
/*
* Sends command to FTP server & gets reply
* Returns: <0 error (standard STinG, or our own)
* else reply code
*/
PRIVATE WORD send_command(WORD handle,char *command)
{
WORD n, rc = 0;
char cmd[5], *p;
if (debug) {
strncpy(cmd,command,4);
cmd[4] = '\0';
if (strequal(cmd,"pass"))
cprintf("---> %s XXXX\r\n",cmd);
else cprintf("---> %s\r\n",command);
}
strcpy(cmdsave,command);
n = (WORD)strlen(cmdsave);
p = cmdsave + n;
*p++ = '\r';
*p++ = '\n';
*p = '\0';
rc = TCP_send(handle,cmdsave,n+2);
if (rc < 0)
return rc;
return get_reply(handle);
}
/*
* generate random port within dynamic range
*/
PRIVATE UWORD generate_port(void)
{
return (UWORD)(FIRST_DYNAMIC_PORT + (lrand48() % NUM_DYNAMIC_PORTS));
}
/*
* this is multi-purpose:
* 1. if localfile is not NULL
* if the file can be opened, output is directed there;
* otherwise output goes to the console
* 2. if localfile *is* NULL
* if bufctl is NULL, output goes to the console;
* otherwise, output is stored in the buffer specified by
* bufctl (and the buffer is automagically expanded)
*/
PRIVATE LONG ftp_directory(char *cmd,char *remotedir,char *localfile,BUFCTL *bufctl)
{
WORD data; /* data port handle */
LONG fh = -1L; /* file handle */
WORD n, rc;
char command[MAXCMDLEN];
if (handle < 0)
return NOT_CONNECTED;
/*
* make sure the type is ascii
*/
if (last_type_set != 'A') {
rc = (WORD)ftp_type('A');
if (rc != 200)
return rc;
message(rc);
}
/*
* establish a data connection
*/
data = ftp_data_connect();
if (data < 0)
return data;
/*
* tell server we want a dir list
*/
if (remotedir)
sprintf(command,"%s %s",cmd,remotedir);
else strcpy(command,cmd);
rc = send_command(handle,command);
/*
* see if that's ok
*/
if ((rc != 125) && (rc != 150)) {
TCP_close(data,5,NULL);
return rc;
}
message(rc);
/*
* if local file specified for dir copy, open it.
* if open fails, treat as if not specified
*/
if (localfile)
fh = Fcreate(localfile,0);
/*
* copy dir list across network
*/
while(1) {
if (constat()) {
rc = ((fh >= 0) || bufctl) ? user_break() : user_input();
if (rc) {
rc = abort_transfer();
break;
}