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