forked from mattiasgustavsson/libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftplib.h
1637 lines (1546 loc) · 36.5 KB
/
ftplib.h
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
/*
ftplib.h - FTP client lib for C/C++.
Do this:
#define FTPLIB_IMPLEMENTATION
before you include this file in *one* C/C++ file to create the implementation.
------------------------------------------------------------------------------
This is simply a merged single-header version of the original ftplib lib.
The original license text can be found below, and I place my changes under the
same license.
/ Mattias Gustavsson ( mattias@mattiasgustavsson.com )
*/
/***************************************************************************/
/* */
/* ftplib.h - header file for callable ftp access routines */
/* Copyright (C) 1996-2001, 2013, 2016 Thomas Pfau, tfpfau@gmail.com */
/* 1407 Thomas Ave, North Brunswick, NJ, 08902 */
/* */
/* This library is free software. You can redistribute it and/or */
/* modify it under the terms of the Artistic License 2.0. */
/* */
/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* Artistic License 2.0 for more details. */
/* */
/* See the file LICENSE or */
/* http://www.perlfoundation.org/artistic_license_2_0 */
/* */
/***************************************************************************/
#if !defined(__FTPLIB_H)
#define __FTPLIB_H
// MG: removed as it is no longer built as a library
/*
#if defined(__unix__) || defined(VMS)
#define GLOBALDEF
#define GLOBALREF extern
#elif defined(_WIN32)
#if defined BUILDING_LIBRARY
#define GLOBALDEF __declspec(dllexport)
#define GLOBALREF __declspec(dllexport)
#else
#define GLOBALREF __declspec(dllimport)
#endif
#endif
*/
#define GLOBALDEF
#define GLOBALREF
#pragma warning( push )
#pragma warning( disable: 4668 )
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <limits.h>
#include <inttypes.h>
#pragma warning( pop )
/* FtpAccess() type codes */
#define FTPLIB_DIR 1
#define FTPLIB_DIR_VERBOSE 2
#define FTPLIB_FILE_READ 3
#define FTPLIB_FILE_WRITE 4
/* FtpAccess() mode codes */
#define FTPLIB_ASCII 'A'
#define FTPLIB_IMAGE 'I'
#define FTPLIB_TEXT FTPLIB_ASCII
#define FTPLIB_BINARY FTPLIB_IMAGE
/* connection modes */
#define FTPLIB_PASSIVE 1
#define FTPLIB_PORT 2
/* connection option names */
#define FTPLIB_CONNMODE 1
#define FTPLIB_CALLBACK 2
#define FTPLIB_IDLETIME 3
#define FTPLIB_CALLBACKARG 4
#define FTPLIB_CALLBACKBYTES 5
#ifdef __cplusplus
extern "C" {
#endif
#if defined(__UINT64_MAX)
typedef uint64_t fsz_t;
#else
typedef uint32_t fsz_t;
#endif
typedef struct NetBuf netbuf;
typedef int (*FtpCallback)(netbuf *nControl, fsz_t xfered, void *arg);
typedef struct FtpCallbackOptions {
FtpCallback cbFunc; /* function to call */
void *cbArg; /* argument to pass to function */
unsigned int bytesXferred; /* callback if this number of bytes transferred */
unsigned int idleTime; /* callback if this many milliseconds have elapsed */
} FtpCallbackOptions;
//GLOBALREF int ftplib_debug;
GLOBALREF void FtpInit(void);
GLOBALREF char *FtpLastResponse(netbuf *nControl);
GLOBALREF int FtpConnect(const char *host, netbuf **nControl);
GLOBALREF int FtpOptions(int opt, long val, netbuf *nControl);
GLOBALREF int FtpSetCallback(const FtpCallbackOptions *opt, netbuf *nControl);
GLOBALREF int FtpClearCallback(netbuf *nControl);
GLOBALREF int FtpLogin(const char *user, const char *pass, netbuf *nControl);
GLOBALREF int FtpAccess(const char *path, int typ, int mode, netbuf *nControl,
netbuf **nData);
GLOBALREF int FtpRead(void *buf, int max, netbuf *nData);
GLOBALREF int FtpWrite(const void *buf, int len, netbuf *nData);
GLOBALREF int FtpClose(netbuf *nData);
GLOBALREF int FtpSite(const char *cmd, netbuf *nControl);
GLOBALREF int FtpSysType(char *buf, int max, netbuf *nControl);
GLOBALREF int FtpMkdir(const char *path, netbuf *nControl);
GLOBALREF int FtpChdir(const char *path, netbuf *nControl);
GLOBALREF int FtpCDUp(netbuf *nControl);
GLOBALREF int FtpRmdir(const char *path, netbuf *nControl);
GLOBALREF int FtpPwd(char *path, int max, netbuf *nControl);
GLOBALREF int FtpNlst(const char *output, const char *path, netbuf *nControl);
GLOBALREF int FtpDir(const char *output, const char *path, netbuf *nControl);
GLOBALREF int FtpSize(const char *path, unsigned int *size, char mode, netbuf *nControl);
#if defined(__UINT64_MAX)
GLOBALREF int FtpSizeLong(const char *path, fsz_t *size, char mode, netbuf *nControl);
#endif
GLOBALREF int FtpModDate(const char *path, char *dt, int max, netbuf *nControl);
GLOBALREF int FtpGet(const char *output, const char *path, char mode,
netbuf *nControl);
GLOBALREF int FtpPut(const char *input, const char *path, char mode,
netbuf *nControl);
GLOBALREF int FtpRename(const char *src, const char *dst, netbuf *nControl);
GLOBALREF int FtpDelete(const char *fnm, netbuf *nControl);
GLOBALREF void FtpQuit(netbuf *nControl);
#ifdef __cplusplus
};
#endif
#endif /* __FTPLIB_H */
#ifdef FTPLIB_IMPLEMENTATION
#undef FTPLIB_IMPLEMENTATION
// MG: FIXUP TO GET THINGS TO COMPILE
#pragma comment (lib, "Ws2_32.lib")
#pragma warning( push )
#pragma warning( disable: 4242 )
#pragma warning( disable: 4244 )
#pragma warning( disable: 4267 )
#pragma warning( disable: 4312 )
#pragma warning( disable: 4365 )
#pragma warning( disable: 4388 )
#pragma warning( disable: 4548 )
#pragma warning( disable: 4668 )
#pragma warning( disable: 4701 )
#pragma warning( disable: 4706 )
#pragma warning( disable: 4996 )
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
const char* hstrerror(int err) { static char str[ 16 ]; itoa( err, str, 10 ); return str; }
/***************************************************************************/
/* */
/* ftplib.c - callable ftp access routines */
/* Copyright (C) 1996-2001, 2013, 2016 Thomas Pfau, tfpfau@gmail.com */
/* 1407 Thomas Ave, North Brunswick, NJ, 08902 */
/* */
/* This library is free software. You can redistribute it and/or */
/* modify it under the terms of the Artistic License 2.0. */
/* */
/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* Artistic License 2.0 for more details. */
/* */
/* See the file LICENSE or */
/* http://www.perlfoundation.org/artistic_license_2_0 */
/* */
/***************************************************************************/
#if defined(__unix__) || defined(__VMS)
#include <unistd.h>
#endif
#if defined(_WIN32)
#define _WINSOCKAPI_
#pragma warning( push )
#pragma warning( disable: 4255 )
#include <windows.h>
#include <winsock.h>
#pragma warning( pop )
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#if defined(__unix__)
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#elif defined(VMS)
#include <types.h>
#include <socket.h>
#include <in.h>
#include <netdb.h>
#include <inet.h>
#elif defined(_WIN32)
#include <winsock2.h>
#endif
#if defined(__APPLE__)
#undef _REENTRANT
#endif
#define BUILDING_LIBRARY
// #include "ftplib.h" /* MG: merge to single header lib */
#if defined(__UINT64_MAX) && !defined(PRIu64)
#if ULONG_MAX == __UINT32_MAX
#define PRIu64 "llu"
#else
#define PRIu64 "lu"
#endif
#endif
#if defined(_WIN32)
#define SETSOCKOPT_OPTVAL_TYPE (const char *)
#else
#define SETSOCKOPT_OPTVAL_TYPE (void *)
#endif
#define FTPLIB_BUFSIZ 8192
#define RESPONSE_BUFSIZ 1024
#define TMP_BUFSIZ 1024
#define ACCEPT_TIMEOUT 30
#define FTPLIB_CONTROL 0
#define FTPLIB_READ 1
#define FTPLIB_WRITE 2
#if !defined FTPLIB_DEFMODE
#define FTPLIB_DEFMODE FTPLIB_PASSIVE
#endif
struct NetBuf {
char *cput,*cget;
int handle;
int cavail,cleft;
char *buf;
int dir;
netbuf *ctrl;
netbuf *data;
int cmode;
struct timeval idletime;
FtpCallback idlecb;
void *idlearg;
unsigned long int xfered;
unsigned long int cbbytes;
unsigned long int xfered1;
char response[RESPONSE_BUFSIZ];
};
static char *version =
"ftplib Release 4.0 07-Jun-2013, copyright 1996-2003, 2013 Thomas Pfau";
GLOBALDEF int ftplib_debug = 0;
#if defined(__unix__) || defined(VMS)
int net_read(int fd, char *buf, size_t len)
{
while ( 1 )
{
int c = read(fd, buf, len);
if ( c == -1 )
{
if ( errno != EINTR && errno != EAGAIN )
return -1;
}
else
{
return c;
}
}
}
int net_write(int fd, const char *buf, size_t len)
{
int done = 0;
while ( len > 0 )
{
int c = write( fd, buf, len );
if ( c == -1 )
{
if ( errno != EINTR && errno != EAGAIN )
return -1;
}
else if ( c == 0 )
{
return done;
}
else
{
buf += c;
done += c;
len -= c;
}
}
return done;
}
#define net_close close
#elif defined(_WIN32)
#define net_read(x,y,z) recv(x,y,z,0)
#define net_write(x,y,z) send(x,y,z,0)
#define net_close closesocket
#endif
#if defined(NEED_MEMCCPY)
/*
* VAX C does not supply a memccpy routine so I provide my own
*/
void *memccpy(void *dest, const void *src, int c, size_t n)
{
int i=0;
const unsigned char *ip=src;
unsigned char *op=dest;
while (i < n)
{
if ((*op++ = *ip++) == c)
break;
i++;
}
if (i == n)
return NULL;
return op;
}
#endif
#if defined(NEED_STRDUP)
/*
* strdup - return a malloc'ed copy of a string
*/
char *strdup(const char *src)
{
int l = strlen(src) + 1;
char *dst = malloc(l);
if (dst)
strcpy(dst,src);
return dst;
}
#endif
/*
* socket_wait - wait for socket to receive or flush data
*
* return 1 if no user callback, otherwise, return value returned by
* user callback
*/
static int socket_wait(netbuf *ctl)
{
fd_set fd,*rfd = NULL,*wfd = NULL;
struct timeval tv;
int rv = 0;
if ((ctl->dir == FTPLIB_CONTROL) || (ctl->idlecb == NULL))
return 1;
if (ctl->dir == FTPLIB_WRITE)
wfd = &fd;
else
rfd = &fd;
FD_ZERO(&fd);
do
{
FD_SET((unsigned int)ctl->handle,&fd);
tv = ctl->idletime;
rv = select(ctl->handle+1, rfd, wfd, NULL, &tv);
if (rv == -1)
{
rv = 0;
strncpy(ctl->ctrl->response, strerror(errno),
sizeof(ctl->ctrl->response));
break;
}
else if (rv > 0)
{
rv = 1;
break;
}
}
while ((rv = ctl->idlecb(ctl, ctl->xfered, ctl->idlearg)));
return rv;
}
/*
* read a line of text
*
* return -1 on error or bytecount
*/
static int readline(char *buf,int max,netbuf *ctl)
{
int x,retval = 0;
char *end,*bp=buf;
int eof = 0;
if ((ctl->dir != FTPLIB_CONTROL) && (ctl->dir != FTPLIB_READ))
return -1;
if (max == 0)
return 0;
do
{
if (ctl->cavail > 0)
{
x = (max >= ctl->cavail) ? ctl->cavail : max-1;
end = (char*) memccpy(bp,ctl->cget,'\n',x);
if (end != NULL)
x = end - bp;
retval += x;
bp += x;
*bp = '\0';
max -= x;
ctl->cget += x;
ctl->cavail -= x;
if (end != NULL)
{
bp -= 2;
if (strcmp(bp,"\r\n") == 0)
{
*bp++ = '\n';
*bp++ = '\0';
--retval;
}
break;
}
}
if (max == 1)
{
*buf = '\0';
break;
}
if (ctl->cput == ctl->cget)
{
ctl->cput = ctl->cget = ctl->buf;
ctl->cavail = 0;
ctl->cleft = FTPLIB_BUFSIZ;
}
if (eof)
{
if (retval == 0)
retval = -1;
break;
}
if (!socket_wait(ctl))
return retval;
if ((x = net_read(ctl->handle,ctl->cput,ctl->cleft)) == -1)
{
if (ftplib_debug)
perror("read");
retval = -1;
break;
}
if (x == 0)
eof = 1;
ctl->cleft -= x;
ctl->cavail += x;
ctl->cput += x;
}
while (1);
return retval;
}
/*
* write lines of text
*
* return -1 on error or bytecount
*/
static int writeline(const char *buf, int len, netbuf *nData)
{
int x, nb=0, w;
const char *ubp = buf;
char *nbp;
char lc=0;
if (nData->dir != FTPLIB_WRITE)
return -1;
nbp = nData->buf;
for (x=0; x < len; x++)
{
if ((*ubp == '\n') && (lc != '\r'))
{
if (nb == FTPLIB_BUFSIZ)
{
if (!socket_wait(nData))
return x;
w = net_write(nData->handle, nbp, FTPLIB_BUFSIZ);
if (w != FTPLIB_BUFSIZ)
{
if (ftplib_debug)
printf("net_write(1) returned %d, errno = %d\n", w, errno);
return(-1);
}
nb = 0;
}
nbp[nb++] = '\r';
}
if (nb == FTPLIB_BUFSIZ)
{
if (!socket_wait(nData))
return x;
w = net_write(nData->handle, nbp, FTPLIB_BUFSIZ);
if (w != FTPLIB_BUFSIZ)
{
if (ftplib_debug)
printf("net_write(2) returned %d, errno = %d\n", w, errno);
return(-1);
}
nb = 0;
}
nbp[nb++] = lc = *ubp++;
}
if (nb)
{
if (!socket_wait(nData))
return x;
w = net_write(nData->handle, nbp, nb);
if (w != nb)
{
if (ftplib_debug)
printf("net_write(3) returned %d, errno = %d\n", w, errno);
return(-1);
}
}
return len;
}
/*
* read a response from the server
*
* return 0 if first char doesn't match
* return 1 if first char matches
*/
static int readresp(char c, netbuf *nControl)
{
char match[5];
if (readline(nControl->response,RESPONSE_BUFSIZ,nControl) == -1)
{
if (ftplib_debug)
perror("Control socket read failed");
return 0;
}
if (ftplib_debug > 1)
fprintf(stderr,"%s",nControl->response);
if (nControl->response[3] == '-')
{
strncpy(match,nControl->response,3);
match[3] = ' ';
match[4] = '\0';
do
{
if (readline(nControl->response,RESPONSE_BUFSIZ,nControl) == -1)
{
if (ftplib_debug)
perror("Control socket read failed");
return 0;
}
if (ftplib_debug > 1)
fprintf(stderr,"%s",nControl->response);
}
while (strncmp(nControl->response,match,4));
}
if (nControl->response[0] == c)
return 1;
return 0;
}
/*
* FtpInit for stupid operating systems that require it (Windows NT)
*/
GLOBALDEF void FtpInit(void)
{
#if defined(_WIN32)
WORD wVersionRequested;
WSADATA wsadata;
int err;
wVersionRequested = MAKEWORD(1,1);
if ((err = WSAStartup(wVersionRequested,&wsadata)) != 0)
fprintf(stderr,"Network failed to start: %d\n",err);
#endif
}
/*
* FtpLastResponse - return a pointer to the last response received
*/
GLOBALDEF char *FtpLastResponse(netbuf *nControl)
{
if ((nControl) && (nControl->dir == FTPLIB_CONTROL))
return nControl->response;
return NULL;
}
/*
* FtpConnect - connect to remote server
*
* return 1 if connected, 0 if not
*/
GLOBALDEF int FtpConnect(const char *host, netbuf **nControl)
{
int sControl;
struct sockaddr_in sin;
int on=1;
netbuf *ctrl;
char *lhost;
char *pnum;
memset(&sin,0,sizeof(sin));
sin.sin_family = AF_INET;
lhost = strdup(host);
pnum = strchr(lhost,':');
if (pnum == NULL)
pnum = "ftp";
else
*pnum++ = '\0';
if (isdigit(*pnum))
sin.sin_port = htons(atoi(pnum));
else
{
struct servent *pse;
#if _REENTRANT
struct servent se;
char tmpbuf[TMP_BUFSIZ];
int i;
if ( ( i = getservbyname_r(pnum,"tcp",&se,tmpbuf,TMP_BUFSIZ,&pse) ) != 0 )
{
errno = i;
if ( ftplib_debug )
perror("getservbyname_r");
free(lhost);
return 0;
}
#else
if ((pse = getservbyname(pnum,"tcp") ) == NULL )
{
if ( ftplib_debug )
perror("getservbyname");
free(lhost);
return 0;
}
#endif
sin.sin_port = pse->s_port;
}
if ((sin.sin_addr.s_addr = inet_addr(lhost)) == INADDR_NONE)
{
struct hostent *phe;
#ifdef _REENTRANT
struct hostent he;
char tmpbuf[TMP_BUFSIZ];
int i, herr;
if ( ( ( i = gethostbyname_r( lhost, &he, tmpbuf, TMP_BUFSIZ, &phe, &herr ) ) != 0 ) ||
( phe == NULL ) )
{
if ( ftplib_debug )
fprintf(stderr, "gethostbyname: %s\n", hstrerror(herr));
free(lhost);
return 0;
}
#else
if ((phe = gethostbyname(lhost)) == NULL)
{
if (ftplib_debug)
fprintf(stderr, "gethostbyname: %s\n", hstrerror(h_errno));
free(lhost);
return 0;
}
#endif
memcpy((char *)&sin.sin_addr, phe->h_addr, phe->h_length);
}
free(lhost);
sControl = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sControl == -1)
{
if (ftplib_debug)
perror("socket");
return 0;
}
if (setsockopt(sControl,SOL_SOCKET,SO_REUSEADDR,
SETSOCKOPT_OPTVAL_TYPE &on, sizeof(on)) == -1)
{
if (ftplib_debug)
perror("setsockopt");
net_close(sControl);
return 0;
}
if (connect(sControl, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
if (ftplib_debug)
perror("connect");
net_close(sControl);
return 0;
}
ctrl = (netbuf*) calloc(1,sizeof(netbuf));
if (ctrl == NULL)
{
if (ftplib_debug)
perror("calloc");
net_close(sControl);
return 0;
}
ctrl->buf = (char*)malloc(FTPLIB_BUFSIZ);
if (ctrl->buf == NULL)
{
if (ftplib_debug)
perror("calloc");
net_close(sControl);
free(ctrl);
return 0;
}
ctrl->handle = sControl;
ctrl->dir = FTPLIB_CONTROL;
ctrl->ctrl = NULL;
ctrl->data = NULL;
ctrl->cmode = FTPLIB_DEFMODE;
ctrl->idlecb = NULL;
ctrl->idletime.tv_sec = ctrl->idletime.tv_usec = 0;
ctrl->idlearg = NULL;
ctrl->xfered = 0;
ctrl->xfered1 = 0;
ctrl->cbbytes = 0;
if (readresp('2', ctrl) == 0)
{
net_close(sControl);
free(ctrl->buf);
free(ctrl);
return 0;
}
*nControl = ctrl;
return 1;
}
GLOBALDEF int FtpSetCallback(const FtpCallbackOptions *opt, netbuf *nControl)
{
nControl->idlecb = opt->cbFunc;
nControl->idlearg = opt->cbArg;
nControl->idletime.tv_sec = opt->idleTime / 1000;
nControl->idletime.tv_usec = (opt->idleTime % 1000) * 1000;
nControl->cbbytes = opt->bytesXferred;
return 1;
}
GLOBALDEF int FtpClearCallback(netbuf *nControl)
{
nControl->idlecb = NULL;
nControl->idlearg = NULL;
nControl->idletime.tv_sec = 0;
nControl->idletime.tv_usec = 0;
nControl->cbbytes = 0;
return 1;
}
/*
* FtpOptions - change connection options
*
* returns 1 if successful, 0 on error
*/
GLOBALDEF int FtpOptions(int opt, long val, netbuf *nControl)
{
int v,rv=0;
switch (opt)
{
case FTPLIB_CONNMODE:
v = (int) val;
if ((v == FTPLIB_PASSIVE) || (v == FTPLIB_PORT))
{
nControl->cmode = v;
rv = 1;
}
break;
case FTPLIB_CALLBACK:
nControl->idlecb = (FtpCallback) val;
rv = 1;
break;
case FTPLIB_IDLETIME:
v = (int) val;
rv = 1;
nControl->idletime.tv_sec = v / 1000;
nControl->idletime.tv_usec = (v % 1000) * 1000;
break;
case FTPLIB_CALLBACKARG:
rv = 1;
nControl->idlearg = (void *) val;
break;
case FTPLIB_CALLBACKBYTES:
rv = 1;
nControl->cbbytes = (int) val;
break;
}
return rv;
}
/*
* FtpSendCmd - send a command and wait for expected response
*
* return 1 if proper response received, 0 otherwise
*/
static int FtpSendCmd(const char *cmd, char expresp, netbuf *nControl)
{
char buf[TMP_BUFSIZ];
if (nControl->dir != FTPLIB_CONTROL)
return 0;
if (ftplib_debug > 2)
fprintf(stderr,"%s\n",cmd);
if ((strlen(cmd) + 3) > sizeof(buf))
return 0;
sprintf(buf,"%s\r\n",cmd);
if (net_write(nControl->handle,buf,strlen(buf)) <= 0)
{
if (ftplib_debug)
perror("write");
return 0;
}
return readresp(expresp, nControl);
}
/*
* FtpLogin - log in to remote server
*
* return 1 if logged in, 0 otherwise
*/
GLOBALDEF int FtpLogin(const char *user, const char *pass, netbuf *nControl)
{
char tempbuf[64];
if (((strlen(user) + 7) > sizeof(tempbuf)) ||
((strlen(pass) + 7) > sizeof(tempbuf)))
return 0;
sprintf(tempbuf,"USER %s",user);
if (!FtpSendCmd(tempbuf,'3',nControl))
{
if (nControl->response[0] == '2')
return 1;
return 0;
}
sprintf(tempbuf,"PASS %s",pass);
return FtpSendCmd(tempbuf,'2',nControl);
}
/*
* FtpOpenPort - set up data connection
*
* return 1 if successful, 0 otherwise
*/
static int FtpOpenPort(netbuf *nControl, netbuf **nData, int mode, int dir)
{
int sData;
union {
struct sockaddr sa;
struct sockaddr_in in;
} sin;
struct linger lng = { 0, 0 };
unsigned int l;
int on=1;
netbuf *ctrl;
char *cp;
unsigned int v[6];
char buf[TMP_BUFSIZ];
if (nControl->dir != FTPLIB_CONTROL)
return -1;
if ((dir != FTPLIB_READ) && (dir != FTPLIB_WRITE))
{
sprintf(nControl->response, "Invalid direction %d\n", dir);
return -1;
}
if ((mode != FTPLIB_ASCII) && (mode != FTPLIB_IMAGE))
{
sprintf(nControl->response, "Invalid mode %c\n", mode);
return -1;
}
l = sizeof(sin);
if (nControl->cmode == FTPLIB_PASSIVE)
{
memset(&sin, 0, l);
sin.in.sin_family = AF_INET;
if (!FtpSendCmd("PASV",'2',nControl))
return -1;
cp = strchr(nControl->response,'(');
if (cp == NULL)
return -1;
cp++;
sscanf(cp,"%u,%u,%u,%u,%u,%u",&v[2],&v[3],&v[4],&v[5],&v[0],&v[1]);
sin.sa.sa_data[2] = v[2];
sin.sa.sa_data[3] = v[3];
sin.sa.sa_data[4] = v[4];
sin.sa.sa_data[5] = v[5];
sin.sa.sa_data[0] = v[0];
sin.sa.sa_data[1] = v[1];
}
else
{
if (getsockname(nControl->handle, &sin.sa, (int*)&l) < 0)
{
if (ftplib_debug)
perror("getsockname");
return -1;
}
}
sData = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if (sData == -1)
{
if (ftplib_debug)
perror("socket");
return -1;
}
if (setsockopt(sData,SOL_SOCKET,SO_REUSEADDR,
SETSOCKOPT_OPTVAL_TYPE &on,sizeof(on)) == -1)
{
if (ftplib_debug)
perror("setsockopt");
net_close(sData);
return -1;
}
if (setsockopt(sData,SOL_SOCKET,SO_LINGER,
SETSOCKOPT_OPTVAL_TYPE &lng,sizeof(lng)) == -1)
{
if (ftplib_debug)
perror("setsockopt");
net_close(sData);
return -1;
}
if (nControl->cmode == FTPLIB_PASSIVE)
{
if (connect(sData, &sin.sa, sizeof(sin.sa)) == -1)
{
if (ftplib_debug)
perror("connect");
net_close(sData);
return -1;
}
}
else
{
sin.in.sin_port = 0;
if (bind(sData, &sin.sa, sizeof(sin)) == -1)
{
if (ftplib_debug)
perror("bind");
net_close(sData);
return -1;
}
if (listen(sData, 1) < 0)
{
if (ftplib_debug)
perror("listen");
net_close(sData);
return -1;
}
if (getsockname(sData, &sin.sa, (int*)&l) < 0)
return -1;
sprintf(buf, "PORT %d,%d,%d,%d,%d,%d",
(unsigned char) sin.sa.sa_data[2],
(unsigned char) sin.sa.sa_data[3],
(unsigned char) sin.sa.sa_data[4],
(unsigned char) sin.sa.sa_data[5],
(unsigned char) sin.sa.sa_data[0],
(unsigned char) sin.sa.sa_data[1]);
if (!FtpSendCmd(buf,'2',nControl))
{
net_close(sData);
return -1;
}
}
ctrl = (netbuf*)calloc(1,sizeof(netbuf));
if (ctrl == NULL)
{
if (ftplib_debug)
perror("calloc");
net_close(sData);
return -1;
}
if ((mode == 'A') && ((ctrl->buf = (char*)malloc(FTPLIB_BUFSIZ)) == NULL))
{
if (ftplib_debug)
perror("calloc");
net_close(sData);
free(ctrl);
return -1;
}
ctrl->handle = sData;
ctrl->dir = dir;
ctrl->idletime = nControl->idletime;
ctrl->idlearg = nControl->idlearg;
ctrl->xfered = 0;
ctrl->xfered1 = 0;