nx_storage_sdk  1.0
Storage SDK
ftplib.h
1 /***************************************************************************
2  ftplib.h - description
3  -------------------
4  begin : Son Jul 27 2003
5  copyright : (C) 2013 by magnus kulke
6  email : mkulke@gmail.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU Lesser General Public License as *
13  * published by the Free Software Foundation; either version 2.1 of the *
14  * License, or (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 /***************************************************************************
19  * Note: ftplib, on which ftplibpp was originally based upon used to be *
20  * licensed as GPL 2.0 software, as of Jan. 26th 2013 its author Thomas *
21  * Pfau allowed the distribution of ftplib via LGPL. Thus the license of *
22  * ftplibpp changed aswell. *
23  ***************************************************************************/
24 
25 #ifndef FTPLIB_H
26 #define FTPLIB_H
27 
28 #if defined(_WIN32)
29 
30 #if BUILDING_DLL
31 # define DLLIMPORT __declspec (dllexport)
32 #else /* Not BUILDING_DLL */
33 # define DLLIMPORT __declspec (dllimport)
34 #endif /* Not BUILDING_DLL */
35 
36 #include <winsock2.h>
37 #include <windows.h>
38 #include <time.h>
39 
40 #define fseeko64 fseek
41 #define fopen64 fopen
42 
43 #endif
44 
45 #ifndef _WIN32
46 #include <unistd.h>
47 #include <sys/time.h>
48 #endif
49 
50 #ifdef NOLFS
51 #define off64_t long
52 #endif
53 
54 #if defined(__APPLE__)
55 #define off64_t __darwin_off_t
56 #define fseeko64 fseeko
57 #define fopen64 fopen
58 #endif
59 
60 //SSL
61 typedef struct ssl_st SSL;
62 typedef struct ssl_ctx_st SSL_CTX;
63 typedef struct bio_st BIO;
64 typedef struct x509_st X509;
65 
66 #include <sys/types.h>
67 
68 #ifndef _FTPLIB_SSL_CLIENT_METHOD_
69 #define _FTPLIB_SSL_CLIENT_METHOD_ TLSv1_2_client_method
70 #endif
71 
72 using namespace std;
73 
74 //SSL
75 typedef struct ssl_st SSL;
76 typedef struct ssl_ctx_st SSL_CTX;
77 typedef struct bio_st BIO;
78 typedef struct x509_st X509;
79 
84 typedef int (*FtpCallbackXfer)(off64_t xfered, void *arg);
85 typedef int (*FtpCallbackIdle)(void *arg);
86 typedef void (*FtpCallbackLog)(char *str, void* arg, bool out);
87 //SSL
88 typedef bool (*FtpCallbackCert)(void *arg, X509 *cert);
89 
90 
91 struct ftphandle {
92  char *cput,*cget;
93  int handle;
94  int cavail,cleft;
95  char *buf;
96  int dir;
97  ftphandle *ctrl;
98  int cmode;
99  struct timeval idletime;
100  FtpCallbackXfer xfercb;
101  FtpCallbackIdle idlecb;
102  FtpCallbackLog logcb;
103  void *cbarg;
104  off64_t xfered;
105  off64_t cbbytes;
106  off64_t xfered1;
107  char response[256];
108  //SSL
109  SSL* ssl;
110  SSL_CTX* ctx;
111  BIO* sbio;
112  int tlsctrl;
113  int tlsdata;
114  FtpCallbackCert certcb;
115 
116  off64_t offset;
117  bool correctpasv;
118 };
119 
120 #if defined(_WIN32)
121 class DLLIMPORT ftplib {
122 #else
123 class ftplib {
124 #endif
125 public:
126 
127  enum accesstype
128  {
129  dir = 1,
130  dirverbose,
131  fileread,
132  filewrite,
133  filereadappend,
134  filewriteappend
135  };
136 
137  enum transfermode
138  {
139  ascii = 'A',
140  image = 'I'
141  };
142 
143  enum connmode
144  {
145  pasv = 1,
146  port
147  };
148 
149  enum fxpmethod
150  {
151  defaultfxp = 0,
152  alternativefxp
153  };
154 
155  enum dataencryption
156  {
157  unencrypted = 0,
158  secure
159  };
160 
161  ftplib();
162  ~ftplib();
163  char* LastResponse();
164  int Connect(const char *host);
165  int Login(const char *user, const char *pass);
166  int Site(const char *cmd);
167  int Raw(const char *cmd);
168  int SysType(char *buf, int max);
169  int Mkdir(const char *path);
170  int Chdir(const char *path);
171  int Cdup();
172  int Rmdir(const char *path);
173  int Pwd(char *path, int max);
174  int Nlst(const char *outputfile, const char *path);
175  int Dir(const char *outputfile, const char *path);
176  int Size(const char *path, int *size, transfermode mode);
177  int ModDate(const char *path, char *dt, int max);
178  int Get(const char *outputfile, const char *path, transfermode mode, off64_t offset = 0);
179  int Put(const char *inputfile, const char *path, transfermode mode, off64_t offset = 0);
180  int Rename(const char *src, const char *dst);
181  int Delete(const char *path);
182  int Quit();
183  void SetCallbackIdleFunction(FtpCallbackIdle pointer);
184  void SetCallbackLogFunction(FtpCallbackLog pointer);
185  void SetCallbackXferFunction(FtpCallbackXfer pointer);
186  void SetCallbackArg(void *arg);
187  void SetCallbackBytes(off64_t bytes);
188  void SetCorrectPasv(bool b) { mp_ftphandle->correctpasv = b; };
189  void SetCallbackIdletime(int time);
190  void SetConnmode(connmode mode);
191  static int Fxp(ftplib* src, ftplib* dst, const char *pathSrc, const char *pathDst, transfermode mode, fxpmethod method);
192  ftphandle* RawOpen(const char *path, accesstype type, transfermode mode);
193  int RawClose(ftphandle* handle);
194  int RawWrite(void* buf, int len, ftphandle* handle);
195  int RawRead(void* buf, int max, ftphandle* handle);
196  int TestControlConnection();
197  // SSL
198  int SetDataEncryption(dataencryption enc);
199  int NegotiateEncryption();
200  void SetCallbackCertFunction(FtpCallbackCert pointer);
201 
202 private:
203  ftphandle* mp_ftphandle;
204 
205  int FtpXfer(const char *localfile, const char *path, ftphandle *nControl, accesstype type, transfermode mode);
206  int FtpOpenPasv(ftphandle *nControl, ftphandle **nData, transfermode mode, int dir, char *cmd);
207  int FtpSendCmd(const char *cmd, char expresp, ftphandle *nControl);
208  int FtpAcceptConnection(ftphandle *nData, ftphandle *nControl);
209  int FtpOpenPort(ftphandle *nControl, ftphandle **nData, transfermode mode, int dir, char *cmd);
210  int FtpRead(void *buf, int max, ftphandle *nData);
211  int FtpWrite(void *buf, int len, ftphandle *nData);
212  int FtpAccess(const char *path, accesstype type, transfermode mode, ftphandle *nControl, ftphandle **nData);
213  int FtpClose(ftphandle *nData);
214  int socket_wait(ftphandle *ctl);
215  int readline(char *buf,int max,ftphandle *ctl);
216  int writeline(char *buf, int len, ftphandle *nData);
217  int readresp(char c, ftphandle *nControl);
218  void sprint_rest(char *buf, off64_t offset);
219  void ClearHandle();
220  int CorrectPasvResponse(unsigned char *v);
221 };
222 
223 #endif
Definition: ftplib.h:91
Definition: ftplib.h:123